"Fossies" - the Fresh Open Source Software Archive 
Member "mc-4.8.29/lib/tty/color-slang.c" (1 Jan 2023, 8095 Bytes) of package /linux/misc/mc-4.8.29.tar.xz:
As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) C and C++ source code syntax highlighting (style:
standard) with prefixed line numbers and
code folding option.
Alternatively you can here
view or
download the uninterpreted source code file.
For more information about "color-slang.c" see the
Fossies "Dox" file reference documentation and the last
Fossies "Diffs" side-by-side code changes report:
4.8.27_vs_4.8.28.
1 /*
2 Color setup for S_Lang screen library
3
4 Copyright (C) 1994-2022
5 Free Software Foundation, Inc.
6
7 Written by:
8 Andrew Borodin <aborodin@vmail.ru>, 2009
9 Egmont Koblinger <egmont@gmail.com>, 2010
10
11 This file is part of the Midnight Commander.
12
13 The Midnight Commander is free software: you can redistribute it
14 and/or modify it under the terms of the GNU General Public License as
15 published by the Free Software Foundation, either version 3 of the License,
16 or (at your option) any later version.
17
18 The Midnight Commander is distributed in the hope that it will be useful,
19 but WITHOUT ANY WARRANTY; without even the implied warranty of
20 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 GNU General Public License for more details.
22
23 You should have received a copy of the GNU General Public License
24 along with this program. If not, see <http://www.gnu.org/licenses/>.
25 */
26
27 /** \file color-slang.c
28 * \brief Source: S-Lang-specific color setup
29 */
30
31 #include <config.h>
32
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <sys/types.h> /* size_t */
37
38 #include "lib/global.h"
39 #include "lib/util.h" /* whitespace() */
40
41 #include "tty-slang.h"
42 #include "color.h" /* variables */
43 #include "color-internal.h"
44
45 /*** global variables ****************************************************************************/
46
47 /*** file scope macro definitions ****************************************************************/
48
49 /*** file scope type declarations ****************************************************************/
50
51 /*** file scope variables ************************************************************************/
52
53 /*** file scope functions ************************************************************************/
54 /* --------------------------------------------------------------------------------------------- */
55
56 static int
57 has_colors (gboolean disable, gboolean force)
58 {
59 mc_tty_color_disable = disable;
60
61 if (force || (getenv ("COLORTERM") != NULL))
62 SLtt_Use_Ansi_Colors = 1;
63
64 if (!mc_tty_color_disable)
65 {
66 const char *terminal = getenv ("TERM");
67 const size_t len = strlen (terminal);
68 char *cts = mc_global.tty.color_terminal_string;
69
70 /* check mc_global.tty.color_terminal_string */
71 while (*cts != '\0')
72 {
73 char *s;
74 size_t i = 0;
75
76 while (whitespace (*cts))
77 cts++;
78 s = cts;
79
80 while (*cts != '\0' && *cts != ',')
81 {
82 cts++;
83 i++;
84 }
85
86 if ((i != 0) && (i == len) && (strncmp (s, terminal, i) == 0))
87 SLtt_Use_Ansi_Colors = 1;
88
89 if (*cts == ',')
90 cts++;
91 }
92 }
93 return SLtt_Use_Ansi_Colors;
94 }
95
96 /* --------------------------------------------------------------------------------------------- */
97
98 static void
99 mc_tty_color_pair_init_special (tty_color_pair_t * mc_color_pair,
100 const char *fg1, const char *bg1,
101 const char *fg2, const char *bg2, SLtt_Char_Type mask)
102 {
103 if (SLtt_Use_Ansi_Colors != 0)
104 {
105 if (!mc_tty_color_disable)
106 {
107 SLtt_set_color (mc_color_pair->pair_index, (char *) "", (char *) fg1, (char *) bg1);
108 }
109 else
110 {
111 SLtt_set_color (mc_color_pair->pair_index, (char *) "", (char *) fg2, (char *) bg2);
112 }
113 }
114 else
115 {
116 SLtt_set_mono (mc_color_pair->pair_index, NULL, mask);
117 }
118 }
119
120 /* --------------------------------------------------------------------------------------------- */
121 /*** public functions ****************************************************************************/
122 /* --------------------------------------------------------------------------------------------- */
123
124 void
125 tty_color_init_lib (gboolean disable, gboolean force)
126 {
127 /* FIXME: if S-Lang is used, has_colors() must be called regardless
128 of whether we are interested in its result */
129 if (has_colors (disable, force) && !disable)
130 {
131 use_colors = TRUE;
132 }
133 }
134
135 /* --------------------------------------------------------------------------------------------- */
136
137 void
138 tty_color_deinit_lib (void)
139 {
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 "black", "white", "black", "lightgray", SLTT_REV_MASK);
154 break;
155 case SPEC_A_BOLD:
156 mc_tty_color_pair_init_special (mc_color_pair,
157 "white", "black", "white", "black", SLTT_BOLD_MASK);
158 break;
159 case SPEC_A_BOLD_REVERSE:
160 mc_tty_color_pair_init_special (mc_color_pair,
161 "white", "white",
162 "white", "white", SLTT_BOLD_MASK | SLTT_REV_MASK);
163 break;
164 case SPEC_A_UNDERLINE:
165 mc_tty_color_pair_init_special (mc_color_pair,
166 "white", "black", "white", "black", SLTT_ULINE_MASK);
167 break;
168 default:
169 break;
170 }
171 }
172 else
173 {
174 const char *fg, *bg;
175
176 fg = tty_color_get_name_by_index (mc_color_pair->ifg);
177 bg = tty_color_get_name_by_index (mc_color_pair->ibg);
178 SLtt_set_color (mc_color_pair->pair_index, (char *) "", (char *) fg, (char *) bg);
179 SLtt_add_color_attribute (mc_color_pair->pair_index, mc_color_pair->attr);
180 }
181 }
182
183 /* --------------------------------------------------------------------------------------------- */
184
185 void
186 tty_setcolor (int color)
187 {
188 SLsmg_set_color (color);
189 }
190
191 /* --------------------------------------------------------------------------------------------- */
192 /**
193 * Set colorpair by index, don't interpret S-Lang "emulated attributes"
194 */
195
196 void
197 tty_lowlevel_setcolor (int color)
198 {
199 SLsmg_set_color (color & 0x7F);
200 }
201
202 /* --------------------------------------------------------------------------------------------- */
203
204 void
205 tty_set_normal_attrs (void)
206 {
207 SLsmg_normal_video ();
208 }
209
210 /* --------------------------------------------------------------------------------------------- */
211
212 gboolean
213 tty_use_256colors (GError ** error)
214 {
215 gboolean ret;
216
217 ret = (SLtt_Use_Ansi_Colors && SLtt_tgetnum ((char *) "Co") == 256);
218
219 if (!ret)
220 g_set_error (error, MC_ERROR, -1,
221 _("Your terminal doesn't even seem to support 256 colors."));
222
223 return ret;
224 }
225
226 /* --------------------------------------------------------------------------------------------- */
227
228 gboolean
229 tty_use_truecolors (GError ** error)
230 {
231 char *colorterm;
232
233 /* True color is supported since slang-2.3.1 on 64-bit machines,
234 and expected to be supported from slang-3 on 32-bit machines:
235 http://lists.jedsoft.org/lists/slang-users/2016/0000014.html.
236 Check for sizeof (long) being 8, exactly as slang does. */
237 if (SLang_Version < 20301 || (sizeof (long) != 8 && SLang_Version < 30000))
238 {
239 g_set_error (error, MC_ERROR, -1, _("True color not supported in this slang version."));
240 return FALSE;
241 }
242
243 /* Duplicate slang's check so that we can pop up an error message
244 rather than silently use wrong colors. */
245 colorterm = getenv ("COLORTERM");
246 if (colorterm == NULL
247 || (strcmp (colorterm, "truecolor") != 0 && strcmp (colorterm, "24bit") != 0))
248 {
249 g_set_error (error, MC_ERROR, -1,
250 _("Set COLORTERM=truecolor if your terminal really supports true colors."));
251 return FALSE;
252 }
253
254 return TRUE;
255 }
256
257 /* --------------------------------------------------------------------------------------------- */