Merge branch '2206_jump_line'
[midnight-commander.git] / lib / tty / color.c
blob5e0d532385a03aac5f1c9362af54c56a4b70ca5e
1 /*
2 Color setup.
3 Interface functions.
5 Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
6 2007, 2008, 2009, 2010, 2011
7 The Free Software Foundation, Inc.
9 Written by:
10 Andrew Borodin <aborodin@vmail.ru>, 2009
11 Slava Zanko <slavazanko@gmail.com>, 2009
12 Egmont Koblinger <egmont@gmail.com>, 2010
14 This file is part of the Midnight Commander.
16 The Midnight Commander is free software: you can redistribute it
17 and/or modify it under the terms of the GNU General Public License as
18 published by the Free Software Foundation, either version 3 of the License,
19 or (at your option) any later version.
21 The Midnight Commander is distributed in the hope that it will be useful,
22 but WITHOUT ANY WARRANTY; without even the implied warranty of
23 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
24 GNU General Public License for more details.
26 You should have received a copy of the GNU General Public License
27 along with this program. If not, see <http://www.gnu.org/licenses/>.
30 /** \file color.c
31 * \brief Source: color setup
34 #include <config.h>
36 #include <stdio.h>
37 #include <stdlib.h>
38 #include <string.h>
39 #include <sys/types.h> /* size_t */
41 #include "lib/global.h"
43 #include "tty.h"
44 #include "color.h"
46 #include "color-internal.h"
48 /*** global variables ****************************************************************************/
50 static char *tty_color_defaults__fg = NULL;
51 static char *tty_color_defaults__bg = NULL;
52 static char *tty_color_defaults__attrs = NULL;
54 /* Set if we are actually using colors */
55 gboolean use_colors = FALSE;
57 /*** file scope macro definitions ****************************************************************/
59 /*** file scope type declarations ****************************************************************/
61 /*** file scope variables ************************************************************************/
63 static GHashTable *mc_tty_color__hashtable = NULL;
65 /*** file scope functions ************************************************************************/
66 /* --------------------------------------------------------------------------------------------- */
68 static gboolean
69 tty_color_free_condition_cb (gpointer key, gpointer value, gpointer user_data)
71 gboolean is_temp_color;
72 tty_color_pair_t *mc_color_pair;
73 (void) key;
75 is_temp_color = user_data != NULL;
76 mc_color_pair = (tty_color_pair_t *) value;
77 return (mc_color_pair->is_temp == is_temp_color);
80 /* --------------------------------------------------------------------------------------------- */
82 static void
83 tty_color_free_all (gboolean is_temp_color)
85 g_hash_table_foreach_remove (mc_tty_color__hashtable, tty_color_free_condition_cb,
86 is_temp_color ? GSIZE_TO_POINTER (1) : NULL);
89 /* --------------------------------------------------------------------------------------------- */
91 static gboolean
92 tty_color_get_next_cpn_cb (gpointer key, gpointer value, gpointer user_data)
94 size_t cp;
95 tty_color_pair_t *mc_color_pair;
96 (void) key;
98 cp = GPOINTER_TO_SIZE (user_data);
99 mc_color_pair = (tty_color_pair_t *) value;
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"))
163 fg = tty_color_defaults__fg;
164 if (bg == NULL || !strcmp (bg, "base"))
165 bg = tty_color_defaults__bg;
166 if (attrs == NULL || !strcmp (attrs, "base"))
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 = (fgcolor != NULL) ? g_strdup (fgcolor) : NULL;
240 tty_color_defaults__bg = (bgcolor != NULL) ? g_strdup (bgcolor) : NULL;
241 tty_color_defaults__attrs = (attrs != NULL) ? g_strdup (attrs) : NULL;
244 /* --------------------------------------------------------------------------------------------- */