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.

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