Configuration file for DWM on MacBook Air
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.

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