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.

384 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
  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. else if(ev->button == Button3)
  120. togglestackpos(NULL);
  121. }
  122. }
  123. else if((c = getclient(ev->window))) {
  124. focus(c);
  125. if(CLEANMASK(ev->state) != MODKEY)
  126. return;
  127. if(ev->button == Button1 && (arrange == dofloat || c->isfloat)) {
  128. restack();
  129. movemouse(c);
  130. }
  131. else if(ev->button == Button2)
  132. zoom(NULL);
  133. else if(ev->button == Button3 && (arrange == dofloat || c->isfloat)) {
  134. restack();
  135. resizemouse(c);
  136. }
  137. }
  138. }
  139. static void
  140. configurerequest(XEvent *e) {
  141. unsigned long newmask;
  142. Client *c;
  143. XConfigureRequestEvent *ev = &e->xconfigurerequest;
  144. XWindowChanges wc;
  145. if((c = getclient(ev->window))) {
  146. c->ismax = False;
  147. gravitate(c, True);
  148. if(ev->value_mask & CWX)
  149. c->x = ev->x;
  150. if(ev->value_mask & CWY)
  151. c->y = ev->y;
  152. if(ev->value_mask & CWWidth)
  153. c->w = ev->width;
  154. if(ev->value_mask & CWHeight)
  155. c->h = ev->height;
  156. if(ev->value_mask & CWBorderWidth)
  157. c->border = ev->border_width;
  158. gravitate(c, False);
  159. wc.x = c->x;
  160. wc.y = c->y;
  161. wc.width = c->w;
  162. wc.height = c->h;
  163. newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
  164. if(newmask)
  165. XConfigureWindow(dpy, c->win, newmask, &wc);
  166. else
  167. configure(c);
  168. XSync(dpy, False);
  169. if(c->isfloat) {
  170. resize(c, False, TopLeft);
  171. if(!isvisible(c))
  172. ban(c);
  173. }
  174. else
  175. arrange(NULL);
  176. }
  177. else {
  178. wc.x = ev->x;
  179. wc.y = ev->y;
  180. wc.width = ev->width;
  181. wc.height = ev->height;
  182. wc.border_width = ev->border_width;
  183. wc.sibling = ev->above;
  184. wc.stack_mode = ev->detail;
  185. XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
  186. XSync(dpy, False);
  187. }
  188. }
  189. static void
  190. destroynotify(XEvent *e) {
  191. Client *c;
  192. XDestroyWindowEvent *ev = &e->xdestroywindow;
  193. if((c = getclient(ev->window)))
  194. unmanage(c);
  195. }
  196. static void
  197. enternotify(XEvent *e) {
  198. Client *c;
  199. XCrossingEvent *ev = &e->xcrossing;
  200. if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
  201. return;
  202. if(((c = getclient(ev->window)) || (c = getctitle(ev->window))) && isvisible(c))
  203. focus(c);
  204. else if(ev->window == root) {
  205. issel = True;
  206. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  207. drawall();
  208. }
  209. }
  210. static void
  211. expose(XEvent *e) {
  212. Client *c;
  213. XExposeEvent *ev = &e->xexpose;
  214. if(ev->count == 0) {
  215. if(barwin == ev->window)
  216. drawstatus();
  217. else if((c = getctitle(ev->window)))
  218. drawtitle(c);
  219. }
  220. }
  221. static void
  222. keypress(XEvent *e) {
  223. static unsigned int len = sizeof(key) / sizeof(key[0]);
  224. unsigned int i;
  225. KeySym keysym;
  226. XKeyEvent *ev = &e->xkey;
  227. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  228. for(i = 0; i < len; i++) {
  229. if(keysym == key[i].keysym
  230. && CLEANMASK(key[i].mod) == CLEANMASK(ev->state))
  231. {
  232. if(key[i].func)
  233. key[i].func(&key[i].arg);
  234. return;
  235. }
  236. }
  237. }
  238. static void
  239. leavenotify(XEvent *e) {
  240. XCrossingEvent *ev = &e->xcrossing;
  241. if((ev->window == root) && !ev->same_screen) {
  242. issel = False;
  243. drawall();
  244. }
  245. }
  246. static void
  247. mappingnotify(XEvent *e) {
  248. XMappingEvent *ev = &e->xmapping;
  249. XRefreshKeyboardMapping(ev);
  250. if(ev->request == MappingKeyboard)
  251. grabkeys();
  252. }
  253. static void
  254. maprequest(XEvent *e) {
  255. static XWindowAttributes wa;
  256. XMapRequestEvent *ev = &e->xmaprequest;
  257. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  258. return;
  259. if(wa.override_redirect) {
  260. XSelectInput(dpy, ev->window,
  261. (StructureNotifyMask | PropertyChangeMask));
  262. return;
  263. }
  264. if(!getclient(ev->window))
  265. manage(ev->window, &wa);
  266. }
  267. static void
  268. propertynotify(XEvent *e) {
  269. Client *c;
  270. Window trans;
  271. XPropertyEvent *ev = &e->xproperty;
  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. resizetitle(c);
  293. drawtitle(c);
  294. }
  295. }
  296. }
  297. static void
  298. unmapnotify(XEvent *e) {
  299. Client *c;
  300. XUnmapEvent *ev = &e->xunmap;
  301. if((c = getclient(ev->window)))
  302. unmanage(c);
  303. }
  304. /* extern */
  305. void (*handler[LASTEvent]) (XEvent *) = {
  306. [ButtonPress] = buttonpress,
  307. [ConfigureRequest] = configurerequest,
  308. [DestroyNotify] = destroynotify,
  309. [EnterNotify] = enternotify,
  310. [LeaveNotify] = leavenotify,
  311. [Expose] = expose,
  312. [KeyPress] = keypress,
  313. [MappingNotify] = mappingnotify,
  314. [MapRequest] = maprequest,
  315. [PropertyNotify] = propertynotify,
  316. [UnmapNotify] = unmapnotify
  317. };
  318. void
  319. grabkeys(void) {
  320. static unsigned int len = sizeof(key) / sizeof(key[0]);
  321. unsigned int i;
  322. KeyCode code;
  323. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  324. for(i = 0; i < len; i++) {
  325. code = XKeysymToKeycode(dpy, key[i].keysym);
  326. XGrabKey(dpy, code, key[i].mod, root, True,
  327. GrabModeAsync, GrabModeAsync);
  328. XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
  329. GrabModeAsync, GrabModeAsync);
  330. XGrabKey(dpy, code, key[i].mod | numlockmask, root, True,
  331. GrabModeAsync, GrabModeAsync);
  332. XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True,
  333. GrabModeAsync, GrabModeAsync);
  334. }
  335. }
  336. void
  337. procevent(void) {
  338. XEvent ev;
  339. while(XPending(dpy)) {
  340. XNextEvent(dpy, &ev);
  341. if(handler[ev.type])
  342. (handler[ev.type])(&ev); /* call handler */
  343. }
  344. }