colors: Change config to use attribute list
[ncmpc.git] / src / colors.c
blobdfaa84f1bdafce8ca8113ebde6994de66c1b7843
1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2009 The Music Player Daemon Project
3 * Project homepage: http://musicpd.org
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
20 #include "colors.h"
21 #include "i18n.h"
22 #ifdef ENABLE_COLORS
23 #include "options.h"
24 #endif
26 #include <assert.h>
27 #include <stdio.h>
28 #include <stdlib.h>
29 #include <string.h>
30 #include <glib.h>
32 #define COLOR_NONE G_MININT /* left most bit only */
33 #define COLOR_ERROR -2
35 #ifdef ENABLE_COLORS
36 typedef struct {
37 short color;
38 short r,g,b;
39 } color_definition_entry_t;
40 #endif
42 typedef struct {
43 const char *name;
44 int color;
45 int mono;
46 } color_entry_t;
48 static color_entry_t colors[COLOR_END] = {
49 /* color pair = field name, color, mono */
50 [COLOR_TITLE] = {"title", COLOR_YELLOW, A_NORMAL},
51 [COLOR_TITLE_BOLD] = {"title-bold", COLOR_YELLOW | A_BOLD, A_BOLD },
52 [COLOR_LINE] = {"line", COLOR_WHITE, A_NORMAL},
53 [COLOR_LINE_BOLD] = {"line-bold", COLOR_WHITE | A_BOLD, A_BOLD },
54 [COLOR_LIST] = {"list", COLOR_GREEN, A_NORMAL},
55 [COLOR_LIST_BOLD] = {"list-bold", COLOR_GREEN | A_BOLD, A_BOLD },
56 [COLOR_PROGRESSBAR] = {"progressbar", COLOR_WHITE, A_NORMAL},
57 [COLOR_STATUS] = {"status-song", COLOR_YELLOW, A_NORMAL},
58 [COLOR_STATUS_BOLD] = {"status-state", COLOR_YELLOW | A_BOLD, A_BOLD },
59 [COLOR_STATUS_TIME] = {"status-time", COLOR_RED, A_NORMAL},
60 [COLOR_STATUS_ALERT] = {"alert", COLOR_RED | A_BOLD, A_BOLD },
61 [COLOR_DIRECTORY] = {"browser-directory", COLOR_YELLOW, A_NORMAL},
62 [COLOR_PLAYLIST] = {"browser-playlist", COLOR_RED, A_NORMAL},
63 [COLOR_BACKGROUND] = {"background", COLOR_BLACK, A_NORMAL},
66 #ifdef ENABLE_COLORS
68 static GList *color_definition_list = NULL;
70 static color_entry_t *
71 colors_lookup_by_name(const char *name)
73 enum color i;
75 for (i = 1; i < COLOR_END; ++i)
76 if (!strcasecmp(colors[i].name, name))
77 return &colors[i];
79 return NULL;
82 static int
83 colors_update_pair(enum color id)
85 int fg, bg;
87 assert(id > 0 && id < COLOR_END);
89 fg = colors[id].color;
90 bg = colors[COLOR_BACKGROUND].color;
92 /* If color == COLOR_NONE (negative),
93 * pass -1 to avoid cast errors */
94 init_pair(id,
95 (fg < 0 ? -1 : fg),
96 (bg < 0 ? -1 : bg));
97 return 0;
101 colors_str2color(const char *str)
103 int i, color = 0;
104 char **parts = g_strsplit(str, ",", 0);
105 for (i = 0; parts[i]; i++) {
106 char *cur = parts[i];
108 /* Legacy colors (brightblue,etc) */
109 if (!strncasecmp(cur, "bright", 6)) {
110 color |= A_BOLD;
111 cur += 6;
114 /* Colors */
115 if (!strcasecmp(cur, "none"))
116 color |= COLOR_NONE;
117 else if (!strcasecmp(cur, "black"))
118 color |= COLOR_BLACK;
119 else if (!strcasecmp(cur, "red"))
120 color |= COLOR_RED;
121 else if (!strcasecmp(cur, "green"))
122 color |= COLOR_GREEN;
123 else if (!strcasecmp(cur, "yellow"))
124 color |= COLOR_YELLOW;
125 else if (!strcasecmp(cur, "blue"))
126 color |= COLOR_BLUE;
127 else if (!strcasecmp(cur, "magenta"))
128 color |= COLOR_MAGENTA;
129 else if (!strcasecmp(cur, "cyan"))
130 color |= COLOR_CYAN;
131 else if (!strcasecmp(cur, "white"))
132 color |= COLOR_WHITE;
133 else if (!strcasecmp(cur, "grey") || !strcasecmp(cur, "gray"))
134 color |= COLOR_BLACK | A_BOLD;
136 /* Attributes */
137 else if (!strcasecmp(cur, "standout"))
138 color |= A_STANDOUT;
139 else if (!strcasecmp(cur, "underline"))
140 color |= A_UNDERLINE;
141 else if (!strcasecmp(cur, "reverse"))
142 color |= A_REVERSE;
143 else if (!strcasecmp(cur, "blink"))
144 color |= A_BLINK;
145 else if (!strcasecmp(cur, "dim"))
146 color |= A_DIM;
147 else if (!strcasecmp(cur, "bold"))
148 color |= A_BOLD;
149 else {
150 /* Numerical colors */
151 char *endptr;
152 int tmp = strtol(cur, &endptr, 10);
153 if (cur != endptr && endptr[0] == '\0') {
154 color |= tmp;
155 } else {
156 fprintf(stderr,_("Warning: Unknown color - %s\n"), str);
157 return COLOR_ERROR;
162 g_strfreev(parts);
163 return color;
166 /* This function is called from conf.c before curses have been started,
167 * it adds the definition to the color_definition_list and init_color() is
168 * done in colors_start() */
170 colors_define(const char *name, short r, short g, short b)
172 color_definition_entry_t *entry;
173 int color = colors_str2color(name);
175 if (color < 0)
176 return color;
178 entry = g_malloc(sizeof(color_definition_entry_t));
179 entry->color = color;
180 entry->r = r;
181 entry->g = g;
182 entry->b = b;
184 color_definition_list = g_list_append(color_definition_list, entry);
186 return 0;
190 colors_assign(const char *name, const char *value)
192 color_entry_t *entry = colors_lookup_by_name(name);
193 int color;
195 if (!entry) {
196 fprintf(stderr,_("Warning: Unknown color field - %s\n"), name);
197 return -1;
200 if ((color = colors_str2color(value)) == COLOR_ERROR)
201 return -1;
203 entry->color = color;
204 return 0;
209 colors_start(void)
211 if (has_colors()) {
212 /* initialize color support */
213 start_color();
214 use_default_colors();
215 /* define any custom colors defined in the configuration file */
216 if (color_definition_list && can_change_color()) {
217 GList *list = color_definition_list;
219 while (list) {
220 color_definition_entry_t *entry = list->data;
222 if (entry->color <= COLORS)
223 init_color(entry->color, entry->r,
224 entry->g, entry->b);
225 list = list->next;
227 } else if (color_definition_list && !can_change_color())
228 fprintf(stderr, "%s\n",
229 _("Terminal lacks support for changing colors"));
231 if (options.enable_colors) {
232 enum color i;
234 for (i = 1; i < COLOR_END; ++i)
235 /* update the color pairs */
236 colors_update_pair(i);
238 } else if (options.enable_colors) {
239 fprintf(stderr, "%s\n",
240 _("Terminal lacks color capabilities"));
241 options.enable_colors = 0;
244 /* free the color_definition_list */
245 if (color_definition_list) {
246 GList *list = color_definition_list;
248 while (list) {
249 g_free(list->data);
250 list=list->next;
253 g_list_free(color_definition_list);
254 color_definition_list = NULL;
257 return 0;
259 #endif
262 colors_use(WINDOW *w, enum color id)
264 color_entry_t *entry = &colors[id];
265 short pair;
266 attr_t attrs;
268 assert(id > 0 && id < COLOR_END);
270 wattr_get(w, &attrs, &pair, NULL);
272 #ifdef ENABLE_COLORS
273 if (options.enable_colors) {
274 /* color mode */
275 if ((int)attrs != entry->color || (short)id != pair)
276 wattr_set(w, entry->color, id, NULL);
277 } else {
278 #endif
279 /* mono mode */
280 if ((int)attrs != entry->mono)
281 (void)wattrset(w, entry->mono);
282 #ifdef ENABLE_COLORS
284 #endif
286 return 0;