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.

498 lines
8.5 KiB

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