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