release v0.29
[ncmpc.git] / src / colors.c
blob5a79fd9bbfff8b2c6a7967e499af18df19c98544
1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2017 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.
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},
69 #ifdef ENABLE_COLORS
71 static GList *color_definition_list = NULL;
73 static color_entry_t *
74 colors_lookup_by_name(const char *name)
76 for (enum color i = 1; i < COLOR_END; ++i)
77 if (!strcasecmp(colors[i].name, name))
78 return &colors[i];
80 return NULL;
83 static void
84 colors_update_pair(enum color id)
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));
98 int
99 colors_str2color(const char *str)
101 int color = 0;
102 char **parts = g_strsplit(str, ",", 0);
103 for (int i = 0; parts[i]; i++) {
104 char *cur = parts[i];
106 /* Legacy colors (brightblue,etc) */
107 if (!strncasecmp(cur, "bright", 6)) {
108 color |= A_BOLD;
109 cur += 6;
112 /* Colors */
113 if (!strcasecmp(cur, "none"))
114 color |= COLOR_NONE;
115 else if (!strcasecmp(cur, "black"))
116 color |= COLOR_BLACK;
117 else if (!strcasecmp(cur, "red"))
118 color |= COLOR_RED;
119 else if (!strcasecmp(cur, "green"))
120 color |= COLOR_GREEN;
121 else if (!strcasecmp(cur, "yellow"))
122 color |= COLOR_YELLOW;
123 else if (!strcasecmp(cur, "blue"))
124 color |= COLOR_BLUE;
125 else if (!strcasecmp(cur, "magenta"))
126 color |= COLOR_MAGENTA;
127 else if (!strcasecmp(cur, "cyan"))
128 color |= COLOR_CYAN;
129 else if (!strcasecmp(cur, "white"))
130 color |= COLOR_WHITE;
131 else if (!strcasecmp(cur, "grey") || !strcasecmp(cur, "gray"))
132 color |= COLOR_BLACK | A_BOLD;
134 /* Attributes */
135 else if (!strcasecmp(cur, "standout"))
136 color |= A_STANDOUT;
137 else if (!strcasecmp(cur, "underline"))
138 color |= A_UNDERLINE;
139 else if (!strcasecmp(cur, "reverse"))
140 color |= A_REVERSE;
141 else if (!strcasecmp(cur, "blink"))
142 color |= A_BLINK;
143 else if (!strcasecmp(cur, "dim"))
144 color |= A_DIM;
145 else if (!strcasecmp(cur, "bold"))
146 color |= A_BOLD;
147 else {
148 /* Numerical colors */
149 char *endptr;
150 int tmp = strtol(cur, &endptr, 10);
151 if (cur != endptr && endptr[0] == '\0') {
152 color |= tmp;
153 } else {
154 fprintf(stderr, "%s: %s\n",
155 _("Unknown color"), str);
156 return COLOR_ERROR;
161 g_strfreev(parts);
162 return color;
165 /* This function is called from conf.c before curses have been started,
166 * it adds the definition to the color_definition_list and init_color() is
167 * done in colors_start() */
168 bool
169 colors_define(const char *name, short r, short g, short b)
171 int color = colors_str2color(name);
173 if (color < 0)
174 return false;
176 color_definition_entry_t *entry =
177 g_malloc(sizeof(color_definition_entry_t));
178 entry->color = color;
179 entry->r = r;
180 entry->g = g;
181 entry->b = b;
183 color_definition_list = g_list_append(color_definition_list, entry);
185 return true;
188 bool
189 colors_assign(const char *name, const char *value)
191 color_entry_t *entry = colors_lookup_by_name(name);
193 if (!entry) {
194 fprintf(stderr, "%s: %s",
195 _("Unknown color field"), name);
196 return false;
199 const int color = colors_str2color(value);
200 if (color == COLOR_ERROR)
201 return false;
203 entry->color = color;
204 return true;
207 void
208 colors_start(void)
210 if (has_colors()) {
211 /* initialize color support */
212 start_color();
213 use_default_colors();
214 /* define any custom colors defined in the configuration file */
215 if (color_definition_list && can_change_color()) {
216 GList *list = color_definition_list;
218 while (list) {
219 color_definition_entry_t *entry = list->data;
221 if (entry->color <= COLORS)
222 init_color(entry->color, entry->r,
223 entry->g, entry->b);
224 list = list->next;
226 } else if (color_definition_list && !can_change_color())
227 fprintf(stderr, "%s\n",
228 _("Terminal lacks support for changing colors"));
230 if (options.enable_colors) {
231 for (enum color i = 1; i < COLOR_END; ++i)
232 /* update the color pairs */
233 colors_update_pair(i);
235 } else if (options.enable_colors) {
236 fprintf(stderr, "%s\n",
237 _("Terminal lacks color capabilities"));
238 options.enable_colors = 0;
241 /* free the color_definition_list */
242 if (color_definition_list) {
243 g_list_free_full(color_definition_list, g_free);
244 color_definition_list = NULL;
247 #endif
249 void
250 colors_use(WINDOW *w, enum color id)
252 color_entry_t *entry = &colors[id];
254 assert(id > 0 && id < COLOR_END);
256 attr_t attrs;
257 short pair;
258 fix_wattr_get(w, &attrs, &pair, NULL);
260 #ifdef ENABLE_COLORS
261 if (options.enable_colors) {
262 /* color mode */
263 if ((int)attrs != entry->color || (short)id != pair)
264 wattr_set(w, entry->color, id, NULL);
265 } else {
266 #endif
267 /* mono mode */
268 if ((int)attrs != entry->mono)
269 (void)wattrset(w, entry->mono);
270 #ifdef ENABLE_COLORS
272 #endif