5 Copyright (C) 1994-2025
6 Free Software Foundation, Inc.
9 Andrew Borodin <aborodin@vmail.ru>, 2009
10 Slava Zanko <slavazanko@gmail.com>, 2009
11 Egmont Koblinger <egmont@gmail.com>, 2010
13 This file is part of the Midnight Commander.
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.
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.
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/>.
30 * \brief Source: color setup
38 #include <sys/types.h> // size_t
40 #include "lib/global.h"
45 #include "color-internal.h"
47 /*** global variables ****************************************************************************/
49 static tty_color_pair_t tty_color_defaults
= {
50 .fg
= NULL
, .bg
= NULL
, .attrs
= NULL
, .pair_index
= 0
53 /* Set if we are actually using colors */
54 gboolean use_colors
= FALSE
;
56 /*** file scope macro definitions ****************************************************************/
58 /*** file scope type declarations ****************************************************************/
60 /*** forward declarations (file scope functions) *************************************************/
62 /*** file scope variables ************************************************************************/
64 static GHashTable
*mc_tty_color__hashtable
= NULL
;
66 /* --------------------------------------------------------------------------------------------- */
67 /*** file scope functions ************************************************************************/
68 /* --------------------------------------------------------------------------------------------- */
71 mc_color__deinit (tty_color_pair_t
*color
)
75 g_free (color
->attrs
);
78 /* --------------------------------------------------------------------------------------------- */
81 tty_color_free_temp_cb (gpointer key
, gpointer value
, gpointer user_data
)
86 return ((tty_color_lib_pair_t
*) value
)->is_temp
;
89 /* --------------------------------------------------------------------------------------------- */
92 tty_color_get_next_cpn_cb (gpointer key
, gpointer value
, gpointer user_data
)
94 tty_color_lib_pair_t
*mc_color_pair
= (tty_color_lib_pair_t
*) value
;
95 size_t cp
= GPOINTER_TO_SIZE (user_data
);
99 return (cp
== mc_color_pair
->pair_index
);
102 /* --------------------------------------------------------------------------------------------- */
105 tty_color_get_next__color_pair_number (void)
109 cp_count
= g_hash_table_size (mc_tty_color__hashtable
);
110 for (cp
= 0; cp
< cp_count
; cp
++)
111 if (g_hash_table_find (mc_tty_color__hashtable
, tty_color_get_next_cpn_cb
,
112 GSIZE_TO_POINTER (cp
))
119 /* --------------------------------------------------------------------------------------------- */
120 /*** public functions ****************************************************************************/
121 /* --------------------------------------------------------------------------------------------- */
124 tty_init_colors (gboolean disable
, gboolean force
)
126 tty_color_init_lib (disable
, force
);
127 mc_tty_color__hashtable
= g_hash_table_new_full (g_str_hash
, g_str_equal
, g_free
, g_free
);
130 /* --------------------------------------------------------------------------------------------- */
133 tty_colors_done (void)
135 tty_color_deinit_lib ();
136 mc_color__deinit (&tty_color_defaults
);
137 g_hash_table_destroy (mc_tty_color__hashtable
);
140 /* --------------------------------------------------------------------------------------------- */
143 tty_use_colors (void)
148 /* --------------------------------------------------------------------------------------------- */
151 tty_try_alloc_color_pair (const tty_color_pair_t
*color
, gboolean is_temp
)
155 tty_color_lib_pair_t
*mc_color_pair
;
158 is_base
= (color
->fg
== NULL
|| strcmp (color
->fg
, "base") == 0);
159 ifg
= tty_color_get_index_by_name (is_base
? tty_color_defaults
.fg
: color
->fg
);
160 is_base
= (color
->bg
== NULL
|| strcmp (color
->bg
, "base") == 0);
161 ibg
= tty_color_get_index_by_name (is_base
? tty_color_defaults
.bg
: color
->bg
);
162 is_base
= (color
->attrs
== NULL
|| strcmp (color
->attrs
, "base") == 0);
163 attr
= tty_attr_get_bits (is_base
? tty_color_defaults
.attrs
: color
->attrs
);
165 color_pair
= g_strdup_printf ("%d.%d.%d", ifg
, ibg
, attr
);
166 if (color_pair
== NULL
)
169 mc_color_pair
= (tty_color_lib_pair_t
*) g_hash_table_lookup (mc_tty_color__hashtable
,
170 (gpointer
) color_pair
);
172 if (mc_color_pair
!= NULL
)
175 return mc_color_pair
->pair_index
;
178 mc_color_pair
= g_try_new0 (tty_color_lib_pair_t
, 1);
179 if (mc_color_pair
== NULL
)
185 mc_color_pair
->is_temp
= is_temp
;
186 mc_color_pair
->fg
= ifg
;
187 mc_color_pair
->bg
= ibg
;
188 mc_color_pair
->attr
= attr
;
189 mc_color_pair
->pair_index
= tty_color_get_next__color_pair_number ();
191 tty_color_try_alloc_lib_pair (mc_color_pair
);
193 g_hash_table_insert (mc_tty_color__hashtable
, (gpointer
) color_pair
, (gpointer
) mc_color_pair
);
195 return mc_color_pair
->pair_index
;
198 /* --------------------------------------------------------------------------------------------- */
201 tty_color_free_temp (void)
203 g_hash_table_foreach_remove (mc_tty_color__hashtable
, tty_color_free_temp_cb
, NULL
);
206 /* --------------------------------------------------------------------------------------------- */
209 tty_color_free_all (void)
211 g_hash_table_remove_all (mc_tty_color__hashtable
);
214 /* --------------------------------------------------------------------------------------------- */
217 tty_color_set_defaults (const tty_color_pair_t
*color
)
219 mc_color__deinit (&tty_color_defaults
);
221 tty_color_defaults
.fg
= g_strdup (color
->fg
);
222 tty_color_defaults
.bg
= g_strdup (color
->bg
);
223 tty_color_defaults
.attrs
= g_strdup (color
->attrs
);
224 tty_color_defaults
.pair_index
= 0;
227 /* --------------------------------------------------------------------------------------------- */