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.

366 lines
8.0 KiB

18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
  1. /* © 2004-2007 Anselm R. Garbe <garbeam at gmail dot com>
  2. * See LICENSE file for license details. */
  3. #include "dwm.h"
  4. #include <stdio.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)(const char *arg);
  13. const char *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. static char buf[32];
  105. unsigned int i, x;
  106. Client *c;
  107. XButtonPressedEvent *ev = &e->xbutton;
  108. buf[0] = 0;
  109. if(barwin == ev->window) {
  110. x = 0;
  111. for(i = 0; i < ntags; i++) {
  112. x += textw(tags[i]);
  113. if(ev->x < x) {
  114. snprintf(buf, sizeof buf, "%d", i);
  115. if(ev->button == Button1) {
  116. if(ev->state & MODKEY)
  117. tag(buf);
  118. else
  119. view(buf);
  120. }
  121. else if(ev->button == Button3) {
  122. if(ev->state & MODKEY)
  123. toggletag(buf);
  124. else
  125. toggleview(buf);
  126. }
  127. return;
  128. }
  129. }
  130. if(ev->x < x + blw)
  131. switch(ev->button) {
  132. case Button1:
  133. setlayout(NULL);
  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 && (lt->arrange == floating || c->isfloating)) {
  142. restack();
  143. movemouse(c);
  144. }
  145. else if(ev->button == Button2)
  146. zoom(NULL);
  147. else if(ev->button == Button3
  148. && (lt->arrange == floating || c->isfloating) && !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(c->isfixed || c->isfloating || (lt->arrange == floating)) {
  163. if(ev->value_mask & CWX)
  164. c->x = ev->x;
  165. if(ev->value_mask & CWY)
  166. c->y = ev->y;
  167. if(ev->value_mask & CWWidth)
  168. c->w = ev->width;
  169. if(ev->value_mask & CWHeight)
  170. c->h = ev->height;
  171. if((ev->value_mask & (CWX | CWY))
  172. && !(ev->value_mask & (CWWidth | CWHeight)))
  173. configure(c);
  174. if(isvisible(c))
  175. XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
  176. }
  177. else {
  178. if(ev->value_mask & CWBorderWidth)
  179. c->border = ev->border_width;
  180. configure(c);
  181. c->border = BORDERPX;
  182. }
  183. }
  184. else {
  185. wc.x = ev->x;
  186. wc.y = ev->y;
  187. wc.width = ev->width;
  188. wc.height = ev->height;
  189. wc.border_width = ev->border_width;
  190. wc.sibling = ev->above;
  191. wc.stack_mode = ev->detail;
  192. XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
  193. }
  194. XSync(dpy, False);
  195. }
  196. static void
  197. destroynotify(XEvent *e) {
  198. Client *c;
  199. XDestroyWindowEvent *ev = &e->xdestroywindow;
  200. if((c = getclient(ev->window)))
  201. unmanage(c);
  202. }
  203. static void
  204. enternotify(XEvent *e) {
  205. Client *c;
  206. XCrossingEvent *ev = &e->xcrossing;
  207. if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
  208. return;
  209. if((c = getclient(ev->window)) && isvisible(c))
  210. focus(c);
  211. else if(ev->window == root) {
  212. selscreen = True;
  213. for(c = stack; c && !isvisible(c); c = c->snext);
  214. focus(c);
  215. }
  216. }
  217. static void
  218. expose(XEvent *e) {
  219. XExposeEvent *ev = &e->xexpose;
  220. if(ev->count == 0) {
  221. if(barwin == ev->window)
  222. drawstatus();
  223. }
  224. }
  225. static void
  226. keypress(XEvent *e) {
  227. static unsigned int len = sizeof key / sizeof key[0];
  228. unsigned int i;
  229. KeySym keysym;
  230. XKeyEvent *ev = &e->xkey;
  231. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  232. for(i = 0; i < len; i++)
  233. if(keysym == key[i].keysym
  234. && CLEANMASK(key[i].mod) == CLEANMASK(ev->state))
  235. {
  236. if(key[i].func)
  237. key[i].func(key[i].arg);
  238. }
  239. }
  240. static void
  241. leavenotify(XEvent *e) {
  242. XCrossingEvent *ev = &e->xcrossing;
  243. if((ev->window == root) && !ev->same_screen) {
  244. selscreen = False;
  245. focus(NULL);
  246. }
  247. }
  248. static void
  249. mappingnotify(XEvent *e) {
  250. XMappingEvent *ev = &e->xmapping;
  251. XRefreshKeyboardMapping(ev);
  252. if(ev->request == MappingKeyboard)
  253. grabkeys();
  254. }
  255. static void
  256. maprequest(XEvent *e) {
  257. static XWindowAttributes wa;
  258. XMapRequestEvent *ev = &e->xmaprequest;
  259. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  260. return;
  261. if(wa.override_redirect)
  262. return;
  263. if(!getclient(ev->window))
  264. manage(ev->window, &wa);
  265. }
  266. static void
  267. propertynotify(XEvent *e) {
  268. Client *c;
  269. Window trans;
  270. XPropertyEvent *ev = &e->xproperty;
  271. if(ev->state == PropertyDelete)
  272. return; /* ignore */
  273. if((c = getclient(ev->window))) {
  274. switch (ev->atom) {
  275. default: break;
  276. case XA_WM_TRANSIENT_FOR:
  277. XGetTransientForHint(dpy, c->win, &trans);
  278. if(!c->isfloating && (c->isfloating = (getclient(trans) != NULL)))
  279. lt->arrange();
  280. break;
  281. case XA_WM_NORMAL_HINTS:
  282. updatesizehints(c);
  283. break;
  284. }
  285. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  286. updatetitle(c);
  287. if(c == sel)
  288. drawstatus();
  289. }
  290. }
  291. }
  292. static void
  293. unmapnotify(XEvent *e) {
  294. Client *c;
  295. XUnmapEvent *ev = &e->xunmap;
  296. if((c = getclient(ev->window)))
  297. unmanage(c);
  298. }
  299. /* extern */
  300. void (*handler[LASTEvent]) (XEvent *) = {
  301. [ButtonPress] = buttonpress,
  302. [ConfigureRequest] = configurerequest,
  303. [DestroyNotify] = destroynotify,
  304. [EnterNotify] = enternotify,
  305. [LeaveNotify] = leavenotify,
  306. [Expose] = expose,
  307. [KeyPress] = keypress,
  308. [MappingNotify] = mappingnotify,
  309. [MapRequest] = maprequest,
  310. [PropertyNotify] = propertynotify,
  311. [UnmapNotify] = unmapnotify
  312. };
  313. void
  314. grabkeys(void) {
  315. static unsigned int len = sizeof key / sizeof key[0];
  316. unsigned int i;
  317. KeyCode code;
  318. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  319. for(i = 0; i < len; i++) {
  320. code = XKeysymToKeycode(dpy, key[i].keysym);
  321. XGrabKey(dpy, code, key[i].mod, root, True,
  322. GrabModeAsync, GrabModeAsync);
  323. XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
  324. GrabModeAsync, GrabModeAsync);
  325. XGrabKey(dpy, code, key[i].mod | numlockmask, root, True,
  326. GrabModeAsync, GrabModeAsync);
  327. XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True,
  328. GrabModeAsync, GrabModeAsync);
  329. }
  330. }