event.c 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351
  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. /* static */
  10. typedef struct {
  11. unsigned long mod;
  12. KeySym keysym;
  13. void (*func)(Arg *arg);
  14. Arg arg;
  15. } Key;
  16. CMDS
  17. KEYS
  18. static unsigned int valid_mask = 255 & ~(NUMLOCKMASK | LockMask);
  19. static void
  20. movemouse(Client *c)
  21. {
  22. int x1, y1, ocx, ocy, di;
  23. unsigned int dui;
  24. Window dummy;
  25. XEvent ev;
  26. ocx = c->x;
  27. ocy = c->y;
  28. if(XGrabPointer(dpy, root, False, MouseMask, GrabModeAsync, GrabModeAsync,
  29. None, cursor[CurMove], CurrentTime) != GrabSuccess)
  30. return;
  31. XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
  32. for(;;) {
  33. XMaskEvent(dpy, MouseMask | ExposureMask, &ev);
  34. switch (ev.type) {
  35. default: break;
  36. case Expose:
  37. handler[Expose](&ev);
  38. break;
  39. case MotionNotify:
  40. XSync(dpy, False);
  41. c->x = ocx + (ev.xmotion.x - x1);
  42. c->y = ocy + (ev.xmotion.y - y1);
  43. resize(c, False, TopLeft);
  44. break;
  45. case ButtonRelease:
  46. XUngrabPointer(dpy, CurrentTime);
  47. return;
  48. }
  49. }
  50. }
  51. static void
  52. resizemouse(Client *c)
  53. {
  54. int ocx, ocy;
  55. Corner sticky;
  56. XEvent ev;
  57. ocx = c->x;
  58. ocy = c->y;
  59. if(XGrabPointer(dpy, root, False, MouseMask, GrabModeAsync, GrabModeAsync,
  60. None, cursor[CurResize], CurrentTime) != GrabSuccess)
  61. return;
  62. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w, c->h);
  63. for(;;) {
  64. XMaskEvent(dpy, MouseMask | ExposureMask, &ev);
  65. switch(ev.type) {
  66. default: break;
  67. case Expose:
  68. handler[Expose](&ev);
  69. break;
  70. case MotionNotify:
  71. XSync(dpy, False);
  72. c->w = abs(ocx - ev.xmotion.x);
  73. c->h = abs(ocy - ev.xmotion.y);
  74. c->x = (ocx <= ev.xmotion.x) ? ocx : ocx - c->w;
  75. c->y = (ocy <= ev.xmotion.y) ? ocy : ocy - c->h;
  76. if(ocx <= ev.xmotion.x)
  77. sticky = (ocy <= ev.xmotion.y) ? TopLeft : BotLeft;
  78. else
  79. sticky = (ocy <= ev.xmotion.y) ? TopRight : BotRight;
  80. resize(c, True, sticky);
  81. break;
  82. case ButtonRelease:
  83. XUngrabPointer(dpy, CurrentTime);
  84. return;
  85. }
  86. }
  87. }
  88. static void
  89. buttonpress(XEvent *e)
  90. {
  91. int x;
  92. Arg a;
  93. Client *c;
  94. XButtonPressedEvent *ev = &e->xbutton;
  95. if(barwin == ev->window) {
  96. switch(ev->button) {
  97. default:
  98. x = 0;
  99. for(a.i = 0; a.i < TLast; a.i++) {
  100. x += textw(tags[a.i]);
  101. if(ev->x < x) {
  102. view(&a);
  103. break;
  104. }
  105. }
  106. break;
  107. case Button4:
  108. a.i = (tsel + 1 < TLast) ? tsel + 1 : 0;
  109. view(&a);
  110. break;
  111. case Button5:
  112. a.i = (tsel - 1 >= 0) ? tsel - 1 : TLast - 1;
  113. view(&a);
  114. break;
  115. }
  116. }
  117. else if((c = getclient(ev->window))) {
  118. focus(c);
  119. switch(ev->button) {
  120. default:
  121. break;
  122. case Button1:
  123. if(!c->ismax && (arrange == dofloat || c->isfloat)) {
  124. higher(c);
  125. movemouse(c);
  126. }
  127. break;
  128. case Button2:
  129. lower(c);
  130. break;
  131. case Button3:
  132. if(!c->ismax && (arrange == dofloat || c->isfloat)) {
  133. higher(c);
  134. resizemouse(c);
  135. }
  136. break;
  137. }
  138. }
  139. }
  140. static void
  141. configurerequest(XEvent *e)
  142. {
  143. Client *c;
  144. XConfigureRequestEvent *ev = &e->xconfigurerequest;
  145. XWindowChanges wc;
  146. ev->value_mask &= ~CWSibling;
  147. if((c = getclient(ev->window))) {
  148. gravitate(c, True);
  149. if(ev->value_mask & CWX)
  150. c->x = ev->x;
  151. if(ev->value_mask & CWY)
  152. c->y = ev->y;
  153. if(ev->value_mask & CWWidth)
  154. c->w = ev->width;
  155. if(ev->value_mask & CWHeight)
  156. c->h = ev->height;
  157. if(ev->value_mask & CWBorderWidth)
  158. c->border = 1;
  159. gravitate(c, False);
  160. resize(c, True, TopLeft);
  161. }
  162. wc.x = ev->x;
  163. wc.y = ev->y;
  164. wc.width = ev->width;
  165. wc.height = ev->height;
  166. wc.border_width = 1;
  167. wc.sibling = None;
  168. wc.stack_mode = Above;
  169. ev->value_mask &= ~CWStackMode;
  170. ev->value_mask |= CWBorderWidth;
  171. XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
  172. XSync(dpy, False);
  173. }
  174. static void
  175. destroynotify(XEvent *e)
  176. {
  177. Client *c;
  178. XDestroyWindowEvent *ev = &e->xdestroywindow;
  179. if((c = getclient(ev->window)))
  180. unmanage(c);
  181. }
  182. static void
  183. enternotify(XEvent *e)
  184. {
  185. Client *c;
  186. XCrossingEvent *ev = &e->xcrossing;
  187. if(ev->detail == NotifyInferior)
  188. return;
  189. if((c = getclient(ev->window)))
  190. focus(c);
  191. else if(ev->window == root)
  192. issel = True;
  193. }
  194. static void
  195. expose(XEvent *e)
  196. {
  197. Client *c;
  198. XExposeEvent *ev = &e->xexpose;
  199. if(ev->count == 0) {
  200. if(barwin == ev->window)
  201. drawstatus();
  202. else if((c = getctitle(ev->window)))
  203. drawtitle(c);
  204. }
  205. }
  206. static void
  207. keypress(XEvent *e)
  208. {
  209. static unsigned int len = sizeof(key) / sizeof(key[0]);
  210. unsigned int i;
  211. KeySym keysym;
  212. XKeyEvent *ev = &e->xkey;
  213. ev->state &= valid_mask;
  214. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  215. for(i = 0; i < len; i++)
  216. if((keysym == key[i].keysym) && ((key[i].mod & valid_mask) == ev->state)) {
  217. if(key[i].func)
  218. key[i].func(&key[i].arg);
  219. return;
  220. }
  221. }
  222. static void
  223. leavenotify(XEvent *e)
  224. {
  225. XCrossingEvent *ev = &e->xcrossing;
  226. if((ev->window == root) && !ev->same_screen)
  227. issel = True;
  228. }
  229. static void
  230. maprequest(XEvent *e)
  231. {
  232. static XWindowAttributes wa;
  233. XMapRequestEvent *ev = &e->xmaprequest;
  234. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  235. return;
  236. if(wa.override_redirect) {
  237. XSelectInput(dpy, ev->window,
  238. (StructureNotifyMask | PropertyChangeMask));
  239. return;
  240. }
  241. if(!getclient(ev->window))
  242. manage(ev->window, &wa);
  243. }
  244. static void
  245. propertynotify(XEvent *e)
  246. {
  247. Client *c;
  248. Window trans;
  249. XPropertyEvent *ev = &e->xproperty;
  250. if(ev->state == PropertyDelete)
  251. return; /* ignore */
  252. if((c = getclient(ev->window))) {
  253. if(ev->atom == wmatom[WMProtocols]) {
  254. c->proto = getproto(c->win);
  255. return;
  256. }
  257. switch (ev->atom) {
  258. default: break;
  259. case XA_WM_TRANSIENT_FOR:
  260. XGetTransientForHint(dpy, c->win, &trans);
  261. if(!c->isfloat && (c->isfloat = (trans != 0)))
  262. arrange(NULL);
  263. break;
  264. case XA_WM_NORMAL_HINTS:
  265. setsize(c);
  266. break;
  267. }
  268. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  269. settitle(c);
  270. drawtitle(c);
  271. }
  272. }
  273. }
  274. static void
  275. unmapnotify(XEvent *e)
  276. {
  277. Client *c;
  278. XUnmapEvent *ev = &e->xunmap;
  279. if((c = getclient(ev->window)))
  280. unmanage(c);
  281. }
  282. /* extern */
  283. void (*handler[LASTEvent]) (XEvent *) = {
  284. [ButtonPress] = buttonpress,
  285. [ConfigureRequest] = configurerequest,
  286. [DestroyNotify] = destroynotify,
  287. [EnterNotify] = enternotify,
  288. [LeaveNotify] = leavenotify,
  289. [Expose] = expose,
  290. [KeyPress] = keypress,
  291. [MapRequest] = maprequest,
  292. [PropertyNotify] = propertynotify,
  293. [UnmapNotify] = unmapnotify
  294. };
  295. void
  296. grabkeys()
  297. {
  298. static unsigned int len = sizeof(key) / sizeof(key[0]);
  299. unsigned int i;
  300. KeyCode code;
  301. for(i = 0; i < len; i++) {
  302. code = XKeysymToKeycode(dpy, key[i].keysym);
  303. XUngrabKey(dpy, code, key[i].mod, root);
  304. XUngrabKey(dpy, code, key[i].mod | NUMLOCKMASK, root);
  305. XUngrabKey(dpy, code, key[i].mod | NUMLOCKMASK | LockMask, root);
  306. XGrabKey(dpy, code, key[i].mod, root, True,
  307. GrabModeAsync, GrabModeAsync);
  308. XGrabKey(dpy, code, key[i].mod | NUMLOCKMASK, root, True,
  309. GrabModeAsync, GrabModeAsync);
  310. XGrabKey(dpy, code, key[i].mod | NUMLOCKMASK | LockMask, root, True,
  311. GrabModeAsync, GrabModeAsync);
  312. }
  313. }