1 /* ncmpc (Ncurses MPD Client)
2 * (c) 2004-2017 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.
34 #define COLOR_NONE G_MININT /* left most bit only */
35 #define COLOR_ERROR -2
41 } color_definition_entry_t
;
50 static color_entry_t colors
[COLOR_END
] = {
51 /* color pair = field name, color, mono */
52 [COLOR_TITLE
] = {"title", COLOR_YELLOW
, A_NORMAL
},
53 [COLOR_TITLE_BOLD
] = {"title-bold", COLOR_YELLOW
| A_BOLD
, A_BOLD
},
54 [COLOR_LINE
] = {"line", COLOR_WHITE
, A_NORMAL
},
55 [COLOR_LINE_BOLD
] = {"line-bold", COLOR_WHITE
| A_BOLD
, A_BOLD
},
56 [COLOR_LINE_FLAGS
] = {"line-flags", COLOR_YELLOW
, A_NORMAL
},
57 [COLOR_LIST
] = {"list", COLOR_GREEN
, A_NORMAL
},
58 [COLOR_LIST_BOLD
] = {"list-bold", COLOR_GREEN
| A_BOLD
, A_BOLD
},
59 [COLOR_PROGRESSBAR
] = {"progressbar", COLOR_WHITE
, A_NORMAL
},
60 [COLOR_STATUS
] = {"status-song", COLOR_YELLOW
, A_NORMAL
},
61 [COLOR_STATUS_BOLD
] = {"status-state", COLOR_YELLOW
| A_BOLD
, A_BOLD
},
62 [COLOR_STATUS_TIME
] = {"status-time", COLOR_RED
, A_NORMAL
},
63 [COLOR_STATUS_ALERT
] = {"alert", COLOR_RED
| A_BOLD
, A_BOLD
},
64 [COLOR_DIRECTORY
] = {"browser-directory", COLOR_YELLOW
, A_NORMAL
},
65 [COLOR_PLAYLIST
] = {"browser-playlist", COLOR_RED
, A_NORMAL
},
66 [COLOR_BACKGROUND
] = {"background", COLOR_BLACK
, A_NORMAL
},
71 static GList
*color_definition_list
= NULL
;
73 static color_entry_t
*
74 colors_lookup_by_name(const char *name
)
76 for (enum color i
= 1; i
< COLOR_END
; ++i
)
77 if (!strcasecmp(colors
[i
].name
, name
))
84 colors_update_pair(enum color id
)
86 assert(id
> 0 && id
< COLOR_END
);
88 int fg
= colors
[id
].color
;
89 int bg
= colors
[COLOR_BACKGROUND
].color
;
91 /* If color == COLOR_NONE (negative),
92 * pass -1 to avoid cast errors */
99 colors_str2color(const char *str
)
102 char **parts
= g_strsplit(str
, ",", 0);
103 for (int i
= 0; parts
[i
]; i
++) {
104 char *cur
= parts
[i
];
106 /* Legacy colors (brightblue,etc) */
107 if (!strncasecmp(cur
, "bright", 6)) {
113 if (!strcasecmp(cur
, "none"))
115 else if (!strcasecmp(cur
, "black"))
116 color
|= COLOR_BLACK
;
117 else if (!strcasecmp(cur
, "red"))
119 else if (!strcasecmp(cur
, "green"))
120 color
|= COLOR_GREEN
;
121 else if (!strcasecmp(cur
, "yellow"))
122 color
|= COLOR_YELLOW
;
123 else if (!strcasecmp(cur
, "blue"))
125 else if (!strcasecmp(cur
, "magenta"))
126 color
|= COLOR_MAGENTA
;
127 else if (!strcasecmp(cur
, "cyan"))
129 else if (!strcasecmp(cur
, "white"))
130 color
|= COLOR_WHITE
;
131 else if (!strcasecmp(cur
, "grey") || !strcasecmp(cur
, "gray"))
132 color
|= COLOR_BLACK
| A_BOLD
;
135 else if (!strcasecmp(cur
, "standout"))
137 else if (!strcasecmp(cur
, "underline"))
138 color
|= A_UNDERLINE
;
139 else if (!strcasecmp(cur
, "reverse"))
141 else if (!strcasecmp(cur
, "blink"))
143 else if (!strcasecmp(cur
, "dim"))
145 else if (!strcasecmp(cur
, "bold"))
148 /* Numerical colors */
150 int tmp
= strtol(cur
, &endptr
, 10);
151 if (cur
!= endptr
&& endptr
[0] == '\0') {
154 fprintf(stderr
, "%s: %s\n",
155 _("Unknown color"), str
);
165 /* This function is called from conf.c before curses have been started,
166 * it adds the definition to the color_definition_list and init_color() is
167 * done in colors_start() */
169 colors_define(const char *name
, short r
, short g
, short b
)
171 int color
= colors_str2color(name
);
176 color_definition_entry_t
*entry
=
177 g_malloc(sizeof(color_definition_entry_t
));
178 entry
->color
= color
;
183 color_definition_list
= g_list_append(color_definition_list
, entry
);
189 colors_assign(const char *name
, const char *value
)
191 color_entry_t
*entry
= colors_lookup_by_name(name
);
194 fprintf(stderr
, "%s: %s",
195 _("Unknown color field"), name
);
199 const int color
= colors_str2color(value
);
200 if (color
== COLOR_ERROR
)
203 entry
->color
= color
;
211 /* initialize color support */
213 use_default_colors();
214 /* define any custom colors defined in the configuration file */
215 if (color_definition_list
&& can_change_color()) {
216 GList
*list
= color_definition_list
;
219 color_definition_entry_t
*entry
= list
->data
;
221 if (entry
->color
<= COLORS
)
222 init_color(entry
->color
, entry
->r
,
226 } else if (color_definition_list
&& !can_change_color())
227 fprintf(stderr
, "%s\n",
228 _("Terminal lacks support for changing colors"));
230 if (options
.enable_colors
) {
231 for (enum color i
= 1; i
< COLOR_END
; ++i
)
232 /* update the color pairs */
233 colors_update_pair(i
);
235 } else if (options
.enable_colors
) {
236 fprintf(stderr
, "%s\n",
237 _("Terminal lacks color capabilities"));
238 options
.enable_colors
= 0;
241 /* free the color_definition_list */
242 if (color_definition_list
) {
243 g_list_free_full(color_definition_list
, g_free
);
244 color_definition_list
= NULL
;
250 colors_use(WINDOW
*w
, enum color id
)
252 color_entry_t
*entry
= &colors
[id
];
254 assert(id
> 0 && id
< COLOR_END
);
258 fix_wattr_get(w
, &attrs
, &pair
, NULL
);
261 if (options
.enable_colors
) {
263 if ((int)attrs
!= entry
->color
|| (short)id
!= pair
)
264 wattr_set(w
, entry
->color
, id
, NULL
);
268 if ((int)attrs
!= entry
->mono
)
269 (void)wattrset(w
, entry
->mono
);