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.

417 lines
8.9 KiB

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