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