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.

394 lines
8.2 KiB

18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 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, &ev);
  33. switch (ev.type) {
  34. case ButtonRelease:
  35. resize(c, True, TopLeft);
  36. XUngrabPointer(dpy, CurrentTime);
  37. return;
  38. case Expose:
  39. handler[Expose](&ev);
  40. break;
  41. case MotionNotify:
  42. XSync(dpy, False);
  43. c->x = ocx + (ev.xmotion.x - x1);
  44. c->y = ocy + (ev.xmotion.y - y1);
  45. if(abs(wax + c->x) < SNAP)
  46. c->x = wax;
  47. else if(abs((wax + waw) - (c->x + c->w)) < SNAP)
  48. c->x = wax + waw - c->w - 2 * BORDERPX;
  49. if(abs(way - c->y) < SNAP)
  50. c->y = way;
  51. else if(abs((way + wah) - (c->y + c->h)) < SNAP)
  52. c->y = way + wah - c->h - 2 * BORDERPX;
  53. resize(c, False, TopLeft);
  54. break;
  55. }
  56. }
  57. }
  58. static void
  59. resizemouse(Client *c) {
  60. int ocx, ocy;
  61. int nw, nh;
  62. Corner sticky;
  63. XEvent ev;
  64. ocx = c->x;
  65. ocy = c->y;
  66. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  67. None, cursor[CurResize], CurrentTime) != GrabSuccess)
  68. return;
  69. c->ismax = False;
  70. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w, c->h);
  71. for(;;) {
  72. XMaskEvent(dpy, MOUSEMASK | ExposureMask, &ev);
  73. switch(ev.type) {
  74. case ButtonRelease:
  75. resize(c, True, TopLeft);
  76. XUngrabPointer(dpy, CurrentTime);
  77. return;
  78. case Expose:
  79. handler[Expose](&ev);
  80. break;
  81. case MotionNotify:
  82. XSync(dpy, False);
  83. if((nw = abs(ocx - ev.xmotion.x)))
  84. c->w = nw;
  85. if((nh = abs(ocy - ev.xmotion.y)))
  86. c->h = nh;
  87. c->x = (ocx <= ev.xmotion.x) ? ocx : ocx - c->w;
  88. c->y = (ocy <= ev.xmotion.y) ? ocy : ocy - c->h;
  89. if(ocx <= ev.xmotion.x)
  90. sticky = (ocy <= ev.xmotion.y) ? TopLeft : BotLeft;
  91. else
  92. sticky = (ocy <= ev.xmotion.y) ? TopRight : BotRight;
  93. resize(c, True, sticky);
  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, TopLeft);
  186. if(!isvisible(c))
  187. ban(c);
  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)) || (c = getctitle(ev->window))) && isvisible(c))
  218. focus(c);
  219. else if(ev->window == root) {
  220. issel = True;
  221. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  222. drawall();
  223. }
  224. }
  225. static void
  226. expose(XEvent *e) {
  227. Client *c;
  228. XExposeEvent *ev = &e->xexpose;
  229. if(ev->count == 0) {
  230. if(barwin == ev->window)
  231. drawstatus();
  232. else if((c = getctitle(ev->window)))
  233. drawclient(c);
  234. }
  235. }
  236. static void
  237. keypress(XEvent *e) {
  238. static unsigned int len = sizeof key / sizeof key[0];
  239. unsigned int i;
  240. KeySym keysym;
  241. XKeyEvent *ev = &e->xkey;
  242. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  243. for(i = 0; i < len; i++) {
  244. if(keysym == key[i].keysym
  245. && CLEANMASK(key[i].mod) == CLEANMASK(ev->state))
  246. {
  247. if(key[i].func)
  248. key[i].func(&key[i].arg);
  249. }
  250. }
  251. }
  252. static void
  253. leavenotify(XEvent *e) {
  254. XCrossingEvent *ev = &e->xcrossing;
  255. if((ev->window == root) && !ev->same_screen) {
  256. issel = False;
  257. drawall();
  258. }
  259. }
  260. static void
  261. mappingnotify(XEvent *e) {
  262. XMappingEvent *ev = &e->xmapping;
  263. XRefreshKeyboardMapping(ev);
  264. if(ev->request == MappingKeyboard)
  265. grabkeys();
  266. }
  267. static void
  268. maprequest(XEvent *e) {
  269. static XWindowAttributes wa;
  270. XMapRequestEvent *ev = &e->xmaprequest;
  271. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  272. return;
  273. if(wa.override_redirect) {
  274. XSelectInput(dpy, ev->window,
  275. (StructureNotifyMask | PropertyChangeMask));
  276. return;
  277. }
  278. if(!getclient(ev->window))
  279. manage(ev->window, &wa);
  280. }
  281. static void
  282. propertynotify(XEvent *e) {
  283. Client *c;
  284. Window trans;
  285. XPropertyEvent *ev = &e->xproperty;
  286. if(ev->state == PropertyDelete)
  287. return; /* ignore */
  288. if((c = getclient(ev->window))) {
  289. if(ev->atom == wmatom[WMProtocols]) {
  290. c->proto = getproto(c->win);
  291. return;
  292. }
  293. switch (ev->atom) {
  294. default: break;
  295. case XA_WM_TRANSIENT_FOR:
  296. XGetTransientForHint(dpy, c->win, &trans);
  297. if(!c->isfloat && (c->isfloat = (trans != 0)))
  298. arrange();
  299. break;
  300. case XA_WM_NORMAL_HINTS:
  301. updatesizehints(c);
  302. break;
  303. }
  304. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  305. updatetitle(c);
  306. resizetitle(c);
  307. drawclient(c);
  308. }
  309. }
  310. }
  311. static void
  312. unmapnotify(XEvent *e) {
  313. Client *c;
  314. XUnmapEvent *ev = &e->xunmap;
  315. if((c = getclient(ev->window)))
  316. unmanage(c);
  317. }
  318. /* extern */
  319. void (*handler[LASTEvent]) (XEvent *) = {
  320. [ButtonPress] = buttonpress,
  321. [ConfigureRequest] = configurerequest,
  322. [DestroyNotify] = destroynotify,
  323. [EnterNotify] = enternotify,
  324. [LeaveNotify] = leavenotify,
  325. [Expose] = expose,
  326. [KeyPress] = keypress,
  327. [MappingNotify] = mappingnotify,
  328. [MapRequest] = maprequest,
  329. [PropertyNotify] = propertynotify,
  330. [UnmapNotify] = unmapnotify
  331. };
  332. void
  333. grabkeys(void) {
  334. static unsigned int len = sizeof key / sizeof key[0];
  335. unsigned int i;
  336. KeyCode code;
  337. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  338. for(i = 0; i < len; i++) {
  339. code = XKeysymToKeycode(dpy, key[i].keysym);
  340. XGrabKey(dpy, code, key[i].mod, root, True,
  341. GrabModeAsync, GrabModeAsync);
  342. XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
  343. GrabModeAsync, GrabModeAsync);
  344. XGrabKey(dpy, code, key[i].mod | numlockmask, root, True,
  345. GrabModeAsync, GrabModeAsync);
  346. XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True,
  347. GrabModeAsync, GrabModeAsync);
  348. }
  349. }
  350. void
  351. procevent(void) {
  352. XEvent ev;
  353. while(XPending(dpy)) {
  354. XNextEvent(dpy, &ev);
  355. if(handler[ev.type])
  356. (handler[ev.type])(&ev); /* call handler */
  357. }
  358. }