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.

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