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 #ifdef ENABLE_COLORS
22 #include "options.h"
23 #endif
25 #include <assert.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <string.h>
29 #include <glib.h>
31 #define COLOR_BRIGHT_MASK (1<<7)
33 #define COLOR_BRIGHT_BLACK (COLOR_BLACK | COLOR_BRIGHT_MASK)
34 #define COLOR_BRIGHT_RED (COLOR_RED | COLOR_BRIGHT_MASK)
35 #define COLOR_BRIGHT_GREEN (COLOR_GREEN | COLOR_BRIGHT_MASK)
36 #define COLOR_BRIGHT_YELLOW (COLOR_YELLOW | COLOR_BRIGHT_MASK)
37 #define COLOR_BRIGHT_BLUE (COLOR_BLUE | COLOR_BRIGHT_MASK)
38 #define COLOR_BRIGHT_MAGENTA (COLOR_MAGENTA | COLOR_BRIGHT_MASK)
39 #define COLOR_BRIGHT_CYAN (COLOR_CYAN | COLOR_BRIGHT_MASK)
40 #define COLOR_BRIGHT_WHITE (COLOR_WHITE | COLOR_BRIGHT_MASK)
42 #define IS_BRIGHT(color) (color & COLOR_BRIGHT_MASK)
44 /* name of the color fields */
45 #define NAME_TITLE "title"
46 #define NAME_TITLE_BOLD "title-bold"
47 #define NAME_LINE "line"
48 #define NAME_LINE_BOLD "line-flags"
49 #define NAME_LIST "list"
50 #define NAME_LIST_BOLD "list-bold"
51 #define NAME_PROGRESS "progressbar"
52 #define NAME_STATUS "status-song"
53 #define NAME_STATUS_BOLD "status-state"
54 #define NAME_STATUS_TIME "status-time"
55 #define NAME_ALERT "alert"
56 #define NAME_BGCOLOR "background"
58 #ifdef ENABLE_COLORS
59 typedef struct {
60 short color;
61 short r,g,b;
62 } color_definition_entry_t;
63 #endif
65 typedef struct {
66 const char *name;
67 short fg;
68 attr_t attrs;
69 } color_entry_t;
71 static color_entry_t colors[COLOR_END] = {
72 /* color pair = field name, color, mono attribute */
73 [COLOR_TITLE] = { NAME_TITLE, COLOR_YELLOW, A_NORMAL },
74 [COLOR_TITLE_BOLD] = { NAME_TITLE_BOLD, COLOR_BRIGHT_YELLOW, A_BOLD },
75 [COLOR_LINE] = { NAME_LINE, COLOR_WHITE, A_NORMAL },
76 [COLOR_LINE_BOLD] = { NAME_LINE_BOLD, COLOR_BRIGHT_WHITE, A_BOLD },
77 [COLOR_LIST] = { NAME_LIST, COLOR_GREEN, A_NORMAL },
78 [COLOR_LIST_BOLD] = { NAME_LIST_BOLD, COLOR_BRIGHT_GREEN, A_BOLD },
79 [COLOR_PROGRESSBAR] = { NAME_PROGRESS, COLOR_WHITE, A_NORMAL },
80 [COLOR_STATUS] = { NAME_STATUS, COLOR_YELLOW, A_NORMAL },
81 [COLOR_STATUS_BOLD] = { NAME_STATUS_BOLD, COLOR_BRIGHT_YELLOW, A_BOLD },
82 [COLOR_STATUS_TIME] = { NAME_STATUS_TIME, COLOR_RED, A_NORMAL },
83 [COLOR_STATUS_ALERT] = { NAME_ALERT, COLOR_BRIGHT_RED, A_BOLD },
84 };
86 #ifdef ENABLE_COLORS
88 /* background color */
89 static short bg = COLOR_BLACK;
91 static GList *color_definition_list = NULL;
93 static color_entry_t *
94 colors_lookup_by_name(const char *name)
95 {
96 enum color i;
98 for (i = 1; i < COLOR_END; ++i)
99 if (!strcasecmp(colors[i].name, name))
100 return &colors[i];
102 return NULL;
103 }
105 static int
106 colors_update_pair(enum color id)
107 {
108 color_entry_t *entry = &colors[id];
109 short fg = -1;
111 assert(id > 0 && id < COLOR_END);
113 if (IS_BRIGHT(entry->fg)) {
114 entry->attrs = A_BOLD;
115 fg = entry->fg & ~COLOR_BRIGHT_MASK;
116 } else {
117 entry->attrs = A_NORMAL;
118 fg = entry->fg;
119 }
121 init_pair(id, fg, bg);
122 return 0;
123 }
125 short
126 colors_str2color(const char *str)
127 {
128 if (!strcasecmp(str, "black"))
129 return COLOR_BLACK;
130 else if (!strcasecmp(str, "red"))
131 return COLOR_RED;
132 else if (!strcasecmp(str, "green"))
133 return COLOR_GREEN;
134 else if (!strcasecmp(str, "yellow"))
135 return COLOR_YELLOW;
136 else if (!strcasecmp(str, "blue"))
137 return COLOR_BLUE;
138 else if (!strcasecmp(str, "magenta"))
139 return COLOR_MAGENTA;
140 else if (!strcasecmp(str, "cyan"))
141 return COLOR_CYAN;
142 else if (!strcasecmp(str, "white"))
143 return COLOR_WHITE;
144 else if (!strcasecmp(str, "brightred"))
145 return COLOR_BRIGHT_RED;
146 else if (!strcasecmp(str, "brightgreen"))
147 return COLOR_BRIGHT_GREEN;
148 else if (!strcasecmp(str, "brightyellow"))
149 return COLOR_BRIGHT_YELLOW;
150 else if (!strcasecmp(str, "brightblue"))
151 return COLOR_BRIGHT_BLUE;
152 else if (!strcasecmp(str, "brightmagenta"))
153 return COLOR_BRIGHT_MAGENTA;
154 else if (!strcasecmp(str, "brightcyan"))
155 return COLOR_BRIGHT_CYAN;
156 else if (!strcasecmp(str, "brightwhite"))
157 return COLOR_BRIGHT_WHITE;
158 else if (!strcasecmp(str, "grey") || !strcasecmp(str, "gray"))
159 return COLOR_BRIGHT_BLACK;
160 else if (!strcasecmp(str, "none"))
161 return -1;
163 fprintf(stderr,_("Warning: Unknown color - %s\n"), str);
164 return -2;
165 }
167 /* This function is called from conf.c before curses have been started,
168 * it adds the definition to the color_definition_list and init_color() is
169 * done in colors_start() */
170 int
171 colors_define(const char *name, short r, short g, short b)
172 {
173 color_definition_entry_t *entry;
174 short color = colors_str2color(name);
176 if (color < 0)
177 return -1;
179 entry = g_malloc(sizeof(color_definition_entry_t));
180 entry->color = color;
181 entry->r = r;
182 entry->g = g;
183 entry->b = b;
185 color_definition_list = g_list_append(color_definition_list, entry);
187 return 0;
188 }
190 int
191 colors_assign(const char *name, const char *value)
192 {
193 color_entry_t *entry = colors_lookup_by_name(name);
194 short color;
196 if (!entry) {
197 if (!strcasecmp(NAME_BGCOLOR, name)) {
198 if ((color = colors_str2color(value)) < -1)
199 return -1;
200 if (color > COLORS)
201 color = color & ~COLOR_BRIGHT_MASK;
202 bg = color;
203 return 0;
204 }
206 fprintf(stderr,_("Warning: Unknown color field - %s\n"), name);
207 return -1;
208 }
210 if ((color = colors_str2color(value)) < -1)
211 return -1;
213 entry->fg = color;
214 return 0;
215 }
218 int
219 colors_start(void)
220 {
221 if (has_colors()) {
222 /* initialize color support */
223 start_color();
224 use_default_colors();
225 /* define any custom colors defined in the configuration file */
226 if (color_definition_list && can_change_color()) {
227 GList *list = color_definition_list;
229 while (list) {
230 color_definition_entry_t *entry = list->data;
232 if (entry->color <= COLORS)
233 init_color(entry->color, entry->r,
234 entry->g, entry->b);
235 list = list->next;
236 }
237 } else if (color_definition_list && !can_change_color())
238 fprintf(stderr, _("Terminal lacks support for changing colors!\n"));
240 if (options.enable_colors) {
241 enum color i;
243 for (i = 1; i < COLOR_END; ++i)
244 /* update the color pairs */
245 colors_update_pair(i);
246 }
247 } else if (options.enable_colors) {
248 fprintf(stderr, _("Terminal lacks color capabilities!\n"));
249 options.enable_colors = 0;
250 }
252 /* free the color_definition_list */
253 if (color_definition_list) {
254 GList *list = color_definition_list;
256 while (list) {
257 g_free(list->data);
258 list=list->next;
259 }
261 g_list_free(color_definition_list);
262 color_definition_list = NULL;
263 }
265 return 0;
266 }
267 #endif
269 int
270 colors_use(WINDOW *w, enum color id)
271 {
272 color_entry_t *entry = &colors[id];
273 short pair;
274 attr_t attrs;
276 assert(id > 0 && id < COLOR_END);
278 wattr_get(w, &attrs, &pair, NULL);
280 #ifdef ENABLE_COLORS
281 if (options.enable_colors) {
282 /* color mode */
283 if (attrs != entry->attrs || (short)id != pair)
284 wattr_set(w, entry->attrs, id, NULL);
285 } else {
286 #endif
287 /* mono mode */
288 if (attrs != entry->attrs)
289 wattrset(w, entry->attrs);
290 #ifdef ENABLE_COLORS
291 }
292 #endif
294 return 0;
295 }