infobar.c 13 KB

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