2011-10-15 14:56:47 +04:00
|
|
|
/*
|
|
|
|
Color setup for NCurses screen library
|
|
|
|
|
2024-01-01 09:46:17 +03:00
|
|
|
Copyright (C) 1994-2024
|
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
|
|
|
|
Slava Zanko <slavazanko@gmail.com>, 2010
|
|
|
|
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-ncurses.c
|
|
|
|
* \brief Source: NCUrses-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"
|
2009-05-10 19:01:15 +04:00
|
|
|
|
2010-01-07 02:57:27 +03:00
|
|
|
#include "tty-ncurses.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
|
|
|
|
2009-09-14 17:43:03 +04: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) *************************************************/
|
|
|
|
|
2009-09-14 17:43:03 +04:00
|
|
|
/*** file scope variables ************************************************************************/
|
|
|
|
|
|
|
|
static GHashTable *mc_tty_color_color_pair_attrs = NULL;
|
|
|
|
|
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
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2009-09-14 17:43:03 +04:00
|
|
|
/*** file scope functions ************************************************************************/
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
mc_tty_color_attr_destroy_cb (gpointer data)
|
|
|
|
{
|
|
|
|
g_free (data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
2010-12-29 12:58:10 +03:00
|
|
|
static void
|
|
|
|
mc_tty_color_save_attr (int color_pair, int color_attr)
|
2009-09-14 17:43:03 +04:00
|
|
|
{
|
|
|
|
int *attr, *key;
|
2010-09-12 18:32:26 +04:00
|
|
|
|
2009-12-12 13:54:22 +03:00
|
|
|
attr = g_try_new0 (int, 1);
|
2009-09-14 17:43:03 +04:00
|
|
|
if (attr == NULL)
|
2010-12-29 12:58:10 +03:00
|
|
|
return;
|
2009-09-14 17:43:03 +04:00
|
|
|
|
2009-12-12 13:54:22 +03:00
|
|
|
key = g_try_new (int, 1);
|
2010-11-08 13:21:45 +03:00
|
|
|
if (key == NULL)
|
|
|
|
{
|
2009-09-17 02:07:04 +04:00
|
|
|
g_free (attr);
|
2010-12-29 12:58:10 +03:00
|
|
|
return;
|
2009-09-14 17:43:03 +04:00
|
|
|
}
|
2009-12-12 13:54:22 +03:00
|
|
|
|
2011-01-11 17:11:56 +03:00
|
|
|
*key = color_pair;
|
2010-12-29 12:58:10 +03:00
|
|
|
*attr = color_attr;
|
2009-09-14 17:43:03 +04:00
|
|
|
|
|
|
|
g_hash_table_replace (mc_tty_color_color_pair_attrs, (gpointer) key, (gpointer) attr);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static int
|
|
|
|
color_get_attr (int color_pair)
|
|
|
|
{
|
2009-12-12 13:54:22 +03:00
|
|
|
int *fnd = NULL;
|
2009-09-14 17:43:03 +04:00
|
|
|
|
2009-12-12 13:54:22 +03:00
|
|
|
if (mc_tty_color_color_pair_attrs != NULL)
|
2010-11-08 13:21:45 +03:00
|
|
|
fnd = (int *) g_hash_table_lookup (mc_tty_color_color_pair_attrs, (gpointer) & color_pair);
|
2009-09-14 17:43:03 +04:00
|
|
|
return (fnd != NULL) ? *fnd : 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static void
|
2024-06-01 21:12:14 +03:00
|
|
|
mc_tty_color_pair_init_special (tty_color_lib_pair_t *mc_color_pair,
|
2010-12-29 12:58:10 +03:00
|
|
|
int fg1, int bg1, int fg2, int bg2, int attr)
|
2009-09-14 17:43:03 +04:00
|
|
|
{
|
2010-02-24 14:01:39 +03:00
|
|
|
if (has_colors () && !mc_tty_color_disable)
|
2010-12-29 12:58:10 +03:00
|
|
|
init_pair (mc_color_pair->pair_index, fg1, bg1);
|
2010-02-24 14:01:39 +03:00
|
|
|
else
|
2010-12-29 12:58:10 +03:00
|
|
|
init_pair (mc_color_pair->pair_index, fg2, bg2);
|
|
|
|
mc_tty_color_save_attr (mc_color_pair->pair_index, attr);
|
2009-09-14 17:43:03 +04: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
|
|
|
{
|
2009-07-08 22:37:01 +04:00
|
|
|
(void) force;
|
2009-05-10 19:01:15 +04:00
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
if (has_colors () && !disable)
|
|
|
|
{
|
2009-09-17 02:07:04 +04:00
|
|
|
use_colors = TRUE;
|
|
|
|
start_color ();
|
|
|
|
use_default_colors ();
|
2009-05-10 19:01:15 +04:00
|
|
|
}
|
2009-09-14 17:43:03 +04:00
|
|
|
|
|
|
|
mc_tty_color_color_pair_attrs = g_hash_table_new_full
|
2009-09-17 02:07:04 +04:00
|
|
|
(g_int_hash, g_int_equal, mc_tty_color_attr_destroy_cb, mc_tty_color_attr_destroy_cb);
|
2009-05-10 19:01:15 +04:00
|
|
|
}
|
|
|
|
|
2009-09-14 17:43:03 +04:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
2009-05-10 19:01:15 +04:00
|
|
|
void
|
2009-09-14 17:43:03 +04:00
|
|
|
tty_color_deinit_lib (void)
|
|
|
|
{
|
|
|
|
g_hash_table_destroy (mc_tty_color_color_pair_attrs);
|
|
|
|
mc_tty_color_color_pair_attrs = NULL;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
void
|
2024-06-01 21:12:14 +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,
|
|
|
|
COLOR_BLACK, COLOR_WHITE,
|
|
|
|
COLOR_BLACK, COLOR_WHITE | A_BOLD, A_REVERSE);
|
|
|
|
break;
|
|
|
|
case SPEC_A_BOLD:
|
|
|
|
mc_tty_color_pair_init_special (mc_color_pair,
|
|
|
|
COLOR_WHITE, COLOR_BLACK,
|
|
|
|
COLOR_WHITE, COLOR_BLACK, A_BOLD);
|
|
|
|
break;
|
|
|
|
case SPEC_A_BOLD_REVERSE:
|
|
|
|
mc_tty_color_pair_init_special (mc_color_pair,
|
|
|
|
COLOR_WHITE, COLOR_WHITE,
|
|
|
|
COLOR_WHITE, COLOR_WHITE, A_BOLD | A_REVERSE);
|
|
|
|
break;
|
|
|
|
case SPEC_A_UNDERLINE:
|
|
|
|
mc_tty_color_pair_init_special (mc_color_pair,
|
|
|
|
COLOR_WHITE, COLOR_BLACK,
|
|
|
|
COLOR_WHITE, COLOR_BLACK, A_UNDERLINE);
|
|
|
|
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
|
|
|
|
{
|
2010-12-29 12:58:10 +03:00
|
|
|
int ifg, ibg, attr;
|
|
|
|
|
2023-09-10 12:22:26 +03:00
|
|
|
ifg = mc_color_pair->fg;
|
|
|
|
ibg = mc_color_pair->bg;
|
2010-12-29 12:58:10 +03:00
|
|
|
attr = mc_color_pair->attr;
|
|
|
|
|
2016-11-02 14:07:33 +03:00
|
|
|
/* In legacy color mode, change bright colors into bold */
|
2021-02-07 15:08:13 +03:00
|
|
|
if (!tty_use_256colors (NULL) && !tty_use_truecolors (NULL))
|
2010-12-29 12:58:10 +03:00
|
|
|
{
|
2011-01-11 17:10:27 +03:00
|
|
|
if (ifg >= 8 && ifg < 16)
|
|
|
|
{
|
|
|
|
ifg &= 0x07;
|
|
|
|
attr |= A_BOLD;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (ibg >= 8 && ibg < 16)
|
|
|
|
{
|
|
|
|
ibg &= 0x07;
|
|
|
|
/* attr | = A_BOLD | A_REVERSE ; */
|
|
|
|
}
|
2010-12-29 12:58:10 +03:00
|
|
|
}
|
2010-01-15 14:19:08 +03:00
|
|
|
|
2010-12-29 12:58:10 +03:00
|
|
|
init_pair (mc_color_pair->pair_index, ifg, ibg);
|
|
|
|
mc_tty_color_save_attr (mc_color_pair->pair_index, attr);
|
2009-09-10 16:14:18 +04:00
|
|
|
}
|
2009-05-10 19:01:15 +04:00
|
|
|
}
|
2009-05-14 19:18:03 +04:00
|
|
|
|
2009-09-14 17:43:03 +04: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-09-14 17:43:03 +04:00
|
|
|
attrset (COLOR_PAIR (color) | color_get_attr (color));
|
2009-05-14 19:18:03 +04:00
|
|
|
}
|
|
|
|
|
2009-09-14 17:43:03 +04:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
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
|
|
|
{
|
2011-01-11 17:10:27 +03:00
|
|
|
tty_setcolor (color);
|
2009-05-14 19:18:03 +04:00
|
|
|
}
|
2009-06-02 08:55:07 +04:00
|
|
|
|
2009-09-14 17:43:03 +04: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
|
|
|
{
|
|
|
|
standend ();
|
|
|
|
}
|
2009-09-14 17:43:03 +04:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2011-01-11 17:10:27 +03:00
|
|
|
|
|
|
|
gboolean
|
2024-06-01 21:12:14 +03:00
|
|
|
tty_use_256colors (GError **error)
|
2011-01-11 17:10:27 +03:00
|
|
|
{
|
2021-02-07 15:08:13 +03:00
|
|
|
(void) error;
|
|
|
|
|
2011-01-11 17:10:27 +03:00
|
|
|
return (COLORS == 256);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2016-11-02 14:07:33 +03:00
|
|
|
|
|
|
|
gboolean
|
2024-06-01 21:12:14 +03:00
|
|
|
tty_use_truecolors (GError **error)
|
2016-11-02 14:07:33 +03:00
|
|
|
{
|
|
|
|
/* Not yet supported in ncurses */
|
|
|
|
g_set_error (error, MC_ERROR, -1, _("True color not supported with ncurses."));
|
|
|
|
return FALSE;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|