Ticket #2170: Color collisions
[midnight-commander.git] / lib / tty / color.c
blob7ff1e86f05108861300d6c134e37521378cfa664
1 /* Color setup.
2 Interface functions.
4 Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
5 2007, 2008, 2009 Free Software Foundation, Inc.
7 Written by:
8 Andrew Borodin <aborodin@vmail.ru>, 2009.
9 Slava Zanko <slavazanko@gmail.com>, 2009.
11 This program is free software; you can redistribute it and/or modify
12 it under the terms of the GNU General Public License as published by
13 the Free Software Foundation; either version 2 of the License, or
14 (at your option) any later version.
16 This program is distributed in the hope that it will be useful,
17 but WITHOUT ANY WARRANTY; without even the implied warranty of
18 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19 GNU General Public License for more details.
21 You should have received a copy of the GNU General Public License
22 along with this program; if not, write to the Free Software
23 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
25 /** \file color.c
26 * \brief Source: color setup
29 #include <config.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <sys/types.h> /* size_t */
36 #include "lib/global.h"
38 #include "tty.h"
39 #include "color.h"
41 #include "color-internal.h"
43 /*** global variables ****************************************************************************/
45 char *command_line_colors = NULL;
47 static char *tty_color_defaults__fg = NULL;
48 static char *tty_color_defaults__bg = NULL;
50 /* Set if we are actually using colors */
51 gboolean use_colors = FALSE;
53 /*** file scope macro definitions ****************************************************************/
55 /*** file scope type declarations ****************************************************************/
57 /*** file scope variables ************************************************************************/
59 static GHashTable *mc_tty_color__hashtable = NULL;
61 /*** file scope functions ************************************************************************/
62 /* --------------------------------------------------------------------------------------------- */
64 static gboolean
65 tty_color_free_condition_cb (gpointer key, gpointer value, gpointer user_data)
67 gboolean is_temp_color;
68 tty_color_pair_t *mc_color_pair;
69 (void) key;
71 is_temp_color = user_data != NULL;
72 mc_color_pair = (tty_color_pair_t *) value;
73 return (mc_color_pair->is_temp == is_temp_color);
76 /* --------------------------------------------------------------------------------------------- */
78 static void
79 tty_color_free_all (gboolean is_temp_color)
81 g_hash_table_foreach_remove (mc_tty_color__hashtable, tty_color_free_condition_cb,
82 is_temp_color ? GINT_TO_POINTER (1) : NULL);
85 /* --------------------------------------------------------------------------------------------- */
87 static gboolean
88 tty_color_get_next_cpn_cb (gpointer key, gpointer value, gpointer user_data)
90 int cp;
91 tty_color_pair_t *mc_color_pair;
92 (void) key;
94 cp = GPOINTER_TO_INT (user_data);
95 mc_color_pair = (tty_color_pair_t *) value;
97 return (cp == mc_color_pair->pair_index);
100 /* --------------------------------------------------------------------------------------------- */
102 static int
103 tty_color_get_next__color_pair_number (void)
105 const size_t cp_count = g_hash_table_size (mc_tty_color__hashtable);
106 size_t cp;
108 for (cp = 0; cp < cp_count; cp++)
109 if (g_hash_table_find (mc_tty_color__hashtable, tty_color_get_next_cpn_cb,
110 GINT_TO_POINTER (cp)) == NULL)
111 break;
113 return cp;
116 /* --------------------------------------------------------------------------------------------- */
117 /*** public functions ****************************************************************************/
118 /* --------------------------------------------------------------------------------------------- */
120 void
121 tty_init_colors (gboolean disable, gboolean force)
123 tty_color_init_lib (disable, force);
124 mc_tty_color__hashtable = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
127 /* --------------------------------------------------------------------------------------------- */
129 void
130 tty_colors_done (void)
132 tty_color_deinit_lib ();
133 g_free (tty_color_defaults__fg);
134 g_free (tty_color_defaults__bg);
136 g_hash_table_destroy (mc_tty_color__hashtable);
139 /* --------------------------------------------------------------------------------------------- */
141 gboolean
142 tty_use_colors (void)
144 return use_colors;
147 /* --------------------------------------------------------------------------------------------- */
150 tty_try_alloc_color_pair2 (const char *fg, const char *bg, gboolean is_temp_color)
152 gchar *color_pair;
153 tty_color_pair_t *mc_color_pair;
154 const char *c_fg, *c_bg;
156 if (fg == NULL)
157 fg = tty_color_defaults__fg;
158 if (bg == NULL)
159 bg = tty_color_defaults__bg;
161 c_fg = tty_color_get_valid_name (fg);
162 c_bg = tty_color_get_valid_name (bg);
164 color_pair = g_strdup_printf ("%s.%s", c_fg, c_bg);
165 if (color_pair == NULL)
166 return 0;
168 mc_color_pair =
169 (tty_color_pair_t *) g_hash_table_lookup (mc_tty_color__hashtable, (gpointer) color_pair);
171 if (mc_color_pair != NULL)
173 g_free (color_pair);
174 return mc_color_pair->pair_index;
177 mc_color_pair = g_try_new0 (tty_color_pair_t, 1);
178 if (mc_color_pair == NULL)
180 g_free (color_pair);
181 return 0;
184 mc_color_pair->is_temp = is_temp_color;
185 mc_color_pair->cfg = c_fg;
186 mc_color_pair->cbg = c_bg;
187 mc_color_pair->ifg = tty_color_get_index_by_name (c_fg);
188 mc_color_pair->ibg = tty_color_get_index_by_name (c_bg);
189 mc_color_pair->pair_index = tty_color_get_next__color_pair_number ();
191 tty_color_try_alloc_pair_lib (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_try_alloc_color_pair (const char *fg, const char *bg)
203 return tty_try_alloc_color_pair2 (fg, bg, TRUE);
206 /* --------------------------------------------------------------------------------------------- */
208 void
209 tty_color_free_all_tmp (void)
211 tty_color_free_all (TRUE);
214 /* --------------------------------------------------------------------------------------------- */
216 void
217 tty_color_free_all_non_tmp (void)
219 tty_color_free_all (FALSE);
222 /* --------------------------------------------------------------------------------------------- */
224 void
225 tty_color_set_defaults (const char *fgcolor, const char *bgcolor)
227 g_free (tty_color_defaults__fg);
228 g_free (tty_color_defaults__fg);
230 tty_color_defaults__fg = (fgcolor != NULL) ? g_strdup (fgcolor) : NULL;
231 tty_color_defaults__bg = (bgcolor != NULL) ? g_strdup (bgcolor) : NULL;
234 /* --------------------------------------------------------------------------------------------- */