1 /****************************************************************************
2 * Copyright (c) 1998-2007,2009 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>
70 #define CUR SP_TERMTYPE
73 MODULE_ID("$Id: lib_vidattr.c,v 1.56 2009/05/10 00:48:29 tom Exp $")
77 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx mode, 1, outc)
79 #define TurnOn(mask,mode) \
80 if ((turn_on & mask) && mode) { doPut(mode); }
82 #define TurnOff(mask,mode) \
83 if ((turn_off & mask) && mode) { doPut(mode); turn_off &= ~mask; }
85 /* if there is no current screen, assume we *can* do color */
86 #define SetColorsIf(why,old_attr) \
87 if (can_color && (why)) { \
88 int old_pair = PAIR_NUMBER(old_attr); \
89 TR(TRACE_ATTRS, ("old pair = %d -- new pair = %d", old_pair, pair)); \
90 if ((pair != old_pair) \
91 || (fix_pair0 && (pair == 0)) \
92 || (reverse ^ ((old_attr & A_REVERSE) != 0))) { \
93 NCURSES_SP_NAME(_nc_do_color) (NCURSES_SP_ARGx \
94 old_pair, pair, reverse, outc); \
98 #define PreviousAttr _nc_prescreen.previous_attr
101 NCURSES_SP_NAME(vidputs) (NCURSES_SP_DCLx
103 NCURSES_SP_OUTC outc)
105 attr_t turn_on, turn_off;
107 bool reverse = FALSE;
108 bool can_color = (SP_PARM == 0 || SP_PARM->_coloron);
109 #if NCURSES_EXT_FUNCS
110 bool fix_pair0 = (SP_PARM != 0 && SP_PARM->_coloron && !SP_PARM->_default_color);
112 #define fix_pair0 FALSE
115 newmode &= A_ATTRIBUTES;
116 T((T_CALLED("vidputs(%s)"), _traceattr(newmode)));
118 /* this allows us to go on whether or not newterm() has been called */
120 PreviousAttr = AttrOf(SCREEN_ATTRS(SP_PARM));
122 TR(TRACE_ATTRS, ("previous attribute was %s", _traceattr(PreviousAttr)));
125 && (magic_cookie_glitch > 0)) {
127 static const chtype table[] =
140 int limit = (max_attributes <= 0) ? 1 : max_attributes;
144 * Limit the number of attribute bits set in the newmode according to
145 * the terminfo max_attributes value.
147 for (n = 0; n < SIZEOF(table); ++n) {
148 if ((table[n] & SP_PARM->_ok_attributes) == 0) {
149 newmode &= ~table[n];
150 } else if ((table[n] & newmode) != 0) {
151 if (used++ >= limit) {
152 newmode &= ~table[n];
153 if (newmode == retain)
161 newmode &= ~(SP_PARM->_xmc_suppress);
163 TR(TRACE_ATTRS, ("suppressed attribute is %s", _traceattr(newmode)));
167 * If we have a terminal that cannot combine color with video
168 * attributes, use the colors in preference.
170 if (((newmode & A_COLOR) != 0
172 && (no_color_video > 0)) {
174 * If we had chosen the A_xxx definitions to correspond to the
175 * no_color_video mask, we could simply shift it up and mask off the
176 * attributes. But we did not (actually copied Solaris' definitions).
177 * However, this is still simpler/faster than a lookup table.
179 * The 63 corresponds to A_STANDOUT, A_UNDERLINE, A_REVERSE, A_BLINK,
180 * A_DIM, A_BOLD which are 1:1 with no_color_video. The bits that
181 * correspond to A_INVIS, A_PROTECT (192) must be shifted up 1 and
182 * A_ALTCHARSET (256) down 2 to line up. We use the NCURSES_BITS
183 * macro so this will work properly for the wide-character layout.
185 unsigned value = no_color_video;
186 attr_t mask = NCURSES_BITS((value & 63)
187 | ((value & 192) << 1)
188 | ((value & 256) >> 2), 8);
190 if ((mask & A_REVERSE) != 0
191 && (newmode & A_REVERSE) != 0) {
198 if (newmode == PreviousAttr)
201 pair = PAIR_NUMBER(newmode);
204 newmode &= ~A_REVERSE;
207 turn_off = (~newmode & PreviousAttr) & ALL_BUT_COLOR;
208 turn_on = (newmode & ~PreviousAttr) & ALL_BUT_COLOR;
210 SetColorsIf(((pair == 0) && !fix_pair0), PreviousAttr);
212 if (newmode == A_NORMAL) {
213 if ((PreviousAttr & A_ALTCHARSET) && exit_alt_charset_mode) {
214 doPut(exit_alt_charset_mode);
215 PreviousAttr &= ~A_ALTCHARSET;
218 if (exit_attribute_mode) {
219 doPut(exit_attribute_mode);
221 if (!SP_PARM || SP_PARM->_use_rmul) {
222 TurnOff(A_UNDERLINE, exit_underline_mode);
224 if (!SP_PARM || SP_PARM->_use_rmso) {
225 TurnOff(A_STANDOUT, exit_standout_mode);
228 PreviousAttr &= ALL_BUT_COLOR;
231 SetColorsIf((pair != 0) || fix_pair0, PreviousAttr);
232 } else if (set_attributes) {
233 if (turn_on || turn_off) {
234 TPUTS_TRACE("set_attributes");
235 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
236 tparm(set_attributes,
237 (newmode & A_STANDOUT) != 0,
238 (newmode & A_UNDERLINE) != 0,
239 (newmode & A_REVERSE) != 0,
240 (newmode & A_BLINK) != 0,
241 (newmode & A_DIM) != 0,
242 (newmode & A_BOLD) != 0,
243 (newmode & A_INVIS) != 0,
244 (newmode & A_PROTECT) != 0,
245 (newmode & A_ALTCHARSET) != 0),
247 PreviousAttr &= ALL_BUT_COLOR;
249 SetColorsIf((pair != 0) || fix_pair0, PreviousAttr);
252 TR(TRACE_ATTRS, ("turning %s off", _traceattr(turn_off)));
254 TurnOff(A_ALTCHARSET, exit_alt_charset_mode);
256 if (!SP_PARM || SP_PARM->_use_rmul) {
257 TurnOff(A_UNDERLINE, exit_underline_mode);
260 if (!SP_PARM || SP_PARM->_use_rmso) {
261 TurnOff(A_STANDOUT, exit_standout_mode);
264 if (turn_off && exit_attribute_mode) {
265 doPut(exit_attribute_mode);
266 turn_on |= (newmode & ALL_BUT_COLOR);
267 PreviousAttr &= ALL_BUT_COLOR;
269 SetColorsIf((pair != 0) || fix_pair0, PreviousAttr);
271 TR(TRACE_ATTRS, ("turning %s on", _traceattr(turn_on)));
273 TurnOn(A_ALTCHARSET, enter_alt_charset_mode);
274 TurnOn(A_BLINK, enter_blink_mode);
275 TurnOn(A_BOLD, enter_bold_mode);
276 TurnOn(A_DIM, enter_dim_mode);
277 TurnOn(A_REVERSE, enter_reverse_mode);
278 TurnOn(A_STANDOUT, enter_standout_mode);
279 TurnOn(A_PROTECT, enter_protected_mode);
280 TurnOn(A_INVIS, enter_secure_mode);
281 TurnOn(A_UNDERLINE, enter_underline_mode);
282 #if USE_WIDEC_SUPPORT
283 TurnOn(A_HORIZONTAL, enter_horizontal_hl_mode);
284 TurnOn(A_LEFT, enter_left_hl_mode);
285 TurnOn(A_LOW, enter_low_hl_mode);
286 TurnOn(A_RIGHT, enter_right_hl_mode);
287 TurnOn(A_TOP, enter_top_hl_mode);
288 TurnOn(A_VERTICAL, enter_vertical_hl_mode);
295 newmode |= A_REVERSE;
298 SetAttr(SCREEN_ATTRS(SP_PARM), newmode);
300 PreviousAttr = newmode;
306 NCURSES_SP_NAME(vidattr) (NCURSES_SP_DCLx
309 return NCURSES_SP_NAME(vidputs) (NCURSES_SP_ARGx
311 NCURSES_SP_NAME(_nc_outch));
316 vidputs(chtype newmode, NCURSES_OUTC outc)
318 SetSafeOutcWrapper(outc);
319 return NCURSES_SP_NAME(vidputs) (CURRENT_SCREEN, newmode, _nc_outc_wrapper);
323 vidattr(chtype newmode)
325 T((T_CALLED("vidattr(%s)"), _traceattr(newmode)));
327 returnCode(vidputs(newmode, _nc_outch));
331 NCURSES_EXPORT(chtype)
332 NCURSES_SP_NAME(termattrs) (NCURSES_SP_DCL0)
334 chtype attrs = A_NORMAL;
336 T((T_CALLED("termattrs()")));
337 if (enter_alt_charset_mode)
338 attrs |= A_ALTCHARSET;
340 if (enter_blink_mode)
349 if (enter_reverse_mode)
352 if (enter_standout_mode)
355 if (enter_protected_mode)
358 if (enter_secure_mode)
361 if (enter_underline_mode)
362 attrs |= A_UNDERLINE;
364 if (SP_PARM->_coloron)
371 NCURSES_EXPORT(chtype)
374 return NCURSES_SP_NAME(termattrs) (CURRENT_SCREEN);