Added two macro examples:
[zuwinko.git] / lib / tty / color.c
blobbcb553dbaf3f9dc61c639df98e6117b75a4f5693
1 /* Color setup.
2 Interface functions.
4 Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
5 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
7 Written by:
8 Andrew Borodin <aborodin@vmail.ru>, 2009
9 Slava Zanko <slavazanko@gmail.com>, 2009
10 Egmont Koblinger <egmont@gmail.com>, 2010
13 This program is free software; you can redistribute it and/or modify
14 it under the terms of the GNU General Public License as published by
15 the Free Software Foundation; either version 2 of the License, or
16 (at your option) any later version.
18 This program is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
23 You should have received a copy of the GNU General Public License
24 along with this program; if not, write to the Free Software
25 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
27 /** \file color.c
28 * \brief Source: color setup
31 #include <config.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <sys/types.h> /* size_t */
38 #include "lib/global.h"
40 #include "tty.h"
41 #include "color.h"
43 #include "color-internal.h"
45 /*** global variables ****************************************************************************/
47 char *command_line_colors = NULL;
49 static char *tty_color_defaults__fg = NULL;
50 static char *tty_color_defaults__bg = NULL;
51 static char *tty_color_defaults__attrs = NULL;
53 /* Set if we are actually using colors */
54 gboolean use_colors = FALSE;
56 /*** file scope macro definitions ****************************************************************/
58 /*** file scope type declarations ****************************************************************/
60 /*** file scope variables ************************************************************************/
62 static GHashTable *mc_tty_color__hashtable = NULL;
64 /*** file scope functions ************************************************************************/
65 /* --------------------------------------------------------------------------------------------- */
67 static gboolean
68 tty_color_free_condition_cb (gpointer key, gpointer value, gpointer user_data)
70 gboolean is_temp_color;
71 tty_color_pair_t *mc_color_pair;
72 (void) key;
74 is_temp_color = user_data != NULL;
75 mc_color_pair = (tty_color_pair_t *) value;
76 return (mc_color_pair->is_temp == is_temp_color);
79 /* --------------------------------------------------------------------------------------------- */
81 static void
82 tty_color_free_all (gboolean is_temp_color)
84 g_hash_table_foreach_remove (mc_tty_color__hashtable, tty_color_free_condition_cb,
85 is_temp_color ? GINT_TO_POINTER (1) : NULL);
88 /* --------------------------------------------------------------------------------------------- */
90 static gboolean
91 tty_color_get_next_cpn_cb (gpointer key, gpointer value, gpointer user_data)
93 size_t cp;
94 tty_color_pair_t *mc_color_pair;
95 (void) key;
97 cp = GPOINTER_TO_INT (user_data);
98 mc_color_pair = (tty_color_pair_t *) value;
100 return (cp == mc_color_pair->pair_index);
103 /* --------------------------------------------------------------------------------------------- */
105 static size_t
106 tty_color_get_next__color_pair_number (void)
108 const size_t cp_count = g_hash_table_size (mc_tty_color__hashtable);
109 size_t cp;
111 for (cp = 0; cp < cp_count; cp++)
112 if (g_hash_table_find (mc_tty_color__hashtable, tty_color_get_next_cpn_cb,
113 GINT_TO_POINTER (cp)) == NULL)
114 break;
116 return cp;
119 /* --------------------------------------------------------------------------------------------- */
120 /*** public functions ****************************************************************************/
121 /* --------------------------------------------------------------------------------------------- */
123 void
124 tty_init_colors (gboolean disable, gboolean force)
126 tty_color_init_lib (disable, force);
127 mc_tty_color__hashtable = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
130 /* --------------------------------------------------------------------------------------------- */
132 void
133 tty_colors_done (void)
135 tty_color_deinit_lib ();
136 g_free (tty_color_defaults__fg);
137 g_free (tty_color_defaults__bg);
138 g_free (tty_color_defaults__attrs);
140 g_hash_table_destroy (mc_tty_color__hashtable);
143 /* --------------------------------------------------------------------------------------------- */
145 gboolean
146 tty_use_colors (void)
148 return use_colors;
151 /* --------------------------------------------------------------------------------------------- */
154 tty_try_alloc_color_pair2 (const char *fg, const char *bg, const char *attrs,
155 gboolean is_temp_color)
157 gchar *color_pair;
158 tty_color_pair_t *mc_color_pair;
159 int ifg, ibg, attr;
161 if (fg == NULL || !strcmp (fg, "base"))
162 fg = tty_color_defaults__fg;
163 if (bg == NULL || !strcmp (bg, "base"))
164 bg = tty_color_defaults__bg;
165 if (attrs == NULL || !strcmp (attrs, "base"))
166 attrs = tty_color_defaults__attrs;
168 ifg = tty_color_get_index_by_name (fg);
169 ibg = tty_color_get_index_by_name (bg);
170 attr = tty_attr_get_bits (attrs);
172 color_pair = g_strdup_printf ("%d.%d.%d", ifg, ibg, attr);
173 if (color_pair == NULL)
174 return 0;
176 mc_color_pair =
177 (tty_color_pair_t *) g_hash_table_lookup (mc_tty_color__hashtable, (gpointer) color_pair);
179 if (mc_color_pair != NULL)
181 g_free (color_pair);
182 return mc_color_pair->pair_index;
185 mc_color_pair = g_try_new0 (tty_color_pair_t, 1);
186 if (mc_color_pair == NULL)
188 g_free (color_pair);
189 return 0;
192 mc_color_pair->is_temp = is_temp_color;
193 mc_color_pair->ifg = ifg;
194 mc_color_pair->ibg = ibg;
195 mc_color_pair->attr = attr;
196 mc_color_pair->pair_index = tty_color_get_next__color_pair_number ();
198 tty_color_try_alloc_pair_lib (mc_color_pair);
200 g_hash_table_insert (mc_tty_color__hashtable, (gpointer) color_pair, (gpointer) mc_color_pair);
202 return mc_color_pair->pair_index;
205 /* --------------------------------------------------------------------------------------------- */
208 tty_try_alloc_color_pair (const char *fg, const char *bg, const char *attrs)
210 return tty_try_alloc_color_pair2 (fg, bg, attrs, TRUE);
213 /* --------------------------------------------------------------------------------------------- */
215 void
216 tty_color_free_all_tmp (void)
218 tty_color_free_all (TRUE);
221 /* --------------------------------------------------------------------------------------------- */
223 void
224 tty_color_free_all_non_tmp (void)
226 tty_color_free_all (FALSE);
229 /* --------------------------------------------------------------------------------------------- */
231 void
232 tty_color_set_defaults (const char *fgcolor, const char *bgcolor, const char *attrs)
234 g_free (tty_color_defaults__fg);
235 g_free (tty_color_defaults__bg);
236 g_free (tty_color_defaults__attrs);
238 tty_color_defaults__fg = (fgcolor != NULL) ? g_strdup (fgcolor) : NULL;
239 tty_color_defaults__bg = (bgcolor != NULL) ? g_strdup (bgcolor) : NULL;
240 tty_color_defaults__attrs = (attrs != NULL) ? g_strdup (attrs) : NULL;
243 /* --------------------------------------------------------------------------------------------- */