2635329ec00ca40c6abc81dec7f34ea98993b2a9
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_NONE -1
33 #define COLOR_ERROR -2
35 #ifdef ENABLE_COLORS
36 typedef struct {
37 short color;
38 short r,g,b;
39 } color_definition_entry_t;
40 #endif
42 typedef struct {
43 const char *name;
44 int color;
45 int mono;
46 } color_entry_t;
48 static color_entry_t colors[COLOR_END] = {
49 /* color pair = field name, color, mono */
50 [COLOR_TITLE] = {"title", COLOR_YELLOW, A_NORMAL},
51 [COLOR_TITLE_BOLD] = {"title-bold", COLOR_YELLOW | A_BOLD, A_BOLD },
52 [COLOR_LINE] = {"line", COLOR_WHITE, A_NORMAL},
53 [COLOR_LINE_BOLD] = {"line-bold", COLOR_WHITE | A_BOLD, A_BOLD },
54 [COLOR_LIST] = {"list", COLOR_GREEN, A_NORMAL},
55 [COLOR_LIST_BOLD] = {"list-bold", COLOR_GREEN | A_BOLD, A_BOLD },
56 [COLOR_PROGRESSBAR] = {"progressbar", COLOR_WHITE, A_NORMAL},
57 [COLOR_STATUS] = {"status-song", COLOR_YELLOW, A_NORMAL},
58 [COLOR_STATUS_BOLD] = {"status-state", COLOR_YELLOW | A_BOLD, A_BOLD },
59 [COLOR_STATUS_TIME] = {"status-time", COLOR_RED, A_NORMAL},
60 [COLOR_STATUS_ALERT] = {"alert", COLOR_RED | A_BOLD, A_BOLD },
61 [COLOR_DIRECTORY] = {"browser-directory", COLOR_YELLOW, A_NORMAL},
62 [COLOR_PLAYLIST] = {"browser-playlist", COLOR_RED, A_NORMAL},
63 [COLOR_BACKGROUND] = {"background", COLOR_BLACK, A_NORMAL},
64 };
66 #ifdef ENABLE_COLORS
68 static GList *color_definition_list = NULL;
70 static color_entry_t *
71 colors_lookup_by_name(const char *name)
72 {
73 enum color i;
75 for (i = 1; i < COLOR_END; ++i)
76 if (!strcasecmp(colors[i].name, name))
77 return &colors[i];
79 return NULL;
80 }
82 static int
83 colors_update_pair(enum color id)
84 {
85 short fg, bg;
87 assert(id > 0 && id < COLOR_END);
89 fg = colors[id].color;
90 bg = colors[COLOR_BACKGROUND].color;
92 /* COLOR_NONE is negative, which
93 * results in a default colors */
94 init_pair(id, fg, bg);
95 return 0;
96 }
98 int
99 colors_str2color(const char *str)
100 {
101 int color;
102 char *endptr;
104 if (!strcasecmp(str, "black"))
105 return COLOR_BLACK;
106 else if (!strcasecmp(str, "red"))
107 return COLOR_RED;
108 else if (!strcasecmp(str, "green"))
109 return COLOR_GREEN;
110 else if (!strcasecmp(str, "yellow"))
111 return COLOR_YELLOW;
112 else if (!strcasecmp(str, "blue"))
113 return COLOR_BLUE;
114 else if (!strcasecmp(str, "magenta"))
115 return COLOR_MAGENTA;
116 else if (!strcasecmp(str, "cyan"))
117 return COLOR_CYAN;
118 else if (!strcasecmp(str, "white"))
119 return COLOR_WHITE;
120 else if (!strcasecmp(str, "brightred"))
121 return COLOR_RED | A_BOLD;
122 else if (!strcasecmp(str, "brightgreen"))
123 return COLOR_GREEN | A_BOLD;
124 else if (!strcasecmp(str, "brightyellow"))
125 return COLOR_YELLOW | A_BOLD;
126 else if (!strcasecmp(str, "brightblue"))
127 return COLOR_BLUE | A_BOLD;
128 else if (!strcasecmp(str, "brightmagenta"))
129 return COLOR_MAGENTA | A_BOLD;
130 else if (!strcasecmp(str, "brightcyan"))
131 return COLOR_CYAN | A_BOLD;
132 else if (!strcasecmp(str, "brightwhite"))
133 return COLOR_WHITE | A_BOLD;
134 else if (!strcasecmp(str, "grey") || !strcasecmp(str, "gray"))
135 return COLOR_BLACK | A_BOLD;
136 else if (!strcasecmp(str, "none"))
137 return COLOR_NONE;
139 color = strtol(str, &endptr, 10);
140 if (str != endptr && endptr[0] == '\0')
141 return color;
143 fprintf(stderr,_("Warning: Unknown color - %s\n"), str);
144 return -2;
145 }
147 /* This function is called from conf.c before curses have been started,
148 * it adds the definition to the color_definition_list and init_color() is
149 * done in colors_start() */
150 int
151 colors_define(const char *name, short r, short g, short b)
152 {
153 color_definition_entry_t *entry;
154 int color = colors_str2color(name);
156 if (color < 0)
157 return color;
159 entry = g_malloc(sizeof(color_definition_entry_t));
160 entry->color = color;
161 entry->r = r;
162 entry->g = g;
163 entry->b = b;
165 color_definition_list = g_list_append(color_definition_list, entry);
167 return 0;
168 }
170 int
171 colors_assign(const char *name, const char *value)
172 {
173 color_entry_t *entry = colors_lookup_by_name(name);
174 int color;
176 if (!entry) {
177 fprintf(stderr,_("Warning: Unknown color field - %s\n"), name);
178 return -1;
179 }
181 if ((color = colors_str2color(value)) == COLOR_ERROR)
182 return -1;
184 entry->color = color;
185 return 0;
186 }
189 int
190 colors_start(void)
191 {
192 if (has_colors()) {
193 /* initialize color support */
194 start_color();
195 use_default_colors();
196 /* define any custom colors defined in the configuration file */
197 if (color_definition_list && can_change_color()) {
198 GList *list = color_definition_list;
200 while (list) {
201 color_definition_entry_t *entry = list->data;
203 if (entry->color <= COLORS)
204 init_color(entry->color, entry->r,
205 entry->g, entry->b);
206 list = list->next;
207 }
208 } else if (color_definition_list && !can_change_color())
209 fprintf(stderr, "%s\n",
210 _("Terminal lacks support for changing colors"));
212 if (options.enable_colors) {
213 enum color i;
215 for (i = 1; i < COLOR_END; ++i)
216 /* update the color pairs */
217 colors_update_pair(i);
218 }
219 } else if (options.enable_colors) {
220 fprintf(stderr, "%s\n",
221 _("Terminal lacks color capabilities"));
222 options.enable_colors = 0;
223 }
225 /* free the color_definition_list */
226 if (color_definition_list) {
227 GList *list = color_definition_list;
229 while (list) {
230 g_free(list->data);
231 list=list->next;
232 }
234 g_list_free(color_definition_list);
235 color_definition_list = NULL;
236 }
238 return 0;
239 }
240 #endif
242 int
243 colors_use(WINDOW *w, enum color id)
244 {
245 color_entry_t *entry = &colors[id];
246 short pair;
247 attr_t attrs;
249 assert(id > 0 && id < COLOR_END);
251 wattr_get(w, &attrs, &pair, NULL);
253 #ifdef ENABLE_COLORS
254 if (options.enable_colors) {
255 /* color mode */
256 if ((int)attrs != entry->mono || (short)id != pair)
257 wattr_set(w, entry->mono, id, NULL);
258 } else {
259 #endif
260 /* mono mode */
261 if ((int)attrs != entry->mono)
262 (void)wattrset(w, entry->mono);
263 #ifdef ENABLE_COLORS
264 }
265 #endif
267 return 0;
268 }