You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

370 lines
7.9 KiB

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