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
|
|
|
|
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-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
|
|
|
|
2023-09-10 13:16:48 +03:00
|
|
|
/* *INDENT-OFF* */
|
|
|
|
static tty_color_pair_t tty_color_defaults =
|
|
|
|
{
|
|
|
|
.fg = NULL,
|
|
|
|
.bg = NULL,
|
|
|
|
.attrs = NULL,
|
|
|
|
.pair_index = 0
|
|
|
|
};
|
|
|
|
/* *INDENT-ON* */
|
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 ****************************************************************/
|
|
|
|
|
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-04 18:22:49 +04:00
|
|
|
/*** file scope variables ************************************************************************/
|
|
|
|
|
|
|
|
static GHashTable *mc_tty_color__hashtable = 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-04 18:22:49 +04:00
|
|
|
/*** file scope functions ************************************************************************/
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
2023-09-10 13:16:48 +03:00
|
|
|
static void
|
2024-06-01 21:12:14 +03:00
|
|
|
mc_color__deinit (tty_color_pair_t *color)
|
2023-09-10 13:16:48 +03:00
|
|
|
{
|
|
|
|
g_free (color->fg);
|
|
|
|
g_free (color->bg);
|
|
|
|
g_free (color->attrs);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
2009-09-10 16:14:18 +04:00
|
|
|
static gboolean
|
2023-06-12 12:35:20 +03:00
|
|
|
tty_color_free_temp_cb (gpointer key, gpointer value, gpointer user_data)
|
2009-09-10 16:14:18 +04:00
|
|
|
{
|
2009-09-17 02:07:04 +04:00
|
|
|
(void) key;
|
2023-06-12 12:35:20 +03:00
|
|
|
(void) user_data;
|
2009-09-17 02:07:04 +04:00
|
|
|
|
2023-06-12 12:35:20 +03:00
|
|
|
return ((tty_color_lib_pair_t *) value)->is_temp;
|
2009-09-10 16:14:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static gboolean
|
|
|
|
tty_color_get_next_cpn_cb (gpointer key, gpointer value, gpointer user_data)
|
|
|
|
{
|
2023-09-10 12:22:26 +03:00
|
|
|
tty_color_lib_pair_t *mc_color_pair = (tty_color_lib_pair_t *) value;
|
2023-06-12 10:15:55 +03:00
|
|
|
size_t cp = GPOINTER_TO_SIZE (user_data);
|
2009-09-17 02:07:04 +04:00
|
|
|
|
2023-06-12 10:15:55 +03:00
|
|
|
(void) key;
|
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 ();
|
2023-09-10 13:16:48 +03:00
|
|
|
mc_color__deinit (&tty_color_defaults);
|
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
|
2024-06-01 21:12:14 +03:00
|
|
|
tty_try_alloc_color_pair (const tty_color_pair_t *color, gboolean is_temp)
|
2009-09-04 18:22:49 +04:00
|
|
|
{
|
2023-06-12 11:26:53 +03:00
|
|
|
gboolean is_base;
|
2009-09-04 18:22:49 +04:00
|
|
|
gchar *color_pair;
|
2023-09-10 12:22:26 +03:00
|
|
|
tty_color_lib_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
|
|
|
|
2023-06-12 11:26:53 +03:00
|
|
|
is_base = (color->fg == NULL || strcmp (color->fg, "base") == 0);
|
|
|
|
ifg = tty_color_get_index_by_name (is_base ? tty_color_defaults.fg : color->fg);
|
|
|
|
is_base = (color->bg == NULL || strcmp (color->bg, "base") == 0);
|
|
|
|
ibg = tty_color_get_index_by_name (is_base ? tty_color_defaults.bg : color->bg);
|
|
|
|
is_base = (color->attrs == NULL || strcmp (color->attrs, "base") == 0);
|
|
|
|
attr = tty_attr_get_bits (is_base ? tty_color_defaults.attrs : color->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 =
|
2023-09-10 12:22:26 +03:00
|
|
|
(tty_color_lib_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
|
|
|
|
2023-09-10 12:22:26 +03:00
|
|
|
mc_color_pair = g_try_new0 (tty_color_lib_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
|
|
|
|
2023-06-12 11:26:53 +03:00
|
|
|
mc_color_pair->is_temp = is_temp;
|
2023-09-10 12:22:26 +03:00
|
|
|
mc_color_pair->fg = ifg;
|
|
|
|
mc_color_pair->bg = ibg;
|
2010-12-29 12:58:10 +03:00
|
|
|
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
|
|
|
|
2023-09-10 12:22:26 +03:00
|
|
|
tty_color_try_alloc_lib_pair (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-10 16:14:18 +04:00
|
|
|
void
|
2023-06-12 12:35:20 +03:00
|
|
|
tty_color_free_temp (void)
|
2009-09-10 16:14:18 +04:00
|
|
|
{
|
2023-06-12 12:35:20 +03:00
|
|
|
g_hash_table_foreach_remove (mc_tty_color__hashtable, tty_color_free_temp_cb, NULL);
|
2009-09-10 16:14:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
void
|
2023-06-12 12:35:20 +03:00
|
|
|
tty_color_free_all (void)
|
2009-09-10 16:14:18 +04:00
|
|
|
{
|
2023-06-12 12:35:20 +03:00
|
|
|
g_hash_table_remove_all (mc_tty_color__hashtable);
|
2009-09-10 16:14:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
void
|
2024-06-01 21:12:14 +03:00
|
|
|
tty_color_set_defaults (const tty_color_pair_t *color)
|
2009-09-10 16:14:18 +04:00
|
|
|
{
|
2023-09-10 13:16:48 +03:00
|
|
|
mc_color__deinit (&tty_color_defaults);
|
2009-09-10 16:14:18 +04:00
|
|
|
|
2023-06-12 11:26:53 +03:00
|
|
|
tty_color_defaults.fg = g_strdup (color->fg);
|
|
|
|
tty_color_defaults.bg = g_strdup (color->bg);
|
|
|
|
tty_color_defaults.attrs = g_strdup (color->attrs);
|
2023-09-10 13:16:48 +03:00
|
|
|
tty_color_defaults.pair_index = 0;
|
2009-09-10 16:14:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|