1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2010 The Music Player Daemon Project
3 * Project homepage: http://musicpd.org
4 *
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.
9 *
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.
14 *
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.
18 */
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_LIST] = {"list", COLOR_GREEN, A_NORMAL},
57 [COLOR_LIST_BOLD] = {"list-bold", COLOR_GREEN | A_BOLD, A_BOLD },
58 [COLOR_PROGRESSBAR] = {"progressbar", COLOR_WHITE, A_NORMAL},
59 [COLOR_STATUS] = {"status-song", COLOR_YELLOW, A_NORMAL},
60 [COLOR_STATUS_BOLD] = {"status-state", COLOR_YELLOW | A_BOLD, A_BOLD },
61 [COLOR_STATUS_TIME] = {"status-time", COLOR_RED, A_NORMAL},
62 [COLOR_STATUS_ALERT] = {"alert", COLOR_RED | A_BOLD, A_BOLD },
63 [COLOR_DIRECTORY] = {"browser-directory", COLOR_YELLOW, A_NORMAL},
64 [COLOR_PLAYLIST] = {"browser-playlist", COLOR_RED, A_NORMAL},
65 [COLOR_BACKGROUND] = {"background", COLOR_BLACK, A_NORMAL},
66 };
68 #ifdef ENABLE_COLORS
70 static GList *color_definition_list = NULL;
72 static color_entry_t *
73 colors_lookup_by_name(const char *name)
74 {
75 enum color i;
77 for (i = 1; i < COLOR_END; ++i)
78 if (!strcasecmp(colors[i].name, name))
79 return &colors[i];
81 return NULL;
82 }
84 static int
85 colors_update_pair(enum color id)
86 {
87 int fg, bg;
89 assert(id > 0 && id < COLOR_END);
91 fg = colors[id].color;
92 bg = colors[COLOR_BACKGROUND].color;
94 /* If color == COLOR_NONE (negative),
95 * pass -1 to avoid cast errors */
96 init_pair(id,
97 (fg < 0 ? -1 : fg),
98 (bg < 0 ? -1 : bg));
99 return 0;
100 }
102 int
103 colors_str2color(const char *str)
104 {
105 int i, color = 0;
106 char **parts = g_strsplit(str, ",", 0);
107 for (i = 0; parts[i]; i++) {
108 char *cur = parts[i];
110 /* Legacy colors (brightblue,etc) */
111 if (!strncasecmp(cur, "bright", 6)) {
112 color |= A_BOLD;
113 cur += 6;
114 }
116 /* Colors */
117 if (!strcasecmp(cur, "none"))
118 color |= COLOR_NONE;
119 else if (!strcasecmp(cur, "black"))
120 color |= COLOR_BLACK;
121 else if (!strcasecmp(cur, "red"))
122 color |= COLOR_RED;
123 else if (!strcasecmp(cur, "green"))
124 color |= COLOR_GREEN;
125 else if (!strcasecmp(cur, "yellow"))
126 color |= COLOR_YELLOW;
127 else if (!strcasecmp(cur, "blue"))
128 color |= COLOR_BLUE;
129 else if (!strcasecmp(cur, "magenta"))
130 color |= COLOR_MAGENTA;
131 else if (!strcasecmp(cur, "cyan"))
132 color |= COLOR_CYAN;
133 else if (!strcasecmp(cur, "white"))
134 color |= COLOR_WHITE;
135 else if (!strcasecmp(cur, "grey") || !strcasecmp(cur, "gray"))
136 color |= COLOR_BLACK | A_BOLD;
138 /* Attributes */
139 else if (!strcasecmp(cur, "standout"))
140 color |= A_STANDOUT;
141 else if (!strcasecmp(cur, "underline"))
142 color |= A_UNDERLINE;
143 else if (!strcasecmp(cur, "reverse"))
144 color |= A_REVERSE;
145 else if (!strcasecmp(cur, "blink"))
146 color |= A_BLINK;
147 else if (!strcasecmp(cur, "dim"))
148 color |= A_DIM;
149 else if (!strcasecmp(cur, "bold"))
150 color |= A_BOLD;
151 else {
152 /* Numerical colors */
153 char *endptr;
154 int tmp = strtol(cur, &endptr, 10);
155 if (cur != endptr && endptr[0] == '\0') {
156 color |= tmp;
157 } else {
158 fprintf(stderr,_("Warning: Unknown color - %s\n"), str);
159 return COLOR_ERROR;
160 }
161 }
163 }
164 g_strfreev(parts);
165 return color;
166 }
168 /* This function is called from conf.c before curses have been started,
169 * it adds the definition to the color_definition_list and init_color() is
170 * done in colors_start() */
171 int
172 colors_define(const char *name, short r, short g, short b)
173 {
174 color_definition_entry_t *entry;
175 int color = colors_str2color(name);
177 if (color < 0)
178 return color;
180 entry = g_malloc(sizeof(color_definition_entry_t));
181 entry->color = color;
182 entry->r = r;
183 entry->g = g;
184 entry->b = b;
186 color_definition_list = g_list_append(color_definition_list, entry);
188 return 0;
189 }
191 int
192 colors_assign(const char *name, const char *value)
193 {
194 color_entry_t *entry = colors_lookup_by_name(name);
195 int color;
197 if (!entry) {
198 fprintf(stderr,_("Warning: Unknown color field - %s\n"), name);
199 return -1;
200 }
202 if ((color = colors_str2color(value)) == COLOR_ERROR)
203 return -1;
205 entry->color = color;
206 return 0;
207 }
210 int
211 colors_start(void)
212 {
213 if (has_colors()) {
214 /* initialize color support */
215 start_color();
216 use_default_colors();
217 /* define any custom colors defined in the configuration file */
218 if (color_definition_list && can_change_color()) {
219 GList *list = color_definition_list;
221 while (list) {
222 color_definition_entry_t *entry = list->data;
224 if (entry->color <= COLORS)
225 init_color(entry->color, entry->r,
226 entry->g, entry->b);
227 list = list->next;
228 }
229 } else if (color_definition_list && !can_change_color())
230 fprintf(stderr, "%s\n",
231 _("Terminal lacks support for changing colors"));
233 if (options.enable_colors) {
234 enum color i;
236 for (i = 1; i < COLOR_END; ++i)
237 /* update the color pairs */
238 colors_update_pair(i);
239 }
240 } else if (options.enable_colors) {
241 fprintf(stderr, "%s\n",
242 _("Terminal lacks color capabilities"));
243 options.enable_colors = 0;
244 }
246 /* free the color_definition_list */
247 if (color_definition_list) {
248 GList *list = color_definition_list;
250 while (list) {
251 g_free(list->data);
252 list=list->next;
253 }
255 g_list_free(color_definition_list);
256 color_definition_list = NULL;
257 }
259 return 0;
260 }
261 #endif
263 int
264 colors_use(WINDOW *w, enum color id)
265 {
266 color_entry_t *entry = &colors[id];
267 short pair;
268 attr_t attrs;
270 assert(id > 0 && id < COLOR_END);
272 fix_wattr_get(w, &attrs, &pair, NULL);
274 #ifdef ENABLE_COLORS
275 if (options.enable_colors) {
276 /* color mode */
277 if ((int)attrs != entry->color || (short)id != pair)
278 wattr_set(w, entry->color, id, NULL);
279 } else {
280 #endif
281 /* mono mode */
282 if ((int)attrs != entry->mono)
283 (void)wattrset(w, entry->mono);
284 #ifdef ENABLE_COLORS
285 }
286 #endif
288 return 0;
289 }