10 std::map<std::string, std::string> config;
12 void read_config(const char *filename)
14 FILE *fp = fopen(filename, "r");
16 throw std::runtime_error("Couldn't open theme file.");
20 if (fgets(buf, 1024, fp) == NULL) {
24 char *ptr = strchr(buf, '\n');
28 ptr = strchr(buf, '\r');
32 ptr = strchr(buf, '#');
36 int len = strlen(buf);
37 while (len > 0 && (buf[len - 1] == ' ' || buf[len - 1] == '\t')) {
45 char key[1024], value[1024];
46 if (sscanf(buf, "%[^=] = %[^=]", key, value) != 2) {
47 fprintf(stderr, "Warning: Ignoring malformed line '%s'.\n", buf);
58 read_config("theme.config");
60 read_config("theme.config.local");
66 std::string get_theme_config(const std::string &key, const std::string subkey)
68 if (config.count(key + "." + subkey)) {
69 return config[key + "." + subkey];
72 std::string modkey = key;
73 while (modkey.find_last_of(".") != std::string::npos) {
74 modkey.resize(modkey.find_last_of("."));
75 if (config.count(modkey + "." + subkey)) {
76 return config[modkey + "." + subkey];
80 return config["default." + subkey];
83 void fill_background(unsigned char *buf, const std::string &screen_name, unsigned width, unsigned height)
85 std::string key = "background." + screen_name;
86 int bg_r = atoi(get_theme_config(key, "red").c_str());
87 int bg_g = atoi(get_theme_config(key, "green").c_str());
88 int bg_b = atoi(get_theme_config(key, "blue").c_str());
90 unsigned char *ptr = buf;
91 for (unsigned i = 0; i < width * height; ++i) {
98 for (unsigned i = 0; i < 10; ++i) {
100 snprintf(rectkey, sizeof(rectkey), "background.%s.rect%u", screen_name.c_str(), i);
101 if (get_theme_config(rectkey, "x0").empty()) {
105 int x0 = std::max<int>(atoi(get_theme_config(rectkey, "x0").c_str()) * width / LOGICAL_SCREEN_WIDTH, 0);
106 int y0 = std::max<int>(atoi(get_theme_config(rectkey, "y0").c_str()) * height / LOGICAL_SCREEN_HEIGHT, 0);
107 int x1 = std::min<int>(atoi(get_theme_config(rectkey, "x1").c_str()) * width / LOGICAL_SCREEN_WIDTH, width);
108 int y1 = std::min<int>(atoi(get_theme_config(rectkey, "y1").c_str()) * height / LOGICAL_SCREEN_HEIGHT, height);
109 int r = atoi(get_theme_config(rectkey, "red").c_str());
110 int g = atoi(get_theme_config(rectkey, "green").c_str());
111 int b = atoi(get_theme_config(rectkey, "blue").c_str());
113 for (int y = y0; y < y1; ++y) {
114 unsigned char *ptr = buf + (y * width + x0) * 4;
115 for (int x = x0; x < x1; ++x) {