openbox/themerc.c (raw)
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 |
#include <glib.h> #ifdef HAVE_STRING_H # include <string.h> #endif char *themerc_engine; char *themerc_theme; char *themerc_font; char *themerc_titlebar_layout; GError *error; static void parse(char *path, int fd) { GScanner *scanner; scanner = g_scanner_new(NULL); g_scanner_input_file(scanner, fd); while (g_scanner_get_next_token(scanner) != G_TOKEN_EOF) { char *name, *val; if (scanner->token != G_TOKEN_IDENTIFIER) { g_scanner_unexp_token(scanner, scanner->token, NULL, NULL, NULL, NULL, TRUE); return; } name = g_strdup(scanner->value.v_identifier); g_scanner_get_next_token(scanner); if (scanner->token != G_TOKEN_EQUAL_SIGN) { g_scanner_unexp_token(scanner, scanner->token, NULL, NULL, NULL, NULL, TRUE); g_free(name); return; } g_scanner_get_next_token(scanner); if (scanner->token == G_TOKEN_STRING) { val = g_strdup(scanner->value.v_identifier); if (!g_ascii_strcasecmp(name, "engine")) { if (themerc_engine != NULL) { g_warning("%s:%d: '%s' already defined", path, scanner->line, name); g_free(name); g_free(val); } else themerc_engine = val; } else if (!g_ascii_strcasecmp(name, "theme")) { if (themerc_theme != NULL) { g_warning("%s:%d: '%s' already defined", path, scanner->line, name); g_free(name); g_free(val); } else themerc_theme = val; } else if (!g_ascii_strcasecmp(name, "font")) { if (themerc_font != NULL) { g_warning("%s:%d: '%s' already defined", path, scanner->line, name); g_free(name); g_free(val); } else themerc_font = val; } else if (!g_ascii_strcasecmp(name, "titlebarlayout")) { if (themerc_titlebar_layout != NULL) { g_warning("%s:%d: '%s' already defined", path, scanner->line, name); g_free(name); g_free(val); } else { char *lowval = g_ascii_strup(val, -1); int i, len = strlen(lowval); g_free(val); for (i = 0; i < len; ++i) { gboolean valid = FALSE; switch(lowval[i]) { case 'I': case 'L': case 'M': case 'C': case 'N': case 'D': valid = TRUE; } if (!valid) { g_warning("%s:%d: invalid titlebarlayout element " "'%c'", path, scanner->line, lowval[i]); break; } } if (i == len) themerc_titlebar_layout = lowval; else { g_free(name); g_free(val); } } } else { g_warning("%s:%d: invalid option '%s'", path, scanner->line, name); g_free(name); g_free(val); } } else { g_scanner_unexp_token(scanner, scanner->token, NULL, NULL, NULL, NULL, TRUE); g_free(name); return; } } } void themerc_startup() { GIOChannel *chan = NULL; char *path = NULL; /* defaults */ themerc_engine = NULL; themerc_theme = NULL; themerc_font = NULL; themerc_titlebar_layout = NULL; path = g_build_filename(g_get_home_dir(), ".openbox", "themerc", NULL); error = NULL; chan = g_io_channel_new_file(path, "r", &error); if (chan == NULL) { g_free(path); path = g_build_filename(THEMERCDIR, "themerc", NULL); error = NULL; chan = g_io_channel_new_file(path, "r", &error); } if (chan != NULL) { parse(path, g_io_channel_unix_get_fd(chan)); g_free(path); g_io_channel_close(chan); } /* non-NULL defaults */ if (themerc_titlebar_layout == NULL) themerc_titlebar_layout = g_strdup("NDLIMC"); if (themerc_font == NULL) themerc_font = g_strdup("sans-7"); } void themerc_shutdown() { if (themerc_engine != NULL) g_free(themerc_engine); if (themerc_theme != NULL) g_free(themerc_theme); if (themerc_font != NULL) g_free(themerc_font); if (themerc_titlebar_layout != NULL) g_free(themerc_titlebar_layout); } |