2011-10-15 14:56:47 +04:00
|
|
|
/*
|
|
|
|
Color setup for S_Lang screen library
|
|
|
|
|
2023-01-03 11:30:57 +03:00
|
|
|
Copyright (C) 1994-2023
|
2014-02-12 10:33:10 +04:00
|
|
|
Free Software Foundation, Inc.
|
2010-11-08 13:21:45 +03:00
|
|
|
|
2009-06-12 14:55:06 +04:00
|
|
|
Written by:
|
2010-12-29 12:58:10 +03:00
|
|
|
Andrew Borodin <aborodin@vmail.ru>, 2009
|
|
|
|
Egmont Koblinger <egmont@gmail.com>, 2010
|
2010-11-08 13:21:45 +03:00
|
|
|
|
2011-10-15 14:56:47 +04:00
|
|
|
This file is part of the Midnight Commander.
|
|
|
|
|
|
|
|
The Midnight Commander is free software: you can redistribute it
|
|
|
|
and/or modify it under the terms of the GNU General Public License as
|
|
|
|
published by the Free Software Foundation, either version 3 of the License,
|
|
|
|
or (at your option) any later version.
|
2009-05-10 19:01:15 +04:00
|
|
|
|
2011-10-15 14:56:47 +04:00
|
|
|
The Midnight Commander is distributed in the hope that it will be useful,
|
2009-05-10 19:01:15 +04:00
|
|
|
but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
GNU General Public License for more details.
|
|
|
|
|
|
|
|
You should have received a copy of the GNU General Public License
|
2011-10-15 14:56:47 +04:00
|
|
|
along with this program. If not, see <http://www.gnu.org/licenses/>.
|
|
|
|
*/
|
2009-05-10 19:01:15 +04:00
|
|
|
|
|
|
|
/** \file color-slang.c
|
|
|
|
* \brief Source: S-Lang-specific color setup
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2009-09-17 02:07:04 +04:00
|
|
|
#include <sys/types.h> /* size_t */
|
2009-05-10 19:01:15 +04:00
|
|
|
|
2010-01-20 18:11:52 +03:00
|
|
|
#include "lib/global.h"
|
2016-12-11 21:31:22 +03:00
|
|
|
#include "lib/util.h" /* whitespace() */
|
2009-05-10 19:01:15 +04:00
|
|
|
|
2010-01-07 02:57:27 +03:00
|
|
|
#include "tty-slang.h"
|
2010-11-08 13:21:45 +03:00
|
|
|
#include "color.h" /* variables */
|
2010-01-07 02:57:27 +03:00
|
|
|
#include "color-internal.h"
|
2009-05-10 19:01:15 +04:00
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/*** global variables ****************************************************************************/
|
|
|
|
|
|
|
|
/*** file scope macro definitions ****************************************************************/
|
|
|
|
|
|
|
|
/*** file scope type declarations ****************************************************************/
|
|
|
|
|
Update template for .c files.
Add section for forward declarations of local functions. This section is
located before file scope variables because functions can be used in
strucutres (see find.c for example):
/*** forward declarations (file scope functions) *************************************************/
/* button callbacks */
static int start_stop (WButton * button, int action);
static int find_do_view_file (WButton * button, int action);
static int find_do_edit_file (WButton * button, int action);
/*** file scope variables ************************************************************************/
static struct
{
...
bcback_fn callback;
} fbuts[] =
{
...
{ B_STOP, NORMAL_BUTTON, N_("S&uspend"), 0, 0, NULL, start_stop },
...
{ B_VIEW, NORMAL_BUTTON, N_("&View - F3"), 0, 0, NULL, find_do_view_file },
{ B_VIEW, NORMAL_BUTTON, N_("&Edit - F4"), 0, 0, NULL, find_do_edit_file }
};
Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
2023-02-24 09:27:11 +03:00
|
|
|
/*** forward declarations (file scope functions) *************************************************/
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/*** file scope variables ************************************************************************/
|
|
|
|
|
Update template for .c files.
Add section for forward declarations of local functions. This section is
located before file scope variables because functions can be used in
strucutres (see find.c for example):
/*** forward declarations (file scope functions) *************************************************/
/* button callbacks */
static int start_stop (WButton * button, int action);
static int find_do_view_file (WButton * button, int action);
static int find_do_edit_file (WButton * button, int action);
/*** file scope variables ************************************************************************/
static struct
{
...
bcback_fn callback;
} fbuts[] =
{
...
{ B_STOP, NORMAL_BUTTON, N_("S&uspend"), 0, 0, NULL, start_stop },
...
{ B_VIEW, NORMAL_BUTTON, N_("&View - F3"), 0, 0, NULL, find_do_view_file },
{ B_VIEW, NORMAL_BUTTON, N_("&Edit - F4"), 0, 0, NULL, find_do_edit_file }
};
Signed-off-by: Andrew Borodin <aborodin@vmail.ru>
2023-02-24 09:27:11 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2010-11-08 13:21:45 +03:00
|
|
|
/*** file scope functions ************************************************************************/
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2009-05-14 19:18:03 +04:00
|
|
|
|
|
|
|
static int
|
2009-07-08 22:37:01 +04:00
|
|
|
has_colors (gboolean disable, gboolean force)
|
2009-05-14 19:18:03 +04:00
|
|
|
{
|
2009-09-09 18:56:52 +04:00
|
|
|
mc_tty_color_disable = disable;
|
|
|
|
|
2009-07-08 22:37:01 +04:00
|
|
|
if (force || (getenv ("COLORTERM") != NULL))
|
2009-09-17 02:07:04 +04:00
|
|
|
SLtt_Use_Ansi_Colors = 1;
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
if (!mc_tty_color_disable)
|
|
|
|
{
|
2009-09-17 02:07:04 +04:00
|
|
|
const char *terminal = getenv ("TERM");
|
|
|
|
const size_t len = strlen (terminal);
|
2011-02-10 18:02:54 +03:00
|
|
|
char *cts = mc_global.tty.color_terminal_string;
|
2009-09-17 02:07:04 +04:00
|
|
|
|
2011-02-10 18:02:54 +03:00
|
|
|
/* check mc_global.tty.color_terminal_string */
|
2010-11-08 13:21:45 +03:00
|
|
|
while (*cts != '\0')
|
|
|
|
{
|
2013-10-15 10:34:04 +04:00
|
|
|
char *s;
|
|
|
|
size_t i = 0;
|
|
|
|
|
2016-12-11 21:31:22 +03:00
|
|
|
while (whitespace (*cts))
|
2009-09-17 02:07:04 +04:00
|
|
|
cts++;
|
|
|
|
s = cts;
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
while (*cts != '\0' && *cts != ',')
|
|
|
|
{
|
2009-09-17 02:07:04 +04:00
|
|
|
cts++;
|
|
|
|
i++;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((i != 0) && (i == len) && (strncmp (s, terminal, i) == 0))
|
|
|
|
SLtt_Use_Ansi_Colors = 1;
|
|
|
|
|
|
|
|
if (*cts == ',')
|
|
|
|
cts++;
|
|
|
|
}
|
2009-05-14 19:18:03 +04:00
|
|
|
}
|
2009-09-09 18:56:52 +04:00
|
|
|
return SLtt_Use_Ansi_Colors;
|
|
|
|
}
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
2009-09-09 18:56:52 +04:00
|
|
|
static void
|
2023-09-10 12:22:26 +03:00
|
|
|
mc_tty_color_pair_init_special (tty_color_lib_pair_t * mc_color_pair,
|
2009-09-17 02:07:04 +04:00
|
|
|
const char *fg1, const char *bg1,
|
|
|
|
const char *fg2, const char *bg2, SLtt_Char_Type mask)
|
2009-09-09 18:56:52 +04:00
|
|
|
{
|
2010-11-08 13:21:45 +03:00
|
|
|
if (SLtt_Use_Ansi_Colors != 0)
|
|
|
|
{
|
|
|
|
if (!mc_tty_color_disable)
|
|
|
|
{
|
2009-09-17 02:07:04 +04:00
|
|
|
SLtt_set_color (mc_color_pair->pair_index, (char *) "", (char *) fg1, (char *) bg1);
|
2010-11-08 13:21:45 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-09-17 02:07:04 +04:00
|
|
|
SLtt_set_color (mc_color_pair->pair_index, (char *) "", (char *) fg2, (char *) bg2);
|
|
|
|
}
|
2010-11-08 13:21:45 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2009-09-17 02:07:04 +04:00
|
|
|
SLtt_set_mono (mc_color_pair->pair_index, NULL, mask);
|
2009-09-09 18:56:52 +04:00
|
|
|
}
|
2009-05-14 19:18:03 +04:00
|
|
|
}
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/*** public functions ****************************************************************************/
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
2009-05-10 19:01:15 +04:00
|
|
|
void
|
2009-09-14 17:43:03 +04:00
|
|
|
tty_color_init_lib (gboolean disable, gboolean force)
|
2009-05-10 19:01:15 +04:00
|
|
|
{
|
|
|
|
/* FIXME: if S-Lang is used, has_colors() must be called regardless
|
|
|
|
of whether we are interested in its result */
|
2010-11-08 13:21:45 +03:00
|
|
|
if (has_colors (disable, force) && !disable)
|
|
|
|
{
|
2009-09-17 02:07:04 +04:00
|
|
|
use_colors = TRUE;
|
2009-05-10 19:01:15 +04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
2009-05-10 19:01:15 +04:00
|
|
|
void
|
2009-09-14 17:43:03 +04:00
|
|
|
tty_color_deinit_lib (void)
|
|
|
|
{
|
|
|
|
}
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
2009-09-14 17:43:03 +04:00
|
|
|
void
|
2023-09-10 12:22:26 +03:00
|
|
|
tty_color_try_alloc_lib_pair (tty_color_lib_pair_t * mc_color_pair)
|
2009-05-10 19:01:15 +04:00
|
|
|
{
|
2023-09-10 12:22:26 +03:00
|
|
|
if (mc_color_pair->fg <= (int) SPEC_A_REVERSE)
|
2010-11-08 13:21:45 +03:00
|
|
|
{
|
2023-09-10 12:22:26 +03:00
|
|
|
switch (mc_color_pair->fg)
|
2010-11-08 13:21:45 +03:00
|
|
|
{
|
2009-09-17 02:07:04 +04:00
|
|
|
case SPEC_A_REVERSE:
|
|
|
|
mc_tty_color_pair_init_special (mc_color_pair,
|
|
|
|
"black", "white", "black", "lightgray", SLTT_REV_MASK);
|
|
|
|
break;
|
|
|
|
case SPEC_A_BOLD:
|
|
|
|
mc_tty_color_pair_init_special (mc_color_pair,
|
|
|
|
"white", "black", "white", "black", SLTT_BOLD_MASK);
|
|
|
|
break;
|
|
|
|
case SPEC_A_BOLD_REVERSE:
|
|
|
|
mc_tty_color_pair_init_special (mc_color_pair,
|
|
|
|
"white", "white",
|
|
|
|
"white", "white", SLTT_BOLD_MASK | SLTT_REV_MASK);
|
|
|
|
break;
|
|
|
|
case SPEC_A_UNDERLINE:
|
|
|
|
mc_tty_color_pair_init_special (mc_color_pair,
|
|
|
|
"white", "black", "white", "black", SLTT_ULINE_MASK);
|
|
|
|
break;
|
2015-04-19 13:57:38 +03:00
|
|
|
default:
|
|
|
|
break;
|
2009-09-17 02:07:04 +04:00
|
|
|
}
|
2010-11-08 13:21:45 +03:00
|
|
|
}
|
|
|
|
else
|
|
|
|
{
|
2013-10-15 10:34:04 +04:00
|
|
|
const char *fg, *bg;
|
|
|
|
|
2023-09-10 12:22:26 +03:00
|
|
|
fg = tty_color_get_name_by_index (mc_color_pair->fg);
|
|
|
|
bg = tty_color_get_name_by_index (mc_color_pair->bg);
|
2009-09-17 02:07:04 +04:00
|
|
|
SLtt_set_color (mc_color_pair->pair_index, (char *) "", (char *) fg, (char *) bg);
|
2010-12-29 12:58:10 +03:00
|
|
|
SLtt_add_color_attribute (mc_color_pair->pair_index, mc_color_pair->attr);
|
2009-09-09 18:56:52 +04:00
|
|
|
}
|
2009-05-10 19:01:15 +04:00
|
|
|
}
|
2009-05-14 19:18:03 +04:00
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
2009-05-14 19:18:03 +04:00
|
|
|
void
|
2009-09-14 17:43:03 +04:00
|
|
|
tty_setcolor (int color)
|
2009-05-14 19:18:03 +04:00
|
|
|
{
|
2009-10-22 15:03:01 +04:00
|
|
|
SLsmg_set_color (color);
|
2009-05-14 19:18:03 +04:00
|
|
|
}
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/**
|
|
|
|
* Set colorpair by index, don't interpret S-Lang "emulated attributes"
|
|
|
|
*/
|
|
|
|
|
2009-05-14 19:18:03 +04:00
|
|
|
void
|
2009-09-14 17:43:03 +04:00
|
|
|
tty_lowlevel_setcolor (int color)
|
2009-05-14 19:18:03 +04:00
|
|
|
{
|
|
|
|
SLsmg_set_color (color & 0x7F);
|
|
|
|
}
|
2009-06-02 08:55:07 +04:00
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
2009-06-02 08:55:07 +04:00
|
|
|
void
|
2009-09-14 17:43:03 +04:00
|
|
|
tty_set_normal_attrs (void)
|
2009-06-02 08:55:07 +04:00
|
|
|
{
|
|
|
|
SLsmg_normal_video ();
|
|
|
|
}
|
2010-11-08 13:21:45 +03:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2011-01-11 17:10:27 +03:00
|
|
|
|
|
|
|
gboolean
|
2021-02-07 15:08:13 +03:00
|
|
|
tty_use_256colors (GError ** error)
|
2011-01-11 17:10:27 +03:00
|
|
|
{
|
2021-02-07 15:08:13 +03:00
|
|
|
gboolean ret;
|
|
|
|
|
|
|
|
ret = (SLtt_Use_Ansi_Colors && SLtt_tgetnum ((char *) "Co") == 256);
|
|
|
|
|
|
|
|
if (!ret)
|
|
|
|
g_set_error (error, MC_ERROR, -1,
|
|
|
|
_("Your terminal doesn't even seem to support 256 colors."));
|
|
|
|
|
|
|
|
return ret;
|
2011-01-11 17:10:27 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2016-11-02 14:07:33 +03:00
|
|
|
|
|
|
|
gboolean
|
|
|
|
tty_use_truecolors (GError ** error)
|
|
|
|
{
|
|
|
|
char *colorterm;
|
|
|
|
|
|
|
|
/* True color is supported since slang-2.3.1 on 64-bit machines,
|
|
|
|
and expected to be supported from slang-3 on 32-bit machines:
|
|
|
|
http://lists.jedsoft.org/lists/slang-users/2016/0000014.html.
|
|
|
|
Check for sizeof (long) being 8, exactly as slang does. */
|
|
|
|
if (SLang_Version < 20301 || (sizeof (long) != 8 && SLang_Version < 30000))
|
|
|
|
{
|
|
|
|
g_set_error (error, MC_ERROR, -1, _("True color not supported in this slang version."));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* Duplicate slang's check so that we can pop up an error message
|
|
|
|
rather than silently use wrong colors. */
|
|
|
|
colorterm = getenv ("COLORTERM");
|
|
|
|
if (colorterm == NULL
|
|
|
|
|| (strcmp (colorterm, "truecolor") != 0 && strcmp (colorterm, "24bit") != 0))
|
|
|
|
{
|
|
|
|
g_set_error (error, MC_ERROR, -1,
|
|
|
|
_("Set COLORTERM=truecolor if your terminal really supports true colors."));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
return TRUE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|