2009-09-04 18:22:49 +04:00
|
|
|
/* Color setup.
|
2010-02-26 12:25:53 +03:00
|
|
|
Interface functions.
|
2009-09-04 18:22:49 +04:00
|
|
|
|
2007-09-26 14:22:25 +04:00
|
|
|
Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
|
2009-05-10 19:01:15 +04:00
|
|
|
2007, 2008, 2009 Free Software Foundation, Inc.
|
2010-02-26 12:25:53 +03:00
|
|
|
|
2009-06-12 14:55:06 +04:00
|
|
|
Written by:
|
|
|
|
Andrew Borodin <aborodin@vmail.ru>, 2009.
|
2009-09-04 18:22:49 +04:00
|
|
|
Slava Zanko <slavazanko@gmail.com>, 2009.
|
2010-02-26 12:25:53 +03:00
|
|
|
|
1998-02-27 07:54:42 +03:00
|
|
|
This program 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 2 of the License, or
|
|
|
|
(at your option) any later version.
|
2009-05-10 19:01:15 +04:00
|
|
|
|
1998-02-27 07:54:42 +03:00
|
|
|
This program is distributed in the hope that it will be useful,
|
|
|
|
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
|
|
|
|
along with this program; if not, write to the Free Software
|
2005-05-27 07:35:10 +04:00
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
|
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-01 15:05:01 +04:00
|
|
|
char *command_line_colors = NULL;
|
2009-06-04 23:55:47 +04:00
|
|
|
|
2009-09-10 16:14:18 +04:00
|
|
|
static char *tty_color_defaults__fg = NULL;
|
|
|
|
static char *tty_color_defaults__bg = NULL;
|
|
|
|
|
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,
|
2010-09-12 18:32:26 +04:00
|
|
|
is_temp_color ? GINT_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-09-12 18:32:26 +04:00
|
|
|
int cp;
|
2009-09-17 02:07:04 +04:00
|
|
|
tty_color_pair_t *mc_color_pair;
|
|
|
|
(void) key;
|
|
|
|
|
2010-09-12 18:32:26 +04:00
|
|
|
cp = GPOINTER_TO_INT (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
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static int
|
2010-09-12 18:32:26 +04:00
|
|
|
tty_color_get_next__color_pair_number (void)
|
2009-09-10 16:14:18 +04:00
|
|
|
{
|
2010-09-12 18:32:26 +04:00
|
|
|
const size_t cp_count = g_hash_table_size (mc_tty_color__hashtable);
|
2010-10-12 13:33:09 +04:00
|
|
|
size_t cp;
|
2009-09-10 16:14:18 +04:00
|
|
|
|
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,
|
|
|
|
GINT_TO_POINTER (cp)) == NULL)
|
|
|
|
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);
|
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
|
|
|
|
tty_try_alloc_color_pair2 (const char *fg, const char *bg, 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;
|
2009-09-04 18:22:49 +04:00
|
|
|
const char *c_fg, *c_bg;
|
2009-08-31 14:17:29 +04:00
|
|
|
|
2009-09-10 16:14:18 +04:00
|
|
|
if (fg == NULL)
|
2009-09-17 02:07:04 +04:00
|
|
|
fg = tty_color_defaults__fg;
|
2010-02-26 12:25:53 +03:00
|
|
|
if (bg == NULL)
|
2009-09-17 02:07:04 +04:00
|
|
|
bg = tty_color_defaults__bg;
|
2010-09-12 18:32:26 +04:00
|
|
|
|
2009-09-14 17:43:03 +04:00
|
|
|
c_fg = tty_color_get_valid_name (fg);
|
|
|
|
c_bg = tty_color_get_valid_name (bg);
|
2009-08-31 14:17:29 +04:00
|
|
|
|
2009-09-17 02:07:04 +04:00
|
|
|
color_pair = g_strdup_printf ("%s.%s", c_fg, c_bg);
|
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;
|
2009-09-04 18:22:49 +04:00
|
|
|
mc_color_pair->cfg = c_fg;
|
|
|
|
mc_color_pair->cbg = c_bg;
|
2009-09-14 17:43:03 +04:00
|
|
|
mc_color_pair->ifg = tty_color_get_index_by_name (c_fg);
|
|
|
|
mc_color_pair->ibg = tty_color_get_index_by_name (c_bg);
|
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
|
|
|
|
tty_try_alloc_color_pair (const char *fg, const char *bg)
|
2009-09-10 16:14:18 +04:00
|
|
|
{
|
|
|
|
return tty_try_alloc_color_pair2 (fg, bg, TRUE);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
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
|
2009-09-14 17:43:03 +04:00
|
|
|
tty_color_set_defaults (const char *fgcolor, const char *bgcolor)
|
2009-09-10 16:14:18 +04:00
|
|
|
{
|
2009-09-17 02:07:04 +04:00
|
|
|
g_free (tty_color_defaults__fg);
|
|
|
|
g_free (tty_color_defaults__fg);
|
2009-09-10 16:14:18 +04:00
|
|
|
|
2009-09-14 17:43:03 +04:00
|
|
|
tty_color_defaults__fg = (fgcolor != NULL) ? g_strdup (fgcolor) : NULL;
|
|
|
|
tty_color_defaults__bg = (bgcolor != NULL) ? g_strdup (bgcolor) : NULL;
|
2009-09-10 16:14:18 +04:00
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|