Code

*: make variables more local
[ncmpc.git] / src / colors.c
1 /* ncmpc (Ncurses MPD Client)
2  * (c) 2004-2010 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_LIST]         = {"list",              COLOR_GREEN,           A_NORMAL},
57         [COLOR_LIST_BOLD]    = {"list-bold",         COLOR_GREEN  | A_BOLD, A_BOLD  },
58         [COLOR_PROGRESSBAR]  = {"progressbar",       COLOR_WHITE,           A_NORMAL},
59         [COLOR_STATUS]       = {"status-song",       COLOR_YELLOW,          A_NORMAL},
60         [COLOR_STATUS_BOLD]  = {"status-state",      COLOR_YELLOW | A_BOLD, A_BOLD  },
61         [COLOR_STATUS_TIME]  = {"status-time",       COLOR_RED,             A_NORMAL},
62         [COLOR_STATUS_ALERT] = {"alert",             COLOR_RED    | A_BOLD, A_BOLD  },
63         [COLOR_DIRECTORY]    = {"browser-directory", COLOR_YELLOW,          A_NORMAL},
64         [COLOR_PLAYLIST]     = {"browser-playlist",  COLOR_RED,             A_NORMAL},
65         [COLOR_BACKGROUND]   = {"background",        COLOR_BLACK,           A_NORMAL},
66 };
68 #ifdef ENABLE_COLORS
70 static GList *color_definition_list = NULL;
72 static color_entry_t *
73 colors_lookup_by_name(const char *name)
74 {
75         for (enum color 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         assert(id > 0 && id < COLOR_END);
87         int fg = colors[id].color;
88         int bg = colors[COLOR_BACKGROUND].color;
90         /* If color == COLOR_NONE (negative),
91          * pass -1 to avoid cast errors */
92         init_pair(id,
93                 (fg < 0 ? -1 : fg),
94                 (bg < 0 ? -1 : bg));
95         return 0;
96 }
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;
110                 }
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,_("Warning: Unknown color - %s\n"), str);
155                                 return COLOR_ERROR;
156                         }
157                 }
159         }
160         g_strfreev(parts);
161         return color;
164 /* This function is called from conf.c before curses have been started,
165  * it adds the definition to the color_definition_list and init_color() is
166  * done in colors_start() */
167 int
168 colors_define(const char *name, short r, short g, short b)
170         int color = colors_str2color(name);
172         if (color < 0)
173                 return color;
175         color_definition_entry_t *entry =
176                 g_malloc(sizeof(color_definition_entry_t));
177         entry->color = color;
178         entry->r = r;
179         entry->g = g;
180         entry->b = b;
182         color_definition_list = g_list_append(color_definition_list, entry);
184         return 0;
187 int
188 colors_assign(const char *name, const char *value)
190         color_entry_t *entry = colors_lookup_by_name(name);
192         if (!entry) {
193                 fprintf(stderr,_("Warning: Unknown color field - %s\n"), name);
194                 return -1;
195         }
197         const int color = colors_str2color(value);
198         if (color == COLOR_ERROR)
199                 return -1;
201         entry->color = color;
202         return 0;
206 int
207 colors_start(void)
209         if (has_colors()) {
210                 /* initialize color support */
211                 start_color();
212                 use_default_colors();
213                 /* define any custom colors defined in the configuration file */
214                 if (color_definition_list && can_change_color()) {
215                         GList *list = color_definition_list;
217                         while (list) {
218                                 color_definition_entry_t *entry = list->data;
220                                 if (entry->color <= COLORS)
221                                         init_color(entry->color, entry->r,
222                                                    entry->g, entry->b);
223                                 list = list->next;
224                         }
225                 } else if (color_definition_list && !can_change_color())
226                         fprintf(stderr, "%s\n",
227                                 _("Terminal lacks support for changing colors"));
229                 if (options.enable_colors) {
230                         for (enum color i = 1; i < COLOR_END; ++i)
231                                 /* update the color pairs */
232                                 colors_update_pair(i);
233                 }
234         } else if (options.enable_colors) {
235                 fprintf(stderr, "%s\n",
236                         _("Terminal lacks color capabilities"));
237                 options.enable_colors = 0;
238         }
240         /* free the color_definition_list */
241         if (color_definition_list) {
242                 GList *list = color_definition_list;
244                 while (list) {
245                         g_free(list->data);
246                         list=list->next;
247                 }
249                 g_list_free(color_definition_list);
250                 color_definition_list = NULL;
251         }
253         return 0;
255 #endif
257 int
258 colors_use(WINDOW *w, enum color id)
260         color_entry_t *entry = &colors[id];
262         assert(id > 0 && id < COLOR_END);
264         attr_t attrs;
265         short pair;
266         fix_wattr_get(w, &attrs, &pair, NULL);
268 #ifdef ENABLE_COLORS
269         if (options.enable_colors) {
270                 /* color mode */
271                 if ((int)attrs != entry->color || (short)id != pair)
272                         wattr_set(w, entry->color, id, NULL);
273         } else {
274 #endif
275                 /* mono mode */
276                 if ((int)attrs != entry->mono)
277                         (void)wattrset(w, entry->mono);
278 #ifdef ENABLE_COLORS
279         }
280 #endif
282         return 0;