]> git.sesse.net Git - ccbs/blob - bigscreen/theme.cpp
Fix so that chosen score is not in the wrong class.
[ccbs] / bigscreen / theme.cpp
1 #include <cstdio>
2 #include <cstring>
3 #include <cstdlib>
4 #include <map>
5 #include <string>
6 #include <stdexcept>
7
8 std::map<std::string, std::string> config;
9
10 void read_config(const char *filename)
11 {
12         FILE *fp = fopen(filename, "r");
13         if (fp == NULL)
14                 throw std::runtime_error("Couldn't open theme file.");
15
16         while (!feof(fp)) {
17                 char buf[1024];
18                 if (fgets(buf, 1024, fp) == NULL) {
19                         break;
20                 }
21
22                 char *ptr = strchr(buf, '\n');
23                 if (ptr != NULL)
24                         *ptr = 0;
25
26                 ptr = strchr(buf, '\r');
27                 if (ptr != NULL)
28                         *ptr = 0;
29
30                 ptr = strchr(buf, '#');
31                 if (ptr != NULL)
32                         *ptr = 0;
33
34                 int len = strlen(buf);
35                 while (len > 0 && (buf[len - 1] == ' ' || buf[len - 1] == '\t')) {
36                         buf[--len] = 0;
37                 }
38                         
39                 if (len == 0) {
40                         continue;
41                 }
42
43                 char key[1024], value[1024];
44                 if (sscanf(buf, "%[^=] = %[^=]", key, value) != 2) {
45                         fprintf(stderr, "Warning: Ignoring malformed line '%s'.\n", buf);
46                         continue;
47                 }
48
49                 config[key] = value;
50         }
51         fclose(fp);
52 }
53
54 void init_theme()
55 {
56         read_config("theme.config");
57         try {
58                 read_config("theme.config.local");
59         } catch (...) {
60                 // Ignore.
61         }
62 }
63
64 std::string get_theme_config(const std::string &key, const std::string subkey)
65 {
66         if (config.count(key + "." + subkey)) {
67                 return config[key + "." + subkey];
68         }
69
70         std::string modkey = key;
71         while (modkey.find_last_of(".") != std::string::npos) {
72                 modkey.resize(modkey.find_last_of("."));
73                 if (config.count(modkey + "." + subkey)) {
74                         return config[modkey + "." + subkey];
75                 }
76         }
77
78         return config["default." + subkey];
79 }
80
81 void fill_background(unsigned char *buf, unsigned width, unsigned height)
82 {
83         int bg_r = atoi(get_theme_config("background", "red").c_str());
84         int bg_g = atoi(get_theme_config("background", "green").c_str());
85         int bg_b = atoi(get_theme_config("background", "blue").c_str());
86
87         unsigned char *ptr = buf;
88         for (unsigned i = 0; i < width * height; ++i) {
89                 *ptr++ = bg_b;
90                 *ptr++ = bg_g;
91                 *ptr++ = bg_r;
92                 *ptr++ = 0;
93         }
94 }