event.c 8.1 KB

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