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