Moved dir $(srcdir)/syntax into $(srcdir)/misc/syntax
[midnight-commander.git] / src / tty / color.c
blobe9ccf1c7d8452473d7c2a9cefc41178068df4e69
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 "../../src/global.h"
38 #include "../../src/tty/tty.h"
39 #include "../../src/tty/color.h"
41 #include "../../src/tty/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 inline void
65 color_hash_destroy_key (gpointer data)
67 g_free (data);
70 /* --------------------------------------------------------------------------------------------- */
72 static void
73 color_hash_destroy_value (gpointer data)
75 tty_color_pair_t *mc_color_pair = (tty_color_pair_t *) data;
76 g_free (mc_color_pair);
79 /* --------------------------------------------------------------------------------------------- */
81 static gboolean
82 tty_color_free_condition_cb (gpointer key, gpointer value, gpointer user_data)
84 gboolean is_temp_color;
85 tty_color_pair_t *mc_color_pair;
86 (void) key;
88 is_temp_color = (gboolean) user_data;
89 mc_color_pair = (tty_color_pair_t *) value;
90 return (mc_color_pair->is_temp == is_temp_color);
93 /* --------------------------------------------------------------------------------------------- */
95 static void
96 tty_color_free_all (gboolean is_temp_color)
98 g_hash_table_foreach_remove (mc_tty_color__hashtable, tty_color_free_condition_cb,
99 (gpointer) is_temp_color);
102 /* --------------------------------------------------------------------------------------------- */
104 static gboolean
105 tty_color_get_next_cpn_cb (gpointer key, gpointer value, gpointer user_data)
107 int cp;
108 tty_color_pair_t *mc_color_pair;
109 (void) key;
111 cp = (int) user_data;
112 mc_color_pair = (tty_color_pair_t *) value;
114 if (cp == mc_color_pair->pair_index)
115 return TRUE;
117 return FALSE;
120 /* --------------------------------------------------------------------------------------------- */
122 static int
123 tty_color_get_next__color_pair_number ()
125 int cp_count = g_hash_table_size (mc_tty_color__hashtable);
126 int cp = 0;
128 for (cp = 0; cp < cp_count; cp++) {
129 if (g_hash_table_find (mc_tty_color__hashtable, tty_color_get_next_cpn_cb, (gpointer) cp) ==
130 NULL)
131 return cp;
133 return cp;
136 /* --------------------------------------------------------------------------------------------- */
137 /*** public functions ****************************************************************************/
138 /* --------------------------------------------------------------------------------------------- */
140 void
141 tty_init_colors (gboolean disable, gboolean force)
143 tty_color_init_lib (disable, force);
144 mc_tty_color__hashtable = g_hash_table_new_full (g_str_hash, g_str_equal,
145 color_hash_destroy_key,
146 color_hash_destroy_value);
149 /* --------------------------------------------------------------------------------------------- */
151 void
152 tty_colors_done (void)
154 tty_color_deinit_lib ();
155 g_free (tty_color_defaults__fg);
156 g_free (tty_color_defaults__bg);
158 g_hash_table_destroy (mc_tty_color__hashtable);
161 /* --------------------------------------------------------------------------------------------- */
163 gboolean
164 tty_use_colors (void)
166 return use_colors;
169 /* --------------------------------------------------------------------------------------------- */
172 tty_try_alloc_color_pair2 (const char *fg, const char *bg, gboolean is_temp_color)
174 gchar *color_pair;
175 tty_color_pair_t *mc_color_pair;
176 const char *c_fg, *c_bg;
178 if (fg == NULL)
179 fg = tty_color_defaults__fg;
181 if (bg == NULL) {
182 bg = tty_color_defaults__bg;
184 c_fg = tty_color_get_valid_name (fg);
185 c_bg = tty_color_get_valid_name (bg);
187 color_pair = g_strdup_printf ("%s.%s", c_fg, c_bg);
188 if (color_pair == NULL)
189 return 0;
191 mc_color_pair =
192 (tty_color_pair_t *) g_hash_table_lookup (mc_tty_color__hashtable, (gpointer) color_pair);
194 if (mc_color_pair != NULL) {
195 g_free (color_pair);
196 return mc_color_pair->pair_index;
199 mc_color_pair = g_try_new0 (tty_color_pair_t, 1);
200 if (mc_color_pair == NULL) {
201 g_free (color_pair);
202 return 0;
205 mc_color_pair->is_temp = is_temp_color;
206 mc_color_pair->cfg = c_fg;
207 mc_color_pair->cbg = c_bg;
208 mc_color_pair->ifg = tty_color_get_index_by_name (c_fg);
209 mc_color_pair->ibg = tty_color_get_index_by_name (c_bg);
210 mc_color_pair->pair_index = tty_color_get_next__color_pair_number ();
212 tty_color_try_alloc_pair_lib (mc_color_pair);
214 g_hash_table_insert (mc_tty_color__hashtable, (gpointer) color_pair, (gpointer) mc_color_pair);
216 return mc_color_pair->pair_index;
219 /* --------------------------------------------------------------------------------------------- */
222 tty_try_alloc_color_pair (const char *fg, const char *bg)
224 return tty_try_alloc_color_pair2 (fg, bg, TRUE);
227 /* --------------------------------------------------------------------------------------------- */
229 void
230 tty_color_free_all_tmp (void)
232 tty_color_free_all (TRUE);
235 /* --------------------------------------------------------------------------------------------- */
237 void
238 tty_color_free_all_non_tmp (void)
240 tty_color_free_all (FALSE);
243 /* --------------------------------------------------------------------------------------------- */
245 void
246 tty_color_set_defaults (const char *fgcolor, const char *bgcolor)
248 g_free (tty_color_defaults__fg);
249 g_free (tty_color_defaults__fg);
251 tty_color_defaults__fg = (fgcolor != NULL) ? g_strdup (fgcolor) : NULL;
252 tty_color_defaults__bg = (bgcolor != NULL) ? g_strdup (bgcolor) : NULL;
255 /* --------------------------------------------------------------------------------------------- */