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.

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