a6c7e2cca9c665a741596483ac385bdf66a84a18
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<<7)
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 if (!strcasecmp(str, "black"))
140 return COLOR_BLACK;
141 else if (!strcasecmp(str, "red"))
142 return COLOR_RED;
143 else if (!strcasecmp(str, "green"))
144 return COLOR_GREEN;
145 else if (!strcasecmp(str, "yellow"))
146 return COLOR_YELLOW;
147 else if (!strcasecmp(str, "blue"))
148 return COLOR_BLUE;
149 else if (!strcasecmp(str, "magenta"))
150 return COLOR_MAGENTA;
151 else if (!strcasecmp(str, "cyan"))
152 return COLOR_CYAN;
153 else if (!strcasecmp(str, "white"))
154 return COLOR_WHITE;
155 else if (!strcasecmp(str, "brightred"))
156 return COLOR_BRIGHT_RED;
157 else if (!strcasecmp(str, "brightgreen"))
158 return COLOR_BRIGHT_GREEN;
159 else if (!strcasecmp(str, "brightyellow"))
160 return COLOR_BRIGHT_YELLOW;
161 else if (!strcasecmp(str, "brightblue"))
162 return COLOR_BRIGHT_BLUE;
163 else if (!strcasecmp(str, "brightmagenta"))
164 return COLOR_BRIGHT_MAGENTA;
165 else if (!strcasecmp(str, "brightcyan"))
166 return COLOR_BRIGHT_CYAN;
167 else if (!strcasecmp(str, "brightwhite"))
168 return COLOR_BRIGHT_WHITE;
169 else if (!strcasecmp(str, "grey") || !strcasecmp(str, "gray"))
170 return COLOR_BRIGHT_BLACK;
171 else if (!strcasecmp(str, "none"))
172 return -1;
174 fprintf(stderr,_("Warning: Unknown color - %s\n"), str);
175 return -2;
176 }
178 /* This function is called from conf.c before curses have been started,
179 * it adds the definition to the color_definition_list and init_color() is
180 * done in colors_start() */
181 int
182 colors_define(const char *name, short r, short g, short b)
183 {
184 color_definition_entry_t *entry;
185 short color = colors_str2color(name);
187 if (color < 0)
188 return -1;
190 entry = g_malloc(sizeof(color_definition_entry_t));
191 entry->color = color;
192 entry->r = r;
193 entry->g = g;
194 entry->b = b;
196 color_definition_list = g_list_append(color_definition_list, entry);
198 return 0;
199 }
201 int
202 colors_assign(const char *name, const char *value)
203 {
204 color_entry_t *entry = colors_lookup_by_name(name);
205 short color;
207 if (!entry) {
208 if (!strcasecmp(NAME_BGCOLOR, name)) {
209 if ((color = colors_str2color(value)) < -1)
210 return -1;
211 if (color > COLORS)
212 color = color & ~COLOR_BRIGHT_MASK;
213 bg = color;
214 return 0;
215 }
217 fprintf(stderr,_("Warning: Unknown color field - %s\n"), name);
218 return -1;
219 }
221 if ((color = colors_str2color(value)) < -1)
222 return -1;
224 entry->fg = color;
225 return 0;
226 }
229 int
230 colors_start(void)
231 {
232 if (has_colors()) {
233 /* initialize color support */
234 start_color();
235 use_default_colors();
236 /* define any custom colors defined in the configuration file */
237 if (color_definition_list && can_change_color()) {
238 GList *list = color_definition_list;
240 while (list) {
241 color_definition_entry_t *entry = list->data;
243 if (entry->color <= COLORS)
244 init_color(entry->color, entry->r,
245 entry->g, entry->b);
246 list = list->next;
247 }
248 } else if (color_definition_list && !can_change_color())
249 fprintf(stderr, "%s\n",
250 _("Terminal lacks support for changing colors"));
252 if (options.enable_colors) {
253 enum color i;
255 for (i = 1; i < COLOR_END; ++i)
256 /* update the color pairs */
257 colors_update_pair(i);
258 }
259 } else if (options.enable_colors) {
260 fprintf(stderr, "%s\n",
261 _("Terminal lacks color capabilities"));
262 options.enable_colors = 0;
263 }
265 /* free the color_definition_list */
266 if (color_definition_list) {
267 GList *list = color_definition_list;
269 while (list) {
270 g_free(list->data);
271 list=list->next;
272 }
274 g_list_free(color_definition_list);
275 color_definition_list = NULL;
276 }
278 return 0;
279 }
280 #endif
282 int
283 colors_use(WINDOW *w, enum color id)
284 {
285 color_entry_t *entry = &colors[id];
286 short pair;
287 attr_t attrs;
289 assert(id > 0 && id < COLOR_END);
291 wattr_get(w, &attrs, &pair, NULL);
293 #ifdef ENABLE_COLORS
294 if (options.enable_colors) {
295 /* color mode */
296 if (attrs != entry->attrs || (short)id != pair)
297 wattr_set(w, entry->attrs, id, NULL);
298 } else {
299 #endif
300 /* mono mode */
301 if (attrs != entry->attrs)
302 wattrset(w, entry->attrs);
303 #ifdef ENABLE_COLORS
304 }
305 #endif
307 return 0;
308 }