This source file includes following definitions.
- mc_color__deinit
- tty_color_free_temp_cb
- tty_color_get_next_cpn_cb
- tty_color_get_next__color_pair_number
- tty_init_colors
- tty_colors_done
- tty_use_colors
- tty_try_alloc_color_pair
- tty_color_free_temp
- tty_color_free_all
- tty_color_set_defaults
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33 #include <config.h>
34
35 #include <stdio.h>
36 #include <stdlib.h>
37 #include <string.h>
38 #include <sys/types.h>
39
40 #include "lib/global.h"
41
42 #include "tty.h"
43 #include "color.h"
44
45 #include "color-internal.h"
46
47
48
49
50 static tty_color_pair_t tty_color_defaults =
51 {
52 .fg = NULL,
53 .bg = NULL,
54 .attrs = NULL,
55 .pair_index = 0
56 };
57
58
59
60 gboolean use_colors = FALSE;
61
62
63
64
65
66
67
68
69
70 static GHashTable *mc_tty_color__hashtable = NULL;
71
72
73
74
75
76 static void
77 mc_color__deinit (tty_color_pair_t *color)
78 {
79 g_free (color->fg);
80 g_free (color->bg);
81 g_free (color->attrs);
82 }
83
84
85
86 static gboolean
87 tty_color_free_temp_cb (gpointer key, gpointer value, gpointer user_data)
88 {
89 (void) key;
90 (void) user_data;
91
92 return ((tty_color_lib_pair_t *) value)->is_temp;
93 }
94
95
96
97 static gboolean
98 tty_color_get_next_cpn_cb (gpointer key, gpointer value, gpointer user_data)
99 {
100 tty_color_lib_pair_t *mc_color_pair = (tty_color_lib_pair_t *) value;
101 size_t cp = GPOINTER_TO_SIZE (user_data);
102
103 (void) key;
104
105 return (cp == mc_color_pair->pair_index);
106 }
107
108
109
110 static size_t
111 tty_color_get_next__color_pair_number (void)
112 {
113 size_t cp_count, cp;
114
115 cp_count = g_hash_table_size (mc_tty_color__hashtable);
116 for (cp = 0; cp < cp_count; cp++)
117 if (g_hash_table_find (mc_tty_color__hashtable, tty_color_get_next_cpn_cb,
118 GSIZE_TO_POINTER (cp)) == NULL)
119 break;
120
121 return cp;
122 }
123
124
125
126
127
128 void
129 tty_init_colors (gboolean disable, gboolean force)
130 {
131 tty_color_init_lib (disable, force);
132 mc_tty_color__hashtable = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
133 }
134
135
136
137 void
138 tty_colors_done (void)
139 {
140 tty_color_deinit_lib ();
141 mc_color__deinit (&tty_color_defaults);
142 g_hash_table_destroy (mc_tty_color__hashtable);
143 }
144
145
146
147 gboolean
148 tty_use_colors (void)
149 {
150 return use_colors;
151 }
152
153
154
155 int
156 tty_try_alloc_color_pair (const tty_color_pair_t *color, gboolean is_temp)
157 {
158 gboolean is_base;
159 gchar *color_pair;
160 tty_color_lib_pair_t *mc_color_pair;
161 int ifg, ibg, attr;
162
163 is_base = (color->fg == NULL || strcmp (color->fg, "base") == 0);
164 ifg = tty_color_get_index_by_name (is_base ? tty_color_defaults.fg : color->fg);
165 is_base = (color->bg == NULL || strcmp (color->bg, "base") == 0);
166 ibg = tty_color_get_index_by_name (is_base ? tty_color_defaults.bg : color->bg);
167 is_base = (color->attrs == NULL || strcmp (color->attrs, "base") == 0);
168 attr = tty_attr_get_bits (is_base ? tty_color_defaults.attrs : color->attrs);
169
170 color_pair = g_strdup_printf ("%d.%d.%d", ifg, ibg, attr);
171 if (color_pair == NULL)
172 return 0;
173
174 mc_color_pair =
175 (tty_color_lib_pair_t *) g_hash_table_lookup (mc_tty_color__hashtable,
176 (gpointer) color_pair);
177
178 if (mc_color_pair != NULL)
179 {
180 g_free (color_pair);
181 return mc_color_pair->pair_index;
182 }
183
184 mc_color_pair = g_try_new0 (tty_color_lib_pair_t, 1);
185 if (mc_color_pair == NULL)
186 {
187 g_free (color_pair);
188 return 0;
189 }
190
191 mc_color_pair->is_temp = is_temp;
192 mc_color_pair->fg = ifg;
193 mc_color_pair->bg = ibg;
194 mc_color_pair->attr = attr;
195 mc_color_pair->pair_index = tty_color_get_next__color_pair_number ();
196
197 tty_color_try_alloc_lib_pair (mc_color_pair);
198
199 g_hash_table_insert (mc_tty_color__hashtable, (gpointer) color_pair, (gpointer) mc_color_pair);
200
201 return mc_color_pair->pair_index;
202 }
203
204
205
206 void
207 tty_color_free_temp (void)
208 {
209 g_hash_table_foreach_remove (mc_tty_color__hashtable, tty_color_free_temp_cb, NULL);
210 }
211
212
213
214 void
215 tty_color_free_all (void)
216 {
217 g_hash_table_remove_all (mc_tty_color__hashtable);
218 }
219
220
221
222 void
223 tty_color_set_defaults (const tty_color_pair_t *color)
224 {
225 mc_color__deinit (&tty_color_defaults);
226
227 tty_color_defaults.fg = g_strdup (color->fg);
228 tty_color_defaults.bg = g_strdup (color->bg);
229 tty_color_defaults.attrs = g_strdup (color->attrs);
230 tty_color_defaults.pair_index = 0;
231 }
232
233