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.

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