]> git.sesse.net Git - ccbs/blobdiff - bigscreen/theme.cpp
Support different backgrounds per-screen.
[ccbs] / bigscreen / theme.cpp
index 008c37fbffa4303dfdd9d43837c568ad7c125a9a..fb6f7707b450de93bedf9bc97cc91ad905dc0e19 100644 (file)
@@ -7,11 +7,11 @@
 
 std::map<std::string, std::string> config;
 
-void init_theme()
+void read_config(const char *filename)
 {
-       FILE *fp = fopen("theme.config", "rb");
+       FILE *fp = fopen(filename, "r");
        if (fp == NULL)
-               throw std::runtime_error("Couldn't open theme.config.");
+               throw std::runtime_error("Couldn't open theme file.");
 
        while (!feof(fp)) {
                char buf[1024];
@@ -46,31 +46,50 @@ void init_theme()
                        continue;
                }
 
-               config.insert(std::make_pair(key, value));
+               config[key] = value;
        }
        fclose(fp);
 }
 
+void init_theme()
+{
+       read_config("theme.config");
+       try {
+               read_config("theme.config.local");
+       } catch (...) {
+               // Ignore.
+       }
+}
+
 std::string get_theme_config(const std::string &key, const std::string subkey)
 {
        if (config.count(key + "." + subkey)) {
                return config[key + "." + subkey];
        }
 
+       std::string modkey = key;
+       while (modkey.find_last_of(".") != std::string::npos) {
+               modkey.resize(modkey.find_last_of("."));
+               if (config.count(modkey + "." + subkey)) {
+                       return config[modkey + "." + subkey];
+               }
+       }
+
        return config["default." + subkey];
 }
 
-void fill_background(unsigned char *buf, unsigned width, unsigned height)
+void fill_background(unsigned char *buf, const std::string &screen_name, 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());
+       std::string key = "background." + screen_name;
+       int bg_r = atoi(get_theme_config(key, "red").c_str());
+       int bg_g = atoi(get_theme_config(key, "green").c_str());
+       int bg_b = atoi(get_theme_config(key, "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++ = bg_g;
+               *ptr++ = bg_r;
                *ptr++ = 0;
        }
 }