4 Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
5 2007, 2008, 2009 Free Software Foundation, Inc.
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. */
26 * \brief Source: color setup
34 #include <sys/types.h> /* size_t */
36 #include "lib/global.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 /* --------------------------------------------------------------------------------------------- */
65 color_hash_destroy_key (gpointer data
)
70 /* --------------------------------------------------------------------------------------------- */
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 /* --------------------------------------------------------------------------------------------- */
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
;
88 is_temp_color
= user_data
!= NULL
;
89 mc_color_pair
= (tty_color_pair_t
*) value
;
90 return (mc_color_pair
->is_temp
== is_temp_color
);
93 /* --------------------------------------------------------------------------------------------- */
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 (is_temp_color
) ? (gpointer
) 1 : NULL
);
102 /* --------------------------------------------------------------------------------------------- */
105 tty_color_get_next_cpn_cb (gpointer key
, gpointer value
, gpointer user_data
)
108 tty_color_pair_t
*mc_color_pair
;
111 cp
= (size_t) user_data
;
112 mc_color_pair
= (tty_color_pair_t
*) value
;
114 if (cp
== mc_color_pair
->pair_index
)
120 /* --------------------------------------------------------------------------------------------- */
123 tty_color_get_next__color_pair_number ()
125 size_t cp_count
= g_hash_table_size (mc_tty_color__hashtable
);
128 for (cp
= 0; cp
< cp_count
; cp
++)
130 if (g_hash_table_find (mc_tty_color__hashtable
, tty_color_get_next_cpn_cb
, (gpointer
) cp
) ==
137 /* --------------------------------------------------------------------------------------------- */
138 /*** public functions ****************************************************************************/
139 /* --------------------------------------------------------------------------------------------- */
142 tty_init_colors (gboolean disable
, gboolean force
)
144 tty_color_init_lib (disable
, force
);
145 mc_tty_color__hashtable
= g_hash_table_new_full (g_str_hash
, g_str_equal
,
146 color_hash_destroy_key
,
147 color_hash_destroy_value
);
150 /* --------------------------------------------------------------------------------------------- */
153 tty_colors_done (void)
155 tty_color_deinit_lib ();
156 g_free (tty_color_defaults__fg
);
157 g_free (tty_color_defaults__bg
);
159 g_hash_table_destroy (mc_tty_color__hashtable
);
162 /* --------------------------------------------------------------------------------------------- */
165 tty_use_colors (void)
170 /* --------------------------------------------------------------------------------------------- */
173 tty_try_alloc_color_pair2 (const char *fg
, const char *bg
, gboolean is_temp_color
)
176 tty_color_pair_t
*mc_color_pair
;
177 const char *c_fg
, *c_bg
;
180 fg
= tty_color_defaults__fg
;
184 bg
= tty_color_defaults__bg
;
186 c_fg
= tty_color_get_valid_name (fg
);
187 c_bg
= tty_color_get_valid_name (bg
);
189 color_pair
= g_strdup_printf ("%s.%s", c_fg
, c_bg
);
190 if (color_pair
== NULL
)
194 (tty_color_pair_t
*) g_hash_table_lookup (mc_tty_color__hashtable
, (gpointer
) color_pair
);
196 if (mc_color_pair
!= NULL
)
199 return mc_color_pair
->pair_index
;
202 mc_color_pair
= g_try_new0 (tty_color_pair_t
, 1);
203 if (mc_color_pair
== NULL
)
209 mc_color_pair
->is_temp
= is_temp_color
;
210 mc_color_pair
->cfg
= c_fg
;
211 mc_color_pair
->cbg
= c_bg
;
212 mc_color_pair
->ifg
= tty_color_get_index_by_name (c_fg
);
213 mc_color_pair
->ibg
= tty_color_get_index_by_name (c_bg
);
214 mc_color_pair
->pair_index
= tty_color_get_next__color_pair_number ();
216 tty_color_try_alloc_pair_lib (mc_color_pair
);
218 g_hash_table_insert (mc_tty_color__hashtable
, (gpointer
) color_pair
, (gpointer
) mc_color_pair
);
220 return mc_color_pair
->pair_index
;
223 /* --------------------------------------------------------------------------------------------- */
226 tty_try_alloc_color_pair (const char *fg
, const char *bg
)
228 return tty_try_alloc_color_pair2 (fg
, bg
, TRUE
);
231 /* --------------------------------------------------------------------------------------------- */
234 tty_color_free_all_tmp (void)
236 tty_color_free_all (TRUE
);
239 /* --------------------------------------------------------------------------------------------- */
242 tty_color_free_all_non_tmp (void)
244 tty_color_free_all (FALSE
);
247 /* --------------------------------------------------------------------------------------------- */
250 tty_color_set_defaults (const char *fgcolor
, const char *bgcolor
)
252 g_free (tty_color_defaults__fg
);
253 g_free (tty_color_defaults__fg
);
255 tty_color_defaults__fg
= (fgcolor
!= NULL
) ? g_strdup (fgcolor
) : NULL
;
256 tty_color_defaults__bg
= (bgcolor
!= NULL
) ? g_strdup (bgcolor
) : NULL
;
259 /* --------------------------------------------------------------------------------------------- */