This source file includes following definitions.
- mc_tty_color_attr_destroy_cb
- mc_tty_color_save_attr
- color_get_attr
- tty_color_init_lib
- tty_color_deinit_lib
- tty_color_try_alloc_lib_pair
- tty_setcolor
- tty_set_normal_attrs
- tty_use_256colors
- tty_use_truecolors
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 #include <config.h>
33
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <sys/types.h>
38
39 #include "lib/global.h"
40
41 #include "tty.h"
42 #include "tty-ncurses.h"
43 #include "color.h"
44 #include "color-internal.h"
45
46
47
48
49
50
51
52
53
54
55
56 static GHashTable *mc_tty_color_color_pair_attrs = NULL;
57 static int overlay_colors = 0;
58
59
60
61
62
63 static inline void
64 mc_tty_color_attr_destroy_cb (gpointer data)
65 {
66 g_free (data);
67 }
68
69
70
71 static void
72 mc_tty_color_save_attr (int color_pair, int color_attr)
73 {
74 int *attr, *key;
75
76 attr = g_try_new0 (int, 1);
77 if (attr == NULL)
78 return;
79
80 key = g_try_new (int, 1);
81 if (key == NULL)
82 {
83 g_free (attr);
84 return;
85 }
86
87 *key = color_pair;
88 *attr = color_attr;
89
90 g_hash_table_replace (mc_tty_color_color_pair_attrs, (gpointer) key, (gpointer) attr);
91 }
92
93
94
95 static int
96 color_get_attr (int color_pair)
97 {
98 int *fnd = NULL;
99
100 if (mc_tty_color_color_pair_attrs != NULL)
101 fnd = (int *) g_hash_table_lookup (mc_tty_color_color_pair_attrs, (gpointer) &color_pair);
102 return (fnd != NULL) ? *fnd : 0;
103 }
104
105
106
107
108
109 void
110 tty_color_init_lib (gboolean disable, gboolean force)
111 {
112 (void) force;
113
114 if (has_colors () && !disable)
115 {
116 use_colors = TRUE;
117 start_color ();
118 use_default_colors ();
119
120
121 tty_use_256colors (NULL);
122 tty_use_truecolors (NULL);
123 }
124
125 mc_tty_color_color_pair_attrs = g_hash_table_new_full (
126 g_int_hash, g_int_equal, mc_tty_color_attr_destroy_cb, mc_tty_color_attr_destroy_cb);
127 }
128
129
130
131 void
132 tty_color_deinit_lib (void)
133 {
134 g_hash_table_destroy (mc_tty_color_color_pair_attrs);
135 mc_tty_color_color_pair_attrs = NULL;
136 }
137
138
139
140 void
141 tty_color_try_alloc_lib_pair (tty_color_lib_pair_t *mc_color_pair)
142 {
143 int ifg, ibg, attr;
144
145 ifg = mc_color_pair->fg;
146 ibg = mc_color_pair->bg;
147 attr = mc_color_pair->attr;
148
149
150
151 if (COLORS <= 8 || (tty_use_truecolors (NULL) && overlay_colors <= 8))
152 {
153 if (ifg >= 8 && ifg < 16)
154 {
155 ifg &= 0x07;
156 attr |= A_BOLD;
157 }
158
159 if (ibg >= 8 && ibg < 16)
160 {
161 ibg &= 0x07;
162 }
163 }
164
165
166
167 if ((ifg & FLAG_TRUECOLOR) != 0)
168 {
169 ifg &= ~FLAG_TRUECOLOR;
170 if (ifg != 0 && ifg <= overlay_colors)
171 ifg += (1 << 16);
172 }
173
174 if ((ibg & FLAG_TRUECOLOR) != 0)
175 {
176 ibg &= ~FLAG_TRUECOLOR;
177 if (ibg != 0 && ibg <= overlay_colors)
178 ibg += (1 << 16);
179 }
180
181 #if NCURSES_VERSION_PATCH >= 20170401 && defined(NCURSES_EXT_COLORS) && defined(NCURSES_EXT_FUNCS) \
182 && defined(HAVE_NCURSES_WIDECHAR)
183 init_extended_pair (mc_color_pair->pair_index, ifg, ibg);
184 #else
185 init_pair (mc_color_pair->pair_index, ifg, ibg);
186 #endif
187 mc_tty_color_save_attr (mc_color_pair->pair_index, attr);
188 }
189
190
191
192 void
193 tty_setcolor (int color)
194 {
195 color = tty_maybe_map_color (color);
196 attr_set (color_get_attr (color), color, NULL);
197 }
198
199
200
201 void
202 tty_set_normal_attrs (void)
203 {
204 standend ();
205 }
206
207
208
209 gboolean
210 tty_use_256colors (GError **error)
211 {
212 (void) error;
213
214 overlay_colors = tty_tigetnum ("CO", NULL);
215
216 if (COLORS == 256 || (COLORS > 256 && overlay_colors == 256))
217 return TRUE;
218
219 if (tty_use_truecolors (NULL))
220 {
221 need_convert_256color = TRUE;
222 return TRUE;
223 }
224
225 g_set_error (error, MC_ERROR, -1,
226 _ ("\nIf your terminal supports 256 colors, you need to set your TERM\n"
227 "environment variable to match your terminal, perhaps using\n"
228 "a *-256color or *-direct256 variant. Use the 'toe -a'\n"
229 "command to list all available variants on your system.\n"));
230 return FALSE;
231 }
232
233
234
235 gboolean
236 tty_use_truecolors (GError **error)
237 {
238
239
240 #if !(NCURSES_VERSION_PATCH >= 20170401 && defined(NCURSES_EXT_COLORS) \
241 && defined(NCURSES_EXT_FUNCS) && defined(HAVE_NCURSES_WIDECHAR))
242 g_set_error (error, MC_ERROR, -1,
243 _ ("For true color support, you need version 6.1 or later of the ncurses\n"
244 "library with wide character and ABI 6 or higher support.\n"
245 "Please upgrade your system.\n"));
246 return FALSE;
247 #else
248
249
250 if (!(tty_tigetflag ("RGB", NULL) && COLORS == COLORS_TRUECOLOR))
251 {
252 g_set_error (
253 error, MC_ERROR, -1,
254 _ ("\nIf your terminal supports true colors, you need to set your TERM\n"
255 "environment variable to a *-direct256, *-direct16, or *-direct variant.\n"
256 "Use the 'toe -a' command to list all available variants on your system.\n"));
257 return FALSE;
258 }
259
260 overlay_colors = tty_tigetnum ("CO", NULL);
261
262 return TRUE;
263 #endif
264 }
265
266