2009-05-10 19:01:15 +04:00
|
|
|
/* Color setup for NCurses screen library
|
|
|
|
Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
|
|
|
|
2007, 2008, 2009 Free Software Foundation, Inc.
|
2009-06-12 14:55:06 +04:00
|
|
|
|
|
|
|
Written by:
|
|
|
|
Andrew Borodin <aborodin@vmail.ru>, 2009.
|
|
|
|
|
2009-05-10 19:01:15 +04: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.
|
|
|
|
|
|
|
|
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
|
|
|
|
Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
|
|
|
|
|
|
|
|
/** \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
|
|
|
|
|
|
|
#include "../../src/global.h"
|
|
|
|
|
|
|
|
#include "../../src/tty/tty-ncurses.h"
|
2009-09-17 02:07:04 +04:00
|
|
|
#include "../../src/tty/color.h" /* variables */
|
2009-05-10 19:01:15 +04:00
|
|
|
#include "../../src/tty/color-internal.h"
|
|
|
|
|
2009-09-14 17:43:03 +04:00
|
|
|
/*** global variables ****************************************************************************/
|
|
|
|
|
|
|
|
/*** file scope macro definitions ****************************************************************/
|
|
|
|
|
|
|
|
/*** file scope type declarations ****************************************************************/
|
|
|
|
|
|
|
|
/*** file scope variables ************************************************************************/
|
|
|
|
|
|
|
|
static GHashTable *mc_tty_color_color_pair_attrs = NULL;
|
|
|
|
|
|
|
|
/*** file scope functions ************************************************************************/
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static inline void
|
|
|
|
mc_tty_color_attr_destroy_cb (gpointer data)
|
|
|
|
{
|
|
|
|
g_free (data);
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
|
|
|
|
static int
|
|
|
|
mc_tty_color_save_attr_lib (int color_pair, int color_attr)
|
|
|
|
{
|
|
|
|
int *attr, *key;
|
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)
|
2009-09-17 02:07:04 +04:00
|
|
|
return color_attr;
|
2009-09-14 17:43:03 +04:00
|
|
|
|
2009-12-12 13:54:22 +03:00
|
|
|
key = g_try_new (int, 1);
|
2009-09-17 02:07:04 +04:00
|
|
|
if (key == NULL) {
|
|
|
|
g_free (attr);
|
|
|
|
return color_attr;
|
2009-09-14 17:43:03 +04:00
|
|
|
}
|
2009-12-12 13:54:22 +03:00
|
|
|
|
|
|
|
*key = color_pair;
|
2009-09-14 17:43:03 +04:00
|
|
|
|
|
|
|
if (color_attr != -1)
|
2009-09-17 02:07:04 +04:00
|
|
|
*attr = color_attr & (A_BOLD | A_REVERSE | A_UNDERLINE);
|
2009-09-14 17:43:03 +04:00
|
|
|
g_hash_table_replace (mc_tty_color_color_pair_attrs, (gpointer) key, (gpointer) attr);
|
|
|
|
return color_attr & (~(*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)
|
|
|
|
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
|
2009-09-17 02:07:04 +04:00
|
|
|
mc_tty_color_pair_init_special (tty_color_pair_t * mc_color_pair,
|
|
|
|
int fg1, int bg1, int fg2, int bg2, int mask)
|
2009-09-14 17:43:03 +04:00
|
|
|
{
|
2009-09-17 02:07:04 +04:00
|
|
|
if (has_colors ()) {
|
|
|
|
if (!mc_tty_color_disable) {
|
|
|
|
init_pair (mc_color_pair->pair_index,
|
|
|
|
mc_tty_color_save_attr_lib (mc_color_pair->pair_index, fg1 | mask), bg1);
|
|
|
|
} else {
|
|
|
|
init_pair (mc_color_pair->pair_index,
|
|
|
|
mc_tty_color_save_attr_lib (mc_color_pair->pair_index, fg2 | mask), bg2);
|
|
|
|
}
|
2009-09-14 17:43:03 +04:00
|
|
|
}
|
|
|
|
#if 0
|
2009-09-17 02:07:04 +04:00
|
|
|
else {
|
|
|
|
SLtt_set_mono (mc_color_pair->pair_index, NULL, mask);
|
2009-09-14 17:43:03 +04:00
|
|
|
}
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|
|
|
|
/*** 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
|
|
|
|
2009-07-08 22:37:01 +04: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
|
2009-09-17 02:07:04 +04:00
|
|
|
tty_color_try_alloc_pair_lib (tty_color_pair_t * mc_color_pair)
|
2009-05-10 19:01:15 +04:00
|
|
|
{
|
2009-09-17 02:07:04 +04:00
|
|
|
if (mc_color_pair->ifg <= (int) SPEC_A_REVERSE) {
|
|
|
|
switch (mc_color_pair->ifg) {
|
|
|
|
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;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
init_pair (mc_color_pair->pair_index,
|
|
|
|
mc_tty_color_save_attr_lib (mc_color_pair->pair_index,
|
|
|
|
mc_color_pair->ifg) & COLOR_WHITE,
|
|
|
|
mc_color_pair->ibg & COLOR_WHITE);
|
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
|
|
|
{
|
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-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
|
|
|
|
|
|
|
/* --------------------------------------------------------------------------------------------- */
|