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.

345 lines
6.7 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
18 years ago
18 years ago
18 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. static unsigned int valid_mask = 255 & ~(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. if((c = getclient(ev->window))) {
  144. gravitate(c, True);
  145. if(ev->value_mask & CWX)
  146. c->x = ev->x;
  147. if(ev->value_mask & CWY)
  148. c->y = ev->y;
  149. if(ev->value_mask & CWWidth)
  150. c->w = ev->width;
  151. if(ev->value_mask & CWHeight)
  152. c->h = ev->height;
  153. if(ev->value_mask & CWBorderWidth)
  154. c->border = 1;
  155. gravitate(c, False);
  156. resize(c, True, TopLeft);
  157. }
  158. else {
  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. XConfigureWindow(dpy, ev->window,
  165. CWX|CWY|CWWidth|CWHeight|CWBorderWidth, &wc);
  166. XSync(dpy, False);
  167. }
  168. }
  169. static void
  170. destroynotify(XEvent *e)
  171. {
  172. Client *c;
  173. XDestroyWindowEvent *ev = &e->xdestroywindow;
  174. if((c = getclient(ev->window)))
  175. unmanage(c);
  176. }
  177. static void
  178. enternotify(XEvent *e)
  179. {
  180. Client *c;
  181. XCrossingEvent *ev = &e->xcrossing;
  182. if(ev->detail == NotifyInferior)
  183. return;
  184. if((c = getclient(ev->window)))
  185. focus(c);
  186. else if(ev->window == root)
  187. issel = True;
  188. }
  189. static void
  190. expose(XEvent *e)
  191. {
  192. Client *c;
  193. XExposeEvent *ev = &e->xexpose;
  194. if(ev->count == 0) {
  195. if(barwin == ev->window)
  196. drawstatus();
  197. else if((c = getctitle(ev->window)))
  198. drawtitle(c);
  199. }
  200. }
  201. static void
  202. keypress(XEvent *e)
  203. {
  204. static unsigned int len = sizeof(key) / sizeof(key[0]);
  205. unsigned int i;
  206. KeySym keysym;
  207. XKeyEvent *ev = &e->xkey;
  208. ev->state &= valid_mask;
  209. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  210. for(i = 0; i < len; i++)
  211. if((keysym == key[i].keysym) && ((key[i].mod & valid_mask) == ev->state)) {
  212. if(key[i].func)
  213. key[i].func(&key[i].arg);
  214. return;
  215. }
  216. }
  217. static void
  218. leavenotify(XEvent *e)
  219. {
  220. XCrossingEvent *ev = &e->xcrossing;
  221. if((ev->window == root) && !ev->same_screen)
  222. issel = True;
  223. }
  224. static void
  225. maprequest(XEvent *e)
  226. {
  227. static XWindowAttributes wa;
  228. XMapRequestEvent *ev = &e->xmaprequest;
  229. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  230. return;
  231. if(wa.override_redirect) {
  232. XSelectInput(dpy, ev->window,
  233. (StructureNotifyMask | PropertyChangeMask));
  234. return;
  235. }
  236. if(!getclient(ev->window))
  237. manage(ev->window, &wa);
  238. }
  239. static void
  240. propertynotify(XEvent *e)
  241. {
  242. Client *c;
  243. Window trans;
  244. XPropertyEvent *ev = &e->xproperty;
  245. if(ev->state == PropertyDelete)
  246. return; /* ignore */
  247. if((c = getclient(ev->window))) {
  248. if(ev->atom == wmatom[WMProtocols]) {
  249. c->proto = getproto(c->win);
  250. return;
  251. }
  252. switch (ev->atom) {
  253. default: break;
  254. case XA_WM_TRANSIENT_FOR:
  255. XGetTransientForHint(dpy, c->win, &trans);
  256. if(!c->isfloat && (c->isfloat = (trans != 0)))
  257. arrange(NULL);
  258. break;
  259. case XA_WM_NORMAL_HINTS:
  260. setsize(c);
  261. break;
  262. }
  263. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  264. settitle(c);
  265. drawtitle(c);
  266. }
  267. }
  268. }
  269. static void
  270. unmapnotify(XEvent *e)
  271. {
  272. Client *c;
  273. XUnmapEvent *ev = &e->xunmap;
  274. if((c = getclient(ev->window)))
  275. unmanage(c);
  276. }
  277. /* extern */
  278. void (*handler[LASTEvent]) (XEvent *) = {
  279. [ButtonPress] = buttonpress,
  280. [ConfigureRequest] = configurerequest,
  281. [DestroyNotify] = destroynotify,
  282. [EnterNotify] = enternotify,
  283. [LeaveNotify] = leavenotify,
  284. [Expose] = expose,
  285. [KeyPress] = keypress,
  286. [MapRequest] = maprequest,
  287. [PropertyNotify] = propertynotify,
  288. [UnmapNotify] = unmapnotify
  289. };
  290. void
  291. grabkeys()
  292. {
  293. static unsigned int len = sizeof(key) / sizeof(key[0]);
  294. unsigned int i;
  295. KeyCode code;
  296. for(i = 0; i < len; i++) {
  297. code = XKeysymToKeycode(dpy, key[i].keysym);
  298. XUngrabKey(dpy, code, key[i].mod, root);
  299. XUngrabKey(dpy, code, key[i].mod | NUMLOCKMASK, root);
  300. XUngrabKey(dpy, code, key[i].mod | NUMLOCKMASK | LockMask, root);
  301. XGrabKey(dpy, code, key[i].mod, root, True,
  302. GrabModeAsync, GrabModeAsync);
  303. XGrabKey(dpy, code, key[i].mod | NUMLOCKMASK, root, True,
  304. GrabModeAsync, GrabModeAsync);
  305. XGrabKey(dpy, code, key[i].mod | NUMLOCKMASK | LockMask, root, True,
  306. GrabModeAsync, GrabModeAsync);
  307. }
  308. }