Configuration file for DWM on MacBook Air
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.

386 lines
8.6 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
  1. /* © 2006-2007 Anselm R. Garbe <garbeam at gmail dot com>
  2. * © 2006-2007 Sander van Dijk <a dot h dot vandijk at gmail dot com>
  3. * See LICENSE file for license details. */
  4. #include "dwm.h"
  5. #include <stdio.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)(const char *arg);
  14. const char *arg;
  15. } Key;
  16. KEYS
  17. #define CLEANMASK(mask) (mask & ~(numlockmask | LockMask))
  18. #define MOUSEMASK (BUTTONMASK | PointerMotionMask)
  19. static Client *
  20. getclient(Window w) {
  21. Client *c;
  22. for(c = clients; c && c->win != w; c = c->next);
  23. return c;
  24. }
  25. static void
  26. movemouse(Client *c) {
  27. int x1, y1, ocx, ocy, di, nx, ny;
  28. unsigned int dui;
  29. Window dummy;
  30. XEvent ev;
  31. ocx = nx = c->x;
  32. ocy = ny = c->y;
  33. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  34. None, cursor[CurMove], CurrentTime) != GrabSuccess)
  35. return;
  36. c->ismax = False;
  37. XQueryPointer(dpy, root, &dummy, &dummy, &x1, &y1, &di, &di, &dui);
  38. for(;;) {
  39. XMaskEvent(dpy, MOUSEMASK | ExposureMask | SubstructureRedirectMask, &ev);
  40. switch (ev.type) {
  41. case ButtonRelease:
  42. XUngrabPointer(dpy, CurrentTime);
  43. return;
  44. case ConfigureRequest:
  45. case Expose:
  46. case MapRequest:
  47. handler[ev.type](&ev);
  48. break;
  49. case MotionNotify:
  50. XSync(dpy, False);
  51. nx = ocx + (ev.xmotion.x - x1);
  52. ny = ocy + (ev.xmotion.y - y1);
  53. if(abs(wax + nx) < SNAP)
  54. nx = wax;
  55. else if(abs((wax + waw) - (nx + c->w + 2 * c->border)) < SNAP)
  56. nx = wax + waw - c->w - 2 * c->border;
  57. if(abs(way - ny) < SNAP)
  58. ny = way;
  59. else if(abs((way + wah) - (ny + c->h + 2 * c->border)) < SNAP)
  60. ny = way + wah - c->h - 2 * c->border;
  61. resize(c, nx, ny, c->w, c->h, False);
  62. break;
  63. }
  64. }
  65. }
  66. static void
  67. resizemouse(Client *c) {
  68. int ocx, ocy;
  69. int nw, nh;
  70. XEvent ev;
  71. ocx = c->x;
  72. ocy = c->y;
  73. if(XGrabPointer(dpy, root, False, MOUSEMASK, GrabModeAsync, GrabModeAsync,
  74. None, cursor[CurResize], CurrentTime) != GrabSuccess)
  75. return;
  76. c->ismax = False;
  77. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0, c->w + c->border - 1, c->h + c->border - 1);
  78. for(;;) {
  79. XMaskEvent(dpy, MOUSEMASK | ExposureMask | SubstructureRedirectMask , &ev);
  80. switch(ev.type) {
  81. case ButtonRelease:
  82. XWarpPointer(dpy, None, c->win, 0, 0, 0, 0,
  83. c->w + c->border - 1, c->h + c->border - 1);
  84. XUngrabPointer(dpy, CurrentTime);
  85. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  86. return;
  87. case ConfigureRequest:
  88. case Expose:
  89. case MapRequest:
  90. handler[ev.type](&ev);
  91. break;
  92. case MotionNotify:
  93. XSync(dpy, False);
  94. if((nw = ev.xmotion.x - ocx - 2 * c->border + 1) <= 0)
  95. nw = 1;
  96. if((nh = ev.xmotion.y - ocy - 2 * c->border + 1) <= 0)
  97. nh = 1;
  98. resize(c, c->x, c->y, nw, nh, True);
  99. break;
  100. }
  101. }
  102. }
  103. static void
  104. buttonpress(XEvent *e) {
  105. static char buf[32];
  106. unsigned int i, x;
  107. Client *c;
  108. XButtonPressedEvent *ev = &e->xbutton;
  109. buf[0] = 0;
  110. if(barwin == ev->window) {
  111. x = 0;
  112. for(i = 0; i < ntags; i++) {
  113. x += textw(tags[i]);
  114. if(ev->x < x) {
  115. snprintf(buf, sizeof buf, "%d", i);
  116. if(ev->button == Button1) {
  117. if(ev->state & MODKEY)
  118. tag(buf);
  119. else
  120. view(buf);
  121. }
  122. else if(ev->button == Button3) {
  123. if(ev->state & MODKEY)
  124. toggletag(buf);
  125. else
  126. toggleview(buf);
  127. }
  128. return;
  129. }
  130. }
  131. if(ev->x < x + blw)
  132. switch(ev->button) {
  133. case Button1:
  134. setlayout(NULL);
  135. break;
  136. }
  137. }
  138. else if((c = getclient(ev->window))) {
  139. focus(c);
  140. if(CLEANMASK(ev->state) != MODKEY)
  141. return;
  142. if(ev->button == Button1 && (lt->arrange == floating || c->isfloating)) {
  143. restack();
  144. movemouse(c);
  145. }
  146. else if(ev->button == Button2)
  147. zoom(NULL);
  148. else if(ev->button == Button3
  149. && (lt->arrange == floating || c->isfloating) && !c->isfixed)
  150. {
  151. restack();
  152. resizemouse(c);
  153. }
  154. }
  155. }
  156. static void
  157. configurerequest(XEvent *e) {
  158. Client *c;
  159. XConfigureRequestEvent *ev = &e->xconfigurerequest;
  160. XWindowChanges wc;
  161. if((c = getclient(ev->window))) {
  162. c->ismax = False;
  163. if(ev->value_mask & CWBorderWidth)
  164. c->border = ev->border_width;
  165. if(c->isfixed || c->isfloating || (lt->arrange == floating)) {
  166. if(ev->value_mask & CWX)
  167. c->x = ev->x;
  168. if(ev->value_mask & CWY)
  169. c->y = ev->y;
  170. if(ev->value_mask & CWWidth)
  171. c->w = ev->width;
  172. if(ev->value_mask & CWHeight)
  173. c->h = ev->height;
  174. if((c->x + c->w) > sw && c->isfloating)
  175. c->x = sw / 2 - c->w / 2; /* center in x direction */
  176. if((c->y + c->h) > sh && c->isfloating)
  177. c->y = sh / 2 - c->h / 2; /* center in y direction */
  178. if((ev->value_mask & (CWX | CWY))
  179. && !(ev->value_mask & (CWWidth | CWHeight)))
  180. configure(c);
  181. if(isvisible(c))
  182. XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
  183. }
  184. else
  185. configure(c);
  186. }
  187. else {
  188. wc.x = ev->x;
  189. wc.y = ev->y;
  190. wc.width = ev->width;
  191. wc.height = ev->height;
  192. wc.border_width = ev->border_width;
  193. wc.sibling = ev->above;
  194. wc.stack_mode = ev->detail;
  195. XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
  196. }
  197. XSync(dpy, False);
  198. }
  199. static void
  200. configurenotify(XEvent *e) {
  201. XConfigureEvent *ev = &e->xconfigure;
  202. if (ev->window == root && (ev->width != sw || ev->height != sh)) {
  203. sw = ev->width;
  204. sh = ev->height;
  205. wah = sh - bh;
  206. waw = sw;
  207. XFreePixmap(dpy, dc.drawable);
  208. dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
  209. XResizeWindow(dpy, barwin, sw, bh);
  210. lt->arrange();
  211. }
  212. }
  213. static void
  214. destroynotify(XEvent *e) {
  215. Client *c;
  216. XDestroyWindowEvent *ev = &e->xdestroywindow;
  217. if((c = getclient(ev->window)))
  218. unmanage(c);
  219. }
  220. static void
  221. enternotify(XEvent *e) {
  222. Client *c;
  223. XCrossingEvent *ev = &e->xcrossing;
  224. if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
  225. return;
  226. if((c = getclient(ev->window)) && isvisible(c))
  227. focus(c);
  228. else if(ev->window == root) {
  229. selscreen = True;
  230. for(c = stack; c && !isvisible(c); c = c->snext);
  231. focus(c);
  232. }
  233. }
  234. static void
  235. expose(XEvent *e) {
  236. XExposeEvent *ev = &e->xexpose;
  237. if(ev->count == 0) {
  238. if(barwin == ev->window)
  239. drawstatus();
  240. }
  241. }
  242. static void
  243. keypress(XEvent *e) {
  244. static unsigned int len = sizeof key / sizeof key[0];
  245. unsigned int i;
  246. KeySym keysym;
  247. XKeyEvent *ev = &e->xkey;
  248. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  249. for(i = 0; i < len; i++)
  250. if(keysym == key[i].keysym
  251. && CLEANMASK(key[i].mod) == CLEANMASK(ev->state))
  252. {
  253. if(key[i].func)
  254. key[i].func(key[i].arg);
  255. }
  256. }
  257. static void
  258. leavenotify(XEvent *e) {
  259. XCrossingEvent *ev = &e->xcrossing;
  260. if((ev->window == root) && !ev->same_screen) {
  261. selscreen = False;
  262. focus(NULL);
  263. }
  264. }
  265. static void
  266. mappingnotify(XEvent *e) {
  267. XMappingEvent *ev = &e->xmapping;
  268. XRefreshKeyboardMapping(ev);
  269. if(ev->request == MappingKeyboard)
  270. grabkeys();
  271. }
  272. static void
  273. maprequest(XEvent *e) {
  274. static XWindowAttributes wa;
  275. XMapRequestEvent *ev = &e->xmaprequest;
  276. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  277. return;
  278. if(wa.override_redirect)
  279. return;
  280. if(!getclient(ev->window))
  281. manage(ev->window, &wa);
  282. }
  283. static void
  284. propertynotify(XEvent *e) {
  285. Client *c;
  286. Window trans;
  287. XPropertyEvent *ev = &e->xproperty;
  288. if(ev->state == PropertyDelete)
  289. return; /* ignore */
  290. if((c = getclient(ev->window))) {
  291. switch (ev->atom) {
  292. default: break;
  293. case XA_WM_TRANSIENT_FOR:
  294. XGetTransientForHint(dpy, c->win, &trans);
  295. if(!c->isfloating && (c->isfloating = (getclient(trans) != NULL)))
  296. lt->arrange();
  297. break;
  298. case XA_WM_NORMAL_HINTS:
  299. updatesizehints(c);
  300. break;
  301. }
  302. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  303. updatetitle(c);
  304. if(c == sel)
  305. drawstatus();
  306. }
  307. }
  308. }
  309. static void
  310. unmapnotify(XEvent *e) {
  311. Client *c;
  312. XUnmapEvent *ev = &e->xunmap;
  313. if((c = getclient(ev->window)))
  314. unmanage(c);
  315. }
  316. /* extern */
  317. void (*handler[LASTEvent]) (XEvent *) = {
  318. [ButtonPress] = buttonpress,
  319. [ConfigureRequest] = configurerequest,
  320. [ConfigureNotify] = configurenotify,
  321. [DestroyNotify] = destroynotify,
  322. [EnterNotify] = enternotify,
  323. [LeaveNotify] = leavenotify,
  324. [Expose] = expose,
  325. [KeyPress] = keypress,
  326. [MappingNotify] = mappingnotify,
  327. [MapRequest] = maprequest,
  328. [PropertyNotify] = propertynotify,
  329. [UnmapNotify] = unmapnotify
  330. };
  331. void
  332. grabkeys(void) {
  333. static unsigned int len = sizeof key / sizeof key[0];
  334. unsigned int i;
  335. KeyCode code;
  336. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  337. for(i = 0; i < len; i++) {
  338. code = XKeysymToKeycode(dpy, key[i].keysym);
  339. XGrabKey(dpy, code, key[i].mod, root, True,
  340. GrabModeAsync, GrabModeAsync);
  341. XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
  342. GrabModeAsync, GrabModeAsync);
  343. XGrabKey(dpy, code, key[i].mod | numlockmask, root, True,
  344. GrabModeAsync, GrabModeAsync);
  345. XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True,
  346. GrabModeAsync, GrabModeAsync);
  347. }
  348. }