event.c 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380
  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 <unistd.h>
  8. #include <X11/keysym.h>
  9. #include <X11/Xatom.h>
  10. /* static */
  11. typedef struct {
  12. unsigned long mod;
  13. KeySym keysym;
  14. void (*func)(Arg *arg);
  15. Arg arg;
  16. } Key;
  17. KEYS
  18. #define CLEANMASK(mask) (mask & ~(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. int nw, nh;
  56. Corner sticky;
  57. XEvent ev;
  58. ocx = c->x;
  59. ocy = c->y;
  60. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  61. None, cursor[CurResize], CurrentTime) != GrabSuccess)
  62. return;
  63. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w, c->h);
  64. for(;;) {
  65. XMaskEvent(dpy, MOUSEMASK | ExposureMask, &ev);
  66. switch(ev.type) {
  67. default: break;
  68. case Expose:
  69. handler[Expose](&ev);
  70. break;
  71. case MotionNotify:
  72. XSync(dpy, False);
  73. if((nw = abs(ocx - ev.xmotion.x)))
  74. c->w = abs(ocx - ev.xmotion.x);
  75. if((nh = abs(ocy - ev.xmotion.y)))
  76. c->h = abs(ocy - ev.xmotion.y);
  77. c->x = (ocx <= ev.xmotion.x) ? ocx : ocx - c->w;
  78. c->y = (ocy <= ev.xmotion.y) ? ocy : ocy - c->h;
  79. if(ocx <= ev.xmotion.x)
  80. sticky = (ocy <= ev.xmotion.y) ? TopLeft : BotLeft;
  81. else
  82. sticky = (ocy <= ev.xmotion.y) ? TopRight : BotRight;
  83. resize(c, True, sticky);
  84. break;
  85. case ButtonRelease:
  86. XUngrabPointer(dpy, CurrentTime);
  87. return;
  88. }
  89. }
  90. }
  91. static void
  92. buttonpress(XEvent *e)
  93. {
  94. int x;
  95. Arg a;
  96. Client *c;
  97. XButtonPressedEvent *ev = &e->xbutton;
  98. if(barwin == ev->window) {
  99. x = 0;
  100. for(a.i = 0; a.i < ntags; a.i++) {
  101. x += textw(tags[a.i]);
  102. if(ev->x < x) {
  103. if(ev->button == Button3)
  104. toggleview(&a);
  105. else
  106. view(&a);
  107. return;
  108. }
  109. }
  110. }
  111. else if((c = getclient(ev->window))) {
  112. focus(c);
  113. switch(ev->button) {
  114. default:
  115. break;
  116. case Button1:
  117. if(!c->ismax && (arrange == dofloat || c->isfloat)) {
  118. restack(c);
  119. movemouse(c);
  120. }
  121. break;
  122. case Button2:
  123. zoom(NULL);
  124. break;
  125. case Button3:
  126. if(!c->ismax && (arrange == dofloat || c->isfloat)) {
  127. restack(c);
  128. resizemouse(c);
  129. }
  130. break;
  131. }
  132. }
  133. }
  134. static void
  135. configurerequest(XEvent *e)
  136. {
  137. Client *c;
  138. XConfigureRequestEvent *ev = &e->xconfigurerequest;
  139. XEvent synev;
  140. XWindowChanges wc;
  141. unsigned long newmask;
  142. if((c = getclient(ev->window))) {
  143. gravitate(c, True);
  144. if(ev->value_mask & CWX)
  145. c->x = ev->x;
  146. if(ev->value_mask & CWY)
  147. c->y = ev->y;
  148. if(ev->value_mask & CWWidth)
  149. c->w = ev->width;
  150. if(ev->value_mask & CWHeight)
  151. c->h = ev->height;
  152. if(ev->value_mask & CWBorderWidth)
  153. c->border = ev->border_width;
  154. gravitate(c, False);
  155. wc.x = c->x;
  156. wc.y = c->y;
  157. wc.width = c->w;
  158. wc.height = c->h;
  159. newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
  160. if(newmask)
  161. XConfigureWindow(dpy, c->win, newmask, &wc);
  162. else {
  163. synev.type = ConfigureNotify;
  164. synev.xconfigure.display = dpy;
  165. synev.xconfigure.event = c->win;
  166. synev.xconfigure.window = c->win;
  167. synev.xconfigure.x = c->x;
  168. synev.xconfigure.y = c->y;
  169. synev.xconfigure.width = c->w;
  170. synev.xconfigure.height = c->h;
  171. synev.xconfigure.border_width = c->border;
  172. synev.xconfigure.above = None;
  173. /* Send synthetic ConfigureNotify */
  174. XSendEvent(dpy, c->win, True, NoEventMask, &synev);
  175. }
  176. XSync(dpy, False);
  177. if(c->isfloat)
  178. resize(c, False, TopLeft);
  179. else
  180. arrange(NULL);
  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. {
  197. Client *c;
  198. XDestroyWindowEvent *ev = &e->xdestroywindow;
  199. if((c = getclient(ev->window)))
  200. unmanage(c);
  201. }
  202. static void
  203. enternotify(XEvent *e)
  204. {
  205. Client *c;
  206. XCrossingEvent *ev = &e->xcrossing;
  207. if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
  208. return;
  209. if((c = getclient(ev->window)) || (c = getctitle(ev->window)))
  210. focus(c);
  211. else if(ev->window == root) {
  212. issel = True;
  213. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  214. drawall();
  215. }
  216. }
  217. static void
  218. expose(XEvent *e)
  219. {
  220. Client *c;
  221. XExposeEvent *ev = &e->xexpose;
  222. if(ev->count == 0) {
  223. if(barwin == ev->window)
  224. drawstatus();
  225. else if((c = getctitle(ev->window)))
  226. drawtitle(c);
  227. }
  228. }
  229. static void
  230. keypress(XEvent *e)
  231. {
  232. static unsigned int len = sizeof(key) / sizeof(key[0]);
  233. unsigned int i;
  234. KeySym keysym;
  235. XKeyEvent *ev = &e->xkey;
  236. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  237. for(i = 0; i < len; i++) {
  238. if(keysym == key[i].keysym &&
  239. CLEANMASK(key[i].mod) == CLEANMASK(ev->state))
  240. {
  241. if(key[i].func)
  242. key[i].func(&key[i].arg);
  243. return;
  244. }
  245. }
  246. }
  247. static void
  248. leavenotify(XEvent *e)
  249. {
  250. XCrossingEvent *ev = &e->xcrossing;
  251. if((ev->window == root) && !ev->same_screen) {
  252. issel = False;
  253. drawall();
  254. }
  255. }
  256. static void
  257. maprequest(XEvent *e)
  258. {
  259. static XWindowAttributes wa;
  260. XMapRequestEvent *ev = &e->xmaprequest;
  261. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  262. return;
  263. if(wa.override_redirect) {
  264. XSelectInput(dpy, ev->window,
  265. (StructureNotifyMask | PropertyChangeMask));
  266. return;
  267. }
  268. if(!getclient(ev->window))
  269. manage(ev->window, &wa);
  270. }
  271. static void
  272. propertynotify(XEvent *e)
  273. {
  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(NULL);
  290. break;
  291. case XA_WM_NORMAL_HINTS:
  292. setsize(c);
  293. break;
  294. }
  295. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  296. settitle(c);
  297. drawtitle(c);
  298. }
  299. }
  300. }
  301. static void
  302. unmapnotify(XEvent *e)
  303. {
  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. [MapRequest] = maprequest,
  319. [PropertyNotify] = propertynotify,
  320. [UnmapNotify] = unmapnotify
  321. };
  322. void
  323. grabkeys()
  324. {
  325. static unsigned int len = sizeof(key) / sizeof(key[0]);
  326. unsigned int i;
  327. KeyCode code;
  328. while(XGrabKeyboard(dpy, root, True, GrabModeAsync,
  329. GrabModeAsync, CurrentTime) != GrabSuccess)
  330. usleep(1000);
  331. XUngrabKeyboard(dpy, CurrentTime);
  332. for(i = 0; i < len; i++) {
  333. code = XKeysymToKeycode(dpy, key[i].keysym);
  334. XGrabKey(dpy, code, key[i].mod, root, True,
  335. GrabModeAsync, GrabModeAsync);
  336. XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
  337. GrabModeAsync, GrabModeAsync);
  338. XGrabKey(dpy, code, key[i].mod | NUMLOCKMASK, root, True,
  339. GrabModeAsync, GrabModeAsync);
  340. XGrabKey(dpy, code, key[i].mod | NUMLOCKMASK | LockMask, root, True,
  341. GrabModeAsync, GrabModeAsync);
  342. }
  343. }