Configuration of dwm for Mac Computers
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.

374 lines
7.9 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. configure(c);
  176. resize(c, c->x, c->y, c->w, c->h, False);
  177. if(!isvisible(c))
  178. ban(c);
  179. }
  180. else
  181. configure(c);
  182. }
  183. else {
  184. wc.x = ev->x;
  185. wc.y = ev->y;
  186. wc.width = ev->width;
  187. wc.height = ev->height;
  188. wc.border_width = ev->border_width;
  189. wc.sibling = ev->above;
  190. wc.stack_mode = ev->detail;
  191. XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
  192. }
  193. XSync(dpy, False);
  194. }
  195. static void
  196. destroynotify(XEvent *e) {
  197. Client *c;
  198. XDestroyWindowEvent *ev = &e->xdestroywindow;
  199. if((c = getclient(ev->window)))
  200. unmanage(c);
  201. }
  202. static void
  203. enternotify(XEvent *e) {
  204. Client *c;
  205. XCrossingEvent *ev = &e->xcrossing;
  206. if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
  207. return;
  208. if((c = getclient(ev->window)) && isvisible(c))
  209. focus(c);
  210. else if(ev->window == root) {
  211. selscreen = True;
  212. for(c = stack; c && !isvisible(c); c = c->snext);
  213. focus(c);
  214. }
  215. }
  216. static void
  217. expose(XEvent *e) {
  218. XExposeEvent *ev = &e->xexpose;
  219. if(ev->count == 0) {
  220. if(barwin == ev->window)
  221. drawstatus();
  222. }
  223. }
  224. static void
  225. keypress(XEvent *e) {
  226. static unsigned int len = sizeof key / sizeof key[0];
  227. unsigned int i;
  228. KeySym keysym;
  229. XKeyEvent *ev = &e->xkey;
  230. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  231. for(i = 0; i < len; i++)
  232. if(keysym == key[i].keysym
  233. && CLEANMASK(key[i].mod) == CLEANMASK(ev->state))
  234. {
  235. if(key[i].func)
  236. key[i].func(&key[i].arg);
  237. }
  238. }
  239. static void
  240. leavenotify(XEvent *e) {
  241. XCrossingEvent *ev = &e->xcrossing;
  242. if((ev->window == root) && !ev->same_screen) {
  243. selscreen = False;
  244. focus(NULL);
  245. }
  246. }
  247. static void
  248. mappingnotify(XEvent *e) {
  249. XMappingEvent *ev = &e->xmapping;
  250. XRefreshKeyboardMapping(ev);
  251. if(ev->request == MappingKeyboard)
  252. grabkeys();
  253. }
  254. static void
  255. maprequest(XEvent *e) {
  256. static XWindowAttributes wa;
  257. XMapRequestEvent *ev = &e->xmaprequest;
  258. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  259. return;
  260. if(wa.override_redirect)
  261. return;
  262. if(!getclient(ev->window))
  263. manage(ev->window, &wa);
  264. }
  265. static void
  266. propertynotify(XEvent *e) {
  267. Client *c;
  268. Window trans;
  269. XPropertyEvent *ev = &e->xproperty;
  270. if(ev->state == PropertyDelete)
  271. return; /* ignore */
  272. if((c = getclient(ev->window))) {
  273. switch (ev->atom) {
  274. default: break;
  275. case XA_WM_TRANSIENT_FOR:
  276. XGetTransientForHint(dpy, c->win, &trans);
  277. if(!c->isfloat && (c->isfloat = (trans != 0)))
  278. arrange();
  279. break;
  280. case XA_WM_NORMAL_HINTS:
  281. updatesizehints(c);
  282. break;
  283. }
  284. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  285. updatetitle(c);
  286. if(c == sel)
  287. drawstatus();
  288. }
  289. }
  290. }
  291. static void
  292. unmapnotify(XEvent *e) {
  293. Client *c;
  294. XUnmapEvent *ev = &e->xunmap;
  295. if((c = getclient(ev->window)))
  296. unmanage(c);
  297. }
  298. /* extern */
  299. void (*handler[LASTEvent]) (XEvent *) = {
  300. [ButtonPress] = buttonpress,
  301. [ConfigureRequest] = configurerequest,
  302. [DestroyNotify] = destroynotify,
  303. [EnterNotify] = enternotify,
  304. [LeaveNotify] = leavenotify,
  305. [Expose] = expose,
  306. [KeyPress] = keypress,
  307. [MappingNotify] = mappingnotify,
  308. [MapRequest] = maprequest,
  309. [PropertyNotify] = propertynotify,
  310. [UnmapNotify] = unmapnotify
  311. };
  312. void
  313. grabkeys(void) {
  314. static unsigned int len = sizeof key / sizeof key[0];
  315. unsigned int i;
  316. KeyCode code;
  317. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  318. for(i = 0; i < len; i++) {
  319. code = XKeysymToKeycode(dpy, key[i].keysym);
  320. XGrabKey(dpy, code, key[i].mod, root, True,
  321. GrabModeAsync, GrabModeAsync);
  322. XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
  323. GrabModeAsync, GrabModeAsync);
  324. XGrabKey(dpy, code, key[i].mod | numlockmask, root, True,
  325. GrabModeAsync, GrabModeAsync);
  326. XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True,
  327. GrabModeAsync, GrabModeAsync);
  328. }
  329. }
  330. void
  331. procevent(void) {
  332. XEvent ev;
  333. while(XPending(dpy)) {
  334. XNextEvent(dpy, &ev);
  335. if(handler[ev.type])
  336. (handler[ev.type])(&ev); /* call handler */
  337. }
  338. }