Update hints translations from Transifex
[midnight-commander.git] / lib / tty / color.c
blobc79e13af28477add3251d70b4273a13562ef717b
1 /*
2 Color setup.
3 Interface functions.
5 Copyright (C) 1994-2023
6 Free Software Foundation, Inc.
8 Written by:
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 <http://www.gnu.org/licenses/>.
29 /** \file color.c
30 * \brief Source: color setup
33 #include <config.h>
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <sys/types.h> /* size_t */
40 #include "lib/global.h"
42 #include "tty.h"
43 #include "color.h"
45 #include "color-internal.h"
47 /*** global variables ****************************************************************************/
49 static char *tty_color_defaults__fg = NULL;
50 static char *tty_color_defaults__bg = NULL;
51 static char *tty_color_defaults__attrs = NULL;
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 /* --------------------------------------------------------------------------------------------- */
70 static gboolean
71 tty_color_free_condition_cb (gpointer key, gpointer value, gpointer user_data)
73 tty_color_pair_t *mc_color_pair = (tty_color_pair_t *) value;
74 gboolean is_temp_color;
76 (void) key;
78 is_temp_color = user_data != NULL;
79 return (mc_color_pair->is_temp == is_temp_color);
82 /* --------------------------------------------------------------------------------------------- */
84 static void
85 tty_color_free_all (gboolean is_temp_color)
87 g_hash_table_foreach_remove (mc_tty_color__hashtable, tty_color_free_condition_cb,
88 is_temp_color ? GSIZE_TO_POINTER (1) : NULL);
91 /* --------------------------------------------------------------------------------------------- */
93 static gboolean
94 tty_color_get_next_cpn_cb (gpointer key, gpointer value, gpointer user_data)
96 tty_color_pair_t *mc_color_pair = (tty_color_pair_t *) value;
97 size_t cp = GPOINTER_TO_SIZE (user_data);
99 (void) key;
101 return (cp == mc_color_pair->pair_index);
104 /* --------------------------------------------------------------------------------------------- */
106 static size_t
107 tty_color_get_next__color_pair_number (void)
109 size_t cp_count, cp;
111 cp_count = g_hash_table_size (mc_tty_color__hashtable);
112 for (cp = 0; cp < cp_count; cp++)
113 if (g_hash_table_find (mc_tty_color__hashtable, tty_color_get_next_cpn_cb,
114 GSIZE_TO_POINTER (cp)) == NULL)
115 break;
117 return cp;
120 /* --------------------------------------------------------------------------------------------- */
121 /*** public functions ****************************************************************************/
122 /* --------------------------------------------------------------------------------------------- */
124 void
125 tty_init_colors (gboolean disable, gboolean force)
127 tty_color_init_lib (disable, force);
128 mc_tty_color__hashtable = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
131 /* --------------------------------------------------------------------------------------------- */
133 void
134 tty_colors_done (void)
136 tty_color_deinit_lib ();
137 g_free (tty_color_defaults__fg);
138 g_free (tty_color_defaults__bg);
139 g_free (tty_color_defaults__attrs);
141 g_hash_table_destroy (mc_tty_color__hashtable);
144 /* --------------------------------------------------------------------------------------------- */
146 gboolean
147 tty_use_colors (void)
149 return use_colors;
152 /* --------------------------------------------------------------------------------------------- */
155 tty_try_alloc_color_pair2 (const char *fg, const char *bg, const char *attrs,
156 gboolean is_temp_color)
158 gchar *color_pair;
159 tty_color_pair_t *mc_color_pair;
160 int ifg, ibg, attr;
162 if (fg == NULL || strcmp (fg, "base") == 0)
163 fg = tty_color_defaults__fg;
164 if (bg == NULL || strcmp (bg, "base") == 0)
165 bg = tty_color_defaults__bg;
166 if (attrs == NULL || strcmp (attrs, "base") == 0)
167 attrs = tty_color_defaults__attrs;
169 ifg = tty_color_get_index_by_name (fg);
170 ibg = tty_color_get_index_by_name (bg);
171 attr = tty_attr_get_bits (attrs);
173 color_pair = g_strdup_printf ("%d.%d.%d", ifg, ibg, attr);
174 if (color_pair == NULL)
175 return 0;
177 mc_color_pair =
178 (tty_color_pair_t *) g_hash_table_lookup (mc_tty_color__hashtable, (gpointer) color_pair);
180 if (mc_color_pair != NULL)
182 g_free (color_pair);
183 return mc_color_pair->pair_index;
186 mc_color_pair = g_try_new0 (tty_color_pair_t, 1);
187 if (mc_color_pair == NULL)
189 g_free (color_pair);
190 return 0;
193 mc_color_pair->is_temp = is_temp_color;
194 mc_color_pair->ifg = ifg;
195 mc_color_pair->ibg = ibg;
196 mc_color_pair->attr = attr;
197 mc_color_pair->pair_index = tty_color_get_next__color_pair_number ();
199 tty_color_try_alloc_pair_lib (mc_color_pair);
201 g_hash_table_insert (mc_tty_color__hashtable, (gpointer) color_pair, (gpointer) mc_color_pair);
203 return mc_color_pair->pair_index;
206 /* --------------------------------------------------------------------------------------------- */
209 tty_try_alloc_color_pair (const char *fg, const char *bg, const char *attrs)
211 return tty_try_alloc_color_pair2 (fg, bg, attrs, TRUE);
214 /* --------------------------------------------------------------------------------------------- */
216 void
217 tty_color_free_all_tmp (void)
219 tty_color_free_all (TRUE);
222 /* --------------------------------------------------------------------------------------------- */
224 void
225 tty_color_free_all_non_tmp (void)
227 tty_color_free_all (FALSE);
230 /* --------------------------------------------------------------------------------------------- */
232 void
233 tty_color_set_defaults (const char *fgcolor, const char *bgcolor, const char *attrs)
235 g_free (tty_color_defaults__fg);
236 g_free (tty_color_defaults__bg);
237 g_free (tty_color_defaults__attrs);
239 tty_color_defaults__fg = g_strdup (fgcolor);
240 tty_color_defaults__bg = g_strdup (bgcolor);
241 tty_color_defaults__attrs = g_strdup (attrs);
244 /* --------------------------------------------------------------------------------------------- */