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.

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