xref: /freebsd/contrib/ncurses/ncurses/tty/lib_vidattr.c (revision c17d43407fe04133a94055b0dbc7ea8965654a9f)
1 /****************************************************************************
2  * Copyright (c) 1998,1999,2000 Free Software Foundation, Inc.              *
3  *                                                                          *
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:                 *
11  *                                                                          *
12  * The above copyright notice and this permission notice shall be included  *
13  * in all copies or substantial portions of the Software.                   *
14  *                                                                          *
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.                               *
22  *                                                                          *
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       *
26  * authorization.                                                           *
27  ****************************************************************************/
28 
29 /****************************************************************************
30  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
31  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
32  ****************************************************************************/
33 
34 /*
35  *	vidputs(newmode, outc)
36  *
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.
40  *
41  *	if the new attribute is normal
42  *		if exit-alt-char-set exists
43  *			emit it
44  *		emit exit-attribute-mode
45  *	else if set-attributes exists
46  *		use it to set exactly what you want
47  *	else
48  *		if exit-attribute-mode exists
49  *			turn off everything
50  *		else
51  *			turn off those which can be turned off and aren't in
52  *			newmode.
53  *		turn on each mode which should be on and isn't, one by one
54  *
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...
58  *
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.
62  */
63 
64 #include <curses.priv.h>
65 #include <term.h>
66 
67 MODULE_ID("$Id: lib_vidattr.c,v 1.36 2000/12/10 03:05:48 tom Exp $")
68 
69 #define doPut(mode) TPUTS_TRACE(#mode); tputs(mode, 1, outc)
70 
71 #define TurnOn(mask,mode) \
72 	if ((turn_on & mask) && mode) { doPut(mode); }
73 
74 #define TurnOff(mask,mode) \
75 	if ((turn_off & mask) && mode) { doPut(mode); turn_off &= ~mask; }
76 
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 		TR(TRACE_ATTRS, ("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); \
86 		} \
87 	}
88 
89 NCURSES_EXPORT(int)
90 vidputs
91 (attr_t newmode, int (*outc) (int))
92 {
93     static attr_t previous_attr = A_NORMAL;
94     attr_t turn_on, turn_off;
95     int pair;
96     bool reverse = FALSE;
97     bool used_ncv = FALSE;
98     bool can_color = (SP == 0 || SP->_coloron);
99 #if NCURSES_EXT_FUNCS
100     bool fix_pair0 = (SP != 0 && SP->_coloron && !SP->_default_color);
101 #else
102 #define fix_pair0 FALSE
103 #endif
104 
105     T((T_CALLED("vidputs(%s)"), _traceattr(newmode)));
106 
107     /* this allows us to go on whether or not newterm() has been called */
108     if (SP)
109 	previous_attr = SP->_current_attr;
110 
111     TR(TRACE_ATTRS, ("previous attribute was %s", _traceattr(previous_attr)));
112 
113 #if !USE_XMC_SUPPORT
114     if ((SP != 0)
115 	&& (magic_cookie_glitch > 0))
116 	newmode &= ~(SP->_xmc_suppress);
117 #endif
118 
119     /*
120      * If we have a terminal that cannot combine color with video
121      * attributes, use the colors in preference.
122      */
123     if (((newmode & A_COLOR) != 0
124 	 || fix_pair0)
125 	&& (no_color_video > 0)) {
126 	/*
127 	 * If we had chosen the A_xxx definitions to correspond to the
128 	 * no_color_video mask, we could simply shift it up and mask off the
129 	 * attributes.  But we did not (actually copied Solaris' definitions).
130 	 * However, this is still simpler/faster than a lookup table.
131 	 *
132 	 * The 63 corresponds to A_STANDOUT, A_UNDERLINE, A_REVERSE, A_BLINK,
133 	 * A_DIM, A_BOLD which are 1:1 with no_color_video.  The bits that
134 	 * correspond to A_INVIS, A_PROTECT (192) must be shifted up 1 and
135 	 * A_ALTCHARSET (256) down 2 to line up.  We use the NCURSES_BITS
136 	 * macro so this will work properly for the wide-character layout.
137 	 */
138 	unsigned value = no_color_video;
139 	attr_t mask = NCURSES_BITS((value & 63)
140 				   | ((value & 192) << 1)
141 				   | ((value & 256) >> 2), 8);
142 
143 	if ((mask & A_REVERSE) != 0
144 	    && (newmode & A_REVERSE) != 0) {
145 	    reverse = TRUE;
146 	    mask &= ~A_REVERSE;
147 	}
148 	newmode &= ~mask;
149     }
150 
151     if (newmode == previous_attr)
152 	returnCode(OK);
153 
154     pair = PAIR_NUMBER(newmode);
155 
156     if (reverse) {
157 	newmode &= ~A_REVERSE;
158     }
159 
160     turn_off = (~newmode & previous_attr) & ALL_BUT_COLOR;
161     turn_on = (newmode & ~previous_attr) & ALL_BUT_COLOR;
162 
163     SetColorsIf(((pair == 0) && !fix_pair0), previous_attr);
164 
165     if (newmode == A_NORMAL) {
166 	if ((previous_attr & A_ALTCHARSET) && exit_alt_charset_mode) {
167 	    doPut(exit_alt_charset_mode);
168 	    previous_attr &= ~A_ALTCHARSET;
169 	}
170 	if (previous_attr) {
171 	    if (exit_attribute_mode) {
172 		doPut(exit_attribute_mode);
173 	    } else {
174 		if (!SP || SP->_use_rmul) {
175 		    TurnOff(A_UNDERLINE, exit_underline_mode);
176 		}
177 		if (!SP || SP->_use_rmso) {
178 		    TurnOff(A_STANDOUT, exit_standout_mode);
179 		}
180 	    }
181 	    previous_attr &= ~A_COLOR;
182 	}
183 
184 	SetColorsIf((pair != 0) || fix_pair0, previous_attr);
185     } else if (set_attributes && !used_ncv) {
186 	if (turn_on || turn_off) {
187 	    TPUTS_TRACE("set_attributes");
188 	    tputs(tparm(set_attributes,
189 			(newmode & A_STANDOUT) != 0,
190 			(newmode & A_UNDERLINE) != 0,
191 			(newmode & A_REVERSE) != 0,
192 			(newmode & A_BLINK) != 0,
193 			(newmode & A_DIM) != 0,
194 			(newmode & A_BOLD) != 0,
195 			(newmode & A_INVIS) != 0,
196 			(newmode & A_PROTECT) != 0,
197 			(newmode & A_ALTCHARSET) != 0), 1, outc);
198 	    previous_attr &= ~A_COLOR;
199 	}
200 	SetColorsIf((pair != 0) || fix_pair0, previous_attr);
201     } else {
202 
203 	TR(TRACE_ATTRS, ("turning %s off", _traceattr(turn_off)));
204 
205 	TurnOff(A_ALTCHARSET, exit_alt_charset_mode);
206 
207 	if (!SP || SP->_use_rmul) {
208 	    TurnOff(A_UNDERLINE, exit_underline_mode);
209 	}
210 
211 	if (!SP || SP->_use_rmso) {
212 	    TurnOff(A_STANDOUT, exit_standout_mode);
213 	}
214 
215 	if (turn_off && exit_attribute_mode) {
216 	    doPut(exit_attribute_mode);
217 	    turn_on |= (newmode & (chtype) (~A_COLOR));
218 	    previous_attr &= ~A_COLOR;
219 	}
220 	SetColorsIf((pair != 0) || fix_pair0, previous_attr);
221 
222 	TR(TRACE_ATTRS, ("turning %s on", _traceattr(turn_on)));
223 	/* *INDENT-OFF* */
224 	TurnOn(A_ALTCHARSET,	enter_alt_charset_mode);
225 	TurnOn(A_BLINK,		enter_blink_mode);
226 	TurnOn(A_BOLD,		enter_bold_mode);
227 	TurnOn(A_DIM,		enter_dim_mode);
228 	TurnOn(A_REVERSE,	enter_reverse_mode);
229 	TurnOn(A_STANDOUT,	enter_standout_mode);
230 	TurnOn(A_PROTECT,	enter_protected_mode);
231 	TurnOn(A_INVIS,		enter_secure_mode);
232 	TurnOn(A_UNDERLINE,	enter_underline_mode);
233 	TurnOn(A_HORIZONTAL,	enter_horizontal_hl_mode);
234 	TurnOn(A_LEFT,		enter_left_hl_mode);
235 	TurnOn(A_LOW,		enter_low_hl_mode);
236 	TurnOn(A_RIGHT,		enter_right_hl_mode);
237 	TurnOn(A_TOP,		enter_top_hl_mode);
238 	TurnOn(A_VERTICAL,	enter_vertical_hl_mode);
239 	/* *INDENT-ON* */
240 
241     }
242 
243     if (reverse)
244 	newmode |= A_REVERSE;
245 
246     if (SP)
247 	SP->_current_attr = newmode;
248     else
249 	previous_attr = newmode;
250 
251     returnCode(OK);
252 }
253 
254 NCURSES_EXPORT(int)
255 vidattr(attr_t newmode)
256 {
257     T((T_CALLED("vidattr(%s)"), _traceattr(newmode)));
258 
259     returnCode(vidputs(newmode, _nc_outch));
260 }
261 
262 NCURSES_EXPORT(chtype)
263 termattrs(void)
264 {
265     chtype attrs = A_NORMAL;
266 
267     T((T_CALLED("termattrs()")));
268     if (enter_alt_charset_mode)
269 	attrs |= A_ALTCHARSET;
270 
271     if (enter_blink_mode)
272 	attrs |= A_BLINK;
273 
274     if (enter_bold_mode)
275 	attrs |= A_BOLD;
276 
277     if (enter_dim_mode)
278 	attrs |= A_DIM;
279 
280     if (enter_reverse_mode)
281 	attrs |= A_REVERSE;
282 
283     if (enter_standout_mode)
284 	attrs |= A_STANDOUT;
285 
286     if (enter_protected_mode)
287 	attrs |= A_PROTECT;
288 
289     if (enter_secure_mode)
290 	attrs |= A_INVIS;
291 
292     if (enter_underline_mode)
293 	attrs |= A_UNDERLINE;
294 
295     if (SP->_coloron)
296 	attrs |= A_COLOR;
297 
298     returnChar(attrs);
299 }
300