1 /****************************************************************************
2 * Copyright (c) 1998,1999,2000 Free Software Foundation, Inc. *
4 * Permission is hereby granted, free of charge, to any person obtaining a *
5 * copy of this software and associated documentation files (the *
6 * "Software"), to deal in the Software without restriction, including *
7 * without limitation the rights to use, copy, modify, merge, publish, *
8 * distribute, distribute with modifications, sublicense, and/or sell *
9 * copies of the Software, and to permit persons to whom the Software is *
10 * furnished to do so, subject to the following conditions: *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS *
16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF *
17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. *
18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, *
19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR *
20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR *
21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. *
23 * Except as contained in this notice, the name(s) of the above copyright *
24 * holders shall not be used in advertising or otherwise to promote the *
25 * sale, use or other dealings in this Software without prior written *
27 ****************************************************************************/
29 /****************************************************************************
30 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
31 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
32 ****************************************************************************/
35 * vidputs(newmode, outc)
37 * newmode is taken to be the logical 'or' of the symbols in curses.h
38 * representing graphic renditions. The terminal is set to be in all of
39 * the given modes, if possible.
41 * if the new attribute is normal
42 * if exit-alt-char-set exists
44 * emit exit-attribute-mode
45 * else if set-attributes exists
46 * use it to set exactly what you want
48 * if exit-attribute-mode exists
51 * turn off those which can be turned off and aren't in
53 * turn on each mode which should be on and isn't, one by one
55 * NOTE that this algorithm won't achieve the desired mix of attributes
56 * in some cases, but those are probably just those cases in which it is
57 * actually impossible, anyway, so...
59 * NOTE that we cannot assume that there's no interaction between color
60 * and other attribute resets. So each time we reset color (or other
61 * attributes) we'll have to be prepared to restore the other.
64 #include <curses.priv.h>
67 MODULE_ID("$Id: lib_vidattr.c,v 1.27 2000/04/29 23:25:27 tom Exp $")
69 #define doPut(mode) TPUTS_TRACE(#mode); tputs(mode, 1, outc)
71 #define TurnOn(mask,mode) \
72 if ((turn_on & mask) && mode) { doPut(mode); }
74 #define TurnOff(mask,mode) \
75 if ((turn_off & mask) && mode) { doPut(mode); turn_off &= ~mask; }
77 /* if there is no current screen, assume we *can* do color */
78 #define SetColorsIf(why,old_attr) \
79 if (can_color && (why)) { \
80 int old_pair = PAIR_NUMBER(old_attr); \
81 T(("old pair = %d -- new pair = %d", old_pair, pair)); \
82 if ((pair != old_pair) \
83 || (fix_pair0 && (pair == 0)) \
84 || (reverse ^ ((old_attr & A_REVERSE) != 0))) { \
85 _nc_do_color(old_pair, pair, reverse, outc); \
90 vidputs(attr_t newmode, int (*outc) (int))
92 static attr_t previous_attr = A_NORMAL;
93 attr_t turn_on, turn_off;
96 bool used_ncv = FALSE;
97 bool can_color = (SP == 0 || SP->_coloron);
98 #ifdef NCURSES_EXT_FUNCS
99 bool fix_pair0 = (SP != 0 && SP->_coloron && !SP->_default_color);
101 #define fix_pair0 FALSE
104 T((T_CALLED("vidputs(%s)"), _traceattr(newmode)));
106 /* this allows us to go on whether or not newterm() has been called */
108 previous_attr = SP->_current_attr;
110 T(("previous attribute was %s", _traceattr(previous_attr)));
114 && (magic_cookie_glitch > 0))
115 newmode &= ~(SP->_xmc_suppress);
119 * If we have a terminal that cannot combine color with video
120 * attributes, use the colors in preference.
122 if (((newmode & A_COLOR) != 0
124 && (no_color_video > 0)) {
126 static const struct {
138 { A_ALTCHARSET, 256 },
143 for (n = 0; n < SIZEOF(table); n++) {
144 if ((table[n].bit & no_color_video)
145 && (table[n].video & newmode)) {
147 if (table[n].video == A_REVERSE)
150 newmode &= ~table[n].video;
155 if (newmode == previous_attr)
158 pair = PAIR_NUMBER(newmode);
161 newmode &= ~A_REVERSE;
164 turn_off = (~newmode & previous_attr) & ALL_BUT_COLOR;
165 turn_on = (newmode & ~previous_attr) & ALL_BUT_COLOR;
167 SetColorsIf(((pair == 0) && !fix_pair0), previous_attr);
169 if (newmode == A_NORMAL) {
170 if ((previous_attr & A_ALTCHARSET) && exit_alt_charset_mode) {
171 doPut(exit_alt_charset_mode);
172 previous_attr &= ~A_ALTCHARSET;
175 doPut(exit_attribute_mode);
176 previous_attr &= ~A_COLOR;
179 SetColorsIf((pair != 0) || fix_pair0, previous_attr);
180 } else if (set_attributes && !used_ncv) {
181 if (turn_on || turn_off) {
182 TPUTS_TRACE("set_attributes");
183 tputs(tparm(set_attributes,
184 (newmode & A_STANDOUT) != 0,
185 (newmode & A_UNDERLINE) != 0,
186 (newmode & A_REVERSE) != 0,
187 (newmode & A_BLINK) != 0,
188 (newmode & A_DIM) != 0,
189 (newmode & A_BOLD) != 0,
190 (newmode & A_INVIS) != 0,
191 (newmode & A_PROTECT) != 0,
192 (newmode & A_ALTCHARSET) != 0), 1, outc);
193 previous_attr &= ~A_COLOR;
195 SetColorsIf((pair != 0) || fix_pair0, previous_attr);
198 T(("turning %s off", _traceattr(turn_off)));
200 TurnOff(A_ALTCHARSET, exit_alt_charset_mode);
202 if (!SP || SP->_use_rmul) {
203 TurnOff(A_UNDERLINE, exit_underline_mode);
206 if (!SP || SP->_use_rmso) {
207 TurnOff(A_STANDOUT, exit_standout_mode);
210 if (turn_off && exit_attribute_mode) {
211 doPut(exit_attribute_mode);
212 turn_on |= (newmode & (chtype) (~A_COLOR));
213 previous_attr &= ~A_COLOR;
215 SetColorsIf((pair != 0) || fix_pair0, previous_attr);
217 T(("turning %s on", _traceattr(turn_on)));
219 TurnOn(A_ALTCHARSET, enter_alt_charset_mode);
220 TurnOn(A_BLINK, enter_blink_mode);
221 TurnOn(A_BOLD, enter_bold_mode);
222 TurnOn(A_DIM, enter_dim_mode);
223 TurnOn(A_REVERSE, enter_reverse_mode);
224 TurnOn(A_STANDOUT, enter_standout_mode);
225 TurnOn(A_PROTECT, enter_protected_mode);
226 TurnOn(A_INVIS, enter_secure_mode);
227 TurnOn(A_UNDERLINE, enter_underline_mode);
228 TurnOn(A_HORIZONTAL, enter_horizontal_hl_mode);
229 TurnOn(A_LEFT, enter_left_hl_mode);
230 TurnOn(A_LOW, enter_low_hl_mode);
231 TurnOn(A_RIGHT, enter_right_hl_mode);
232 TurnOn(A_TOP, enter_top_hl_mode);
233 TurnOn(A_VERTICAL, enter_vertical_hl_mode);
239 newmode |= A_REVERSE;
242 SP->_current_attr = newmode;
244 previous_attr = newmode;
250 vidattr(attr_t newmode)
252 T((T_CALLED("vidattr(%s)"), _traceattr(newmode)));
254 returnCode(vidputs(newmode, _nc_outch));
260 chtype attrs = A_NORMAL;
262 if (enter_alt_charset_mode)
263 attrs |= A_ALTCHARSET;
265 if (enter_blink_mode)
274 if (enter_reverse_mode)
277 if (enter_standout_mode)
280 if (enter_protected_mode)
283 if (enter_secure_mode)
286 if (enter_underline_mode)
287 attrs |= A_UNDERLINE;