This source file includes following definitions.
- mc_tty_color_attr_destroy_cb
- mc_tty_color_save_attr
- color_get_attr
- mc_tty_color_pair_init_special
- tty_color_init_lib
- tty_color_deinit_lib
- tty_color_try_alloc_lib_pair
- tty_setcolor
- tty_lowlevel_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-ncurses.h"
42 #include "color.h"
43 #include "color-internal.h"
44
45
46
47
48
49
50
51
52
53
54
55 static GHashTable *mc_tty_color_color_pair_attrs = NULL;
56
57
58
59
60
61 static inline void
62 mc_tty_color_attr_destroy_cb (gpointer data)
63 {
64 g_free (data);
65 }
66
67
68
69 static void
70 mc_tty_color_save_attr (int color_pair, int color_attr)
71 {
72 int *attr, *key;
73
74 attr = g_try_new0 (int, 1);
75 if (attr == NULL)
76 return;
77
78 key = g_try_new (int, 1);
79 if (key == NULL)
80 {
81 g_free (attr);
82 return;
83 }
84
85 *key = color_pair;
86 *attr = color_attr;
87
88 g_hash_table_replace (mc_tty_color_color_pair_attrs, (gpointer) key, (gpointer) attr);
89 }
90
91
92
93 static int
94 color_get_attr (int color_pair)
95 {
96 int *fnd = NULL;
97
98 if (mc_tty_color_color_pair_attrs != NULL)
99 fnd = (int *) g_hash_table_lookup (mc_tty_color_color_pair_attrs, (gpointer) &color_pair);
100 return (fnd != NULL) ? *fnd : 0;
101 }
102
103
104
105 static void
106 mc_tty_color_pair_init_special (tty_color_lib_pair_t *mc_color_pair, int fg1, int bg1, int fg2,
107 int bg2, int attr)
108 {
109 if (has_colors () && !mc_tty_color_disable)
110 init_pair (mc_color_pair->pair_index, fg1, bg1);
111 else
112 init_pair (mc_color_pair->pair_index, fg2, bg2);
113 mc_tty_color_save_attr (mc_color_pair->pair_index, attr);
114 }
115
116
117
118
119
120 void
121 tty_color_init_lib (gboolean disable, gboolean force)
122 {
123 (void) force;
124
125 if (has_colors () && !disable)
126 {
127 use_colors = TRUE;
128 start_color ();
129 use_default_colors ();
130 }
131
132 mc_tty_color_color_pair_attrs = g_hash_table_new_full (
133 g_int_hash, g_int_equal, mc_tty_color_attr_destroy_cb, mc_tty_color_attr_destroy_cb);
134 }
135
136
137
138 void
139 tty_color_deinit_lib (void)
140 {
141 g_hash_table_destroy (mc_tty_color_color_pair_attrs);
142 mc_tty_color_color_pair_attrs = NULL;
143 }
144
145
146
147 void
148 tty_color_try_alloc_lib_pair (tty_color_lib_pair_t *mc_color_pair)
149 {
150 if (mc_color_pair->fg <= (int) SPEC_A_REVERSE)
151 {
152 switch (mc_color_pair->fg)
153 {
154 case SPEC_A_REVERSE:
155 mc_tty_color_pair_init_special (mc_color_pair, COLOR_BLACK, COLOR_WHITE, COLOR_BLACK,
156 COLOR_WHITE | A_BOLD, A_REVERSE);
157 break;
158 case SPEC_A_BOLD:
159 mc_tty_color_pair_init_special (mc_color_pair, COLOR_WHITE, COLOR_BLACK, COLOR_WHITE,
160 COLOR_BLACK, A_BOLD);
161 break;
162 case SPEC_A_BOLD_REVERSE:
163 mc_tty_color_pair_init_special (mc_color_pair, COLOR_WHITE, COLOR_WHITE, COLOR_WHITE,
164 COLOR_WHITE, A_BOLD | A_REVERSE);
165 break;
166 case SPEC_A_UNDERLINE:
167 mc_tty_color_pair_init_special (mc_color_pair, COLOR_WHITE, COLOR_BLACK, COLOR_WHITE,
168 COLOR_BLACK, A_UNDERLINE);
169 break;
170 default:
171 break;
172 }
173 }
174 else
175 {
176 int ifg, ibg, attr;
177
178 ifg = mc_color_pair->fg;
179 ibg = mc_color_pair->bg;
180 attr = mc_color_pair->attr;
181
182
183 if (!tty_use_256colors (NULL) && !tty_use_truecolors (NULL))
184 {
185 if (ifg >= 8 && ifg < 16)
186 {
187 ifg &= 0x07;
188 attr |= A_BOLD;
189 }
190
191 if (ibg >= 8 && ibg < 16)
192 {
193 ibg &= 0x07;
194
195 }
196 }
197
198 init_pair (mc_color_pair->pair_index, ifg, ibg);
199 mc_tty_color_save_attr (mc_color_pair->pair_index, attr);
200 }
201 }
202
203
204
205 void
206 tty_setcolor (int color)
207 {
208 attrset (COLOR_PAIR (color) | color_get_attr (color));
209 }
210
211
212
213 void
214 tty_lowlevel_setcolor (int color)
215 {
216 tty_setcolor (color);
217 }
218
219
220
221 void
222 tty_set_normal_attrs (void)
223 {
224 standend ();
225 }
226
227
228
229 gboolean
230 tty_use_256colors (GError **error)
231 {
232 (void) error;
233
234 return (COLORS == 256);
235 }
236
237
238
239 gboolean
240 tty_use_truecolors (GError **error)
241 {
242
243 g_set_error (error, MC_ERROR, -1, _ ("True color not supported with ncurses."));
244 return FALSE;
245 }
246
247