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.

437 lines
7.9 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. void (*arrange)(void *aux);
  12. void
  13. max(void *aux)
  14. {
  15. if(!stack)
  16. return;
  17. stack->x = sx;
  18. stack->y = sy;
  19. stack->w = sw - 2 * stack->border;
  20. stack->h = sh - 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 - 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. 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. craise(c);
  81. focus(c);
  82. }
  83. void
  84. ckill(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]) + brush.font.height;
  102. c->tw += textw(&brush.font, c->name) + brush.font.height;
  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. craise(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. c->tw = c->w = wa->width;
  213. c->h = wa->height;
  214. c->th = th;
  215. c->border = 1;
  216. update_size(c);
  217. XSetWindowBorderWidth(dpy, c->win, 1);
  218. XSetWindowBorder(dpy, c->win, brush.border);
  219. XSelectInput(dpy, c->win,
  220. StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
  221. XGetTransientForHint(dpy, c->win, &c->trans);
  222. twa.override_redirect = 1;
  223. twa.background_pixmap = ParentRelative;
  224. twa.event_mask = ExposureMask;
  225. c->tags[tsel] = tags[tsel];
  226. c->title = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th,
  227. 0, DefaultDepth(dpy, screen), CopyFromParent,
  228. DefaultVisual(dpy, screen),
  229. CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
  230. update_name(c);
  231. for(l=&clients; *l; l=&(*l)->next);
  232. c->next = *l; /* *l == nil */
  233. *l = c;
  234. XMapRaised(dpy, c->win);
  235. XMapRaised(dpy, c->title);
  236. XGrabButton(dpy, Button1, Mod1Mask, c->win, False, ButtonPressMask,
  237. GrabModeAsync, GrabModeSync, None, None);
  238. XGrabButton(dpy, Button2, Mod1Mask, c->win, False, ButtonPressMask,
  239. GrabModeAsync, GrabModeSync, None, None);
  240. XGrabButton(dpy, Button3, Mod1Mask, c->win, False, ButtonPressMask,
  241. GrabModeAsync, GrabModeSync, None, None);
  242. arrange(NULL);
  243. focus(c);
  244. }
  245. void
  246. gravitate(Client *c, Bool invert)
  247. {
  248. int dx = 0, dy = 0;
  249. switch(c->grav) {
  250. case StaticGravity:
  251. case NorthWestGravity:
  252. case NorthGravity:
  253. case NorthEastGravity:
  254. dy = c->border;
  255. break;
  256. case EastGravity:
  257. case CenterGravity:
  258. case WestGravity:
  259. dy = -(c->h / 2) + c->border;
  260. break;
  261. case SouthEastGravity:
  262. case SouthGravity:
  263. case SouthWestGravity:
  264. dy = -c->h;
  265. break;
  266. default:
  267. break;
  268. }
  269. switch (c->grav) {
  270. case StaticGravity:
  271. case NorthWestGravity:
  272. case WestGravity:
  273. case SouthWestGravity:
  274. dx = c->border;
  275. break;
  276. case NorthGravity:
  277. case CenterGravity:
  278. case SouthGravity:
  279. dx = -(c->w / 2) + c->border;
  280. break;
  281. case NorthEastGravity:
  282. case EastGravity:
  283. case SouthEastGravity:
  284. dx = -(c->w + c->border);
  285. break;
  286. default:
  287. break;
  288. }
  289. if(invert) {
  290. dx = -dx;
  291. dy = -dy;
  292. }
  293. c->x += dx;
  294. c->y += dy;
  295. }
  296. void
  297. resize(Client *c)
  298. {
  299. XConfigureEvent e;
  300. if(c->incw)
  301. c->w -= (c->w - c->basew) % c->incw;
  302. if(c->inch)
  303. c->h -= (c->h - c->baseh) % c->inch;
  304. if(c->minw && c->w < c->minw)
  305. c->w = c->minw;
  306. if(c->minh && c->h < c->minh)
  307. c->h = c->minh;
  308. if(c->maxw && c->w > c->maxw)
  309. c->w = c->maxw;
  310. if(c->maxh && c->h > c->maxh)
  311. c->h = c->maxh;
  312. resize_title(c);
  313. XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
  314. e.type = ConfigureNotify;
  315. e.event = c->win;
  316. e.window = c->win;
  317. e.x = c->x;
  318. e.y = c->y;
  319. e.width = c->w;
  320. e.height = c->h;
  321. e.border_width = c->border;
  322. e.above = None;
  323. e.override_redirect = False;
  324. XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&e);
  325. XFlush(dpy);
  326. }
  327. static int
  328. dummy_error_handler(Display *dpy, XErrorEvent *error)
  329. {
  330. return 0;
  331. }
  332. void
  333. unmanage(Client *c)
  334. {
  335. Client **l;
  336. XGrabServer(dpy);
  337. XSetErrorHandler(dummy_error_handler);
  338. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  339. XDestroyWindow(dpy, c->title);
  340. for(l=&clients; *l && *l != c; l=&(*l)->next);
  341. *l = c->next;
  342. for(l=&stack; *l && *l != c; l=&(*l)->snext);
  343. *l = c->snext;
  344. free(c);
  345. XFlush(dpy);
  346. XSetErrorHandler(error_handler);
  347. XUngrabServer(dpy);
  348. arrange(NULL);
  349. if(stack)
  350. focus(stack);
  351. }
  352. Client *
  353. gettitle(Window w)
  354. {
  355. Client *c;
  356. for(c = clients; c; c = c->next)
  357. if(c->title == w)
  358. return c;
  359. return NULL;
  360. }
  361. Client *
  362. getclient(Window w)
  363. {
  364. Client *c;
  365. for(c = clients; c; c = c->next)
  366. if(c->win == w)
  367. return c;
  368. return NULL;
  369. }
  370. void
  371. draw_client(Client *c)
  372. {
  373. int i;
  374. if(c == stack)
  375. return;
  376. brush.x = brush.y = 0;
  377. brush.h = c->th;
  378. brush.w = 0;
  379. for(i = 0; i < TLast; i++) {
  380. if(c->tags[i]) {
  381. brush.x += brush.w;
  382. brush.w = textw(&brush.font, c->tags[i]) + brush.font.height;
  383. draw(dpy, &brush, True, c->tags[i]);
  384. }
  385. }
  386. brush.x += brush.w;
  387. brush.w = textw(&brush.font, c->name) + brush.font.height;
  388. draw(dpy, &brush, True, c->name);
  389. XCopyArea(dpy, brush.drawable, c->title, brush.gc,
  390. 0, 0, c->tw, c->th, 0, 0);
  391. XFlush(dpy);
  392. }