Merge branch '2621_path_encoding_and_extfs'
[midnight-commander.git] / lib / tty / color-slang.c
blob000252e51fc4d9e4803cae0e9d05f1ba89d166ae
1 /* Color setup for S_Lang screen library
2 Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5 Written by:
6 Andrew Borodin <aborodin@vmail.ru>, 2009
7 Egmont Koblinger <egmont@gmail.com>, 2010
9 This program is free software; you can redistribute it and/or modify
10 it under the terms of the GNU General Public License as published by
11 the Free Software Foundation; either version 2 of the License, or
12 (at your option) any later version.
14 This program is distributed in the hope that it will be useful,
15 but WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 GNU General Public License for more details.
19 You should have received a copy of the GNU General Public License
20 along with this program; if not, write to the Free Software
21 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
23 /** \file color-slang.c
24 * \brief Source: S-Lang-specific color setup
27 #include <config.h>
29 #include <stdio.h>
30 #include <stdlib.h>
31 #include <string.h>
32 #include <sys/types.h> /* size_t */
34 #include "lib/global.h"
36 #include "tty-slang.h"
37 #include "color.h" /* variables */
38 #include "color-internal.h"
40 /*** global variables ****************************************************************************/
42 /*** file scope macro definitions ****************************************************************/
44 /*** file scope type declarations ****************************************************************/
46 /*** file scope variables ************************************************************************/
48 /*** file scope functions ************************************************************************/
49 /* --------------------------------------------------------------------------------------------- */
51 static int
52 has_colors (gboolean disable, gboolean force)
54 mc_tty_color_disable = disable;
56 if (force || (getenv ("COLORTERM") != NULL))
57 SLtt_Use_Ansi_Colors = 1;
59 if (!mc_tty_color_disable)
61 const char *terminal = getenv ("TERM");
62 const size_t len = strlen (terminal);
64 char *cts = mc_global.tty.color_terminal_string;
65 char *s;
66 size_t i;
68 /* check mc_global.tty.color_terminal_string */
69 while (*cts != '\0')
71 while (*cts == ' ' || *cts == '\t')
72 cts++;
73 s = cts;
74 i = 0;
76 while (*cts != '\0' && *cts != ',')
78 cts++;
79 i++;
82 if ((i != 0) && (i == len) && (strncmp (s, terminal, i) == 0))
83 SLtt_Use_Ansi_Colors = 1;
85 if (*cts == ',')
86 cts++;
89 return SLtt_Use_Ansi_Colors;
92 /* --------------------------------------------------------------------------------------------- */
94 static void
95 mc_tty_color_pair_init_special (tty_color_pair_t * mc_color_pair,
96 const char *fg1, const char *bg1,
97 const char *fg2, const char *bg2, SLtt_Char_Type mask)
99 if (SLtt_Use_Ansi_Colors != 0)
101 if (!mc_tty_color_disable)
103 SLtt_set_color (mc_color_pair->pair_index, (char *) "", (char *) fg1, (char *) bg1);
105 else
107 SLtt_set_color (mc_color_pair->pair_index, (char *) "", (char *) fg2, (char *) bg2);
110 else
112 SLtt_set_mono (mc_color_pair->pair_index, NULL, mask);
116 /* --------------------------------------------------------------------------------------------- */
117 /*** public functions ****************************************************************************/
118 /* --------------------------------------------------------------------------------------------- */
120 void
121 tty_color_init_lib (gboolean disable, gboolean force)
123 /* FIXME: if S-Lang is used, has_colors() must be called regardless
124 of whether we are interested in its result */
125 if (has_colors (disable, force) && !disable)
127 use_colors = TRUE;
131 /* --------------------------------------------------------------------------------------------- */
133 void
134 tty_color_deinit_lib (void)
138 /* --------------------------------------------------------------------------------------------- */
140 void
141 tty_color_try_alloc_pair_lib (tty_color_pair_t * mc_color_pair)
143 const char *fg, *bg;
144 if (mc_color_pair->ifg <= (int) SPEC_A_REVERSE)
146 switch (mc_color_pair->ifg)
148 case SPEC_A_REVERSE:
149 mc_tty_color_pair_init_special (mc_color_pair,
150 "black", "white", "black", "lightgray", SLTT_REV_MASK);
151 break;
152 case SPEC_A_BOLD:
153 mc_tty_color_pair_init_special (mc_color_pair,
154 "white", "black", "white", "black", SLTT_BOLD_MASK);
155 break;
156 case SPEC_A_BOLD_REVERSE:
158 mc_tty_color_pair_init_special (mc_color_pair,
159 "white", "white",
160 "white", "white", SLTT_BOLD_MASK | SLTT_REV_MASK);
161 break;
162 case SPEC_A_UNDERLINE:
163 mc_tty_color_pair_init_special (mc_color_pair,
164 "white", "black", "white", "black", SLTT_ULINE_MASK);
165 break;
168 else
170 fg = tty_color_get_name_by_index (mc_color_pair->ifg);
171 bg = tty_color_get_name_by_index (mc_color_pair->ibg);
172 SLtt_set_color (mc_color_pair->pair_index, (char *) "", (char *) fg, (char *) bg);
173 SLtt_add_color_attribute (mc_color_pair->pair_index, mc_color_pair->attr);
177 /* --------------------------------------------------------------------------------------------- */
179 void
180 tty_setcolor (int color)
182 SLsmg_set_color (color);
185 /* --------------------------------------------------------------------------------------------- */
187 * Set colorpair by index, don't interpret S-Lang "emulated attributes"
190 void
191 tty_lowlevel_setcolor (int color)
193 SLsmg_set_color (color & 0x7F);
196 /* --------------------------------------------------------------------------------------------- */
198 void
199 tty_set_normal_attrs (void)
201 SLsmg_normal_video ();
204 /* --------------------------------------------------------------------------------------------- */
206 gboolean
207 tty_use_256colors (void)
209 return (SLtt_Use_Ansi_Colors && SLtt_tgetnum ((char *) "Co") == 256);
212 /* --------------------------------------------------------------------------------------------- */