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.

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