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.

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