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.

376 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
  1. /* (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
  2. * See LICENSE file for license details.
  3. */
  4. #include "dwm.h"
  5. #include <stdlib.h>
  6. #include <X11/keysym.h>
  7. #include <X11/Xatom.h>
  8. /* static */
  9. typedef struct {
  10. unsigned long mod;
  11. KeySym keysym;
  12. void (*func)(Arg *arg);
  13. Arg arg;
  14. } Key;
  15. KEYS
  16. #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
  17. #define MOUSEMASK (BUTTONMASK | PointerMotionMask)
  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) && (ev->button == Button1))
  117. togglemode(NULL);
  118. }
  119. else if((c = getclient(ev->window))) {
  120. focus(c);
  121. if(CLEANMASK(ev->state) != MODKEY)
  122. return;
  123. if(ev->button == Button1 && (arrange == dofloat || c->isfloat)) {
  124. restack();
  125. movemouse(c);
  126. }
  127. else if(ev->button == Button2)
  128. zoom(NULL);
  129. else if(ev->button == Button3 && (arrange == dofloat || c->isfloat)) {
  130. restack();
  131. resizemouse(c);
  132. }
  133. }
  134. }
  135. static void
  136. configurerequest(XEvent *e) {
  137. unsigned long newmask;
  138. Client *c;
  139. XConfigureRequestEvent *ev = &e->xconfigurerequest;
  140. XWindowChanges wc;
  141. if((c = getclient(ev->window))) {
  142. c->ismax = False;
  143. gravitate(c, True);
  144. if(ev->value_mask & CWX)
  145. c->x = ev->x;
  146. if(ev->value_mask & CWY)
  147. c->y = ev->y;
  148. if(ev->value_mask & CWWidth)
  149. c->w = ev->width;
  150. if(ev->value_mask & CWHeight)
  151. c->h = ev->height;
  152. if(ev->value_mask & CWBorderWidth)
  153. c->border = ev->border_width;
  154. gravitate(c, False);
  155. wc.x = c->x;
  156. wc.y = c->y;
  157. wc.width = c->w;
  158. wc.height = c->h;
  159. newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
  160. if(newmask)
  161. XConfigureWindow(dpy, c->win, newmask, &wc);
  162. else
  163. configure(c);
  164. XSync(dpy, False);
  165. if(c->isfloat) {
  166. resize(c, False, TopLeft);
  167. if(!isvisible(c))
  168. ban(c);
  169. }
  170. else
  171. arrange();
  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();
  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. resizetitle(c);
  289. drawtitle(c);
  290. }
  291. }
  292. }
  293. static void
  294. unmapnotify(XEvent *e) {
  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. [MappingNotify] = mappingnotify,
  310. [MapRequest] = maprequest,
  311. [PropertyNotify] = propertynotify,
  312. [UnmapNotify] = unmapnotify
  313. };
  314. void
  315. grabkeys(void) {
  316. static unsigned int len = sizeof(key) / sizeof(key[0]);
  317. unsigned int i;
  318. KeyCode code;
  319. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  320. for(i = 0; i < len; i++) {
  321. code = XKeysymToKeycode(dpy, key[i].keysym);
  322. XGrabKey(dpy, code, key[i].mod, root, True,
  323. GrabModeAsync, GrabModeAsync);
  324. XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
  325. GrabModeAsync, GrabModeAsync);
  326. XGrabKey(dpy, code, key[i].mod | numlockmask, root, True,
  327. GrabModeAsync, GrabModeAsync);
  328. XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True,
  329. GrabModeAsync, GrabModeAsync);
  330. }
  331. }
  332. void
  333. procevent(void) {
  334. XEvent ev;
  335. while(XPending(dpy)) {
  336. XNextEvent(dpy, &ev);
  337. if(handler[ev.type])
  338. (handler[ev.type])(&ev); /* call handler */
  339. }
  340. }