configure.c: fix AX_GCC_FUNC_ATTRIBUTE detection on custom CFLAGS
[midnight-commander.git] / lib / tty / color.c
blob50cfdb3abf9490061ad0181f2bb7f8510631c063
1 /*
2 Color setup.
3 Interface functions.
5 Copyright (C) 1994-2019
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 /*** file scope variables ************************************************************************/
62 static GHashTable *mc_tty_color__hashtable = NULL;
64 /*** file scope functions ************************************************************************/
65 /* --------------------------------------------------------------------------------------------- */
67 static gboolean
68 tty_color_free_condition_cb (gpointer key, gpointer value, gpointer user_data)
70 gboolean is_temp_color;
71 tty_color_pair_t *mc_color_pair;
72 (void) key;
74 is_temp_color = user_data != NULL;
75 mc_color_pair = (tty_color_pair_t *) value;
76 return (mc_color_pair->is_temp == is_temp_color);
79 /* --------------------------------------------------------------------------------------------- */
81 static void
82 tty_color_free_all (gboolean is_temp_color)
84 g_hash_table_foreach_remove (mc_tty_color__hashtable, tty_color_free_condition_cb,
85 is_temp_color ? GSIZE_TO_POINTER (1) : NULL);
88 /* --------------------------------------------------------------------------------------------- */
90 static gboolean
91 tty_color_get_next_cpn_cb (gpointer key, gpointer value, gpointer user_data)
93 size_t cp;
94 tty_color_pair_t *mc_color_pair;
95 (void) key;
97 cp = GPOINTER_TO_SIZE (user_data);
98 mc_color_pair = (tty_color_pair_t *) value;
100 return (cp == mc_color_pair->pair_index);
103 /* --------------------------------------------------------------------------------------------- */
105 static size_t
106 tty_color_get_next__color_pair_number (void)
108 size_t cp_count, cp;
110 cp_count = g_hash_table_size (mc_tty_color__hashtable);
111 for (cp = 0; cp < cp_count; cp++)
112 if (g_hash_table_find (mc_tty_color__hashtable, tty_color_get_next_cpn_cb,
113 GSIZE_TO_POINTER (cp)) == NULL)
114 break;
116 return cp;
119 /* --------------------------------------------------------------------------------------------- */
120 /*** public functions ****************************************************************************/
121 /* --------------------------------------------------------------------------------------------- */
123 void
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 /* --------------------------------------------------------------------------------------------- */
132 void
133 tty_colors_done (void)
135 tty_color_deinit_lib ();
136 g_free (tty_color_defaults__fg);
137 g_free (tty_color_defaults__bg);
138 g_free (tty_color_defaults__attrs);
140 g_hash_table_destroy (mc_tty_color__hashtable);
143 /* --------------------------------------------------------------------------------------------- */
145 gboolean
146 tty_use_colors (void)
148 return use_colors;
151 /* --------------------------------------------------------------------------------------------- */
154 tty_try_alloc_color_pair2 (const char *fg, const char *bg, const char *attrs,
155 gboolean is_temp_color)
157 gchar *color_pair;
158 tty_color_pair_t *mc_color_pair;
159 int ifg, ibg, attr;
161 if (fg == NULL || !strcmp (fg, "base"))
162 fg = tty_color_defaults__fg;
163 if (bg == NULL || !strcmp (bg, "base"))
164 bg = tty_color_defaults__bg;
165 if (attrs == NULL || !strcmp (attrs, "base"))
166 attrs = tty_color_defaults__attrs;
168 ifg = tty_color_get_index_by_name (fg);
169 ibg = tty_color_get_index_by_name (bg);
170 attr = tty_attr_get_bits (attrs);
172 color_pair = g_strdup_printf ("%d.%d.%d", ifg, ibg, attr);
173 if (color_pair == NULL)
174 return 0;
176 mc_color_pair =
177 (tty_color_pair_t *) g_hash_table_lookup (mc_tty_color__hashtable, (gpointer) color_pair);
179 if (mc_color_pair != NULL)
181 g_free (color_pair);
182 return mc_color_pair->pair_index;
185 mc_color_pair = g_try_new0 (tty_color_pair_t, 1);
186 if (mc_color_pair == NULL)
188 g_free (color_pair);
189 return 0;
192 mc_color_pair->is_temp = is_temp_color;
193 mc_color_pair->ifg = ifg;
194 mc_color_pair->ibg = ibg;
195 mc_color_pair->attr = attr;
196 mc_color_pair->pair_index = tty_color_get_next__color_pair_number ();
198 tty_color_try_alloc_pair_lib (mc_color_pair);
200 g_hash_table_insert (mc_tty_color__hashtable, (gpointer) color_pair, (gpointer) mc_color_pair);
202 return mc_color_pair->pair_index;
205 /* --------------------------------------------------------------------------------------------- */
208 tty_try_alloc_color_pair (const char *fg, const char *bg, const char *attrs)
210 return tty_try_alloc_color_pair2 (fg, bg, attrs, TRUE);
213 /* --------------------------------------------------------------------------------------------- */
215 void
216 tty_color_free_all_tmp (void)
218 tty_color_free_all (TRUE);
221 /* --------------------------------------------------------------------------------------------- */
223 void
224 tty_color_free_all_non_tmp (void)
226 tty_color_free_all (FALSE);
229 /* --------------------------------------------------------------------------------------------- */
231 void
232 tty_color_set_defaults (const char *fgcolor, const char *bgcolor, const char *attrs)
234 g_free (tty_color_defaults__fg);
235 g_free (tty_color_defaults__bg);
236 g_free (tty_color_defaults__attrs);
238 tty_color_defaults__fg = g_strdup (fgcolor);
239 tty_color_defaults__bg = g_strdup (bgcolor);
240 tty_color_defaults__attrs = g_strdup (attrs);
243 /* --------------------------------------------------------------------------------------------- */