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.

369 lines
8.0 KiB

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