1 /****************************************************************************
2 * Copyright 2018-2019,2020 Thomas E. Dickey *
3 * Copyright 1998-2014,2017 Free Software Foundation, Inc. *
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: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
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. *
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 *
28 ****************************************************************************/
30 /****************************************************************************
31 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
32 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
33 * and: Thomas E. Dickey 1996-on *
34 * and: Juergen Pfeifer 2009 *
35 ****************************************************************************/
38 * vidputs(newmode, outc)
40 * newmode is taken to be the logical 'or' of the symbols in curses.h
41 * representing graphic renditions. The terminal is set to be in all of
42 * the given modes, if possible.
44 * if the new attribute is normal
45 * if exit-alt-char-set exists
47 * emit exit-attribute-mode
48 * else if set-attributes exists
49 * use it to set exactly what you want
51 * if exit-attribute-mode exists
54 * turn off those which can be turned off and aren't in
56 * turn on each mode which should be on and isn't, one by one
58 * NOTE that this algorithm won't achieve the desired mix of attributes
59 * in some cases, but those are probably just those cases in which it is
60 * actually impossible, anyway, so...
62 * NOTE that we cannot assume that there's no interaction between color
63 * and other attribute resets. So each time we reset color (or other
64 * attributes) we'll have to be prepared to restore the other.
67 #include <curses.priv.h>
70 #define CUR SP_TERMTYPE
73 MODULE_ID("$Id: lib_vidattr.c,v 1.78 2020/05/27 23:56:32 tom Exp $")
77 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx mode, 1, outc)
79 #define TurnOn(mask, mode) \
80 if ((turn_on & mask) && mode) { \
82 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx mode, 1, outc); \
85 #define TurnOff(mask, mode) \
86 if ((turn_off & mask) && mode) { \
88 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx mode, 1, outc); \
92 /* if there is no current screen, assume we *can* do color */
93 #define SetColorsIf(why, old_attr) \
94 if (can_color && (why)) { \
95 int old_pair = PairNumber(old_attr); \
96 TR(TRACE_ATTRS, ("old pair = %d -- new pair = %d", old_pair, pair)); \
97 if ((pair != old_pair) \
98 || (fix_pair0 && (pair == 0)) \
99 || (reverse ^ ((old_attr & A_REVERSE) != 0))) { \
100 NCURSES_SP_NAME(_nc_do_color) (NCURSES_SP_ARGx \
107 #define PreviousAttr _nc_prescreen.previous_attr
110 NCURSES_SP_NAME(vidputs) (NCURSES_SP_DCLx
112 NCURSES_SP_OUTC outc)
114 attr_t turn_on, turn_off;
116 bool reverse = FALSE;
117 bool can_color = (SP_PARM == 0 || SP_PARM->_coloron);
118 #if NCURSES_EXT_FUNCS
119 bool fix_pair0 = (SP_PARM != 0 && SP_PARM->_coloron && !SP_PARM->_default_color);
121 #define fix_pair0 FALSE
124 newmode &= A_ATTRIBUTES;
126 T((T_CALLED("vidputs(%p,%s)"), (void *) SP_PARM, _traceattr(newmode)));
128 if (!IsValidTIScreen(SP_PARM))
131 /* this allows us to go on whether or not newterm() has been called */
133 PreviousAttr = AttrOf(SCREEN_ATTRS(SP_PARM));
135 TR(TRACE_ATTRS, ("previous attribute was %s", _traceattr(PreviousAttr)));
138 && (magic_cookie_glitch > 0)) {
140 static const chtype table[] =
156 #ifdef max_attributes /* not in U/Win */
157 int limit = (max_attributes <= 0) ? 1 : max_attributes;
164 * Limit the number of attribute bits set in the newmode according to
165 * the terminfo max_attributes value.
167 for (n = 0; n < SIZEOF(table); ++n) {
168 if ((table[n] & SP_PARM->_ok_attributes) == 0) {
169 newmode &= ~table[n];
170 } else if ((table[n] & newmode) != 0) {
171 if (used++ >= limit) {
172 newmode &= ~table[n];
173 if (newmode == retain)
181 newmode &= ~(SP_PARM->_xmc_suppress);
183 TR(TRACE_ATTRS, ("suppressed attribute is %s", _traceattr(newmode)));
187 * If we have a terminal that cannot combine color with video
188 * attributes, use the colors in preference.
190 if (((newmode & A_COLOR) != 0
192 && (no_color_video > 0)) {
194 * If we had chosen the A_xxx definitions to correspond to the
195 * no_color_video mask, we could simply shift it up and mask off the
196 * attributes. But we did not (actually copied Solaris' definitions).
197 * However, this is still simpler/faster than a lookup table.
199 * The 63 corresponds to A_STANDOUT, A_UNDERLINE, A_REVERSE, A_BLINK,
200 * A_DIM, A_BOLD which are 1:1 with no_color_video. The bits that
201 * correspond to A_INVIS, A_PROTECT (192) must be shifted up 1 and
202 * A_ALTCHARSET (256) down 2 to line up. We use the NCURSES_BITS
203 * macro so this will work properly for the wide-character layout.
205 unsigned value = (unsigned) no_color_video;
206 attr_t mask = NCURSES_BITS((value & 63)
207 | ((value & 192) << 1)
208 | ((value & 256) >> 2), 8);
210 if ((mask & A_REVERSE) != 0
211 && (newmode & A_REVERSE) != 0) {
218 if (newmode == PreviousAttr)
221 pair = PairNumber(newmode);
224 newmode &= ~A_REVERSE;
227 turn_off = (~newmode & PreviousAttr) & ALL_BUT_COLOR;
228 turn_on = (newmode & ~(PreviousAttr & TPARM_ATTR)) & ALL_BUT_COLOR;
230 SetColorsIf(((pair == 0) && !fix_pair0), PreviousAttr);
232 if (newmode == A_NORMAL) {
233 if ((PreviousAttr & A_ALTCHARSET) && exit_alt_charset_mode) {
234 doPut(exit_alt_charset_mode);
235 PreviousAttr &= ~A_ALTCHARSET;
238 if (exit_attribute_mode) {
239 doPut(exit_attribute_mode);
241 if (!SP_PARM || SP_PARM->_use_rmul) {
242 TurnOff(A_UNDERLINE, exit_underline_mode);
244 if (!SP_PARM || SP_PARM->_use_rmso) {
245 TurnOff(A_STANDOUT, exit_standout_mode);
248 if (!SP_PARM || SP_PARM->_use_ritm) {
249 TurnOff(A_ITALIC, exit_italics_mode);
253 PreviousAttr &= ALL_BUT_COLOR;
256 SetColorsIf((pair != 0) || fix_pair0, PreviousAttr);
257 } else if (set_attributes) {
258 if (turn_on || turn_off) {
259 TPUTS_TRACE("set_attributes");
260 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
261 TIPARM_9(set_attributes,
262 (newmode & A_STANDOUT) != 0,
263 (newmode & A_UNDERLINE) != 0,
264 (newmode & A_REVERSE) != 0,
265 (newmode & A_BLINK) != 0,
266 (newmode & A_DIM) != 0,
267 (newmode & A_BOLD) != 0,
268 (newmode & A_INVIS) != 0,
269 (newmode & A_PROTECT) != 0,
270 (newmode & A_ALTCHARSET) != 0),
272 PreviousAttr &= ALL_BUT_COLOR;
275 if (!SP_PARM || SP_PARM->_use_ritm) {
276 if (turn_on & A_ITALIC) {
277 TurnOn(A_ITALIC, enter_italics_mode);
278 } else if (turn_off & A_ITALIC) {
279 TurnOff(A_ITALIC, exit_italics_mode);
283 SetColorsIf((pair != 0) || fix_pair0, PreviousAttr);
286 TR(TRACE_ATTRS, ("turning %s off", _traceattr(turn_off)));
288 TurnOff(A_ALTCHARSET, exit_alt_charset_mode);
290 if (!SP_PARM || SP_PARM->_use_rmul) {
291 TurnOff(A_UNDERLINE, exit_underline_mode);
294 if (!SP_PARM || SP_PARM->_use_rmso) {
295 TurnOff(A_STANDOUT, exit_standout_mode);
298 if (!SP_PARM || SP_PARM->_use_ritm) {
299 TurnOff(A_ITALIC, exit_italics_mode);
302 if (turn_off && exit_attribute_mode) {
303 doPut(exit_attribute_mode);
304 turn_on |= (newmode & ALL_BUT_COLOR);
305 PreviousAttr &= ALL_BUT_COLOR;
307 SetColorsIf((pair != 0) || fix_pair0, PreviousAttr);
309 TR(TRACE_ATTRS, ("turning %s on", _traceattr(turn_on)));
311 TurnOn(A_ALTCHARSET, enter_alt_charset_mode);
312 TurnOn(A_BLINK, enter_blink_mode);
313 TurnOn(A_BOLD, enter_bold_mode);
314 TurnOn(A_DIM, enter_dim_mode);
315 TurnOn(A_REVERSE, enter_reverse_mode);
316 TurnOn(A_STANDOUT, enter_standout_mode);
317 TurnOn(A_PROTECT, enter_protected_mode);
318 TurnOn(A_INVIS, enter_secure_mode);
319 TurnOn(A_UNDERLINE, enter_underline_mode);
321 TurnOn(A_ITALIC, enter_italics_mode);
323 #if USE_WIDEC_SUPPORT && defined(enter_horizontal_hl_mode)
324 TurnOn(A_HORIZONTAL, enter_horizontal_hl_mode);
325 TurnOn(A_LEFT, enter_left_hl_mode);
326 TurnOn(A_LOW, enter_low_hl_mode);
327 TurnOn(A_RIGHT, enter_right_hl_mode);
328 TurnOn(A_TOP, enter_top_hl_mode);
329 TurnOn(A_VERTICAL, enter_vertical_hl_mode);
335 newmode |= A_REVERSE;
338 SetAttr(SCREEN_ATTRS(SP_PARM), newmode);
340 PreviousAttr = newmode;
347 vidputs(chtype newmode, NCURSES_OUTC outc)
349 SetSafeOutcWrapper(outc);
350 return NCURSES_SP_NAME(vidputs) (CURRENT_SCREEN,
357 NCURSES_SP_NAME(vidattr) (NCURSES_SP_DCLx chtype newmode)
359 T((T_CALLED("vidattr(%p,%s)"), (void *) SP_PARM, _traceattr(newmode)));
360 returnCode(NCURSES_SP_NAME(vidputs) (NCURSES_SP_ARGx
362 NCURSES_SP_NAME(_nc_putchar)));
367 vidattr(chtype newmode)
369 return NCURSES_SP_NAME(vidattr) (CURRENT_SCREEN, newmode);
373 NCURSES_EXPORT(chtype)
374 NCURSES_SP_NAME(termattrs) (NCURSES_SP_DCL0)
376 chtype attrs = A_NORMAL;
378 T((T_CALLED("termattrs(%p)"), (void *) SP_PARM));
380 if (HasTerminal(SP_PARM)) {
381 #ifdef USE_TERM_DRIVER
382 attrs = CallDriver(SP_PARM, td_conattr);
383 #else /* ! USE_TERM_DRIVER */
385 if (enter_alt_charset_mode)
386 attrs |= A_ALTCHARSET;
388 if (enter_blink_mode)
397 if (enter_reverse_mode)
400 if (enter_standout_mode)
403 if (enter_protected_mode)
406 if (enter_secure_mode)
409 if (enter_underline_mode)
410 attrs |= A_UNDERLINE;
412 if (SP_PARM->_coloron)
416 if (enter_italics_mode)
420 #endif /* USE_TERM_DRIVER */
426 NCURSES_EXPORT(chtype)
429 return NCURSES_SP_NAME(termattrs) (CURRENT_SCREEN);