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 };
87 #ifdef ENABLE_COLORS
89 /* background color */
90 static short bg = COLOR_BLACK;
92 static GList *color_definition_list = NULL;
94 static color_entry_t *
95 colors_lookup_by_name(const char *name)
96 {
97 enum color i;
99 for (i = 1; i < COLOR_END; ++i)
100 if (!strcasecmp(colors[i].name, name))
101 return &colors[i];
103 return NULL;
104 }
106 static int
107 colors_update_pair(enum color id)
108 {
109 color_entry_t *entry = &colors[id];
110 short fg = -1;
112 assert(id > 0 && id < COLOR_END);
114 if (IS_BRIGHT(entry->fg)) {
115 entry->attrs = A_BOLD;
116 fg = entry->fg & ~COLOR_BRIGHT_MASK;
117 } else {
118 entry->attrs = A_NORMAL;
119 fg = entry->fg;
120 }
122 init_pair(id, fg, bg);
123 return 0;
124 }
126 short
127 colors_str2color(const char *str)
128 {
129 if (!strcasecmp(str, "black"))
130 return COLOR_BLACK;
131 else if (!strcasecmp(str, "red"))
132 return COLOR_RED;
133 else if (!strcasecmp(str, "green"))
134 return COLOR_GREEN;
135 else if (!strcasecmp(str, "yellow"))
136 return COLOR_YELLOW;
137 else if (!strcasecmp(str, "blue"))
138 return COLOR_BLUE;
139 else if (!strcasecmp(str, "magenta"))
140 return COLOR_MAGENTA;
141 else if (!strcasecmp(str, "cyan"))
142 return COLOR_CYAN;
143 else if (!strcasecmp(str, "white"))
144 return COLOR_WHITE;
145 else if (!strcasecmp(str, "brightred"))
146 return COLOR_BRIGHT_RED;
147 else if (!strcasecmp(str, "brightgreen"))
148 return COLOR_BRIGHT_GREEN;
149 else if (!strcasecmp(str, "brightyellow"))
150 return COLOR_BRIGHT_YELLOW;
151 else if (!strcasecmp(str, "brightblue"))
152 return COLOR_BRIGHT_BLUE;
153 else if (!strcasecmp(str, "brightmagenta"))
154 return COLOR_BRIGHT_MAGENTA;
155 else if (!strcasecmp(str, "brightcyan"))
156 return COLOR_BRIGHT_CYAN;
157 else if (!strcasecmp(str, "brightwhite"))
158 return COLOR_BRIGHT_WHITE;
159 else if (!strcasecmp(str, "grey") || !strcasecmp(str, "gray"))
160 return COLOR_BRIGHT_BLACK;
161 else if (!strcasecmp(str, "none"))
162 return -1;
164 fprintf(stderr,_("Warning: Unknown color - %s\n"), str);
165 return -2;
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 short color = colors_str2color(name);
177 if (color < 0)
178 return -1;
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 short color;
197 if (!entry) {
198 if (!strcasecmp(NAME_BGCOLOR, name)) {
199 if ((color = colors_str2color(value)) < -1)
200 return -1;
201 if (color > COLORS)
202 color = color & ~COLOR_BRIGHT_MASK;
203 bg = color;
204 return 0;
205 }
207 fprintf(stderr,_("Warning: Unknown color field - %s\n"), name);
208 return -1;
209 }
211 if ((color = colors_str2color(value)) < -1)
212 return -1;
214 entry->fg = color;
215 return 0;
216 }
219 int
220 colors_start(void)
221 {
222 if (has_colors()) {
223 /* initialize color support */
224 start_color();
225 use_default_colors();
226 /* define any custom colors defined in the configuration file */
227 if (color_definition_list && can_change_color()) {
228 GList *list = color_definition_list;
230 while (list) {
231 color_definition_entry_t *entry = list->data;
233 if (entry->color <= COLORS)
234 init_color(entry->color, entry->r,
235 entry->g, entry->b);
236 list = list->next;
237 }
238 } else if (color_definition_list && !can_change_color())
239 fprintf(stderr, "%s\n",
240 _("Terminal lacks support for changing colors"));
242 if (options.enable_colors) {
243 enum color i;
245 for (i = 1; i < COLOR_END; ++i)
246 /* update the color pairs */
247 colors_update_pair(i);
248 }
249 } else if (options.enable_colors) {
250 fprintf(stderr, "%s\n",
251 _("Terminal lacks color capabilities"));
252 options.enable_colors = 0;
253 }
255 /* free the color_definition_list */
256 if (color_definition_list) {
257 GList *list = color_definition_list;
259 while (list) {
260 g_free(list->data);
261 list=list->next;
262 }
264 g_list_free(color_definition_list);
265 color_definition_list = NULL;
266 }
268 return 0;
269 }
270 #endif
272 int
273 colors_use(WINDOW *w, enum color id)
274 {
275 color_entry_t *entry = &colors[id];
276 short pair;
277 attr_t attrs;
279 assert(id > 0 && id < COLOR_END);
281 wattr_get(w, &attrs, &pair, NULL);
283 #ifdef ENABLE_COLORS
284 if (options.enable_colors) {
285 /* color mode */
286 if (attrs != entry->attrs || (short)id != pair)
287 wattr_set(w, entry->attrs, id, NULL);
288 } else {
289 #endif
290 /* mono mode */
291 if (attrs != entry->attrs)
292 wattrset(w, entry->attrs);
293 #ifdef ENABLE_COLORS
294 }
295 #endif
297 return 0;
298 }