Manual pages: mcmcdiffmceditmcview

root/lib/tty/color.c

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

DEFINITIONS

This source file includes following definitions.
  1. tty_color_free_temp_cb
  2. tty_color_get_next_cpn_cb
  3. tty_color_get_next__color_pair_number
  4. tty_init_colors
  5. tty_colors_done
  6. tty_use_colors
  7. tty_try_alloc_color_pair
  8. tty_color_free_temp
  9. tty_color_free_all

   1 /*
   2    Color setup.
   3    Interface functions.
   4 
   5    Copyright (C) 1994-2025
   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 <https://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 #include "lib/util.h"  // MC_PTR_FREE
  42 
  43 #include "tty.h"
  44 #include "color.h"
  45 
  46 #include "color-internal.h"
  47 
  48 /*** global variables ****************************************************************************/
  49 
  50 int *tty_color_role_to_pair = NULL;
  51 
  52 /* Set if we are actually using colors */
  53 gboolean use_colors = FALSE;
  54 
  55 gboolean need_convert_256color = FALSE;
  56 
  57 /*** file scope macro definitions ****************************************************************/
  58 
  59 /*** file scope type declarations ****************************************************************/
  60 
  61 /*** forward declarations (file scope functions) *************************************************/
  62 
  63 /*** file scope variables ************************************************************************/
  64 
  65 static GHashTable *mc_tty_color__hashtable = NULL;
  66 
  67 /* --------------------------------------------------------------------------------------------- */
  68 /*** file scope functions ************************************************************************/
  69 /* --------------------------------------------------------------------------------------------- */
  70 
  71 static gboolean
  72 tty_color_free_temp_cb (gpointer key, gpointer value, gpointer user_data)
     /* [previous][next][first][last][top][bottom][index][help]  */
  73 {
  74     (void) key;
  75     (void) user_data;
  76 
  77     return ((tty_color_lib_pair_t *) value)->is_temp;
  78 }
  79 
  80 /* --------------------------------------------------------------------------------------------- */
  81 
  82 static gboolean
  83 tty_color_get_next_cpn_cb (gpointer key, gpointer value, gpointer user_data)
     /* [previous][next][first][last][top][bottom][index][help]  */
  84 {
  85     tty_color_lib_pair_t *mc_color_pair = (tty_color_lib_pair_t *) value;
  86     size_t cp = GPOINTER_TO_SIZE (user_data);
  87 
  88     (void) key;
  89 
  90     return (cp == mc_color_pair->pair_index);
  91 }
  92 
  93 /* --------------------------------------------------------------------------------------------- */
  94 
  95 static size_t
  96 tty_color_get_next__color_pair_number (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
  97 {
  98     size_t cp_count, cp;
  99 
 100     cp_count = g_hash_table_size (mc_tty_color__hashtable);
 101     for (cp = 0; cp < cp_count; cp++)
 102         if (g_hash_table_find (mc_tty_color__hashtable, tty_color_get_next_cpn_cb,
 103                                GSIZE_TO_POINTER (cp))
 104             == NULL)
 105             break;
 106 
 107     return cp;
 108 }
 109 
 110 /* --------------------------------------------------------------------------------------------- */
 111 /*** public functions ****************************************************************************/
 112 /* --------------------------------------------------------------------------------------------- */
 113 
 114 void
 115 tty_init_colors (gboolean disable, gboolean force, int color_map_size)
     /* [previous][next][first][last][top][bottom][index][help]  */
 116 {
 117     tty_color_role_to_pair = g_new (int, color_map_size);
 118     tty_color_init_lib (disable, force);
 119     mc_tty_color__hashtable = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
 120 }
 121 
 122 /* --------------------------------------------------------------------------------------------- */
 123 
 124 void
 125 tty_colors_done (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 126 {
 127     tty_color_deinit_lib ();
 128     g_hash_table_destroy (mc_tty_color__hashtable);
 129     MC_PTR_FREE (tty_color_role_to_pair);
 130 }
 131 
 132 /* --------------------------------------------------------------------------------------------- */
 133 
 134 gboolean
 135 tty_use_colors (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 136 {
 137     return use_colors;
 138 }
 139 
 140 /* --------------------------------------------------------------------------------------------- */
 141 
 142 int
 143 tty_try_alloc_color_pair (const tty_color_pair_t *color, gboolean is_temp)
     /* [previous][next][first][last][top][bottom][index][help]  */
 144 {
 145     gchar *color_pair;
 146     tty_color_lib_pair_t *mc_color_pair;
 147     int ifg, ibg, attr;
 148 
 149     ifg = tty_color_get_index_by_name (color->fg);
 150     ibg = tty_color_get_index_by_name (color->bg);
 151     attr = tty_attr_get_bits (color->attrs);
 152 
 153     color_pair = g_strdup_printf ("%d.%d.%d", ifg, ibg, attr);
 154     if (color_pair == NULL)
 155         return 0;
 156 
 157     mc_color_pair = (tty_color_lib_pair_t *) g_hash_table_lookup (mc_tty_color__hashtable,
 158                                                                   (gpointer) color_pair);
 159 
 160     if (mc_color_pair != NULL)
 161     {
 162         g_free (color_pair);
 163         return mc_color_pair->pair_index;
 164     }
 165 
 166     if (need_convert_256color)
 167     {
 168         if ((ifg & FLAG_TRUECOLOR) == 0)
 169             ifg = convert_256color_to_truecolor (ifg);
 170 
 171         if ((ibg & FLAG_TRUECOLOR) == 0)
 172             ibg = convert_256color_to_truecolor (ibg);
 173     }
 174 
 175     mc_color_pair = g_try_new0 (tty_color_lib_pair_t, 1);
 176     if (mc_color_pair == NULL)
 177     {
 178         g_free (color_pair);
 179         return 0;
 180     }
 181 
 182     mc_color_pair->is_temp = is_temp;
 183     mc_color_pair->fg = ifg;
 184     mc_color_pair->bg = ibg;
 185     mc_color_pair->attr = attr;
 186     mc_color_pair->pair_index = tty_color_get_next__color_pair_number ();
 187 
 188     tty_color_try_alloc_lib_pair (mc_color_pair);
 189 
 190     g_hash_table_insert (mc_tty_color__hashtable, (gpointer) color_pair, (gpointer) mc_color_pair);
 191 
 192     return mc_color_pair->pair_index;
 193 }
 194 
 195 /* --------------------------------------------------------------------------------------------- */
 196 
 197 void
 198 tty_color_free_temp (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 199 {
 200     g_hash_table_foreach_remove (mc_tty_color__hashtable, tty_color_free_temp_cb, NULL);
 201 }
 202 
 203 /* --------------------------------------------------------------------------------------------- */
 204 
 205 void
 206 tty_color_free_all (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 207 {
 208     g_hash_table_remove_all (mc_tty_color__hashtable);
 209 }
 210 
 211 /* --------------------------------------------------------------------------------------------- */

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