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-2026
   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 #ifdef HAVE_TESTS
  66 extern GHashTable *mc_tty_color__hashtable;
  67 #endif
  68 
  69 MC_TESTABLE GHashTable *mc_tty_color__hashtable = NULL;
  70 
  71 /* --------------------------------------------------------------------------------------------- */
  72 /*** file scope functions ************************************************************************/
  73 /* --------------------------------------------------------------------------------------------- */
  74 
  75 static gboolean
  76 tty_color_free_temp_cb (gpointer key, gpointer value, gpointer user_data)
     /* [previous][next][first][last][top][bottom][index][help]  */
  77 {
  78     (void) key;
  79     (void) user_data;
  80 
  81     return ((tty_color_lib_pair_t *) value)->is_temp;
  82 }
  83 
  84 /* --------------------------------------------------------------------------------------------- */
  85 
  86 static gboolean
  87 tty_color_get_next_cpn_cb (gpointer key, gpointer value, gpointer user_data)
     /* [previous][next][first][last][top][bottom][index][help]  */
  88 {
  89     tty_color_lib_pair_t *mc_color_pair = (tty_color_lib_pair_t *) value;
  90     size_t cp = GPOINTER_TO_SIZE (user_data);
  91 
  92     (void) key;
  93 
  94     return (cp == mc_color_pair->pair_index);
  95 }
  96 
  97 /* --------------------------------------------------------------------------------------------- */
  98 
  99 static size_t
 100 tty_color_get_next__color_pair_number (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 101 {
 102     size_t cp_count, cp;
 103 
 104     cp_count = g_hash_table_size (mc_tty_color__hashtable);
 105     for (cp = 0; cp < cp_count; cp++)
 106         if (g_hash_table_find (mc_tty_color__hashtable, tty_color_get_next_cpn_cb,
 107                                GSIZE_TO_POINTER (cp))
 108             == NULL)
 109             break;
 110 
 111     return cp;
 112 }
 113 
 114 /* --------------------------------------------------------------------------------------------- */
 115 /*** public functions ****************************************************************************/
 116 /* --------------------------------------------------------------------------------------------- */
 117 
 118 void
 119 tty_init_colors (gboolean disable, gboolean force, int color_map_size)
     /* [previous][next][first][last][top][bottom][index][help]  */
 120 {
 121     tty_color_role_to_pair = g_new (int, color_map_size);
 122     tty_color_init_lib (disable, force);
 123     mc_tty_color__hashtable = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
 124 }
 125 
 126 /* --------------------------------------------------------------------------------------------- */
 127 
 128 void
 129 tty_colors_done (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 130 {
 131     tty_color_deinit_lib ();
 132     g_hash_table_destroy (mc_tty_color__hashtable);
 133     MC_PTR_FREE (tty_color_role_to_pair);
 134 }
 135 
 136 /* --------------------------------------------------------------------------------------------- */
 137 
 138 gboolean
 139 tty_use_colors (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 140 {
 141     return use_colors;
 142 }
 143 
 144 /* --------------------------------------------------------------------------------------------- */
 145 
 146 int
 147 tty_try_alloc_color_pair (const tty_color_pair_t *color, gboolean is_temp)
     /* [previous][next][first][last][top][bottom][index][help]  */
 148 {
 149     gchar *color_pair;
 150     tty_color_lib_pair_t *mc_color_pair;
 151     int ifg, ibg, attr;
 152 
 153     ifg = tty_color_get_index_by_name (color->fg);
 154     ibg = tty_color_get_index_by_name (color->bg);
 155     attr = tty_attr_get_bits (color->attrs);
 156 
 157     color_pair = g_strdup_printf ("%d.%d.%d", ifg, ibg, attr);
 158     if (color_pair == NULL)
 159         return 0;
 160 
 161     mc_color_pair = (tty_color_lib_pair_t *) g_hash_table_lookup (mc_tty_color__hashtable,
 162                                                                   (gpointer) color_pair);
 163 
 164     if (mc_color_pair != NULL)
 165     {
 166         g_free (color_pair);
 167         return mc_color_pair->pair_index;
 168     }
 169 
 170     if (need_convert_256color)
 171     {
 172         if ((ifg & FLAG_TRUECOLOR) == 0)
 173             ifg = convert_256color_to_truecolor (ifg);
 174 
 175         if ((ibg & FLAG_TRUECOLOR) == 0)
 176             ibg = convert_256color_to_truecolor (ibg);
 177     }
 178 
 179     mc_color_pair = g_try_new0 (tty_color_lib_pair_t, 1);
 180     if (mc_color_pair == NULL)
 181     {
 182         g_free (color_pair);
 183         return 0;
 184     }
 185 
 186     mc_color_pair->is_temp = is_temp;
 187     mc_color_pair->fg = ifg;
 188     mc_color_pair->bg = ibg;
 189     mc_color_pair->attr = attr;
 190     mc_color_pair->pair_index = tty_color_get_next__color_pair_number ();
 191 
 192     tty_color_try_alloc_lib_pair (mc_color_pair);
 193 
 194     g_hash_table_insert (mc_tty_color__hashtable, (gpointer) color_pair, (gpointer) mc_color_pair);
 195 
 196     return mc_color_pair->pair_index;
 197 }
 198 
 199 /* --------------------------------------------------------------------------------------------- */
 200 
 201 void
 202 tty_color_free_temp (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 203 {
 204     g_hash_table_foreach_remove (mc_tty_color__hashtable, tty_color_free_temp_cb, NULL);
 205 }
 206 
 207 /* --------------------------------------------------------------------------------------------- */
 208 
 209 void
 210 tty_color_free_all (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 211 {
 212     g_hash_table_remove_all (mc_tty_color__hashtable);
 213 }
 214 
 215 /* --------------------------------------------------------------------------------------------- */

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