event.c 8.0 KB

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