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.

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