infobar.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564
  1. /*
  2. * wmfs2 by Martin Duquesnoy <xorg62@gmail.com> { for(i = 2011; i < 2111; ++i) ©(i); }
  3. * For license, see COPYING.
  4. */
  5. #include "wmfs.h"
  6. #include "draw.h"
  7. #include "infobar.h"
  8. #include "barwin.h"
  9. #include "util.h"
  10. #include "tag.h"
  11. #include "status.h"
  12. #include "systray.h"
  13. #include "client.h"
  14. #define ELEM_FREE_BARWIN(e) \
  15. while(!SLIST_EMPTY(&e->bars)) \
  16. { \
  17. b = SLIST_FIRST(&e->bars); \
  18. SLIST_REMOVE_HEAD(&e->bars, enext); \
  19. barwin_remove(b); \
  20. }
  21. static void infobar_elem_tag_init(struct element *e);
  22. static void infobar_elem_tag_update(struct element *e);
  23. static void infobar_elem_status_init(struct element *e);
  24. static void infobar_elem_status_update(struct element *e);
  25. static void infobar_elem_systray_init(struct element *e);
  26. static void infobar_elem_systray_update(struct element *e);
  27. static void infobar_elem_launcher_init(struct element *e);
  28. static void infobar_elem_launcher_update(struct element *e);
  29. const struct elem_funcs
  30. {
  31. char c;
  32. void (*func_init)(struct element *e);
  33. void (*func_update)(struct element *e);
  34. } elem_funcs[] =
  35. {
  36. { 't', infobar_elem_tag_init, infobar_elem_tag_update },
  37. { 's', infobar_elem_status_init, infobar_elem_status_update },
  38. { 'y', infobar_elem_systray_init, infobar_elem_systray_update },
  39. { 'l', infobar_elem_launcher_init, infobar_elem_launcher_update },
  40. { '\0', NULL, NULL }
  41. };
  42. static void
  43. infobar_elem_tag_init(struct element *e)
  44. {
  45. struct tag *t;
  46. struct barwin *b, *prev = NULL;
  47. int s, j;
  48. /* Get final size before to use in placement */
  49. e->geo.w = e->infobar->theme->tags_border_width << 1;
  50. TAILQ_FOREACH(t, &e->infobar->screen->tags, next)
  51. e->geo.w += draw_textw(e->infobar->theme, t->name) + PAD;
  52. infobar_elem_placement(e);
  53. j = e->geo.x;
  54. e->geo.h -= (e->infobar->theme->tags_border_width << 1);
  55. e->statusctx = &e->infobar->theme->tags_n_sl;
  56. e->statusctx->flags |= STATUS_BLOCK_REFRESH;
  57. if(SLIST_EMPTY(&e->bars) || (e->infobar->screen->flags & SCREEN_TAG_UPDATE))
  58. {
  59. if((e->infobar->screen->flags & SCREEN_TAG_UPDATE))
  60. {
  61. ELEM_FREE_BARWIN(e);
  62. SLIST_INIT(&e->bars);
  63. }
  64. TAILQ_FOREACH(t, &e->infobar->screen->tags, next)
  65. {
  66. s = draw_textw(e->infobar->theme, t->name) + PAD;
  67. /* Init barwin */
  68. b = barwin_new(e->infobar->bar->win, j, 0, s, e->geo.h, 0, 0, false);
  69. /* Status doesn't have theme yet */
  70. t->statusctx.theme = e->infobar->theme;
  71. t->statusctx.flags |= STATUS_BLOCK_REFRESH;
  72. /* Set border */
  73. if(e->infobar->theme->tags_border_width)
  74. {
  75. XSetWindowBorder(W->dpy, b->win, e->infobar->theme->tags_border_col);
  76. XSetWindowBorderWidth(W->dpy, b->win, e->infobar->theme->tags_border_width);
  77. }
  78. b->ptr = (void*)t;
  79. barwin_map(b);
  80. b->mousebinds = W->tmp_head.tag;
  81. SLIST_INSERT_TAIL(&e->bars, b, enext, prev);
  82. prev = b;
  83. j += s;
  84. }
  85. }
  86. else
  87. {
  88. SLIST_FOREACH(b, &e->bars, enext)
  89. {
  90. barwin_move(b, j, 0);
  91. j += b->geo.w;
  92. }
  93. }
  94. }
  95. static void
  96. infobar_elem_tag_update(struct element *e)
  97. {
  98. struct tag *t, *sel = e->infobar->screen->seltag;
  99. struct barwin *b;
  100. SLIST_FOREACH(b, &e->bars, enext)
  101. {
  102. t = (struct tag*)b->ptr;
  103. /* Selected */
  104. if(t == sel)
  105. {
  106. b->fg = e->infobar->theme->tags_s.fg;
  107. b->bg = e->infobar->theme->tags_s.bg;
  108. e->statusctx = &e->infobar->theme->tags_s_sl;
  109. }
  110. else
  111. {
  112. /* Normal tag */
  113. if(SLIST_EMPTY(&t->clients))
  114. {
  115. b->fg = e->infobar->theme->tags_n.fg;
  116. b->bg = e->infobar->theme->tags_n.bg;
  117. e->statusctx = &e->infobar->theme->tags_n_sl;
  118. }
  119. /* Urgent tag */
  120. else if(t->flags & TAG_URGENT)
  121. {
  122. b->fg = e->infobar->theme->tags_u.fg;
  123. b->bg = e->infobar->theme->tags_u.bg;
  124. e->statusctx = &e->infobar->theme->tags_u_sl;
  125. }
  126. /* Occupied tag */
  127. else
  128. {
  129. b->fg = e->infobar->theme->tags_o.fg;
  130. b->bg = e->infobar->theme->tags_o.bg;
  131. e->statusctx = &e->infobar->theme->tags_o_sl;
  132. }
  133. }
  134. barwin_refresh_color(b);
  135. /* Manage status line */
  136. e->statusctx->barwin = b;
  137. status_copy_mousebind(e->statusctx);
  138. status_render(e->statusctx);
  139. t->statusctx.barwin = b;
  140. status_copy_mousebind(&t->statusctx);
  141. status_render(&t->statusctx);
  142. draw_text(b->dr, e->infobar->theme, (PAD >> 1),
  143. TEXTY(e->infobar->theme, e->geo.h), b->fg, t->name);
  144. barwin_refresh(b);
  145. }
  146. }
  147. static void
  148. infobar_elem_status_init(struct element *e)
  149. {
  150. struct element *en = TAILQ_NEXT(e, next);
  151. struct barwin *b;
  152. infobar_elem_placement(e);
  153. e->geo.w = e->infobar->geo.w - e->geo.x - (en ? e->infobar->geo.w - en->geo.x : 0);
  154. if(!(b = SLIST_FIRST(&e->bars)))
  155. {
  156. b = barwin_new(e->infobar->bar->win, e->geo.x, 0, e->geo.w, e->geo.h, 0, 0, false);
  157. barwin_refresh_color(b);
  158. SLIST_INSERT_HEAD(&e->bars, b, enext);
  159. e->infobar->statusctx = status_new_ctx(b, e->infobar->theme);
  160. e->infobar->statusctx.status = strdup("wmfs2");
  161. e->infobar->statusctx.update = true;
  162. }
  163. else
  164. {
  165. barwin_move(b, e->geo.x, e->geo.y);
  166. barwin_resize(b, e->geo.w, e->geo.h);
  167. }
  168. b->fg = e->infobar->theme->bars.fg;
  169. b->bg = e->infobar->theme->bars.bg;
  170. barwin_map(b);
  171. }
  172. static void
  173. infobar_elem_status_update(struct element *e)
  174. {
  175. if(e->infobar->statusctx.update)
  176. status_manage(&e->infobar->statusctx);
  177. else
  178. {
  179. status_render(&e->infobar->statusctx);
  180. status_copy_mousebind(&e->infobar->statusctx);
  181. }
  182. }
  183. static void
  184. infobar_elem_systray_init(struct element *e)
  185. {
  186. struct barwin *b;
  187. /* Activate systray mask; no more systray element allowed now */
  188. W->flags |= WMFS_SYSTRAY;
  189. W->systray.infobar = e->infobar;
  190. e->geo.w = systray_get_width();
  191. infobar_elem_placement(e);
  192. if(!(b = SLIST_FIRST(&e->bars)))
  193. {
  194. b = barwin_new(e->infobar->bar->win, e->geo.x, 0, e->geo.w, e->geo.h, 0, 0, false);
  195. XFreePixmap(W->dpy, b->dr);
  196. SLIST_INSERT_HEAD(&e->bars, b, enext);
  197. W->systray.barwin = b;
  198. systray_acquire();
  199. }
  200. else
  201. {
  202. barwin_move(b, e->geo.x, e->geo.y);
  203. barwin_resize(b, e->geo.w, e->geo.h);
  204. }
  205. XMoveResizeWindow(W->dpy, W->systray.win, 0, 0, e->geo.w, e->geo.h);
  206. }
  207. static void
  208. infobar_elem_systray_update(struct element *e)
  209. {
  210. (void)e;
  211. systray_update();
  212. }
  213. static void
  214. infobar_elem_launcher_init(struct element *e)
  215. {
  216. struct barwin *b;
  217. if(!(W->flags & WMFS_LAUNCHER))
  218. e->geo.w = 1;
  219. infobar_elem_placement(e);
  220. if(!(b = SLIST_FIRST(&e->bars)))
  221. {
  222. b = barwin_new(e->infobar->bar->win, e->geo.x, 0, e->geo.w, e->geo.h, 0, 0, false);
  223. b->fg = e->infobar->theme->bars.fg;
  224. b->bg = e->infobar->theme->bars.bg;
  225. SLIST_INSERT_HEAD(&e->bars, b, enext);
  226. }
  227. else
  228. {
  229. barwin_move(b, e->geo.x, e->geo.y);
  230. barwin_resize(b, e->geo.w, e->geo.h);
  231. }
  232. barwin_refresh_color(b);
  233. barwin_refresh(b);
  234. }
  235. static void
  236. infobar_elem_launcher_update(struct element *e)
  237. {
  238. struct barwin *b = SLIST_FIRST(&e->bars);
  239. int l;
  240. if(!(W->flags & WMFS_LAUNCHER))
  241. return;
  242. barwin_refresh_color(b);
  243. l = draw_textw(e->infobar->theme, e->data) + 2;
  244. draw_text(b->dr, e->infobar->theme, 1, TEXTY(e->infobar->theme, e->geo.h), b->fg, e->data);
  245. /* Cursor */
  246. XDrawLine(W->dpy, b->dr, W->gc, l, 2, l, e->geo.h - 4);
  247. barwin_refresh(b);
  248. }
  249. #define ELEM_INIT(a) \
  250. do { \
  251. e = xcalloc(1, sizeof(struct element)); \
  252. SLIST_INIT(&e->bars); \
  253. e->infobar = i; \
  254. e->type = j; \
  255. e->data = NULL; \
  256. e->align = a; \
  257. e->func_init = elem_funcs[j].func_init; \
  258. e->func_update = elem_funcs[j].func_update; \
  259. } while(/* CONSTCOND */ 0);
  260. static void
  261. infobar_elem_init(struct infobar *i)
  262. {
  263. struct element *e, *es = NULL;
  264. int n, j, k, l = (int)strlen(i->elemorder);
  265. bool s = false;
  266. TAILQ_INIT(&i->elements);
  267. for(n = 0; n < l; ++n)
  268. {
  269. /* Element status found, manage other element from the end */
  270. if(i->elemorder[n] == 's')
  271. {
  272. s = true;
  273. ++n;
  274. break;
  275. }
  276. /* Only one systray element in a wmfs session */
  277. if(i->elemorder[n] == 'y' && W->flags & WMFS_SYSTRAY)
  278. continue;
  279. for(j = 0; j < (int)LEN(elem_funcs); ++j)
  280. if(elem_funcs[j].c == i->elemorder[n])
  281. {
  282. ELEM_INIT(Left);
  283. TAILQ_INSERT_TAIL(&i->elements, e, next);
  284. e->func_init(e);
  285. es = e;
  286. break;
  287. }
  288. }
  289. /* Status make next elements aligning to the right */
  290. if(s)
  291. {
  292. /* Manage element from the end */
  293. for(k = l - 1; k >= n; --k)
  294. {
  295. /* Only one status */
  296. if(i->elemorder[k] == 's' || (i->elemorder[n] == 'y' && W->flags & WMFS_SYSTRAY))
  297. continue;
  298. for(j = 0; j < (int)LEN(elem_funcs); ++j)
  299. if(elem_funcs[j].c == i->elemorder[k])
  300. {
  301. ELEM_INIT(Right);
  302. if(es)
  303. TAILQ_INSERT_AFTER(&i->elements, es, e, next);
  304. else
  305. TAILQ_INSERT_HEAD(&i->elements, e, next);
  306. e->func_init(e);
  307. break;
  308. }
  309. }
  310. /* Init status at the end */
  311. j = ElemStatus;
  312. ELEM_INIT(Left);
  313. if(es)
  314. TAILQ_INSERT_AFTER(&i->elements, es, e, next);
  315. else
  316. TAILQ_INSERT_HEAD(&i->elements, e, next);
  317. e->func_init(e);
  318. }
  319. }
  320. void
  321. infobar_elem_update(struct infobar *i, int type)
  322. {
  323. struct element *e;
  324. TAILQ_FOREACH(e, &i->elements, next)
  325. if(type == e->type || type == -1)
  326. e->func_update(e);
  327. }
  328. void
  329. infobar_elem_reinit(struct infobar *i)
  330. {
  331. struct element *e;
  332. barwin_refresh_color(i->bar);
  333. TAILQ_FOREACH(e, &i->elements, next)
  334. {
  335. /* Status element found, scan from the tail now */
  336. if(e->type == ElemStatus)
  337. {
  338. struct element *ee;
  339. TAILQ_FOREACH_REVERSE(ee, &i->elements, esub, next)
  340. {
  341. if(e == ee)
  342. break;
  343. ee->func_init(ee);
  344. ee->func_update(ee);
  345. }
  346. e->func_init(e);
  347. e->func_update(e);
  348. return;
  349. }
  350. e->func_init(e);
  351. e->func_update(e);
  352. }
  353. barwin_refresh(i->bar);
  354. }
  355. struct infobar*
  356. infobar_new(struct screen *s, char *name, struct theme *theme, enum barpos pos, const char *elem)
  357. {
  358. bool map;
  359. struct infobar *i = (struct infobar*)xcalloc(1, sizeof(struct infobar));
  360. i->screen = s;
  361. i->theme = theme;
  362. i->elemorder = xstrdup(elem);
  363. i->name = xstrdup(name);
  364. map = infobar_placement(i, pos);
  365. /* struct barwin create */
  366. i->bar = barwin_new(W->root, i->geo.x, i->geo.y, i->geo.w, i->geo.h,
  367. theme->bars.fg, theme->bars.bg, false);
  368. SLIST_INSERT_HEAD(&s->infobars, i, next);
  369. /* struct elements */
  370. infobar_elem_init(i);
  371. /* Render, only if pos is Top or Bottom */
  372. if(!map)
  373. return i;
  374. barwin_map(i->bar);
  375. barwin_map_subwin(i->bar);
  376. barwin_refresh_color(i->bar);
  377. infobar_refresh(i);
  378. return i;
  379. }
  380. void
  381. infobar_refresh(struct infobar *i)
  382. {
  383. infobar_elem_update(i, -1);
  384. barwin_refresh(i->bar);
  385. }
  386. void
  387. infobar_remove(struct infobar *i)
  388. {
  389. struct element *e;
  390. struct barwin *b;
  391. free(i->elemorder);
  392. free(i->name);
  393. if(i == W->systray.infobar)
  394. systray_freeicons();
  395. while(!TAILQ_EMPTY(&i->elements))
  396. {
  397. e = TAILQ_FIRST(&i->elements);
  398. TAILQ_REMOVE(&i->elements, e, next);
  399. ELEM_FREE_BARWIN(e);
  400. free(e);
  401. }
  402. barwin_remove(i->bar);
  403. SLIST_REMOVE(&i->screen->infobars, i, infobar, next);
  404. free(i);
  405. }
  406. void
  407. infobar_free(struct screen *s)
  408. {
  409. struct infobar *i;
  410. while(!SLIST_EMPTY(&s->infobars))
  411. {
  412. i = SLIST_FIRST(&s->infobars);
  413. /* SLIST_REMOVE is done by infobar_remove */
  414. infobar_remove(i);
  415. }
  416. }
  417. void
  418. uicb_infobar_toggle_hide(Uicb iname)
  419. {
  420. struct client *c;
  421. struct infobar *i;
  422. if(iname)
  423. i = infobar_gb_name(iname);
  424. else
  425. i = SLIST_FIRST(&W->screen->infobars);
  426. if(i->pos == BarHide)
  427. {
  428. i->pos = i->opos;
  429. if(infobar_placement(i, i->pos))
  430. {
  431. barwin_map(i->bar);
  432. barwin_map_subwin(i->bar);
  433. barwin_refresh_color(i->bar);
  434. infobar_refresh(i);
  435. }
  436. }
  437. else
  438. {
  439. i->opos = i->pos;
  440. i->pos = BarHide;
  441. barwin_unmap_subwin(i->bar);
  442. barwin_unmap(i->bar);
  443. switch(i->opos)
  444. {
  445. case BarTop:
  446. i->screen->ugeo.y -= i->geo.h;
  447. case BarBottom:
  448. i->screen->ugeo.h += i->geo.h;
  449. case BarHide:
  450. default:
  451. break;
  452. }
  453. }
  454. SLIST_FOREACH(c, &W->h.client, next)
  455. layout_fix_hole(c);
  456. }