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.

379 lines
7.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
  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. int x1, y1, ocx, ocy, di;
  21. unsigned int dui;
  22. Window dummy;
  23. XEvent ev;
  24. ocx = c->x;
  25. ocy = c->y;
  26. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  27. None, cursor[CurMove], CurrentTime) != GrabSuccess)
  28. return;
  29. c->ismax = False;
  30. XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
  31. for(;;) {
  32. XMaskEvent(dpy, MOUSEMASK | ExposureMask, &ev);
  33. switch (ev.type) {
  34. case ButtonRelease:
  35. resize(c, True, TopLeft);
  36. XUngrabPointer(dpy, CurrentTime);
  37. return;
  38. case Expose:
  39. handler[Expose](&ev);
  40. break;
  41. case MotionNotify:
  42. XSync(dpy, False);
  43. c->x = ocx + (ev.xmotion.x - x1);
  44. c->y = ocy + (ev.xmotion.y - y1);
  45. resize(c, False, TopLeft);
  46. break;
  47. }
  48. }
  49. }
  50. static void
  51. resizemouse(Client *c) {
  52. int ocx, ocy;
  53. int nw, nh;
  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. c->ismax = False;
  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. case ButtonRelease:
  67. resize(c, True, TopLeft);
  68. XUngrabPointer(dpy, CurrentTime);
  69. return;
  70. case Expose:
  71. handler[Expose](&ev);
  72. break;
  73. case MotionNotify:
  74. XSync(dpy, False);
  75. if((nw = abs(ocx - ev.xmotion.x)))
  76. c->w = nw;
  77. if((nh = abs(ocy - ev.xmotion.y)))
  78. c->h = nh;
  79. c->x = (ocx <= ev.xmotion.x) ? ocx : ocx - c->w;
  80. c->y = (ocy <= ev.xmotion.y) ? ocy : ocy - c->h;
  81. if(ocx <= ev.xmotion.x)
  82. sticky = (ocy <= ev.xmotion.y) ? TopLeft : BotLeft;
  83. else
  84. sticky = (ocy <= ev.xmotion.y) ? TopRight : BotRight;
  85. resize(c, True, sticky);
  86. break;
  87. }
  88. }
  89. }
  90. static void
  91. buttonpress(XEvent *e) {
  92. int x;
  93. Arg a;
  94. Client *c;
  95. XButtonPressedEvent *ev = &e->xbutton;
  96. if(barwin == ev->window) {
  97. x = 0;
  98. for(a.i = 0; a.i < ntags; a.i++) {
  99. x += textw(tags[a.i]);
  100. if(ev->x < x) {
  101. if(ev->button == Button1) {
  102. if(ev->state & MODKEY)
  103. tag(&a);
  104. else
  105. view(&a);
  106. }
  107. else if(ev->button == Button3) {
  108. if(ev->state & MODKEY)
  109. toggletag(&a);
  110. else
  111. toggleview(&a);
  112. }
  113. return;
  114. }
  115. }
  116. if(ev->x < x + bmw) {
  117. if(ev->button == Button1)
  118. togglemode(NULL);
  119. }
  120. }
  121. else if((c = getclient(ev->window))) {
  122. focus(c);
  123. if(CLEANMASK(ev->state) != MODKEY)
  124. return;
  125. if(ev->button == Button1 && (arrange == dofloat || c->isfloat)) {
  126. restack();
  127. movemouse(c);
  128. }
  129. else if(ev->button == Button2)
  130. zoom(NULL);
  131. else if(ev->button == Button3 && (arrange == dofloat || c->isfloat)) {
  132. restack();
  133. resizemouse(c);
  134. }
  135. }
  136. }
  137. static void
  138. configurerequest(XEvent *e) {
  139. unsigned long newmask;
  140. Client *c;
  141. XConfigureRequestEvent *ev = &e->xconfigurerequest;
  142. XEvent synev;
  143. XWindowChanges wc;
  144. if((c = getclient(ev->window))) {
  145. c->ismax = False;
  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 = ev->border_width;
  157. gravitate(c, False);
  158. wc.x = c->x;
  159. wc.y = c->y;
  160. wc.width = c->w;
  161. wc.height = c->h;
  162. newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
  163. if(newmask)
  164. XConfigureWindow(dpy, c->win, newmask, &wc);
  165. else
  166. configure(c);
  167. XSync(dpy, False);
  168. if(c->isfloat)
  169. resize(c, False, TopLeft);
  170. else
  171. arrange(NULL);
  172. }
  173. else {
  174. wc.x = ev->x;
  175. wc.y = ev->y;
  176. wc.width = ev->width;
  177. wc.height = ev->height;
  178. wc.border_width = ev->border_width;
  179. wc.sibling = ev->above;
  180. wc.stack_mode = ev->detail;
  181. XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
  182. XSync(dpy, False);
  183. }
  184. }
  185. static void
  186. destroynotify(XEvent *e) {
  187. Client *c;
  188. XDestroyWindowEvent *ev = &e->xdestroywindow;
  189. if((c = getclient(ev->window)))
  190. unmanage(c);
  191. }
  192. static void
  193. enternotify(XEvent *e) {
  194. Client *c;
  195. XCrossingEvent *ev = &e->xcrossing;
  196. if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
  197. return;
  198. if(((c = getclient(ev->window)) || (c = getctitle(ev->window))) && isvisible(c))
  199. focus(c);
  200. else if(ev->window == root) {
  201. issel = True;
  202. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  203. drawall();
  204. }
  205. }
  206. static void
  207. expose(XEvent *e) {
  208. Client *c;
  209. XExposeEvent *ev = &e->xexpose;
  210. if(ev->count == 0) {
  211. if(barwin == ev->window)
  212. drawstatus();
  213. else if((c = getctitle(ev->window)))
  214. drawtitle(c);
  215. }
  216. }
  217. static void
  218. keypress(XEvent *e) {
  219. static unsigned int len = sizeof(key) / sizeof(key[0]);
  220. unsigned int i;
  221. KeySym keysym;
  222. XKeyEvent *ev = &e->xkey;
  223. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  224. for(i = 0; i < len; i++) {
  225. if(keysym == key[i].keysym
  226. && CLEANMASK(key[i].mod) == CLEANMASK(ev->state))
  227. {
  228. if(key[i].func)
  229. key[i].func(&key[i].arg);
  230. return;
  231. }
  232. }
  233. }
  234. static void
  235. leavenotify(XEvent *e) {
  236. XCrossingEvent *ev = &e->xcrossing;
  237. if((ev->window == root) && !ev->same_screen) {
  238. issel = False;
  239. drawall();
  240. }
  241. }
  242. static void
  243. mappingnotify(XEvent *e) {
  244. XMappingEvent *ev = &e->xmapping;
  245. XRefreshKeyboardMapping(ev);
  246. if(ev->request == MappingKeyboard)
  247. grabkeys();
  248. }
  249. static void
  250. maprequest(XEvent *e) {
  251. static XWindowAttributes wa;
  252. XMapRequestEvent *ev = &e->xmaprequest;
  253. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  254. return;
  255. if(wa.override_redirect) {
  256. XSelectInput(dpy, ev->window,
  257. (StructureNotifyMask | PropertyChangeMask));
  258. return;
  259. }
  260. if(!getclient(ev->window))
  261. manage(ev->window, &wa);
  262. }
  263. static void
  264. propertynotify(XEvent *e) {
  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. updatesize(c);
  284. break;
  285. }
  286. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  287. updatetitle(c);
  288. drawtitle(c);
  289. }
  290. }
  291. }
  292. static void
  293. unmapnotify(XEvent *e) {
  294. Client *c;
  295. XUnmapEvent *ev = &e->xunmap;
  296. if((c = getclient(ev->window)))
  297. unmanage(c);
  298. }
  299. /* extern */
  300. void (*handler[LASTEvent]) (XEvent *) = {
  301. [ButtonPress] = buttonpress,
  302. [ConfigureRequest] = configurerequest,
  303. [DestroyNotify] = destroynotify,
  304. [EnterNotify] = enternotify,
  305. [LeaveNotify] = leavenotify,
  306. [Expose] = expose,
  307. [KeyPress] = keypress,
  308. [MappingNotify] = mappingnotify,
  309. [MapRequest] = maprequest,
  310. [PropertyNotify] = propertynotify,
  311. [UnmapNotify] = unmapnotify
  312. };
  313. void
  314. grabkeys(void) {
  315. static unsigned int len = sizeof(key) / sizeof(key[0]);
  316. unsigned int i;
  317. KeyCode code;
  318. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  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. }
  331. void
  332. procevent(void) {
  333. XEvent ev;
  334. while(XPending(dpy)) {
  335. XNextEvent(dpy, &ev);
  336. if(handler[ev.type])
  337. (handler[ev.type])(&ev); /* call handler */
  338. }
  339. }