2 Color setup for S_Lang screen library
4 Copyright (C) 1994-2023
5 Free Software Foundation, Inc.
8 Andrew Borodin <aborodin@vmail.ru>, 2009
9 Egmont Koblinger <egmont@gmail.com>, 2010
11 This file is part of the Midnight Commander.
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.
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.
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/>.
27 /** \file color-slang.c
28 * \brief Source: S-Lang-specific color setup
36 #include <sys/types.h> /* size_t */
38 #include "lib/global.h"
39 #include "lib/util.h" /* whitespace() */
41 #include "tty-slang.h"
42 #include "color.h" /* variables */
43 #include "color-internal.h"
45 /*** global variables ****************************************************************************/
47 /*** file scope macro definitions ****************************************************************/
49 /*** file scope type declarations ****************************************************************/
51 /*** forward declarations (file scope functions) *************************************************/
53 /*** file scope variables ************************************************************************/
55 /* --------------------------------------------------------------------------------------------- */
56 /*** file scope functions ************************************************************************/
57 /* --------------------------------------------------------------------------------------------- */
60 has_colors (gboolean disable
, gboolean force
)
62 mc_tty_color_disable
= disable
;
64 if (force
|| (getenv ("COLORTERM") != NULL
))
65 SLtt_Use_Ansi_Colors
= 1;
67 if (!mc_tty_color_disable
)
69 const char *terminal
= getenv ("TERM");
70 const size_t len
= strlen (terminal
);
71 char *cts
= mc_global
.tty
.color_terminal_string
;
73 /* check mc_global.tty.color_terminal_string */
79 while (whitespace (*cts
))
83 while (*cts
!= '\0' && *cts
!= ',')
89 if ((i
!= 0) && (i
== len
) && (strncmp (s
, terminal
, i
) == 0))
90 SLtt_Use_Ansi_Colors
= 1;
96 return SLtt_Use_Ansi_Colors
;
99 /* --------------------------------------------------------------------------------------------- */
102 mc_tty_color_pair_init_special (tty_color_pair_t
* mc_color_pair
,
103 const char *fg1
, const char *bg1
,
104 const char *fg2
, const char *bg2
, SLtt_Char_Type mask
)
106 if (SLtt_Use_Ansi_Colors
!= 0)
108 if (!mc_tty_color_disable
)
110 SLtt_set_color (mc_color_pair
->pair_index
, (char *) "", (char *) fg1
, (char *) bg1
);
114 SLtt_set_color (mc_color_pair
->pair_index
, (char *) "", (char *) fg2
, (char *) bg2
);
119 SLtt_set_mono (mc_color_pair
->pair_index
, NULL
, mask
);
123 /* --------------------------------------------------------------------------------------------- */
124 /*** public functions ****************************************************************************/
125 /* --------------------------------------------------------------------------------------------- */
128 tty_color_init_lib (gboolean disable
, gboolean force
)
130 /* FIXME: if S-Lang is used, has_colors() must be called regardless
131 of whether we are interested in its result */
132 if (has_colors (disable
, force
) && !disable
)
138 /* --------------------------------------------------------------------------------------------- */
141 tty_color_deinit_lib (void)
145 /* --------------------------------------------------------------------------------------------- */
148 tty_color_try_alloc_pair_lib (tty_color_pair_t
* mc_color_pair
)
150 if (mc_color_pair
->ifg
<= (int) SPEC_A_REVERSE
)
152 switch (mc_color_pair
->ifg
)
155 mc_tty_color_pair_init_special (mc_color_pair
,
156 "black", "white", "black", "lightgray", SLTT_REV_MASK
);
159 mc_tty_color_pair_init_special (mc_color_pair
,
160 "white", "black", "white", "black", SLTT_BOLD_MASK
);
162 case SPEC_A_BOLD_REVERSE
:
163 mc_tty_color_pair_init_special (mc_color_pair
,
165 "white", "white", SLTT_BOLD_MASK
| SLTT_REV_MASK
);
167 case SPEC_A_UNDERLINE
:
168 mc_tty_color_pair_init_special (mc_color_pair
,
169 "white", "black", "white", "black", SLTT_ULINE_MASK
);
179 fg
= tty_color_get_name_by_index (mc_color_pair
->ifg
);
180 bg
= tty_color_get_name_by_index (mc_color_pair
->ibg
);
181 SLtt_set_color (mc_color_pair
->pair_index
, (char *) "", (char *) fg
, (char *) bg
);
182 SLtt_add_color_attribute (mc_color_pair
->pair_index
, mc_color_pair
->attr
);
186 /* --------------------------------------------------------------------------------------------- */
189 tty_setcolor (int color
)
191 SLsmg_set_color (color
);
194 /* --------------------------------------------------------------------------------------------- */
196 * Set colorpair by index, don't interpret S-Lang "emulated attributes"
200 tty_lowlevel_setcolor (int color
)
202 SLsmg_set_color (color
& 0x7F);
205 /* --------------------------------------------------------------------------------------------- */
208 tty_set_normal_attrs (void)
210 SLsmg_normal_video ();
213 /* --------------------------------------------------------------------------------------------- */
216 tty_use_256colors (GError
** error
)
220 ret
= (SLtt_Use_Ansi_Colors
&& SLtt_tgetnum ((char *) "Co") == 256);
223 g_set_error (error
, MC_ERROR
, -1,
224 _("Your terminal doesn't even seem to support 256 colors."));
229 /* --------------------------------------------------------------------------------------------- */
232 tty_use_truecolors (GError
** error
)
236 /* True color is supported since slang-2.3.1 on 64-bit machines,
237 and expected to be supported from slang-3 on 32-bit machines:
238 http://lists.jedsoft.org/lists/slang-users/2016/0000014.html.
239 Check for sizeof (long) being 8, exactly as slang does. */
240 if (SLang_Version
< 20301 || (sizeof (long) != 8 && SLang_Version
< 30000))
242 g_set_error (error
, MC_ERROR
, -1, _("True color not supported in this slang version."));
246 /* Duplicate slang's check so that we can pop up an error message
247 rather than silently use wrong colors. */
248 colorterm
= getenv ("COLORTERM");
249 if (colorterm
== NULL
250 || (strcmp (colorterm
, "truecolor") != 0 && strcmp (colorterm
, "24bit") != 0))
252 g_set_error (error
, MC_ERROR
, -1,
253 _("Set COLORTERM=truecolor if your terminal really supports true colors."));
260 /* --------------------------------------------------------------------------------------------- */