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