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.

366 lines
7.4 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. 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. x = 0;
  96. for(a.i = 0; a.i < ntags; a.i++) {
  97. x += textw(tags[a.i]);
  98. if(ev->x < x) {
  99. if(ev->button == Button3)
  100. toggleview(&a);
  101. else
  102. view(&a);
  103. return;
  104. }
  105. }
  106. }
  107. else if((c = getclient(ev->window))) {
  108. higher(c);
  109. focus(c);
  110. switch(ev->button) {
  111. default:
  112. break;
  113. case Button1:
  114. if(!c->ismax && (arrange == dofloat || c->isfloat))
  115. movemouse(c);
  116. break;
  117. case Button2:
  118. zoom(NULL);
  119. break;
  120. case Button3:
  121. if(!c->ismax && (arrange == dofloat || c->isfloat))
  122. resizemouse(c);
  123. break;
  124. }
  125. }
  126. }
  127. static void
  128. configurerequest(XEvent *e)
  129. {
  130. Client *c;
  131. XConfigureRequestEvent *ev = &e->xconfigurerequest;
  132. XEvent synev;
  133. XWindowChanges wc;
  134. unsigned long newmask;
  135. if((c = getclient(ev->window))) {
  136. gravitate(c, True);
  137. if(ev->value_mask & CWX)
  138. c->x = ev->x;
  139. if(ev->value_mask & CWY)
  140. c->y = ev->y;
  141. if(ev->value_mask & CWWidth)
  142. c->w = ev->width;
  143. if(ev->value_mask & CWHeight)
  144. c->h = ev->height;
  145. if(ev->value_mask & CWBorderWidth)
  146. c->border = ev->border_width;
  147. gravitate(c, False);
  148. wc.x = c->x;
  149. wc.y = c->y;
  150. wc.width = c->w;
  151. wc.height = c->h;
  152. newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
  153. if(newmask)
  154. XConfigureWindow(dpy, c->win, newmask, &wc);
  155. else {
  156. synev.type = ConfigureNotify;
  157. synev.xconfigure.display = dpy;
  158. synev.xconfigure.event = c->win;
  159. synev.xconfigure.window = c->win;
  160. synev.xconfigure.x = c->x;
  161. synev.xconfigure.y = c->y;
  162. synev.xconfigure.width = c->w;
  163. synev.xconfigure.height = c->h;
  164. synev.xconfigure.border_width = c->border;
  165. synev.xconfigure.above = None;
  166. /* Send synthetic ConfigureNotify */
  167. XSendEvent(dpy, c->win, True, NoEventMask, &synev);
  168. }
  169. XSync(dpy, False);
  170. if(c->isfloat)
  171. resize(c, False, TopLeft);
  172. else
  173. arrange(NULL);
  174. }
  175. else {
  176. wc.x = ev->x;
  177. wc.y = ev->y;
  178. wc.width = ev->width;
  179. wc.height = ev->height;
  180. wc.border_width = ev->border_width;
  181. wc.sibling = ev->above;
  182. wc.stack_mode = ev->detail;
  183. XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
  184. XSync(dpy, False);
  185. }
  186. }
  187. static void
  188. destroynotify(XEvent *e)
  189. {
  190. Client *c;
  191. XDestroyWindowEvent *ev = &e->xdestroywindow;
  192. if((c = getclient(ev->window)))
  193. unmanage(c);
  194. }
  195. static void
  196. enternotify(XEvent *e)
  197. {
  198. Client *c;
  199. XCrossingEvent *ev = &e->xcrossing;
  200. if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
  201. return;
  202. if((c = getclient(ev->window)) || (c = getctitle(ev->window)))
  203. focus(c);
  204. else if(ev->window == root) {
  205. issel = True;
  206. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  207. drawall();
  208. }
  209. }
  210. static void
  211. expose(XEvent *e)
  212. {
  213. Client *c;
  214. XExposeEvent *ev = &e->xexpose;
  215. if(ev->count == 0) {
  216. if(barwin == ev->window)
  217. drawstatus();
  218. else if((c = getctitle(ev->window)))
  219. drawtitle(c);
  220. }
  221. }
  222. static void
  223. keypress(XEvent *e)
  224. {
  225. static unsigned int len = sizeof(key) / sizeof(key[0]);
  226. unsigned int i;
  227. KeySym keysym;
  228. XKeyEvent *ev = &e->xkey;
  229. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  230. for(i = 0; i < len; i++)
  231. if(keysym == key[i].keysym &&
  232. CLEANMASK(key[i].mod) == CLEANMASK(ev->state)) {
  233. if(key[i].func)
  234. key[i].func(&key[i].arg);
  235. return;
  236. }
  237. }
  238. static void
  239. leavenotify(XEvent *e)
  240. {
  241. XCrossingEvent *ev = &e->xcrossing;
  242. if((ev->window == root) && !ev->same_screen) {
  243. issel = False;
  244. drawall();
  245. }
  246. }
  247. static void
  248. maprequest(XEvent *e)
  249. {
  250. static XWindowAttributes wa;
  251. XMapRequestEvent *ev = &e->xmaprequest;
  252. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  253. return;
  254. if(wa.override_redirect) {
  255. XSelectInput(dpy, ev->window,
  256. (StructureNotifyMask | PropertyChangeMask));
  257. return;
  258. }
  259. if(!getclient(ev->window))
  260. manage(ev->window, &wa);
  261. }
  262. static void
  263. propertynotify(XEvent *e)
  264. {
  265. Client *c;
  266. Window trans;
  267. XPropertyEvent *ev = &e->xproperty;
  268. if(ev->state == PropertyDelete)
  269. return; /* ignore */
  270. if((c = getclient(ev->window))) {
  271. if(ev->atom == wmatom[WMProtocols]) {
  272. c->proto = getproto(c->win);
  273. return;
  274. }
  275. switch (ev->atom) {
  276. default: break;
  277. case XA_WM_TRANSIENT_FOR:
  278. XGetTransientForHint(dpy, c->win, &trans);
  279. if(!c->isfloat && (c->isfloat = (trans != 0)))
  280. arrange(NULL);
  281. break;
  282. case XA_WM_NORMAL_HINTS:
  283. setsize(c);
  284. break;
  285. }
  286. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  287. settitle(c);
  288. drawtitle(c);
  289. }
  290. }
  291. }
  292. static void
  293. unmapnotify(XEvent *e)
  294. {
  295. Client *c;
  296. XUnmapEvent *ev = &e->xunmap;
  297. if((c = getclient(ev->window)))
  298. unmanage(c);
  299. }
  300. /* extern */
  301. void (*handler[LASTEvent]) (XEvent *) = {
  302. [ButtonPress] = buttonpress,
  303. [ConfigureRequest] = configurerequest,
  304. [DestroyNotify] = destroynotify,
  305. [EnterNotify] = enternotify,
  306. [LeaveNotify] = leavenotify,
  307. [Expose] = expose,
  308. [KeyPress] = keypress,
  309. [MapRequest] = maprequest,
  310. [PropertyNotify] = propertynotify,
  311. [UnmapNotify] = unmapnotify
  312. };
  313. void
  314. grabkeys()
  315. {
  316. static unsigned int len = sizeof(key) / sizeof(key[0]);
  317. unsigned int i;
  318. KeyCode code;
  319. for(i = 0; i < len; i++) {
  320. code = XKeysymToKeycode(dpy, key[i].keysym);
  321. XGrabKey(dpy, code, key[i].mod, root, True,
  322. GrabModeAsync, GrabModeAsync);
  323. XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
  324. GrabModeAsync, GrabModeAsync);
  325. XGrabKey(dpy, code, key[i].mod | NUMLOCKMASK, root, True,
  326. GrabModeAsync, GrabModeAsync);
  327. XGrabKey(dpy, code, key[i].mod | NUMLOCKMASK | LockMask, root, True,
  328. GrabModeAsync, GrabModeAsync);
  329. }
  330. }