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.

609 lines
10 KiB

19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 years ago
19 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, *old;
  26. if(!(old = sel))
  27. return;
  28. for(l = &clients; *l && *l != sel; l = &(*l)->next);
  29. *l = sel->next;
  30. old->next = clients; /* pop */
  31. clients = old;
  32. sel = old;
  33. arrange(NULL);
  34. focus(sel);
  35. }
  36. void
  37. max(Arg *arg)
  38. {
  39. if(!sel)
  40. return;
  41. sel->x = sx;
  42. sel->y = sy + bh;
  43. sel->w = sw - 2 * sel->border;
  44. sel->h = sh - 2 * sel->border - bh;
  45. craise(sel);
  46. resize(sel, False);
  47. discard_events(EnterWindowMask);
  48. }
  49. void
  50. view(Arg *arg)
  51. {
  52. Client *c;
  53. tsel = arg->i;
  54. arrange(NULL);
  55. for(c = clients; c; c = next(c->next))
  56. draw_client(c);
  57. draw_bar();
  58. }
  59. void
  60. tappend(Arg *arg)
  61. {
  62. if(!sel)
  63. return;
  64. sel->tags[arg->i] = tags[arg->i];
  65. arrange(NULL);
  66. }
  67. void
  68. ttrunc(Arg *arg)
  69. {
  70. int i;
  71. if(!sel)
  72. return;
  73. for(i = 0; i < TLast; i++)
  74. sel->tags[i] = NULL;
  75. tappend(arg);
  76. }
  77. static void
  78. ban_client(Client *c)
  79. {
  80. XMoveWindow(dpy, c->win, c->x + 2 * sw, c->y);
  81. XMoveWindow(dpy, c->title, c->tx + 2 * sw, c->ty);
  82. }
  83. void
  84. floating(Arg *arg)
  85. {
  86. Client *c;
  87. arrange = floating;
  88. for(c = clients; c; c = c->next) {
  89. if(c->tags[tsel])
  90. resize(c, True);
  91. else
  92. ban_client(c);
  93. }
  94. if(sel && !sel->tags[tsel]) {
  95. if((sel = next(clients))) {
  96. craise(sel);
  97. focus(sel);
  98. }
  99. }
  100. discard_events(EnterWindowMask);
  101. }
  102. void
  103. tiling(Arg *arg)
  104. {
  105. Client *c;
  106. int n, i, w, h;
  107. w = sw - mw;
  108. arrange = tiling;
  109. for(n = 0, c = clients; c; c = c->next)
  110. if(c->tags[tsel] && !c->floating)
  111. n++;
  112. if(n > 1)
  113. h = (sh - bh) / (n - 1);
  114. else
  115. h = sh - bh;
  116. for(i = 0, c = clients; c; c = c->next) {
  117. if(c->tags[tsel]) {
  118. if(c->floating) {
  119. craise(c);
  120. resize(c, True);
  121. continue;
  122. }
  123. if(n == 1) {
  124. c->x = sx;
  125. c->y = sy + bh;
  126. c->w = sw - 2 * c->border;
  127. c->h = sh - 2 * c->border - bh;
  128. }
  129. else if(i == 0) {
  130. c->x = sx;
  131. c->y = sy + bh;
  132. c->w = mw - 2 * c->border;
  133. c->h = sh - 2 * c->border - bh;
  134. }
  135. else {
  136. c->x = sx + mw;
  137. c->y = sy + (i - 1) * h + bh;
  138. c->w = w - 2 * c->border;
  139. c->h = h - 2 * c->border;
  140. }
  141. resize(c, False);
  142. i++;
  143. }
  144. else
  145. ban_client(c);
  146. }
  147. if(sel && !sel->tags[tsel]) {
  148. if((sel = next(clients))) {
  149. craise(sel);
  150. focus(sel);
  151. }
  152. }
  153. discard_events(EnterWindowMask);
  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. sel = c;
  285. if(old && old != c)
  286. draw_client(old);
  287. draw_client(c);
  288. XSetInputFocus(dpy, c->win, RevertToPointerRoot, CurrentTime);
  289. XFlush(dpy);
  290. discard_events(EnterWindowMask);
  291. }
  292. static void
  293. init_tags(Client *c)
  294. {
  295. XClassHint ch;
  296. static unsigned int len = rule ? sizeof(rule) / sizeof(rule[0]) : 0;
  297. unsigned int i, j;
  298. Bool matched = False;
  299. if(!len) {
  300. c->tags[tsel] = tags[tsel];
  301. return;
  302. }
  303. if(XGetClassHint(dpy, c->win, &ch)) {
  304. if(ch.res_class && ch.res_name) {
  305. for(i = 0; i < len; i++)
  306. if(!strncmp(rule[i].class, ch.res_class, sizeof(rule[i].class))
  307. && !strncmp(rule[i].instance, ch.res_name, sizeof(rule[i].instance)))
  308. {
  309. for(j = 0; j < TLast; j++)
  310. c->tags[j] = rule[i].tags[j];
  311. c->floating = rule[i].floating;
  312. matched = True;
  313. break;
  314. }
  315. }
  316. if(ch.res_class)
  317. XFree(ch.res_class);
  318. if(ch.res_name)
  319. XFree(ch.res_name);
  320. }
  321. if(!matched)
  322. c->tags[tsel] = tags[tsel];
  323. }
  324. void
  325. manage(Window w, XWindowAttributes *wa)
  326. {
  327. Client *c, **l;
  328. XSetWindowAttributes twa;
  329. Window trans;
  330. c = emallocz(sizeof(Client));
  331. c->win = w;
  332. c->tx = c->x = wa->x;
  333. c->ty = c->y = wa->y;
  334. if(c->y < bh)
  335. c->ty = c->y += bh;
  336. c->tw = c->w = wa->width;
  337. c->h = wa->height;
  338. c->th = bh;
  339. c->border = 1;
  340. c->proto = win_proto(c->win);
  341. update_size(c);
  342. XSelectInput(dpy, c->win,
  343. StructureNotifyMask | PropertyChangeMask | EnterWindowMask);
  344. XGetTransientForHint(dpy, c->win, &trans);
  345. twa.override_redirect = 1;
  346. twa.background_pixmap = ParentRelative;
  347. twa.event_mask = ExposureMask;
  348. c->title = XCreateWindow(dpy, root, c->tx, c->ty, c->tw, c->th,
  349. 0, DefaultDepth(dpy, screen), CopyFromParent,
  350. DefaultVisual(dpy, screen),
  351. CWOverrideRedirect | CWBackPixmap | CWEventMask, &twa);
  352. update_name(c);
  353. init_tags(c);
  354. for(l = &clients; *l; l = &(*l)->next);
  355. c->next = *l; /* *l == nil */
  356. *l = c;
  357. XGrabButton(dpy, Button1, Mod1Mask, c->win, False, ButtonPressMask,
  358. GrabModeAsync, GrabModeSync, None, None);
  359. XGrabButton(dpy, Button2, Mod1Mask, c->win, False, ButtonPressMask,
  360. GrabModeAsync, GrabModeSync, None, None);
  361. XGrabButton(dpy, Button3, Mod1Mask, c->win, False, ButtonPressMask,
  362. GrabModeAsync, GrabModeSync, None, None);
  363. if(!c->floating)
  364. c->floating = trans
  365. || ((c->maxw == c->minw) && (c->maxh == c->minh));
  366. arrange(NULL);
  367. /* mapping the window now prevents flicker */
  368. if(c->tags[tsel]) {
  369. XMapRaised(dpy, c->win);
  370. XMapRaised(dpy, c->title);
  371. focus(c);
  372. }
  373. else {
  374. ban_client(c);
  375. XMapRaised(dpy, c->win);
  376. XMapRaised(dpy, c->title);
  377. }
  378. }
  379. void
  380. gravitate(Client *c, Bool invert)
  381. {
  382. int dx = 0, dy = 0;
  383. switch(c->grav) {
  384. case StaticGravity:
  385. case NorthWestGravity:
  386. case NorthGravity:
  387. case NorthEastGravity:
  388. dy = c->border;
  389. break;
  390. case EastGravity:
  391. case CenterGravity:
  392. case WestGravity:
  393. dy = -(c->h / 2) + c->border;
  394. break;
  395. case SouthEastGravity:
  396. case SouthGravity:
  397. case SouthWestGravity:
  398. dy = -c->h;
  399. break;
  400. default:
  401. break;
  402. }
  403. switch (c->grav) {
  404. case StaticGravity:
  405. case NorthWestGravity:
  406. case WestGravity:
  407. case SouthWestGravity:
  408. dx = c->border;
  409. break;
  410. case NorthGravity:
  411. case CenterGravity:
  412. case SouthGravity:
  413. dx = -(c->w / 2) + c->border;
  414. break;
  415. case NorthEastGravity:
  416. case EastGravity:
  417. case SouthEastGravity:
  418. dx = -(c->w + c->border);
  419. break;
  420. default:
  421. break;
  422. }
  423. if(invert) {
  424. dx = -dx;
  425. dy = -dy;
  426. }
  427. c->x += dx;
  428. c->y += dy;
  429. }
  430. void
  431. resize(Client *c, Bool inc)
  432. {
  433. XConfigureEvent e;
  434. if(inc) {
  435. if(c->incw)
  436. c->w -= (c->w - c->basew) % c->incw;
  437. if(c->inch)
  438. c->h -= (c->h - c->baseh) % c->inch;
  439. }
  440. if(c->minw && c->w < c->minw)
  441. c->w = c->minw;
  442. if(c->minh && c->h < c->minh)
  443. c->h = c->minh;
  444. if(c->maxw && c->w > c->maxw)
  445. c->w = c->maxw;
  446. if(c->maxh && c->h > c->maxh)
  447. c->h = c->maxh;
  448. resize_title(c);
  449. XSetWindowBorderWidth(dpy, c->win, 1);
  450. XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
  451. e.type = ConfigureNotify;
  452. e.event = c->win;
  453. e.window = c->win;
  454. e.x = c->x;
  455. e.y = c->y;
  456. e.width = c->w;
  457. e.height = c->h;
  458. e.border_width = c->border;
  459. e.above = None;
  460. e.override_redirect = False;
  461. XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&e);
  462. XFlush(dpy);
  463. }
  464. static int
  465. dummy_error_handler(Display *dsply, XErrorEvent *err)
  466. {
  467. return 0;
  468. }
  469. void
  470. unmanage(Client *c)
  471. {
  472. Client **l;
  473. XGrabServer(dpy);
  474. XSetErrorHandler(dummy_error_handler);
  475. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  476. XDestroyWindow(dpy, c->title);
  477. for(l = &clients; *l && *l != c; l = &(*l)->next);
  478. *l = c->next;
  479. for(l = &clients; *l; l = &(*l)->next)
  480. if((*l)->revert == c)
  481. (*l)->revert = NULL;
  482. if(sel == c)
  483. sel = sel->revert ? sel->revert : clients;
  484. free(c);
  485. XFlush(dpy);
  486. XSetErrorHandler(error_handler);
  487. XUngrabServer(dpy);
  488. arrange(NULL);
  489. if(sel)
  490. focus(sel);
  491. }
  492. Client *
  493. gettitle(Window w)
  494. {
  495. Client *c;
  496. for(c = clients; c; c = c->next)
  497. if(c->title == w)
  498. return c;
  499. return NULL;
  500. }
  501. Client *
  502. getclient(Window w)
  503. {
  504. Client *c;
  505. for(c = clients; c; c = c->next)
  506. if(c->win == w)
  507. return c;
  508. return NULL;
  509. }
  510. void
  511. draw_client(Client *c)
  512. {
  513. int i;
  514. if(c == sel) {
  515. draw_bar();
  516. XUnmapWindow(dpy, c->title);
  517. XSetWindowBorder(dpy, c->win, dc.fg);
  518. return;
  519. }
  520. XSetWindowBorder(dpy, c->win, dc.bg);
  521. XMapWindow(dpy, c->title);
  522. dc.x = dc.y = 0;
  523. dc.w = 0;
  524. for(i = 0; i < TLast; i++) {
  525. if(c->tags[i]) {
  526. dc.x += dc.w;
  527. dc.w = textw(c->tags[i]) + dc.font.height;
  528. drawtext(c->tags[i], True);
  529. }
  530. }
  531. dc.x += dc.w;
  532. dc.w = textw(c->name) + dc.font.height;
  533. drawtext(c->name, True);
  534. XCopyArea(dpy, dc.drawable, c->title, dc.gc,
  535. 0, 0, c->tw, c->th, 0, 0);
  536. XFlush(dpy);
  537. }