config.c 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387
  1. /*
  2. * wmfs2 by Martin Duquesnoy <xorg62@gmail.com> { for(i = 2011; i < 2111; ++i) ©(i); }
  3. * For license, see COPYING.
  4. */
  5. #include "config.h"
  6. #include "wmfs.h"
  7. #include "parse.h"
  8. #include "tag.h"
  9. #include "screen.h"
  10. #include "infobar.h"
  11. #include "util.h"
  12. #include "status.h"
  13. #define ISTRDUP(t, s) \
  14. do { \
  15. if((tmp = s)) \
  16. t = xstrdup(tmp); \
  17. } while(/* CONSTCOND */ 0);
  18. static void
  19. config_mouse_section(struct mbhead *mousebinds, struct conf_sec **sec)
  20. {
  21. struct mousebind *m;
  22. int i = 0;
  23. char *p;
  24. SLIST_INIT(mousebinds);
  25. for(; sec[i]; ++i)
  26. {
  27. m = xcalloc(1, sizeof(struct mousebind));
  28. m->button = fetch_opt_first(sec[i], "1", "button").num;
  29. m->func = uicb_name_func(fetch_opt_first(sec[i], "", "func").str);
  30. if((p = fetch_opt_first(sec[i], "", "cmd").str))
  31. m->cmd = xstrdup(p);
  32. m->use_area = false;
  33. SLIST_INSERT_HEAD(mousebinds, m, next);
  34. SLIST_INSERT_HEAD(&W->h.mousebind, m, globnext);
  35. }
  36. }
  37. static void
  38. config_theme(void)
  39. {
  40. struct theme *t, *p = NULL;
  41. size_t i, n;
  42. struct conf_sec *sec, **ks;
  43. char *tmp;
  44. /* [themes] */
  45. sec = fetch_section_first(NULL, "themes");
  46. ks = fetch_section(sec, "theme");
  47. /* No theme section? Make one with default value anyway. */
  48. if(!(n = fetch_section_count(ks)))
  49. ++n;
  50. SLIST_INIT(&W->h.theme);
  51. /* [theme]*/
  52. for(i = 0; i < n; ++i)
  53. {
  54. t = (struct theme*)xcalloc(1, sizeof(struct theme));
  55. t->name = fetch_opt_first(ks[i], "default", "name").str;
  56. wmfs_init_font(fetch_opt_first(ks[i], "fixed", "font").str, t);
  57. /* bars */
  58. t->bars.fg = color_atoh(fetch_opt_first(ks[i], "#CCCCCC", "bars_fg").str);
  59. t->bars.bg = color_atoh(fetch_opt_first(ks[i], "#222222", "bars_bg").str);
  60. t->bars_width = fetch_opt_first(ks[i], "12", "bars_width").num;
  61. /*
  62. * Elements
  63. */
  64. t->tags_n.fg = color_atoh(fetch_opt_first(ks[i], "#CCCCCC", "tags_normal_fg").str);
  65. t->tags_n.bg = color_atoh(fetch_opt_first(ks[i], "#222222", "tags_normal_bg").str);
  66. t->tags_s.fg = color_atoh(fetch_opt_first(ks[i], "#222222", "tags_sel_fg").str);
  67. t->tags_s.bg = color_atoh(fetch_opt_first(ks[i], "#CCCCCC", "tags_sel_bg").str);
  68. t->tags_o.fg = color_atoh(fetch_opt_first(ks[i], "#CCCCCC", "tags_occupied_fg").str);
  69. t->tags_o.bg = color_atoh(fetch_opt_first(ks[i], "#444444", "tags_occupied_bg").str);
  70. t->tags_u.fg = color_atoh(fetch_opt_first(ks[i], "#444444", "tags_urgent_fg").str);
  71. t->tags_u.bg = color_atoh(fetch_opt_first(ks[i], "#CC4444", "tags_urgent_bg").str);
  72. t->tags_border_col = color_atoh(fetch_opt_first(ks[i], "#888888", "tags_border_color").str);
  73. t->tags_border_width = fetch_opt_first(ks[i], "0", "tags_border_width").num;
  74. /* status line */
  75. t->tags_n_sl = status_new_ctx(NULL, t);
  76. t->tags_s_sl = status_new_ctx(NULL, t);
  77. t->tags_o_sl = status_new_ctx(NULL, t);
  78. t->tags_u_sl = status_new_ctx(NULL, t);
  79. ISTRDUP(t->tags_n_sl.status, fetch_opt_first(ks[i], "", "tags_normal_statusline").str);
  80. ISTRDUP(t->tags_s_sl.status, fetch_opt_first(ks[i], "", "tags_sel_statusline").str);
  81. ISTRDUP(t->tags_o_sl.status, fetch_opt_first(ks[i], "", "tags_occupied_statusline").str);
  82. ISTRDUP(t->tags_u_sl.status, fetch_opt_first(ks[i], "", "tags_urgent_statusline").str);
  83. if(t->tags_n_sl.status)
  84. status_parse(&t->tags_n_sl);
  85. if(t->tags_s_sl.status)
  86. status_parse(&t->tags_s_sl);
  87. if(t->tags_o_sl.status)
  88. status_parse(&t->tags_o_sl);
  89. if(t->tags_u_sl.status)
  90. status_parse(&t->tags_u_sl);
  91. /* Client / frame */
  92. t->client_n.fg = color_atoh(fetch_opt_first(ks[i], "#CCCCCC", "client_normal_fg").str);
  93. t->client_n.bg = color_atoh(fetch_opt_first(ks[i], "#222222", "client_normal_bg").str);
  94. t->client_s.fg = color_atoh(fetch_opt_first(ks[i], "#222222", "client_sel_fg").str);
  95. t->client_s.bg = color_atoh(fetch_opt_first(ks[i], "#CCCCCC", "client_sel_bg").str);
  96. t->frame_bg = color_atoh(fetch_opt_first(ks[i], "#555555", "frame_bg").str);
  97. t->client_titlebar_width = fetch_opt_first(ks[i], "12", "client_titlebar_width").num;
  98. t->client_border_width = fetch_opt_first(ks[i], "1", "client_border_width").num;
  99. /* status line */
  100. t->client_n_sl = status_new_ctx(NULL, t);
  101. t->client_s_sl = status_new_ctx(NULL, t);
  102. ISTRDUP(t->client_n_sl.status, fetch_opt_first(ks[i], "", "client_normal_statusline").str);
  103. ISTRDUP(t->client_s_sl.status, fetch_opt_first(ks[i], "", "client_sel_statusline").str);
  104. if(t->client_n_sl.status)
  105. status_parse(&t->client_n_sl);
  106. if(t->client_s_sl.status)
  107. status_parse(&t->client_s_sl);
  108. SLIST_INSERT_TAIL(&W->h.theme, t, next, p);
  109. p = t;
  110. }
  111. free(ks);
  112. }
  113. static void
  114. config_bars(void)
  115. {
  116. struct screen *s;
  117. struct theme *t;
  118. size_t i, n;
  119. struct conf_sec *sec, **ks;
  120. int screenid;
  121. char *name, *elem;
  122. enum barpos pos = BarTop;
  123. /* [bars] */
  124. sec = fetch_section_first(NULL, "bars");
  125. ks = fetch_section(sec, "bar");
  126. n = fetch_section_count(ks);
  127. /* [bar] */
  128. for(i = 0; i < n; ++i)
  129. {
  130. name = fetch_opt_first(ks[i], "default", "name").str;
  131. elem = fetch_opt_first(ks[i], "", "elements").str;
  132. screenid = fetch_opt_first(ks[i], "-1", "screen").num;
  133. t = name_to_theme(fetch_opt_first(ks[i], "default", "theme").str);
  134. pos = fetch_opt_first(ks[i], "0", "position").num;
  135. SLIST_FOREACH(s, &W->h.screen, next)
  136. if(screenid == s->id || screenid == -1)
  137. infobar_new(s, name, t, pos, elem);
  138. }
  139. free(ks);
  140. }
  141. static void
  142. config_tag(void)
  143. {
  144. struct screen *s;
  145. size_t i, n;
  146. struct conf_sec *sec, **ks, **mb;
  147. char *name;
  148. int screenid;
  149. /* [tags] */
  150. sec = fetch_section_first(NULL, "tags");
  151. ks = fetch_section(sec, "tag");
  152. n = fetch_section_count(ks);
  153. /* [mouse] */
  154. if((mb = fetch_section(sec, "mouse")))
  155. {
  156. config_mouse_section(&W->tmp_head.tag, mb);
  157. free(mb);
  158. }
  159. /* [tag] */
  160. for(i = 0; i < n; ++i)
  161. {
  162. name = fetch_opt_first(ks[i], "tag", "name").str;
  163. screenid = fetch_opt_first(ks[i], "-1", "screen").num;
  164. SLIST_FOREACH(s, &W->h.screen, next)
  165. if(screenid == s->id || screenid == -1)
  166. tag_new(s, name);
  167. }
  168. /* If no tag at all on a screen, add one anyway */
  169. SLIST_FOREACH(s, &W->h.screen, next)
  170. if(TAILQ_EMPTY(&s->tags))
  171. tag_new(s, "tag");
  172. free(ks);
  173. }
  174. static void
  175. config_client(void)
  176. {
  177. struct conf_sec *sec, **mb;
  178. char *tmp;
  179. /* [client] */
  180. sec = fetch_section_first(NULL, "client");
  181. W->client_mod = modkey_keysym(fetch_opt_first(sec, "Super", "key_modifier").str);
  182. tmp = fetch_opt_first(sec, "default", "theme").str;
  183. W->ctheme = name_to_theme(tmp);
  184. /* [mouse] */
  185. /* for client frame AND titlebar */
  186. if((mb = fetch_section(sec, "mouse")))
  187. {
  188. config_mouse_section(&W->tmp_head.client, mb);
  189. free(mb);
  190. }
  191. }
  192. static void
  193. config_rule(void)
  194. {
  195. int i, n;
  196. struct conf_sec *sec, **ks;
  197. struct rule *r;
  198. char *tn, *tmp;
  199. /* [rules] */
  200. sec = fetch_section_first(NULL, "rules");
  201. ks = fetch_section(sec, "rule");
  202. n = fetch_section_count(ks);
  203. SLIST_INIT(&W->h.rule);
  204. /* [rule] */
  205. for(i = 0; i < n; ++i)
  206. {
  207. r = (struct rule*)xcalloc(1, sizeof(struct rule));
  208. ISTRDUP(r->class, fetch_opt_first(ks[i], "", "class").str);
  209. ISTRDUP(r->instance, fetch_opt_first(ks[i], "", "instance").str);
  210. ISTRDUP(r->role, fetch_opt_first(ks[i], "", "role").str);
  211. ISTRDUP(r->name , fetch_opt_first(ks[i], "", "name").str);
  212. r->screen = fetch_opt_first(ks[i], "-1", "screen").num;
  213. r->tag = fetch_opt_first(ks[i], "-1", "tag").num;
  214. FLAGAPPLY(r->flags, fetch_opt_first(ks[i], "false", "free").boolean, RULE_FREE);
  215. FLAGAPPLY(r->flags, fetch_opt_first(ks[i], "false", "tab").boolean, RULE_TAB);
  216. FLAGAPPLY(r->flags, fetch_opt_first(ks[i], "false", "ignore_tag").boolean, RULE_IGNORE_TAG);
  217. if((tn = fetch_opt_first(ks[i], "", "theme").str))
  218. r->theme = name_to_theme(tn);
  219. else
  220. r->theme = SLIST_FIRST(&W->h.theme);
  221. SLIST_INSERT_HEAD(&W->h.rule, r, next);
  222. }
  223. free(ks);
  224. }
  225. static void
  226. config_launcher(void)
  227. {
  228. struct conf_sec *sec, **ks;
  229. struct launcher *l;
  230. int n, i;
  231. /* [launchers] */
  232. sec = fetch_section_first(NULL, "launchers");
  233. ks = fetch_section(sec, "launcher");
  234. n = fetch_section_count(ks);
  235. SLIST_INIT(&W->h.launcher);
  236. /* [launcher] */
  237. for(i = 0; i < n; ++i)
  238. {
  239. l = xcalloc(1, sizeof(struct launcher));
  240. l->name = xstrdup(fetch_opt_first(ks[i], "default", "name").str);
  241. l->prompt = xstrdup(fetch_opt_first(ks[i], ":", "prompt").str);
  242. l->command = xstrdup(fetch_opt_first(ks[i], "spawn", "command").str);
  243. if((l->width = fetch_opt_first(ks[i], "150", "width").num) <= 0)
  244. l->width = 150;
  245. SLIST_INSERT_HEAD(&W->h.launcher, l, next);
  246. }
  247. free(ks);
  248. }
  249. static void
  250. config_keybind(void)
  251. {
  252. int i, n;
  253. size_t j;
  254. struct conf_sec *sec, **ks;
  255. struct opt_type *opt;
  256. char *cmd;
  257. struct keybind *k;
  258. /* [keys] */
  259. sec = fetch_section_first(NULL, "keys");
  260. ks = fetch_section(sec, "key");
  261. n = fetch_section_count(ks);
  262. SLIST_INIT(&W->h.keybind);
  263. /* [key] */
  264. for(i = 0; i < n; ++i)
  265. {
  266. k = (struct keybind*)xcalloc(1, sizeof(struct keybind));
  267. /* mod = {} */
  268. opt = fetch_opt(ks[i], "", "mod");
  269. for(j = k->mod = 0; j < fetch_opt_count(opt); ++j)
  270. k->mod |= modkey_keysym(opt[j].str);
  271. free(opt);
  272. /* key = */
  273. k->keysym = XStringToKeysym(fetch_opt_first(ks[i], "None", "key").str);
  274. /* func = */
  275. if(!(k->func = uicb_name_func(fetch_opt_first(ks[i], "", "func").str)))
  276. {
  277. warnxl("configuration: Unknown Function \"%s\".",
  278. fetch_opt_first(ks[i], "", "func").str);
  279. k->func = uicb_spawn;
  280. }
  281. /* cmd = */
  282. if((cmd = fetch_opt_first(ks[i], "", "cmd").str))
  283. k->cmd = xstrdup(cmd);
  284. SLIST_INSERT_HEAD(&W->h.keybind, k, next);
  285. }
  286. wmfs_grab_keys();
  287. free(ks);
  288. }
  289. void
  290. config_init(void)
  291. {
  292. if(get_conf(W->confpath) == -1)
  293. {
  294. warnl("parsing configuration file (%s) failed.", W->confpath);
  295. sprintf(W->confpath, "%s/"CONFIG_DEFAULT_PATH, getenv("HOME"));
  296. if(get_conf(W->confpath) == -1)
  297. errxl(1, "parsing default configuration file (%s) failed.", W->confpath);
  298. }
  299. config_theme();
  300. config_keybind();
  301. config_tag();
  302. config_client();
  303. config_bars();
  304. config_rule();
  305. config_launcher();
  306. free_conf();
  307. }