1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2009 The Music Player Daemon Project
3 * Project homepage: http://musicpd.org
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License along
16 * with this program; if not, write to the Free Software Foundation, Inc.,
17 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
32 #define COLOR_NONE G_MININT /* left most bit only */
33 #define COLOR_ERROR -2
39 } color_definition_entry_t
;
48 static color_entry_t colors
[COLOR_END
] = {
49 /* color pair = field name, color, mono */
50 [COLOR_TITLE
] = {"title", COLOR_YELLOW
, A_NORMAL
},
51 [COLOR_TITLE_BOLD
] = {"title-bold", COLOR_YELLOW
| A_BOLD
, A_BOLD
},
52 [COLOR_LINE
] = {"line", COLOR_WHITE
, A_NORMAL
},
53 [COLOR_LINE_BOLD
] = {"line-bold", COLOR_WHITE
| A_BOLD
, A_BOLD
},
54 [COLOR_LIST
] = {"list", COLOR_GREEN
, A_NORMAL
},
55 [COLOR_LIST_BOLD
] = {"list-bold", COLOR_GREEN
| A_BOLD
, A_BOLD
},
56 [COLOR_PROGRESSBAR
] = {"progressbar", COLOR_WHITE
, A_NORMAL
},
57 [COLOR_STATUS
] = {"status-song", COLOR_YELLOW
, A_NORMAL
},
58 [COLOR_STATUS_BOLD
] = {"status-state", COLOR_YELLOW
| A_BOLD
, A_BOLD
},
59 [COLOR_STATUS_TIME
] = {"status-time", COLOR_RED
, A_NORMAL
},
60 [COLOR_STATUS_ALERT
] = {"alert", COLOR_RED
| A_BOLD
, A_BOLD
},
61 [COLOR_DIRECTORY
] = {"browser-directory", COLOR_YELLOW
, A_NORMAL
},
62 [COLOR_PLAYLIST
] = {"browser-playlist", COLOR_RED
, A_NORMAL
},
63 [COLOR_BACKGROUND
] = {"background", COLOR_BLACK
, A_NORMAL
},
68 static GList
*color_definition_list
= NULL
;
70 static color_entry_t
*
71 colors_lookup_by_name(const char *name
)
75 for (i
= 1; i
< COLOR_END
; ++i
)
76 if (!strcasecmp(colors
[i
].name
, name
))
83 colors_update_pair(enum color id
)
87 assert(id
> 0 && id
< COLOR_END
);
89 fg
= colors
[id
].color
;
90 bg
= colors
[COLOR_BACKGROUND
].color
;
92 /* If color == COLOR_NONE (negative),
93 * pass -1 to avoid cast errors */
101 colors_str2color(const char *str
)
104 char **parts
= g_strsplit(str
, ",", 0);
105 for (i
= 0; parts
[i
]; i
++) {
106 char *cur
= parts
[i
];
108 /* Legacy colors (brightblue,etc) */
109 if (!strncasecmp(cur
, "bright", 6)) {
115 if (!strcasecmp(cur
, "none"))
117 else if (!strcasecmp(cur
, "black"))
118 color
|= COLOR_BLACK
;
119 else if (!strcasecmp(cur
, "red"))
121 else if (!strcasecmp(cur
, "green"))
122 color
|= COLOR_GREEN
;
123 else if (!strcasecmp(cur
, "yellow"))
124 color
|= COLOR_YELLOW
;
125 else if (!strcasecmp(cur
, "blue"))
127 else if (!strcasecmp(cur
, "magenta"))
128 color
|= COLOR_MAGENTA
;
129 else if (!strcasecmp(cur
, "cyan"))
131 else if (!strcasecmp(cur
, "white"))
132 color
|= COLOR_WHITE
;
133 else if (!strcasecmp(cur
, "grey") || !strcasecmp(cur
, "gray"))
134 color
|= COLOR_BLACK
| A_BOLD
;
137 else if (!strcasecmp(cur
, "standout"))
139 else if (!strcasecmp(cur
, "underline"))
140 color
|= A_UNDERLINE
;
141 else if (!strcasecmp(cur
, "reverse"))
143 else if (!strcasecmp(cur
, "blink"))
145 else if (!strcasecmp(cur
, "dim"))
147 else if (!strcasecmp(cur
, "bold"))
150 /* Numerical colors */
152 int tmp
= strtol(cur
, &endptr
, 10);
153 if (cur
!= endptr
&& endptr
[0] == '\0') {
156 fprintf(stderr
,_("Warning: Unknown color - %s\n"), str
);
166 /* This function is called from conf.c before curses have been started,
167 * it adds the definition to the color_definition_list and init_color() is
168 * done in colors_start() */
170 colors_define(const char *name
, short r
, short g
, short b
)
172 color_definition_entry_t
*entry
;
173 int color
= colors_str2color(name
);
178 entry
= g_malloc(sizeof(color_definition_entry_t
));
179 entry
->color
= color
;
184 color_definition_list
= g_list_append(color_definition_list
, entry
);
190 colors_assign(const char *name
, const char *value
)
192 color_entry_t
*entry
= colors_lookup_by_name(name
);
196 fprintf(stderr
,_("Warning: Unknown color field - %s\n"), name
);
200 if ((color
= colors_str2color(value
)) == COLOR_ERROR
)
203 entry
->color
= color
;
212 /* initialize color support */
214 use_default_colors();
215 /* define any custom colors defined in the configuration file */
216 if (color_definition_list
&& can_change_color()) {
217 GList
*list
= color_definition_list
;
220 color_definition_entry_t
*entry
= list
->data
;
222 if (entry
->color
<= COLORS
)
223 init_color(entry
->color
, entry
->r
,
227 } else if (color_definition_list
&& !can_change_color())
228 fprintf(stderr
, "%s\n",
229 _("Terminal lacks support for changing colors"));
231 if (options
.enable_colors
) {
234 for (i
= 1; i
< COLOR_END
; ++i
)
235 /* update the color pairs */
236 colors_update_pair(i
);
238 } else if (options
.enable_colors
) {
239 fprintf(stderr
, "%s\n",
240 _("Terminal lacks color capabilities"));
241 options
.enable_colors
= 0;
244 /* free the color_definition_list */
245 if (color_definition_list
) {
246 GList
*list
= color_definition_list
;
253 g_list_free(color_definition_list
);
254 color_definition_list
= NULL
;
262 colors_use(WINDOW
*w
, enum color id
)
264 color_entry_t
*entry
= &colors
[id
];
268 assert(id
> 0 && id
< COLOR_END
);
270 wattr_get(w
, &attrs
, &pair
, NULL
);
273 if (options
.enable_colors
) {
275 if ((int)attrs
!= entry
->color
|| (short)id
!= pair
)
276 wattr_set(w
, entry
->color
, id
, NULL
);
280 if ((int)attrs
!= entry
->mono
)
281 (void)wattrset(w
, entry
->mono
);