File: colors.c

package info (click to toggle)
ncmpc 0.25-0.1%2Bdeb9u1
  • links: PTS, VCS
  • area: main
  • in suites: stretch
  • size: 2,616 kB
  • sloc: ansic: 12,229; sh: 4,157; makefile: 282; ruby: 28
file content (284 lines) | stat: -rw-r--r-- 7,340 bytes parent folder | download | duplicates (2)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
/* ncmpc (Ncurses MPD Client)
 * (c) 2004-2010 The Music Player Daemon Project
 * Project homepage: http://musicpd.org
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License along
 * with this program; if not, write to the Free Software Foundation, Inc.,
 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include "colors.h"
#include "i18n.h"
#include "ncfix.h"

#ifdef ENABLE_COLORS
#include "options.h"
#endif

#include <assert.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <glib.h>

#define COLOR_NONE  G_MININT /* left most bit only */
#define COLOR_ERROR -2

#ifdef ENABLE_COLORS
typedef struct {
	short color;
	short r,g,b;
} color_definition_entry_t;
#endif

typedef struct {
	const char *name;
	int color;
	int mono;
} color_entry_t;

static color_entry_t colors[COLOR_END] = {
	/* color pair = field name, color, mono */
	[COLOR_TITLE]        = {"title",             COLOR_YELLOW,          A_NORMAL},
	[COLOR_TITLE_BOLD]   = {"title-bold",        COLOR_YELLOW | A_BOLD, A_BOLD  },
	[COLOR_LINE]         = {"line",              COLOR_WHITE,           A_NORMAL},
	[COLOR_LINE_BOLD]    = {"line-bold",         COLOR_WHITE  | A_BOLD, A_BOLD  },
	[COLOR_LINE_FLAGS]   = {"line-flags",        COLOR_YELLOW,          A_NORMAL},
	[COLOR_LIST]         = {"list",              COLOR_GREEN,           A_NORMAL},
	[COLOR_LIST_BOLD]    = {"list-bold",         COLOR_GREEN  | A_BOLD, A_BOLD  },
	[COLOR_PROGRESSBAR]  = {"progressbar",       COLOR_WHITE,           A_NORMAL},
	[COLOR_STATUS]       = {"status-song",       COLOR_YELLOW,          A_NORMAL},
	[COLOR_STATUS_BOLD]  = {"status-state",      COLOR_YELLOW | A_BOLD, A_BOLD  },
	[COLOR_STATUS_TIME]  = {"status-time",       COLOR_RED,             A_NORMAL},
	[COLOR_STATUS_ALERT] = {"alert",             COLOR_RED    | A_BOLD, A_BOLD  },
	[COLOR_DIRECTORY]    = {"browser-directory", COLOR_YELLOW,          A_NORMAL},
	[COLOR_PLAYLIST]     = {"browser-playlist",  COLOR_RED,             A_NORMAL},
	[COLOR_BACKGROUND]   = {"background",        COLOR_BLACK,           A_NORMAL},
};

#ifdef ENABLE_COLORS

static GList *color_definition_list = NULL;

static color_entry_t *
colors_lookup_by_name(const char *name)
{
	for (enum color i = 1; i < COLOR_END; ++i)
		if (!strcasecmp(colors[i].name, name))
			return &colors[i];

	return NULL;
}

static int
colors_update_pair(enum color id)
{
	assert(id > 0 && id < COLOR_END);

	int fg = colors[id].color;
	int bg = colors[COLOR_BACKGROUND].color;

	/* If color == COLOR_NONE (negative),
	 * pass -1 to avoid cast errors */
	init_pair(id,
		(fg < 0 ? -1 : fg),
		(bg < 0 ? -1 : bg));
	return 0;
}

int
colors_str2color(const char *str)
{
	int color = 0;
	char **parts = g_strsplit(str, ",", 0);
	for (int i = 0; parts[i]; i++) {
		char *cur = parts[i];

		/* Legacy colors (brightblue,etc) */
		if (!strncasecmp(cur, "bright", 6)) {
			color |= A_BOLD;
			cur += 6;
		}

		/* Colors */
		if (!strcasecmp(cur, "none"))
			color |= COLOR_NONE;
		else if (!strcasecmp(cur, "black"))
			color |= COLOR_BLACK;
		else if (!strcasecmp(cur, "red"))
			color |= COLOR_RED;
		else if (!strcasecmp(cur, "green"))
			color |= COLOR_GREEN;
		else if (!strcasecmp(cur, "yellow"))
			color |= COLOR_YELLOW;
		else if (!strcasecmp(cur, "blue"))
			color |= COLOR_BLUE;
		else if (!strcasecmp(cur, "magenta"))
			color |= COLOR_MAGENTA;
		else if (!strcasecmp(cur, "cyan"))
			color |= COLOR_CYAN;
		else if (!strcasecmp(cur, "white"))
			color |= COLOR_WHITE;
		else if (!strcasecmp(cur, "grey") || !strcasecmp(cur, "gray"))
			color |= COLOR_BLACK | A_BOLD;

		/* Attributes */
		else if (!strcasecmp(cur, "standout"))
			color |= A_STANDOUT;
		else if (!strcasecmp(cur, "underline"))
			color |= A_UNDERLINE;
		else if (!strcasecmp(cur, "reverse"))
			color |= A_REVERSE;
		else if (!strcasecmp(cur, "blink"))
			color |= A_BLINK;
		else if (!strcasecmp(cur, "dim"))
			color |= A_DIM;
		else if (!strcasecmp(cur, "bold"))
			color |= A_BOLD;
		else {
			/* Numerical colors */
			char *endptr;
			int tmp = strtol(cur, &endptr, 10);
			if (cur != endptr && endptr[0] == '\0') {
				color |= tmp;
			} else {
				fprintf(stderr,_("Warning: Unknown color - %s\n"), str);
				return COLOR_ERROR;
			}
		}

	}
	g_strfreev(parts);
	return color;
}

/* This function is called from conf.c before curses have been started,
 * it adds the definition to the color_definition_list and init_color() is
 * done in colors_start() */
int
colors_define(const char *name, short r, short g, short b)
{
	int color = colors_str2color(name);

	if (color < 0)
		return color;

	color_definition_entry_t *entry =
		g_malloc(sizeof(color_definition_entry_t));
	entry->color = color;
	entry->r = r;
	entry->g = g;
	entry->b = b;

	color_definition_list = g_list_append(color_definition_list, entry);

	return 0;
}

int
colors_assign(const char *name, const char *value)
{
	color_entry_t *entry = colors_lookup_by_name(name);

	if (!entry) {
		fprintf(stderr,_("Warning: Unknown color field - %s\n"), name);
		return -1;
	}

	const int color = colors_str2color(value);
	if (color == COLOR_ERROR)
		return -1;

	entry->color = color;
	return 0;
}


int
colors_start(void)
{
	if (has_colors()) {
		/* initialize color support */
		start_color();
		use_default_colors();
		/* define any custom colors defined in the configuration file */
		if (color_definition_list && can_change_color()) {
			GList *list = color_definition_list;

			while (list) {
				color_definition_entry_t *entry = list->data;

				if (entry->color <= COLORS)
					init_color(entry->color, entry->r,
						   entry->g, entry->b);
				list = list->next;
			}
		} else if (color_definition_list && !can_change_color())
			fprintf(stderr, "%s\n",
				_("Terminal lacks support for changing colors"));

		if (options.enable_colors) {
			for (enum color i = 1; i < COLOR_END; ++i)
				/* update the color pairs */
				colors_update_pair(i);
		}
	} else if (options.enable_colors) {
		fprintf(stderr, "%s\n",
			_("Terminal lacks color capabilities"));
		options.enable_colors = 0;
	}

	/* free the color_definition_list */
	if (color_definition_list) {
		GList *list = color_definition_list;

		while (list) {
			g_free(list->data);
			list=list->next;
		}

		g_list_free(color_definition_list);
		color_definition_list = NULL;
	}

	return 0;
}
#endif

int
colors_use(WINDOW *w, enum color id)
{
	color_entry_t *entry = &colors[id];

	assert(id > 0 && id < COLOR_END);

	attr_t attrs;
	short pair;
	fix_wattr_get(w, &attrs, &pair, NULL);

#ifdef ENABLE_COLORS
	if (options.enable_colors) {
		/* color mode */
		if ((int)attrs != entry->color || (short)id != pair)
			wattr_set(w, entry->color, id, NULL);
	} else {
#endif
		/* mono mode */
		if ((int)attrs != entry->mono)
			(void)wattrset(w, entry->mono);
#ifdef ENABLE_COLORS
	}
#endif

	return 0;
}