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.

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