Code

Changed directory layout (for future use of gettext)
[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 <stdio.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <ncurses.h>
23 #include <glib.h>
25 #include "config.h"
26 #include "options.h"
27 #include "support.h"
28 #include "colors.h"
30 #ifdef DEBUG
31 #define D(x) x
32 #else
33 #define D(x)
34 #endif
36 #define COLOR_BRIGHT_MASK   (1<<7)
38 #define COLOR_BRIGHT_BLACK    (COLOR_BLACK | COLOR_BRIGHT_MASK)
39 #define COLOR_BRIGHT_RED      (COLOR_RED   | COLOR_BRIGHT_MASK)
40 #define COLOR_BRIGHT_GREEN    (COLOR_GREEN | COLOR_BRIGHT_MASK)
41 #define COLOR_BRIGHT_YELLOW   (COLOR_YELLOW | COLOR_BRIGHT_MASK)
42 #define COLOR_BRIGHT_BLUE     (COLOR_BLUE | COLOR_BRIGHT_MASK)
43 #define COLOR_BRIGHT_MAGENTA  (COLOR_MAGENTA | COLOR_BRIGHT_MASK)
44 #define COLOR_BRIGHT_CYAN     (COLOR_CYAN | COLOR_BRIGHT_MASK)
45 #define COLOR_BRIGHT_WHITE    (COLOR_WHITE | COLOR_BRIGHT_MASK)
47 #define IS_BRIGHT(color) (color & COLOR_BRIGHT_MASK)
49 /* name of the color fields */
50 #define NAME_TITLE        "title"
51 #define NAME_TITLE_BOLD   "title-bold"
52 #define NAME_LINE         "line"
53 #define NAME_LINE_BOLD    "line-flags"
54 #define NAME_LIST         "list"
55 #define NAME_LIST_BOLD    "list-bold"
56 #define NAME_PROGRESS     "progressbar"
57 #define NAME_STATUS       "status-song"
58 #define NAME_STATUS_BOLD  "status-state"
59 #define NAME_STATUS_TIME  "status-time"
60 #define NAME_ALERT        "alert"
61 #define NAME_BGCOLOR      "background"
63 typedef struct {
64   short color;
65   short r,g,b;
66 } color_definition_entry_t;
68 typedef struct {
69   int    id;
70   char   *name;
71   short  fg;
72   attr_t attrs;
73 } color_entry_t;
75 static color_entry_t colors[] = {
77   /* color pair,        field name,        color,            mono attribute */
78   /*-------------------------------------------------------------------------*/
79   { COLOR_TITLE,        NAME_TITLE,        COLOR_YELLOW,         A_NORMAL },
80   { COLOR_TITLE_BOLD,   NAME_TITLE_BOLD,   COLOR_BRIGHT_YELLOW,  A_BOLD },
81   { COLOR_LINE,         NAME_LINE,         COLOR_WHITE,          A_NORMAL },
82   { COLOR_LINE_BOLD,    NAME_LINE_BOLD,    COLOR_BRIGHT_WHITE,   A_BOLD },
83   { COLOR_LIST,         NAME_LIST,         COLOR_GREEN,          A_NORMAL },
84   { COLOR_LIST_BOLD,    NAME_LIST_BOLD,    COLOR_BRIGHT_GREEN,   A_BOLD },
85   { COLOR_PROGRESSBAR,  NAME_PROGRESS,     COLOR_WHITE,          A_NORMAL },
86   { COLOR_STATUS,       NAME_STATUS,       COLOR_YELLOW,         A_NORMAL },
87   { COLOR_STATUS_BOLD,  NAME_STATUS_BOLD,  COLOR_BRIGHT_YELLOW,  A_BOLD },
88   { COLOR_STATUS_TIME,  NAME_STATUS_TIME,  COLOR_RED,            A_NORMAL },
89   { COLOR_STATUS_ALERT, NAME_ALERT,        COLOR_BRIGHT_RED,     A_BOLD },
90   { 0,                  NULL,              0,                    0 }
91 };
93 /* background color */
94 static short bg = COLOR_BLACK;
96 static GList *color_definition_list = NULL;
98 static color_entry_t *
99 colors_lookup(int id)
101   int i;
103   i=0;
104   while( colors[i].name != NULL )
105     {
106       if( colors[i].id == id )
107         return &colors[i];
108       i++;
109     }
110   return NULL;
113 static color_entry_t *
114 colors_lookup_by_name(char *name)
116   int i;
118   i=0;
119   while( colors[i].name != NULL )
120     {
121       if( !strcasecmp(colors[i].name, name) )
122         return &colors[i];
123       i++;
124     }
125   return NULL;
128 static int
129 colors_update_pair(int id)
131   color_entry_t *entry = colors_lookup(id);
132   short fg = -1;
134   if( !entry )
135     return -1;
137   if( IS_BRIGHT(entry->fg) )
138     {
139       entry->attrs = A_BOLD;
140       fg = entry->fg & ~COLOR_BRIGHT_MASK;
141     }
142   else
143     {
144       entry->attrs = A_NORMAL;
145       fg = entry->fg;
146     }
148   init_pair(entry->id, fg, bg);
149   
150   return 0;
153 short
154 colors_str2color(char *str)
156   if( !strcasecmp(str,"black") )
157     return COLOR_BLACK;
158   else if( !strcasecmp(str,"red") )
159     return COLOR_RED;
160   else if( !strcasecmp(str,"green") )
161     return COLOR_GREEN;
162   else if( !strcasecmp(str,"yellow") )
163     return COLOR_YELLOW;
164   else if( !strcasecmp(str,"blue") )
165     return COLOR_BLUE;
166   else if( !strcasecmp(str,"magenta") )
167     return COLOR_MAGENTA;
168   else if( !strcasecmp(str,"cyan") )
169     return COLOR_CYAN;
170   else if( !strcasecmp(str,"white") )
171     return COLOR_WHITE;
172   else if( !strcasecmp(str,"brightred") )
173     return COLOR_BRIGHT_RED;
174   else if( !strcasecmp(str,"brightgreen") )
175     return COLOR_BRIGHT_GREEN;
176   else if( !strcasecmp(str,"brightyellow") )
177     return COLOR_BRIGHT_YELLOW;
178   else if( !strcasecmp(str,"brightblue") )
179     return COLOR_BRIGHT_BLUE;
180   else if( !strcasecmp(str,"brightmagenta") )
181     return COLOR_BRIGHT_MAGENTA;
182   else if( !strcasecmp(str,"brightcyan") )
183     return COLOR_BRIGHT_CYAN;
184   else if( !strcasecmp(str,"brightwhite") )
185     return COLOR_BRIGHT_WHITE;
186   else if( !strcasecmp(str,"grey") || !strcasecmp(str,"gray") )
187     return COLOR_BRIGHT_BLACK;
188   else if( !strcasecmp(str,"none") )
189     return -1;
190   fprintf(stderr,"Warning: Unknown color - %s\n", str);
191   return -2;
194 /* This function is called from conf.c before curses have been started,
195  * it adds the definition to the color_definition_list and init_color() is 
196  * done in colors_start() */
197 int
198 colors_define(char *name, short r, short g, short b)
200   color_definition_entry_t *entry;
201   short color = colors_str2color(name);
203   if( color<0 )
204     return -1;
206   entry = g_malloc(sizeof(color_definition_entry_t));
207   entry->color = color;
208   entry->r = r;
209   entry->g = g;
210   entry->b = b;
212   color_definition_list = g_list_append(color_definition_list, entry);
214   return 0;
218 int
219 colors_assign(char *name, char *value)
221   color_entry_t *entry = colors_lookup_by_name(name);
222   short color;
224   if( !entry )
225     {
226       if( !strcasecmp(NAME_BGCOLOR, name) )
227         {
228           if( (color=colors_str2color(value)) < -1 )
229             return -1;
230           if( color > COLORS )
231             color = color & ~COLOR_BRIGHT_MASK;
232           bg = color;
233           return 0;
234         }
235       fprintf(stderr,"Warning: Unknown color field - %s\n", name);
236       return -1;
237     }
239   if( (color=colors_str2color(value)) < -1 )
240     return -1;
241   entry->fg = color;
243   return 0;
247 int
248 colors_start(void)
250   if( has_colors() )
251     {
252       /* initialize color support */
253       start_color();
254       use_default_colors();
255       /* define any custom colors defined in the configuration file */
256       if( color_definition_list && can_change_color() )
257         {
258           GList *list = color_definition_list;
260           while( list )
261             {
262               color_definition_entry_t *entry = list->data;
264               if( entry->color <= COLORS )
265                 init_color(entry->color, entry->r, entry->g, entry->b);
266               list=list->next;
267             }
268         }
269       else if( !can_change_color() )
270         fprintf(stderr, "Terminal lacks support for changing colors!\n");
272       if( options.enable_colors )
273         {
274           int i = 0;
276           while(colors[i].name)
277             {
278               /* update the color pairs */
279               colors_update_pair(colors[i].id);
280               i++;
281             }
283         }
284     }
285   else if( options.enable_colors )
286     {
287       fprintf(stderr, "Terminal lacks color capabilities!\n");
288       options.enable_colors = 0;
289     }
291   /* free the color_definition_list */
292   if( color_definition_list )
293     {
294       GList *list = color_definition_list;
296       while( list )
297         {
298           g_free(list->data);
299           list=list->next;
300         }
301       g_list_free(color_definition_list);
302       color_definition_list = NULL;
303     }
305   return 0;
309 int
310 colors_use(WINDOW *w, int id)
312   color_entry_t *entry = colors_lookup(id);
313   short pair;
314   attr_t attrs;
316   if( !entry )
317     return -1;
319   wattr_get(w, &attrs, &pair, NULL);
321   if( options.enable_colors )
322     {
323       /* color mode */
324       if( attrs != entry->attrs || id!=pair )
325         wattr_set(w, entry->attrs, id, NULL);
326     }
327   else
328     {
329       /* mono mode */
330       if( attrs != entry->attrs )
331         wattrset(w, entry->attrs);
332     }
333   
334   return 0;