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.

386 lines
8.1 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. return;
  81. case ConfigureRequest:
  82. case Expose:
  83. case MapRequest:
  84. handler[ev.type](&ev);
  85. break;
  86. case MotionNotify:
  87. XSync(dpy, False);
  88. nw = ev.xmotion.x - ocx - 2 * c->border + 1;
  89. c->w = nw > 0 ? nw : 1;
  90. nh = ev.xmotion.y - ocy - 2 * c->border + 1;
  91. c->h = nh > 0 ? nh : 1;
  92. resize(c, True);
  93. break;
  94. }
  95. }
  96. }
  97. static void
  98. buttonpress(XEvent *e) {
  99. int x;
  100. Arg a;
  101. Client *c;
  102. XButtonPressedEvent *ev = &e->xbutton;
  103. if(barwin == ev->window) {
  104. x = 0;
  105. for(a.i = 0; a.i < ntags; a.i++) {
  106. x += textw(tags[a.i]);
  107. if(ev->x < x) {
  108. if(ev->button == Button1) {
  109. if(ev->state & MODKEY)
  110. tag(&a);
  111. else
  112. view(&a);
  113. }
  114. else if(ev->button == Button3) {
  115. if(ev->state & MODKEY)
  116. toggletag(&a);
  117. else
  118. toggleview(&a);
  119. }
  120. return;
  121. }
  122. }
  123. if(ev->x < x + bmw)
  124. switch(ev->button) {
  125. case Button1:
  126. togglemode(NULL);
  127. break;
  128. case Button4:
  129. a.i = 1;
  130. incnmaster(&a);
  131. break;
  132. case Button5:
  133. a.i = -1;
  134. incnmaster(&a);
  135. break;
  136. }
  137. }
  138. else if((c = getclient(ev->window))) {
  139. focus(c);
  140. if(CLEANMASK(ev->state) != MODKEY)
  141. return;
  142. if(ev->button == Button1 && (arrange == dofloat || c->isfloat)) {
  143. restack();
  144. movemouse(c);
  145. }
  146. else if(ev->button == Button2)
  147. zoom(NULL);
  148. else if(ev->button == Button3 && (arrange == dofloat || c->isfloat) &&
  149. !c->isfixed) {
  150. restack();
  151. resizemouse(c);
  152. }
  153. }
  154. }
  155. static void
  156. configurerequest(XEvent *e) {
  157. unsigned long newmask;
  158. Client *c;
  159. XConfigureRequestEvent *ev = &e->xconfigurerequest;
  160. XWindowChanges wc;
  161. if((c = getclient(ev->window))) {
  162. c->ismax = False;
  163. if(ev->value_mask & CWX)
  164. c->x = ev->x;
  165. if(ev->value_mask & CWY)
  166. c->y = ev->y;
  167. if(ev->value_mask & CWWidth)
  168. c->w = ev->width;
  169. if(ev->value_mask & CWHeight)
  170. c->h = ev->height;
  171. if(ev->value_mask & CWBorderWidth)
  172. c->border = ev->border_width;
  173. wc.x = c->x;
  174. wc.y = c->y;
  175. wc.width = c->w;
  176. wc.height = c->h;
  177. newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
  178. if(newmask)
  179. XConfigureWindow(dpy, c->win, newmask, &wc);
  180. else
  181. configure(c);
  182. XSync(dpy, False);
  183. if(c->isfloat) {
  184. resize(c, False);
  185. if(!isvisible(c))
  186. XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
  187. }
  188. else
  189. arrange();
  190. }
  191. else {
  192. wc.x = ev->x;
  193. wc.y = ev->y;
  194. wc.width = ev->width;
  195. wc.height = ev->height;
  196. wc.border_width = ev->border_width;
  197. wc.sibling = ev->above;
  198. wc.stack_mode = ev->detail;
  199. XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
  200. XSync(dpy, False);
  201. }
  202. }
  203. static void
  204. destroynotify(XEvent *e) {
  205. Client *c;
  206. XDestroyWindowEvent *ev = &e->xdestroywindow;
  207. if((c = getclient(ev->window)))
  208. unmanage(c);
  209. }
  210. static void
  211. enternotify(XEvent *e) {
  212. Client *c;
  213. XCrossingEvent *ev = &e->xcrossing;
  214. if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
  215. return;
  216. if((c = getclient(ev->window)) && isvisible(c))
  217. focus(c);
  218. else if(ev->window == root) {
  219. selscreen = True;
  220. for(c = stack; c && !isvisible(c); c = c->snext);
  221. focus(c);
  222. }
  223. }
  224. static void
  225. expose(XEvent *e) {
  226. XExposeEvent *ev = &e->xexpose;
  227. if(ev->count == 0) {
  228. if(barwin == ev->window)
  229. drawstatus();
  230. }
  231. }
  232. static void
  233. keypress(XEvent *e) {
  234. static unsigned int len = sizeof key / sizeof key[0];
  235. unsigned int i;
  236. KeySym keysym;
  237. XKeyEvent *ev = &e->xkey;
  238. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  239. for(i = 0; i < len; i++) {
  240. if(keysym == key[i].keysym
  241. && CLEANMASK(key[i].mod) == CLEANMASK(ev->state))
  242. {
  243. if(key[i].func)
  244. key[i].func(&key[i].arg);
  245. }
  246. }
  247. }
  248. static void
  249. leavenotify(XEvent *e) {
  250. XCrossingEvent *ev = &e->xcrossing;
  251. if((ev->window == root) && !ev->same_screen) {
  252. selscreen = False;
  253. focus(NULL);
  254. }
  255. }
  256. static void
  257. mappingnotify(XEvent *e) {
  258. XMappingEvent *ev = &e->xmapping;
  259. XRefreshKeyboardMapping(ev);
  260. if(ev->request == MappingKeyboard)
  261. grabkeys();
  262. }
  263. static void
  264. maprequest(XEvent *e) {
  265. static XWindowAttributes wa;
  266. XMapRequestEvent *ev = &e->xmaprequest;
  267. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  268. return;
  269. if(wa.override_redirect) {
  270. XSelectInput(dpy, ev->window,
  271. (StructureNotifyMask | PropertyChangeMask));
  272. return;
  273. }
  274. if(!getclient(ev->window))
  275. manage(ev->window, &wa);
  276. }
  277. static void
  278. propertynotify(XEvent *e) {
  279. Client *c;
  280. Window trans;
  281. XPropertyEvent *ev = &e->xproperty;
  282. if(ev->state == PropertyDelete)
  283. return; /* ignore */
  284. if((c = getclient(ev->window))) {
  285. switch (ev->atom) {
  286. default: break;
  287. case XA_WM_TRANSIENT_FOR:
  288. XGetTransientForHint(dpy, c->win, &trans);
  289. if(!c->isfloat && (c->isfloat = (trans != 0)))
  290. arrange();
  291. break;
  292. case XA_WM_NORMAL_HINTS:
  293. updatesizehints(c);
  294. break;
  295. }
  296. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  297. updatetitle(c);
  298. if(c == sel)
  299. drawstatus();
  300. }
  301. }
  302. }
  303. static void
  304. unmapnotify(XEvent *e) {
  305. Client *c;
  306. XUnmapEvent *ev = &e->xunmap;
  307. if((c = getclient(ev->window)))
  308. unmanage(c);
  309. }
  310. /* extern */
  311. void (*handler[LASTEvent]) (XEvent *) = {
  312. [ButtonPress] = buttonpress,
  313. [ConfigureRequest] = configurerequest,
  314. [DestroyNotify] = destroynotify,
  315. [EnterNotify] = enternotify,
  316. [LeaveNotify] = leavenotify,
  317. [Expose] = expose,
  318. [KeyPress] = keypress,
  319. [MappingNotify] = mappingnotify,
  320. [MapRequest] = maprequest,
  321. [PropertyNotify] = propertynotify,
  322. [UnmapNotify] = unmapnotify
  323. };
  324. void
  325. grabkeys(void) {
  326. static unsigned int len = sizeof key / sizeof key[0];
  327. unsigned int i;
  328. KeyCode code;
  329. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  330. for(i = 0; i < len; i++) {
  331. code = XKeysymToKeycode(dpy, key[i].keysym);
  332. XGrabKey(dpy, code, key[i].mod, root, True,
  333. GrabModeAsync, GrabModeAsync);
  334. XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
  335. GrabModeAsync, GrabModeAsync);
  336. XGrabKey(dpy, code, key[i].mod | numlockmask, root, True,
  337. GrabModeAsync, GrabModeAsync);
  338. XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True,
  339. GrabModeAsync, GrabModeAsync);
  340. }
  341. }
  342. void
  343. procevent(void) {
  344. XEvent ev;
  345. while(XPending(dpy)) {
  346. XNextEvent(dpy, &ev);
  347. if(handler[ev.type])
  348. (handler[ev.type])(&ev); /* call handler */
  349. }
  350. }