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.

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