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.

616 lines
11 KiB

18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
18 years ago
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 <stdlib.h>
  6. #include <stdio.h>
  7. #include <string.h>
  8. #include <X11/Xatom.h>
  9. #include <X11/Xutil.h>
  10. #include "dwm.h"
  11. void (*arrange)(Arg *) = tiling;
  12. static Rule rule[] = {
  13. /* class instance tags floating */
  14. { "Firefox-bin", "Gecko", { [Twww] = "www" }, False },
  15. };
  16. static Client *
  17. next(Client *c)
  18. {
  19. for(; c && !c->tags[tsel]; c = c->next);
  20. return c;
  21. }
  22. void
  23. zoom(Arg *arg)
  24. {
  25. Client **l, *c;
  26. if(!sel)
  27. return;
  28. if(sel == next(clients) && sel->next) {
  29. if((c = next(sel->next)))
  30. sel = c;
  31. }
  32. for(l = &clients; *l && *l != sel; l = &(*l)->next);
  33. *l = sel->next;
  34. sel->next = clients; /* pop */
  35. clients = sel;
  36. arrange(NULL);
  37. focus(sel);
  38. }
  39. void
  40. max(Arg *arg)
  41. {
  42. if(!sel)
  43. return;
  44. sel->x = sx;
  45. sel->y = sy + bh;
  46. sel->w = sw - 2 * sel->border;
  47. sel->h = sh - 2 * sel->border - bh;
  48. craise(sel);
  49. resize(sel, False);
  50. }
  51. void
  52. view(Arg *arg)
  53. {
  54. Client *c;
  55. tsel = arg->i;
  56. arrange(NULL);
  57. for(c = clients; c; c = next(c->next))
  58. draw_client(c);
  59. draw_bar();
  60. }
  61. void
  62. tappend(Arg *arg)
  63. {
  64. if(!sel)
  65. return;
  66. sel->tags[arg->i] = tags[arg->i];
  67. arrange(NULL);
  68. }
  69. void
  70. ttrunc(Arg *arg)
  71. {
  72. int i;
  73. if(!sel)
  74. return;
  75. for(i = 0; i < TLast; i++)
  76. sel->tags[i] = NULL;
  77. tappend(arg);
  78. }
  79. static void
  80. ban_client(Client *c)
  81. {
  82. XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
  83. XMoveWindow(dpy, c->title, c->tx + 2 * sw, c->ty);
  84. }
  85. void
  86. floating(Arg *arg)
  87. {
  88. Client *c;
  89. arrange = floating;
  90. for(c = clients; c; c = c->next) {
  91. if(c->tags[tsel])
  92. resize(c, True);
  93. else
  94. ban_client(c);
  95. }
  96. if(sel && !sel->tags[tsel]) {
  97. if((sel = next(clients))) {
  98. craise(sel);
  99. focus(sel);
  100. }
  101. }
  102. }
  103. void
  104. tiling(Arg *arg)
  105. {
  106. Client *c;
  107. int n, i, w, h;
  108. w = sw - mw;
  109. arrange = tiling;
  110. for(n = 0, c = clients; c; c = c->next)
  111. if(c->tags[tsel] && !c->floating)
  112. n++;
  113. if(n > 1)
  114. h = (sh - bh) / (n - 1);
  115. else
  116. h = sh - bh;
  117. for(i = 0, c = clients; c; c = c->next) {
  118. if(c->tags[tsel]) {
  119. if(c->floating) {
  120. craise(c);
  121. resize(c, True);
  122. continue;
  123. }
  124. if(n == 1) {
  125. c->x = sx;
  126. c->y = sy + bh;
  127. c->w = sw - 2 * c->border;
  128. c->h = sh - 2 * c->border - bh;
  129. }
  130. else if(i == 0) {
  131. c->x = sx;
  132. c->y = sy + bh;
  133. c->w = mw - 2 * c->border;
  134. c->h = sh - 2 * c->border - bh;
  135. }
  136. else {
  137. c->x = sx + mw;
  138. c->y = sy + (i - 1) * h + bh;
  139. c->w = w - 2 * c->border;
  140. c->h = h - 2 * c->border;
  141. }
  142. resize(c, False);
  143. i++;
  144. }
  145. else
  146. ban_client(c);
  147. }
  148. if(!sel || (sel && !sel->tags[tsel])) {
  149. if((sel = next(clients))) {
  150. craise(sel);
  151. focus(sel);
  152. }
  153. }
  154. }
  155. void
  156. prevc(Arg *arg)
  157. {
  158. Client *c;
  159. if(!sel)
  160. return;
  161. if((c = sel->revert && sel->revert->tags[tsel] ? sel->revert : NULL)) {
  162. craise(c);
  163. focus(c);
  164. }
  165. }
  166. void
  167. nextc(Arg *arg)
  168. {
  169. Client *c;
  170. if(!sel)
  171. return;
  172. if(!(c = next(sel->next)))
  173. c = next(clients);
  174. if(c) {
  175. craise(c);
  176. c->revert = sel;
  177. focus(c);
  178. }
  179. }
  180. void
  181. ckill(Arg *arg)
  182. {
  183. if(!sel)
  184. return;
  185. if(sel->proto & WM_PROTOCOL_DELWIN)
  186. send_message(sel->win, wm_atom[WMProtocols], wm_atom[WMDelete]);
  187. else
  188. XKillClient(dpy, sel->win);
  189. }
  190. static void
  191. resize_title(Client *c)
  192. {
  193. int i;
  194. c->tw = 0;
  195. for(i = 0; i < TLast; i++)
  196. if(c->tags[i])
  197. c->tw += textw(c->tags[i]) + dc.font.height;
  198. c->tw += textw(c->name) + dc.font.height;
  199. if(c->tw > c->w)
  200. c->tw = c->w + 2;
  201. c->tx = c->x + c->w - c->tw + 2;
  202. c->ty = c->y;
  203. XMoveResizeWindow(dpy, c->title, c->tx, c->ty, c->tw, c->th);
  204. }
  205. void
  206. update_name(Client *c)
  207. {
  208. XTextProperty name;
  209. int n;
  210. char **list = NULL;
  211. name.nitems = 0;
  212. c->name[0] = 0;
  213. XGetTextProperty(dpy, c->win, &name, net_atom[NetWMName]);
  214. if(!name.nitems)
  215. XGetWMName(dpy, c->win, &name);
  216. if(!name.nitems)
  217. return;
  218. if(name.encoding == XA_STRING)
  219. strncpy(c->name, (char *)name.value, sizeof(c->name));
  220. else {
  221. if(XmbTextPropertyToTextList(dpy, &name, &list, &n) >= Success
  222. && n > 0 && *list)
  223. {
  224. strncpy(c->name, *list, sizeof(c->name));
  225. XFreeStringList(list);
  226. }
  227. }
  228. XFree(name.value);
  229. resize_title(c);
  230. }
  231. void
  232. update_size(Client *c)
  233. {
  234. XSizeHints size;
  235. long msize;
  236. if(!XGetWMNormalHints(dpy, c->win, &size, &msize) || !size.flags)
  237. size.flags = PSize;
  238. c->flags = size.flags;
  239. if(c->flags & PBaseSize) {
  240. c->basew = size.base_width;
  241. c->baseh = size.base_height;
  242. }
  243. else
  244. c->basew = c->baseh = 0;
  245. if(c->flags & PResizeInc) {
  246. c->incw = size.width_inc;
  247. c->inch = size.height_inc;
  248. }
  249. else
  250. c->incw = c->inch = 0;
  251. if(c->flags & PMaxSize) {
  252. c->maxw = size.max_width;
  253. c->maxh = size.max_height;
  254. }
  255. else
  256. c->maxw = c->maxh = 0;
  257. if(c->flags & PMinSize) {
  258. c->minw = size.min_width;
  259. c->minh = size.min_height;
  260. }
  261. else
  262. c->minw = c->minh = 0;
  263. if(c->flags & PWinGravity)
  264. c->grav = size.win_gravity;
  265. else
  266. c->grav = NorthWestGravity;
  267. }
  268. void
  269. craise(Client *c)
  270. {
  271. XRaiseWindow(dpy, c->win);
  272. XRaiseWindow(dpy, c->title);
  273. }
  274. void
  275. lower(Client *c)
  276. {
  277. XLowerWindow(dpy, c->title);
  278. XLowerWindow(dpy, c->win);
  279. }
  280. void
  281. focus(Client *c)
  282. {
  283. Client *old = sel;
  284. XEvent ev;
  285. XFlush(dpy);
  286. sel = c;
  287. if(old && old != c)
  288. draw_client(old);
  289. draw_client(c);
  290. XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
  291. XFlush(dpy);
  292. while(XCheckMaskEvent(dpy, EnterWindowMask, &ev));
  293. }
  294. static void
  295. init_tags(Client *c)
  296. {
  297. XClassHint ch;
  298. static unsigned int len = rule ? sizeof(rule) / sizeof(rule[0]) : 0;
  299. unsigned int i, j;
  300. Bool matched = False;
  301. if(!len) {
  302. c->tags[tsel] = tags[tsel];
  303. return;
  304. }
  305. if(XGetClassHint(dpy, c->win, &ch)) {
  306. if(ch.res_class && ch.res_name) {
  307. for(i = 0; i < len; i++)
  308. if(!strncmp(rule[i].class, ch.res_class, sizeof(rule[i].class))
  309. && !strncmp(rule[i].instance, ch.res_name, sizeof(rule[i].instance)))
  310. {
  311. for(j = 0; j < TLast; j++)
  312. c->tags[j] = rule[i].tags[j];
  313. c->floating = rule[i].floating;
  314. matched = True;
  315. break;
  316. }
  317. }
  318. if(ch.res_class)
  319. XFree(ch.res_class);
  320. if(ch.res_name)
  321. XFree(ch.res_name);
  322. }
  323. if(!matched)
  324. c->tags[tsel] = tags[tsel];
  325. }
  326. void
  327. manage(Window w, XWindowAttributes *wa)
  328. {
  329. Client *c, **l;
  330. XSetWindowAttributes twa;
  331. Window trans;
  332. c = emallocz(sizeof(Client));
  333. c->win = w;
  334. c->tx = c->x = wa->x;
  335. c->ty = c->y = wa->y;
  336. if(c->y < bh)
  337. c->ty = c->y += bh;
  338. c->tw = c->w = wa->width;
  339. c->h = wa->height;
  340. c->th = bh;
  341. c->border = 1;
  342. c->proto = win_proto(c->win);
  343. update_size(c);
  344. XSelectInput(dpy, c->win,
  345. StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
  346. XGetTransientForHint(dpy, c->win, &trans);
  347. twa.override_redirect = 1;
  348. twa.background_pixmap = ParentRelative;
  349. twa.event_mask = ExposureMask;
  350. c->title = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th,
  351. 0, DefaultDepth(dpy, screen), CopyFromParent,
  352. DefaultVisual(dpy, screen),
  353. CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
  354. update_name(c);
  355. init_tags(c);
  356. for(l = &clients; *l; l = &(*l)->next);
  357. c->next = *l; /* *l == nil */
  358. *l = c;
  359. XGrabButton(dpy, Button1, Mod1Mask, c->win, False, ButtonPressMask,
  360. GrabModeAsync, GrabModeSync, None, None);
  361. XGrabButton(dpy, Button2, Mod1Mask, c->win, False, ButtonPressMask,
  362. GrabModeAsync, GrabModeSync, None, None);
  363. XGrabButton(dpy, Button3, Mod1Mask, c->win, False, ButtonPressMask,
  364. GrabModeAsync, GrabModeSync, None, None);
  365. if(!c->floating)
  366. c->floating = trans
  367. || ((c->maxw == c->minw) && (c->maxh == c->minh));
  368. arrange(NULL);
  369. /* mapping the window now prevents flicker */
  370. if(c->tags[tsel]) {
  371. XMapRaised(dpy, c->win);
  372. XMapRaised(dpy, c->title);
  373. focus(c);
  374. }
  375. else {
  376. ban_client(c);
  377. XMapRaised(dpy, c->win);
  378. XMapRaised(dpy, c->title);
  379. }
  380. }
  381. void
  382. gravitate(Client *c, Bool invert)
  383. {
  384. int dx = 0, dy = 0;
  385. switch(c->grav) {
  386. case StaticGravity:
  387. case NorthWestGravity:
  388. case NorthGravity:
  389. case NorthEastGravity:
  390. dy = c->border;
  391. break;
  392. case EastGravity:
  393. case CenterGravity:
  394. case WestGravity:
  395. dy = -(c->h / 2) + c->border;
  396. break;
  397. case SouthEastGravity:
  398. case SouthGravity:
  399. case SouthWestGravity:
  400. dy = -c->h;
  401. break;
  402. default:
  403. break;
  404. }
  405. switch (c->grav) {
  406. case StaticGravity:
  407. case NorthWestGravity:
  408. case WestGravity:
  409. case SouthWestGravity:
  410. dx = c->border;
  411. break;
  412. case NorthGravity:
  413. case CenterGravity:
  414. case SouthGravity:
  415. dx = -(c->w / 2) + c->border;
  416. break;
  417. case NorthEastGravity:
  418. case EastGravity:
  419. case SouthEastGravity:
  420. dx = -(c->w + c->border);
  421. break;
  422. default:
  423. break;
  424. }
  425. if(invert) {
  426. dx = -dx;
  427. dy = -dy;
  428. }
  429. c->x += dx;
  430. c->y += dy;
  431. }
  432. void
  433. resize(Client *c, Bool inc)
  434. {
  435. XConfigureEvent e;
  436. if(inc) {
  437. if(c->incw)
  438. c->w -= (c->w - c->basew) % c->incw;
  439. if(c->inch)
  440. c->h -= (c->h - c->baseh) % c->inch;
  441. }
  442. if(c->x > sw) /* might happen on restart */
  443. c->x = sw - c->w;
  444. if(c->y > sh)
  445. c->ty = c->y = sh - c->h;
  446. if(c->minw && c->w < c->minw)
  447. c->w = c->minw;
  448. if(c->minh && c->h < c->minh)
  449. c->h = c->minh;
  450. if(c->maxw && c->w > c->maxw)
  451. c->w = c->maxw;
  452. if(c->maxh && c->h > c->maxh)
  453. c->h = c->maxh;
  454. resize_title(c);
  455. XSetWindowBorderWidth(dpy, c->win, 1);
  456. XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
  457. e.type = ConfigureNotify;
  458. e.event = c->win;
  459. e.window = c->win;
  460. e.x = c->x;
  461. e.y = c->y;
  462. e.width = c->w;
  463. e.height = c->h;
  464. e.border_width = c->border;
  465. e.above = None;
  466. e.override_redirect = False;
  467. XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&e);
  468. XFlush(dpy);
  469. }
  470. static int
  471. dummy_error_handler(Display *dsply, XErrorEvent *err)
  472. {
  473. return 0;
  474. }
  475. void
  476. unmanage(Client *c)
  477. {
  478. Client **l;
  479. XGrabServer(dpy);
  480. XSetErrorHandler(dummy_error_handler);
  481. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  482. XDestroyWindow(dpy, c->title);
  483. for(l = &clients; *l && *l != c; l = &(*l)->next);
  484. *l = c->next;
  485. for(l = &clients; *l; l = &(*l)->next)
  486. if((*l)->revert == c)
  487. (*l)->revert = NULL;
  488. if(sel == c)
  489. sel = sel->revert ? sel->revert : clients;
  490. free(c);
  491. XFlush(dpy);
  492. XSetErrorHandler(error_handler);
  493. XUngrabServer(dpy);
  494. arrange(NULL);
  495. if(sel)
  496. focus(sel);
  497. }
  498. Client *
  499. gettitle(Window w)
  500. {
  501. Client *c;
  502. for(c = clients; c; c = c->next)
  503. if(c->title == w)
  504. return c;
  505. return NULL;
  506. }
  507. Client *
  508. getclient(Window w)
  509. {
  510. Client *c;
  511. for(c = clients; c; c = c->next)
  512. if(c->win == w)
  513. return c;
  514. return NULL;
  515. }
  516. void
  517. draw_client(Client *c)
  518. {
  519. int i;
  520. if(c == sel) {
  521. draw_bar();
  522. XUnmapWindow(dpy, c->title);
  523. XSetWindowBorder(dpy, c->win, dc.fg);
  524. return;
  525. }
  526. XSetWindowBorder(dpy, c->win, dc.bg);
  527. XMapWindow(dpy, c->title);
  528. dc.x = dc.y = 0;
  529. dc.w = 0;
  530. for(i = 0; i < TLast; i++) {
  531. if(c->tags[i]) {
  532. dc.x += dc.w;
  533. dc.w = textw(c->tags[i]) + dc.font.height;
  534. drawtext(c->tags[i], False, True);
  535. }
  536. }
  537. dc.x += dc.w;
  538. dc.w = textw(c->name) + dc.font.height;
  539. drawtext(c->name, False, True);
  540. XCopyArea(dpy, dc.drawable, c->title, dc.gc,
  541. 0, 0, c->tw, c->th, 0, 0);
  542. XFlush(dpy);
  543. }