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