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,
107 int fg1, int bg1, int fg2, 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,
156 COLOR_BLACK, COLOR_WHITE,
157 COLOR_BLACK, COLOR_WHITE | A_BOLD, A_REVERSE);
158 break;
159 case SPEC_A_BOLD:
160 mc_tty_color_pair_init_special (mc_color_pair,
161 COLOR_WHITE, COLOR_BLACK,
162 COLOR_WHITE, COLOR_BLACK, A_BOLD);
163 break;
164 case SPEC_A_BOLD_REVERSE:
165 mc_tty_color_pair_init_special (mc_color_pair,
166 COLOR_WHITE, COLOR_WHITE,
167 COLOR_WHITE, COLOR_WHITE, A_BOLD | A_REVERSE);
168 break;
169 case SPEC_A_UNDERLINE:
170 mc_tty_color_pair_init_special (mc_color_pair,
171 COLOR_WHITE, COLOR_BLACK,
172 COLOR_WHITE, COLOR_BLACK, A_UNDERLINE);
173 break;
174 default:
175 break;
176 }
177 }
178 else
179 {
180 int ifg, ibg, attr;
181
182 ifg = mc_color_pair->fg;
183 ibg = mc_color_pair->bg;
184 attr = mc_color_pair->attr;
185
186
187 if (!tty_use_256colors (NULL) && !tty_use_truecolors (NULL))
188 {
189 if (ifg >= 8 && ifg < 16)
190 {
191 ifg &= 0x07;
192 attr |= A_BOLD;
193 }
194
195 if (ibg >= 8 && ibg < 16)
196 {
197 ibg &= 0x07;
198
199 }
200 }
201
202 init_pair (mc_color_pair->pair_index, ifg, ibg);
203 mc_tty_color_save_attr (mc_color_pair->pair_index, attr);
204 }
205 }
206
207
208
209 void
210 tty_setcolor (int color)
211 {
212 attrset (COLOR_PAIR (color) | color_get_attr (color));
213 }
214
215
216
217 void
218 tty_lowlevel_setcolor (int color)
219 {
220 tty_setcolor (color);
221 }
222
223
224
225 void
226 tty_set_normal_attrs (void)
227 {
228 standend ();
229 }
230
231
232
233 gboolean
234 tty_use_256colors (GError **error)
235 {
236 (void) error;
237
238 return (COLORS == 256);
239 }
240
241
242
243 gboolean
244 tty_use_truecolors (GError **error)
245 {
246
247 g_set_error (error, MC_ERROR, -1, _("True color not supported with ncurses."));
248 return FALSE;
249 }
250
251