main: eliminate local dummy variable
[ncmpc.git] / src / colors.c
blob0e78cadc83f37fc25eadc734d91dcfc882cf0492
1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2010 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 #include "ncfix.h"
24 #ifdef ENABLE_COLORS
25 #include "options.h"
26 #endif
28 #include <assert.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <glib.h>
34 #define COLOR_NONE G_MININT /* left most bit only */
35 #define COLOR_ERROR -2
37 #ifdef ENABLE_COLORS
38 typedef struct {
39 short color;
40 short r,g,b;
41 } color_definition_entry_t;
42 #endif
44 typedef struct {
45 const char *name;
46 int color;
47 int mono;
48 } color_entry_t;
50 static color_entry_t colors[COLOR_END] = {
51 /* color pair = field name, color, mono */
52 [COLOR_TITLE] = {"title", COLOR_YELLOW, A_NORMAL},
53 [COLOR_TITLE_BOLD] = {"title-bold", COLOR_YELLOW | A_BOLD, A_BOLD },
54 [COLOR_LINE] = {"line", COLOR_WHITE, A_NORMAL},
55 [COLOR_LINE_BOLD] = {"line-bold", COLOR_WHITE | A_BOLD, A_BOLD },
56 [COLOR_LINE_FLAGS] = {"line-flags", COLOR_YELLOW, A_NORMAL},
57 [COLOR_LIST] = {"list", COLOR_GREEN, A_NORMAL},
58 [COLOR_LIST_BOLD] = {"list-bold", COLOR_GREEN | A_BOLD, A_BOLD },
59 [COLOR_PROGRESSBAR] = {"progressbar", COLOR_WHITE, A_NORMAL},
60 [COLOR_STATUS] = {"status-song", COLOR_YELLOW, A_NORMAL},
61 [COLOR_STATUS_BOLD] = {"status-state", COLOR_YELLOW | A_BOLD, A_BOLD },
62 [COLOR_STATUS_TIME] = {"status-time", COLOR_RED, A_NORMAL},
63 [COLOR_STATUS_ALERT] = {"alert", COLOR_RED | A_BOLD, A_BOLD },
64 [COLOR_DIRECTORY] = {"browser-directory", COLOR_YELLOW, A_NORMAL},
65 [COLOR_PLAYLIST] = {"browser-playlist", COLOR_RED, A_NORMAL},
66 [COLOR_BACKGROUND] = {"background", COLOR_BLACK, A_NORMAL},
69 #ifdef ENABLE_COLORS
71 static GList *color_definition_list = NULL;
73 static color_entry_t *
74 colors_lookup_by_name(const char *name)
76 for (enum color i = 1; i < COLOR_END; ++i)
77 if (!strcasecmp(colors[i].name, name))
78 return &colors[i];
80 return NULL;
83 static int
84 colors_update_pair(enum color id)
86 assert(id > 0 && id < COLOR_END);
88 int fg = colors[id].color;
89 int bg = colors[COLOR_BACKGROUND].color;
91 /* If color == COLOR_NONE (negative),
92 * pass -1 to avoid cast errors */
93 init_pair(id,
94 (fg < 0 ? -1 : fg),
95 (bg < 0 ? -1 : bg));
96 return 0;
99 int
100 colors_str2color(const char *str)
102 int color = 0;
103 char **parts = g_strsplit(str, ",", 0);
104 for (int i = 0; parts[i]; i++) {
105 char *cur = parts[i];
107 /* Legacy colors (brightblue,etc) */
108 if (!strncasecmp(cur, "bright", 6)) {
109 color |= A_BOLD;
110 cur += 6;
113 /* Colors */
114 if (!strcasecmp(cur, "none"))
115 color |= COLOR_NONE;
116 else if (!strcasecmp(cur, "black"))
117 color |= COLOR_BLACK;
118 else if (!strcasecmp(cur, "red"))
119 color |= COLOR_RED;
120 else if (!strcasecmp(cur, "green"))
121 color |= COLOR_GREEN;
122 else if (!strcasecmp(cur, "yellow"))
123 color |= COLOR_YELLOW;
124 else if (!strcasecmp(cur, "blue"))
125 color |= COLOR_BLUE;
126 else if (!strcasecmp(cur, "magenta"))
127 color |= COLOR_MAGENTA;
128 else if (!strcasecmp(cur, "cyan"))
129 color |= COLOR_CYAN;
130 else if (!strcasecmp(cur, "white"))
131 color |= COLOR_WHITE;
132 else if (!strcasecmp(cur, "grey") || !strcasecmp(cur, "gray"))
133 color |= COLOR_BLACK | A_BOLD;
135 /* Attributes */
136 else if (!strcasecmp(cur, "standout"))
137 color |= A_STANDOUT;
138 else if (!strcasecmp(cur, "underline"))
139 color |= A_UNDERLINE;
140 else if (!strcasecmp(cur, "reverse"))
141 color |= A_REVERSE;
142 else if (!strcasecmp(cur, "blink"))
143 color |= A_BLINK;
144 else if (!strcasecmp(cur, "dim"))
145 color |= A_DIM;
146 else if (!strcasecmp(cur, "bold"))
147 color |= A_BOLD;
148 else {
149 /* Numerical colors */
150 char *endptr;
151 int tmp = strtol(cur, &endptr, 10);
152 if (cur != endptr && endptr[0] == '\0') {
153 color |= tmp;
154 } else {
155 fprintf(stderr,_("Warning: Unknown color - %s\n"), str);
156 return COLOR_ERROR;
161 g_strfreev(parts);
162 return color;
165 /* This function is called from conf.c before curses have been started,
166 * it adds the definition to the color_definition_list and init_color() is
167 * done in colors_start() */
169 colors_define(const char *name, short r, short g, short b)
171 int color = colors_str2color(name);
173 if (color < 0)
174 return color;
176 color_definition_entry_t *entry =
177 g_malloc(sizeof(color_definition_entry_t));
178 entry->color = color;
179 entry->r = r;
180 entry->g = g;
181 entry->b = b;
183 color_definition_list = g_list_append(color_definition_list, entry);
185 return 0;
189 colors_assign(const char *name, const char *value)
191 color_entry_t *entry = colors_lookup_by_name(name);
193 if (!entry) {
194 fprintf(stderr,_("Warning: Unknown color field - %s\n"), name);
195 return -1;
198 const int color = colors_str2color(value);
199 if (color == COLOR_ERROR)
200 return -1;
202 entry->color = color;
203 return 0;
208 colors_start(void)
210 if (has_colors()) {
211 /* initialize color support */
212 start_color();
213 use_default_colors();
214 /* define any custom colors defined in the configuration file */
215 if (color_definition_list && can_change_color()) {
216 GList *list = color_definition_list;
218 while (list) {
219 color_definition_entry_t *entry = list->data;
221 if (entry->color <= COLORS)
222 init_color(entry->color, entry->r,
223 entry->g, entry->b);
224 list = list->next;
226 } else if (color_definition_list && !can_change_color())
227 fprintf(stderr, "%s\n",
228 _("Terminal lacks support for changing colors"));
230 if (options.enable_colors) {
231 for (enum color i = 1; i < COLOR_END; ++i)
232 /* update the color pairs */
233 colors_update_pair(i);
235 } else if (options.enable_colors) {
236 fprintf(stderr, "%s\n",
237 _("Terminal lacks color capabilities"));
238 options.enable_colors = 0;
241 /* free the color_definition_list */
242 if (color_definition_list) {
243 GList *list = color_definition_list;
245 while (list) {
246 g_free(list->data);
247 list=list->next;
250 g_list_free(color_definition_list);
251 color_definition_list = NULL;
254 return 0;
256 #endif
259 colors_use(WINDOW *w, enum color id)
261 color_entry_t *entry = &colors[id];
263 assert(id > 0 && id < COLOR_END);
265 attr_t attrs;
266 short pair;
267 fix_wattr_get(w, &attrs, &pair, NULL);
269 #ifdef ENABLE_COLORS
270 if (options.enable_colors) {
271 /* color mode */
272 if ((int)attrs != entry->color || (short)id != pair)
273 wattr_set(w, entry->color, id, NULL);
274 } else {
275 #endif
276 /* mono mode */
277 if ((int)attrs != entry->mono)
278 (void)wattrset(w, entry->mono);
279 #ifdef ENABLE_COLORS
281 #endif
283 return 0;