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.

443 lines
8.0 KiB

18 years ago
18 years ago
18 years ago
18 years ago
  1. /*
  2. * (C)opyright MMVI Anselm R. Garbe <garbeam at gmail dot com>
  3. * See LICENSE file for license details.
  4. */
  5. #include <math.h>
  6. #include <stdlib.h>
  7. #include <string.h>
  8. #include <X11/Xatom.h>
  9. #include "util.h"
  10. #include "wm.h"
  11. void (*arrange)(void *aux);
  12. void
  13. max(void *aux)
  14. {
  15. if(!stack)
  16. return;
  17. stack->x = sx;
  18. stack->y = bh;
  19. stack->w = sw - 2 * stack->border;
  20. stack->h = sh - bh - 2 * stack->border;
  21. resize(stack);
  22. discard_events(EnterWindowMask);
  23. }
  24. void
  25. floating(void *aux)
  26. {
  27. Client *c;
  28. arrange = floating;
  29. for(c = stack; c; c = c->snext)
  30. resize(c);
  31. discard_events(EnterWindowMask);
  32. }
  33. void
  34. grid(void *aux)
  35. {
  36. Client *c;
  37. int n, cols, rows, gw, gh, i, j;
  38. float rt, fd;
  39. arrange = grid;
  40. if(!clients)
  41. return;
  42. for(n = 0, c = clients; c; c = c->next, n++);
  43. rt = sqrt(n);
  44. if(modff(rt, &fd) < 0.5)
  45. rows = floor(rt);
  46. else
  47. rows = ceil(rt);
  48. if(rows * rows < n)
  49. cols = rows + 1;
  50. else
  51. cols = rows;
  52. gw = (sw - 2) / cols;
  53. gh = (sh - bh - 2) / rows;
  54. for(i = j = 0, c = clients; c; c = c->next) {
  55. c->x = i * gw;
  56. c->y = j * gh + bh;
  57. c->w = gw;
  58. c->h = gh;
  59. resize(c);
  60. if(++i == cols) {
  61. j++;
  62. i = 0;
  63. }
  64. }
  65. discard_events(EnterWindowMask);
  66. }
  67. void
  68. sel(void *aux)
  69. {
  70. const char *arg = aux;
  71. Client *c = NULL;
  72. if(!arg || !stack)
  73. return;
  74. if(!strncmp(arg, "next", 5))
  75. c = stack->snext ? stack->snext : stack;
  76. else if(!strncmp(arg, "prev", 5))
  77. for(c = stack; c && c->snext; c = c->snext);
  78. if(!c)
  79. c = stack;
  80. raise(c);
  81. focus(c);
  82. }
  83. void
  84. kill(void *aux)
  85. {
  86. Client *c = stack;
  87. if(!c)
  88. return;
  89. if(c->proto & WM_PROTOCOL_DELWIN)
  90. send_message(c->win, wm_atom[WMProtocols], wm_atom[WMDelete]);
  91. else
  92. XKillClient(dpy, c->win);
  93. }
  94. static void
  95. resize_title(Client *c)
  96. {
  97. int i;
  98. c->tw = 0;
  99. for(i = 0; i < TLast; i++)
  100. if(c->tags[i])
  101. c->tw += textw(&brush.font, c->tags[i]) + bh;
  102. c->tw += textw(&brush.font, c->name) + bh;
  103. if(c->tw > c->w)
  104. c->tw = c->w + 2;
  105. c->tx = c->x + c->w - c->tw + 2;
  106. c->ty = c->y;
  107. XMoveResizeWindow(dpy, c->title, c->tx, c->ty, c->tw, c->th);
  108. }
  109. void
  110. update_name(Client *c)
  111. {
  112. XTextProperty name;
  113. int n;
  114. char **list = NULL;
  115. name.nitems = 0;
  116. c->name[0] = 0;
  117. XGetTextProperty(dpy, c->win, &name, net_atom[NetWMName]);
  118. if(!name.nitems)
  119. XGetWMName(dpy, c->win, &name);
  120. if(!name.nitems)
  121. return;
  122. if(name.encoding == XA_STRING)
  123. strncpy(c->name, (char *)name.value, sizeof(c->name));
  124. else {
  125. if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
  126. && n > 0 && *list)
  127. {
  128. strncpy(c->name, *list, sizeof(c->name));
  129. XFreeStringList(list);
  130. }
  131. }
  132. XFree(name.value);
  133. resize_title(c);
  134. }
  135. void
  136. update_size(Client *c)
  137. {
  138. XSizeHints size;
  139. long msize;
  140. if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
  141. size.flags = PSize;
  142. c->flags = size.flags;
  143. if(c->flags & PBaseSize) {
  144. c->basew = size.base_width;
  145. c->baseh = size.base_height;
  146. }
  147. else
  148. c->basew = c->baseh = 0;
  149. if(c->flags & PResizeInc) {
  150. c->incw = size.width_inc;
  151. c->inch = size.height_inc;
  152. }
  153. else
  154. c->incw = c->inch = 0;
  155. if(c->flags & PMaxSize) {
  156. c->maxw = size.max_width;
  157. c->maxh = size.max_height;
  158. }
  159. else
  160. c->maxw = c->maxh = 0;
  161. if(c->flags & PMinSize) {
  162. c->minw = size.min_width;
  163. c->minh = size.min_height;
  164. }
  165. else
  166. c->minw = c->minh = 0;
  167. if(c->flags & PWinGravity)
  168. c->grav = size.win_gravity;
  169. else
  170. c->grav = NorthWestGravity;
  171. }
  172. void
  173. raise(Client *c)
  174. {
  175. XRaiseWindow(dpy, c->win);
  176. XRaiseWindow(dpy, c->title);
  177. }
  178. void
  179. lower(Client *c)
  180. {
  181. XLowerWindow(dpy, c->title);
  182. XLowerWindow(dpy, c->win);
  183. }
  184. void
  185. focus(Client *c)
  186. {
  187. Client **l, *old;
  188. old = stack;
  189. for(l = &stack; *l && *l != c; l = &(*l)->snext);
  190. if(*l)
  191. *l = c->snext;
  192. c->snext = stack;
  193. stack = c;
  194. if(old && old != c) {
  195. XMapWindow(dpy, old->title);
  196. draw_client(old);
  197. }
  198. XUnmapWindow(dpy, c->title);
  199. draw_client(c);
  200. XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
  201. XFlush(dpy);
  202. }
  203. void
  204. manage(Window w, XWindowAttributes *wa)
  205. {
  206. Client *c, **l;
  207. XSetWindowAttributes twa;
  208. c = emallocz(sizeof(Client));
  209. c->win = w;
  210. c->tx = c->x = wa->x;
  211. c->ty = c->y = wa->y;
  212. if(c->y < bh)
  213. c->ty = c->y += bh;
  214. c->tw = c->w = wa->width;
  215. c->h = wa->height;
  216. c->th = bh;
  217. c->border = 1;
  218. update_size(c);
  219. XSetWindowBorderWidth(dpy, c->win, 1);
  220. XSetWindowBorder(dpy, c->win, brush.border);
  221. XSelectInput(dpy, c->win,
  222. StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
  223. XGetTransientForHint(dpy, c->win, &c->trans);
  224. twa.override_redirect = 1;
  225. twa.background_pixmap = ParentRelative;
  226. twa.event_mask = ExposureMask;
  227. c->tags[tsel] = tags[tsel];
  228. c->title = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th,
  229. 0, DefaultDepth(dpy, screen), CopyFromParent,
  230. DefaultVisual(dpy, screen),
  231. CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
  232. update_name(c);
  233. for(l=&clients; *l; l=&(*l)->next);
  234. c->next = *l; /* *l == nil */
  235. *l = c;
  236. XMapRaised(dpy, c->win);
  237. XMapRaised(dpy, c->title);
  238. XGrabButton(dpy, Button1, Mod1Mask, c->win, False, ButtonPressMask,
  239. GrabModeAsync, GrabModeSync, None, None);
  240. XGrabButton(dpy, Button2, Mod1Mask, c->win, False, ButtonPressMask,
  241. GrabModeAsync, GrabModeSync, None, None);
  242. XGrabButton(dpy, Button3, Mod1Mask, c->win, False, ButtonPressMask,
  243. GrabModeAsync, GrabModeSync, None, None);
  244. arrange(NULL);
  245. focus(c);
  246. }
  247. void
  248. gravitate(Client *c, Bool invert)
  249. {
  250. int dx = 0, dy = 0;
  251. switch(c->grav) {
  252. case StaticGravity:
  253. case NorthWestGravity:
  254. case NorthGravity:
  255. case NorthEastGravity:
  256. dy = c->border;
  257. break;
  258. case EastGravity:
  259. case CenterGravity:
  260. case WestGravity:
  261. dy = -(c->h / 2) + c->border;
  262. break;
  263. case SouthEastGravity:
  264. case SouthGravity:
  265. case SouthWestGravity:
  266. dy = -c->h;
  267. break;
  268. default:
  269. break;
  270. }
  271. switch (c->grav) {
  272. case StaticGravity:
  273. case NorthWestGravity:
  274. case WestGravity:
  275. case SouthWestGravity:
  276. dx = c->border;
  277. break;
  278. case NorthGravity:
  279. case CenterGravity:
  280. case SouthGravity:
  281. dx = -(c->w / 2) + c->border;
  282. break;
  283. case NorthEastGravity:
  284. case EastGravity:
  285. case SouthEastGravity:
  286. dx = -(c->w + c->border);
  287. break;
  288. default:
  289. break;
  290. }
  291. if(invert) {
  292. dx = -dx;
  293. dy = -dy;
  294. }
  295. c->x += dx;
  296. c->y += dy;
  297. }
  298. void
  299. resize(Client *c)
  300. {
  301. XConfigureEvent e;
  302. if(c->incw)
  303. c->w -= (c->w - c->basew) % c->incw;
  304. if(c->inch)
  305. c->h -= (c->h - c->baseh) % c->inch;
  306. if(c->minw && c->w < c->minw)
  307. c->w = c->minw;
  308. if(c->minh && c->h < c->minh)
  309. c->h = c->minh;
  310. if(c->maxw && c->w > c->maxw)
  311. c->w = c->maxw;
  312. if(c->maxh && c->h > c->maxh)
  313. c->h = c->maxh;
  314. resize_title(c);
  315. XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
  316. e.type = ConfigureNotify;
  317. e.event = c->win;
  318. e.window = c->win;
  319. e.x = c->x;
  320. e.y = c->y;
  321. e.width = c->w;
  322. e.height = c->h;
  323. e.border_width = c->border;
  324. e.above = None;
  325. e.override_redirect = False;
  326. XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&e);
  327. XFlush(dpy);
  328. }
  329. static int
  330. dummy_error_handler(Display *dpy, XErrorEvent *error)
  331. {
  332. return 0;
  333. }
  334. void
  335. unmanage(Client *c)
  336. {
  337. Client **l;
  338. XGrabServer(dpy);
  339. XSetErrorHandler(dummy_error_handler);
  340. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  341. XDestroyWindow(dpy, c->title);
  342. for(l=&clients; *l && *l != c; l=&(*l)->next);
  343. eassert(*l == c);
  344. *l = c->next;
  345. for(l=&stack; *l && *l != c; l=&(*l)->snext);
  346. eassert(*l == c);
  347. *l = c->snext;
  348. free(c);
  349. XFlush(dpy);
  350. XSetErrorHandler(error_handler);
  351. XUngrabServer(dpy);
  352. arrange(NULL);
  353. if(stack)
  354. focus(stack);
  355. }
  356. Client *
  357. gettitle(Window w)
  358. {
  359. Client *c;
  360. for(c = clients; c; c = c->next)
  361. if(c->title == w)
  362. return c;
  363. return NULL;
  364. }
  365. Client *
  366. getclient(Window w)
  367. {
  368. Client *c;
  369. for(c = clients; c; c = c->next)
  370. if(c->win == w)
  371. return c;
  372. return NULL;
  373. }
  374. void
  375. draw_client(Client *c)
  376. {
  377. int i;
  378. if(c == stack) {
  379. draw_bar();
  380. return;
  381. }
  382. brush.x = brush.y = 0;
  383. brush.h = c->th;
  384. brush.w = 0;
  385. for(i = 0; i < TLast; i++) {
  386. if(c->tags[i]) {
  387. brush.x += brush.w;
  388. brush.w = textw(&brush.font, c->tags[i]) + bh;
  389. draw(dpy, &brush, True, c->tags[i]);
  390. }
  391. }
  392. brush.x += brush.w;
  393. brush.w = textw(&brush.font, c->name) + bh;
  394. draw(dpy, &brush, True, c->name);
  395. XCopyArea(dpy, brush.drawable, c->title, brush.gc,
  396. 0, 0, c->tw, c->th, 0, 0);
  397. XFlush(dpy);
  398. }