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.

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