Massive moved some dirs from $(srcdir)/src into $(srcdir)/lib
[midnight-commander.git] / lib / tty / color-ncurses.c
blobf31750ef20c6d210ac14025ad5f91f44c7162f16
1 /* Color setup for NCurses screen library
2 Copyright (C) 1994, 1998, 1999, 2000, 2001, 2002, 2003, 2004, 2005,
3 2007, 2008, 2009 Free Software Foundation, Inc.
5 Written by:
6 Andrew Borodin <aborodin@vmail.ru>, 2009.
8 This program is free software; you can redistribute it and/or modify
9 it under the terms of the GNU General Public License as published by
10 the Free Software Foundation; either version 2 of the License, or
11 (at your option) any later version.
13 This program is distributed in the hope that it will be useful,
14 but WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 GNU General Public License for more details.
18 You should have received a copy of the GNU General Public License
19 along with this program; if not, write to the Free Software
20 Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA. */
22 /** \file color-ncurses.c
23 * \brief Source: NCUrses-specific color setup
26 #include <config.h>
28 #include <stdio.h>
29 #include <stdlib.h>
30 #include <string.h>
31 #include <sys/types.h> /* size_t */
33 #include "../../src/global.h"
35 #include "../../src/tty/tty-ncurses.h"
36 #include "../../src/tty/color.h" /* variables */
37 #include "../../src/tty/color-internal.h"
39 /*** global variables ****************************************************************************/
41 /*** file scope macro definitions ****************************************************************/
43 /*** file scope type declarations ****************************************************************/
45 /*** file scope variables ************************************************************************/
47 static GHashTable *mc_tty_color_color_pair_attrs = NULL;
49 /*** file scope functions ************************************************************************/
50 /* --------------------------------------------------------------------------------------------- */
52 static inline void
53 mc_tty_color_attr_destroy_cb (gpointer data)
55 g_free (data);
58 /* --------------------------------------------------------------------------------------------- */
60 static int
61 mc_tty_color_save_attr_lib (int color_pair, int color_attr)
63 int *attr, *key;
64 attr = g_try_new0 (int, 1);
65 if (attr == NULL)
66 return color_attr;
68 key = g_try_new (int, 1);
69 if (key == NULL) {
70 g_free (attr);
71 return color_attr;
74 *key = color_pair;
76 if (color_attr != -1)
77 *attr = color_attr & (A_BOLD | A_REVERSE | A_UNDERLINE);
78 g_hash_table_replace (mc_tty_color_color_pair_attrs, (gpointer) key, (gpointer) attr);
79 return color_attr & (~(*attr));
82 /* --------------------------------------------------------------------------------------------- */
84 static int
85 color_get_attr (int color_pair)
87 int *fnd = NULL;
89 if (mc_tty_color_color_pair_attrs != NULL)
90 fnd = (int *) g_hash_table_lookup (mc_tty_color_color_pair_attrs, (gpointer) & color_pair);
91 return (fnd != NULL) ? *fnd : 0;
94 /* --------------------------------------------------------------------------------------------- */
96 static void
97 mc_tty_color_pair_init_special (tty_color_pair_t * mc_color_pair,
98 int fg1, int bg1, int fg2, int bg2, int mask)
100 if (has_colors ()) {
101 if (!mc_tty_color_disable) {
102 init_pair (mc_color_pair->pair_index,
103 mc_tty_color_save_attr_lib (mc_color_pair->pair_index, fg1 | mask), bg1);
104 } else {
105 init_pair (mc_color_pair->pair_index,
106 mc_tty_color_save_attr_lib (mc_color_pair->pair_index, fg2 | mask), bg2);
109 #if 0
110 else {
111 SLtt_set_mono (mc_color_pair->pair_index, NULL, mask);
113 #endif
116 /* --------------------------------------------------------------------------------------------- */
117 /*** public functions ****************************************************************************/
118 /* --------------------------------------------------------------------------------------------- */
120 void
121 tty_color_init_lib (gboolean disable, gboolean force)
123 (void) force;
125 if (has_colors () && !disable) {
126 use_colors = TRUE;
127 start_color ();
128 use_default_colors ();
131 mc_tty_color_color_pair_attrs = g_hash_table_new_full
132 (g_int_hash, g_int_equal, mc_tty_color_attr_destroy_cb, mc_tty_color_attr_destroy_cb);
135 /* --------------------------------------------------------------------------------------------- */
137 void
138 tty_color_deinit_lib (void)
140 g_hash_table_destroy (mc_tty_color_color_pair_attrs);
141 mc_tty_color_color_pair_attrs = NULL;
144 /* --------------------------------------------------------------------------------------------- */
146 void
147 tty_color_try_alloc_pair_lib (tty_color_pair_t * mc_color_pair)
149 if (mc_color_pair->ifg <= (int) SPEC_A_REVERSE) {
150 switch (mc_color_pair->ifg) {
151 case SPEC_A_REVERSE:
152 mc_tty_color_pair_init_special (mc_color_pair,
153 COLOR_BLACK, COLOR_WHITE,
154 COLOR_BLACK, COLOR_WHITE | A_BOLD, A_REVERSE);
155 break;
156 case SPEC_A_BOLD:
157 mc_tty_color_pair_init_special (mc_color_pair,
158 COLOR_WHITE, COLOR_BLACK,
159 COLOR_WHITE, COLOR_BLACK, A_BOLD);
160 break;
161 case SPEC_A_BOLD_REVERSE:
163 mc_tty_color_pair_init_special (mc_color_pair,
164 COLOR_WHITE, COLOR_WHITE,
165 COLOR_WHITE, COLOR_WHITE, A_BOLD | A_REVERSE);
166 break;
167 case SPEC_A_UNDERLINE:
168 mc_tty_color_pair_init_special (mc_color_pair,
169 COLOR_WHITE, COLOR_BLACK,
170 COLOR_WHITE, COLOR_BLACK, A_UNDERLINE);
171 break;
173 } else {
174 int mask_fg = (mc_color_pair->ifg == -1) ? mc_color_pair->ifg : 0xff;
175 int mask_bg = (mc_color_pair->ibg == -1) ? mc_color_pair->ibg : 0xff;
177 init_pair (mc_color_pair->pair_index,
178 mc_tty_color_save_attr_lib (mc_color_pair->pair_index,
179 mc_color_pair->ifg) & mask_fg,
180 mc_color_pair->ibg & mask_bg);
184 /* --------------------------------------------------------------------------------------------- */
186 void
187 tty_setcolor (int color)
189 attrset (COLOR_PAIR (color) | color_get_attr (color));
192 /* --------------------------------------------------------------------------------------------- */
194 void
195 tty_lowlevel_setcolor (int color)
197 attrset (COLOR_PAIR (color) | color_get_attr (color));
200 /* --------------------------------------------------------------------------------------------- */
202 void
203 tty_set_normal_attrs (void)
205 standend ();
208 /* --------------------------------------------------------------------------------------------- */