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.

388 lines
8.1 KiB

18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 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. int x1, y1, ocx, ocy, di;
  21. unsigned int dui;
  22. Window dummy;
  23. XEvent ev;
  24. ocx = c->x;
  25. ocy = c->y;
  26. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  27. None, cursor[CurMove], CurrentTime) != GrabSuccess)
  28. return;
  29. c->ismax = False;
  30. XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
  31. for(;;) {
  32. XMaskEvent(dpy, MOUSEMASK | ExposureMask, &ev);
  33. switch (ev.type) {
  34. case ButtonRelease:
  35. XUngrabPointer(dpy, CurrentTime);
  36. return;
  37. case Expose:
  38. handler[Expose](&ev);
  39. break;
  40. case MotionNotify:
  41. XSync(dpy, False);
  42. c->x = ocx + (ev.xmotion.x - x1);
  43. c->y = ocy + (ev.xmotion.y - y1);
  44. resize(c, False, TopLeft);
  45. break;
  46. }
  47. }
  48. }
  49. static void
  50. resizemouse(Client *c) {
  51. int ocx, ocy;
  52. int nw, nh;
  53. Corner sticky;
  54. XEvent ev;
  55. ocx = c->x;
  56. ocy = c->y;
  57. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  58. None, cursor[CurResize], CurrentTime) != GrabSuccess)
  59. return;
  60. c->ismax = False;
  61. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w, c->h);
  62. for(;;) {
  63. XMaskEvent(dpy, MOUSEMASK | ExposureMask, &ev);
  64. switch(ev.type) {
  65. case ButtonRelease:
  66. XUngrabPointer(dpy, CurrentTime);
  67. return;
  68. case Expose:
  69. handler[Expose](&ev);
  70. break;
  71. case MotionNotify:
  72. XSync(dpy, False);
  73. if((nw = abs(ocx - ev.xmotion.x)))
  74. c->w = nw;
  75. if((nh = abs(ocy - ev.xmotion.y)))
  76. c->h = nh;
  77. c->x = (ocx <= ev.xmotion.x) ? ocx : ocx - c->w;
  78. c->y = (ocy <= ev.xmotion.y) ? ocy : ocy - c->h;
  79. if(ocx <= ev.xmotion.x)
  80. sticky = (ocy <= ev.xmotion.y) ? TopLeft : BotLeft;
  81. else
  82. sticky = (ocy <= ev.xmotion.y) ? TopRight : BotRight;
  83. resize(c, True, sticky);
  84. break;
  85. }
  86. }
  87. }
  88. static void
  89. buttonpress(XEvent *e) {
  90. int x;
  91. Arg a;
  92. Client *c;
  93. XButtonPressedEvent *ev = &e->xbutton;
  94. if(barwin == ev->window) {
  95. x = 0;
  96. for(a.i = 0; a.i < ntags; a.i++) {
  97. x += textw(tags[a.i]);
  98. if(ev->x < x) {
  99. if(ev->button == Button1) {
  100. if(ev->state & MODKEY)
  101. tag(&a);
  102. else
  103. view(&a);
  104. }
  105. else if(ev->button == Button3) {
  106. if(ev->state & MODKEY)
  107. toggletag(&a);
  108. else
  109. toggleview(&a);
  110. }
  111. return;
  112. }
  113. }
  114. if(ev->x < x + bmw) {
  115. if(ev->button == Button1)
  116. togglemode(NULL);
  117. }
  118. }
  119. else if((c = getclient(ev->window))) {
  120. focus(c);
  121. if(CLEANMASK(ev->state) != MODKEY)
  122. return;
  123. if(ev->button == Button1 && (arrange == dofloat || c->isfloat)) {
  124. restack();
  125. movemouse(c);
  126. }
  127. else if(ev->button == Button2)
  128. zoom(NULL);
  129. else if(ev->button == Button3 && (arrange == dofloat || c->isfloat)) {
  130. restack();
  131. resizemouse(c);
  132. }
  133. }
  134. }
  135. static void
  136. configurerequest(XEvent *e) {
  137. unsigned long newmask;
  138. Client *c;
  139. XConfigureRequestEvent *ev = &e->xconfigurerequest;
  140. XEvent synev;
  141. XWindowChanges wc;
  142. if((c = getclient(ev->window))) {
  143. c->ismax = False;
  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. XSendEvent(dpy, c->win, True, NoEventMask, &synev);
  175. }
  176. XSync(dpy, False);
  177. if(c->isfloat)
  178. resize(c, False, TopLeft);
  179. else
  180. arrange(NULL);
  181. }
  182. else {
  183. wc.x = ev->x;
  184. wc.y = ev->y;
  185. wc.width = ev->width;
  186. wc.height = ev->height;
  187. wc.border_width = ev->border_width;
  188. wc.sibling = ev->above;
  189. wc.stack_mode = ev->detail;
  190. XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
  191. XSync(dpy, False);
  192. }
  193. }
  194. static void
  195. destroynotify(XEvent *e) {
  196. Client *c;
  197. XDestroyWindowEvent *ev = &e->xdestroywindow;
  198. if((c = getclient(ev->window)))
  199. unmanage(c);
  200. }
  201. static void
  202. enternotify(XEvent *e) {
  203. Client *c;
  204. XCrossingEvent *ev = &e->xcrossing;
  205. if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
  206. return;
  207. if(((c = getclient(ev->window)) || (c = getctitle(ev->window))) && isvisible(c))
  208. focus(c);
  209. else if(ev->window == root) {
  210. issel = True;
  211. XSetInputFocus(dpy, root, RevertToPointerRoot, CurrentTime);
  212. drawall();
  213. }
  214. }
  215. static void
  216. expose(XEvent *e) {
  217. Client *c;
  218. XExposeEvent *ev = &e->xexpose;
  219. if(ev->count == 0) {
  220. if(barwin == ev->window)
  221. drawstatus();
  222. else if((c = getctitle(ev->window)))
  223. drawtitle(c);
  224. }
  225. }
  226. static void
  227. keypress(XEvent *e) {
  228. static unsigned int len = sizeof(key) / sizeof(key[0]);
  229. unsigned int i;
  230. KeySym keysym;
  231. XKeyEvent *ev = &e->xkey;
  232. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  233. for(i = 0; i < len; i++) {
  234. if(keysym == key[i].keysym
  235. && CLEANMASK(key[i].mod) == CLEANMASK(ev->state))
  236. {
  237. if(key[i].func)
  238. key[i].func(&key[i].arg);
  239. return;
  240. }
  241. }
  242. }
  243. static void
  244. leavenotify(XEvent *e) {
  245. XCrossingEvent *ev = &e->xcrossing;
  246. if((ev->window == root) && !ev->same_screen) {
  247. issel = False;
  248. drawall();
  249. }
  250. }
  251. static void
  252. mappingnotify(XEvent *e) {
  253. XMappingEvent *ev = &e->xmapping;
  254. XRefreshKeyboardMapping(ev);
  255. if(ev->request == MappingKeyboard)
  256. grabkeys();
  257. }
  258. static void
  259. maprequest(XEvent *e) {
  260. static XWindowAttributes wa;
  261. XMapRequestEvent *ev = &e->xmaprequest;
  262. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  263. return;
  264. if(wa.override_redirect) {
  265. XSelectInput(dpy, ev->window,
  266. (StructureNotifyMask | PropertyChangeMask));
  267. return;
  268. }
  269. if(!getclient(ev->window))
  270. manage(ev->window, &wa);
  271. }
  272. static void
  273. propertynotify(XEvent *e) {
  274. Client *c;
  275. Window trans;
  276. XPropertyEvent *ev = &e->xproperty;
  277. if(ev->state == PropertyDelete)
  278. return; /* ignore */
  279. if((c = getclient(ev->window))) {
  280. if(ev->atom == wmatom[WMProtocols]) {
  281. c->proto = getproto(c->win);
  282. return;
  283. }
  284. switch (ev->atom) {
  285. default: break;
  286. case XA_WM_TRANSIENT_FOR:
  287. XGetTransientForHint(dpy, c->win, &trans);
  288. if(!c->isfloat && (c->isfloat = (trans != 0)))
  289. arrange(NULL);
  290. break;
  291. case XA_WM_NORMAL_HINTS:
  292. updatesize(c);
  293. break;
  294. }
  295. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  296. updatetitle(c);
  297. drawtitle(c);
  298. }
  299. }
  300. }
  301. static void
  302. unmapnotify(XEvent *e) {
  303. Client *c;
  304. XUnmapEvent *ev = &e->xunmap;
  305. if((c = getclient(ev->window)))
  306. unmanage(c);
  307. }
  308. /* extern */
  309. void (*handler[LASTEvent]) (XEvent *) = {
  310. [ButtonPress] = buttonpress,
  311. [ConfigureRequest] = configurerequest,
  312. [DestroyNotify] = destroynotify,
  313. [EnterNotify] = enternotify,
  314. [LeaveNotify] = leavenotify,
  315. [Expose] = expose,
  316. [KeyPress] = keypress,
  317. [MappingNotify] = mappingnotify,
  318. [MapRequest] = maprequest,
  319. [PropertyNotify] = propertynotify,
  320. [UnmapNotify] = unmapnotify
  321. };
  322. void
  323. grabkeys(void) {
  324. static unsigned int len = sizeof(key) / sizeof(key[0]);
  325. unsigned int i;
  326. KeyCode code;
  327. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  328. for(i = 0; i < len; i++) {
  329. code = XKeysymToKeycode(dpy, key[i].keysym);
  330. XGrabKey(dpy, code, key[i].mod, root, True,
  331. GrabModeAsync, GrabModeAsync);
  332. XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
  333. GrabModeAsync, GrabModeAsync);
  334. XGrabKey(dpy, code, key[i].mod | numlockmask, root, True,
  335. GrabModeAsync, GrabModeAsync);
  336. XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True,
  337. GrabModeAsync, GrabModeAsync);
  338. }
  339. }
  340. void
  341. procevent(void) {
  342. XEvent ev;
  343. while(XPending(dpy)) {
  344. XNextEvent(dpy, &ev);
  345. if(handler[ev.type])
  346. (handler[ev.type])(&ev); /* call handler */
  347. }
  348. }