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.

388 lines
7.9 KiB

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