You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

387 lines
8.4 KiB

18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
  1. /*
  2. * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
  3. * See LICENSE file for license details.
  4. */
  5. #include "dwm.h"
  6. #include <stdlib.h>
  7. #include <X11/keysym.h>
  8. #include <X11/Xatom.h>
  9. #define ButtonMask (ButtonPressMask | ButtonReleaseMask)
  10. #define MouseMask (ButtonMask | PointerMotionMask)
  11. /* CUSTOMIZE */
  12. typedef struct {
  13. unsigned long mod;
  14. KeySym keysym;
  15. void (*func)(Arg *arg);
  16. Arg arg;
  17. } Key;
  18. const char *browse[] = { "firefox", NULL };
  19. const char *gimp[] = { "gimp", NULL };
  20. const char *term[] = {
  21. "urxvtc", "-tr", "+sb", "-bg", "black", "-fg", "white", "-cr", "white",
  22. "-fn", "-*-terminus-medium-*-*-*-13-*-*-*-*-*-iso10646-*", NULL
  23. };
  24. const char *xlock[] = { "xlock", NULL };
  25. static Key key[] = {
  26. /* modifier key function arguments */
  27. { ControlMask, XK_0, appendtag, { .i = Tscratch } },
  28. { ControlMask, XK_1, appendtag, { .i = Tdev } },
  29. { ControlMask, XK_2, appendtag, { .i = Twww } },
  30. { ControlMask, XK_3, appendtag, { .i = Twork } },
  31. { MODKEY, XK_0, view, { .i = Tscratch } },
  32. { MODKEY, XK_1, view, { .i = Tdev } },
  33. { MODKEY, XK_2, view, { .i = Twww } },
  34. { MODKEY, XK_3, view, { .i = Twork } },
  35. { MODKEY, XK_j, focusnext, { 0 } },
  36. { MODKEY, XK_k, focusprev, { 0 } },
  37. { MODKEY, XK_m, maximize, { 0 } },
  38. { MODKEY, XK_space, dotile, { 0 } },
  39. { MODKEY, XK_Return, zoom, { 0 } },
  40. { ControlMask|ShiftMask,XK_0, heretag, { .i = Tscratch } },
  41. { ControlMask|ShiftMask,XK_1, heretag, { .i = Tdev } },
  42. { ControlMask|ShiftMask,XK_2, heretag, { .i = Twww } },
  43. { ControlMask|ShiftMask,XK_3, heretag, { .i = Twork } },
  44. { MODKEY|ShiftMask, XK_0, replacetag, { .i = Tscratch } },
  45. { MODKEY|ShiftMask, XK_1, replacetag, { .i = Tdev } },
  46. { MODKEY|ShiftMask, XK_2, replacetag, { .i = Twww } },
  47. { MODKEY|ShiftMask, XK_3, replacetag, { .i = Twork } },
  48. { MODKEY|ShiftMask, XK_c, killclient, { 0 } },
  49. { MODKEY|ShiftMask, XK_g, spawn, { .argv = gimp } },
  50. { MODKEY|ShiftMask, XK_l, spawn, { .argv = xlock } },
  51. { MODKEY|ShiftMask, XK_q, quit, { 0 } },
  52. { MODKEY|ShiftMask, XK_space, dofloat, { 0 } },
  53. { MODKEY|ShiftMask, XK_w, spawn, { .argv = browse } },
  54. { MODKEY|ShiftMask, XK_Return, spawn, { .argv = term } },
  55. };
  56. /* static */
  57. static void
  58. movemouse(Client *c)
  59. {
  60. XEvent ev;
  61. int x1, y1, ocx, ocy, di;
  62. unsigned int dui;
  63. Window dummy;
  64. ocx = c->x;
  65. ocy = c->y;
  66. if(XGrabPointer(dpy, root, False, MouseMask, GrabModeAsync, GrabModeAsync,
  67. None, cursor[CurMove], CurrentTime) != GrabSuccess)
  68. return;
  69. XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
  70. for(;;) {
  71. XMaskEvent(dpy, MouseMask | ExposureMask, &ev);
  72. switch (ev.type) {
  73. default: break;
  74. case Expose:
  75. handler[Expose](&ev);
  76. break;
  77. case MotionNotify:
  78. XSync(dpy, False);
  79. c->x = ocx + (ev.xmotion.x - x1);
  80. c->y = ocy + (ev.xmotion.y - y1);
  81. resize(c, False, TopLeft);
  82. break;
  83. case ButtonRelease:
  84. XUngrabPointer(dpy, CurrentTime);
  85. return;
  86. }
  87. }
  88. }
  89. static void
  90. resizemouse(Client *c)
  91. {
  92. XEvent ev;
  93. int ocx, ocy;
  94. Corner sticky;
  95. ocx = c->x;
  96. ocy = c->y;
  97. if(XGrabPointer(dpy, root, False, MouseMask, GrabModeAsync, GrabModeAsync,
  98. None, cursor[CurResize], CurrentTime) != GrabSuccess)
  99. return;
  100. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w, c->h);
  101. for(;;) {
  102. XMaskEvent(dpy, MouseMask | ExposureMask, &ev);
  103. switch(ev.type) {
  104. default: break;
  105. case Expose:
  106. handler[Expose](&ev);
  107. break;
  108. case MotionNotify:
  109. XSync(dpy, False);
  110. c->w = abs(ocx - ev.xmotion.x);
  111. c->h = abs(ocy - ev.xmotion.y);
  112. c->x = (ocx <= ev.xmotion.x) ? ocx : ocx - c->w;
  113. c->y = (ocy <= ev.xmotion.y) ? ocy : ocy - c->h;
  114. if(ocx <= ev.xmotion.x)
  115. sticky = (ocy <= ev.xmotion.y) ? TopLeft : BotLeft;
  116. else
  117. sticky = (ocy <= ev.xmotion.y) ? TopRight : BotRight;
  118. resize(c, True, sticky);
  119. break;
  120. case ButtonRelease:
  121. XUngrabPointer(dpy, CurrentTime);
  122. return;
  123. }
  124. }
  125. }
  126. static void
  127. buttonpress(XEvent *e)
  128. {
  129. int x;
  130. Arg a;
  131. XButtonPressedEvent *ev = &e->xbutton;
  132. Client *c;
  133. if(barwin == ev->window) {
  134. switch(ev->button) {
  135. default:
  136. x = 0;
  137. for(a.i = 0; a.i < TLast; a.i++) {
  138. x += textw(tags[a.i]);
  139. if(ev->x < x) {
  140. view(&a);
  141. break;
  142. }
  143. }
  144. break;
  145. case Button4:
  146. a.i = (tsel + 1 < TLast) ? tsel + 1 : 0;
  147. view(&a);
  148. break;
  149. case Button5:
  150. a.i = (tsel - 1 >= 0) ? tsel - 1 : TLast - 1;
  151. view(&a);
  152. break;
  153. }
  154. }
  155. else if((c = getclient(ev->window))) {
  156. switch(ev->button) {
  157. default:
  158. break;
  159. case Button1:
  160. if(arrange == dotile && !c->isfloat) {
  161. if((ev->state & ControlMask) && (ev->button == Button1))
  162. zoom(NULL);
  163. }
  164. else {
  165. higher(c);
  166. movemouse(c);
  167. }
  168. break;
  169. case Button2:
  170. lower(c);
  171. break;
  172. case Button3:
  173. if(arrange == dofloat || c->isfloat) {
  174. higher(c);
  175. resizemouse(c);
  176. }
  177. break;
  178. }
  179. }
  180. }
  181. static void
  182. configurerequest(XEvent *e)
  183. {
  184. XConfigureRequestEvent *ev = &e->xconfigurerequest;
  185. XWindowChanges wc;
  186. Client *c;
  187. ev->value_mask &= ~CWSibling;
  188. if((c = getclient(ev->window))) {
  189. gravitate(c, True);
  190. if(ev->value_mask & CWX)
  191. c->x = ev->x;
  192. if(ev->value_mask & CWY)
  193. c->y = ev->y;
  194. if(ev->value_mask & CWWidth)
  195. c->w = ev->width;
  196. if(ev->value_mask & CWHeight)
  197. c->h = ev->height;
  198. if(ev->value_mask & CWBorderWidth)
  199. c->border = 1;
  200. gravitate(c, False);
  201. resize(c, True, TopLeft);
  202. }
  203. wc.x = ev->x;
  204. wc.y = ev->y;
  205. wc.width = ev->width;
  206. wc.height = ev->height;
  207. wc.border_width = 1;
  208. wc.sibling = None;
  209. wc.stack_mode = Above;
  210. ev->value_mask &= ~CWStackMode;
  211. ev->value_mask |= CWBorderWidth;
  212. XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
  213. XSync(dpy, False);
  214. }
  215. static void
  216. destroynotify(XEvent *e)
  217. {
  218. Client *c;
  219. XDestroyWindowEvent *ev = &e->xdestroywindow;
  220. if((c = getclient(ev->window)))
  221. unmanage(c);
  222. }
  223. static void
  224. enternotify(XEvent *e)
  225. {
  226. XCrossingEvent *ev = &e->xcrossing;
  227. Client *c;
  228. if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
  229. return;
  230. if((c = getclient(ev->window)))
  231. focus(c);
  232. else if(ev->window == root)
  233. issel = True;
  234. }
  235. static void
  236. expose(XEvent *e)
  237. {
  238. XExposeEvent *ev = &e->xexpose;
  239. Client *c;
  240. if(ev->count == 0) {
  241. if(barwin == ev->window)
  242. drawstatus();
  243. else if((c = getctitle(ev->window)))
  244. drawtitle(c);
  245. }
  246. }
  247. static void
  248. keypress(XEvent *e)
  249. {
  250. XKeyEvent *ev = &e->xkey;
  251. static unsigned int len = key ? sizeof(key) / sizeof(key[0]) : 0;
  252. unsigned int i;
  253. KeySym keysym;
  254. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  255. for(i = 0; i < len; i++)
  256. if((keysym == key[i].keysym) && (key[i].mod == ev->state)) {
  257. if(key[i].func)
  258. key[i].func(&key[i].arg);
  259. return;
  260. }
  261. }
  262. static void
  263. leavenotify(XEvent *e)
  264. {
  265. XCrossingEvent *ev = &e->xcrossing;
  266. if((ev->window == root) && !ev->same_screen)
  267. issel = True;
  268. }
  269. static void
  270. maprequest(XEvent *e)
  271. {
  272. XMapRequestEvent *ev = &e->xmaprequest;
  273. static XWindowAttributes wa;
  274. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  275. return;
  276. if(wa.override_redirect) {
  277. XSelectInput(dpy, ev->window,
  278. (StructureNotifyMask | PropertyChangeMask));
  279. return;
  280. }
  281. if(!getclient(ev->window))
  282. manage(ev->window, &wa);
  283. }
  284. static void
  285. propertynotify(XEvent *e)
  286. {
  287. XPropertyEvent *ev = &e->xproperty;
  288. Window trans;
  289. Client *c;
  290. if(ev->state == PropertyDelete)
  291. return; /* ignore */
  292. if((c = getclient(ev->window))) {
  293. if(ev->atom == wmatom[WMProtocols]) {
  294. c->proto = getproto(c->win);
  295. return;
  296. }
  297. switch (ev->atom) {
  298. default: break;
  299. case XA_WM_TRANSIENT_FOR:
  300. XGetTransientForHint(dpy, c->win, &trans);
  301. if(!c->isfloat && (c->isfloat = (trans != 0)))
  302. arrange(NULL);
  303. break;
  304. case XA_WM_NORMAL_HINTS:
  305. setsize(c);
  306. break;
  307. }
  308. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  309. settitle(c);
  310. drawtitle(c);
  311. }
  312. }
  313. }
  314. static void
  315. unmapnotify(XEvent *e)
  316. {
  317. Client *c;
  318. XUnmapEvent *ev = &e->xunmap;
  319. if((c = getclient(ev->window)))
  320. unmanage(c);
  321. }
  322. /* extern */
  323. void (*handler[LASTEvent]) (XEvent *) = {
  324. [ButtonPress] = buttonpress,
  325. [ConfigureRequest] = configurerequest,
  326. [DestroyNotify] = destroynotify,
  327. [EnterNotify] = enternotify,
  328. [LeaveNotify] = leavenotify,
  329. [Expose] = expose,
  330. [KeyPress] = keypress,
  331. [MapRequest] = maprequest,
  332. [PropertyNotify] = propertynotify,
  333. [UnmapNotify] = unmapnotify
  334. };
  335. void
  336. grabkeys()
  337. {
  338. static unsigned int len = key ? sizeof(key) / sizeof(key[0]) : 0;
  339. unsigned int i;
  340. KeyCode code;
  341. for(i = 0; i < len; i++) {
  342. code = XKeysymToKeycode(dpy, key[i].keysym);
  343. XUngrabKey(dpy, code, key[i].mod, root);
  344. XGrabKey(dpy, code, key[i].mod, root, True,
  345. GrabModeAsync, GrabModeAsync);
  346. }
  347. }