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.

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