2011-10-15 14:56:47 +04:00
|
|
|
/*
|
|
|
|
Color setup.
|
2010-02-26 12:25:53 +03:00
|
|
|
Interface functions.
|
2009-09-04 18:22:49 +04:00
|
|
|
|
2021-01-01 14:21:28 +03:00
|
|
|
Copyright (C) 1994-2021
|
2014-02-12 10:33:10 +04:00
|
|
|
Free Software Foundation, Inc.
|
2010-02-26 12:25:53 +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>, 2009
|
|
|
|
Egmont Koblinger <egmont@gmail.com>, 2010
|
|
|
|
|
2011-10-15 14:56:47 +04:00
|
|
|
This file is part of the Midnight Commander.
|
2010-02-26 12:25:53 +03:00
|
|
|
|
2011-10-15 14:56:47 +04:00
|
|
|
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,
|
1998-02-27 07:54:42 +03: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/>.
|
|
|
|
*/
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2009-02-05 21:28:18 +03:00
|
|
|
/** \file color.c
|
|
|
|
* \brief Source: color setup
|
|
|
|
*/
|
|
|
|
|
1998-02-27 07:54:42 +03:00
|
|
|
#include <config.h>
|
2005-02-08 12:04:03 +03:00
|
|
|
|
1998-02-27 07:54:42 +03:00
|
|
|
#include <stdio.h>
|
2005-02-08 12:04:03 +03:00
|
|
|
#include <stdlib.h>
|
1998-02-27 07:54:42 +03:00
|
|
|
#include <string.h>
|
2009-09-17 02:07:04 +04:00
|
|
|
#include <sys/types.h> /* size_t */
|
2005-02-08 12:04:03 +03:00
|
|
|
|
2010-01-20 18:11:52 +03:00
|
|
|
#include "lib/global.h"
|
2009-05-08 14:01:05 +04:00
|
|
|
|
2010-01-07 02:57:27 +03:00
|
|
|
#include "tty.h"
|
|
|
|
#include "color.h"
|
2009-09-04 18:22:49 +04:00
|
|
|
|
2010-01-07 02:57:27 +03:00
|
|
|
#include "color-internal.h"
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2009-09-04 18:22:49 +04:00
|
|
|
/*** global variables ****************************************************************************/
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2009-09-10 16:14:18 +04:00
|
|
|
static char *tty_color_defaults__fg = NULL;
|
|
|
|
static char *tty_color_defaults__bg = NULL;
|
2010-12-29 12:58:10 +03:00
|
|
|
static char *tty_color_defaults__attrs = NULL;
|
2009-09-10 16:14:18 +04:00
|
|
|
|
1998-02-27 07:54:42 +03:00
|
|
|
/* Set if we are actually using colors */
|
2009-05-10 19:01:15 +04:00
|
|
|
gboolean use_colors = FALSE;
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2009-09-04 18:22:49 +04:00
|
|
|
/*** file scope macro definitions ****************************************************************/
|
2009-06-04 23:55:47 +04:00
|
|
|
|
2009-09-04 18:22:49 +04:00
|
|
|
/*** file scope type declarations ****************************************************************/
|
|
|
|
|
|
|
|
/*** file scope variables ************************************************************************/
|
|
|
|
|
|
|
|
static GHashTable *mc_tty_color__hashtable = NULL;
|
|
|
|
|
|
|
|
/*** file scope functions ************************************************************************/
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
2009-09-10 16:14:18 +04:00
|
|
|
static gboolean
|
|
|
|
tty_color_free_condition_cb (gpointer key, gpointer value, gpointer user_data)
|
|
|
|
{
|
2009-09-17 02:07:04 +04:00
|
|
|
gboolean is_temp_color;
|
|
|
|
tty_color_pair_t *mc_color_pair;
|
|
|
|
(void) key;
|
|
|
|
|
2010-02-26 15:13:10 +03:00
|
|
|
is_temp_color = user_data != NULL;
|
2009-09-17 02:07:04 +04:00
|
|
|
mc_color_pair = (tty_color_pair_t *) value;
|
2009-09-10 16:14:18 +04:00
|
|
|
return (mc_color_pair->is_temp == is_temp_color);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static void
|
2009-09-17 02:07:04 +04:00
|
|
|
tty_color_free_all (gboolean is_temp_color)
|
2009-09-10 16:14:18 +04:00
|
|
|
{
|
2009-09-17 02:07:04 +04:00
|
|
|
g_hash_table_foreach_remove (mc_tty_color__hashtable, tty_color_free_condition_cb,
|
2012-11-18 13:36:16 +04:00
|
|
|
is_temp_color ? GSIZE_TO_POINTER (1) : NULL);
|
2009-09-10 16:14:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
tty_color_get_next_cpn_cb (gpointer key, gpointer value, gpointer user_data)
|
|
|
|
{
|
2010-12-29 12:58:10 +03:00
|
|
|
size_t cp;
|
2009-09-17 02:07:04 +04:00
|
|
|
tty_color_pair_t *mc_color_pair;
|
|
|
|
(void) key;
|
|
|
|
|
2012-11-18 13:36:16 +04:00
|
|
|
cp = GPOINTER_TO_SIZE (user_data);
|
2009-09-17 02:07:04 +04:00
|
|
|
mc_color_pair = (tty_color_pair_t *) value;
|
2009-09-10 16:14:18 +04:00
|
|
|
|
2010-09-12 18:32:26 +04:00
|
|
|
return (cp == mc_color_pair->pair_index);
|
2009-09-10 16:14:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
2010-12-29 12:58:10 +03:00
|
|
|
static size_t
|
2010-09-12 18:32:26 +04:00
|
|
|
tty_color_get_next__color_pair_number (void)
|
2009-09-10 16:14:18 +04:00
|
|
|
{
|
2012-11-18 13:36:16 +04:00
|
|
|
size_t cp_count, cp;
|
2009-09-10 16:14:18 +04:00
|
|
|
|
2012-11-18 13:36:16 +04:00
|
|
|
cp_count = g_hash_table_size (mc_tty_color__hashtable);
|
2010-02-26 12:25:53 +03:00
|
|
|
for (cp = 0; cp < cp_count; cp++)
|
2010-09-12 18:32:26 +04:00
|
|
|
if (g_hash_table_find (mc_tty_color__hashtable, tty_color_get_next_cpn_cb,
|
2012-11-18 13:36:16 +04:00
|
|
|
GSIZE_TO_POINTER (cp)) == NULL)
|
2010-09-12 18:32:26 +04:00
|
|
|
break;
|
|
|
|
|
2009-09-10 16:14:18 +04:00
|
|
|
return cp;
|
|
|
|
}
|
|
|
|
|
2009-09-04 18:22:49 +04:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/*** public functions ****************************************************************************/
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2009-09-04 18:22:49 +04:00
|
|
|
void
|
|
|
|
tty_init_colors (gboolean disable, gboolean force)
|
|
|
|
{
|
2009-09-14 17:43:03 +04:00
|
|
|
tty_color_init_lib (disable, force);
|
2010-09-12 18:32:26 +04:00
|
|
|
mc_tty_color__hashtable = g_hash_table_new_full (g_str_hash, g_str_equal, g_free, g_free);
|
2009-09-04 18:22:49 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
void
|
|
|
|
tty_colors_done (void)
|
|
|
|
{
|
2009-09-14 17:43:03 +04:00
|
|
|
tty_color_deinit_lib ();
|
|
|
|
g_free (tty_color_defaults__fg);
|
|
|
|
g_free (tty_color_defaults__bg);
|
2010-12-29 12:58:10 +03:00
|
|
|
g_free (tty_color_defaults__attrs);
|
2009-09-10 16:14:18 +04:00
|
|
|
|
2009-09-04 18:22:49 +04:00
|
|
|
g_hash_table_destroy (mc_tty_color__hashtable);
|
|
|
|
}
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2009-09-04 18:22:49 +04:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2009-06-04 23:55:47 +04:00
|
|
|
|
2009-09-04 18:22:49 +04:00
|
|
|
gboolean
|
|
|
|
tty_use_colors (void)
|
|
|
|
{
|
|
|
|
return use_colors;
|
|
|
|
}
|
2009-06-04 23:55:47 +04:00
|
|
|
|
2009-09-04 18:22:49 +04:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2009-06-04 23:55:47 +04:00
|
|
|
|
2009-09-14 17:43:03 +04:00
|
|
|
int
|
2010-12-29 12:58:10 +03:00
|
|
|
tty_try_alloc_color_pair2 (const char *fg, const char *bg, const char *attrs,
|
|
|
|
gboolean is_temp_color)
|
2009-09-04 18:22:49 +04:00
|
|
|
{
|
|
|
|
gchar *color_pair;
|
2009-09-14 17:43:03 +04:00
|
|
|
tty_color_pair_t *mc_color_pair;
|
2010-12-29 12:58:10 +03:00
|
|
|
int ifg, ibg, attr;
|
2009-08-31 14:17:29 +04:00
|
|
|
|
2010-12-29 12:58:10 +03:00
|
|
|
if (fg == NULL || !strcmp (fg, "base"))
|
2009-09-17 02:07:04 +04:00
|
|
|
fg = tty_color_defaults__fg;
|
2010-12-29 12:58:10 +03:00
|
|
|
if (bg == NULL || !strcmp (bg, "base"))
|
2009-09-17 02:07:04 +04:00
|
|
|
bg = tty_color_defaults__bg;
|
2010-12-29 12:58:10 +03:00
|
|
|
if (attrs == NULL || !strcmp (attrs, "base"))
|
|
|
|
attrs = tty_color_defaults__attrs;
|
2010-09-12 18:32:26 +04:00
|
|
|
|
2010-12-29 12:58:10 +03:00
|
|
|
ifg = tty_color_get_index_by_name (fg);
|
|
|
|
ibg = tty_color_get_index_by_name (bg);
|
|
|
|
attr = tty_attr_get_bits (attrs);
|
2009-08-31 14:17:29 +04:00
|
|
|
|
2010-12-29 12:58:10 +03:00
|
|
|
color_pair = g_strdup_printf ("%d.%d.%d", ifg, ibg, attr);
|
2009-09-04 18:22:49 +04:00
|
|
|
if (color_pair == NULL)
|
2009-09-17 02:07:04 +04:00
|
|
|
return 0;
|
2009-06-04 23:55:47 +04:00
|
|
|
|
2009-09-17 02:07:04 +04:00
|
|
|
mc_color_pair =
|
|
|
|
(tty_color_pair_t *) g_hash_table_lookup (mc_tty_color__hashtable, (gpointer) color_pair);
|
2009-06-04 23:55:47 +04:00
|
|
|
|
2010-02-26 12:25:53 +03:00
|
|
|
if (mc_color_pair != NULL)
|
|
|
|
{
|
2009-09-17 02:07:04 +04:00
|
|
|
g_free (color_pair);
|
|
|
|
return mc_color_pair->pair_index;
|
2009-09-04 18:22:49 +04:00
|
|
|
}
|
2009-06-04 23:55:47 +04:00
|
|
|
|
2009-12-12 13:54:22 +03:00
|
|
|
mc_color_pair = g_try_new0 (tty_color_pair_t, 1);
|
2010-02-26 12:25:53 +03:00
|
|
|
if (mc_color_pair == NULL)
|
|
|
|
{
|
2009-09-17 02:07:04 +04:00
|
|
|
g_free (color_pair);
|
|
|
|
return 0;
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
2009-06-04 23:55:47 +04:00
|
|
|
|
2009-09-10 16:14:18 +04:00
|
|
|
mc_color_pair->is_temp = is_temp_color;
|
2010-12-29 12:58:10 +03:00
|
|
|
mc_color_pair->ifg = ifg;
|
|
|
|
mc_color_pair->ibg = ibg;
|
|
|
|
mc_color_pair->attr = attr;
|
2009-09-17 02:07:04 +04:00
|
|
|
mc_color_pair->pair_index = tty_color_get_next__color_pair_number ();
|
1998-02-27 07:54:42 +03:00
|
|
|
|
2009-09-14 17:43:03 +04:00
|
|
|
tty_color_try_alloc_pair_lib (mc_color_pair);
|
2009-09-04 18:22:49 +04:00
|
|
|
|
|
|
|
g_hash_table_insert (mc_tty_color__hashtable, (gpointer) color_pair, (gpointer) mc_color_pair);
|
|
|
|
|
|
|
|
return mc_color_pair->pair_index;
|
1998-02-27 07:54:42 +03:00
|
|
|
}
|
|
|
|
|
2009-09-04 18:22:49 +04:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
2009-09-14 17:43:03 +04:00
|
|
|
int
|
2010-12-29 12:58:10 +03:00
|
|
|
tty_try_alloc_color_pair (const char *fg, const char *bg, const char *attrs)
|
2009-09-10 16:14:18 +04:00
|
|
|
{
|
2010-12-29 12:58:10 +03:00
|
|
|
return tty_try_alloc_color_pair2 (fg, bg, attrs, TRUE);
|
2009-09-10 16:14:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
void
|
2009-09-17 02:07:04 +04:00
|
|
|
tty_color_free_all_tmp (void)
|
2009-09-10 16:14:18 +04:00
|
|
|
{
|
2009-09-17 02:07:04 +04:00
|
|
|
tty_color_free_all (TRUE);
|
2009-09-10 16:14:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
void
|
2009-09-17 02:07:04 +04:00
|
|
|
tty_color_free_all_non_tmp (void)
|
2009-09-10 16:14:18 +04:00
|
|
|
{
|
2009-09-17 02:07:04 +04:00
|
|
|
tty_color_free_all (FALSE);
|
2009-09-10 16:14:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
void
|
2010-12-29 12:58:10 +03:00
|
|
|
tty_color_set_defaults (const char *fgcolor, const char *bgcolor, const char *attrs)
|
2009-09-10 16:14:18 +04:00
|
|
|
{
|
2009-09-17 02:07:04 +04:00
|
|
|
g_free (tty_color_defaults__fg);
|
2011-01-16 03:13:48 +03:00
|
|
|
g_free (tty_color_defaults__bg);
|
2010-12-29 12:58:10 +03:00
|
|
|
g_free (tty_color_defaults__attrs);
|
2009-09-10 16:14:18 +04:00
|
|
|
|
2013-09-17 12:48:11 +04:00
|
|
|
tty_color_defaults__fg = g_strdup (fgcolor);
|
|
|
|
tty_color_defaults__bg = g_strdup (bgcolor);
|
|
|
|
tty_color_defaults__attrs = g_strdup (attrs);
|
2009-09-10 16:14:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|