1 /****************************************************************************
2 * Copyright (c) 1998-2011,2013 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.67 2013/08/31 20:08:59 tom Exp $")
76 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx mode, 1, outc)
78 #define TurnOn(mask, mode) \
79 if ((turn_on & mask) && mode) { doPut(mode); }
81 #define TurnOff(mask, mode) \
82 if ((turn_off & mask) && mode) { doPut(mode); turn_off &= ~mask; }
84 /* if there is no current screen, assume we *can* do color */
85 #define SetColorsIf(why, old_attr) \
86 if (can_color && (why)) { \
87 int old_pair = PairNumber(old_attr); \
88 TR(TRACE_ATTRS, ("old pair = %d -- new pair = %d", old_pair, pair)); \
89 if ((pair != old_pair) \
90 || (fix_pair0 && (pair == 0)) \
91 || (reverse ^ ((old_attr & A_REVERSE) != 0))) { \
92 NCURSES_SP_NAME(_nc_do_color) (NCURSES_SP_ARGx \
99 #define PreviousAttr _nc_prescreen.previous_attr
102 NCURSES_SP_NAME(vidputs) (NCURSES_SP_DCLx
104 NCURSES_SP_OUTC outc)
106 attr_t turn_on, turn_off;
108 bool reverse = FALSE;
109 bool can_color = (SP_PARM == 0 || SP_PARM->_coloron);
110 #if NCURSES_EXT_FUNCS
111 bool fix_pair0 = (SP_PARM != 0 && SP_PARM->_coloron && !SP_PARM->_default_color);
113 #define fix_pair0 FALSE
116 newmode &= A_ATTRIBUTES;
118 T((T_CALLED("vidputs(%p,%s)"), (void *) SP_PARM, _traceattr(newmode)));
120 if (!IsTermInfo(SP_PARM))
123 /* this allows us to go on whether or not newterm() has been called */
125 PreviousAttr = AttrOf(SCREEN_ATTRS(SP_PARM));
127 TR(TRACE_ATTRS, ("previous attribute was %s", _traceattr(PreviousAttr)));
130 && (magic_cookie_glitch > 0)) {
132 static const chtype table[] =
148 int limit = (max_attributes <= 0) ? 1 : max_attributes;
152 * Limit the number of attribute bits set in the newmode according to
153 * the terminfo max_attributes value.
155 for (n = 0; n < SIZEOF(table); ++n) {
156 if ((table[n] & SP_PARM->_ok_attributes) == 0) {
157 newmode &= ~table[n];
158 } else if ((table[n] & newmode) != 0) {
159 if (used++ >= limit) {
160 newmode &= ~table[n];
161 if (newmode == retain)
169 newmode &= ~(SP_PARM->_xmc_suppress);
171 TR(TRACE_ATTRS, ("suppressed attribute is %s", _traceattr(newmode)));
175 * If we have a terminal that cannot combine color with video
176 * attributes, use the colors in preference.
178 if (((newmode & A_COLOR) != 0
180 && (no_color_video > 0)) {
182 * If we had chosen the A_xxx definitions to correspond to the
183 * no_color_video mask, we could simply shift it up and mask off the
184 * attributes. But we did not (actually copied Solaris' definitions).
185 * However, this is still simpler/faster than a lookup table.
187 * The 63 corresponds to A_STANDOUT, A_UNDERLINE, A_REVERSE, A_BLINK,
188 * A_DIM, A_BOLD which are 1:1 with no_color_video. The bits that
189 * correspond to A_INVIS, A_PROTECT (192) must be shifted up 1 and
190 * A_ALTCHARSET (256) down 2 to line up. We use the NCURSES_BITS
191 * macro so this will work properly for the wide-character layout.
193 unsigned value = (unsigned) no_color_video;
194 attr_t mask = NCURSES_BITS((value & 63)
195 | ((value & 192) << 1)
196 | ((value & 256) >> 2), 8);
198 if ((mask & A_REVERSE) != 0
199 && (newmode & A_REVERSE) != 0) {
206 if (newmode == PreviousAttr)
209 pair = PairNumber(newmode);
212 newmode &= ~A_REVERSE;
215 turn_off = (~newmode & PreviousAttr) & ALL_BUT_COLOR;
216 turn_on = (newmode & ~PreviousAttr) & ALL_BUT_COLOR;
218 SetColorsIf(((pair == 0) && !fix_pair0), PreviousAttr);
220 if (newmode == A_NORMAL) {
221 if ((PreviousAttr & A_ALTCHARSET) && exit_alt_charset_mode) {
222 doPut(exit_alt_charset_mode);
223 PreviousAttr &= ~A_ALTCHARSET;
226 if (exit_attribute_mode) {
227 doPut(exit_attribute_mode);
229 if (!SP_PARM || SP_PARM->_use_rmul) {
230 TurnOff(A_UNDERLINE, exit_underline_mode);
232 if (!SP_PARM || SP_PARM->_use_rmso) {
233 TurnOff(A_STANDOUT, exit_standout_mode);
236 if (!SP_PARM || SP_PARM->_use_ritm) {
237 TurnOff(A_ITALIC, exit_italics_mode);
241 PreviousAttr &= ALL_BUT_COLOR;
244 SetColorsIf((pair != 0) || fix_pair0, PreviousAttr);
245 } else if (set_attributes) {
246 if (turn_on || turn_off) {
247 TPUTS_TRACE("set_attributes");
248 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
249 tparm(set_attributes,
250 (newmode & A_STANDOUT) != 0,
251 (newmode & A_UNDERLINE) != 0,
252 (newmode & A_REVERSE) != 0,
253 (newmode & A_BLINK) != 0,
254 (newmode & A_DIM) != 0,
255 (newmode & A_BOLD) != 0,
256 (newmode & A_INVIS) != 0,
257 (newmode & A_PROTECT) != 0,
258 (newmode & A_ALTCHARSET) != 0),
260 PreviousAttr &= ALL_BUT_COLOR;
263 if (!SP_PARM || SP_PARM->_use_ritm) {
264 if (turn_on & A_ITALIC) {
265 TurnOn(A_ITALIC, enter_italics_mode);
266 } else if (turn_off & A_ITALIC) {
267 TurnOff(A_ITALIC, exit_italics_mode);
271 SetColorsIf((pair != 0) || fix_pair0, PreviousAttr);
274 TR(TRACE_ATTRS, ("turning %s off", _traceattr(turn_off)));
276 TurnOff(A_ALTCHARSET, exit_alt_charset_mode);
278 if (!SP_PARM || SP_PARM->_use_rmul) {
279 TurnOff(A_UNDERLINE, exit_underline_mode);
282 if (!SP_PARM || SP_PARM->_use_rmso) {
283 TurnOff(A_STANDOUT, exit_standout_mode);
286 if (!SP_PARM || SP_PARM->_use_ritm) {
287 TurnOff(A_ITALIC, exit_italics_mode);
290 if (turn_off && exit_attribute_mode) {
291 doPut(exit_attribute_mode);
292 turn_on |= (newmode & ALL_BUT_COLOR);
293 PreviousAttr &= ALL_BUT_COLOR;
295 SetColorsIf((pair != 0) || fix_pair0, PreviousAttr);
297 TR(TRACE_ATTRS, ("turning %s on", _traceattr(turn_on)));
299 TurnOn(A_ALTCHARSET, enter_alt_charset_mode);
300 TurnOn(A_BLINK, enter_blink_mode);
301 TurnOn(A_BOLD, enter_bold_mode);
302 TurnOn(A_DIM, enter_dim_mode);
303 TurnOn(A_REVERSE, enter_reverse_mode);
304 TurnOn(A_STANDOUT, enter_standout_mode);
305 TurnOn(A_PROTECT, enter_protected_mode);
306 TurnOn(A_INVIS, enter_secure_mode);
307 TurnOn(A_UNDERLINE, enter_underline_mode);
309 TurnOn(A_ITALIC, enter_italics_mode);
311 #if USE_WIDEC_SUPPORT
312 TurnOn(A_HORIZONTAL, enter_horizontal_hl_mode);
313 TurnOn(A_LEFT, enter_left_hl_mode);
314 TurnOn(A_LOW, enter_low_hl_mode);
315 TurnOn(A_RIGHT, enter_right_hl_mode);
316 TurnOn(A_TOP, enter_top_hl_mode);
317 TurnOn(A_VERTICAL, enter_vertical_hl_mode);
324 newmode |= A_REVERSE;
327 SetAttr(SCREEN_ATTRS(SP_PARM), newmode);
329 PreviousAttr = newmode;
336 vidputs(chtype newmode, NCURSES_OUTC outc)
338 SetSafeOutcWrapper(outc);
339 return NCURSES_SP_NAME(vidputs) (CURRENT_SCREEN,
346 NCURSES_SP_NAME(vidattr) (NCURSES_SP_DCLx chtype newmode)
348 T((T_CALLED("vidattr(%p,%s)"), (void *) SP_PARM, _traceattr(newmode)));
349 returnCode(NCURSES_SP_NAME(vidputs) (NCURSES_SP_ARGx
351 NCURSES_SP_NAME(_nc_putchar)));
356 vidattr(chtype newmode)
358 return NCURSES_SP_NAME(vidattr) (CURRENT_SCREEN, newmode);
362 NCURSES_EXPORT(chtype)
363 NCURSES_SP_NAME(termattrs) (NCURSES_SP_DCL0)
365 chtype attrs = A_NORMAL;
367 T((T_CALLED("termattrs(%p)"), (void *) SP_PARM));
369 if (HasTerminal(SP_PARM)) {
370 #ifdef USE_TERM_DRIVER
371 attrs = CallDriver(SP_PARM, conattr);
372 #else /* ! USE_TERM_DRIVER */
374 if (enter_alt_charset_mode)
375 attrs |= A_ALTCHARSET;
377 if (enter_blink_mode)
386 if (enter_reverse_mode)
389 if (enter_standout_mode)
392 if (enter_protected_mode)
395 if (enter_secure_mode)
398 if (enter_underline_mode)
399 attrs |= A_UNDERLINE;
401 if (SP_PARM->_coloron)
405 if (enter_italics_mode)
409 #endif /* USE_TERM_DRIVER */
415 NCURSES_EXPORT(chtype)
418 return NCURSES_SP_NAME(termattrs) (CURRENT_SCREEN);