infobar.c 14 KB

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