client.c 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612
  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. if((c = next(clients)))
  56. focus(c);
  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. discard_events(EnterWindowMask);
  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->tags[tsel]) {
  150. if((sel = next(clients))) {
  151. craise(sel);
  152. focus(sel);
  153. }
  154. }
  155. discard_events(EnterWindowMask);
  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. 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. discard_events(EnterWindowMask);
  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->minw && c->w < c->minw)
  443. c->w = c->minw;
  444. if(c->minh && c->h < c->minh)
  445. c->h = c->minh;
  446. if(c->maxw && c->w > c->maxw)
  447. c->w = c->maxw;
  448. if(c->maxh && c->h > c->maxh)
  449. c->h = c->maxh;
  450. resize_title(c);
  451. XSetWindowBorderWidth(dpy, c->win, 1);
  452. XMoveResizeWindow(dpy, c->win, c->x, c->y, c->w, c->h);
  453. e.type = ConfigureNotify;
  454. e.event = c->win;
  455. e.window = c->win;
  456. e.x = c->x;
  457. e.y = c->y;
  458. e.width = c->w;
  459. e.height = c->h;
  460. e.border_width = c->border;
  461. e.above = None;
  462. e.override_redirect = False;
  463. XSendEvent(dpy, c->win, False, StructureNotifyMask, (XEvent *)&e);
  464. XFlush(dpy);
  465. }
  466. static int
  467. dummy_error_handler(Display *dsply, XErrorEvent *err)
  468. {
  469. return 0;
  470. }
  471. void
  472. unmanage(Client *c)
  473. {
  474. Client **l;
  475. XGrabServer(dpy);
  476. XSetErrorHandler(dummy_error_handler);
  477. XUngrabButton(dpy, AnyButton, AnyModifier, c->win);
  478. XDestroyWindow(dpy, c->title);
  479. for(l = &clients; *l && *l != c; l = &(*l)->next);
  480. *l = c->next;
  481. for(l = &clients; *l; l = &(*l)->next)
  482. if((*l)->revert == c)
  483. (*l)->revert = NULL;
  484. if(sel == c)
  485. sel = sel->revert ? sel->revert : clients;
  486. free(c);
  487. XFlush(dpy);
  488. XSetErrorHandler(error_handler);
  489. XUngrabServer(dpy);
  490. arrange(NULL);
  491. if(sel)
  492. focus(sel);
  493. }
  494. Client *
  495. gettitle(Window w)
  496. {
  497. Client *c;
  498. for(c = clients; c; c = c->next)
  499. if(c->title == w)
  500. return c;
  501. return NULL;
  502. }
  503. Client *
  504. getclient(Window w)
  505. {
  506. Client *c;
  507. for(c = clients; c; c = c->next)
  508. if(c->win == w)
  509. return c;
  510. return NULL;
  511. }
  512. void
  513. draw_client(Client *c)
  514. {
  515. int i;
  516. if(c == sel) {
  517. draw_bar();
  518. XUnmapWindow(dpy, c->title);
  519. XSetWindowBorder(dpy, c->win, dc.fg);
  520. return;
  521. }
  522. XSetWindowBorder(dpy, c->win, dc.bg);
  523. XMapWindow(dpy, c->title);
  524. dc.x = dc.y = 0;
  525. dc.w = 0;
  526. for(i = 0; i < TLast; i++) {
  527. if(c->tags[i]) {
  528. dc.x += dc.w;
  529. dc.w = textw(c->tags[i]) + dc.font.height;
  530. drawtext(c->tags[i], True);
  531. }
  532. }
  533. dc.x += dc.w;
  534. dc.w = textw(c->name) + dc.font.height;
  535. drawtext(c->name, True);
  536. XCopyArea(dpy, dc.drawable, c->title, dc.gc,
  537. 0, 0, c->tw, c->th, 0, 0);
  538. XFlush(dpy);
  539. }