X-Git-Url: https://git.tokkee.org/?a=blobdiff_plain;f=src%2Fcolors.c;h=1cf4c973cfc63c933b221b4384041764eb51beac;hb=92cb9c45ac41c2e1f7a9517a66e239ee2cb8ccc4;hp=0bcf2782c8a76677122d32f3d6cfdd3af2bb426c;hpb=88af2c3ace0fa42dbb816cc9fe8c4b54ce89233a;p=ncmpc.git diff --git a/src/colors.c b/src/colors.c index 0bcf278..1cf4c97 100644 --- a/src/colors.c +++ b/src/colors.c @@ -1,5 +1,6 @@ -/* - * (c) 2004 by Kalle Wallin +/* ncmpc (Ncurses MPD Client) + * (c) 2004-2017 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 @@ -10,14 +11,16 @@ * 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., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA * + * 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 @@ -28,32 +31,8 @@ #include #include -#define COLOR_BRIGHT_MASK (1<<7) - -#define COLOR_BRIGHT_BLACK (COLOR_BLACK | COLOR_BRIGHT_MASK) -#define COLOR_BRIGHT_RED (COLOR_RED | COLOR_BRIGHT_MASK) -#define COLOR_BRIGHT_GREEN (COLOR_GREEN | COLOR_BRIGHT_MASK) -#define COLOR_BRIGHT_YELLOW (COLOR_YELLOW | COLOR_BRIGHT_MASK) -#define COLOR_BRIGHT_BLUE (COLOR_BLUE | COLOR_BRIGHT_MASK) -#define COLOR_BRIGHT_MAGENTA (COLOR_MAGENTA | COLOR_BRIGHT_MASK) -#define COLOR_BRIGHT_CYAN (COLOR_CYAN | COLOR_BRIGHT_MASK) -#define COLOR_BRIGHT_WHITE (COLOR_WHITE | COLOR_BRIGHT_MASK) - -#define IS_BRIGHT(color) (color & COLOR_BRIGHT_MASK) - -/* name of the color fields */ -#define NAME_TITLE "title" -#define NAME_TITLE_BOLD "title-bold" -#define NAME_LINE "line" -#define NAME_LINE_BOLD "line-flags" -#define NAME_LIST "list" -#define NAME_LIST_BOLD "list-bold" -#define NAME_PROGRESS "progressbar" -#define NAME_STATUS "status-song" -#define NAME_STATUS_BOLD "status-state" -#define NAME_STATUS_TIME "status-time" -#define NAME_ALERT "alert" -#define NAME_BGCOLOR "background" +#define COLOR_NONE G_MININT /* left most bit only */ +#define COLOR_ERROR -2 #ifdef ENABLE_COLORS typedef struct { @@ -64,104 +43,123 @@ typedef struct { typedef struct { const char *name; - short fg; - attr_t attrs; + int color; + int mono; } color_entry_t; static color_entry_t colors[COLOR_END] = { - /* color pair = field name, color, mono attribute */ - [COLOR_TITLE] = { NAME_TITLE, COLOR_YELLOW, A_NORMAL }, - [COLOR_TITLE_BOLD] = { NAME_TITLE_BOLD, COLOR_BRIGHT_YELLOW, A_BOLD }, - [COLOR_LINE] = { NAME_LINE, COLOR_WHITE, A_NORMAL }, - [COLOR_LINE_BOLD] = { NAME_LINE_BOLD, COLOR_BRIGHT_WHITE, A_BOLD }, - [COLOR_LIST] = { NAME_LIST, COLOR_GREEN, A_NORMAL }, - [COLOR_LIST_BOLD] = { NAME_LIST_BOLD, COLOR_BRIGHT_GREEN, A_BOLD }, - [COLOR_PROGRESSBAR] = { NAME_PROGRESS, COLOR_WHITE, A_NORMAL }, - [COLOR_STATUS] = { NAME_STATUS, COLOR_YELLOW, A_NORMAL }, - [COLOR_STATUS_BOLD] = { NAME_STATUS_BOLD, COLOR_BRIGHT_YELLOW, A_BOLD }, - [COLOR_STATUS_TIME] = { NAME_STATUS_TIME, COLOR_RED, A_NORMAL }, - [COLOR_STATUS_ALERT] = { NAME_ALERT, COLOR_BRIGHT_RED, A_BOLD }, + /* 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 -/* background color */ -static short bg = COLOR_BLACK; - static GList *color_definition_list = NULL; static color_entry_t * colors_lookup_by_name(const char *name) { - enum color i; - - for (i = 1; i < COLOR_END; ++i) + for (enum color i = 1; i < COLOR_END; ++i) if (!strcasecmp(colors[i].name, name)) return &colors[i]; return NULL; } -static int +static void colors_update_pair(enum color id) { - color_entry_t *entry = &colors[id]; - short fg = -1; - assert(id > 0 && id < COLOR_END); - if (IS_BRIGHT(entry->fg)) { - entry->attrs = A_BOLD; - fg = entry->fg & ~COLOR_BRIGHT_MASK; - } else { - entry->attrs = A_NORMAL; - fg = entry->fg; - } + int fg = colors[id].color; + int bg = colors[COLOR_BACKGROUND].color; - init_pair(id, fg, bg); - return 0; + /* If color == COLOR_NONE (negative), + * pass -1 to avoid cast errors */ + init_pair(id, + (fg < 0 ? -1 : fg), + (bg < 0 ? -1 : bg)); } -short +int colors_str2color(const char *str) { - if (!strcasecmp(str, "black")) - return COLOR_BLACK; - else if (!strcasecmp(str, "red")) - return COLOR_RED; - else if (!strcasecmp(str, "green")) - return COLOR_GREEN; - else if (!strcasecmp(str, "yellow")) - return COLOR_YELLOW; - else if (!strcasecmp(str, "blue")) - return COLOR_BLUE; - else if (!strcasecmp(str, "magenta")) - return COLOR_MAGENTA; - else if (!strcasecmp(str, "cyan")) - return COLOR_CYAN; - else if (!strcasecmp(str, "white")) - return COLOR_WHITE; - else if (!strcasecmp(str, "brightred")) - return COLOR_BRIGHT_RED; - else if (!strcasecmp(str, "brightgreen")) - return COLOR_BRIGHT_GREEN; - else if (!strcasecmp(str, "brightyellow")) - return COLOR_BRIGHT_YELLOW; - else if (!strcasecmp(str, "brightblue")) - return COLOR_BRIGHT_BLUE; - else if (!strcasecmp(str, "brightmagenta")) - return COLOR_BRIGHT_MAGENTA; - else if (!strcasecmp(str, "brightcyan")) - return COLOR_BRIGHT_CYAN; - else if (!strcasecmp(str, "brightwhite")) - return COLOR_BRIGHT_WHITE; - else if (!strcasecmp(str, "grey") || !strcasecmp(str, "gray")) - return COLOR_BRIGHT_BLACK; - else if (!strcasecmp(str, "none")) - return -1; + 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, "%s: %s\n", + _("Unknown color"), str); + return COLOR_ERROR; + } + } - fprintf(stderr,_("Warning: Unknown color - %s\n"), str); - return -2; + } + g_strfreev(parts); + return color; } /* This function is called from conf.c before curses have been started, @@ -170,13 +168,13 @@ colors_str2color(const char *str) int colors_define(const char *name, short r, short g, short b) { - color_definition_entry_t *entry; - short color = colors_str2color(name); + int color = colors_str2color(name); if (color < 0) - return -1; + return color; - entry = g_malloc(sizeof(color_definition_entry_t)); + color_definition_entry_t *entry = + g_malloc(sizeof(color_definition_entry_t)); entry->color = color; entry->r = r; entry->g = g; @@ -191,31 +189,22 @@ int colors_assign(const char *name, const char *value) { color_entry_t *entry = colors_lookup_by_name(name); - short color; if (!entry) { - if (!strcasecmp(NAME_BGCOLOR, name)) { - if ((color = colors_str2color(value)) < -1) - return -1; - if (color > COLORS) - color = color & ~COLOR_BRIGHT_MASK; - bg = color; - return 0; - } - - fprintf(stderr,_("Warning: Unknown color field - %s\n"), name); + fprintf(stderr, "%s: %s", + _("Unknown color field"), name); return -1; } - if ((color = colors_str2color(value)) < -1) + const int color = colors_str2color(value); + if (color == COLOR_ERROR) return -1; - entry->fg = color; + entry->color = color; return 0; } - -int +void colors_start(void) { if (has_colors()) { @@ -235,17 +224,17 @@ colors_start(void) list = list->next; } } else if (color_definition_list && !can_change_color()) - fprintf(stderr, _("Terminal lacks support for changing colors!\n")); + fprintf(stderr, "%s\n", + _("Terminal lacks support for changing colors")); if (options.enable_colors) { - enum color i; - - for (i = 1; i < COLOR_END; ++i) + for (enum color i = 1; i < COLOR_END; ++i) /* update the color pairs */ colors_update_pair(i); } } else if (options.enable_colors) { - fprintf(stderr, _("Terminal lacks color capabilities!\n")); + fprintf(stderr, "%s\n", + _("Terminal lacks color capabilities")); options.enable_colors = 0; } @@ -261,35 +250,31 @@ colors_start(void) g_list_free(color_definition_list); color_definition_list = NULL; } - - return 0; } #endif -int +void colors_use(WINDOW *w, enum color id) { color_entry_t *entry = &colors[id]; - short pair; - attr_t attrs; assert(id > 0 && id < COLOR_END); - wattr_get(w, &attrs, &pair, NULL); + attr_t attrs; + short pair; + fix_wattr_get(w, &attrs, &pair, NULL); #ifdef ENABLE_COLORS if (options.enable_colors) { /* color mode */ - if (attrs != entry->attrs || (short)id != pair) - wattr_set(w, entry->attrs, id, NULL); + if ((int)attrs != entry->color || (short)id != pair) + wattr_set(w, entry->color, id, NULL); } else { #endif /* mono mode */ - if (attrs != entry->attrs) - wattrset(w, entry->attrs); + if ((int)attrs != entry->mono) + (void)wattrset(w, entry->mono); #ifdef ENABLE_COLORS } #endif - - return 0; }