root/lib/tty/color-ncurses.c

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

DEFINITIONS

This source file includes following definitions.
  1. mc_tty_color_attr_destroy_cb
  2. mc_tty_color_save_attr
  3. color_get_attr
  4. mc_tty_color_pair_init_special
  5. tty_color_init_lib
  6. tty_color_deinit_lib
  7. tty_color_try_alloc_lib_pair
  8. tty_setcolor
  9. tty_lowlevel_setcolor
  10. tty_set_normal_attrs
  11. tty_use_256colors
  12. tty_use_truecolors

   1 /*
   2    Color setup for NCurses screen library
   3 
   4    Copyright (C) 1994-2025
   5    Free Software Foundation, Inc.
   6 
   7    Written by:
   8    Andrew Borodin <aborodin@vmail.ru>, 2009
   9    Slava Zanko <slavazanko@gmail.com>, 2010
  10    Egmont Koblinger <egmont@gmail.com>, 2010
  11 
  12    This file is part of the Midnight Commander.
  13 
  14    The Midnight Commander is free software: you can redistribute it
  15    and/or modify it under the terms of the GNU General Public License as
  16    published by the Free Software Foundation, either version 3 of the License,
  17    or (at your option) any later version.
  18 
  19    The Midnight Commander is distributed in the hope that it will be useful,
  20    but WITHOUT ANY WARRANTY; without even the implied warranty of
  21    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
  22    GNU General Public License for more details.
  23 
  24    You should have received a copy of the GNU General Public License
  25    along with this program.  If not, see <https://www.gnu.org/licenses/>.
  26  */
  27 
  28 /** \file color-ncurses.c
  29  *  \brief Source: NCUrses-specific color setup
  30  */
  31 
  32 #include <config.h>
  33 
  34 #include <stdio.h>
  35 #include <stdlib.h>
  36 #include <string.h>
  37 #include <sys/types.h>  // size_t
  38 
  39 #include "lib/global.h"
  40 
  41 #include "tty-ncurses.h"
  42 #include "color.h"  // variables
  43 #include "color-internal.h"
  44 
  45 /*** global variables ****************************************************************************/
  46 
  47 /*** file scope macro definitions ****************************************************************/
  48 
  49 /*** file scope type declarations ****************************************************************/
  50 
  51 /*** forward declarations (file scope functions) *************************************************/
  52 
  53 /*** file scope variables ************************************************************************/
  54 
  55 static GHashTable *mc_tty_color_color_pair_attrs = NULL;
  56 
  57 /* --------------------------------------------------------------------------------------------- */
  58 /*** file scope functions ************************************************************************/
  59 /* --------------------------------------------------------------------------------------------- */
  60 
  61 static inline void
  62 mc_tty_color_attr_destroy_cb (gpointer data)
     /* [previous][next][first][last][top][bottom][index][help]  */
  63 {
  64     g_free (data);
  65 }
  66 
  67 /* --------------------------------------------------------------------------------------------- */
  68 
  69 static void
  70 mc_tty_color_save_attr (int color_pair, int color_attr)
     /* [previous][next][first][last][top][bottom][index][help]  */
  71 {
  72     int *attr, *key;
  73 
  74     attr = g_try_new0 (int, 1);
  75     if (attr == NULL)
  76         return;
  77 
  78     key = g_try_new (int, 1);
  79     if (key == NULL)
  80     {
  81         g_free (attr);
  82         return;
  83     }
  84 
  85     *key = color_pair;
  86     *attr = color_attr;
  87 
  88     g_hash_table_replace (mc_tty_color_color_pair_attrs, (gpointer) key, (gpointer) attr);
  89 }
  90 
  91 /* --------------------------------------------------------------------------------------------- */
  92 
  93 static int
  94 color_get_attr (int color_pair)
     /* [previous][next][first][last][top][bottom][index][help]  */
  95 {
  96     int *fnd = NULL;
  97 
  98     if (mc_tty_color_color_pair_attrs != NULL)
  99         fnd = (int *) g_hash_table_lookup (mc_tty_color_color_pair_attrs, (gpointer) &color_pair);
 100     return (fnd != NULL) ? *fnd : 0;
 101 }
 102 
 103 /* --------------------------------------------------------------------------------------------- */
 104 
 105 static void
 106 mc_tty_color_pair_init_special (tty_color_lib_pair_t *mc_color_pair, int fg1, int bg1, int fg2,
     /* [previous][next][first][last][top][bottom][index][help]  */
 107                                 int bg2, int attr)
 108 {
 109     if (has_colors () && !mc_tty_color_disable)
 110         init_pair (mc_color_pair->pair_index, fg1, bg1);
 111     else
 112         init_pair (mc_color_pair->pair_index, fg2, bg2);
 113     mc_tty_color_save_attr (mc_color_pair->pair_index, attr);
 114 }
 115 
 116 /* --------------------------------------------------------------------------------------------- */
 117 /*** public functions ****************************************************************************/
 118 /* --------------------------------------------------------------------------------------------- */
 119 
 120 void
 121 tty_color_init_lib (gboolean disable, gboolean force)
     /* [previous][next][first][last][top][bottom][index][help]  */
 122 {
 123     (void) force;
 124 
 125     if (has_colors () && !disable)
 126     {
 127         use_colors = TRUE;
 128         start_color ();
 129         use_default_colors ();
 130     }
 131 
 132     mc_tty_color_color_pair_attrs = g_hash_table_new_full (
 133         g_int_hash, g_int_equal, mc_tty_color_attr_destroy_cb, mc_tty_color_attr_destroy_cb);
 134 }
 135 
 136 /* --------------------------------------------------------------------------------------------- */
 137 
 138 void
 139 tty_color_deinit_lib (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 140 {
 141     g_hash_table_destroy (mc_tty_color_color_pair_attrs);
 142     mc_tty_color_color_pair_attrs = NULL;
 143 }
 144 
 145 /* --------------------------------------------------------------------------------------------- */
 146 
 147 void
 148 tty_color_try_alloc_lib_pair (tty_color_lib_pair_t *mc_color_pair)
     /* [previous][next][first][last][top][bottom][index][help]  */
 149 {
 150     if (mc_color_pair->fg <= (int) SPEC_A_REVERSE)
 151     {
 152         switch (mc_color_pair->fg)
 153         {
 154         case SPEC_A_REVERSE:
 155             mc_tty_color_pair_init_special (mc_color_pair, COLOR_BLACK, COLOR_WHITE, COLOR_BLACK,
 156                                             COLOR_WHITE | A_BOLD, A_REVERSE);
 157             break;
 158         case SPEC_A_BOLD:
 159             mc_tty_color_pair_init_special (mc_color_pair, COLOR_WHITE, COLOR_BLACK, COLOR_WHITE,
 160                                             COLOR_BLACK, A_BOLD);
 161             break;
 162         case SPEC_A_BOLD_REVERSE:
 163             mc_tty_color_pair_init_special (mc_color_pair, COLOR_WHITE, COLOR_WHITE, COLOR_WHITE,
 164                                             COLOR_WHITE, A_BOLD | A_REVERSE);
 165             break;
 166         case SPEC_A_UNDERLINE:
 167             mc_tty_color_pair_init_special (mc_color_pair, COLOR_WHITE, COLOR_BLACK, COLOR_WHITE,
 168                                             COLOR_BLACK, A_UNDERLINE);
 169             break;
 170         default:
 171             break;
 172         }
 173     }
 174     else
 175     {
 176         int ifg, ibg, attr;
 177 
 178         ifg = mc_color_pair->fg;
 179         ibg = mc_color_pair->bg;
 180         attr = mc_color_pair->attr;
 181 
 182         // In legacy color mode, change bright colors into bold
 183         if (!tty_use_256colors (NULL) && !tty_use_truecolors (NULL))
 184         {
 185             if (ifg >= 8 && ifg < 16)
 186             {
 187                 ifg &= 0x07;
 188                 attr |= A_BOLD;
 189             }
 190 
 191             if (ibg >= 8 && ibg < 16)
 192             {
 193                 ibg &= 0x07;
 194                 // attr | = A_BOLD | A_REVERSE ;
 195             }
 196         }
 197 
 198         init_pair (mc_color_pair->pair_index, ifg, ibg);
 199         mc_tty_color_save_attr (mc_color_pair->pair_index, attr);
 200     }
 201 }
 202 
 203 /* --------------------------------------------------------------------------------------------- */
 204 
 205 void
 206 tty_setcolor (int color)
     /* [previous][next][first][last][top][bottom][index][help]  */
 207 {
 208     attrset (COLOR_PAIR (color) | color_get_attr (color));
 209 }
 210 
 211 /* --------------------------------------------------------------------------------------------- */
 212 
 213 void
 214 tty_lowlevel_setcolor (int color)
     /* [previous][next][first][last][top][bottom][index][help]  */
 215 {
 216     tty_setcolor (color);
 217 }
 218 
 219 /* --------------------------------------------------------------------------------------------- */
 220 
 221 void
 222 tty_set_normal_attrs (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 223 {
 224     standend ();
 225 }
 226 
 227 /* --------------------------------------------------------------------------------------------- */
 228 
 229 gboolean
 230 tty_use_256colors (GError **error)
     /* [previous][next][first][last][top][bottom][index][help]  */
 231 {
 232     (void) error;
 233 
 234     return (COLORS == 256);
 235 }
 236 
 237 /* --------------------------------------------------------------------------------------------- */
 238 
 239 gboolean
 240 tty_use_truecolors (GError **error)
     /* [previous][next][first][last][top][bottom][index][help]  */
 241 {
 242     // Not yet supported in ncurses
 243     g_set_error (error, MC_ERROR, -1, _ ("True color not supported with ncurses."));
 244     return FALSE;
 245 }
 246 
 247 /* --------------------------------------------------------------------------------------------- */

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