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.

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