(mc_search__recode_str): minor optimization.
[midnight-commander.git] / lib / tty / color-ncurses.c
blob5ed46c869f1143ca9ad4811e0109cc7a346ecd40
1 /*
2 Color setup for NCurses screen library
4 Copyright (C) 1994-2018
5 Free Software Foundation, Inc.
7 Written by:
8 Andrew Borodin <aborodin@vmail.ru>, 2009
9 Slava Zanko <slavazanko@gmail.com>, 2010
10 Egmont Koblinger <egmont@gmail.com>, 2010
12 This file is part of the Midnight Commander.
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.
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.
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/>.
28 /** \file color-ncurses.c
29 * \brief Source: NCUrses-specific color setup
32 #include <config.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <sys/types.h> /* size_t */
39 #include "lib/global.h"
41 #include "tty-ncurses.h"
42 #include "color.h" /* variables */
43 #include "color-internal.h"
45 /*** global variables ****************************************************************************/
47 /*** file scope macro definitions ****************************************************************/
49 /*** file scope type declarations ****************************************************************/
51 /*** file scope variables ************************************************************************/
53 static GHashTable *mc_tty_color_color_pair_attrs = NULL;
55 /*** file scope functions ************************************************************************/
56 /* --------------------------------------------------------------------------------------------- */
58 static inline void
59 mc_tty_color_attr_destroy_cb (gpointer data)
61 g_free (data);
64 /* --------------------------------------------------------------------------------------------- */
66 static void
67 mc_tty_color_save_attr (int color_pair, int color_attr)
69 int *attr, *key;
71 attr = g_try_new0 (int, 1);
72 if (attr == NULL)
73 return;
75 key = g_try_new (int, 1);
76 if (key == NULL)
78 g_free (attr);
79 return;
82 *key = color_pair;
83 *attr = color_attr;
85 g_hash_table_replace (mc_tty_color_color_pair_attrs, (gpointer) key, (gpointer) attr);
88 /* --------------------------------------------------------------------------------------------- */
90 static int
91 color_get_attr (int color_pair)
93 int *fnd = NULL;
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;
100 /* --------------------------------------------------------------------------------------------- */
102 static void
103 mc_tty_color_pair_init_special (tty_color_pair_t * mc_color_pair,
104 int fg1, int bg1, int fg2, int bg2, int attr)
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);
113 /* --------------------------------------------------------------------------------------------- */
114 /*** public functions ****************************************************************************/
115 /* --------------------------------------------------------------------------------------------- */
117 void
118 tty_color_init_lib (gboolean disable, gboolean force)
120 (void) force;
122 if (has_colors () && !disable)
124 use_colors = TRUE;
125 start_color ();
126 use_default_colors ();
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);
133 /* --------------------------------------------------------------------------------------------- */
135 void
136 tty_color_deinit_lib (void)
138 g_hash_table_destroy (mc_tty_color_color_pair_attrs);
139 mc_tty_color_color_pair_attrs = NULL;
142 /* --------------------------------------------------------------------------------------------- */
144 void
145 tty_color_try_alloc_pair_lib (tty_color_pair_t * mc_color_pair)
147 if (mc_color_pair->ifg <= (int) SPEC_A_REVERSE)
149 switch (mc_color_pair->ifg)
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;
175 else
177 int ifg, ibg, attr;
179 ifg = mc_color_pair->ifg;
180 ibg = mc_color_pair->ibg;
181 attr = mc_color_pair->attr;
183 /* In legacy color mode, change bright colors into bold */
184 if (!tty_use_256colors () && !tty_use_truecolors (NULL))
186 if (ifg >= 8 && ifg < 16)
188 ifg &= 0x07;
189 attr |= A_BOLD;
192 if (ibg >= 8 && ibg < 16)
194 ibg &= 0x07;
195 /* attr | = A_BOLD | A_REVERSE ; */
199 init_pair (mc_color_pair->pair_index, ifg, ibg);
200 mc_tty_color_save_attr (mc_color_pair->pair_index, attr);
204 /* --------------------------------------------------------------------------------------------- */
206 void
207 tty_setcolor (int color)
209 attrset (COLOR_PAIR (color) | color_get_attr (color));
212 /* --------------------------------------------------------------------------------------------- */
214 void
215 tty_lowlevel_setcolor (int color)
217 tty_setcolor (color);
220 /* --------------------------------------------------------------------------------------------- */
222 void
223 tty_set_normal_attrs (void)
225 standend ();
228 /* --------------------------------------------------------------------------------------------- */
230 gboolean
231 tty_use_256colors (void)
233 return (COLORS == 256);
236 /* --------------------------------------------------------------------------------------------- */
238 gboolean
239 tty_use_truecolors (GError ** error)
241 /* Not yet supported in ncurses */
242 g_set_error (error, MC_ERROR, -1, _("True color not supported with ncurses."));
243 return FALSE;
246 /* --------------------------------------------------------------------------------------------- */