root/lib/tty/color.c

/* [previous][next][first][last][top][bottom][index][help]  */

DEFINITIONS

This source file includes following definitions.
  1. tty_color_free_condition_cb
  2. tty_color_free_all
  3. tty_color_get_next_cpn_cb
  4. tty_color_get_next__color_pair_number
  5. tty_init_colors
  6. tty_colors_done
  7. tty_use_colors
  8. tty_try_alloc_color_pair2
  9. tty_try_alloc_color_pair
  10. tty_color_free_all_tmp
  11. tty_color_free_all_non_tmp
  12. tty_color_set_defaults

   1 /*
   2    Color setup.
   3    Interface functions.
   4 
   5    Copyright (C) 1994-2019
   6    Free Software Foundation, Inc.
   7 
   8    Written by:
   9    Andrew Borodin <aborodin@vmail.ru>, 2009
  10    Slava Zanko <slavazanko@gmail.com>, 2009
  11    Egmont Koblinger <egmont@gmail.com>, 2010
  12 
  13    This file is part of the Midnight Commander.
  14 
  15    The Midnight Commander is free software: you can redistribute it
  16    and/or modify it under the terms of the GNU General Public License as
  17    published by the Free Software Foundation, either version 3 of the License,
  18    or (at your option) any later version.
  19 
  20    The Midnight Commander is distributed in the hope that it will be useful,
  21    but WITHOUT ANY WARRANTY; without even the implied warranty of
  22    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  23    GNU General Public License for more details.
  24 
  25    You should have received a copy of the GNU General Public License
  26    along with this program.  If not, see <http://www.gnu.org/licenses/>.
  27  */
  28 
  29 /** \file color.c
  30  *  \brief Source: color setup
  31  */
  32 
  33 #include <config.h>
  34 
  35 #include <stdio.h>
  36 #include <stdlib.h>
  37 #include <string.h>
  38 #include <sys/types.h>          /* size_t */
  39 
  40 #include "lib/global.h"
  41 
  42 #include "tty.h"
  43 #include "color.h"
  44 
  45 #include "color-internal.h"
  46 
  47 /*** global variables ****************************************************************************/
  48 
  49 static char *tty_color_defaults__fg = NULL;
  50 static char *tty_color_defaults__bg = NULL;
  51 static char *tty_color_defaults__attrs = NULL;
  52 
  53 /* Set if we are actually using colors */
  54 gboolean use_colors = FALSE;
  55 
  56 /*** file scope macro definitions ****************************************************************/
  57 
  58 /*** file scope type declarations ****************************************************************/
  59 
  60 /*** file scope variables ************************************************************************/
  61 
  62 static GHashTable *mc_tty_color__hashtable = NULL;
  63 
  64 /*** file scope functions ************************************************************************/
  65 /* --------------------------------------------------------------------------------------------- */
  66 
  67 static gboolean
  68 tty_color_free_condition_cb (gpointer key, gpointer value, gpointer user_data)
     /* [previous][next][first][last][top][bottom][index][help]  */
  69 {
  70     gboolean is_temp_color;
  71     tty_color_pair_t *mc_color_pair;
  72     (void) key;
  73 
  74     is_temp_color = user_data != NULL;
  75     mc_color_pair = (tty_color_pair_t *) value;
  76     return (mc_color_pair->is_temp == is_temp_color);
  77 }
  78 
  79 /* --------------------------------------------------------------------------------------------- */
  80 
  81 static void
  82 tty_color_free_all (gboolean is_temp_color)
     /* [previous][next][first][last][top][bottom][index][help]  */
  83 {
  84     g_hash_table_foreach_remove (mc_tty_color__hashtable, tty_color_free_condition_cb,
  85                                  is_temp_color ? GSIZE_TO_POINTER (1) : NULL);
  86 }
  87 
  88 /* --------------------------------------------------------------------------------------------- */
  89 
  90 static gboolean
  91 tty_color_get_next_cpn_cb (gpointer key, gpointer value, gpointer user_data)
     /* [previous][next][first][last][top][bottom][index][help]  */
  92 {
  93     size_t cp;
  94     tty_color_pair_t *mc_color_pair;
  95     (void) key;
  96 
  97     cp = GPOINTER_TO_SIZE (user_data);
  98     mc_color_pair = (tty_color_pair_t *) value;
  99 
 100     return (cp == mc_color_pair->pair_index);
 101 }
 102 
 103 /* --------------------------------------------------------------------------------------------- */
 104 
 105 static size_t
 106 tty_color_get_next__color_pair_number (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 107 {
 108     size_t cp_count, cp;
 109 
 110     cp_count = g_hash_table_size (mc_tty_color__hashtable);
 111     for (cp = 0; cp < cp_count; cp++)
 112         if (g_hash_table_find (mc_tty_color__hashtable, tty_color_get_next_cpn_cb,
 113                                GSIZE_TO_POINTER (cp)) == NULL)
 114             break;
 115 
 116     return cp;
 117 }
 118 
 119 /* --------------------------------------------------------------------------------------------- */
 120 /*** public functions ****************************************************************************/
 121 /* --------------------------------------------------------------------------------------------- */
 122 
 123 void
 124 tty_init_colors (gboolean disable, gboolean force)
     /* [previous][next][first][last][top][bottom][index][help]  */
 125 {
 126     tty_color_init_lib (disable, force);
 127     mc_tty_color__hashtable = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
 128 }
 129 
 130 /* --------------------------------------------------------------------------------------------- */
 131 
 132 void
 133 tty_colors_done (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 134 {
 135     tty_color_deinit_lib ();
 136     g_free (tty_color_defaults__fg);
 137     g_free (tty_color_defaults__bg);
 138     g_free (tty_color_defaults__attrs);
 139 
 140     g_hash_table_destroy (mc_tty_color__hashtable);
 141 }
 142 
 143 /* --------------------------------------------------------------------------------------------- */
 144 
 145 gboolean
 146 tty_use_colors (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 147 {
 148     return use_colors;
 149 }
 150 
 151 /* --------------------------------------------------------------------------------------------- */
 152 
 153 int
 154 tty_try_alloc_color_pair2 (const char *fg, const char *bg, const char *attrs,
     /* [previous][next][first][last][top][bottom][index][help]  */
 155                            gboolean is_temp_color)
 156 {
 157     gchar *color_pair;
 158     tty_color_pair_t *mc_color_pair;
 159     int ifg, ibg, attr;
 160 
 161     if (fg == NULL || !strcmp (fg, "base"))
 162         fg = tty_color_defaults__fg;
 163     if (bg == NULL || !strcmp (bg, "base"))
 164         bg = tty_color_defaults__bg;
 165     if (attrs == NULL || !strcmp (attrs, "base"))
 166         attrs = tty_color_defaults__attrs;
 167 
 168     ifg = tty_color_get_index_by_name (fg);
 169     ibg = tty_color_get_index_by_name (bg);
 170     attr = tty_attr_get_bits (attrs);
 171 
 172     color_pair = g_strdup_printf ("%d.%d.%d", ifg, ibg, attr);
 173     if (color_pair == NULL)
 174         return 0;
 175 
 176     mc_color_pair =
 177         (tty_color_pair_t *) g_hash_table_lookup (mc_tty_color__hashtable, (gpointer) color_pair);
 178 
 179     if (mc_color_pair != NULL)
 180     {
 181         g_free (color_pair);
 182         return mc_color_pair->pair_index;
 183     }
 184 
 185     mc_color_pair = g_try_new0 (tty_color_pair_t, 1);
 186     if (mc_color_pair == NULL)
 187     {
 188         g_free (color_pair);
 189         return 0;
 190     }
 191 
 192     mc_color_pair->is_temp = is_temp_color;
 193     mc_color_pair->ifg = ifg;
 194     mc_color_pair->ibg = ibg;
 195     mc_color_pair->attr = attr;
 196     mc_color_pair->pair_index = tty_color_get_next__color_pair_number ();
 197 
 198     tty_color_try_alloc_pair_lib (mc_color_pair);
 199 
 200     g_hash_table_insert (mc_tty_color__hashtable, (gpointer) color_pair, (gpointer) mc_color_pair);
 201 
 202     return mc_color_pair->pair_index;
 203 }
 204 
 205 /* --------------------------------------------------------------------------------------------- */
 206 
 207 int
 208 tty_try_alloc_color_pair (const char *fg, const char *bg, const char *attrs)
     /* [previous][next][first][last][top][bottom][index][help]  */
 209 {
 210     return tty_try_alloc_color_pair2 (fg, bg, attrs, TRUE);
 211 }
 212 
 213 /* --------------------------------------------------------------------------------------------- */
 214 
 215 void
 216 tty_color_free_all_tmp (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 217 {
 218     tty_color_free_all (TRUE);
 219 }
 220 
 221 /* --------------------------------------------------------------------------------------------- */
 222 
 223 void
 224 tty_color_free_all_non_tmp (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 225 {
 226     tty_color_free_all (FALSE);
 227 }
 228 
 229 /* --------------------------------------------------------------------------------------------- */
 230 
 231 void
 232 tty_color_set_defaults (const char *fgcolor, const char *bgcolor, const char *attrs)
     /* [previous][next][first][last][top][bottom][index][help]  */
 233 {
 234     g_free (tty_color_defaults__fg);
 235     g_free (tty_color_defaults__bg);
 236     g_free (tty_color_defaults__attrs);
 237 
 238     tty_color_defaults__fg = g_strdup (fgcolor);
 239     tty_color_defaults__bg = g_strdup (bgcolor);
 240     tty_color_defaults__attrs = g_strdup (attrs);
 241 }
 242 
 243 /* --------------------------------------------------------------------------------------------- */

/* [previous][next][first][last][top][bottom][index][help]  */