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.

399 lines
8.4 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
18 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. XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
  30. for(;;) {
  31. XMaskEvent(dpy, MOUSEMASK | ExposureMask, &ev);
  32. switch (ev.type) {
  33. default:
  34. break;
  35. case Expose:
  36. handler[Expose](&ev);
  37. break;
  38. case MotionNotify:
  39. XSync(dpy, False);
  40. c->x = ocx + (ev.xmotion.x - x1);
  41. c->y = ocy + (ev.xmotion.y - y1);
  42. resize(c, False, TopLeft);
  43. break;
  44. case ButtonRelease:
  45. XUngrabPointer(dpy, CurrentTime);
  46. return;
  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. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w, c->h);
  62. for(;;) {
  63. XMaskEvent(dpy, MOUSEMASK | ExposureMask, &ev);
  64. switch(ev.type) {
  65. default:
  66. break;
  67. case Expose:
  68. handler[Expose](&ev);
  69. break;
  70. case MotionNotify:
  71. XSync(dpy, False);
  72. if((nw = abs(ocx - ev.xmotion.x)))
  73. c->w = abs(ocx - ev.xmotion.x);
  74. if((nh = abs(ocy - ev.xmotion.y)))
  75. c->h = abs(ocy - ev.xmotion.y);
  76. c->x = (ocx <= ev.xmotion.x) ? ocx : ocx - c->w;
  77. c->y = (ocy <= ev.xmotion.y) ? ocy : ocy - c->h;
  78. if(ocx <= ev.xmotion.x)
  79. sticky = (ocy <= ev.xmotion.y) ? TopLeft : BotLeft;
  80. else
  81. sticky = (ocy <= ev.xmotion.y) ? TopRight : BotRight;
  82. resize(c, True, sticky);
  83. break;
  84. case ButtonRelease:
  85. XUngrabPointer(dpy, CurrentTime);
  86. return;
  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(c);
  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(c);
  133. resizemouse(c);
  134. }
  135. }
  136. }
  137. static void
  138. synconfig(Client *c, int x, int y, int w, int h, unsigned int border) {
  139. XEvent synev;
  140. synev.type = ConfigureNotify;
  141. synev.xconfigure.display = dpy;
  142. synev.xconfigure.event = c->win;
  143. synev.xconfigure.window = c->win;
  144. synev.xconfigure.x = x;
  145. synev.xconfigure.y = y;
  146. synev.xconfigure.width = w;
  147. synev.xconfigure.height = h;
  148. synev.xconfigure.border_width = border;
  149. synev.xconfigure.above = None;
  150. XSendEvent(dpy, c->win, True, NoEventMask, &synev);
  151. }
  152. static void
  153. configurerequest(XEvent *e) {
  154. unsigned long newmask;
  155. Client *c;
  156. XConfigureRequestEvent *ev = &e->xconfigurerequest;
  157. XWindowChanges wc;
  158. if((c = getclient(ev->window))) {
  159. if((c == sel) && !c->isfloat && (arrange != dofloat)) {
  160. synconfig(c, sx, sy + bh, sw - 2, sh - 2 - bh, ev->border_width);
  161. XSync(dpy, False);
  162. return;
  163. }
  164. gravitate(c, True);
  165. if(ev->value_mask & CWX)
  166. c->x = ev->x;
  167. if(ev->value_mask & CWY)
  168. c->y = ev->y;
  169. if(ev->value_mask & CWWidth)
  170. c->w = ev->width;
  171. if(ev->value_mask & CWHeight)
  172. c->h = ev->height;
  173. if(ev->value_mask & CWBorderWidth)
  174. c->border = ev->border_width;
  175. gravitate(c, False);
  176. wc.x = c->x;
  177. wc.y = c->y;
  178. wc.width = c->w;
  179. wc.height = c->h;
  180. newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
  181. if(newmask)
  182. XConfigureWindow(dpy, c->win, newmask, &wc);
  183. else
  184. synconfig(c, c->x, c->y, c->w, c->h, c->border);
  185. XSync(dpy, False);
  186. if(c->isfloat)
  187. resize(c, False, TopLeft);
  188. else
  189. arrange(NULL);
  190. }
  191. else {
  192. wc.x = ev->x;
  193. wc.y = ev->y;
  194. wc.width = ev->width;
  195. wc.height = ev->height;
  196. wc.border_width = ev->border_width;
  197. wc.sibling = ev->above;
  198. wc.stack_mode = ev->detail;
  199. XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
  200. XSync(dpy, False);
  201. }
  202. }
  203. static void
  204. destroynotify(XEvent *e) {
  205. Client *c;
  206. XDestroyWindowEvent *ev = &e->xdestroywindow;
  207. if((c = getclient(ev->window)))
  208. unmanage(c);
  209. }
  210. static void
  211. enternotify(XEvent *e) {
  212. Client *c;
  213. XCrossingEvent *ev = &e->xcrossing;
  214. if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
  215. return;
  216. if(((c = getclient(ev->window)) || (c = getctitle(ev->window))) && isvisible(c))
  217. focus(c);
  218. else if(ev->window == root) {
  219. issel = True;
  220. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  221. drawall();
  222. }
  223. }
  224. static void
  225. expose(XEvent *e) {
  226. Client *c;
  227. XExposeEvent *ev = &e->xexpose;
  228. if(ev->count == 0) {
  229. if(barwin == ev->window)
  230. drawstatus();
  231. else if((c = getctitle(ev->window)))
  232. drawtitle(c);
  233. }
  234. }
  235. static void
  236. keypress(XEvent *e) {
  237. static unsigned int len = sizeof(key) / sizeof(key[0]);
  238. unsigned int i;
  239. KeySym keysym;
  240. XKeyEvent *ev = &e->xkey;
  241. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  242. for(i = 0; i < len; i++) {
  243. if(keysym == key[i].keysym
  244. && CLEANMASK(key[i].mod) == CLEANMASK(ev->state))
  245. {
  246. if(key[i].func)
  247. key[i].func(&key[i].arg);
  248. return;
  249. }
  250. }
  251. }
  252. static void
  253. leavenotify(XEvent *e) {
  254. XCrossingEvent *ev = &e->xcrossing;
  255. if((ev->window == root) && !ev->same_screen) {
  256. issel = False;
  257. drawall();
  258. }
  259. }
  260. static void
  261. mappingnotify(XEvent *e) {
  262. XMappingEvent *ev = &e->xmapping;
  263. XRefreshKeyboardMapping(ev);
  264. if(ev->request == MappingKeyboard)
  265. grabkeys();
  266. }
  267. static void
  268. maprequest(XEvent *e) {
  269. static XWindowAttributes wa;
  270. XMapRequestEvent *ev = &e->xmaprequest;
  271. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  272. return;
  273. if(wa.override_redirect) {
  274. XSelectInput(dpy, ev->window,
  275. (StructureNotifyMask | PropertyChangeMask));
  276. return;
  277. }
  278. if(!getclient(ev->window))
  279. manage(ev->window, &wa);
  280. }
  281. static void
  282. propertynotify(XEvent *e) {
  283. Client *c;
  284. Window trans;
  285. XPropertyEvent *ev = &e->xproperty;
  286. if(ev->state == PropertyDelete)
  287. return; /* ignore */
  288. if((c = getclient(ev->window))) {
  289. if(ev->atom == wmatom[WMProtocols]) {
  290. c->proto = getproto(c->win);
  291. return;
  292. }
  293. switch (ev->atom) {
  294. default: break;
  295. case XA_WM_TRANSIENT_FOR:
  296. XGetTransientForHint(dpy, c->win, &trans);
  297. if(!c->isfloat && (c->isfloat = (trans != 0)))
  298. arrange(NULL);
  299. break;
  300. case XA_WM_NORMAL_HINTS:
  301. updatesize(c);
  302. break;
  303. }
  304. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  305. updatetitle(c);
  306. drawtitle(c);
  307. }
  308. }
  309. }
  310. static void
  311. unmapnotify(XEvent *e) {
  312. Client *c;
  313. XUnmapEvent *ev = &e->xunmap;
  314. if((c = getclient(ev->window)))
  315. unmanage(c);
  316. }
  317. /* extern */
  318. void (*handler[LASTEvent]) (XEvent *) = {
  319. [ButtonPress] = buttonpress,
  320. [ConfigureRequest] = configurerequest,
  321. [DestroyNotify] = destroynotify,
  322. [EnterNotify] = enternotify,
  323. [LeaveNotify] = leavenotify,
  324. [Expose] = expose,
  325. [KeyPress] = keypress,
  326. [MappingNotify] = mappingnotify,
  327. [MapRequest] = maprequest,
  328. [PropertyNotify] = propertynotify,
  329. [UnmapNotify] = unmapnotify
  330. };
  331. void
  332. grabkeys() {
  333. static unsigned int len = sizeof(key) / sizeof(key[0]);
  334. unsigned int i;
  335. KeyCode code;
  336. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  337. for(i = 0; i < len; i++) {
  338. code = XKeysymToKeycode(dpy, key[i].keysym);
  339. XGrabKey(dpy, code, key[i].mod, root, True,
  340. GrabModeAsync, GrabModeAsync);
  341. XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
  342. GrabModeAsync, GrabModeAsync);
  343. XGrabKey(dpy, code, key[i].mod | numlockmask, root, True,
  344. GrabModeAsync, GrabModeAsync);
  345. XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True,
  346. GrabModeAsync, GrabModeAsync);
  347. }
  348. }
  349. void
  350. procevent() {
  351. XEvent ev;
  352. while(XPending(dpy)) {
  353. XNextEvent(dpy, &ev);
  354. if(handler[ev.type])
  355. (handler[ev.type])(&ev); /* call handler */
  356. }
  357. }