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.

422 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. view(&a);
  105. break;
  106. case Button2:
  107. toggletag(&a);
  108. break;
  109. case Button3:
  110. toggleview(&a);
  111. break;
  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. switch(ev->button) {
  126. default:
  127. break;
  128. case Button1:
  129. if(!c->ismax && (arrange == dofloat || c->isfloat)) {
  130. restack(c);
  131. movemouse(c);
  132. }
  133. break;
  134. case Button2:
  135. zoom(NULL);
  136. break;
  137. case Button3:
  138. if(!c->ismax && (arrange == dofloat || c->isfloat)) {
  139. restack(c);
  140. resizemouse(c);
  141. }
  142. break;
  143. }
  144. }
  145. }
  146. static void
  147. synconfig(Client *c, int x, int y, int w, int h, unsigned int border)
  148. {
  149. XEvent synev;
  150. synev.type = ConfigureNotify;
  151. synev.xconfigure.display = dpy;
  152. synev.xconfigure.event = c->win;
  153. synev.xconfigure.window = c->win;
  154. synev.xconfigure.x = x;
  155. synev.xconfigure.y = y;
  156. synev.xconfigure.width = w;
  157. synev.xconfigure.height = h;
  158. synev.xconfigure.border_width = border;
  159. synev.xconfigure.above = None;
  160. XSendEvent(dpy, c->win, True, NoEventMask, &synev);
  161. }
  162. static void
  163. configurerequest(XEvent *e)
  164. {
  165. unsigned long newmask;
  166. Client *c;
  167. XConfigureRequestEvent *ev = &e->xconfigurerequest;
  168. XWindowChanges wc;
  169. if((c = getclient(ev->window))) {
  170. if(!c->isfloat && (arrange != dofloat) && c->ismax) {
  171. synconfig(c, sx, sy + bh, sw - 2, sh - 2 - bh, ev->border_width);
  172. XSync(dpy, False);
  173. return;
  174. }
  175. gravitate(c, True);
  176. if(ev->value_mask & CWX)
  177. c->x = ev->x;
  178. if(ev->value_mask & CWY)
  179. c->y = ev->y;
  180. if(ev->value_mask & CWWidth)
  181. c->w = ev->width;
  182. if(ev->value_mask & CWHeight)
  183. c->h = ev->height;
  184. if(ev->value_mask & CWBorderWidth)
  185. c->border = ev->border_width;
  186. gravitate(c, False);
  187. wc.x = c->x;
  188. wc.y = c->y;
  189. wc.width = c->w;
  190. wc.height = c->h;
  191. newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
  192. if(newmask)
  193. XConfigureWindow(dpy, c->win, newmask, &wc);
  194. else
  195. synconfig(c, c->x, c->y, c->w, c->h, c->border);
  196. XSync(dpy, False);
  197. if(c->isfloat)
  198. resize(c, False, TopLeft);
  199. else
  200. arrange(NULL);
  201. }
  202. else {
  203. wc.x = ev->x;
  204. wc.y = ev->y;
  205. wc.width = ev->width;
  206. wc.height = ev->height;
  207. wc.border_width = ev->border_width;
  208. wc.sibling = ev->above;
  209. wc.stack_mode = ev->detail;
  210. XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
  211. XSync(dpy, False);
  212. }
  213. }
  214. static void
  215. destroynotify(XEvent *e)
  216. {
  217. Client *c;
  218. XDestroyWindowEvent *ev = &e->xdestroywindow;
  219. if((c = getclient(ev->window)))
  220. unmanage(c);
  221. }
  222. static void
  223. enternotify(XEvent *e)
  224. {
  225. Client *c;
  226. XCrossingEvent *ev = &e->xcrossing;
  227. if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
  228. return;
  229. if((c = getclient(ev->window)) || (c = getctitle(ev->window)))
  230. focus(c);
  231. else if(ev->window == root) {
  232. issel = True;
  233. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  234. drawall();
  235. }
  236. }
  237. static void
  238. expose(XEvent *e)
  239. {
  240. Client *c;
  241. XExposeEvent *ev = &e->xexpose;
  242. if(ev->count == 0) {
  243. if(barwin == ev->window)
  244. drawstatus();
  245. else if((c = getctitle(ev->window)))
  246. drawtitle(c);
  247. }
  248. }
  249. static void
  250. keypress(XEvent *e)
  251. {
  252. static unsigned int len = sizeof(key) / sizeof(key[0]);
  253. unsigned int i;
  254. KeySym keysym;
  255. XKeyEvent *ev = &e->xkey;
  256. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  257. for(i = 0; i < len; i++) {
  258. if(keysym == key[i].keysym &&
  259. CLEANMASK(key[i].mod) == CLEANMASK(ev->state))
  260. {
  261. if(key[i].func)
  262. key[i].func(&key[i].arg);
  263. return;
  264. }
  265. }
  266. }
  267. static void
  268. leavenotify(XEvent *e)
  269. {
  270. XCrossingEvent *ev = &e->xcrossing;
  271. if((ev->window == root) && !ev->same_screen) {
  272. issel = False;
  273. drawall();
  274. }
  275. }
  276. static void
  277. mappingnotify(XEvent *e)
  278. {
  279. XMappingEvent *ev = &e->xmapping;
  280. XRefreshKeyboardMapping(ev);
  281. if(ev->request == MappingKeyboard)
  282. grabkeys();
  283. }
  284. static void
  285. maprequest(XEvent *e)
  286. {
  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. {
  302. Client *c;
  303. Window trans;
  304. XPropertyEvent *ev = &e->xproperty;
  305. if(ev->state == PropertyDelete)
  306. return; /* ignore */
  307. if((c = getclient(ev->window))) {
  308. if(ev->atom == wmatom[WMProtocols]) {
  309. c->proto = getproto(c->win);
  310. return;
  311. }
  312. switch (ev->atom) {
  313. default: break;
  314. case XA_WM_TRANSIENT_FOR:
  315. XGetTransientForHint(dpy, c->win, &trans);
  316. if(!c->isfloat && (c->isfloat = (trans != 0)))
  317. arrange(NULL);
  318. break;
  319. case XA_WM_NORMAL_HINTS:
  320. setsize(c);
  321. break;
  322. }
  323. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  324. settitle(c);
  325. drawtitle(c);
  326. }
  327. }
  328. }
  329. static void
  330. unmapnotify(XEvent *e)
  331. {
  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. {
  354. static unsigned int len = sizeof(key) / sizeof(key[0]);
  355. unsigned int i;
  356. KeyCode code;
  357. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  358. for(i = 0; i < len; i++) {
  359. code = XKeysymToKeycode(dpy, key[i].keysym);
  360. XGrabKey(dpy, code, key[i].mod, root, True,
  361. GrabModeAsync, GrabModeAsync);
  362. XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
  363. GrabModeAsync, GrabModeAsync);
  364. XGrabKey(dpy, code, key[i].mod | numlockmask, root, True,
  365. GrabModeAsync, GrabModeAsync);
  366. XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True,
  367. GrabModeAsync, GrabModeAsync);
  368. }
  369. }
  370. void
  371. procevent()
  372. {
  373. XEvent ev;
  374. while(XPending(dpy)) {
  375. XNextEvent(dpy, &ev);
  376. if(handler[ev.type])
  377. (handler[ev.type])(&ev); /* call handler */
  378. }
  379. }