"Fossies" - the Fresh Open Source Software Archive

Member "ncdc-1.22.1/src/ui_colors.c" (26 Mar 2019, 5062 Bytes) of package /linux/privat/ncdc-1.22.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file. For more information about "ui_colors.c" see the Fossies "Dox" file reference documentation and the last Fossies "Diffs" side-by-side code changes report: 1.20_vs_1.21.

    1 /* ncdc - NCurses Direct Connect client
    2 
    3   Copyright (c) 2011-2019 Yoran Heling
    4 
    5   Permission is hereby granted, free of charge, to any person obtaining
    6   a copy of this software and associated documentation files (the
    7   "Software"), to deal in the Software without restriction, including
    8   without limitation the rights to use, copy, modify, merge, publish,
    9   distribute, sublicense, and/or sell copies of the Software, and to
   10   permit persons to whom the Software is furnished to do so, subject to
   11   the following conditions:
   12 
   13   The above copyright notice and this permission notice shall be included
   14   in all copies or substantial portions of the Software.
   15 
   16   THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
   17   EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
   18   MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
   19   IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
   20   CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
   21   TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
   22   SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
   23 
   24 */
   25 
   26 #include "ncdc.h"
   27 #include "ui_colors.h"
   28 
   29 
   30 // colors
   31 
   32 #if INTERFACE
   33 
   34 #define COLOR_DEFAULT (-1)
   35 
   36 //  name            default  value
   37 #define UI_COLORS \
   38   C(list_default,   "default")\
   39   C(list_header,    "default,bold")\
   40   C(list_select,    "default,bold")\
   41   C(log_default,    "default")\
   42   C(log_highlight,  "yellow,bold")\
   43   C(log_join,       "cyan,bold")\
   44   C(log_nick,       "default")\
   45   C(log_ownnick,    "default,bold")\
   46   C(log_quit,       "cyan")\
   47   C(log_time,       "black,bold")\
   48   C(separator,      "default,reverse")\
   49   C(tab_active,     "default,bold")\
   50   C(tabprio_high,   "magenta,bold")\
   51   C(tabprio_low,    "black,bold")\
   52   C(tabprio_med,    "cyan,bold")\
   53   C(title,          "default,reverse")
   54 
   55 enum ui_coltype {
   56 #define C(n, d) UIC_##n,
   57   UI_COLORS
   58 #undef C
   59   UIC_NONE
   60 };
   61 
   62 
   63 struct ui_color_t {
   64   int var;
   65   short fg, bg, d_fg, d_bg;
   66   int x, d_x, a;
   67 };
   68 
   69 struct ui_attr_t {
   70   char name[11];
   71   gboolean color : 1;
   72   int attr;
   73 }
   74 
   75 struct ui_cursor_t {
   76   int x;
   77   int y;
   78 }
   79 
   80 #define UIC(n) (ui_colors[(ui_coltype)UIC_##n].a)
   81 
   82 #endif // INTERFACE
   83 
   84 
   85 ui_color_t ui_colors[] = {
   86 #define C(n, d) { VAR_color_##n },
   87   UI_COLORS
   88 #undef C
   89   { -1 }
   90 };
   91 
   92 
   93 ui_attr_t ui_attr_names[] = {
   94   { "black",     TRUE,  COLOR_BLACK   },
   95   { "blink",     FALSE, A_BLINK       },
   96   { "blue",      TRUE,  COLOR_BLUE    },
   97   { "bold",      FALSE, A_BOLD        },
   98   { "cyan",      TRUE,  COLOR_CYAN    },
   99   { "default",   TRUE,  COLOR_DEFAULT },
  100   { "green",     TRUE,  COLOR_GREEN   },
  101   { "magenta",   TRUE,  COLOR_MAGENTA },
  102   { "red",       TRUE,  COLOR_RED     },
  103   { "reverse",   FALSE, A_REVERSE     },
  104   { "underline", FALSE, A_UNDERLINE   },
  105   { "white",     TRUE,  COLOR_WHITE   },
  106   { "yellow",    TRUE,  COLOR_YELLOW  },
  107   { "" }
  108 };
  109 
  110 
  111 static ui_attr_t *ui_attr_by_name(const char *n) {
  112   ui_attr_t *a = ui_attr_names;
  113   for(; *a->name; a++)
  114     if(strcmp(a->name, n) == 0)
  115       return a;
  116   return NULL;
  117 }
  118 
  119 
  120 static char *ui_name_by_attr(int n) {
  121   ui_attr_t *a = ui_attr_names;
  122   for(; *a->name; a++)
  123     if(a->attr == n)
  124       return a->name;
  125   return NULL;
  126 }
  127 
  128 
  129 gboolean ui_color_str_parse(const char *str, short *fg, short *bg, int *x, GError **err) {
  130   int state = 0; // 0 = no fg, 1 = no bg, 2 = both
  131   short f = COLOR_DEFAULT, b = COLOR_DEFAULT;
  132   int a = 0;
  133   char **args = g_strsplit(str, ",", 0);
  134   char **arg = args;
  135   for(; arg && *arg; arg++) {
  136     g_strstrip(*arg);
  137     if(!**arg)
  138       continue;
  139     ui_attr_t *attr = ui_attr_by_name(*arg);
  140     if(!attr) {
  141       g_set_error(err, 1, 0, "Unknown color or attribute: %s", *arg);
  142       g_strfreev(args);
  143       return FALSE;
  144     }
  145     if(!attr->color)
  146       a |= attr->attr;
  147     else if(!state) {
  148       f = attr->attr;
  149       state++;
  150     } else if(state == 1) {
  151       b = attr->attr;
  152       state++;
  153     } else {
  154       g_set_error(err, 1, 0, "Don't know what to do with a third color: %s", *arg);
  155       g_strfreev(args);
  156       return FALSE;
  157     }
  158   }
  159   g_strfreev(args);
  160   if(fg) *fg = f;
  161   if(bg) *bg = b;
  162   if(x)  *x  = a;
  163   return TRUE;
  164 }
  165 
  166 
  167 char *ui_color_str_gen(int fd, int bg, int x) {
  168   static char buf[100]; // must be smaller than (max_color_name * 2) + (max_attr_name * 3) + 6
  169   strcpy(buf, ui_name_by_attr(fd));
  170   if(bg != COLOR_DEFAULT) {
  171     strcat(buf, ",");
  172     strcat(buf, ui_name_by_attr(bg));
  173   }
  174   ui_attr_t *attr = ui_attr_names;
  175   for(; attr->name[0]; attr++)
  176     if(!attr->color && x & attr->attr) {
  177       strcat(buf, ",");
  178       strcat(buf, attr->name);
  179     }
  180   return buf;
  181 }
  182 
  183 
  184 // TODO: re-use color pairs when we have too many (>64) color groups
  185 void ui_colors_update() {
  186   int pair = 0;
  187   ui_color_t *c = ui_colors;
  188   for(; c->var>=0; c++) {
  189     g_warn_if_fail(ui_color_str_parse(var_get(0, c->var), &c->fg, &c->bg, &c->x, NULL));
  190     init_pair(++pair, c->fg, c->bg);
  191     c->a = c->x | COLOR_PAIR(pair);
  192   }
  193 }
  194 
  195 
  196 void ui_colors_init() {
  197   if(!has_colors())
  198     return;
  199 
  200   start_color();
  201   use_default_colors();
  202 
  203   ui_colors_update();
  204 }