2011-10-15 14:56:47 +04:00
|
|
|
/*
|
|
|
|
Internal stuff of color setup
|
|
|
|
|
2009-06-12 14:55:06 +04:00
|
|
|
Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
|
2013-10-15 10:34:04 +04:00
|
|
|
2007, 2008, 2009, 2010, 2011, 2013
|
2011-10-15 14:56:47 +04:00
|
|
|
The 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
|
2013-10-15 10:34:04 +04:00
|
|
|
Slava Zanko <slavazanko@gmail.com>, 2009, 2013
|
2010-12-29 12:58:10 +03:00
|
|
|
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-06-12 14:55:06 +04:00
|
|
|
|
2011-10-15 14:56:47 +04:00
|
|
|
The Midnight Commander is distributed in the hope that it will be useful,
|
2009-06-12 14:55:06 +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-internal.c
|
|
|
|
* \brief Source: Internal stuff of color setup
|
|
|
|
*/
|
|
|
|
|
|
|
|
#include <config.h>
|
|
|
|
|
2009-09-17 02:07:04 +04:00
|
|
|
#include <string.h> /* strcmp */
|
2009-05-10 19:01:15 +04:00
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
#include "color.h" /* colors and attributes */
|
2010-01-07 02:57:27 +03:00
|
|
|
#include "color-internal.h"
|
2009-05-10 19:01:15 +04:00
|
|
|
|
2009-09-04 18:22:49 +04:00
|
|
|
/*** global variables ****************************************************************************/
|
|
|
|
|
2009-09-09 18:56:52 +04:00
|
|
|
gboolean mc_tty_color_disable;
|
|
|
|
|
2009-09-04 18:22:49 +04:00
|
|
|
/*** file scope macro definitions ****************************************************************/
|
|
|
|
|
2011-01-11 17:11:08 +03:00
|
|
|
#define COLOR_INTENSITY 8
|
|
|
|
|
2009-09-04 18:22:49 +04:00
|
|
|
/*** file scope type declarations ****************************************************************/
|
|
|
|
|
2010-11-08 13:21:45 +03:00
|
|
|
typedef struct mc_tty_color_table_struct
|
|
|
|
{
|
2009-09-04 18:22:49 +04:00
|
|
|
const char *name;
|
2009-09-17 02:07:04 +04:00
|
|
|
int value;
|
2009-09-04 18:22:49 +04:00
|
|
|
} mc_tty_color_table_t;
|
|
|
|
|
|
|
|
/*** file scope variables ************************************************************************/
|
|
|
|
|
2009-09-17 02:07:04 +04:00
|
|
|
mc_tty_color_table_t const color_table[] = {
|
2010-11-08 13:21:45 +03:00
|
|
|
{"black", COLOR_BLACK},
|
2011-01-11 17:11:08 +03:00
|
|
|
{"gray", COLOR_BLACK + COLOR_INTENSITY},
|
2010-11-08 13:21:45 +03:00
|
|
|
{"red", COLOR_RED},
|
2011-01-11 17:11:08 +03:00
|
|
|
{"brightred", COLOR_RED + COLOR_INTENSITY},
|
2010-11-08 13:21:45 +03:00
|
|
|
{"green", COLOR_GREEN},
|
2011-01-11 17:11:08 +03:00
|
|
|
{"brightgreen", COLOR_GREEN + COLOR_INTENSITY},
|
2010-11-08 13:21:45 +03:00
|
|
|
{"brown", COLOR_YELLOW},
|
2011-01-11 17:11:08 +03:00
|
|
|
{"yellow", COLOR_YELLOW + COLOR_INTENSITY},
|
2010-11-08 13:21:45 +03:00
|
|
|
{"blue", COLOR_BLUE},
|
2011-01-11 17:11:08 +03:00
|
|
|
{"brightblue", COLOR_BLUE + COLOR_INTENSITY},
|
2010-11-08 13:21:45 +03:00
|
|
|
{"magenta", COLOR_MAGENTA},
|
2011-01-11 17:11:08 +03:00
|
|
|
{"brightmagenta", COLOR_MAGENTA + COLOR_INTENSITY},
|
2010-11-08 13:21:45 +03:00
|
|
|
{"cyan", COLOR_CYAN},
|
2011-01-11 17:11:08 +03:00
|
|
|
{"brightcyan", COLOR_CYAN + COLOR_INTENSITY},
|
2010-11-08 13:21:45 +03:00
|
|
|
{"lightgray", COLOR_WHITE},
|
2011-01-11 17:11:08 +03:00
|
|
|
{"white", COLOR_WHITE + COLOR_INTENSITY},
|
2010-11-08 13:21:45 +03:00
|
|
|
{"default", -1}, /* default color of the terminal */
|
2009-09-09 18:56:52 +04:00
|
|
|
/* special colors */
|
2010-11-08 13:21:45 +03:00
|
|
|
{"A_REVERSE", SPEC_A_REVERSE},
|
|
|
|
{"A_BOLD", SPEC_A_BOLD},
|
|
|
|
{"A_BOLD_REVERSE", SPEC_A_BOLD_REVERSE},
|
|
|
|
{"A_UNDERLINE", SPEC_A_UNDERLINE},
|
2009-09-09 18:56:52 +04:00
|
|
|
/* End of list */
|
2010-11-08 13:21:45 +03:00
|
|
|
{NULL, 0}
|
2009-05-10 19:01:15 +04:00
|
|
|
};
|
|
|
|
|
2010-12-29 12:58:10 +03:00
|
|
|
mc_tty_color_table_t const attributes_table[] = {
|
|
|
|
{"bold", A_BOLD},
|
|
|
|
{"underline", A_UNDERLINE},
|
|
|
|
{"reverse", A_REVERSE},
|
|
|
|
{"blink", A_BLINK},
|
|
|
|
/* End of list */
|
|
|
|
{NULL, 0}
|
|
|
|
};
|
|
|
|
|
2009-09-04 18:22:49 +04:00
|
|
|
/*** file scope functions ************************************************************************/
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
2010-12-29 12:58:10 +03:00
|
|
|
static int
|
|
|
|
parse_256_color_name (const char *color_name)
|
|
|
|
{
|
|
|
|
int i;
|
|
|
|
char dummy;
|
|
|
|
if (sscanf (color_name, "color%d%c", &i, &dummy) == 1 && i >= 0 && i < 256)
|
|
|
|
{
|
|
|
|
return i;
|
|
|
|
}
|
|
|
|
if (sscanf (color_name, "gray%d%c", &i, &dummy) == 1 && i >= 0 && i < 24)
|
|
|
|
{
|
|
|
|
return 232 + i;
|
|
|
|
}
|
|
|
|
if (strncmp (color_name, "rgb", 3) == 0 &&
|
|
|
|
color_name[3] >= '0' && color_name[3] < '6' &&
|
|
|
|
color_name[4] >= '0' && color_name[4] < '6' &&
|
|
|
|
color_name[5] >= '0' && color_name[5] < '6' && color_name[6] == '\0')
|
|
|
|
{
|
|
|
|
return 16 + 36 * (color_name[3] - '0') + 6 * (color_name[4] - '0') + (color_name[5] - '0');
|
|
|
|
}
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2009-09-04 18:22:49 +04:00
|
|
|
/*** public functions ****************************************************************************/
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2009-05-10 19:01:15 +04:00
|
|
|
|
2009-09-04 18:22:49 +04:00
|
|
|
const char *
|
2010-12-29 12:58:10 +03:00
|
|
|
tty_color_get_name_by_index (int idx)
|
2009-05-10 19:01:15 +04:00
|
|
|
{
|
2010-12-29 12:58:10 +03:00
|
|
|
int i;
|
|
|
|
|
|
|
|
/* Find the real English name of the first 16 colors, */
|
|
|
|
/* as well as the A_* special values. */
|
|
|
|
for (i = 0; color_table[i].name != NULL; i++)
|
|
|
|
if (idx == color_table[i].value)
|
|
|
|
return color_table[i].name;
|
|
|
|
/* Create and return the strings "color16" to "color255". */
|
|
|
|
if (idx >= 16 && idx < 256)
|
2010-09-12 18:32:26 +04:00
|
|
|
{
|
2013-10-15 10:34:04 +04:00
|
|
|
static char **color_N_names = NULL;
|
|
|
|
|
2010-12-29 12:58:10 +03:00
|
|
|
if (color_N_names == NULL)
|
|
|
|
{
|
|
|
|
color_N_names = g_try_malloc0 (240 * sizeof (char *));
|
|
|
|
}
|
|
|
|
if (color_N_names[idx - 16] == NULL)
|
|
|
|
{
|
|
|
|
color_N_names[idx - 16] = g_try_malloc (9);
|
|
|
|
sprintf (color_N_names[idx - 16], "color%d", idx);
|
|
|
|
}
|
|
|
|
return color_N_names[idx - 16];
|
2010-09-12 18:32:26 +04:00
|
|
|
}
|
2010-12-29 12:58:10 +03:00
|
|
|
return "default";
|
2009-05-10 19:01:15 +04:00
|
|
|
}
|
|
|
|
|
2009-09-04 18:22:49 +04:00
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
int
|
2009-09-14 17:43:03 +04:00
|
|
|
tty_color_get_index_by_name (const char *color_name)
|
2009-05-10 19:01:15 +04:00
|
|
|
{
|
2009-09-04 18:22:49 +04:00
|
|
|
|
2009-09-14 17:43:03 +04:00
|
|
|
if (color_name != NULL)
|
2010-09-12 18:32:26 +04:00
|
|
|
{
|
|
|
|
size_t i;
|
2009-09-17 02:07:04 +04:00
|
|
|
for (i = 0; color_table[i].name != NULL; i++)
|
|
|
|
if (strcmp (color_name, color_table[i].name) == 0)
|
|
|
|
return color_table[i].value;
|
2010-12-29 12:58:10 +03:00
|
|
|
return parse_256_color_name (color_name);
|
2010-09-12 18:32:26 +04:00
|
|
|
}
|
2009-09-04 18:22:49 +04:00
|
|
|
return -1;
|
2009-05-10 19:01:15 +04:00
|
|
|
}
|
2009-09-04 18:22:49 +04:00
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
2010-12-29 12:58:10 +03:00
|
|
|
|
|
|
|
int
|
|
|
|
tty_attr_get_bits (const char *attrs)
|
|
|
|
{
|
|
|
|
int attr_bits = 0;
|
|
|
|
|
|
|
|
if (attrs != NULL)
|
|
|
|
{
|
2013-10-15 10:34:04 +04:00
|
|
|
gchar **attr_list;
|
|
|
|
int i;
|
|
|
|
|
2010-12-29 12:58:10 +03:00
|
|
|
attr_list = g_strsplit (attrs, "+", -1);
|
2013-10-15 10:34:04 +04:00
|
|
|
|
2010-12-29 12:58:10 +03:00
|
|
|
for (i = 0; attr_list[i] != NULL; i++)
|
|
|
|
{
|
2013-10-15 10:34:04 +04:00
|
|
|
int j;
|
|
|
|
|
2010-12-29 12:58:10 +03:00
|
|
|
for (j = 0; attributes_table[j].name != NULL; j++)
|
|
|
|
{
|
|
|
|
if (strcmp (attr_list[i], attributes_table[j].name) == 0)
|
|
|
|
{
|
|
|
|
attr_bits |= attributes_table[j].value;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
g_strfreev (attr_list);
|
|
|
|
}
|
|
|
|
return attr_bits;
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|