Merge branch '3607_cleanup'
[midnight-commander.git] / lib / tty / color-slang.c
blobd8646ca0b22876b3bdff3c658d04df9641dcfd3c
1 /*
2 Color setup for S_Lang screen library
4 Copyright (C) 1994-2016
5 Free Software Foundation, Inc.
7 Written by:
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
31 #include <config.h>
33 #include <stdio.h>
34 #include <stdlib.h>
35 #include <string.h>
36 #include <sys/types.h> /* size_t */
38 #include "lib/global.h"
40 #include "tty-slang.h"
41 #include "color.h" /* variables */
42 #include "color-internal.h"
44 /*** global variables ****************************************************************************/
46 /*** file scope macro definitions ****************************************************************/
48 /*** file scope type declarations ****************************************************************/
50 /*** file scope variables ************************************************************************/
52 /*** file scope functions ************************************************************************/
53 /* --------------------------------------------------------------------------------------------- */
55 static int
56 has_colors (gboolean disable, gboolean force)
58 mc_tty_color_disable = disable;
60 if (force || (getenv ("COLORTERM") != NULL))
61 SLtt_Use_Ansi_Colors = 1;
63 if (!mc_tty_color_disable)
65 const char *terminal = getenv ("TERM");
66 const size_t len = strlen (terminal);
67 char *cts = mc_global.tty.color_terminal_string;
69 /* check mc_global.tty.color_terminal_string */
70 while (*cts != '\0')
72 char *s;
73 size_t i = 0;
75 while (*cts == ' ' || *cts == '\t')
76 cts++;
77 s = cts;
79 while (*cts != '\0' && *cts != ',')
81 cts++;
82 i++;
85 if ((i != 0) && (i == len) && (strncmp (s, terminal, i) == 0))
86 SLtt_Use_Ansi_Colors = 1;
88 if (*cts == ',')
89 cts++;
92 return SLtt_Use_Ansi_Colors;
95 /* --------------------------------------------------------------------------------------------- */
97 static void
98 mc_tty_color_pair_init_special (tty_color_pair_t * mc_color_pair,
99 const char *fg1, const char *bg1,
100 const char *fg2, const char *bg2, SLtt_Char_Type mask)
102 if (SLtt_Use_Ansi_Colors != 0)
104 if (!mc_tty_color_disable)
106 SLtt_set_color (mc_color_pair->pair_index, (char *) "", (char *) fg1, (char *) bg1);
108 else
110 SLtt_set_color (mc_color_pair->pair_index, (char *) "", (char *) fg2, (char *) bg2);
113 else
115 SLtt_set_mono (mc_color_pair->pair_index, NULL, mask);
119 /* --------------------------------------------------------------------------------------------- */
120 /*** public functions ****************************************************************************/
121 /* --------------------------------------------------------------------------------------------- */
123 void
124 tty_color_init_lib (gboolean disable, gboolean force)
126 /* FIXME: if S-Lang is used, has_colors() must be called regardless
127 of whether we are interested in its result */
128 if (has_colors (disable, force) && !disable)
130 use_colors = TRUE;
134 /* --------------------------------------------------------------------------------------------- */
136 void
137 tty_color_deinit_lib (void)
141 /* --------------------------------------------------------------------------------------------- */
143 void
144 tty_color_try_alloc_pair_lib (tty_color_pair_t * mc_color_pair)
146 if (mc_color_pair->ifg <= (int) SPEC_A_REVERSE)
148 switch (mc_color_pair->ifg)
150 case SPEC_A_REVERSE:
151 mc_tty_color_pair_init_special (mc_color_pair,
152 "black", "white", "black", "lightgray", SLTT_REV_MASK);
153 break;
154 case SPEC_A_BOLD:
155 mc_tty_color_pair_init_special (mc_color_pair,
156 "white", "black", "white", "black", SLTT_BOLD_MASK);
157 break;
158 case SPEC_A_BOLD_REVERSE:
159 mc_tty_color_pair_init_special (mc_color_pair,
160 "white", "white",
161 "white", "white", SLTT_BOLD_MASK | SLTT_REV_MASK);
162 break;
163 case SPEC_A_UNDERLINE:
164 mc_tty_color_pair_init_special (mc_color_pair,
165 "white", "black", "white", "black", SLTT_ULINE_MASK);
166 break;
167 default:
168 break;
171 else
173 const char *fg, *bg;
175 fg = tty_color_get_name_by_index (mc_color_pair->ifg);
176 bg = tty_color_get_name_by_index (mc_color_pair->ibg);
177 SLtt_set_color (mc_color_pair->pair_index, (char *) "", (char *) fg, (char *) bg);
178 SLtt_add_color_attribute (mc_color_pair->pair_index, mc_color_pair->attr);
182 /* --------------------------------------------------------------------------------------------- */
184 void
185 tty_setcolor (int color)
187 SLsmg_set_color (color);
190 /* --------------------------------------------------------------------------------------------- */
192 * Set colorpair by index, don't interpret S-Lang "emulated attributes"
195 void
196 tty_lowlevel_setcolor (int color)
198 SLsmg_set_color (color & 0x7F);
201 /* --------------------------------------------------------------------------------------------- */
203 void
204 tty_set_normal_attrs (void)
206 SLsmg_normal_video ();
209 /* --------------------------------------------------------------------------------------------- */
211 gboolean
212 tty_use_256colors (void)
214 return (SLtt_Use_Ansi_Colors && SLtt_tgetnum ((char *) "Co") == 256);
217 /* --------------------------------------------------------------------------------------------- */