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.

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