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.

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