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.

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