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.

387 lines
8.2 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
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. unsigned long newmask;
  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 & 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(ev->value_mask & CWBorderWidth)
  173. c->border = ev->border_width;
  174. wc.x = c->x;
  175. wc.y = c->y;
  176. wc.width = c->w;
  177. wc.height = c->h;
  178. newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
  179. if(newmask)
  180. XConfigureWindow(dpy, c->win, newmask, &wc);
  181. else
  182. configure(c);
  183. XSync(dpy, False);
  184. if(c->isfloat) {
  185. resize(c, False);
  186. if(!isvisible(c))
  187. XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
  188. }
  189. else
  190. arrange();
  191. }
  192. else {
  193. wc.x = ev->x;
  194. wc.y = ev->y;
  195. wc.width = ev->width;
  196. wc.height = ev->height;
  197. wc.border_width = ev->border_width;
  198. wc.sibling = ev->above;
  199. wc.stack_mode = ev->detail;
  200. XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
  201. XSync(dpy, False);
  202. }
  203. }
  204. static void
  205. destroynotify(XEvent *e) {
  206. Client *c;
  207. XDestroyWindowEvent *ev = &e->xdestroywindow;
  208. if((c = getclient(ev->window)))
  209. unmanage(c);
  210. }
  211. static void
  212. enternotify(XEvent *e) {
  213. Client *c;
  214. XCrossingEvent *ev = &e->xcrossing;
  215. if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
  216. return;
  217. if((c = getclient(ev->window)) && isvisible(c))
  218. focus(c);
  219. else if(ev->window == root) {
  220. selscreen = True;
  221. for(c = stack; c && !isvisible(c); c = c->snext);
  222. focus(c);
  223. }
  224. }
  225. static void
  226. expose(XEvent *e) {
  227. XExposeEvent *ev = &e->xexpose;
  228. if(ev->count == 0) {
  229. if(barwin == ev->window)
  230. drawstatus();
  231. }
  232. }
  233. static void
  234. keypress(XEvent *e) {
  235. static unsigned int len = sizeof key / sizeof key[0];
  236. unsigned int i;
  237. KeySym keysym;
  238. XKeyEvent *ev = &e->xkey;
  239. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  240. for(i = 0; i < len; i++) {
  241. if(keysym == key[i].keysym
  242. && CLEANMASK(key[i].mod) == CLEANMASK(ev->state))
  243. {
  244. if(key[i].func)
  245. key[i].func(&key[i].arg);
  246. }
  247. }
  248. }
  249. static void
  250. leavenotify(XEvent *e) {
  251. XCrossingEvent *ev = &e->xcrossing;
  252. if((ev->window == root) && !ev->same_screen) {
  253. selscreen = False;
  254. focus(NULL);
  255. }
  256. }
  257. static void
  258. mappingnotify(XEvent *e) {
  259. XMappingEvent *ev = &e->xmapping;
  260. XRefreshKeyboardMapping(ev);
  261. if(ev->request == MappingKeyboard)
  262. grabkeys();
  263. }
  264. static void
  265. maprequest(XEvent *e) {
  266. static XWindowAttributes wa;
  267. XMapRequestEvent *ev = &e->xmaprequest;
  268. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  269. return;
  270. if(wa.override_redirect) {
  271. XSelectInput(dpy, ev->window,
  272. (StructureNotifyMask | PropertyChangeMask));
  273. return;
  274. }
  275. if(!getclient(ev->window))
  276. manage(ev->window, &wa);
  277. }
  278. static void
  279. propertynotify(XEvent *e) {
  280. Client *c;
  281. Window trans;
  282. XPropertyEvent *ev = &e->xproperty;
  283. if(ev->state == PropertyDelete)
  284. return; /* ignore */
  285. if((c = getclient(ev->window))) {
  286. switch (ev->atom) {
  287. default: break;
  288. case XA_WM_TRANSIENT_FOR:
  289. XGetTransientForHint(dpy, c->win, &trans);
  290. if(!c->isfloat && (c->isfloat = (trans != 0)))
  291. arrange();
  292. break;
  293. case XA_WM_NORMAL_HINTS:
  294. updatesizehints(c);
  295. break;
  296. }
  297. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  298. updatetitle(c);
  299. if(c == sel)
  300. drawstatus();
  301. }
  302. }
  303. }
  304. static void
  305. unmapnotify(XEvent *e) {
  306. Client *c;
  307. XUnmapEvent *ev = &e->xunmap;
  308. if((c = getclient(ev->window)))
  309. unmanage(c);
  310. }
  311. /* extern */
  312. void (*handler[LASTEvent]) (XEvent *) = {
  313. [ButtonPress] = buttonpress,
  314. [ConfigureRequest] = configurerequest,
  315. [DestroyNotify] = destroynotify,
  316. [EnterNotify] = enternotify,
  317. [LeaveNotify] = leavenotify,
  318. [Expose] = expose,
  319. [KeyPress] = keypress,
  320. [MappingNotify] = mappingnotify,
  321. [MapRequest] = maprequest,
  322. [PropertyNotify] = propertynotify,
  323. [UnmapNotify] = unmapnotify
  324. };
  325. void
  326. grabkeys(void) {
  327. static unsigned int len = sizeof key / sizeof key[0];
  328. unsigned int i;
  329. KeyCode code;
  330. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  331. for(i = 0; i < len; i++) {
  332. code = XKeysymToKeycode(dpy, key[i].keysym);
  333. XGrabKey(dpy, code, key[i].mod, root, True,
  334. GrabModeAsync, GrabModeAsync);
  335. XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
  336. GrabModeAsync, GrabModeAsync);
  337. XGrabKey(dpy, code, key[i].mod | numlockmask, root, True,
  338. GrabModeAsync, GrabModeAsync);
  339. XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True,
  340. GrabModeAsync, GrabModeAsync);
  341. }
  342. }
  343. void
  344. procevent(void) {
  345. XEvent ev;
  346. while(XPending(dpy)) {
  347. XNextEvent(dpy, &ev);
  348. if(handler[ev.type])
  349. (handler[ev.type])(&ev); /* call handler */
  350. }
  351. }