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.

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