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.

379 lines
7.7 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
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. Corner sticky;
  55. XEvent ev;
  56. ocx = c->x;
  57. ocy = c->y;
  58. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  59. None, cursor[CurResize], CurrentTime) != GrabSuccess)
  60. return;
  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. default: break;
  66. case Expose:
  67. handler[Expose](&ev);
  68. break;
  69. case MotionNotify:
  70. XSync(dpy, False);
  71. c->w = abs(ocx - ev.xmotion.x);
  72. c->h = abs(ocy - ev.xmotion.y);
  73. c->x = (ocx <= ev.xmotion.x) ? ocx : ocx - c->w;
  74. c->y = (ocy <= ev.xmotion.y) ? ocy : ocy - c->h;
  75. if(ocx <= ev.xmotion.x)
  76. sticky = (ocy <= ev.xmotion.y) ? TopLeft : BotLeft;
  77. else
  78. sticky = (ocy <= ev.xmotion.y) ? TopRight : BotRight;
  79. resize(c, True, sticky);
  80. break;
  81. case ButtonRelease:
  82. XUngrabPointer(dpy, CurrentTime);
  83. return;
  84. }
  85. }
  86. }
  87. static void
  88. buttonpress(XEvent *e)
  89. {
  90. int x;
  91. Arg a;
  92. Client *c;
  93. XButtonPressedEvent *ev = &e->xbutton;
  94. if(barwin == ev->window) {
  95. switch(ev->button) {
  96. default:
  97. x = 0;
  98. for(a.i = 0; a.i < ntags; a.i++) {
  99. x += textw(tags[a.i]);
  100. if(ev->x < x) {
  101. view(&a);
  102. break;
  103. }
  104. }
  105. break;
  106. case Button4:
  107. viewnext(&a);
  108. break;
  109. case Button5:
  110. viewprev(&a);
  111. break;
  112. }
  113. }
  114. else if((c = getclient(ev->window))) {
  115. focus(c);
  116. switch(ev->button) {
  117. default:
  118. break;
  119. case Button1:
  120. if(!c->ismax && (arrange == dofloat || c->isfloat)) {
  121. higher(c);
  122. movemouse(c);
  123. }
  124. break;
  125. case Button2:
  126. lower(c);
  127. break;
  128. case Button3:
  129. if(!c->ismax && (arrange == dofloat || c->isfloat)) {
  130. higher(c);
  131. resizemouse(c);
  132. }
  133. break;
  134. }
  135. }
  136. }
  137. static void
  138. configurerequest(XEvent *e)
  139. {
  140. Client *c;
  141. XConfigureRequestEvent *ev = &e->xconfigurerequest;
  142. XEvent synev;
  143. XWindowChanges wc;
  144. unsigned long newmask;
  145. if((c = getclient(ev->window))) {
  146. gravitate(c, True);
  147. if(c->isfloat) {
  148. if(ev->value_mask & CWX)
  149. c->x = ev->x;
  150. if(ev->value_mask & CWY)
  151. c->y = ev->y;
  152. if(ev->value_mask & CWWidth)
  153. c->w = ev->width;
  154. if(ev->value_mask & CWHeight)
  155. c->h = ev->height;
  156. }
  157. if(ev->value_mask & CWBorderWidth)
  158. c->border = ev->border_width;
  159. gravitate(c, False);
  160. resize(c, True, TopLeft);
  161. wc.x = c->x;
  162. wc.y = c->y;
  163. wc.width = c->w;
  164. wc.height = c->h;
  165. newmask = ev->value_mask & (~(CWSibling | CWStackMode | CWBorderWidth));
  166. if(newmask)
  167. XConfigureWindow(dpy, c->win, newmask, &wc);
  168. else {
  169. synev.type = ConfigureNotify;
  170. synev.xconfigure.display = dpy;
  171. synev.xconfigure.event = c->win;
  172. synev.xconfigure.window = c->win;
  173. synev.xconfigure.x = c->x;
  174. synev.xconfigure.y = c->y;
  175. synev.xconfigure.width = c->w;
  176. synev.xconfigure.height = c->h;
  177. synev.xconfigure.border_width = c->border;
  178. synev.xconfigure.above = None;
  179. /* Send synthetic ConfigureNotify */
  180. XSendEvent(dpy, c->win, True, NoEventMask, &synev);
  181. }
  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. }
  193. XSync(dpy, False);
  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->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. }
  215. static void
  216. expose(XEvent *e)
  217. {
  218. Client *c;
  219. XExposeEvent *ev = &e->xexpose;
  220. if(ev->count == 0) {
  221. if(barwin == ev->window)
  222. drawstatus();
  223. else if((c = getctitle(ev->window)))
  224. drawtitle(c);
  225. }
  226. }
  227. static void
  228. keypress(XEvent *e)
  229. {
  230. static unsigned int len = sizeof(key) / sizeof(key[0]);
  231. unsigned int i;
  232. KeySym keysym;
  233. XKeyEvent *ev = &e->xkey;
  234. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  235. for(i = 0; i < len; i++)
  236. if(keysym == key[i].keysym &&
  237. CLEANMASK(key[i].mod) == CLEANMASK(ev->state)) {
  238. if(key[i].func)
  239. key[i].func(&key[i].arg);
  240. return;
  241. }
  242. }
  243. static void
  244. leavenotify(XEvent *e)
  245. {
  246. XCrossingEvent *ev = &e->xcrossing;
  247. if((ev->window == root) && !ev->same_screen)
  248. issel = True;
  249. }
  250. static void
  251. maprequest(XEvent *e)
  252. {
  253. static XWindowAttributes wa;
  254. XMapRequestEvent *ev = &e->xmaprequest;
  255. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  256. return;
  257. if(wa.override_redirect) {
  258. XSelectInput(dpy, ev->window,
  259. (StructureNotifyMask | PropertyChangeMask));
  260. return;
  261. }
  262. if(!getclient(ev->window))
  263. manage(ev->window, &wa);
  264. }
  265. static void
  266. propertynotify(XEvent *e)
  267. {
  268. Client *c;
  269. Window trans;
  270. XPropertyEvent *ev = &e->xproperty;
  271. if(ev->state == PropertyDelete)
  272. return; /* ignore */
  273. if((c = getclient(ev->window))) {
  274. if(ev->atom == wmatom[WMProtocols]) {
  275. c->proto = getproto(c->win);
  276. return;
  277. }
  278. switch (ev->atom) {
  279. default: break;
  280. case XA_WM_TRANSIENT_FOR:
  281. XGetTransientForHint(dpy, c->win, &trans);
  282. if(!c->isfloat && (c->isfloat = (trans != 0)))
  283. arrange(NULL);
  284. break;
  285. case XA_WM_NORMAL_HINTS:
  286. setsize(c);
  287. break;
  288. }
  289. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  290. settitle(c);
  291. drawtitle(c);
  292. }
  293. }
  294. }
  295. static void
  296. unmapnotify(XEvent *e)
  297. {
  298. Client *c;
  299. XUnmapEvent *ev = &e->xunmap;
  300. if((c = getclient(ev->window)))
  301. unmanage(c);
  302. }
  303. /* extern */
  304. void (*handler[LASTEvent]) (XEvent *) = {
  305. [ButtonPress] = buttonpress,
  306. [ConfigureRequest] = configurerequest,
  307. [DestroyNotify] = destroynotify,
  308. [EnterNotify] = enternotify,
  309. [LeaveNotify] = leavenotify,
  310. [Expose] = expose,
  311. [KeyPress] = keypress,
  312. [MapRequest] = maprequest,
  313. [PropertyNotify] = propertynotify,
  314. [UnmapNotify] = unmapnotify
  315. };
  316. void
  317. grabkeys()
  318. {
  319. static unsigned int len = sizeof(key) / sizeof(key[0]);
  320. unsigned int i;
  321. KeyCode code;
  322. for(i = 0; i < len; i++) {
  323. code = XKeysymToKeycode(dpy, key[i].keysym);
  324. /* normal */
  325. XUngrabKey(dpy, code, key[i].mod, root);
  326. XGrabKey(dpy, code, key[i].mod, root, True,
  327. GrabModeAsync, GrabModeAsync);
  328. /* capslock */
  329. XUngrabKey(dpy, code, key[i].mod | LockMask, root);
  330. XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
  331. GrabModeAsync, GrabModeAsync);
  332. /* numlock */
  333. XUngrabKey(dpy, code, key[i].mod | NUMLOCKMASK, root);
  334. XGrabKey(dpy, code, key[i].mod | NUMLOCKMASK, root, True,
  335. GrabModeAsync, GrabModeAsync);
  336. /* capslock & numlock */
  337. XUngrabKey(dpy, code, key[i].mod | NUMLOCKMASK | LockMask, root);
  338. XGrabKey(dpy, code, key[i].mod | NUMLOCKMASK | LockMask, root, True,
  339. GrabModeAsync, GrabModeAsync);
  340. }
  341. }