event.c 7.8 KB

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