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.

386 lines
8.6 KiB

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