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.

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