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.

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