Code

colors: added enum color_t
[ncmpc.git] / src / colors.c
1 /*
2  * (c) 2004 by Kalle Wallin <kaw@linux.se>
3  *
4  * This program is free software; you can redistribute it and/or modify
5  * it under the terms of the GNU General Public License as published by
6  * the Free Software Foundation; either version 2 of the License, or
7  * (at your option) any later version.
8  *
9  * This program is distributed in the hope that it will be useful,
10  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12  * GNU General Public License for more details.
13  * You should have received a copy of the GNU General Public License
14  * along with this program; if not, write to the Free Software
15  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
16  *
17  */
19 #include "colors.h"
20 #include "i18n.h"
21 #include "options.h"
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <string.h>
26 #include <glib.h>
28 #define COLOR_BRIGHT_MASK (1<<7)
30 #define COLOR_BRIGHT_BLACK (COLOR_BLACK | COLOR_BRIGHT_MASK)
31 #define COLOR_BRIGHT_RED (COLOR_RED | COLOR_BRIGHT_MASK)
32 #define COLOR_BRIGHT_GREEN (COLOR_GREEN | COLOR_BRIGHT_MASK)
33 #define COLOR_BRIGHT_YELLOW (COLOR_YELLOW | COLOR_BRIGHT_MASK)
34 #define COLOR_BRIGHT_BLUE (COLOR_BLUE | COLOR_BRIGHT_MASK)
35 #define COLOR_BRIGHT_MAGENTA (COLOR_MAGENTA | COLOR_BRIGHT_MASK)
36 #define COLOR_BRIGHT_CYAN (COLOR_CYAN | COLOR_BRIGHT_MASK)
37 #define COLOR_BRIGHT_WHITE (COLOR_WHITE | COLOR_BRIGHT_MASK)
39 #define IS_BRIGHT(color) (color & COLOR_BRIGHT_MASK)
41 /* name of the color fields */
42 #define NAME_TITLE "title"
43 #define NAME_TITLE_BOLD "title-bold"
44 #define NAME_LINE "line"
45 #define NAME_LINE_BOLD "line-flags"
46 #define NAME_LIST "list"
47 #define NAME_LIST_BOLD "list-bold"
48 #define NAME_PROGRESS "progressbar"
49 #define NAME_STATUS "status-song"
50 #define NAME_STATUS_BOLD "status-state"
51 #define NAME_STATUS_TIME "status-time"
52 #define NAME_ALERT "alert"
53 #define NAME_BGCOLOR "background"
55 typedef struct {
56         short color;
57         short r,g,b;
58 } color_definition_entry_t;
60 typedef struct {
61         enum color id;
62         const char *name;
63         short fg;
64         attr_t attrs;
65 } color_entry_t;
67 static color_entry_t colors[] = {
68         /* color pair, field name, color, mono attribute */
69         { COLOR_TITLE,        NAME_TITLE,        COLOR_YELLOW,         A_NORMAL },
70         { COLOR_TITLE_BOLD,   NAME_TITLE_BOLD,   COLOR_BRIGHT_YELLOW,  A_BOLD },
71         { COLOR_LINE,         NAME_LINE,         COLOR_WHITE,          A_NORMAL },
72         { COLOR_LINE_BOLD,    NAME_LINE_BOLD,    COLOR_BRIGHT_WHITE,   A_BOLD },
73         { COLOR_LIST,         NAME_LIST,         COLOR_GREEN,          A_NORMAL },
74         { COLOR_LIST_BOLD,    NAME_LIST_BOLD,    COLOR_BRIGHT_GREEN,   A_BOLD },
75         { COLOR_PROGRESSBAR,  NAME_PROGRESS,     COLOR_WHITE,          A_NORMAL },
76         { COLOR_STATUS,       NAME_STATUS,       COLOR_YELLOW,         A_NORMAL },
77         { COLOR_STATUS_BOLD,  NAME_STATUS_BOLD,  COLOR_BRIGHT_YELLOW,  A_BOLD },
78         { COLOR_STATUS_TIME,  NAME_STATUS_TIME,  COLOR_RED,            A_NORMAL },
79         { COLOR_STATUS_ALERT, NAME_ALERT,        COLOR_BRIGHT_RED,     A_BOLD },
80         { 0,                  NULL,              0,                    0 }
81 };
83 /* background color */
84 static short bg = COLOR_BLACK;
86 static GList *color_definition_list = NULL;
88 static color_entry_t *
89 colors_lookup(enum color id)
90 {
91         int i = 0;
93         while (colors[i].name != NULL) {
94                 if (colors[i].id == id)
95                         return &colors[i];
96                 i++;
97         }
99         return NULL;
102 static color_entry_t *
103 colors_lookup_by_name(const char *name)
105         int i = 0;
107         while (colors[i].name != NULL) {
108                 if (!strcasecmp(colors[i].name, name))
109                         return &colors[i];
110                 i++;
111         }
113         return NULL;
116 static int
117 colors_update_pair(enum color id)
119         color_entry_t *entry = colors_lookup(id);
120         short fg = -1;
122         if (!entry)
123                 return -1;
125         if (IS_BRIGHT(entry->fg)) {
126                 entry->attrs = A_BOLD;
127                 fg = entry->fg & ~COLOR_BRIGHT_MASK;
128         } else {
129                 entry->attrs = A_NORMAL;
130                 fg = entry->fg;
131         }
133         init_pair(entry->id, fg, bg);
134         return 0;
137 short
138 colors_str2color(const char *str)
140         if (!strcasecmp(str, "black"))
141                 return COLOR_BLACK;
142         else if (!strcasecmp(str, "red"))
143                 return COLOR_RED;
144         else if (!strcasecmp(str, "green"))
145                 return COLOR_GREEN;
146         else if (!strcasecmp(str, "yellow"))
147                 return COLOR_YELLOW;
148         else if (!strcasecmp(str, "blue"))
149                 return COLOR_BLUE;
150         else if (!strcasecmp(str, "magenta"))
151                 return COLOR_MAGENTA;
152         else if (!strcasecmp(str, "cyan"))
153                 return COLOR_CYAN;
154         else if (!strcasecmp(str, "white"))
155                 return COLOR_WHITE;
156         else if (!strcasecmp(str, "brightred"))
157                 return COLOR_BRIGHT_RED;
158         else if (!strcasecmp(str, "brightgreen"))
159                 return COLOR_BRIGHT_GREEN;
160         else if (!strcasecmp(str, "brightyellow"))
161                 return COLOR_BRIGHT_YELLOW;
162         else if (!strcasecmp(str, "brightblue"))
163                 return COLOR_BRIGHT_BLUE;
164         else if (!strcasecmp(str, "brightmagenta"))
165                 return COLOR_BRIGHT_MAGENTA;
166         else if (!strcasecmp(str, "brightcyan"))
167                 return COLOR_BRIGHT_CYAN;
168         else if (!strcasecmp(str, "brightwhite"))
169                 return COLOR_BRIGHT_WHITE;
170         else if (!strcasecmp(str, "grey") || !strcasecmp(str, "gray"))
171                 return COLOR_BRIGHT_BLACK;
172         else if (!strcasecmp(str, "none"))
173                 return -1;
175         fprintf(stderr,_("Warning: Unknown color - %s\n"), str);
176         return -2;
179 /* This function is called from conf.c before curses have been started,
180  * it adds the definition to the color_definition_list and init_color() is
181  * done in colors_start() */
182 int
183 colors_define(const char *name, short r, short g, short b)
185         color_definition_entry_t *entry;
186         short color = colors_str2color(name);
188         if (color < 0)
189                 return -1;
191         entry = g_malloc(sizeof(color_definition_entry_t));
192         entry->color = color;
193         entry->r = r;
194         entry->g = g;
195         entry->b = b;
197         color_definition_list = g_list_append(color_definition_list, entry);
199         return 0;
202 int
203 colors_assign(const char *name, const char *value)
205         color_entry_t *entry = colors_lookup_by_name(name);
206         short color;
208         if (!entry) {
209                 if (!strcasecmp(NAME_BGCOLOR, name)) {
210                         if ((color = colors_str2color(value)) < -1)
211                                 return -1;
212                         if (color > COLORS)
213                                 color = color & ~COLOR_BRIGHT_MASK;
214                         bg = color;
215                         return 0;
216                 }
218                 fprintf(stderr,_("Warning: Unknown color field - %s\n"), name);
219                 return -1;
220         }
222         if ((color = colors_str2color(value)) < -1)
223                 return -1;
225         entry->fg = color;
226         return 0;
229 int
230 colors_start(void)
232         if (has_colors()) {
233                 /* initialize color support */
234                 start_color();
235                 use_default_colors();
236                 /* define any custom colors defined in the configuration file */
237                 if (color_definition_list && can_change_color()) {
238                         GList *list = color_definition_list;
240                         while (list) {
241                                 color_definition_entry_t *entry = list->data;
243                                 if (entry->color <= COLORS)
244                                         init_color(entry->color, entry->r,
245                                                    entry->g, entry->b);
246                                 list = list->next;
247                         }
248                 } else if (color_definition_list && !can_change_color())
249                         fprintf(stderr, _("Terminal lacks support for changing colors!\n"));
251                 if (options.enable_colors) {
252                         int i = 0;
254                         while (colors[i].name) {
255                                 /* update the color pairs */
256                                 colors_update_pair(colors[i].id);
257                                 i++;
258                         }
259                 }
260         } else if (options.enable_colors) {
261                 fprintf(stderr, _("Terminal lacks color capabilities!\n"));
262                 options.enable_colors = 0;
263         }
265         /* free the color_definition_list */
266         if (color_definition_list) {
267                 GList *list = color_definition_list;
269                 while (list) {
270                         g_free(list->data);
271                         list=list->next;
272                 }
274                 g_list_free(color_definition_list);
275                 color_definition_list = NULL;
276         }
278         return 0;
281 int
282 colors_use(WINDOW *w, enum color id)
284         color_entry_t *entry = colors_lookup(id);
285         short pair;
286         attr_t attrs;
288         if (!entry)
289                 return -1;
291         wattr_get(w, &attrs, &pair, NULL);
293         if (options.enable_colors) {
294                 /* color mode */
295                 if (attrs != entry->attrs || (short)id != pair)
296                         wattr_set(w, entry->attrs, id, NULL);
297         } else {
298                 /* mono mode */
299                 if (attrs != entry->attrs)
300                         wattrset(w, entry->attrs);
301         }
303         return 0;