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.

383 lines
7.9 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. #include <stdio.h>
  2. /*
  3. * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
  4. * See LICENSE file for license details.
  5. */
  6. #include "dwm.h"
  7. #include <stdlib.h>
  8. #include <X11/keysym.h>
  9. #include <X11/Xatom.h>
  10. /* static */
  11. typedef struct {
  12. unsigned long mod;
  13. KeySym keysym;
  14. void (*func)(Arg *arg);
  15. Arg arg;
  16. } Key;
  17. KEYS
  18. #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
  19. static void
  20. movemouse(Client *c) {
  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. c->ismax = False;
  31. XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
  32. for(;;) {
  33. XMaskEvent(dpy, MOUSEMASK | ExposureMask, &ev);
  34. switch (ev.type) {
  35. case ButtonRelease:
  36. resize(c, True, TopLeft);
  37. XUngrabPointer(dpy, CurrentTime);
  38. return;
  39. case Expose:
  40. handler[Expose](&ev);
  41. break;
  42. case MotionNotify:
  43. XSync(dpy, False);
  44. c->x = ocx + (ev.xmotion.x - x1);
  45. c->y = ocy + (ev.xmotion.y - y1);
  46. resize(c, False, TopLeft);
  47. break;
  48. }
  49. }
  50. }
  51. static void
  52. resizemouse(Client *c) {
  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. c->ismax = False;
  63. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w, c->h);
  64. for(;;) {
  65. XMaskEvent(dpy, MOUSEMASK | ExposureMask, &ev);
  66. switch(ev.type) {
  67. case ButtonRelease:
  68. resize(c, True, TopLeft);
  69. XUngrabPointer(dpy, CurrentTime);
  70. return;
  71. case Expose:
  72. handler[Expose](&ev);
  73. break;
  74. case MotionNotify:
  75. XSync(dpy, False);
  76. if((nw = abs(ocx - ev.xmotion.x)))
  77. c->w = nw;
  78. if((nh = abs(ocy - ev.xmotion.y)))
  79. c->h = nh;
  80. c->x = (ocx <= ev.xmotion.x) ? ocx : ocx - c->w;
  81. c->y = (ocy <= ev.xmotion.y) ? ocy : ocy - c->h;
  82. if(ocx <= ev.xmotion.x)
  83. sticky = (ocy <= ev.xmotion.y) ? TopLeft : BotLeft;
  84. else
  85. sticky = (ocy <= ev.xmotion.y) ? TopRight : BotRight;
  86. resize(c, True, sticky);
  87. break;
  88. }
  89. }
  90. }
  91. static void
  92. buttonpress(XEvent *e) {
  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 == Button1) {
  103. if(ev->state & MODKEY)
  104. tag(&a);
  105. else
  106. view(&a);
  107. }
  108. else if(ev->button == Button3) {
  109. if(ev->state & MODKEY)
  110. toggletag(&a);
  111. else
  112. toggleview(&a);
  113. }
  114. return;
  115. }
  116. }
  117. if(ev->x < x + bmw) {
  118. if(ev->button == Button1)
  119. togglemode(NULL);
  120. }
  121. }
  122. else if((c = getclient(ev->window))) {
  123. focus(c);
  124. if(CLEANMASK(ev->state) != MODKEY)
  125. return;
  126. if(ev->button == Button1 && (arrange == dofloat || c->isfloat)) {
  127. restack();
  128. movemouse(c);
  129. }
  130. else if(ev->button == Button2)
  131. zoom(NULL);
  132. else if(ev->button == Button3 && (arrange == dofloat || c->isfloat)) {
  133. restack();
  134. resizemouse(c);
  135. }
  136. }
  137. }
  138. static void
  139. configurerequest(XEvent *e) {
  140. unsigned long newmask;
  141. Client *c;
  142. XConfigureRequestEvent *ev = &e->xconfigurerequest;
  143. XEvent synev;
  144. XWindowChanges wc;
  145. fputs("configurerequest\n", stderr);
  146. if((c = getclient(ev->window))) {
  147. c->ismax = False;
  148. gravitate(c, True);
  149. if(ev->value_mask & CWX)
  150. c->x = ev->x;
  151. if(ev->value_mask & CWY)
  152. c->y = ev->y;
  153. if(ev->value_mask & CWWidth)
  154. c->w = ev->width;
  155. if(ev->value_mask & CWHeight)
  156. c->h = ev->height;
  157. if(ev->value_mask & CWBorderWidth)
  158. c->border = ev->border_width;
  159. gravitate(c, False);
  160. wc.x = c->x;
  161. wc.y = c->y;
  162. wc.width = c->w;
  163. wc.height = c->h;
  164. newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
  165. if(newmask)
  166. XConfigureWindow(dpy, c->win, newmask, &wc);
  167. else
  168. configure(c);
  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. Client *c;
  190. XDestroyWindowEvent *ev = &e->xdestroywindow;
  191. if((c = getclient(ev->window)))
  192. unmanage(c);
  193. }
  194. static void
  195. enternotify(XEvent *e) {
  196. Client *c;
  197. XCrossingEvent *ev = &e->xcrossing;
  198. fputs("enternotify\n", stderr);
  199. if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
  200. return;
  201. if(((c = getclient(ev->window)) || (c = getctitle(ev->window))) && isvisible(c))
  202. focus(c);
  203. else if(ev->window == root) {
  204. issel = True;
  205. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  206. drawall();
  207. }
  208. }
  209. static void
  210. expose(XEvent *e) {
  211. Client *c;
  212. XExposeEvent *ev = &e->xexpose;
  213. if(ev->count == 0) {
  214. if(barwin == ev->window)
  215. drawstatus();
  216. else if((c = getctitle(ev->window)))
  217. drawtitle(c);
  218. }
  219. }
  220. static void
  221. keypress(XEvent *e) {
  222. static unsigned int len = sizeof(key) / sizeof(key[0]);
  223. unsigned int i;
  224. KeySym keysym;
  225. XKeyEvent *ev = &e->xkey;
  226. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  227. for(i = 0; i < len; i++) {
  228. if(keysym == key[i].keysym
  229. && CLEANMASK(key[i].mod) == CLEANMASK(ev->state))
  230. {
  231. if(key[i].func)
  232. key[i].func(&key[i].arg);
  233. return;
  234. }
  235. }
  236. }
  237. static void
  238. leavenotify(XEvent *e) {
  239. XCrossingEvent *ev = &e->xcrossing;
  240. if((ev->window == root) && !ev->same_screen) {
  241. issel = False;
  242. drawall();
  243. }
  244. }
  245. static void
  246. mappingnotify(XEvent *e) {
  247. XMappingEvent *ev = &e->xmapping;
  248. XRefreshKeyboardMapping(ev);
  249. if(ev->request == MappingKeyboard)
  250. grabkeys();
  251. }
  252. static void
  253. maprequest(XEvent *e) {
  254. static XWindowAttributes wa;
  255. XMapRequestEvent *ev = &e->xmaprequest;
  256. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  257. return;
  258. if(wa.override_redirect) {
  259. XSelectInput(dpy, ev->window,
  260. (StructureNotifyMask | PropertyChangeMask));
  261. return;
  262. }
  263. if(!getclient(ev->window))
  264. manage(ev->window, &wa);
  265. }
  266. static void
  267. propertynotify(XEvent *e) {
  268. Client *c;
  269. Window trans;
  270. XPropertyEvent *ev = &e->xproperty;
  271. fputs("propertynotify\n", stderr);
  272. if(ev->state == PropertyDelete)
  273. return; /* ignore */
  274. if((c = getclient(ev->window))) {
  275. if(ev->atom == wmatom[WMProtocols]) {
  276. c->proto = getproto(c->win);
  277. return;
  278. }
  279. switch (ev->atom) {
  280. default: break;
  281. case XA_WM_TRANSIENT_FOR:
  282. XGetTransientForHint(dpy, c->win, &trans);
  283. if(!c->isfloat && (c->isfloat = (trans != 0)))
  284. arrange(NULL);
  285. break;
  286. case XA_WM_NORMAL_HINTS:
  287. updatesize(c);
  288. break;
  289. }
  290. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  291. updatetitle(c);
  292. drawtitle(c);
  293. }
  294. }
  295. }
  296. static void
  297. unmapnotify(XEvent *e) {
  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. [MappingNotify] = mappingnotify,
  313. [MapRequest] = maprequest,
  314. [PropertyNotify] = propertynotify,
  315. [UnmapNotify] = unmapnotify
  316. };
  317. void
  318. grabkeys(void) {
  319. static unsigned int len = sizeof(key) / sizeof(key[0]);
  320. unsigned int i;
  321. KeyCode code;
  322. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  323. for(i = 0; i < len; i++) {
  324. code = XKeysymToKeycode(dpy, key[i].keysym);
  325. XGrabKey(dpy, code, key[i].mod, root, True,
  326. GrabModeAsync, GrabModeAsync);
  327. XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
  328. GrabModeAsync, GrabModeAsync);
  329. XGrabKey(dpy, code, key[i].mod | numlockmask, root, True,
  330. GrabModeAsync, GrabModeAsync);
  331. XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True,
  332. GrabModeAsync, GrabModeAsync);
  333. }
  334. }
  335. void
  336. procevent(void) {
  337. XEvent ev;
  338. while(XPending(dpy)) {
  339. XNextEvent(dpy, &ev);
  340. if(handler[ev.type])
  341. (handler[ev.type])(&ev); /* call handler */
  342. }
  343. }