Merge branch '2621_path_encoding_and_extfs'
[midnight-commander.git] / lib / tty / color.c
bloba31ccfa1586770d57164e0cfda2a2246d2853cfd
1 /* Color setup.
2 Interface functions.
4 Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
5 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
7 Written by:
8 Andrew Borodin <aborodin@vmail.ru>, 2009
9 Slava Zanko <slavazanko@gmail.com>, 2009
10 Egmont Koblinger <egmont@gmail.com>, 2010
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
27 /** \file color.c
28 * \brief Source: color setup
31 #include <config.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <sys/types.h> /* size_t */
38 #include "lib/global.h"
40 #include "tty.h"
41 #include "color.h"
43 #include "color-internal.h"
45 /*** global variables ****************************************************************************/
47 static char *tty_color_defaults__fg = NULL;
48 static char *tty_color_defaults__bg = NULL;
49 static char *tty_color_defaults__attrs = NULL;
51 /* Set if we are actually using colors */
52 gboolean use_colors = FALSE;
54 /*** file scope macro definitions ****************************************************************/
56 /*** file scope type declarations ****************************************************************/
58 /*** file scope variables ************************************************************************/
60 static GHashTable *mc_tty_color__hashtable = NULL;
62 /*** file scope functions ************************************************************************/
63 /* --------------------------------------------------------------------------------------------- */
65 static gboolean
66 tty_color_free_condition_cb (gpointer key, gpointer value, gpointer user_data)
68 gboolean is_temp_color;
69 tty_color_pair_t *mc_color_pair;
70 (void) key;
72 is_temp_color = user_data != NULL;
73 mc_color_pair = (tty_color_pair_t *) value;
74 return (mc_color_pair->is_temp == is_temp_color);
77 /* --------------------------------------------------------------------------------------------- */
79 static void
80 tty_color_free_all (gboolean is_temp_color)
82 g_hash_table_foreach_remove (mc_tty_color__hashtable, tty_color_free_condition_cb,
83 is_temp_color ? GINT_TO_POINTER (1) : NULL);
86 /* --------------------------------------------------------------------------------------------- */
88 static gboolean
89 tty_color_get_next_cpn_cb (gpointer key, gpointer value, gpointer user_data)
91 size_t cp;
92 tty_color_pair_t *mc_color_pair;
93 (void) key;
95 cp = GPOINTER_TO_INT (user_data);
96 mc_color_pair = (tty_color_pair_t *) value;
98 return (cp == mc_color_pair->pair_index);
101 /* --------------------------------------------------------------------------------------------- */
103 static size_t
104 tty_color_get_next__color_pair_number (void)
106 const size_t cp_count = g_hash_table_size (mc_tty_color__hashtable);
107 size_t cp;
109 for (cp = 0; cp < cp_count; cp++)
110 if (g_hash_table_find (mc_tty_color__hashtable, tty_color_get_next_cpn_cb,
111 GINT_TO_POINTER (cp)) == NULL)
112 break;
114 return cp;
117 /* --------------------------------------------------------------------------------------------- */
118 /*** public functions ****************************************************************************/
119 /* --------------------------------------------------------------------------------------------- */
121 void
122 tty_init_colors (gboolean disable, gboolean force)
124 tty_color_init_lib (disable, force);
125 mc_tty_color__hashtable = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
128 /* --------------------------------------------------------------------------------------------- */
130 void
131 tty_colors_done (void)
133 tty_color_deinit_lib ();
134 g_free (tty_color_defaults__fg);
135 g_free (tty_color_defaults__bg);
136 g_free (tty_color_defaults__attrs);
138 g_hash_table_destroy (mc_tty_color__hashtable);
141 /* --------------------------------------------------------------------------------------------- */
143 gboolean
144 tty_use_colors (void)
146 return use_colors;
149 /* --------------------------------------------------------------------------------------------- */
152 tty_try_alloc_color_pair2 (const char *fg, const char *bg, const char *attrs,
153 gboolean is_temp_color)
155 gchar *color_pair;
156 tty_color_pair_t *mc_color_pair;
157 int ifg, ibg, attr;
159 if (fg == NULL || !strcmp (fg, "base"))
160 fg = tty_color_defaults__fg;
161 if (bg == NULL || !strcmp (bg, "base"))
162 bg = tty_color_defaults__bg;
163 if (attrs == NULL || !strcmp (attrs, "base"))
164 attrs = tty_color_defaults__attrs;
166 ifg = tty_color_get_index_by_name (fg);
167 ibg = tty_color_get_index_by_name (bg);
168 attr = tty_attr_get_bits (attrs);
170 color_pair = g_strdup_printf ("%d.%d.%d", ifg, ibg, attr);
171 if (color_pair == NULL)
172 return 0;
174 mc_color_pair =
175 (tty_color_pair_t *) g_hash_table_lookup (mc_tty_color__hashtable, (gpointer) color_pair);
177 if (mc_color_pair != NULL)
179 g_free (color_pair);
180 return mc_color_pair->pair_index;
183 mc_color_pair = g_try_new0 (tty_color_pair_t, 1);
184 if (mc_color_pair == NULL)
186 g_free (color_pair);
187 return 0;
190 mc_color_pair->is_temp = is_temp_color;
191 mc_color_pair->ifg = ifg;
192 mc_color_pair->ibg = ibg;
193 mc_color_pair->attr = attr;
194 mc_color_pair->pair_index = tty_color_get_next__color_pair_number ();
196 tty_color_try_alloc_pair_lib (mc_color_pair);
198 g_hash_table_insert (mc_tty_color__hashtable, (gpointer) color_pair, (gpointer) mc_color_pair);
200 return mc_color_pair->pair_index;
203 /* --------------------------------------------------------------------------------------------- */
206 tty_try_alloc_color_pair (const char *fg, const char *bg, const char *attrs)
208 return tty_try_alloc_color_pair2 (fg, bg, attrs, TRUE);
211 /* --------------------------------------------------------------------------------------------- */
213 void
214 tty_color_free_all_tmp (void)
216 tty_color_free_all (TRUE);
219 /* --------------------------------------------------------------------------------------------- */
221 void
222 tty_color_free_all_non_tmp (void)
224 tty_color_free_all (FALSE);
227 /* --------------------------------------------------------------------------------------------- */
229 void
230 tty_color_set_defaults (const char *fgcolor, const char *bgcolor, const char *attrs)
232 g_free (tty_color_defaults__fg);
233 g_free (tty_color_defaults__bg);
234 g_free (tty_color_defaults__attrs);
236 tty_color_defaults__fg = (fgcolor != NULL) ? g_strdup (fgcolor) : NULL;
237 tty_color_defaults__bg = (bgcolor != NULL) ? g_strdup (bgcolor) : NULL;
238 tty_color_defaults__attrs = (attrs != NULL) ? g_strdup (attrs) : NULL;
241 /* --------------------------------------------------------------------------------------------- */