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.

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