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.

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