75aef550bd4a9a5deb1d9cb4f11581efdde7138a
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.
18 */
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_BRIGHT_MASK (1<<8)
34 #define COLOR_BRIGHT_BLACK (COLOR_BLACK | COLOR_BRIGHT_MASK)
35 #define COLOR_BRIGHT_RED (COLOR_RED | COLOR_BRIGHT_MASK)
36 #define COLOR_BRIGHT_GREEN (COLOR_GREEN | COLOR_BRIGHT_MASK)
37 #define COLOR_BRIGHT_YELLOW (COLOR_YELLOW | COLOR_BRIGHT_MASK)
38 #define COLOR_BRIGHT_BLUE (COLOR_BLUE | COLOR_BRIGHT_MASK)
39 #define COLOR_BRIGHT_MAGENTA (COLOR_MAGENTA | COLOR_BRIGHT_MASK)
40 #define COLOR_BRIGHT_CYAN (COLOR_CYAN | COLOR_BRIGHT_MASK)
41 #define COLOR_BRIGHT_WHITE (COLOR_WHITE | COLOR_BRIGHT_MASK)
43 #define IS_BRIGHT(color) (color & COLOR_BRIGHT_MASK)
45 /* name of the color fields */
46 #define NAME_TITLE "title"
47 #define NAME_TITLE_BOLD "title-bold"
48 #define NAME_LINE "line"
49 #define NAME_LINE_BOLD "line-flags"
50 #define NAME_LIST "list"
51 #define NAME_LIST_BOLD "list-bold"
52 #define NAME_PROGRESS "progressbar"
53 #define NAME_STATUS "status-song"
54 #define NAME_STATUS_BOLD "status-state"
55 #define NAME_STATUS_TIME "status-time"
56 #define NAME_ALERT "alert"
57 #define NAME_BGCOLOR "background"
59 #ifdef ENABLE_COLORS
60 typedef struct {
61 short color;
62 short r,g,b;
63 } color_definition_entry_t;
64 #endif
66 typedef struct {
67 const char *name;
68 short fg;
69 attr_t attrs;
70 } color_entry_t;
72 static color_entry_t colors[COLOR_END] = {
73 /* color pair = field name, color, mono attribute */
74 [COLOR_TITLE] = { NAME_TITLE, COLOR_YELLOW, A_NORMAL },
75 [COLOR_TITLE_BOLD] = { NAME_TITLE_BOLD, COLOR_BRIGHT_YELLOW, A_BOLD },
76 [COLOR_LINE] = { NAME_LINE, COLOR_WHITE, A_NORMAL },
77 [COLOR_LINE_BOLD] = { NAME_LINE_BOLD, COLOR_BRIGHT_WHITE, A_BOLD },
78 [COLOR_LIST] = { NAME_LIST, COLOR_GREEN, A_NORMAL },
79 [COLOR_LIST_BOLD] = { NAME_LIST_BOLD, COLOR_BRIGHT_GREEN, A_BOLD },
80 [COLOR_PROGRESSBAR] = { NAME_PROGRESS, COLOR_WHITE, A_NORMAL },
81 [COLOR_STATUS] = { NAME_STATUS, COLOR_YELLOW, A_NORMAL },
82 [COLOR_STATUS_BOLD] = { NAME_STATUS_BOLD, COLOR_BRIGHT_YELLOW, A_BOLD },
83 [COLOR_STATUS_TIME] = { NAME_STATUS_TIME, COLOR_RED, A_NORMAL },
84 [COLOR_STATUS_ALERT] = { NAME_ALERT, COLOR_BRIGHT_RED, A_BOLD },
85 [COLOR_DIRECTORY] = {
86 .name = "browser-directory",
87 .fg = COLOR_YELLOW,
88 .attrs = A_NORMAL,
89 },
90 [COLOR_PLAYLIST] = {
91 .name = "browser-playlist",
92 .fg = COLOR_RED,
93 .attrs = A_NORMAL,
94 },
95 };
97 #ifdef ENABLE_COLORS
99 /* background color */
100 static short bg = COLOR_BLACK;
102 static GList *color_definition_list = NULL;
104 static color_entry_t *
105 colors_lookup_by_name(const char *name)
106 {
107 enum color i;
109 for (i = 1; i < COLOR_END; ++i)
110 if (!strcasecmp(colors[i].name, name))
111 return &colors[i];
113 return NULL;
114 }
116 static int
117 colors_update_pair(enum color id)
118 {
119 color_entry_t *entry = &colors[id];
120 short fg = -1;
122 assert(id > 0 && id < COLOR_END);
124 if (IS_BRIGHT(entry->fg)) {
125 entry->attrs = A_BOLD;
126 fg = entry->fg & ~COLOR_BRIGHT_MASK;
127 } else {
128 entry->attrs = A_NORMAL;
129 fg = entry->fg;
130 }
132 init_pair(id, fg, bg);
133 return 0;
134 }
136 short
137 colors_str2color(const char *str)
138 {
139 short color;
141 if (!strcasecmp(str, "black"))
142 return COLOR_BLACK;
143 else if (!strcasecmp(str, "red"))
144 return COLOR_RED;
145 else if (!strcasecmp(str, "green"))
146 return COLOR_GREEN;
147 else if (!strcasecmp(str, "yellow"))
148 return COLOR_YELLOW;
149 else if (!strcasecmp(str, "blue"))
150 return COLOR_BLUE;
151 else if (!strcasecmp(str, "magenta"))
152 return COLOR_MAGENTA;
153 else if (!strcasecmp(str, "cyan"))
154 return COLOR_CYAN;
155 else if (!strcasecmp(str, "white"))
156 return COLOR_WHITE;
157 else if (!strcasecmp(str, "brightred"))
158 return COLOR_BRIGHT_RED;
159 else if (!strcasecmp(str, "brightgreen"))
160 return COLOR_BRIGHT_GREEN;
161 else if (!strcasecmp(str, "brightyellow"))
162 return COLOR_BRIGHT_YELLOW;
163 else if (!strcasecmp(str, "brightblue"))
164 return COLOR_BRIGHT_BLUE;
165 else if (!strcasecmp(str, "brightmagenta"))
166 return COLOR_BRIGHT_MAGENTA;
167 else if (!strcasecmp(str, "brightcyan"))
168 return COLOR_BRIGHT_CYAN;
169 else if (!strcasecmp(str, "brightwhite"))
170 return COLOR_BRIGHT_WHITE;
171 else if (!strcasecmp(str, "grey") || !strcasecmp(str, "gray"))
172 return COLOR_BRIGHT_BLACK;
173 else if (!strcasecmp(str, "none"))
174 return -1;
176 if (!strcmp(str, "0"))
177 return 0;
178 color = atoi(str);
179 if (color > 0)
180 return color;
182 fprintf(stderr,_("Warning: Unknown color - %s\n"), str);
183 return -2;
184 }
186 /* This function is called from conf.c before curses have been started,
187 * it adds the definition to the color_definition_list and init_color() is
188 * done in colors_start() */
189 int
190 colors_define(const char *name, short r, short g, short b)
191 {
192 color_definition_entry_t *entry;
193 short color = colors_str2color(name);
195 if (color < 0)
196 return -1;
198 entry = g_malloc(sizeof(color_definition_entry_t));
199 entry->color = color;
200 entry->r = r;
201 entry->g = g;
202 entry->b = b;
204 color_definition_list = g_list_append(color_definition_list, entry);
206 return 0;
207 }
209 int
210 colors_assign(const char *name, const char *value)
211 {
212 color_entry_t *entry = colors_lookup_by_name(name);
213 short color;
215 if (!entry) {
216 if (!strcasecmp(NAME_BGCOLOR, name)) {
217 if ((color = colors_str2color(value)) < -1)
218 return -1;
219 if (color > COLORS)
220 color = color & ~COLOR_BRIGHT_MASK;
221 bg = color;
222 return 0;
223 }
225 fprintf(stderr,_("Warning: Unknown color field - %s\n"), name);
226 return -1;
227 }
229 if ((color = colors_str2color(value)) < -1)
230 return -1;
232 entry->fg = color;
233 return 0;
234 }
237 int
238 colors_start(void)
239 {
240 if (has_colors()) {
241 /* initialize color support */
242 start_color();
243 use_default_colors();
244 /* define any custom colors defined in the configuration file */
245 if (color_definition_list && can_change_color()) {
246 GList *list = color_definition_list;
248 while (list) {
249 color_definition_entry_t *entry = list->data;
251 if (entry->color <= COLORS)
252 init_color(entry->color, entry->r,
253 entry->g, entry->b);
254 list = list->next;
255 }
256 } else if (color_definition_list && !can_change_color())
257 fprintf(stderr, "%s\n",
258 _("Terminal lacks support for changing colors"));
260 if (options.enable_colors) {
261 enum color i;
263 for (i = 1; i < COLOR_END; ++i)
264 /* update the color pairs */
265 colors_update_pair(i);
266 }
267 } else if (options.enable_colors) {
268 fprintf(stderr, "%s\n",
269 _("Terminal lacks color capabilities"));
270 options.enable_colors = 0;
271 }
273 /* free the color_definition_list */
274 if (color_definition_list) {
275 GList *list = color_definition_list;
277 while (list) {
278 g_free(list->data);
279 list=list->next;
280 }
282 g_list_free(color_definition_list);
283 color_definition_list = NULL;
284 }
286 return 0;
287 }
288 #endif
290 int
291 colors_use(WINDOW *w, enum color id)
292 {
293 color_entry_t *entry = &colors[id];
294 short pair;
295 attr_t attrs;
297 assert(id > 0 && id < COLOR_END);
299 wattr_get(w, &attrs, &pair, NULL);
301 #ifdef ENABLE_COLORS
302 if (options.enable_colors) {
303 /* color mode */
304 if (attrs != entry->attrs || (short)id != pair)
305 wattr_set(w, entry->attrs, id, NULL);
306 } else {
307 #endif
308 /* mono mode */
309 if (attrs != entry->attrs)
310 (void)wattrset(w, entry->attrs);
311 #ifdef ENABLE_COLORS
312 }
313 #endif
315 return 0;
316 }