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.

355 lines
7.0 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
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. Corner sticky;
  55. XEvent ev;
  56. ocx = c->x;
  57. ocy = c->y;
  58. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  59. None, cursor[CurResize], CurrentTime) != GrabSuccess)
  60. return;
  61. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w, c->h);
  62. for(;;) {
  63. XMaskEvent(dpy, MOUSEMASK | ExposureMask, &ev);
  64. switch(ev.type) {
  65. default: break;
  66. case Expose:
  67. handler[Expose](&ev);
  68. break;
  69. case MotionNotify:
  70. XSync(dpy, False);
  71. c->w = abs(ocx - ev.xmotion.x);
  72. c->h = abs(ocy - ev.xmotion.y);
  73. c->x = (ocx <= ev.xmotion.x) ? ocx : ocx - c->w;
  74. c->y = (ocy <= ev.xmotion.y) ? ocy : ocy - c->h;
  75. if(ocx <= ev.xmotion.x)
  76. sticky = (ocy <= ev.xmotion.y) ? TopLeft : BotLeft;
  77. else
  78. sticky = (ocy <= ev.xmotion.y) ? TopRight : BotRight;
  79. resize(c, True, sticky);
  80. break;
  81. case ButtonRelease:
  82. XUngrabPointer(dpy, CurrentTime);
  83. return;
  84. }
  85. }
  86. }
  87. static void
  88. buttonpress(XEvent *e)
  89. {
  90. int x;
  91. Arg a;
  92. Client *c;
  93. XButtonPressedEvent *ev = &e->xbutton;
  94. if(barwin == ev->window) {
  95. switch(ev->button) {
  96. default:
  97. x = 0;
  98. for(a.i = 0; a.i < TLast; a.i++) {
  99. x += textw(tags[a.i]);
  100. if(ev->x < x) {
  101. view(&a);
  102. break;
  103. }
  104. }
  105. break;
  106. case Button4:
  107. viewnext(&a);
  108. break;
  109. case Button5:
  110. viewprev(&a);
  111. break;
  112. }
  113. }
  114. else if((c = getclient(ev->window))) {
  115. focus(c);
  116. switch(ev->button) {
  117. default:
  118. break;
  119. case Button1:
  120. if(!c->ismax && (arrange == dofloat || c->isfloat)) {
  121. higher(c);
  122. movemouse(c);
  123. }
  124. break;
  125. case Button2:
  126. lower(c);
  127. break;
  128. case Button3:
  129. if(!c->ismax && (arrange == dofloat || c->isfloat)) {
  130. higher(c);
  131. resizemouse(c);
  132. }
  133. break;
  134. }
  135. }
  136. }
  137. static void
  138. configurerequest(XEvent *e)
  139. {
  140. Client *c;
  141. XConfigureRequestEvent *ev = &e->xconfigurerequest;
  142. XWindowChanges wc;
  143. ev->value_mask &= ~CWSibling;
  144. if((c = getclient(ev->window))) {
  145. gravitate(c, True);
  146. if(ev->value_mask & CWX)
  147. c->x = ev->x;
  148. if(ev->value_mask & CWY)
  149. c->y = ev->y;
  150. if(ev->value_mask & CWWidth)
  151. c->w = ev->width;
  152. if(ev->value_mask & CWHeight)
  153. c->h = ev->height;
  154. if(ev->value_mask & CWBorderWidth)
  155. c->border = 1;
  156. gravitate(c, False);
  157. resize(c, True, TopLeft);
  158. }
  159. wc.x = ev->x;
  160. wc.y = ev->y;
  161. wc.width = ev->width;
  162. wc.height = ev->height;
  163. wc.border_width = 1;
  164. wc.sibling = None;
  165. wc.stack_mode = Above;
  166. ev->value_mask &= ~CWStackMode;
  167. ev->value_mask |= CWBorderWidth;
  168. XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
  169. XSync(dpy, False);
  170. }
  171. static void
  172. destroynotify(XEvent *e)
  173. {
  174. Client *c;
  175. XDestroyWindowEvent *ev = &e->xdestroywindow;
  176. if((c = getclient(ev->window)))
  177. unmanage(c);
  178. }
  179. static void
  180. enternotify(XEvent *e)
  181. {
  182. Client *c;
  183. XCrossingEvent *ev = &e->xcrossing;
  184. if(ev->detail == NotifyInferior)
  185. return;
  186. if((c = getclient(ev->window)) || (c = getctitle(ev->window)))
  187. focus(c);
  188. else if(ev->window == root)
  189. issel = True;
  190. }
  191. static void
  192. expose(XEvent *e)
  193. {
  194. Client *c;
  195. XExposeEvent *ev = &e->xexpose;
  196. if(ev->count == 0) {
  197. if(barwin == ev->window)
  198. drawstatus();
  199. else if((c = getctitle(ev->window)))
  200. drawtitle(c);
  201. }
  202. }
  203. static void
  204. keypress(XEvent *e)
  205. {
  206. static unsigned int len = sizeof(key) / sizeof(key[0]);
  207. unsigned int i;
  208. KeySym keysym;
  209. XKeyEvent *ev = &e->xkey;
  210. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  211. for(i = 0; i < len; i++)
  212. if(keysym == key[i].keysym &&
  213. CLEANMASK(key[i].mod) == CLEANMASK(ev->state)) {
  214. if(key[i].func)
  215. key[i].func(&key[i].arg);
  216. return;
  217. }
  218. }
  219. static void
  220. leavenotify(XEvent *e)
  221. {
  222. XCrossingEvent *ev = &e->xcrossing;
  223. if((ev->window == root) && !ev->same_screen)
  224. issel = True;
  225. }
  226. static void
  227. maprequest(XEvent *e)
  228. {
  229. static XWindowAttributes wa;
  230. XMapRequestEvent *ev = &e->xmaprequest;
  231. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  232. return;
  233. if(wa.override_redirect) {
  234. XSelectInput(dpy, ev->window,
  235. (StructureNotifyMask | PropertyChangeMask));
  236. return;
  237. }
  238. if(!getclient(ev->window))
  239. manage(ev->window, &wa);
  240. }
  241. static void
  242. propertynotify(XEvent *e)
  243. {
  244. Client *c;
  245. Window trans;
  246. XPropertyEvent *ev = &e->xproperty;
  247. if(ev->state == PropertyDelete)
  248. return; /* ignore */
  249. if((c = getclient(ev->window))) {
  250. if(ev->atom == wmatom[WMProtocols]) {
  251. c->proto = getproto(c->win);
  252. return;
  253. }
  254. switch (ev->atom) {
  255. default: break;
  256. case XA_WM_TRANSIENT_FOR:
  257. XGetTransientForHint(dpy, c->win, &trans);
  258. if(!c->isfloat && (c->isfloat = (trans != 0)))
  259. arrange(NULL);
  260. break;
  261. case XA_WM_NORMAL_HINTS:
  262. setsize(c);
  263. break;
  264. }
  265. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  266. settitle(c);
  267. drawtitle(c);
  268. }
  269. }
  270. }
  271. static void
  272. unmapnotify(XEvent *e)
  273. {
  274. Client *c;
  275. XUnmapEvent *ev = &e->xunmap;
  276. if((c = getclient(ev->window)))
  277. unmanage(c);
  278. }
  279. /* extern */
  280. void (*handler[LASTEvent]) (XEvent *) = {
  281. [ButtonPress] = buttonpress,
  282. [ConfigureRequest] = configurerequest,
  283. [DestroyNotify] = destroynotify,
  284. [EnterNotify] = enternotify,
  285. [LeaveNotify] = leavenotify,
  286. [Expose] = expose,
  287. [KeyPress] = keypress,
  288. [MapRequest] = maprequest,
  289. [PropertyNotify] = propertynotify,
  290. [UnmapNotify] = unmapnotify
  291. };
  292. void
  293. grabkeys()
  294. {
  295. static unsigned int len = sizeof(key) / sizeof(key[0]);
  296. unsigned int i;
  297. KeyCode code;
  298. for(i = 0; i < len; i++) {
  299. code = XKeysymToKeycode(dpy, key[i].keysym);
  300. /* normal */
  301. XUngrabKey(dpy, code, key[i].mod, root);
  302. XGrabKey(dpy, code, key[i].mod, root, True,
  303. GrabModeAsync, GrabModeAsync);
  304. /* capslock */
  305. XUngrabKey(dpy, code, key[i].mod | LockMask, root);
  306. XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
  307. GrabModeAsync, GrabModeAsync);
  308. /* numlock */
  309. XUngrabKey(dpy, code, key[i].mod | NUMLOCKMASK, root);
  310. XGrabKey(dpy, code, key[i].mod | NUMLOCKMASK, root, True,
  311. GrabModeAsync, GrabModeAsync);
  312. /* capslock & numlock */
  313. XUngrabKey(dpy, code, key[i].mod | NUMLOCKMASK | LockMask, root);
  314. XGrabKey(dpy, code, key[i].mod | NUMLOCKMASK | LockMask, root, True,
  315. GrabModeAsync, GrabModeAsync);
  316. }
  317. }