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.

381 lines
8.5 KiB

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