1 /****************************************************************************
2 * Copyright (c) 1998 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.20 1998/02/11 12:13:56 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 int vidputs(attr_t newmode, int (*outc)(int))
79 static attr_t previous_attr = A_NORMAL;
80 attr_t turn_on, turn_off;
81 int pair, current_pair;
83 bool used_ncv = FALSE;
85 T((T_CALLED("vidputs(%s)"), _traceattr(newmode)));
87 /* this allows us to go on whether or not newterm() has been called */
89 previous_attr = SP->_current_attr;
91 T(("previous attribute was %s", _traceattr(previous_attr)));
94 if (magic_cookie_glitch > 0)
95 newmode &= ~(SP->_xmc_suppress);
99 * If we have a terminal that cannot combine color with video
100 * attributes, use the colors in preference.
102 if ((newmode & A_COLOR)
103 && (no_color_video > 0)) {
104 static const struct {
116 { A_ALTCHARSET, 256 },
119 for (n = 0; n < SIZEOF(table); n++) {
120 if ((table[n].bit & no_color_video)
121 && (table[n].video & newmode)) {
123 if (table[n].video == A_REVERSE)
126 newmode &= ~table[n].video;
131 if (newmode == previous_attr)
134 pair = PAIR_NUMBER(newmode);
135 current_pair = PAIR_NUMBER(previous_attr);
138 newmode &= ~A_REVERSE;
141 if (previous_attr & A_REVERSE)
142 current_pair = -current_pair;
144 turn_off = (~newmode & previous_attr) & ALL_BUT_COLOR;
145 turn_on = (newmode & ~previous_attr) & ALL_BUT_COLOR;
147 /* if there is no current screen, assume we *can* do color */
148 if ((!SP || SP->_coloron) && pair == 0) {
149 T(("old pair = %d -- new pair = %d", current_pair, pair));
150 if (pair != current_pair) {
151 _nc_do_color(pair, reverse, outc);
152 previous_attr &= ~A_COLOR;
156 if (newmode == A_NORMAL) {
157 if((previous_attr & A_ALTCHARSET) && exit_alt_charset_mode) {
158 doPut(exit_alt_charset_mode);
159 previous_attr &= ~A_ALTCHARSET;
162 doPut(exit_attribute_mode);
163 previous_attr &= ~A_COLOR;
166 } else if (set_attributes && !used_ncv) {
167 if (turn_on || turn_off) {
168 TPUTS_TRACE("set_attributes");
169 tputs(tparm(set_attributes,
170 (newmode & A_STANDOUT) != 0,
171 (newmode & A_UNDERLINE) != 0,
172 (newmode & A_REVERSE) != 0,
173 (newmode & A_BLINK) != 0,
174 (newmode & A_DIM) != 0,
175 (newmode & A_BOLD) != 0,
176 (newmode & A_INVIS) != 0,
177 (newmode & A_PROTECT) != 0,
178 (newmode & A_ALTCHARSET) != 0), 1, outc);
179 previous_attr &= ~A_COLOR;
183 T(("turning %s off", _traceattr(turn_off)));
185 TurnOff(A_ALTCHARSET, exit_alt_charset_mode);
186 TurnOff(A_UNDERLINE, exit_underline_mode);
187 TurnOff(A_STANDOUT, exit_standout_mode);
189 if (turn_off && exit_attribute_mode) {
190 doPut(exit_attribute_mode);
191 turn_on |= (newmode & (chtype)(~A_COLOR));
192 previous_attr &= ~A_COLOR;
195 T(("turning %s on", _traceattr(turn_on)));
197 TurnOn (A_ALTCHARSET, enter_alt_charset_mode);
198 TurnOn (A_BLINK, enter_blink_mode);
199 TurnOn (A_BOLD, enter_bold_mode);
200 TurnOn (A_DIM, enter_dim_mode);
201 TurnOn (A_REVERSE, enter_reverse_mode);
202 TurnOn (A_STANDOUT, enter_standout_mode);
203 TurnOn (A_PROTECT, enter_protected_mode);
204 TurnOn (A_INVIS, enter_secure_mode);
205 TurnOn (A_UNDERLINE, enter_underline_mode);
206 TurnOn (A_HORIZONTAL, enter_horizontal_hl_mode);
207 TurnOn (A_LEFT, enter_left_hl_mode);
208 TurnOn (A_LOW, enter_low_hl_mode);
209 TurnOn (A_RIGHT, enter_right_hl_mode);
210 TurnOn (A_TOP, enter_top_hl_mode);
211 TurnOn (A_VERTICAL, enter_vertical_hl_mode);
214 /* if there is no current screen, assume we *can* do color */
215 if ((!SP || SP->_coloron) && pair != 0) {
216 current_pair = PAIR_NUMBER(previous_attr);
217 T(("old pair = %d -- new pair = %d", current_pair, pair));
218 if (pair != current_pair) {
219 _nc_do_color(pair, reverse, outc);
224 newmode |= A_REVERSE;
227 SP->_current_attr = newmode;
229 previous_attr = newmode;
234 int vidattr(attr_t newmode)
236 T((T_CALLED("vidattr(%s)"), _traceattr(newmode)));
238 returnCode(vidputs(newmode, _nc_outch));
241 chtype termattrs(void)
243 chtype attrs = A_NORMAL;
245 if (enter_alt_charset_mode)
246 attrs |= A_ALTCHARSET;
248 if (enter_blink_mode)
257 if (enter_reverse_mode)
260 if (enter_standout_mode)
263 if (enter_protected_mode)
266 if (enter_secure_mode)
269 if (enter_underline_mode)
270 attrs |= A_UNDERLINE;