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.

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