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.

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