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.

378 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. XWindowChanges wc;
  143. if((c = getclient(ev->window))) {
  144. c->ismax = False;
  145. gravitate(c, True);
  146. if(ev->value_mask & CWX)
  147. c->x = ev->x;
  148. if(ev->value_mask & CWY)
  149. c->y = ev->y;
  150. if(ev->value_mask & CWWidth)
  151. c->w = ev->width;
  152. if(ev->value_mask & CWHeight)
  153. c->h = ev->height;
  154. if(ev->value_mask & CWBorderWidth)
  155. c->border = ev->border_width;
  156. gravitate(c, False);
  157. wc.x = c->x;
  158. wc.y = c->y;
  159. wc.width = c->w;
  160. wc.height = c->h;
  161. newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
  162. if(newmask)
  163. XConfigureWindow(dpy, c->win, newmask, &wc);
  164. else
  165. configure(c);
  166. XSync(dpy, False);
  167. if(c->isfloat)
  168. resize(c, False, TopLeft);
  169. else
  170. arrange(NULL);
  171. }
  172. else {
  173. wc.x = ev->x;
  174. wc.y = ev->y;
  175. wc.width = ev->width;
  176. wc.height = ev->height;
  177. wc.border_width = ev->border_width;
  178. wc.sibling = ev->above;
  179. wc.stack_mode = ev->detail;
  180. XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
  181. XSync(dpy, False);
  182. }
  183. }
  184. static void
  185. destroynotify(XEvent *e) {
  186. Client *c;
  187. XDestroyWindowEvent *ev = &e->xdestroywindow;
  188. if((c = getclient(ev->window)))
  189. unmanage(c);
  190. }
  191. static void
  192. enternotify(XEvent *e) {
  193. Client *c;
  194. XCrossingEvent *ev = &e->xcrossing;
  195. if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
  196. return;
  197. if(((c = getclient(ev->window)) || (c = getctitle(ev->window))) && isvisible(c))
  198. focus(c);
  199. else if(ev->window == root) {
  200. issel = True;
  201. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  202. drawall();
  203. }
  204. }
  205. static void
  206. expose(XEvent *e) {
  207. Client *c;
  208. XExposeEvent *ev = &e->xexpose;
  209. if(ev->count == 0) {
  210. if(barwin == ev->window)
  211. drawstatus();
  212. else if((c = getctitle(ev->window)))
  213. drawtitle(c);
  214. }
  215. }
  216. static void
  217. keypress(XEvent *e) {
  218. static unsigned int len = sizeof(key) / sizeof(key[0]);
  219. unsigned int i;
  220. KeySym keysym;
  221. XKeyEvent *ev = &e->xkey;
  222. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  223. for(i = 0; i < len; i++) {
  224. if(keysym == key[i].keysym
  225. && CLEANMASK(key[i].mod) == CLEANMASK(ev->state))
  226. {
  227. if(key[i].func)
  228. key[i].func(&key[i].arg);
  229. return;
  230. }
  231. }
  232. }
  233. static void
  234. leavenotify(XEvent *e) {
  235. XCrossingEvent *ev = &e->xcrossing;
  236. if((ev->window == root) && !ev->same_screen) {
  237. issel = False;
  238. drawall();
  239. }
  240. }
  241. static void
  242. mappingnotify(XEvent *e) {
  243. XMappingEvent *ev = &e->xmapping;
  244. XRefreshKeyboardMapping(ev);
  245. if(ev->request == MappingKeyboard)
  246. grabkeys();
  247. }
  248. static void
  249. maprequest(XEvent *e) {
  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. Client *c;
  265. Window trans;
  266. XPropertyEvent *ev = &e->xproperty;
  267. if(ev->state == PropertyDelete)
  268. return; /* ignore */
  269. if((c = getclient(ev->window))) {
  270. if(ev->atom == wmatom[WMProtocols]) {
  271. c->proto = getproto(c->win);
  272. return;
  273. }
  274. switch (ev->atom) {
  275. default: break;
  276. case XA_WM_TRANSIENT_FOR:
  277. XGetTransientForHint(dpy, c->win, &trans);
  278. if(!c->isfloat && (c->isfloat = (trans != 0)))
  279. arrange(NULL);
  280. break;
  281. case XA_WM_NORMAL_HINTS:
  282. updatesize(c);
  283. break;
  284. }
  285. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  286. updatetitle(c);
  287. drawtitle(c);
  288. }
  289. }
  290. }
  291. static void
  292. unmapnotify(XEvent *e) {
  293. Client *c;
  294. XUnmapEvent *ev = &e->xunmap;
  295. if((c = getclient(ev->window)))
  296. unmanage(c);
  297. }
  298. /* extern */
  299. void (*handler[LASTEvent]) (XEvent *) = {
  300. [ButtonPress] = buttonpress,
  301. [ConfigureRequest] = configurerequest,
  302. [DestroyNotify] = destroynotify,
  303. [EnterNotify] = enternotify,
  304. [LeaveNotify] = leavenotify,
  305. [Expose] = expose,
  306. [KeyPress] = keypress,
  307. [MappingNotify] = mappingnotify,
  308. [MapRequest] = maprequest,
  309. [PropertyNotify] = propertynotify,
  310. [UnmapNotify] = unmapnotify
  311. };
  312. void
  313. grabkeys(void) {
  314. static unsigned int len = sizeof(key) / sizeof(key[0]);
  315. unsigned int i;
  316. KeyCode code;
  317. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  318. for(i = 0; i < len; i++) {
  319. code = XKeysymToKeycode(dpy, key[i].keysym);
  320. XGrabKey(dpy, code, key[i].mod, root, True,
  321. GrabModeAsync, GrabModeAsync);
  322. XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
  323. GrabModeAsync, GrabModeAsync);
  324. XGrabKey(dpy, code, key[i].mod | numlockmask, root, True,
  325. GrabModeAsync, GrabModeAsync);
  326. XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True,
  327. GrabModeAsync, GrabModeAsync);
  328. }
  329. }
  330. void
  331. procevent(void) {
  332. XEvent ev;
  333. while(XPending(dpy)) {
  334. XNextEvent(dpy, &ev);
  335. if(handler[ev.type])
  336. (handler[ev.type])(&ev); /* call handler */
  337. }
  338. }