+
+void stack(void);
+
+#define SWM_CONF_WS "\n= \t"
+#define SWM_CONF_FILE "scrotwm.conf"
+int
+conf_load(char *filename)
+{
+ FILE *config;
+ char *line, *cp, *var, *val;
+ size_t len, lineno = 0;
+
+ DNPRINTF(SWM_D_MISC, "conf_load: filename %s\n", filename);
+
+ if (filename == NULL)
+ return (1);
+
+ if ((config = fopen(filename, "r")) == NULL)
+ return (1);
+
+ for (;;) {
+ if ((line = fparseln(config, &len, &lineno, NULL, 0)) == NULL)
+ if (feof(config))
+ break;
+ cp = line;
+ cp += (long)strspn(cp, SWM_CONF_WS);
+ if (cp[0] == '\0') {
+ /* empty line */
+ free(line);
+ continue;
+ }
+ if ((var = strsep(&cp, SWM_CONF_WS)) == NULL || cp == NULL)
+ break;
+ cp += (long)strspn(cp, SWM_CONF_WS);
+ if ((val = strsep(&cp, SWM_CONF_WS)) == NULL)
+ break;
+
+ DNPRINTF(SWM_D_MISC, "conf_load: %s=%s\n",var ,val);
+ switch (var[0]) {
+ case 'b':
+ if (!strncmp(var, "bar_enabled", strlen("bar_enabled")))
+ bar_enabled = atoi(val);
+ else if (!strncmp(var, "bar_border",
+ strlen("bar_border")))
+ bar_border = strtol(val, NULL, 16);
+ else if (!strncmp(var, "bar_color",
+ strlen("bar_color")))
+ bar_color = strtol(val, NULL, 16);
+ else if (!strncmp(var, "bar_font_color",
+ strlen("bar_font_color")))
+ bar_font_color = strtol(val, NULL, 16);
+ else if (!strncmp(var, "bar_font", strlen("bar_font")))
+ asprintf(&bar_fonts[0], "%s", val);
+ else
+ goto bad;
+ break;
+
+ case 'c':
+ if (!strncmp(var, "color_focus", strlen("color_focus")))
+ color_focus = strtol(val, NULL, 16);
+ else if (!strncmp(var, "color_unfocus",
+ strlen("color_unfocus")))
+ color_unfocus = strtol(val, NULL, 16);
+ else
+ goto bad;
+ break;
+
+ case 's':
+ if (!strncmp(var, "spawn_term", strlen("spawn_term")))
+ asprintf(&spawn_term[0], "%s", val); /* XXX args? */
+ break;
+ default:
+ goto bad;
+ }
+ free(line);
+ }
+
+ fclose(config);
+ return (0);
+bad:
+ errx(1, "invalid conf file entry: %s=%s", var, val);
+}
+
+void
+bar_print(void)
+{
+ time_t tmt;
+ struct tm tm;
+
+ if (bar_enabled == 0)
+ return;
+
+ /* clear old text */
+ XSetForeground(display, bar_gc, bar_color);
+ XDrawString(display, bar_window, bar_gc, 4, bar_fs->ascent, bar_text,
+ strlen(bar_text));
+
+ /* draw new text */
+ time(&tmt);
+ localtime_r(&tmt, &tm);
+ strftime(bar_text, sizeof bar_text, "%a %b %d %R %Z %Y", &tm);
+ XSetForeground(display, bar_gc, bar_font_color);
+ XDrawString(display, bar_window, bar_gc, 4, bar_fs->ascent, bar_text,
+ strlen(bar_text));
+ XSync(display, False);
+
+ alarm(60);
+}
+
+void
+bar_signal(int sig)
+{
+ /* XXX yeah yeah byte me */
+ bar_print();
+}
+
+void
+bar_toggle(union arg *args)
+{
+ int i;
+
+ DNPRINTF(SWM_D_MISC, "bar_toggle\n");
+
+ if (bar_enabled) {
+ bar_enabled = 0;
+ height += bar_height; /* correct screen height */
+ XUnmapWindow(display, bar_window);
+ } else {
+ bar_enabled = 1;
+ height -= bar_height; /* correct screen height */
+ XMapWindow(display, bar_window);
+ }
+ XSync(display, False);
+ for (i = 0; i < SWM_WS_MAX; i++)
+ ws[i].restack = 1;
+
+ stack();
+ bar_print(); /* must be after stack */
+}
+
+void
+bar_setup(void)
+{
+ int i;
+
+ for (i = 0; bar_fonts[i] != NULL; i++) {
+ bar_fs = XLoadQueryFont(display, bar_fonts[i]);
+ if (bar_fs)
+ break;
+ }
+ if (bar_fonts[i] == NULL)
+ errx(1, "couldn't load font");
+ bar_height = bar_fs->ascent + bar_fs->descent + 3;
+
+ bar_window = XCreateSimpleWindow(display, root, 0, 0, width,
+ bar_height - 2, 1, bar_border, bar_color);
+ bar_gc = XCreateGC(display, bar_window, 0, &bar_gcv);
+ XSetFont(display, bar_gc, bar_fs->fid);
+ XSelectInput(display, bar_window, VisibilityChangeMask);
+ if (bar_enabled) {
+ height -= bar_height; /* correct screen height */
+ XMapWindow(display, bar_window);
+ }
+ DNPRINTF(SWM_D_MISC, "bar_setup: bar_window %d\n", (int)bar_window);
+
+ if (signal(SIGALRM, bar_signal) == SIG_ERR)
+ err(1, "could not install bar_signal");
+ bar_print();
+}
+