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.

382 lines
8.4 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. destroynotify(XEvent *e) {
  212. Client *c;
  213. XDestroyWindowEvent *ev = &e->xdestroywindow;
  214. if((c = getclient(ev->window)))
  215. unmanage(c);
  216. }
  217. static void
  218. enternotify(XEvent *e) {
  219. Client *c;
  220. XCrossingEvent *ev = &e->xcrossing;
  221. if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
  222. return;
  223. if((c = getclient(ev->window)))
  224. focus(c);
  225. else if(ev->window == root) {
  226. selscreen = True;
  227. focus(NULL);
  228. }
  229. }
  230. static void
  231. expose(XEvent *e) {
  232. XExposeEvent *ev = &e->xexpose;
  233. if(ev->count == 0) {
  234. if(barwin == ev->window)
  235. drawstatus();
  236. }
  237. }
  238. static void
  239. keypress(XEvent *e) {
  240. static unsigned int len = sizeof key / sizeof key[0];
  241. unsigned int i;
  242. KeySym keysym;
  243. XKeyEvent *ev = &e->xkey;
  244. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  245. for(i = 0; i < len; i++)
  246. if(keysym == key[i].keysym
  247. && CLEANMASK(key[i].mod) == CLEANMASK(ev->state))
  248. {
  249. if(key[i].func)
  250. key[i].func(key[i].arg);
  251. }
  252. }
  253. static void
  254. leavenotify(XEvent *e) {
  255. XCrossingEvent *ev = &e->xcrossing;
  256. if((ev->window == root) && !ev->same_screen) {
  257. selscreen = False;
  258. focus(NULL);
  259. }
  260. }
  261. static void
  262. mappingnotify(XEvent *e) {
  263. XMappingEvent *ev = &e->xmapping;
  264. XRefreshKeyboardMapping(ev);
  265. if(ev->request == MappingKeyboard)
  266. grabkeys();
  267. }
  268. static void
  269. maprequest(XEvent *e) {
  270. static XWindowAttributes wa;
  271. XMapRequestEvent *ev = &e->xmaprequest;
  272. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  273. return;
  274. if(wa.override_redirect)
  275. return;
  276. if(!getclient(ev->window))
  277. manage(ev->window, &wa);
  278. }
  279. static void
  280. propertynotify(XEvent *e) {
  281. Client *c;
  282. Window trans;
  283. XPropertyEvent *ev = &e->xproperty;
  284. if(ev->state == PropertyDelete)
  285. return; /* ignore */
  286. if((c = getclient(ev->window))) {
  287. switch (ev->atom) {
  288. default: break;
  289. case XA_WM_TRANSIENT_FOR:
  290. XGetTransientForHint(dpy, c->win, &trans);
  291. if(!c->isfloating && (c->isfloating = (getclient(trans) != NULL)))
  292. lt->arrange();
  293. break;
  294. case XA_WM_NORMAL_HINTS:
  295. updatesizehints(c);
  296. break;
  297. }
  298. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  299. updatetitle(c);
  300. if(c == sel)
  301. drawstatus();
  302. }
  303. }
  304. }
  305. static void
  306. unmapnotify(XEvent *e) {
  307. Client *c;
  308. XUnmapEvent *ev = &e->xunmap;
  309. if((c = getclient(ev->window)))
  310. unmanage(c);
  311. }
  312. /* extern */
  313. void (*handler[LASTEvent]) (XEvent *) = {
  314. [ButtonPress] = buttonpress,
  315. [ConfigureRequest] = configurerequest,
  316. [ConfigureNotify] = configurenotify,
  317. [DestroyNotify] = destroynotify,
  318. [EnterNotify] = enternotify,
  319. [LeaveNotify] = leavenotify,
  320. [Expose] = expose,
  321. [KeyPress] = keypress,
  322. [MappingNotify] = mappingnotify,
  323. [MapRequest] = maprequest,
  324. [PropertyNotify] = propertynotify,
  325. [UnmapNotify] = unmapnotify
  326. };
  327. void
  328. grabkeys(void) {
  329. static unsigned int len = sizeof key / sizeof key[0];
  330. unsigned int i;
  331. KeyCode code;
  332. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  333. for(i = 0; i < len; i++) {
  334. code = XKeysymToKeycode(dpy, key[i].keysym);
  335. XGrabKey(dpy, code, key[i].mod, root, True,
  336. GrabModeAsync, GrabModeAsync);
  337. XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
  338. GrabModeAsync, GrabModeAsync);
  339. XGrabKey(dpy, code, key[i].mod | numlockmask, root, True,
  340. GrabModeAsync, GrabModeAsync);
  341. XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True,
  342. GrabModeAsync, GrabModeAsync);
  343. }
  344. }