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.

380 lines
8.5 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. return;
  127. }
  128. }
  129. if((ev->x < x + blw) && ev->button == Button1)
  130. setlayout(NULL);
  131. }
  132. else if((c = getclient(ev->window))) {
  133. focus(c);
  134. if(CLEANMASK(ev->state) != MODKEY)
  135. return;
  136. if(ev->button == Button1 && (lt->arrange == floating || c->isfloating)) {
  137. restack();
  138. movemouse(c);
  139. }
  140. else if(ev->button == Button2)
  141. zoom(NULL);
  142. else if(ev->button == Button3
  143. && (lt->arrange == floating || c->isfloating) && !c->isfixed)
  144. {
  145. restack();
  146. resizemouse(c);
  147. }
  148. }
  149. }
  150. static void
  151. configurerequest(XEvent *e) {
  152. Client *c;
  153. XConfigureRequestEvent *ev = &e->xconfigurerequest;
  154. XWindowChanges wc;
  155. if((c = getclient(ev->window))) {
  156. c->ismax = False;
  157. if(ev->value_mask & CWBorderWidth)
  158. c->border = ev->border_width;
  159. if(c->isfixed || c->isfloating || (lt->arrange == floating)) {
  160. if(ev->value_mask & CWX)
  161. c->x = ev->x;
  162. if(ev->value_mask & CWY)
  163. c->y = ev->y;
  164. if(ev->value_mask & CWWidth)
  165. c->w = ev->width;
  166. if(ev->value_mask & CWHeight)
  167. c->h = ev->height;
  168. if((c->x + c->w) > sw && c->isfloating)
  169. c->x = sw / 2 - c->w / 2; /* center in x direction */
  170. if((c->y + c->h) > sh && c->isfloating)
  171. c->y = sh / 2 - c->h / 2; /* center in y direction */
  172. if((ev->value_mask & (CWX | CWY))
  173. && !(ev->value_mask & (CWWidth | CWHeight)))
  174. configure(c);
  175. if(isvisible(c))
  176. XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
  177. }
  178. else
  179. configure(c);
  180. }
  181. else {
  182. wc.x = ev->x;
  183. wc.y = ev->y;
  184. wc.width = ev->width;
  185. wc.height = ev->height;
  186. wc.border_width = ev->border_width;
  187. wc.sibling = ev->above;
  188. wc.stack_mode = ev->detail;
  189. XConfigureWindow(dpy, ev->window, ev->value_mask, &wc);
  190. }
  191. XSync(dpy, False);
  192. }
  193. static void
  194. configurenotify(XEvent *e) {
  195. XConfigureEvent *ev = &e->xconfigure;
  196. if (ev->window == root && (ev->width != sw || ev->height != sh)) {
  197. sw = ev->width;
  198. sh = ev->height;
  199. XFreePixmap(dpy, dc.drawable);
  200. dc.drawable = XCreatePixmap(dpy, root, sw, bh, DefaultDepth(dpy, screen));
  201. XResizeWindow(dpy, barwin, sw, bh);
  202. updatebarpos();
  203. lt->arrange(NULL);
  204. }
  205. }
  206. static void
  207. destroynotify(XEvent *e) {
  208. Client *c;
  209. XDestroyWindowEvent *ev = &e->xdestroywindow;
  210. if((c = getclient(ev->window)))
  211. unmanage(c);
  212. }
  213. static void
  214. enternotify(XEvent *e) {
  215. Client *c;
  216. XCrossingEvent *ev = &e->xcrossing;
  217. if(ev->mode != NotifyNormal || ev->detail == NotifyInferior)
  218. return;
  219. if((c = getclient(ev->window)))
  220. focus(c);
  221. else if(ev->window == root) {
  222. selscreen = True;
  223. focus(NULL);
  224. }
  225. }
  226. static void
  227. expose(XEvent *e) {
  228. XExposeEvent *ev = &e->xexpose;
  229. if(ev->count == 0) {
  230. if(barwin == ev->window)
  231. drawstatus();
  232. }
  233. }
  234. static void
  235. keypress(XEvent *e) {
  236. static unsigned int len = sizeof key / sizeof key[0];
  237. unsigned int i;
  238. KeySym keysym;
  239. XKeyEvent *ev = &e->xkey;
  240. keysym = XKeycodeToKeysym(dpy, (KeyCode)ev->keycode, 0);
  241. for(i = 0; i < len; i++)
  242. if(keysym == key[i].keysym
  243. && CLEANMASK(key[i].mod) == CLEANMASK(ev->state))
  244. {
  245. if(key[i].func)
  246. key[i].func(key[i].arg);
  247. }
  248. }
  249. static void
  250. leavenotify(XEvent *e) {
  251. XCrossingEvent *ev = &e->xcrossing;
  252. if((ev->window == root) && !ev->same_screen) {
  253. selscreen = False;
  254. focus(NULL);
  255. }
  256. }
  257. static void
  258. mappingnotify(XEvent *e) {
  259. XMappingEvent *ev = &e->xmapping;
  260. XRefreshKeyboardMapping(ev);
  261. if(ev->request == MappingKeyboard)
  262. grabkeys();
  263. }
  264. static void
  265. maprequest(XEvent *e) {
  266. static XWindowAttributes wa;
  267. XMapRequestEvent *ev = &e->xmaprequest;
  268. if(!XGetWindowAttributes(dpy, ev->window, &wa))
  269. return;
  270. if(wa.override_redirect)
  271. return;
  272. if(!getclient(ev->window))
  273. manage(ev->window, &wa);
  274. }
  275. static void
  276. propertynotify(XEvent *e) {
  277. Client *c;
  278. Window trans;
  279. XPropertyEvent *ev = &e->xproperty;
  280. if(ev->state == PropertyDelete)
  281. return; /* ignore */
  282. if((c = getclient(ev->window))) {
  283. switch (ev->atom) {
  284. default: break;
  285. case XA_WM_TRANSIENT_FOR:
  286. XGetTransientForHint(dpy, c->win, &trans);
  287. if(!c->isfloating && (c->isfloating = (getclient(trans) != NULL)))
  288. lt->arrange(NULL);
  289. break;
  290. case XA_WM_NORMAL_HINTS:
  291. updatesizehints(c);
  292. break;
  293. }
  294. if(ev->atom == XA_WM_NAME || ev->atom == netatom[NetWMName]) {
  295. updatetitle(c);
  296. if(c == sel)
  297. drawstatus();
  298. }
  299. }
  300. }
  301. static void
  302. unmapnotify(XEvent *e) {
  303. Client *c;
  304. XUnmapEvent *ev = &e->xunmap;
  305. if((c = getclient(ev->window)) && (ev->event == root)) {
  306. if(ev->send_event || c->unmapped-- == 0)
  307. unmanage(c);
  308. }
  309. }
  310. /* extern */
  311. void (*handler[LASTEvent]) (XEvent *) = {
  312. [ButtonPress] = buttonpress,
  313. [ConfigureRequest] = configurerequest,
  314. [ConfigureNotify] = configurenotify,
  315. [DestroyNotify] = destroynotify,
  316. [EnterNotify] = enternotify,
  317. [LeaveNotify] = leavenotify,
  318. [Expose] = expose,
  319. [KeyPress] = keypress,
  320. [MappingNotify] = mappingnotify,
  321. [MapRequest] = maprequest,
  322. [PropertyNotify] = propertynotify,
  323. [UnmapNotify] = unmapnotify
  324. };
  325. void
  326. grabkeys(void) {
  327. static unsigned int len = sizeof key / sizeof key[0];
  328. unsigned int i;
  329. KeyCode code;
  330. XUngrabKey(dpy, AnyKey, AnyModifier, root);
  331. for(i = 0; i < len; i++) {
  332. code = XKeysymToKeycode(dpy, key[i].keysym);
  333. XGrabKey(dpy, code, key[i].mod, root, True,
  334. GrabModeAsync, GrabModeAsync);
  335. XGrabKey(dpy, code, key[i].mod | LockMask, root, True,
  336. GrabModeAsync, GrabModeAsync);
  337. XGrabKey(dpy, code, key[i].mod | numlockmask, root, True,
  338. GrabModeAsync, GrabModeAsync);
  339. XGrabKey(dpy, code, key[i].mod | numlockmask | LockMask, root, True,
  340. GrabModeAsync, GrabModeAsync);
  341. }
  342. }