Merge branch '2087_standalone_mcedit_crash'
[kaloumi3.git] / lib / tty / color-slang.c
blob279c262391ff67d4d6733426894539bc461b82bd
1 /* Color setup for S_Lang screen library
2 Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2007, 2008, 2009 Free Software Foundation, Inc.
5 Written by:
6 Andrew Borodin <aborodin@vmail.ru>, 2009.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
22 /** \file color-slang.c
23 * \brief Source: S-Lang-specific color setup
26 #include <config.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <sys/types.h> /* size_t */
33 #include "lib/global.h"
35 #include "tty-slang.h"
36 #include "color.h" /* variables */
37 #include "color-internal.h"
39 #include "src/setup.h" /* color_terminal_string */
41 static int
42 has_colors (gboolean disable, gboolean force)
44 mc_tty_color_disable = disable;
46 if (force || (getenv ("COLORTERM") != NULL))
47 SLtt_Use_Ansi_Colors = 1;
49 if (!mc_tty_color_disable) {
50 const char *terminal = getenv ("TERM");
51 const size_t len = strlen (terminal);
53 char *cts = color_terminal_string;
54 char *s;
55 size_t i;
57 /* check color_terminal_string */
58 while (*cts != '\0') {
59 while (*cts == ' ' || *cts == '\t')
60 cts++;
61 s = cts;
62 i = 0;
64 while (*cts != '\0' && *cts != ',') {
65 cts++;
66 i++;
69 if ((i != 0) && (i == len) && (strncmp (s, terminal, i) == 0))
70 SLtt_Use_Ansi_Colors = 1;
72 if (*cts == ',')
73 cts++;
76 return SLtt_Use_Ansi_Colors;
79 static void
80 mc_tty_color_pair_init_special (tty_color_pair_t * mc_color_pair,
81 const char *fg1, const char *bg1,
82 const char *fg2, const char *bg2, SLtt_Char_Type mask)
84 if (SLtt_Use_Ansi_Colors != 0) {
85 if (!mc_tty_color_disable) {
86 SLtt_set_color (mc_color_pair->pair_index, (char *) "", (char *) fg1, (char *) bg1);
87 } else {
88 SLtt_set_color (mc_color_pair->pair_index, (char *) "", (char *) fg2, (char *) bg2);
90 } else {
91 SLtt_set_mono (mc_color_pair->pair_index, NULL, mask);
95 void
96 tty_color_init_lib (gboolean disable, gboolean force)
98 /* FIXME: if S-Lang is used, has_colors() must be called regardless
99 of whether we are interested in its result */
100 if (has_colors (disable, force) && !disable) {
101 use_colors = TRUE;
105 void
106 tty_color_deinit_lib (void)
110 void
111 tty_color_try_alloc_pair_lib (tty_color_pair_t * mc_color_pair)
113 const char *fg, *bg;
114 if (mc_color_pair->ifg <= (int) SPEC_A_REVERSE) {
115 switch (mc_color_pair->ifg) {
116 case SPEC_A_REVERSE:
117 mc_tty_color_pair_init_special (mc_color_pair,
118 "black", "white", "black", "lightgray", SLTT_REV_MASK);
119 break;
120 case SPEC_A_BOLD:
121 mc_tty_color_pair_init_special (mc_color_pair,
122 "white", "black", "white", "black", SLTT_BOLD_MASK);
123 break;
124 case SPEC_A_BOLD_REVERSE:
126 mc_tty_color_pair_init_special (mc_color_pair,
127 "white", "white",
128 "white", "white", SLTT_BOLD_MASK | SLTT_REV_MASK);
129 break;
130 case SPEC_A_UNDERLINE:
131 mc_tty_color_pair_init_special (mc_color_pair,
132 "white", "black", "white", "black", SLTT_ULINE_MASK);
133 break;
135 } else {
136 fg = (mc_color_pair->cfg) ? mc_color_pair->cfg : "default";
137 bg = (mc_color_pair->cbg) ? mc_color_pair->cbg : "default";
138 SLtt_set_color (mc_color_pair->pair_index, (char *) "", (char *) fg, (char *) bg);
142 void
143 tty_setcolor (int color)
145 SLsmg_set_color (color);
148 /* Set colorpair by index, don't interpret S-Lang "emulated attributes" */
149 void
150 tty_lowlevel_setcolor (int color)
152 SLsmg_set_color (color & 0x7F);
155 void
156 tty_set_normal_attrs (void)
158 SLsmg_normal_video ();