]> git.sesse.net Git - ccbs/blobdiff - bigscreen/theme.cpp
Move the theming out to a runtime-read config file. Opens, among others, for differen...
[ccbs] / bigscreen / theme.cpp
diff --git a/bigscreen/theme.cpp b/bigscreen/theme.cpp
new file mode 100644 (file)
index 0000000..008c37f
--- /dev/null
@@ -0,0 +1,76 @@
+#include <cstdio>
+#include <cstring>
+#include <cstdlib>
+#include <map>
+#include <string>
+#include <stdexcept>
+
+std::map<std::string, std::string> config;
+
+void init_theme()
+{
+       FILE *fp = fopen("theme.config", "rb");
+       if (fp == NULL)
+               throw std::runtime_error("Couldn't open theme.config.");
+
+       while (!feof(fp)) {
+               char buf[1024];
+               if (fgets(buf, 1024, fp) == NULL) {
+                       break;
+               }
+
+               char *ptr = strchr(buf, '\n');
+               if (ptr != NULL)
+                       *ptr = 0;
+
+               ptr = strchr(buf, '\r');
+               if (ptr != NULL)
+                       *ptr = 0;
+
+               ptr = strchr(buf, '#');
+               if (ptr != NULL)
+                       *ptr = 0;
+
+               int len = strlen(buf);
+               while (len > 0 && (buf[len - 1] == ' ' || buf[len - 1] == '\t')) {
+                       buf[--len] = 0;
+               }
+                       
+               if (len == 0) {
+                       continue;
+               }
+
+               char key[1024], value[1024];
+               if (sscanf(buf, "%[^=] = %[^=]", key, value) != 2) {
+                       fprintf(stderr, "Warning: Ignoring malformed line '%s'.\n", buf);
+                       continue;
+               }
+
+               config.insert(std::make_pair(key, value));
+       }
+       fclose(fp);
+}
+
+std::string get_theme_config(const std::string &key, const std::string subkey)
+{
+       if (config.count(key + "." + subkey)) {
+               return config[key + "." + subkey];
+       }
+
+       return config["default." + subkey];
+}
+
+void fill_background(unsigned char *buf, unsigned width, unsigned height)
+{
+       int bg_r = atoi(get_theme_config("background", "red").c_str());
+       int bg_g = atoi(get_theme_config("background", "green").c_str());
+       int bg_b = atoi(get_theme_config("background", "blue").c_str());
+
+       unsigned char *ptr = buf;
+       for (unsigned i = 0; i < width * height; ++i) {
+               *ptr++ = bg_r;
+               *ptr++ = bg_g;
+               *ptr++ = bg_b;
+               *ptr++ = 0;
+       }
+}