config.c 5.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216
  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. #define CONFIG_DEFAULT_PATH ".config/wmfs/wmfsrc2" /* tmp */
  13. static void
  14. config_theme(void)
  15. {
  16. struct theme *t, *p = NULL;
  17. size_t i, n;
  18. struct conf_sec *sec, **ks;
  19. /* [themes] */
  20. sec = fetch_section_first(NULL, "themes");
  21. ks = fetch_section(sec, "theme");
  22. /* No theme section? Make one with default value anyway. */
  23. if(!(n = fetch_section_count(ks)))
  24. ++n;
  25. SLIST_INIT(&W->h.theme);
  26. /* [theme]*/
  27. for(i = 0; i < n; ++i)
  28. {
  29. t = (struct theme*)xcalloc(1, sizeof(struct theme));
  30. t->name = fetch_opt_first(ks[i], "default", "name").str;
  31. wmfs_init_font(fetch_opt_first(ks[i], "fixed", "font").str, t);
  32. /* bars */
  33. t->bars.fg = color_atoh(fetch_opt_first(ks[i], "#CCCCCC", "bars_fg").str);
  34. t->bars.bg = color_atoh(fetch_opt_first(ks[i], "#222222", "bars_bg").str);
  35. t->bars_width = fetch_opt_first(ks[i], "12", "bars_width").num;
  36. /*
  37. * Elements
  38. */
  39. t->tags_n.fg = color_atoh(fetch_opt_first(ks[i], "#CCCCCC", "tags_normal_fg").str);
  40. t->tags_n.bg = color_atoh(fetch_opt_first(ks[i], "#222222", "tags_normal_bg").str);
  41. t->tags_s.fg = color_atoh(fetch_opt_first(ks[i], "#222222", "tags_sel_fg").str);
  42. t->tags_s.bg = color_atoh(fetch_opt_first(ks[i], "#CCCCCC", "tags_sel_bg").str);
  43. t->tags_border_col = color_atoh(fetch_opt_first(ks[i], "#888888", "tags_border_color").str);
  44. t->tags_border_width = fetch_opt_first(ks[i], "0", "tags_border_width").num;
  45. /* Client / frame */
  46. t->client_n.fg = color_atoh(fetch_opt_first(ks[i], "#CCCCCC", "client_normal_fg").str);
  47. t->client_n.bg = color_atoh(fetch_opt_first(ks[i], "#222222", "client_normal_bg").str);
  48. t->client_s.fg = color_atoh(fetch_opt_first(ks[i], "#222222", "client_sel_fg").str);
  49. t->client_s.bg = color_atoh(fetch_opt_first(ks[i], "#CCCCCC", "client_sel_bg").str);
  50. t->frame_bg = color_atoh(fetch_opt_first(ks[i], "#555555", "frame_bg").str);
  51. t->client_titlebar_width = fetch_opt_first(ks[i], "12", "client_titlebar_width").num;
  52. t->client_border_width = fetch_opt_first(ks[i], "1", "client_border_width").num;
  53. /* insert_tail with SLIST */
  54. if(SLIST_EMPTY(&W->h.theme))
  55. SLIST_INSERT_HEAD(&W->h.theme, t, next);
  56. else
  57. SLIST_INSERT_AFTER(p, t, next);
  58. p = t;
  59. }
  60. free(ks);
  61. }
  62. static void
  63. config_bars(void)
  64. {
  65. struct screen *s;
  66. struct theme *t;
  67. size_t i, n;
  68. struct conf_sec *sec, **ks;
  69. int screenid;
  70. char *elem;
  71. Barpos pos = BarTop;
  72. /* [bars] */
  73. sec = fetch_section_first(NULL, "bars");
  74. ks = fetch_section(sec, "bar");
  75. n = fetch_section_count(ks);
  76. /* [bar] */
  77. for(i = 0; i < n; ++i)
  78. {
  79. elem = fetch_opt_first(ks[i], "", "elements").str;
  80. screenid = fetch_opt_first(ks[i], "-1", "screen").num;
  81. t = name_to_theme(fetch_opt_first(ks[i], "default", "theme").str);
  82. pos = fetch_opt_first(ks[i], "0", "position").num;
  83. SLIST_FOREACH(s, &W->h.screen, next)
  84. if(screenid == s->id || screenid == -1)
  85. infobar_new(s, t, pos, elem);
  86. }
  87. free(ks);
  88. }
  89. static void
  90. config_tag(void)
  91. {
  92. struct screen *s;
  93. struct tag *t;
  94. size_t i, n;
  95. struct conf_sec *sec, **ks;
  96. char *name;
  97. int screenid;
  98. /* [tags] */
  99. sec = fetch_section_first(NULL, "tags");
  100. ks = fetch_section(sec, "tag");
  101. n = fetch_section_count(ks);
  102. /* [tag] */
  103. for(i = 0; i < n; ++i)
  104. {
  105. name = fetch_opt_first(ks[i], "tag", "name").str;
  106. screenid = fetch_opt_first(ks[i], "-1", "screen").num;
  107. SLIST_FOREACH(s, &W->h.screen, next)
  108. if(screenid == s->id || screenid == -1)
  109. {
  110. t = tag_new(s, name);
  111. /* Set first tag as seltag */
  112. if(t == TAILQ_FIRST(&s->tags))
  113. s->seltag = t;
  114. }
  115. }
  116. free(ks);
  117. }
  118. static void
  119. config_keybind(void)
  120. {
  121. int i;
  122. size_t j, n;
  123. struct conf_sec *sec, **ks;
  124. struct opt_type *opt;
  125. char *cmd;
  126. struct keybind *k;
  127. /* [keys] */
  128. sec = fetch_section_first(NULL, "keys");
  129. ks = fetch_section(sec, "key");
  130. n = fetch_section_count(ks);
  131. SLIST_INIT(&W->h.keybind);
  132. /* [key] */
  133. for(i = 0; i < n; ++i)
  134. {
  135. k = (struct keybind*)xcalloc(1, sizeof(struct keybind));
  136. /* mod = {} */
  137. opt = fetch_opt(ks[i], "", "mod");
  138. for(j = k->mod = 0; j < fetch_opt_count(opt); ++j)
  139. k->mod |= modkey_keysym(opt[j].str);
  140. free(opt);
  141. /* key = */
  142. k->keysym = XStringToKeysym(fetch_opt_first(ks[i], "None", "key").str);
  143. /* func = */
  144. if(!(k->func = uicb_name_func(fetch_opt_first(ks[i], "", "func").str)))
  145. {
  146. warnx("configuration: Unknown Function \"%s\".",
  147. fetch_opt_first(ks[i], "", "func").str);
  148. k->func = uicb_spawn;
  149. }
  150. /* cmd = */
  151. if((cmd = fetch_opt_first(ks[i], "", "cmd").str))
  152. k->cmd = xstrdup(cmd);
  153. SLIST_INSERT_HEAD(&W->h.keybind, k, next);
  154. }
  155. wmfs_grab_keys();
  156. free(ks);
  157. }
  158. void
  159. config_init(void)
  160. {
  161. char *path;
  162. xasprintf(&path, "%s/"CONFIG_DEFAULT_PATH, getenv("HOME"));
  163. if(get_conf(path) == -1)
  164. errx(1, "parsing configuration file (%s) failed.", path);
  165. config_theme();
  166. config_keybind();
  167. config_tag();
  168. config_bars();
  169. free(path);
  170. free_conf();
  171. }