68c5b600753998d821a4209683cb1f0e08e2d1c9
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 <assert.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <glib.h>
29 #define COLOR_BRIGHT_MASK (1<<7)
31 #define COLOR_BRIGHT_BLACK (COLOR_BLACK | COLOR_BRIGHT_MASK)
32 #define COLOR_BRIGHT_RED (COLOR_RED | COLOR_BRIGHT_MASK)
33 #define COLOR_BRIGHT_GREEN (COLOR_GREEN | COLOR_BRIGHT_MASK)
34 #define COLOR_BRIGHT_YELLOW (COLOR_YELLOW | COLOR_BRIGHT_MASK)
35 #define COLOR_BRIGHT_BLUE (COLOR_BLUE | COLOR_BRIGHT_MASK)
36 #define COLOR_BRIGHT_MAGENTA (COLOR_MAGENTA | COLOR_BRIGHT_MASK)
37 #define COLOR_BRIGHT_CYAN (COLOR_CYAN | COLOR_BRIGHT_MASK)
38 #define COLOR_BRIGHT_WHITE (COLOR_WHITE | COLOR_BRIGHT_MASK)
40 #define IS_BRIGHT(color) (color & COLOR_BRIGHT_MASK)
42 /* name of the color fields */
43 #define NAME_TITLE "title"
44 #define NAME_TITLE_BOLD "title-bold"
45 #define NAME_LINE "line"
46 #define NAME_LINE_BOLD "line-flags"
47 #define NAME_LIST "list"
48 #define NAME_LIST_BOLD "list-bold"
49 #define NAME_PROGRESS "progressbar"
50 #define NAME_STATUS "status-song"
51 #define NAME_STATUS_BOLD "status-state"
52 #define NAME_STATUS_TIME "status-time"
53 #define NAME_ALERT "alert"
54 #define NAME_BGCOLOR "background"
56 typedef struct {
57 short color;
58 short r,g,b;
59 } color_definition_entry_t;
61 typedef struct {
62 const char *name;
63 short fg;
64 attr_t attrs;
65 } color_entry_t;
67 static color_entry_t colors[COLOR_END] = {
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 };
82 /* background color */
83 static short bg = COLOR_BLACK;
85 static GList *color_definition_list = NULL;
87 static color_entry_t *
88 colors_lookup_by_name(const char *name)
89 {
90 enum color i;
92 for (i = 1; i < COLOR_END; ++i)
93 if (!strcasecmp(colors[i].name, name))
94 return &colors[i];
96 return NULL;
97 }
99 static int
100 colors_update_pair(enum color id)
101 {
102 color_entry_t *entry = &colors[id];
103 short fg = -1;
105 assert(id > 0 && id < COLOR_END);
107 if (IS_BRIGHT(entry->fg)) {
108 entry->attrs = A_BOLD;
109 fg = entry->fg & ~COLOR_BRIGHT_MASK;
110 } else {
111 entry->attrs = A_NORMAL;
112 fg = entry->fg;
113 }
115 init_pair(id, fg, bg);
116 return 0;
117 }
119 short
120 colors_str2color(const char *str)
121 {
122 if (!strcasecmp(str, "black"))
123 return COLOR_BLACK;
124 else if (!strcasecmp(str, "red"))
125 return COLOR_RED;
126 else if (!strcasecmp(str, "green"))
127 return COLOR_GREEN;
128 else if (!strcasecmp(str, "yellow"))
129 return COLOR_YELLOW;
130 else if (!strcasecmp(str, "blue"))
131 return COLOR_BLUE;
132 else if (!strcasecmp(str, "magenta"))
133 return COLOR_MAGENTA;
134 else if (!strcasecmp(str, "cyan"))
135 return COLOR_CYAN;
136 else if (!strcasecmp(str, "white"))
137 return COLOR_WHITE;
138 else if (!strcasecmp(str, "brightred"))
139 return COLOR_BRIGHT_RED;
140 else if (!strcasecmp(str, "brightgreen"))
141 return COLOR_BRIGHT_GREEN;
142 else if (!strcasecmp(str, "brightyellow"))
143 return COLOR_BRIGHT_YELLOW;
144 else if (!strcasecmp(str, "brightblue"))
145 return COLOR_BRIGHT_BLUE;
146 else if (!strcasecmp(str, "brightmagenta"))
147 return COLOR_BRIGHT_MAGENTA;
148 else if (!strcasecmp(str, "brightcyan"))
149 return COLOR_BRIGHT_CYAN;
150 else if (!strcasecmp(str, "brightwhite"))
151 return COLOR_BRIGHT_WHITE;
152 else if (!strcasecmp(str, "grey") || !strcasecmp(str, "gray"))
153 return COLOR_BRIGHT_BLACK;
154 else if (!strcasecmp(str, "none"))
155 return -1;
157 fprintf(stderr,_("Warning: Unknown color - %s\n"), str);
158 return -2;
159 }
161 /* This function is called from conf.c before curses have been started,
162 * it adds the definition to the color_definition_list and init_color() is
163 * done in colors_start() */
164 int
165 colors_define(const char *name, short r, short g, short b)
166 {
167 color_definition_entry_t *entry;
168 short color = colors_str2color(name);
170 if (color < 0)
171 return -1;
173 entry = g_malloc(sizeof(color_definition_entry_t));
174 entry->color = color;
175 entry->r = r;
176 entry->g = g;
177 entry->b = b;
179 color_definition_list = g_list_append(color_definition_list, entry);
181 return 0;
182 }
184 int
185 colors_assign(const char *name, const char *value)
186 {
187 color_entry_t *entry = colors_lookup_by_name(name);
188 short color;
190 if (!entry) {
191 if (!strcasecmp(NAME_BGCOLOR, name)) {
192 if ((color = colors_str2color(value)) < -1)
193 return -1;
194 if (color > COLORS)
195 color = color & ~COLOR_BRIGHT_MASK;
196 bg = color;
197 return 0;
198 }
200 fprintf(stderr,_("Warning: Unknown color field - %s\n"), name);
201 return -1;
202 }
204 if ((color = colors_str2color(value)) < -1)
205 return -1;
207 entry->fg = color;
208 return 0;
209 }
211 int
212 colors_start(void)
213 {
214 if (has_colors()) {
215 /* initialize color support */
216 start_color();
217 use_default_colors();
218 /* define any custom colors defined in the configuration file */
219 if (color_definition_list && can_change_color()) {
220 GList *list = color_definition_list;
222 while (list) {
223 color_definition_entry_t *entry = list->data;
225 if (entry->color <= COLORS)
226 init_color(entry->color, entry->r,
227 entry->g, entry->b);
228 list = list->next;
229 }
230 } else if (color_definition_list && !can_change_color())
231 fprintf(stderr, _("Terminal lacks support for changing colors!\n"));
233 if (options.enable_colors) {
234 enum color i;
236 for (i = 1; i < COLOR_END; ++i)
237 /* update the color pairs */
238 colors_update_pair(i);
239 }
240 } else if (options.enable_colors) {
241 fprintf(stderr, _("Terminal lacks color capabilities!\n"));
242 options.enable_colors = 0;
243 }
245 /* free the color_definition_list */
246 if (color_definition_list) {
247 GList *list = color_definition_list;
249 while (list) {
250 g_free(list->data);
251 list=list->next;
252 }
254 g_list_free(color_definition_list);
255 color_definition_list = NULL;
256 }
258 return 0;
259 }
261 int
262 colors_use(WINDOW *w, enum color id)
263 {
264 color_entry_t *entry = &colors[id];
265 short pair;
266 attr_t attrs;
268 assert(id > 0 && id < COLOR_END);
270 wattr_get(w, &attrs, &pair, NULL);
272 if (options.enable_colors) {
273 /* color mode */
274 if (attrs != entry->attrs || (short)id != pair)
275 wattr_set(w, entry->attrs, id, NULL);
276 } else {
277 /* mono mode */
278 if (attrs != entry->attrs)
279 wattrset(w, entry->attrs);
280 }
282 return 0;
283 }