Code

Only display the "change color" error message if we need init_color()
[ncmpc.git] / src / colors.c
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[] = {
74   /* color pair,        field name,        color,            mono attribute */
75   /*-------------------------------------------------------------------------*/
76   { COLOR_TITLE,        NAME_TITLE,        COLOR_YELLOW,         A_NORMAL },
77   { COLOR_TITLE_BOLD,   NAME_TITLE_BOLD,   COLOR_BRIGHT_YELLOW,  A_BOLD },
78   { COLOR_LINE,         NAME_LINE,         COLOR_WHITE,          A_NORMAL },
79   { COLOR_LINE_BOLD,    NAME_LINE_BOLD,    COLOR_BRIGHT_WHITE,   A_BOLD },
80   { COLOR_LIST,         NAME_LIST,         COLOR_GREEN,          A_NORMAL },
81   { COLOR_LIST_BOLD,    NAME_LIST_BOLD,    COLOR_BRIGHT_GREEN,   A_BOLD },
82   { COLOR_PROGRESSBAR,  NAME_PROGRESS,     COLOR_WHITE,          A_NORMAL },
83   { COLOR_STATUS,       NAME_STATUS,       COLOR_YELLOW,         A_NORMAL },
84   { COLOR_STATUS_BOLD,  NAME_STATUS_BOLD,  COLOR_BRIGHT_YELLOW,  A_BOLD },
85   { COLOR_STATUS_TIME,  NAME_STATUS_TIME,  COLOR_RED,            A_NORMAL },
86   { COLOR_STATUS_ALERT, NAME_ALERT,        COLOR_BRIGHT_RED,     A_BOLD },
87   { 0,                  NULL,              0,                    0 }
88 };
90 /* background color */
91 static short bg = COLOR_BLACK;
93 static GList *color_definition_list = NULL;
95 static color_entry_t *
96 colors_lookup(int id)
97 {
98   int i;
100   i=0;
101   while( colors[i].name != NULL )
102     {
103       if( colors[i].id == id )
104         return &colors[i];
105       i++;
106     }
107   return NULL;
110 static color_entry_t *
111 colors_lookup_by_name(char *name)
113   int i;
115   i=0;
116   while( colors[i].name != NULL )
117     {
118       if( !strcasecmp(colors[i].name, name) )
119         return &colors[i];
120       i++;
121     }
122   return NULL;
125 static int
126 colors_update_pair(int id)
128   color_entry_t *entry = colors_lookup(id);
129   short fg = -1;
131   if( !entry )
132     return -1;
134   if( IS_BRIGHT(entry->fg) )
135     {
136       entry->attrs = A_BOLD;
137       fg = entry->fg & ~COLOR_BRIGHT_MASK;
138     }
139   else
140     {
141       entry->attrs = A_NORMAL;
142       fg = entry->fg;
143     }
145   init_pair(entry->id, fg, bg);
146   
147   return 0;
150 short
151 colors_str2color(char *str)
153   if( !strcasecmp(str,"black") )
154     return COLOR_BLACK;
155   else if( !strcasecmp(str,"red") )
156     return COLOR_RED;
157   else if( !strcasecmp(str,"green") )
158     return COLOR_GREEN;
159   else if( !strcasecmp(str,"yellow") )
160     return COLOR_YELLOW;
161   else if( !strcasecmp(str,"blue") )
162     return COLOR_BLUE;
163   else if( !strcasecmp(str,"magenta") )
164     return COLOR_MAGENTA;
165   else if( !strcasecmp(str,"cyan") )
166     return COLOR_CYAN;
167   else if( !strcasecmp(str,"white") )
168     return COLOR_WHITE;
169   else if( !strcasecmp(str,"brightred") )
170     return COLOR_BRIGHT_RED;
171   else if( !strcasecmp(str,"brightgreen") )
172     return COLOR_BRIGHT_GREEN;
173   else if( !strcasecmp(str,"brightyellow") )
174     return COLOR_BRIGHT_YELLOW;
175   else if( !strcasecmp(str,"brightblue") )
176     return COLOR_BRIGHT_BLUE;
177   else if( !strcasecmp(str,"brightmagenta") )
178     return COLOR_BRIGHT_MAGENTA;
179   else if( !strcasecmp(str,"brightcyan") )
180     return COLOR_BRIGHT_CYAN;
181   else if( !strcasecmp(str,"brightwhite") )
182     return COLOR_BRIGHT_WHITE;
183   else if( !strcasecmp(str,"grey") || !strcasecmp(str,"gray") )
184     return COLOR_BRIGHT_BLACK;
185   else if( !strcasecmp(str,"none") )
186     return -1;
187   fprintf(stderr,_("Warning: Unknown color - %s\n"), str);
188   return -2;
191 /* This function is called from conf.c before curses have been started,
192  * it adds the definition to the color_definition_list and init_color() is 
193  * done in colors_start() */
194 int
195 colors_define(char *name, short r, short g, short b)
197   color_definition_entry_t *entry;
198   short color = colors_str2color(name);
200   if( color<0 )
201     return -1;
203   entry = g_malloc(sizeof(color_definition_entry_t));
204   entry->color = color;
205   entry->r = r;
206   entry->g = g;
207   entry->b = b;
209   color_definition_list = g_list_append(color_definition_list, entry);
211   return 0;
215 int
216 colors_assign(char *name, char *value)
218   color_entry_t *entry = colors_lookup_by_name(name);
219   short color;
221   if( !entry )
222     {
223       if( !strcasecmp(NAME_BGCOLOR, name) )
224         {
225           if( (color=colors_str2color(value)) < -1 )
226             return -1;
227           if( color > COLORS )
228             color = color & ~COLOR_BRIGHT_MASK;
229           bg = color;
230           return 0;
231         }
232       fprintf(stderr,_("Warning: Unknown color field - %s\n"), name);
233       return -1;
234     }
236   if( (color=colors_str2color(value)) < -1 )
237     return -1;
238   entry->fg = color;
240   return 0;
244 int
245 colors_start(void)
247   if( has_colors() )
248     {
249       /* initialize color support */
250       start_color();
251       use_default_colors();
252       /* define any custom colors defined in the configuration file */
253       if( color_definition_list && can_change_color() )
254         {
255           GList *list = color_definition_list;
257           while( list )
258             {
259               color_definition_entry_t *entry = list->data;
261               if( entry->color <= COLORS )
262                 init_color(entry->color, entry->r, entry->g, entry->b);
263               list=list->next;
264             }
265         }
266       else if( color_definition_list && !can_change_color() )
267         fprintf(stderr, _("Terminal lacks support for changing colors!\n"));
269       if( options.enable_colors )
270         {
271           int i = 0;
273           while(colors[i].name)
274             {
275               /* update the color pairs */
276               colors_update_pair(colors[i].id);
277               i++;
278             }
280         }
281     }
282   else if( options.enable_colors )
283     {
284       fprintf(stderr, _("Terminal lacks color capabilities!\n"));
285       options.enable_colors = 0;
286     }
288   /* free the color_definition_list */
289   if( color_definition_list )
290     {
291       GList *list = color_definition_list;
293       while( list )
294         {
295           g_free(list->data);
296           list=list->next;
297         }
298       g_list_free(color_definition_list);
299       color_definition_list = NULL;
300     }
302   return 0;
306 int
307 colors_use(WINDOW *w, int id)
309   color_entry_t *entry = colors_lookup(id);
310   short pair;
311   attr_t attrs;
313   if( !entry )
314     return -1;
316   wattr_get(w, &attrs, &pair, NULL);
318   if( options.enable_colors )
319     {
320       /* color mode */
321       if( attrs != entry->attrs || id!=pair )
322         wattr_set(w, entry->attrs, id, NULL);
323     }
324   else
325     {
326       /* mono mode */
327       if( attrs != entry->attrs )
328         wattrset(w, entry->attrs);
329     }
330   
331   return 0;