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_pair_lib
  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-2019
   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 /*** file scope variables ************************************************************************/
  52 
  53 static GHashTable *mc_tty_color_color_pair_attrs = NULL;
  54 
  55 /*** file scope functions ************************************************************************/
  56 /* --------------------------------------------------------------------------------------------- */
  57 
  58 static inline void
  59 mc_tty_color_attr_destroy_cb (gpointer data)
     /* [previous][next][first][last][top][bottom][index][help]  */
  60 {
  61     g_free (data);
  62 }
  63 
  64 /* --------------------------------------------------------------------------------------------- */
  65 
  66 static void
  67 mc_tty_color_save_attr (int color_pair, int color_attr)
     /* [previous][next][first][last][top][bottom][index][help]  */
  68 {
  69     int *attr, *key;
  70 
  71     attr = g_try_new0 (int, 1);
  72     if (attr == NULL)
  73         return;
  74 
  75     key = g_try_new (int, 1);
  76     if (key == NULL)
  77     {
  78         g_free (attr);
  79         return;
  80     }
  81 
  82     *key = color_pair;
  83     *attr = color_attr;
  84 
  85     g_hash_table_replace (mc_tty_color_color_pair_attrs, (gpointer) key, (gpointer) attr);
  86 }
  87 
  88 /* --------------------------------------------------------------------------------------------- */
  89 
  90 static int
  91 color_get_attr (int color_pair)
     /* [previous][next][first][last][top][bottom][index][help]  */
  92 {
  93     int *fnd = NULL;
  94 
  95     if (mc_tty_color_color_pair_attrs != NULL)
  96         fnd = (int *) g_hash_table_lookup (mc_tty_color_color_pair_attrs, (gpointer) & color_pair);
  97     return (fnd != NULL) ? *fnd : 0;
  98 }
  99 
 100 /* --------------------------------------------------------------------------------------------- */
 101 
 102 static void
 103 mc_tty_color_pair_init_special (tty_color_pair_t * mc_color_pair,
     /* [previous][next][first][last][top][bottom][index][help]  */
 104                                 int fg1, int bg1, int fg2, int bg2, int attr)
 105 {
 106     if (has_colors () && !mc_tty_color_disable)
 107         init_pair (mc_color_pair->pair_index, fg1, bg1);
 108     else
 109         init_pair (mc_color_pair->pair_index, fg2, bg2);
 110     mc_tty_color_save_attr (mc_color_pair->pair_index, attr);
 111 }
 112 
 113 /* --------------------------------------------------------------------------------------------- */
 114 /*** public functions ****************************************************************************/
 115 /* --------------------------------------------------------------------------------------------- */
 116 
 117 void
 118 tty_color_init_lib (gboolean disable, gboolean force)
     /* [previous][next][first][last][top][bottom][index][help]  */
 119 {
 120     (void) force;
 121 
 122     if (has_colors () && !disable)
 123     {
 124         use_colors = TRUE;
 125         start_color ();
 126         use_default_colors ();
 127     }
 128 
 129     mc_tty_color_color_pair_attrs = g_hash_table_new_full
 130         (g_int_hash, g_int_equal, mc_tty_color_attr_destroy_cb, mc_tty_color_attr_destroy_cb);
 131 }
 132 
 133 /* --------------------------------------------------------------------------------------------- */
 134 
 135 void
 136 tty_color_deinit_lib (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 137 {
 138     g_hash_table_destroy (mc_tty_color_color_pair_attrs);
 139     mc_tty_color_color_pair_attrs = NULL;
 140 }
 141 
 142 /* --------------------------------------------------------------------------------------------- */
 143 
 144 void
 145 tty_color_try_alloc_pair_lib (tty_color_pair_t * mc_color_pair)
     /* [previous][next][first][last][top][bottom][index][help]  */
 146 {
 147     if (mc_color_pair->ifg <= (int) SPEC_A_REVERSE)
 148     {
 149         switch (mc_color_pair->ifg)
 150         {
 151         case SPEC_A_REVERSE:
 152             mc_tty_color_pair_init_special (mc_color_pair,
 153                                             COLOR_BLACK, COLOR_WHITE,
 154                                             COLOR_BLACK, COLOR_WHITE | A_BOLD, A_REVERSE);
 155             break;
 156         case SPEC_A_BOLD:
 157             mc_tty_color_pair_init_special (mc_color_pair,
 158                                             COLOR_WHITE, COLOR_BLACK,
 159                                             COLOR_WHITE, COLOR_BLACK, A_BOLD);
 160             break;
 161         case SPEC_A_BOLD_REVERSE:
 162             mc_tty_color_pair_init_special (mc_color_pair,
 163                                             COLOR_WHITE, COLOR_WHITE,
 164                                             COLOR_WHITE, COLOR_WHITE, A_BOLD | A_REVERSE);
 165             break;
 166         case SPEC_A_UNDERLINE:
 167             mc_tty_color_pair_init_special (mc_color_pair,
 168                                             COLOR_WHITE, COLOR_BLACK,
 169                                             COLOR_WHITE, COLOR_BLACK, A_UNDERLINE);
 170             break;
 171         default:
 172             break;
 173         }
 174     }
 175     else
 176     {
 177         int ifg, ibg, attr;
 178 
 179         ifg = mc_color_pair->ifg;
 180         ibg = mc_color_pair->ibg;
 181         attr = mc_color_pair->attr;
 182 
 183         /* In legacy color mode, change bright colors into bold */
 184         if (!tty_use_256colors () && !tty_use_truecolors (NULL))
 185         {
 186             if (ifg >= 8 && ifg < 16)
 187             {
 188                 ifg &= 0x07;
 189                 attr |= A_BOLD;
 190             }
 191 
 192             if (ibg >= 8 && ibg < 16)
 193             {
 194                 ibg &= 0x07;
 195                 /* attr | = A_BOLD | A_REVERSE ; */
 196             }
 197         }
 198 
 199         init_pair (mc_color_pair->pair_index, ifg, ibg);
 200         mc_tty_color_save_attr (mc_color_pair->pair_index, attr);
 201     }
 202 }
 203 
 204 /* --------------------------------------------------------------------------------------------- */
 205 
 206 void
 207 tty_setcolor (int color)
     /* [previous][next][first][last][top][bottom][index][help]  */
 208 {
 209     attrset (COLOR_PAIR (color) | color_get_attr (color));
 210 }
 211 
 212 /* --------------------------------------------------------------------------------------------- */
 213 
 214 void
 215 tty_lowlevel_setcolor (int color)
     /* [previous][next][first][last][top][bottom][index][help]  */
 216 {
 217     tty_setcolor (color);
 218 }
 219 
 220 /* --------------------------------------------------------------------------------------------- */
 221 
 222 void
 223 tty_set_normal_attrs (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 224 {
 225     standend ();
 226 }
 227 
 228 /* --------------------------------------------------------------------------------------------- */
 229 
 230 gboolean
 231 tty_use_256colors (void)
     /* [previous][next][first][last][top][bottom][index][help]  */
 232 {
 233     return (COLORS == 256);
 234 }
 235 
 236 /* --------------------------------------------------------------------------------------------- */
 237 
 238 gboolean
 239 tty_use_truecolors (GError ** error)
     /* [previous][next][first][last][top][bottom][index][help]  */
 240 {
 241     /* Not yet supported in ncurses */
 242     g_set_error (error, MC_ERROR, -1, _("True color not supported with ncurses."));
 243     return FALSE;
 244 }
 245 
 246 /* --------------------------------------------------------------------------------------------- */

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