Merge branch '2944_cleanup'
[midnight-commander.git] / lib / tty / color-slang.c
bloba41c46987f0e6d3886d6fd624a4aed7881c2933b
1 /*
2 Color setup for S_Lang screen library
4 Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
5 2007, 2008, 2009, 2010, 2011
6 The Free Software Foundation, Inc.
8 Written by:
9 Andrew Borodin <aborodin@vmail.ru>, 2009
10 Egmont Koblinger <egmont@gmail.com>, 2010
12 This file is part of the Midnight Commander.
14 The Midnight Commander is free software: you can redistribute it
15 and/or modify it under the terms of the GNU General Public License as
16 published by the Free Software Foundation, either version 3 of the License,
17 or (at your option) any later version.
19 The Midnight Commander is distributed in the hope that it will be useful,
20 but WITHOUT ANY WARRANTY; without even the implied warranty of
21 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 GNU General Public License for more details.
24 You should have received a copy of the GNU General Public License
25 along with this program. If not, see <http://www.gnu.org/licenses/>.
28 /** \file color-slang.c
29 * \brief Source: S-Lang-specific color setup
32 #include <config.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <sys/types.h> /* size_t */
39 #include "lib/global.h"
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 /*** file scope variables ************************************************************************/
53 /*** file scope functions ************************************************************************/
54 /* --------------------------------------------------------------------------------------------- */
56 static int
57 has_colors (gboolean disable, gboolean force)
59 mc_tty_color_disable = disable;
61 if (force || (getenv ("COLORTERM") != NULL))
62 SLtt_Use_Ansi_Colors = 1;
64 if (!mc_tty_color_disable)
66 const char *terminal = getenv ("TERM");
67 const size_t len = strlen (terminal);
69 char *cts = mc_global.tty.color_terminal_string;
70 char *s;
71 size_t i;
73 /* check mc_global.tty.color_terminal_string */
74 while (*cts != '\0')
76 while (*cts == ' ' || *cts == '\t')
77 cts++;
78 s = cts;
79 i = 0;
81 while (*cts != '\0' && *cts != ',')
83 cts++;
84 i++;
87 if ((i != 0) && (i == len) && (strncmp (s, terminal, i) == 0))
88 SLtt_Use_Ansi_Colors = 1;
90 if (*cts == ',')
91 cts++;
94 return SLtt_Use_Ansi_Colors;
97 /* --------------------------------------------------------------------------------------------- */
99 static void
100 mc_tty_color_pair_init_special (tty_color_pair_t * mc_color_pair,
101 const char *fg1, const char *bg1,
102 const char *fg2, const char *bg2, SLtt_Char_Type mask)
104 if (SLtt_Use_Ansi_Colors != 0)
106 if (!mc_tty_color_disable)
108 SLtt_set_color (mc_color_pair->pair_index, (char *) "", (char *) fg1, (char *) bg1);
110 else
112 SLtt_set_color (mc_color_pair->pair_index, (char *) "", (char *) fg2, (char *) bg2);
115 else
117 SLtt_set_mono (mc_color_pair->pair_index, NULL, mask);
121 /* --------------------------------------------------------------------------------------------- */
122 /*** public functions ****************************************************************************/
123 /* --------------------------------------------------------------------------------------------- */
125 void
126 tty_color_init_lib (gboolean disable, gboolean force)
128 /* FIXME: if S-Lang is used, has_colors() must be called regardless
129 of whether we are interested in its result */
130 if (has_colors (disable, force) && !disable)
132 use_colors = TRUE;
136 /* --------------------------------------------------------------------------------------------- */
138 void
139 tty_color_deinit_lib (void)
143 /* --------------------------------------------------------------------------------------------- */
145 void
146 tty_color_try_alloc_pair_lib (tty_color_pair_t * mc_color_pair)
148 const char *fg, *bg;
149 if (mc_color_pair->ifg <= (int) SPEC_A_REVERSE)
151 switch (mc_color_pair->ifg)
153 case SPEC_A_REVERSE:
154 mc_tty_color_pair_init_special (mc_color_pair,
155 "black", "white", "black", "lightgray", SLTT_REV_MASK);
156 break;
157 case SPEC_A_BOLD:
158 mc_tty_color_pair_init_special (mc_color_pair,
159 "white", "black", "white", "black", SLTT_BOLD_MASK);
160 break;
161 case SPEC_A_BOLD_REVERSE:
163 mc_tty_color_pair_init_special (mc_color_pair,
164 "white", "white",
165 "white", "white", SLTT_BOLD_MASK | SLTT_REV_MASK);
166 break;
167 case SPEC_A_UNDERLINE:
168 mc_tty_color_pair_init_special (mc_color_pair,
169 "white", "black", "white", "black", SLTT_ULINE_MASK);
170 break;
173 else
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 /* --------------------------------------------------------------------------------------------- */