1 /****************************************************************************
2 * Copyright (c) 1998-2018,2019 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 * and: Thomas E. Dickey 1996-on *
33 * and: Juergen Pfeifer 2009 *
34 ****************************************************************************/
37 * vidputs(newmode, outc)
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.
43 * if the new attribute is normal
44 * if exit-alt-char-set exists
46 * emit exit-attribute-mode
47 * else if set-attributes exists
48 * use it to set exactly what you want
50 * if exit-attribute-mode exists
53 * turn off those which can be turned off and aren't in
55 * turn on each mode which should be on and isn't, one by one
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...
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.
66 #include <curses.priv.h>
69 #define CUR SP_TERMTYPE
72 MODULE_ID("$Id: lib_vidattr.c,v 1.74 2019/04/13 22:40:54 tom Exp $")
76 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx mode, 1, outc)
78 #define TurnOn(mask, mode) \
79 if ((turn_on & mask) && mode) { \
81 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx mode, 1, outc); \
84 #define TurnOff(mask, mode) \
85 if ((turn_off & mask) && mode) { \
87 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx mode, 1, outc); \
91 /* if there is no current screen, assume we *can* do color */
92 #define SetColorsIf(why, old_attr) \
93 if (can_color && (why)) { \
94 int old_pair = PairNumber(old_attr); \
95 TR(TRACE_ATTRS, ("old pair = %d -- new pair = %d", old_pair, pair)); \
96 if ((pair != old_pair) \
97 || (fix_pair0 && (pair == 0)) \
98 || (reverse ^ ((old_attr & A_REVERSE) != 0))) { \
99 NCURSES_SP_NAME(_nc_do_color) (NCURSES_SP_ARGx \
106 #define PreviousAttr _nc_prescreen.previous_attr
109 NCURSES_SP_NAME(vidputs) (NCURSES_SP_DCLx
111 NCURSES_SP_OUTC outc)
113 attr_t turn_on, turn_off;
115 bool reverse = FALSE;
116 bool can_color = (SP_PARM == 0 || SP_PARM->_coloron);
117 #if NCURSES_EXT_FUNCS
118 bool fix_pair0 = (SP_PARM != 0 && SP_PARM->_coloron && !SP_PARM->_default_color);
120 #define fix_pair0 FALSE
123 newmode &= A_ATTRIBUTES;
125 T((T_CALLED("vidputs(%p,%s)"), (void *) SP_PARM, _traceattr(newmode)));
127 if (!IsValidTIScreen(SP_PARM))
130 /* this allows us to go on whether or not newterm() has been called */
132 PreviousAttr = AttrOf(SCREEN_ATTRS(SP_PARM));
134 TR(TRACE_ATTRS, ("previous attribute was %s", _traceattr(PreviousAttr)));
137 && (magic_cookie_glitch > 0)) {
139 static const chtype table[] =
155 int limit = (max_attributes <= 0) ? 1 : max_attributes;
159 * Limit the number of attribute bits set in the newmode according to
160 * the terminfo max_attributes value.
162 for (n = 0; n < SIZEOF(table); ++n) {
163 if ((table[n] & SP_PARM->_ok_attributes) == 0) {
164 newmode &= ~table[n];
165 } else if ((table[n] & newmode) != 0) {
166 if (used++ >= limit) {
167 newmode &= ~table[n];
168 if (newmode == retain)
176 newmode &= ~(SP_PARM->_xmc_suppress);
178 TR(TRACE_ATTRS, ("suppressed attribute is %s", _traceattr(newmode)));
182 * If we have a terminal that cannot combine color with video
183 * attributes, use the colors in preference.
185 if (((newmode & A_COLOR) != 0
187 && (no_color_video > 0)) {
189 * If we had chosen the A_xxx definitions to correspond to the
190 * no_color_video mask, we could simply shift it up and mask off the
191 * attributes. But we did not (actually copied Solaris' definitions).
192 * However, this is still simpler/faster than a lookup table.
194 * The 63 corresponds to A_STANDOUT, A_UNDERLINE, A_REVERSE, A_BLINK,
195 * A_DIM, A_BOLD which are 1:1 with no_color_video. The bits that
196 * correspond to A_INVIS, A_PROTECT (192) must be shifted up 1 and
197 * A_ALTCHARSET (256) down 2 to line up. We use the NCURSES_BITS
198 * macro so this will work properly for the wide-character layout.
200 unsigned value = (unsigned) no_color_video;
201 attr_t mask = NCURSES_BITS((value & 63)
202 | ((value & 192) << 1)
203 | ((value & 256) >> 2), 8);
205 if ((mask & A_REVERSE) != 0
206 && (newmode & A_REVERSE) != 0) {
213 if (newmode == PreviousAttr)
216 pair = PairNumber(newmode);
219 newmode &= ~A_REVERSE;
222 turn_off = (~newmode & PreviousAttr) & ALL_BUT_COLOR;
223 turn_on = (newmode & ~(PreviousAttr & TPARM_ATTR)) & ALL_BUT_COLOR;
225 SetColorsIf(((pair == 0) && !fix_pair0), PreviousAttr);
227 if (newmode == A_NORMAL) {
228 if ((PreviousAttr & A_ALTCHARSET) && exit_alt_charset_mode) {
229 doPut(exit_alt_charset_mode);
230 PreviousAttr &= ~A_ALTCHARSET;
233 if (exit_attribute_mode) {
234 doPut(exit_attribute_mode);
236 if (!SP_PARM || SP_PARM->_use_rmul) {
237 TurnOff(A_UNDERLINE, exit_underline_mode);
239 if (!SP_PARM || SP_PARM->_use_rmso) {
240 TurnOff(A_STANDOUT, exit_standout_mode);
243 if (!SP_PARM || SP_PARM->_use_ritm) {
244 TurnOff(A_ITALIC, exit_italics_mode);
248 PreviousAttr &= ALL_BUT_COLOR;
251 SetColorsIf((pair != 0) || fix_pair0, PreviousAttr);
252 } else if (set_attributes) {
253 if (turn_on || turn_off) {
254 TPUTS_TRACE("set_attributes");
255 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
256 tparm(set_attributes,
257 (newmode & A_STANDOUT) != 0,
258 (newmode & A_UNDERLINE) != 0,
259 (newmode & A_REVERSE) != 0,
260 (newmode & A_BLINK) != 0,
261 (newmode & A_DIM) != 0,
262 (newmode & A_BOLD) != 0,
263 (newmode & A_INVIS) != 0,
264 (newmode & A_PROTECT) != 0,
265 (newmode & A_ALTCHARSET) != 0),
267 PreviousAttr &= ALL_BUT_COLOR;
270 if (!SP_PARM || SP_PARM->_use_ritm) {
271 if (turn_on & A_ITALIC) {
272 TurnOn(A_ITALIC, enter_italics_mode);
273 } else if (turn_off & A_ITALIC) {
274 TurnOff(A_ITALIC, exit_italics_mode);
278 SetColorsIf((pair != 0) || fix_pair0, PreviousAttr);
281 TR(TRACE_ATTRS, ("turning %s off", _traceattr(turn_off)));
283 TurnOff(A_ALTCHARSET, exit_alt_charset_mode);
285 if (!SP_PARM || SP_PARM->_use_rmul) {
286 TurnOff(A_UNDERLINE, exit_underline_mode);
289 if (!SP_PARM || SP_PARM->_use_rmso) {
290 TurnOff(A_STANDOUT, exit_standout_mode);
293 if (!SP_PARM || SP_PARM->_use_ritm) {
294 TurnOff(A_ITALIC, exit_italics_mode);
297 if (turn_off && exit_attribute_mode) {
298 doPut(exit_attribute_mode);
299 turn_on |= (newmode & ALL_BUT_COLOR);
300 PreviousAttr &= ALL_BUT_COLOR;
302 SetColorsIf((pair != 0) || fix_pair0, PreviousAttr);
304 TR(TRACE_ATTRS, ("turning %s on", _traceattr(turn_on)));
306 TurnOn(A_ALTCHARSET, enter_alt_charset_mode);
307 TurnOn(A_BLINK, enter_blink_mode);
308 TurnOn(A_BOLD, enter_bold_mode);
309 TurnOn(A_DIM, enter_dim_mode);
310 TurnOn(A_REVERSE, enter_reverse_mode);
311 TurnOn(A_STANDOUT, enter_standout_mode);
312 TurnOn(A_PROTECT, enter_protected_mode);
313 TurnOn(A_INVIS, enter_secure_mode);
314 TurnOn(A_UNDERLINE, enter_underline_mode);
316 TurnOn(A_ITALIC, enter_italics_mode);
318 #if USE_WIDEC_SUPPORT && defined(enter_horizontal_hl_mode)
319 TurnOn(A_HORIZONTAL, enter_horizontal_hl_mode);
320 TurnOn(A_LEFT, enter_left_hl_mode);
321 TurnOn(A_LOW, enter_low_hl_mode);
322 TurnOn(A_RIGHT, enter_right_hl_mode);
323 TurnOn(A_TOP, enter_top_hl_mode);
324 TurnOn(A_VERTICAL, enter_vertical_hl_mode);
331 newmode |= A_REVERSE;
334 SetAttr(SCREEN_ATTRS(SP_PARM), newmode);
336 PreviousAttr = newmode;
343 vidputs(chtype newmode, NCURSES_OUTC outc)
345 SetSafeOutcWrapper(outc);
346 return NCURSES_SP_NAME(vidputs) (CURRENT_SCREEN,
353 NCURSES_SP_NAME(vidattr) (NCURSES_SP_DCLx chtype newmode)
355 T((T_CALLED("vidattr(%p,%s)"), (void *) SP_PARM, _traceattr(newmode)));
356 returnCode(NCURSES_SP_NAME(vidputs) (NCURSES_SP_ARGx
358 NCURSES_SP_NAME(_nc_putchar)));
363 vidattr(chtype newmode)
365 return NCURSES_SP_NAME(vidattr) (CURRENT_SCREEN, newmode);
369 NCURSES_EXPORT(chtype)
370 NCURSES_SP_NAME(termattrs) (NCURSES_SP_DCL0)
372 chtype attrs = A_NORMAL;
374 T((T_CALLED("termattrs(%p)"), (void *) SP_PARM));
376 if (HasTerminal(SP_PARM)) {
377 #ifdef USE_TERM_DRIVER
378 attrs = CallDriver(SP_PARM, td_conattr);
379 #else /* ! USE_TERM_DRIVER */
381 if (enter_alt_charset_mode)
382 attrs |= A_ALTCHARSET;
384 if (enter_blink_mode)
393 if (enter_reverse_mode)
396 if (enter_standout_mode)
399 if (enter_protected_mode)
402 if (enter_secure_mode)
405 if (enter_underline_mode)
406 attrs |= A_UNDERLINE;
408 if (SP_PARM->_coloron)
412 if (enter_italics_mode)
416 #endif /* USE_TERM_DRIVER */
422 NCURSES_EXPORT(chtype)
425 return NCURSES_SP_NAME(termattrs) (CURRENT_SCREEN);