123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251 |
- /*
- Color setup for NCurses screen library
- Copyright (C) 1994-2024
- Free Software Foundation, Inc.
- Written by:
- Andrew Borodin <aborodin@vmail.ru>, 2009
- Slava Zanko <slavazanko@gmail.com>, 2010
- Egmont Koblinger <egmont@gmail.com>, 2010
- 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.
- The Midnight Commander 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, see <http://www.gnu.org/licenses/>.
- */
- /** \file color-ncurses.c
- * \brief Source: NCUrses-specific color setup
- */
- #include <config.h>
- #include <stdio.h>
- #include <stdlib.h>
- #include <string.h>
- #include <sys/types.h> /* size_t */
- #include "lib/global.h"
- #include "tty-ncurses.h"
- #include "color.h" /* variables */
- #include "color-internal.h"
- /*** global variables ****************************************************************************/
- /*** file scope macro definitions ****************************************************************/
- /*** file scope type declarations ****************************************************************/
- /*** forward declarations (file scope functions) *************************************************/
- /*** 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 void
- mc_tty_color_save_attr (int color_pair, int color_attr)
- {
- int *attr, *key;
- attr = g_try_new0 (int, 1);
- if (attr == NULL)
- return;
- key = g_try_new (int, 1);
- if (key == NULL)
- {
- g_free (attr);
- return;
- }
- *key = color_pair;
- *attr = color_attr;
- g_hash_table_replace (mc_tty_color_color_pair_attrs, (gpointer) key, (gpointer) attr);
- }
- /* --------------------------------------------------------------------------------------------- */
- static int
- color_get_attr (int color_pair)
- {
- int *fnd = NULL;
- if (mc_tty_color_color_pair_attrs != NULL)
- fnd = (int *) g_hash_table_lookup (mc_tty_color_color_pair_attrs, (gpointer) & color_pair);
- return (fnd != NULL) ? *fnd : 0;
- }
- /* --------------------------------------------------------------------------------------------- */
- static void
- mc_tty_color_pair_init_special (tty_color_lib_pair_t *mc_color_pair,
- int fg1, int bg1, int fg2, int bg2, int attr)
- {
- if (has_colors () && !mc_tty_color_disable)
- init_pair (mc_color_pair->pair_index, fg1, bg1);
- else
- init_pair (mc_color_pair->pair_index, fg2, bg2);
- mc_tty_color_save_attr (mc_color_pair->pair_index, attr);
- }
- /* --------------------------------------------------------------------------------------------- */
- /*** public functions ****************************************************************************/
- /* --------------------------------------------------------------------------------------------- */
- void
- tty_color_init_lib (gboolean disable, gboolean force)
- {
- (void) force;
- if (has_colors () && !disable)
- {
- use_colors = TRUE;
- start_color ();
- use_default_colors ();
- }
- mc_tty_color_color_pair_attrs = g_hash_table_new_full
- (g_int_hash, g_int_equal, mc_tty_color_attr_destroy_cb, mc_tty_color_attr_destroy_cb);
- }
- /* --------------------------------------------------------------------------------------------- */
- void
- tty_color_deinit_lib (void)
- {
- g_hash_table_destroy (mc_tty_color_color_pair_attrs);
- mc_tty_color_color_pair_attrs = NULL;
- }
- /* --------------------------------------------------------------------------------------------- */
- void
- tty_color_try_alloc_lib_pair (tty_color_lib_pair_t *mc_color_pair)
- {
- if (mc_color_pair->fg <= (int) SPEC_A_REVERSE)
- {
- switch (mc_color_pair->fg)
- {
- 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;
- default:
- break;
- }
- }
- else
- {
- int ifg, ibg, attr;
- ifg = mc_color_pair->fg;
- ibg = mc_color_pair->bg;
- attr = mc_color_pair->attr;
- /* In legacy color mode, change bright colors into bold */
- if (!tty_use_256colors (NULL) && !tty_use_truecolors (NULL))
- {
- if (ifg >= 8 && ifg < 16)
- {
- ifg &= 0x07;
- attr |= A_BOLD;
- }
- if (ibg >= 8 && ibg < 16)
- {
- ibg &= 0x07;
- /* attr | = A_BOLD | A_REVERSE ; */
- }
- }
- init_pair (mc_color_pair->pair_index, ifg, ibg);
- mc_tty_color_save_attr (mc_color_pair->pair_index, attr);
- }
- }
- /* --------------------------------------------------------------------------------------------- */
- void
- tty_setcolor (int color)
- {
- attrset (COLOR_PAIR (color) | color_get_attr (color));
- }
- /* --------------------------------------------------------------------------------------------- */
- void
- tty_lowlevel_setcolor (int color)
- {
- tty_setcolor (color);
- }
- /* --------------------------------------------------------------------------------------------- */
- void
- tty_set_normal_attrs (void)
- {
- standend ();
- }
- /* --------------------------------------------------------------------------------------------- */
- gboolean
- tty_use_256colors (GError **error)
- {
- (void) error;
- return (COLORS == 256);
- }
- /* --------------------------------------------------------------------------------------------- */
- gboolean
- tty_use_truecolors (GError **error)
- {
- /* Not yet supported in ncurses */
- g_set_error (error, MC_ERROR, -1, _("True color not supported with ncurses."));
- return FALSE;
- }
- /* --------------------------------------------------------------------------------------------- */
|