1 /****************************************************************************
2  * Copyright 2018-2019,2020 Thomas E. Dickey                                *
3  * Copyright 2002-2014,2017 Free Software Foundation, Inc.                  *
4  *                                                                          *
5  * Permission is hereby granted, free of charge, to any person obtaining a  *
6  * copy of this software and associated documentation files (the            *
7  * "Software"), to deal in the Software without restriction, including      *
8  * without limitation the rights to use, copy, modify, merge, publish,      *
9  * distribute, distribute with modifications, sublicense, and/or sell       *
10  * copies of the Software, and to permit persons to whom the Software is    *
11  * furnished to do so, subject to the following conditions:                 *
12  *                                                                          *
13  * The above copyright notice and this permission notice shall be included  *
14  * in all copies or substantial portions of the Software.                   *
15  *                                                                          *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
19  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
21  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
23  *                                                                          *
24  * Except as contained in this notice, the name(s) of the above copyright   *
25  * holders shall not be used in advertising or otherwise to promote the     *
26  * sale, use or other dealings in this Software without prior written       *
27  * authorization.                                                           *
28  ****************************************************************************/
29 
30 /****************************************************************************
31  *  Author: Thomas E. Dickey                                                *
32  ****************************************************************************/
33 
34 #include <curses.priv.h>
35 
36 #ifndef CUR
37 #define CUR SP_TERMTYPE
38 #endif
39 
40 MODULE_ID("$Id: lib_vid_attr.c,v 1.28 2020/02/02 23:34:34 tom Exp $")
41 
42 #define doPut(mode) \
43 	TPUTS_TRACE(#mode); \
44 	NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx mode, 1, outc)
45 
46 #define TurnOn(mask, mode) \
47 	if ((turn_on & mask) && mode) { \
48 	    TPUTS_TRACE(#mode); \
49 	    NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx mode, 1, outc); \
50 	}
51 
52 #define TurnOff(mask, mode) \
53 	if ((turn_off & mask) && mode) { \
54 	    TPUTS_TRACE(#mode); \
55 	    NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx mode, 1, outc); \
56 	    turn_off &= ~mask; \
57 	}
58 
59 	/* if there is no current screen, assume we *can* do color */
60 #define SetColorsIf(why, old_attr, old_pair) \
61 	if (can_color && (why)) { \
62 		TR(TRACE_ATTRS, ("old pair = %d -- new pair = %d", old_pair, color_pair)); \
63 		if ((color_pair != old_pair) \
64 		 || (fix_pair0 && (color_pair == 0)) \
65 		 || (reverse ^ ((old_attr & A_REVERSE) != 0))) { \
66 		    NCURSES_SP_NAME(_nc_do_color) (NCURSES_SP_ARGx \
67 						   old_pair, color_pair, \
68 						   reverse, outc); \
69 		} \
70 	}
71 
72 #define set_color(mode, pair) \
73 	mode &= ALL_BUT_COLOR; \
74 	mode |= (attr_t) ColorPair(pair)
75 
76 NCURSES_EXPORT(int)
77 NCURSES_SP_NAME(vid_puts) (NCURSES_SP_DCLx
78 			   attr_t newmode,
79 			   NCURSES_PAIRS_T pair_arg,
80 			   void *opts OPTIONAL_PAIR,
81 			   NCURSES_SP_OUTC outc)
82 {
83     int color_pair = pair_arg;
84 #if NCURSES_EXT_COLORS
85     static attr_t previous_attr = A_NORMAL;
86     static int previous_pair = 0;
87 
88     attr_t turn_on, turn_off;
89     bool reverse = FALSE;
90     bool can_color = (SP_PARM == 0 || SP_PARM->_coloron);
91 #if NCURSES_EXT_FUNCS
92     bool fix_pair0 = (SP_PARM != 0 && SP_PARM->_coloron && !SP_PARM->_default_color);
93 #else
94 #define fix_pair0 FALSE
95 #endif
96 
97     if (!IsValidTIScreen(SP_PARM))
98 	returnCode(ERR);
99 
100     newmode &= A_ATTRIBUTES;
101     set_extended_pair(opts, color_pair);
102     T((T_CALLED("vid_puts(%s,%d)"), _traceattr(newmode), color_pair));
103 
104     /* this allows us to go on whether or not newterm() has been called */
105     if (SP_PARM) {
106 	previous_attr = AttrOf(SCREEN_ATTRS(SP_PARM));
107 	previous_pair = GetPair(SCREEN_ATTRS(SP_PARM));
108     }
109 
110     TR(TRACE_ATTRS, ("previous attribute was %s, %d",
111 		     _traceattr(previous_attr), previous_pair));
112 
113 #if !USE_XMC_SUPPORT
114     if ((SP_PARM != 0)
115 	&& (magic_cookie_glitch > 0))
116 	newmode &= ~(SP_PARM->_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 ((color_pair != 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 = (unsigned) 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 	&& color_pair == previous_pair)
153 	returnCode(OK);
154 
155     if (reverse) {
156 	newmode &= ~A_REVERSE;
157     }
158 
159     turn_off = (~newmode & previous_attr) & ALL_BUT_COLOR;
160     turn_on = (newmode & ~(previous_attr & TPARM_ATTR)) & ALL_BUT_COLOR;
161 
162     SetColorsIf(((color_pair == 0) && !fix_pair0), previous_attr, previous_pair);
163 
164     if (newmode == A_NORMAL) {
165 	if ((previous_attr & A_ALTCHARSET) && exit_alt_charset_mode) {
166 	    doPut(exit_alt_charset_mode);
167 	    previous_attr &= ~A_ALTCHARSET;
168 	}
169 	if (previous_attr) {
170 	    if (exit_attribute_mode) {
171 		doPut(exit_attribute_mode);
172 	    } else {
173 		if (!SP_PARM || SP_PARM->_use_rmul) {
174 		    TurnOff(A_UNDERLINE, exit_underline_mode);
175 		}
176 		if (!SP_PARM || SP_PARM->_use_rmso) {
177 		    TurnOff(A_STANDOUT, exit_standout_mode);
178 		}
179 #if USE_ITALIC
180 		if (!SP_PARM || SP_PARM->_use_ritm) {
181 		    TurnOff(A_ITALIC, exit_italics_mode);
182 		}
183 #endif
184 	    }
185 	    previous_attr &= ALL_BUT_COLOR;
186 	    previous_pair = 0;
187 	}
188 
189 	SetColorsIf((color_pair != 0) || fix_pair0, previous_attr, previous_pair);
190     } else if (set_attributes) {
191 	if (turn_on || turn_off) {
192 	    TPUTS_TRACE("set_attributes");
193 	    NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
194 				    TPARM_9(set_attributes,
195 					    (newmode & A_STANDOUT) != 0,
196 					    (newmode & A_UNDERLINE) != 0,
197 					    (newmode & A_REVERSE) != 0,
198 					    (newmode & A_BLINK) != 0,
199 					    (newmode & A_DIM) != 0,
200 					    (newmode & A_BOLD) != 0,
201 					    (newmode & A_INVIS) != 0,
202 					    (newmode & A_PROTECT) != 0,
203 					    (newmode & A_ALTCHARSET) != 0),
204 				    1, outc);
205 	    previous_attr &= ALL_BUT_COLOR;
206 	    previous_pair = 0;
207 	}
208 #if USE_ITALIC
209 	if (!SP_PARM || SP_PARM->_use_ritm) {
210 	    if (turn_on & A_ITALIC) {
211 		TurnOn(A_ITALIC, enter_italics_mode);
212 	    } else if (turn_off & A_ITALIC) {
213 		TurnOff(A_ITALIC, exit_italics_mode);
214 	    }
215 	}
216 #endif
217 	SetColorsIf((color_pair != 0) || fix_pair0, previous_attr, previous_pair);
218     } else {
219 
220 	TR(TRACE_ATTRS, ("turning %s off", _traceattr(turn_off)));
221 
222 	TurnOff(A_ALTCHARSET, exit_alt_charset_mode);
223 
224 	if (!SP_PARM || SP_PARM->_use_rmul) {
225 	    TurnOff(A_UNDERLINE, exit_underline_mode);
226 	}
227 
228 	if (!SP_PARM || SP_PARM->_use_rmso) {
229 	    TurnOff(A_STANDOUT, exit_standout_mode);
230 	}
231 #if USE_ITALIC
232 	if (!SP_PARM || SP_PARM->_use_ritm) {
233 	    TurnOff(A_ITALIC, exit_italics_mode);
234 	}
235 #endif
236 	if (turn_off && exit_attribute_mode) {
237 	    doPut(exit_attribute_mode);
238 	    turn_on |= (newmode & ALL_BUT_COLOR);
239 	    previous_attr &= ALL_BUT_COLOR;
240 	    previous_pair = 0;
241 	}
242 	SetColorsIf((color_pair != 0) || fix_pair0, previous_attr, previous_pair);
243 
244 	TR(TRACE_ATTRS, ("turning %s on", _traceattr(turn_on)));
245 	/* *INDENT-OFF* */
246 	TurnOn(A_ALTCHARSET,	enter_alt_charset_mode);
247 	TurnOn(A_BLINK,		enter_blink_mode);
248 	TurnOn(A_BOLD,		enter_bold_mode);
249 	TurnOn(A_DIM,		enter_dim_mode);
250 	TurnOn(A_REVERSE,	enter_reverse_mode);
251 	TurnOn(A_STANDOUT,	enter_standout_mode);
252 	TurnOn(A_PROTECT,	enter_protected_mode);
253 	TurnOn(A_INVIS,		enter_secure_mode);
254 	TurnOn(A_UNDERLINE,	enter_underline_mode);
255 #if USE_ITALIC
256 	TurnOn(A_ITALIC,	enter_italics_mode);
257 #endif
258 #if USE_WIDEC_SUPPORT && defined(enter_horizontal_hl_mode)
259 	TurnOn(A_HORIZONTAL,	enter_horizontal_hl_mode);
260 	TurnOn(A_LEFT,		enter_left_hl_mode);
261 	TurnOn(A_LOW,		enter_low_hl_mode);
262 	TurnOn(A_RIGHT,		enter_right_hl_mode);
263 	TurnOn(A_TOP,		enter_top_hl_mode);
264 	TurnOn(A_VERTICAL,	enter_vertical_hl_mode);
265 #endif
266 	/* *INDENT-ON* */
267 
268     }
269 
270     if (reverse)
271 	newmode |= A_REVERSE;
272 
273     if (SP_PARM) {
274 	SetAttr(SCREEN_ATTRS(SP_PARM), newmode);
275 	SetPair(SCREEN_ATTRS(SP_PARM), color_pair);
276     } else {
277 	previous_attr = newmode;
278 	previous_pair = color_pair;
279     }
280 
281     returnCode(OK);
282 #else
283     T((T_CALLED("vid_puts(%s,%d)"), _traceattr(newmode), color_pair));
284     (void) opts;
285     set_color(newmode, color_pair);
286     returnCode(NCURSES_SP_NAME(vidputs) (NCURSES_SP_ARGx newmode, outc));
287 #endif
288 }
289 
290 #if NCURSES_SP_FUNCS
291 NCURSES_EXPORT(int)
292 vid_puts(attr_t newmode,
293 	 NCURSES_PAIRS_T pair_arg,
294 	 void *opts GCC_UNUSED,
295 	 NCURSES_OUTC outc)
296 {
297     SetSafeOutcWrapper(outc);
298     return NCURSES_SP_NAME(vid_puts) (CURRENT_SCREEN,
299 				      newmode,
300 				      pair_arg,
301 				      opts,
302 				      _nc_outc_wrapper);
303 }
304 #endif
305 
306 #undef vid_attr
307 NCURSES_EXPORT(int)
308 NCURSES_SP_NAME(vid_attr) (NCURSES_SP_DCLx
309 			   attr_t newmode,
310 			   NCURSES_PAIRS_T pair_arg,
311 			   void *opts)
312 {
313     T((T_CALLED("vid_attr(%s,%d)"), _traceattr(newmode), (int) pair_arg));
314     returnCode(NCURSES_SP_NAME(vid_puts) (NCURSES_SP_ARGx
315 					  newmode,
316 					  pair_arg,
317 					  opts,
318 					  NCURSES_SP_NAME(_nc_putchar)));
319 }
320 
321 #if NCURSES_SP_FUNCS
322 NCURSES_EXPORT(int)
323 vid_attr(attr_t newmode, NCURSES_PAIRS_T pair_arg, void *opts)
324 {
325     return NCURSES_SP_NAME(vid_attr) (CURRENT_SCREEN, newmode, pair_arg, opts);
326 }
327 #endif
328 
329 /*
330  * This implementation uses the same mask values for A_xxx and WA_xxx, so
331  * we can use termattrs() for part of the logic.
332  */
333 NCURSES_EXPORT(attr_t)
334 NCURSES_SP_NAME(term_attrs) (NCURSES_SP_DCL0)
335 {
336     attr_t attrs = 0;
337 
338     T((T_CALLED("term_attrs()")));
339     if (SP_PARM) {
340 	attrs = NCURSES_SP_NAME(termattrs) (NCURSES_SP_ARG);
341 
342 #if USE_WIDEC_SUPPORT && defined(enter_horizontal_hl_mode)
343 	/* these are only supported for wide-character mode */
344 	if (enter_horizontal_hl_mode)
345 	    attrs |= WA_HORIZONTAL;
346 	if (enter_left_hl_mode)
347 	    attrs |= WA_LEFT;
348 	if (enter_low_hl_mode)
349 	    attrs |= WA_LOW;
350 	if (enter_right_hl_mode)
351 	    attrs |= WA_RIGHT;
352 	if (enter_top_hl_mode)
353 	    attrs |= WA_TOP;
354 	if (enter_vertical_hl_mode)
355 	    attrs |= WA_VERTICAL;
356 #endif
357     }
358 
359     returnAttr(attrs);
360 }
361 
362 #if NCURSES_SP_FUNCS
363 NCURSES_EXPORT(attr_t)
364 term_attrs(void)
365 {
366     return NCURSES_SP_NAME(term_attrs) (CURRENT_SCREEN);
367 }
368 #endif
369