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.

363 lines
7.8 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. a.i = -1;
  132. setlayout(a);
  133. break;
  134. }
  135. }
  136. else if((c = getclient(ev->window))) {
  137. focus(c);
  138. if(CLEANMASK(ev->state) != MODKEY)
  139. return;
  140. if(ev->button == Button1 && (lt->arrange == versatile || c->isversatile)) {
  141. restack();
  142. movemouse(c);
  143. }
  144. else if(ev->button == Button2)
  145. zoom(a);
  146. else if(ev->button == Button3
  147. && (lt->arrange == versatile || c->isversatile) && !c->isfixed)
  148. {
  149. restack();
  150. resizemouse(c);
  151. }
  152. }
  153. }
  154. static void
  155. configurerequest(XEvent *e) {
  156. Client *c;
  157. XConfigureRequestEvent *ev = &e->xconfigurerequest;
  158. XWindowChanges wc;
  159. if((c = getclient(ev->window))) {
  160. c->ismax = False;
  161. if(ev->value_mask & CWBorderWidth)
  162. c->border = ev->border_width;
  163. if(c->isfixed || c->isversatile || (lt->arrange == versatile)) {
  164. if(ev->value_mask & CWX)
  165. c->x = ev->x;
  166. if(ev->value_mask & CWY)
  167. c->y = ev->y;
  168. if(ev->value_mask & CWWidth)
  169. c->w = ev->width;
  170. if(ev->value_mask & CWHeight)
  171. c->h = ev->height;
  172. if((ev->value_mask & (CWX | CWY))
  173. && !(ev->value_mask & (CWWidth | CWHeight)))
  174. configure(c);
  175. if(isvisible(c))
  176. XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
  177. }
  178. else
  179. configure(c);
  180. }
  181. else {
  182. wc.x = ev->x;
  183. wc.y = ev->y;
  184. wc.width = ev->width;
  185. wc.height = ev->height;
  186. wc.border_width = ev->border_width;
  187. wc.sibling = ev->above;
  188. wc.stack_mode = ev->detail;
  189. XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
  190. }
  191. XSync(dpy, False);
  192. }
  193. static void
  194. destroynotify(XEvent *e) {
  195. Client *c;
  196. XDestroyWindowEvent *ev = &e->xdestroywindow;
  197. if((c = getclient(ev->window)))
  198. unmanage(c);
  199. }
  200. static void
  201. enternotify(XEvent *e) {
  202. Client *c;
  203. XCrossingEvent *ev = &e->xcrossing;
  204. if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
  205. return;
  206. if((c = getclient(ev->window)) && isvisible(c))
  207. focus(c);
  208. else if(ev->window == root) {
  209. selscreen = True;
  210. for(c = stack; c && !isvisible(c); c = c->snext);
  211. focus(c);
  212. }
  213. }
  214. static void
  215. expose(XEvent *e) {
  216. XExposeEvent *ev = &e->xexpose;
  217. if(ev->count == 0) {
  218. if(barwin == ev->window)
  219. drawstatus();
  220. }
  221. }
  222. static void
  223. keypress(XEvent *e) {
  224. static unsigned int len = sizeof key / sizeof key[0];
  225. unsigned int i;
  226. KeySym keysym;
  227. XKeyEvent *ev = &e->xkey;
  228. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  229. for(i = 0; i < len; i++)
  230. if(keysym == key[i].keysym
  231. && CLEANMASK(key[i].mod) == CLEANMASK(ev->state))
  232. {
  233. if(key[i].func)
  234. key[i].func(key[i].arg);
  235. }
  236. }
  237. static void
  238. leavenotify(XEvent *e) {
  239. XCrossingEvent *ev = &e->xcrossing;
  240. if((ev->window == root) && !ev->same_screen) {
  241. selscreen = False;
  242. focus(NULL);
  243. }
  244. }
  245. static void
  246. mappingnotify(XEvent *e) {
  247. XMappingEvent *ev = &e->xmapping;
  248. XRefreshKeyboardMapping(ev);
  249. if(ev->request == MappingKeyboard)
  250. grabkeys();
  251. }
  252. static void
  253. maprequest(XEvent *e) {
  254. static XWindowAttributes wa;
  255. XMapRequestEvent *ev = &e->xmaprequest;
  256. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  257. return;
  258. if(wa.override_redirect)
  259. return;
  260. if(!getclient(ev->window))
  261. manage(ev->window, &wa);
  262. }
  263. static void
  264. propertynotify(XEvent *e) {
  265. Client *c;
  266. Window trans;
  267. XPropertyEvent *ev = &e->xproperty;
  268. if(ev->state == PropertyDelete)
  269. return; /* ignore */
  270. if((c = getclient(ev->window))) {
  271. switch (ev->atom) {
  272. default: break;
  273. case XA_WM_TRANSIENT_FOR:
  274. XGetTransientForHint(dpy, c->win, &trans);
  275. if(!c->isversatile && (c->isversatile = (getclient(trans) != NULL)))
  276. lt->arrange();
  277. break;
  278. case XA_WM_NORMAL_HINTS:
  279. updatesizehints(c);
  280. break;
  281. }
  282. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  283. updatetitle(c);
  284. if(c == sel)
  285. drawstatus();
  286. }
  287. }
  288. }
  289. static void
  290. unmapnotify(XEvent *e) {
  291. Client *c;
  292. XUnmapEvent *ev = &e->xunmap;
  293. if((c = getclient(ev->window)))
  294. unmanage(c);
  295. }
  296. /* extern */
  297. void (*handler[LASTEvent]) (XEvent *) = {
  298. [ButtonPress] = buttonpress,
  299. [ConfigureRequest] = configurerequest,
  300. [DestroyNotify] = destroynotify,
  301. [EnterNotify] = enternotify,
  302. [LeaveNotify] = leavenotify,
  303. [Expose] = expose,
  304. [KeyPress] = keypress,
  305. [MappingNotify] = mappingnotify,
  306. [MapRequest] = maprequest,
  307. [PropertyNotify] = propertynotify,
  308. [UnmapNotify] = unmapnotify
  309. };
  310. void
  311. grabkeys(void) {
  312. static unsigned int len = sizeof key / sizeof key[0];
  313. unsigned int i;
  314. KeyCode code;
  315. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  316. for(i = 0; i < len; i++) {
  317. code = XKeysymToKeycode(dpy, key[i].keysym);
  318. XGrabKey(dpy, code, key[i].mod, root, True,
  319. GrabModeAsync, GrabModeAsync);
  320. XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
  321. GrabModeAsync, GrabModeAsync);
  322. XGrabKey(dpy, code, key[i].mod | numlockmask, root, True,
  323. GrabModeAsync, GrabModeAsync);
  324. XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True,
  325. GrabModeAsync, GrabModeAsync);
  326. }
  327. }