xref: /openbsd-src/lib/libcurses/tty/lib_vidattr.c (revision 43003dfe3ad45d1698bed8a37f2b0f5b14f20d4f)
1 /*	$OpenBSD: lib_vidattr.c,v 1.8 2001/01/22 18:02:00 millert Exp $	*/
2 
3 /****************************************************************************
4  * Copyright (c) 1998,1999,2000 Free Software Foundation, Inc.              *
5  *                                                                          *
6  * Permission is hereby granted, free of charge, to any person obtaining a  *
7  * copy of this software and associated documentation files (the            *
8  * "Software"), to deal in the Software without restriction, including      *
9  * without limitation the rights to use, copy, modify, merge, publish,      *
10  * distribute, distribute with modifications, sublicense, and/or sell       *
11  * copies of the Software, and to permit persons to whom the Software is    *
12  * furnished to do so, subject to the following conditions:                 *
13  *                                                                          *
14  * The above copyright notice and this permission notice shall be included  *
15  * in all copies or substantial portions of the Software.                   *
16  *                                                                          *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
20  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
21  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
22  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
23  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
24  *                                                                          *
25  * Except as contained in this notice, the name(s) of the above copyright   *
26  * holders shall not be used in advertising or otherwise to promote the     *
27  * sale, use or other dealings in this Software without prior written       *
28  * authorization.                                                           *
29  ****************************************************************************/
30 
31 /****************************************************************************
32  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
33  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
34  ****************************************************************************/
35 
36 /*
37  *	vidputs(newmode, outc)
38  *
39  *	newmode is taken to be the logical 'or' of the symbols in curses.h
40  *	representing graphic renditions.  The terminal is set to be in all of
41  *	the given modes, if possible.
42  *
43  *	if the new attribute is normal
44  *		if exit-alt-char-set exists
45  *			emit it
46  *		emit exit-attribute-mode
47  *	else if set-attributes exists
48  *		use it to set exactly what you want
49  *	else
50  *		if exit-attribute-mode exists
51  *			turn off everything
52  *		else
53  *			turn off those which can be turned off and aren't in
54  *			newmode.
55  *		turn on each mode which should be on and isn't, one by one
56  *
57  *	NOTE that this algorithm won't achieve the desired mix of attributes
58  *	in some cases, but those are probably just those cases in which it is
59  *	actually impossible, anyway, so...
60  *
61  * 	NOTE that we cannot assume that there's no interaction between color
62  *	and other attribute resets.  So each time we reset color (or other
63  *	attributes) we'll have to be prepared to restore the other.
64  */
65 
66 #include <curses.priv.h>
67 #include <term.h>
68 
69 MODULE_ID("$From: lib_vidattr.c,v 1.36 2000/12/10 03:05:48 tom Exp $")
70 
71 #define doPut(mode) TPUTS_TRACE(#mode); tputs(mode, 1, outc)
72 
73 #define TurnOn(mask,mode) \
74 	if ((turn_on & mask) && mode) { doPut(mode); }
75 
76 #define TurnOff(mask,mode) \
77 	if ((turn_off & mask) && mode) { doPut(mode); turn_off &= ~mask; }
78 
79 	/* if there is no current screen, assume we *can* do color */
80 #define SetColorsIf(why,old_attr) \
81 	if (can_color && (why)) { \
82 		int old_pair = PAIR_NUMBER(old_attr); \
83 		TR(TRACE_ATTRS, ("old pair = %d -- new pair = %d", old_pair, pair)); \
84 		if ((pair != old_pair) \
85 		 || (fix_pair0 && (pair == 0)) \
86 		 || (reverse ^ ((old_attr & A_REVERSE) != 0))) { \
87 			_nc_do_color(old_pair, pair, reverse, outc); \
88 		} \
89 	}
90 
91 NCURSES_EXPORT(int)
92 vidputs
93 (attr_t newmode, int (*outc) (int))
94 {
95     static attr_t previous_attr = A_NORMAL;
96     attr_t turn_on, turn_off;
97     int pair;
98     bool reverse = FALSE;
99     bool used_ncv = FALSE;
100     bool can_color = (SP == 0 || SP->_coloron);
101 #if NCURSES_EXT_FUNCS
102     bool fix_pair0 = (SP != 0 && SP->_coloron && !SP->_default_color);
103 #else
104 #define fix_pair0 FALSE
105 #endif
106 
107     T((T_CALLED("vidputs(%s)"), _traceattr(newmode)));
108 
109     /* this allows us to go on whether or not newterm() has been called */
110     if (SP)
111 	previous_attr = SP->_current_attr;
112 
113     TR(TRACE_ATTRS, ("previous attribute was %s", _traceattr(previous_attr)));
114 
115 #if !USE_XMC_SUPPORT
116     if ((SP != 0)
117 	&& (magic_cookie_glitch > 0))
118 	newmode &= ~(SP->_xmc_suppress);
119 #endif
120 
121     /*
122      * If we have a terminal that cannot combine color with video
123      * attributes, use the colors in preference.
124      */
125     if (((newmode & A_COLOR) != 0
126 	 || fix_pair0)
127 	&& (no_color_video > 0)) {
128 	/*
129 	 * If we had chosen the A_xxx definitions to correspond to the
130 	 * no_color_video mask, we could simply shift it up and mask off the
131 	 * attributes.  But we did not (actually copied Solaris' definitions).
132 	 * However, this is still simpler/faster than a lookup table.
133 	 *
134 	 * The 63 corresponds to A_STANDOUT, A_UNDERLINE, A_REVERSE, A_BLINK,
135 	 * A_DIM, A_BOLD which are 1:1 with no_color_video.  The bits that
136 	 * correspond to A_INVIS, A_PROTECT (192) must be shifted up 1 and
137 	 * A_ALTCHARSET (256) down 2 to line up.  We use the NCURSES_BITS
138 	 * macro so this will work properly for the wide-character layout.
139 	 */
140 	unsigned value = no_color_video;
141 	attr_t mask = NCURSES_BITS((value & 63)
142 				   | ((value & 192) << 1)
143 				   | ((value & 256) >> 2), 8);
144 
145 	if ((mask & A_REVERSE) != 0
146 	    && (newmode & A_REVERSE) != 0) {
147 	    reverse = TRUE;
148 	    mask &= ~A_REVERSE;
149 	}
150 	newmode &= ~mask;
151     }
152 
153     if (newmode == previous_attr)
154 	returnCode(OK);
155 
156     pair = PAIR_NUMBER(newmode);
157 
158     if (reverse) {
159 	newmode &= ~A_REVERSE;
160     }
161 
162     turn_off = (~newmode & previous_attr) & ALL_BUT_COLOR;
163     turn_on = (newmode & ~previous_attr) & ALL_BUT_COLOR;
164 
165     SetColorsIf(((pair == 0) && !fix_pair0), previous_attr);
166 
167     if (newmode == A_NORMAL) {
168 	if ((previous_attr & A_ALTCHARSET) && exit_alt_charset_mode) {
169 	    doPut(exit_alt_charset_mode);
170 	    previous_attr &= ~A_ALTCHARSET;
171 	}
172 	if (previous_attr) {
173 	    if (exit_attribute_mode) {
174 		doPut(exit_attribute_mode);
175 	    } else {
176 		if (!SP || SP->_use_rmul) {
177 		    TurnOff(A_UNDERLINE, exit_underline_mode);
178 		}
179 		if (!SP || SP->_use_rmso) {
180 		    TurnOff(A_STANDOUT, exit_standout_mode);
181 		}
182 	    }
183 	    previous_attr &= ~A_COLOR;
184 	}
185 
186 	SetColorsIf((pair != 0) || fix_pair0, previous_attr);
187     } else if (set_attributes && !used_ncv) {
188 	if (turn_on || turn_off) {
189 	    TPUTS_TRACE("set_attributes");
190 	    tputs(tparm(set_attributes,
191 			(newmode & A_STANDOUT) != 0,
192 			(newmode & A_UNDERLINE) != 0,
193 			(newmode & A_REVERSE) != 0,
194 			(newmode & A_BLINK) != 0,
195 			(newmode & A_DIM) != 0,
196 			(newmode & A_BOLD) != 0,
197 			(newmode & A_INVIS) != 0,
198 			(newmode & A_PROTECT) != 0,
199 			(newmode & A_ALTCHARSET) != 0), 1, outc);
200 	    previous_attr &= ~A_COLOR;
201 	}
202 	SetColorsIf((pair != 0) || fix_pair0, previous_attr);
203     } else {
204 
205 	TR(TRACE_ATTRS, ("turning %s off", _traceattr(turn_off)));
206 
207 	TurnOff(A_ALTCHARSET, exit_alt_charset_mode);
208 
209 	if (!SP || SP->_use_rmul) {
210 	    TurnOff(A_UNDERLINE, exit_underline_mode);
211 	}
212 
213 	if (!SP || SP->_use_rmso) {
214 	    TurnOff(A_STANDOUT, exit_standout_mode);
215 	}
216 
217 	if (turn_off && exit_attribute_mode) {
218 	    doPut(exit_attribute_mode);
219 	    turn_on |= (newmode & (chtype) (~A_COLOR));
220 	    previous_attr &= ~A_COLOR;
221 	}
222 	SetColorsIf((pair != 0) || fix_pair0, previous_attr);
223 
224 	TR(TRACE_ATTRS, ("turning %s on", _traceattr(turn_on)));
225 	/* *INDENT-OFF* */
226 	TurnOn(A_ALTCHARSET,	enter_alt_charset_mode);
227 	TurnOn(A_BLINK,		enter_blink_mode);
228 	TurnOn(A_BOLD,		enter_bold_mode);
229 	TurnOn(A_DIM,		enter_dim_mode);
230 	TurnOn(A_REVERSE,	enter_reverse_mode);
231 	TurnOn(A_STANDOUT,	enter_standout_mode);
232 	TurnOn(A_PROTECT,	enter_protected_mode);
233 	TurnOn(A_INVIS,		enter_secure_mode);
234 	TurnOn(A_UNDERLINE,	enter_underline_mode);
235 	TurnOn(A_HORIZONTAL,	enter_horizontal_hl_mode);
236 	TurnOn(A_LEFT,		enter_left_hl_mode);
237 	TurnOn(A_LOW,		enter_low_hl_mode);
238 	TurnOn(A_RIGHT,		enter_right_hl_mode);
239 	TurnOn(A_TOP,		enter_top_hl_mode);
240 	TurnOn(A_VERTICAL,	enter_vertical_hl_mode);
241 	/* *INDENT-ON* */
242 
243     }
244 
245     if (reverse)
246 	newmode |= A_REVERSE;
247 
248     if (SP)
249 	SP->_current_attr = newmode;
250     else
251 	previous_attr = newmode;
252 
253     returnCode(OK);
254 }
255 
256 NCURSES_EXPORT(int)
257 vidattr(attr_t newmode)
258 {
259     T((T_CALLED("vidattr(%s)"), _traceattr(newmode)));
260 
261     returnCode(vidputs(newmode, _nc_outch));
262 }
263 
264 NCURSES_EXPORT(chtype)
265 termattrs(void)
266 {
267     chtype attrs = A_NORMAL;
268 
269     T((T_CALLED("termattrs()")));
270     if (enter_alt_charset_mode)
271 	attrs |= A_ALTCHARSET;
272 
273     if (enter_blink_mode)
274 	attrs |= A_BLINK;
275 
276     if (enter_bold_mode)
277 	attrs |= A_BOLD;
278 
279     if (enter_dim_mode)
280 	attrs |= A_DIM;
281 
282     if (enter_reverse_mode)
283 	attrs |= A_REVERSE;
284 
285     if (enter_standout_mode)
286 	attrs |= A_STANDOUT;
287 
288     if (enter_protected_mode)
289 	attrs |= A_PROTECT;
290 
291     if (enter_secure_mode)
292 	attrs |= A_INVIS;
293 
294     if (enter_underline_mode)
295 	attrs |= A_UNDERLINE;
296 
297     if (SP->_coloron)
298 	attrs |= A_COLOR;
299 
300     returnChar(attrs);
301 }
302