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-2024
   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 <http://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,
     /* [previous][next][first][last][top][bottom][index][help]  */
 107                                 int fg1, int bg1, int fg2, 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,
 156                                             COLOR_BLACK, COLOR_WHITE,
 157                                             COLOR_BLACK, COLOR_WHITE | A_BOLD, A_REVERSE);
 158             break;
 159         case SPEC_A_BOLD:
 160             mc_tty_color_pair_init_special (mc_color_pair,
 161                                             COLOR_WHITE, COLOR_BLACK,
 162                                             COLOR_WHITE, COLOR_BLACK, A_BOLD);
 163             break;
 164         case SPEC_A_BOLD_REVERSE:
 165             mc_tty_color_pair_init_special (mc_color_pair,
 166                                             COLOR_WHITE, COLOR_WHITE,
 167                                             COLOR_WHITE, COLOR_WHITE, A_BOLD | A_REVERSE);
 168             break;
 169         case SPEC_A_UNDERLINE:
 170             mc_tty_color_pair_init_special (mc_color_pair,
 171                                             COLOR_WHITE, COLOR_BLACK,
 172                                             COLOR_WHITE, COLOR_BLACK, A_UNDERLINE);
 173             break;
 174         default:
 175             break;
 176         }
 177     }
 178     else
 179     {
 180         int ifg, ibg, attr;
 181 
 182         ifg = mc_color_pair->fg;
 183         ibg = mc_color_pair->bg;
 184         attr = mc_color_pair->attr;
 185 
 186         /* In legacy color mode, change bright colors into bold */
 187         if (!tty_use_256colors (NULL) && !tty_use_truecolors (NULL))
 188         {
 189             if (ifg >= 8 && ifg < 16)
 190             {
 191                 ifg &= 0x07;
 192                 attr |= A_BOLD;
 193             }
 194 
 195             if (ibg >= 8 && ibg < 16)
 196             {
 197                 ibg &= 0x07;
 198                 /* attr | = A_BOLD | A_REVERSE ; */
 199             }
 200         }
 201 
 202         init_pair (mc_color_pair->pair_index, ifg, ibg);
 203         mc_tty_color_save_attr (mc_color_pair->pair_index, attr);
 204     }
 205 }
 206 
 207 /* --------------------------------------------------------------------------------------------- */
 208 
 209 void
 210 tty_setcolor (int color)
     /* [previous][next][first][last][top][bottom][index][help]  */
 211 {
 212     attrset (COLOR_PAIR (color) | color_get_attr (color));
 213 }
 214 
 215 /* --------------------------------------------------------------------------------------------- */
 216 
 217 void
 218 tty_lowlevel_setcolor (int color)
     /* [previous][next][first][last][top][bottom][index][help]  */
 219 {
 220     tty_setcolor (color);
 221 }
 222 
 223 /* --------------------------------------------------------------------------------------------- */
 224 
 225 void
 226 tty_set_normal_attrs (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 227 {
 228     standend ();
 229 }
 230 
 231 /* --------------------------------------------------------------------------------------------- */
 232 
 233 gboolean
 234 tty_use_256colors (GError ** error)
     /* [previous][next][first][last][top][bottom][index][help]  */
 235 {
 236     (void) error;
 237 
 238     return (COLORS == 256);
 239 }
 240 
 241 /* --------------------------------------------------------------------------------------------- */
 242 
 243 gboolean
 244 tty_use_truecolors (GError ** error)
     /* [previous][next][first][last][top][bottom][index][help]  */
 245 {
 246     /* Not yet supported in ncurses */
 247     g_set_error (error, MC_ERROR, -1, _("True color not supported with ncurses."));
 248     return FALSE;
 249 }
 250 
 251 /* --------------------------------------------------------------------------------------------- */

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