infobar.c 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502
  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. if(SLIST_EMPTY(&t->clients))
  96. {
  97. b->fg = e->infobar->theme->tags_n.fg;
  98. b->bg = e->infobar->theme->tags_n.bg;
  99. e->statusctx = &e->infobar->theme->tags_n_sl;
  100. }
  101. /* Occupied tag */
  102. else
  103. {
  104. b->fg = e->infobar->theme->tags_o.fg;
  105. b->bg = e->infobar->theme->tags_o.bg;
  106. e->statusctx = &e->infobar->theme->tags_o_sl;
  107. }
  108. }
  109. barwin_refresh_color(b);
  110. /* Manage status line */
  111. e->statusctx->barwin = b;
  112. status_copy_mousebind(e->statusctx);
  113. status_render(e->statusctx);
  114. draw_text(b->dr, e->infobar->theme, (PAD >> 1),
  115. TEXTY(e->infobar->theme, e->geo.h), b->fg, t->name);
  116. barwin_refresh(b);
  117. }
  118. }
  119. static void
  120. infobar_elem_status_init(struct element *e)
  121. {
  122. struct element *en = TAILQ_NEXT(e, next);
  123. struct barwin *b;
  124. infobar_elem_placement(e);
  125. e->geo.w = e->infobar->geo.w - e->geo.x - (en ? e->infobar->geo.w - en->geo.x : 0);
  126. if(!(b = SLIST_FIRST(&e->bars)))
  127. {
  128. b = barwin_new(e->infobar->bar->win, e->geo.x, 0, e->geo.w, e->geo.h, 0, 0, false);
  129. barwin_refresh_color(b);
  130. SLIST_INSERT_HEAD(&e->bars, b, enext);
  131. e->infobar->statusctx = status_new_ctx(b, e->infobar->theme);
  132. e->infobar->statusctx.status = strdup("wmfs2");
  133. e->infobar->statusctx.update = true;
  134. }
  135. else
  136. {
  137. barwin_move(b, e->geo.x, e->geo.y);
  138. barwin_resize(b, e->geo.w, e->geo.h);
  139. }
  140. b->fg = e->infobar->theme->bars.fg;
  141. b->bg = e->infobar->theme->bars.bg;
  142. barwin_map(b);
  143. }
  144. static void
  145. infobar_elem_status_update(struct element *e)
  146. {
  147. if(e->infobar->statusctx.update)
  148. status_manage(&e->infobar->statusctx);
  149. else
  150. {
  151. status_render(&e->infobar->statusctx);
  152. status_copy_mousebind(&e->infobar->statusctx);
  153. }
  154. }
  155. static void
  156. infobar_elem_systray_init(struct element *e)
  157. {
  158. struct barwin *b;
  159. /* Activate systray mask; no more systray element allowed now */
  160. W->flags |= WMFS_SYSTRAY;
  161. W->systray.infobar = e->infobar;
  162. e->geo.w = systray_get_width();
  163. infobar_elem_placement(e);
  164. if(!(b = SLIST_FIRST(&e->bars)))
  165. {
  166. b = barwin_new(e->infobar->bar->win, e->geo.x, 0, e->geo.w, e->geo.h, 0, 0, false);
  167. XFreePixmap(W->dpy, b->dr);
  168. SLIST_INSERT_HEAD(&e->bars, b, enext);
  169. W->systray.barwin = b;
  170. systray_acquire();
  171. }
  172. else
  173. {
  174. barwin_move(b, e->geo.x, e->geo.y);
  175. barwin_resize(b, e->geo.w, e->geo.h);
  176. }
  177. XMoveResizeWindow(W->dpy, W->systray.win, 0, 0, e->geo.w, e->geo.h);
  178. }
  179. static void
  180. infobar_elem_systray_update(struct element *e)
  181. {
  182. (void)e;
  183. systray_update();
  184. }
  185. static void
  186. infobar_elem_launcher_init(struct element *e)
  187. {
  188. struct barwin *b;
  189. if(!(W->flags & WMFS_LAUNCHER))
  190. e->geo.w = 1;
  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. b->fg = e->infobar->theme->bars.fg;
  196. b->bg = e->infobar->theme->bars.bg;
  197. SLIST_INSERT_HEAD(&e->bars, b, enext);
  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. barwin_refresh_color(b);
  205. barwin_refresh(b);
  206. }
  207. static void
  208. infobar_elem_launcher_update(struct element *e)
  209. {
  210. struct barwin *b = SLIST_FIRST(&e->bars);
  211. int l;
  212. if(!(W->flags & WMFS_LAUNCHER))
  213. return;
  214. barwin_refresh_color(b);
  215. l = draw_textw(e->infobar->theme, e->data) + 2;
  216. draw_text(b->dr, e->infobar->theme, 1, TEXTY(e->infobar->theme, e->geo.h), b->fg, e->data);
  217. /* Cursor */
  218. XDrawLine(W->dpy, b->dr, W->gc, l, 2, l, e->geo.h - 4);
  219. barwin_refresh(b);
  220. }
  221. #define ELEM_INIT(a) \
  222. do { \
  223. e = xcalloc(1, sizeof(struct element)); \
  224. SLIST_INIT(&e->bars); \
  225. e->infobar = i; \
  226. e->type = j; \
  227. e->data = NULL; \
  228. e->align = a; \
  229. e->func_init = elem_funcs[j].func_init; \
  230. e->func_update = elem_funcs[j].func_update; \
  231. } while(/* CONSTCOND */ 0);
  232. static void
  233. infobar_elem_init(struct infobar *i)
  234. {
  235. struct element *e, *es = NULL;
  236. int n, j, k, l = (int)strlen(i->elemorder);
  237. bool s = false;
  238. TAILQ_INIT(&i->elements);
  239. for(n = 0; n < l; ++n)
  240. {
  241. /* Element status found, manage other element from the end */
  242. if(i->elemorder[n] == 's')
  243. {
  244. s = true;
  245. ++n;
  246. break;
  247. }
  248. /* Only one systray element in a wmfs session */
  249. if(i->elemorder[n] == 'y' && W->flags & WMFS_SYSTRAY)
  250. continue;
  251. for(j = 0; j < (int)LEN(elem_funcs); ++j)
  252. if(elem_funcs[j].c == i->elemorder[n])
  253. {
  254. ELEM_INIT(Left);
  255. TAILQ_INSERT_TAIL(&i->elements, e, next);
  256. e->func_init(e);
  257. es = e;
  258. break;
  259. }
  260. }
  261. /* Status make next elements aligning to the right */
  262. if(s)
  263. {
  264. /* Manage element from the end */
  265. for(k = l - 1; k >= n; --k)
  266. {
  267. /* Only one status */
  268. if(i->elemorder[k] == 's' || (i->elemorder[n] == 'y' && W->flags & WMFS_SYSTRAY))
  269. continue;
  270. for(j = 0; j < (int)LEN(elem_funcs); ++j)
  271. if(elem_funcs[j].c == i->elemorder[k])
  272. {
  273. ELEM_INIT(Right);
  274. if(es)
  275. TAILQ_INSERT_AFTER(&i->elements, es, e, next);
  276. else
  277. TAILQ_INSERT_HEAD(&i->elements, e, next);
  278. e->func_init(e);
  279. break;
  280. }
  281. }
  282. /* Init status at the end */
  283. j = ElemStatus;
  284. ELEM_INIT(Left);
  285. if(es)
  286. TAILQ_INSERT_AFTER(&i->elements, es, e, next);
  287. else
  288. TAILQ_INSERT_HEAD(&i->elements, e, next);
  289. e->func_init(e);
  290. }
  291. }
  292. void
  293. infobar_elem_update(struct infobar *i, int type)
  294. {
  295. struct element *e;
  296. TAILQ_FOREACH(e, &i->elements, next)
  297. if(type == e->type || type == -1)
  298. e->func_update(e);
  299. }
  300. void
  301. infobar_elem_remove(struct element *e)
  302. {
  303. struct barwin *b;
  304. TAILQ_REMOVE(&e->infobar->elements, e, next);
  305. while(!SLIST_EMPTY(&e->bars))
  306. {
  307. b = SLIST_FIRST(&e->bars);
  308. SLIST_REMOVE_HEAD(&e->bars, enext);
  309. barwin_remove(b);
  310. }
  311. free(e);
  312. }
  313. void
  314. infobar_elem_reinit(struct infobar *i)
  315. {
  316. struct element *e;
  317. barwin_refresh_color(i->bar);
  318. TAILQ_FOREACH(e, &i->elements, next)
  319. {
  320. /* Status element found, scan from the tail now */
  321. if(e->type == ElemStatus)
  322. {
  323. struct element *ee;
  324. TAILQ_FOREACH_REVERSE(ee, &i->elements, esub, next)
  325. {
  326. if(e == ee)
  327. break;
  328. ee->func_init(ee);
  329. ee->func_update(ee);
  330. }
  331. e->func_init(e);
  332. e->func_update(e);
  333. return;
  334. }
  335. e->func_init(e);
  336. e->func_update(e);
  337. }
  338. barwin_refresh(i->bar);
  339. }
  340. struct infobar*
  341. infobar_new(struct screen *s, char *name, struct theme *theme, enum barpos pos, const char *elem)
  342. {
  343. bool map;
  344. struct infobar *i = (struct infobar*)xcalloc(1, sizeof(struct infobar));
  345. i->screen = s;
  346. i->theme = theme;
  347. i->elemorder = xstrdup(elem);
  348. i->name = xstrdup(name);
  349. map = infobar_placement(i, pos);
  350. /* struct barwin create */
  351. i->bar = barwin_new(W->root, i->geo.x, i->geo.y, i->geo.w, i->geo.h,
  352. theme->bars.fg, theme->bars.bg, false);
  353. SLIST_INSERT_HEAD(&s->infobars, i, next);
  354. /* struct elements */
  355. infobar_elem_init(i);
  356. /* Render, only if pos is Top or Bottom */
  357. if(!map)
  358. return i;
  359. barwin_map(i->bar);
  360. barwin_map_subwin(i->bar);
  361. barwin_refresh_color(i->bar);
  362. infobar_refresh(i);
  363. return i;
  364. }
  365. void
  366. infobar_refresh(struct infobar *i)
  367. {
  368. infobar_elem_update(i, -1);
  369. barwin_refresh(i->bar);
  370. }
  371. void
  372. infobar_remove(struct infobar *i)
  373. {
  374. struct element *e;
  375. free(i->elemorder);
  376. free(i->name);
  377. if(i == W->systray.infobar)
  378. systray_freeicons();
  379. TAILQ_FOREACH(e, &i->elements, next)
  380. infobar_elem_remove(e);
  381. barwin_remove(i->bar);
  382. SLIST_REMOVE(&i->screen->infobars, i, infobar, next);
  383. free(i);
  384. }
  385. void
  386. infobar_free(struct screen *s)
  387. {
  388. struct infobar *i;
  389. while(!SLIST_EMPTY(&s->infobars))
  390. {
  391. i = SLIST_FIRST(&s->infobars);
  392. /* SLIST_REMOVE is done by infobar_remove */
  393. infobar_remove(i);
  394. }
  395. }