Code

colors, utils: use g_list_free_full(g_free)
[ncmpc.git] / src / colors.c
index 2635329ec00ca40c6abc81dec7f34ea98993b2a9..5a79fd9bbfff8b2c6a7967e499af18df19c98544 100644 (file)
@@ -1,24 +1,26 @@
 /* ncmpc (Ncurses MPD Client)
- * (c) 2004-2009 The Music Player Daemon Project
+ * (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
  * 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
@@ -29,7 +31,7 @@
 #include <string.h>
 #include <glib.h>
 
-#define COLOR_NONE  -1
+#define COLOR_NONE  G_MININT /* left most bit only */
 #define COLOR_ERROR -2
 
 #ifdef ENABLE_COLORS
@@ -51,6 +53,7 @@ static color_entry_t colors[COLOR_END] = {
        [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},
@@ -70,93 +73,108 @@ 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)
 {
-       short fg, bg;
-
        assert(id > 0 && id < COLOR_END);
 
-       fg = colors[id].color;
-       bg = colors[COLOR_BACKGROUND].color;
+       int fg = colors[id].color;
+       int bg = colors[COLOR_BACKGROUND].color;
 
-       /* COLOR_NONE is negative, which
-        * results in a default colors */
-       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));
 }
 
 int
 colors_str2color(const char *str)
 {
-       int color;
-       char *endptr;
-
-       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_RED | A_BOLD;
-       else if (!strcasecmp(str, "brightgreen"))
-               return COLOR_GREEN | A_BOLD;
-       else if (!strcasecmp(str, "brightyellow"))
-               return COLOR_YELLOW | A_BOLD;
-       else if (!strcasecmp(str, "brightblue"))
-               return COLOR_BLUE | A_BOLD;
-       else if (!strcasecmp(str, "brightmagenta"))
-               return COLOR_MAGENTA | A_BOLD;
-       else if (!strcasecmp(str, "brightcyan"))
-               return COLOR_CYAN | A_BOLD;
-       else if (!strcasecmp(str, "brightwhite"))
-               return COLOR_WHITE | A_BOLD;
-       else if (!strcasecmp(str, "grey") || !strcasecmp(str, "gray"))
-               return COLOR_BLACK | A_BOLD;
-       else if (!strcasecmp(str, "none"))
-               return COLOR_NONE;
-
-       color = strtol(str, &endptr, 10);
-       if (str != endptr && endptr[0] == '\0')
-               return color;
-
-       fprintf(stderr,_("Warning: Unknown color - %s\n"), str);
-       return -2;
+       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;
+                       }
+               }
+
+       }
+       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
+bool
 colors_define(const char *name, short r, short g, short b)
 {
-       color_definition_entry_t *entry;
        int color = colors_str2color(name);
 
        if (color < 0)
-               return color;
+               return false;
 
-       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;
@@ -164,29 +182,29 @@ colors_define(const char *name, short r, short g, short b)
 
        color_definition_list = g_list_append(color_definition_list, entry);
 
-       return 0;
+       return true;
 }
 
-int
+bool
 colors_assign(const char *name, const char *value)
 {
        color_entry_t *entry = colors_lookup_by_name(name);
-       int color;
 
        if (!entry) {
-               fprintf(stderr,_("Warning: Unknown color field - %s\n"), name);
-               return -1;
+               fprintf(stderr, "%s: %s",
+                       _("Unknown color field"), name);
+               return false;
        }
 
-       if ((color = colors_str2color(value)) == COLOR_ERROR)
-               return -1;
+       const int color = colors_str2color(value);
+       if (color == COLOR_ERROR)
+               return false;
 
        entry->color = color;
-       return 0;
+       return true;
 }
 
-
-int
+void
 colors_start(void)
 {
        if (has_colors()) {
@@ -210,9 +228,7 @@ colors_start(void)
                                _("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);
                }
@@ -224,37 +240,28 @@ colors_start(void)
 
        /* 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);
+               g_list_free_full(color_definition_list, g_free);
                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 ((int)attrs != entry->mono || (short)id != pair)
-                       wattr_set(w, entry->mono, id, NULL);
+               if ((int)attrs != entry->color || (short)id != pair)
+                       wattr_set(w, entry->color, id, NULL);
        } else {
 #endif
                /* mono mode */
@@ -263,6 +270,4 @@ colors_use(WINDOW *w, enum color id)
 #ifdef ENABLE_COLORS
        }
 #endif
-
-       return 0;
 }