This source file includes following definitions.
- 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
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 #include "lib/util.h"
42
43 #include "tty.h"
44 #include "color.h"
45
46 #include "color-internal.h"
47
48
49
50 int *tty_color_role_to_pair = NULL;
51
52
53 gboolean use_colors = FALSE;
54
55 gboolean need_convert_256color = FALSE;
56
57
58
59
60
61
62
63
64
65 #ifdef HAVE_TESTS
66 extern GHashTable *mc_tty_color__hashtable;
67 #endif
68
69 MC_TESTABLE GHashTable *mc_tty_color__hashtable = NULL;
70
71
72
73
74
75 static gboolean
76 tty_color_free_temp_cb (gpointer key, gpointer value, gpointer user_data)
77 {
78 (void) key;
79 (void) user_data;
80
81 return ((tty_color_lib_pair_t *) value)->is_temp;
82 }
83
84
85
86 static gboolean
87 tty_color_get_next_cpn_cb (gpointer key, gpointer value, gpointer user_data)
88 {
89 tty_color_lib_pair_t *mc_color_pair = (tty_color_lib_pair_t *) value;
90 size_t cp = GPOINTER_TO_SIZE (user_data);
91
92 (void) key;
93
94 return (cp == mc_color_pair->pair_index);
95 }
96
97
98
99 static size_t
100 tty_color_get_next__color_pair_number (void)
101 {
102 size_t cp_count, cp;
103
104 cp_count = g_hash_table_size (mc_tty_color__hashtable);
105 for (cp = 0; cp < cp_count; cp++)
106 if (g_hash_table_find (mc_tty_color__hashtable, tty_color_get_next_cpn_cb,
107 GSIZE_TO_POINTER (cp))
108 == NULL)
109 break;
110
111 return cp;
112 }
113
114
115
116
117
118 void
119 tty_init_colors (gboolean disable, gboolean force, int color_map_size)
120 {
121 tty_color_role_to_pair = g_new (int, color_map_size);
122 tty_color_init_lib (disable, force);
123 mc_tty_color__hashtable = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
124 }
125
126
127
128 void
129 tty_colors_done (void)
130 {
131 tty_color_deinit_lib ();
132 g_hash_table_destroy (mc_tty_color__hashtable);
133 MC_PTR_FREE (tty_color_role_to_pair);
134 }
135
136
137
138 gboolean
139 tty_use_colors (void)
140 {
141 return use_colors;
142 }
143
144
145
146 int
147 tty_try_alloc_color_pair (const tty_color_pair_t *color, gboolean is_temp)
148 {
149 gchar *color_pair;
150 tty_color_lib_pair_t *mc_color_pair;
151 int ifg, ibg, attr;
152
153 ifg = tty_color_get_index_by_name (color->fg);
154 ibg = tty_color_get_index_by_name (color->bg);
155 attr = tty_attr_get_bits (color->attrs);
156
157 color_pair = g_strdup_printf ("%d.%d.%d", ifg, ibg, attr);
158 if (color_pair == NULL)
159 return 0;
160
161 mc_color_pair = (tty_color_lib_pair_t *) g_hash_table_lookup (mc_tty_color__hashtable,
162 (gpointer) color_pair);
163
164 if (mc_color_pair != NULL)
165 {
166 g_free (color_pair);
167 return mc_color_pair->pair_index;
168 }
169
170 if (need_convert_256color)
171 {
172 if ((ifg & FLAG_TRUECOLOR) == 0)
173 ifg = convert_256color_to_truecolor (ifg);
174
175 if ((ibg & FLAG_TRUECOLOR) == 0)
176 ibg = convert_256color_to_truecolor (ibg);
177 }
178
179 mc_color_pair = g_try_new0 (tty_color_lib_pair_t, 1);
180 if (mc_color_pair == NULL)
181 {
182 g_free (color_pair);
183 return 0;
184 }
185
186 mc_color_pair->is_temp = is_temp;
187 mc_color_pair->fg = ifg;
188 mc_color_pair->bg = ibg;
189 mc_color_pair->attr = attr;
190 mc_color_pair->pair_index = tty_color_get_next__color_pair_number ();
191
192 tty_color_try_alloc_lib_pair (mc_color_pair);
193
194 g_hash_table_insert (mc_tty_color__hashtable, (gpointer) color_pair, (gpointer) mc_color_pair);
195
196 return mc_color_pair->pair_index;
197 }
198
199
200
201 void
202 tty_color_free_temp (void)
203 {
204 g_hash_table_foreach_remove (mc_tty_color__hashtable, tty_color_free_temp_cb, NULL);
205 }
206
207
208
209 void
210 tty_color_free_all (void)
211 {
212 g_hash_table_remove_all (mc_tty_color__hashtable);
213 }
214
215