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.

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