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.

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