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 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
;
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 /* --------------------------------------------------------------------------------------------- */
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 /* --------------------------------------------------------------------------------------------- */
88 tty_color_get_next_cpn_cb (gpointer key
, gpointer value
, gpointer user_data
)
91 tty_color_pair_t
*mc_color_pair
;
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 /* --------------------------------------------------------------------------------------------- */
103 tty_color_get_next__color_pair_number (void)
105 const size_t cp_count
= g_hash_table_size (mc_tty_color__hashtable
);
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
)
116 /* --------------------------------------------------------------------------------------------- */
117 /*** public functions ****************************************************************************/
118 /* --------------------------------------------------------------------------------------------- */
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 /* --------------------------------------------------------------------------------------------- */
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 /* --------------------------------------------------------------------------------------------- */
142 tty_use_colors (void)
147 /* --------------------------------------------------------------------------------------------- */
150 tty_try_alloc_color_pair2 (const char *fg
, const char *bg
, gboolean is_temp_color
)
153 tty_color_pair_t
*mc_color_pair
;
154 const char *c_fg
, *c_bg
;
157 fg
= tty_color_defaults__fg
;
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
)
169 (tty_color_pair_t
*) g_hash_table_lookup (mc_tty_color__hashtable
, (gpointer
) color_pair
);
171 if (mc_color_pair
!= NULL
)
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
)
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 /* --------------------------------------------------------------------------------------------- */
209 tty_color_free_all_tmp (void)
211 tty_color_free_all (TRUE
);
214 /* --------------------------------------------------------------------------------------------- */
217 tty_color_free_all_non_tmp (void)
219 tty_color_free_all (FALSE
);
222 /* --------------------------------------------------------------------------------------------- */
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 /* --------------------------------------------------------------------------------------------- */