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>
69 MODULE_ID("$Id: lib_vidattr.c,v 1.53 2009/05/02 22:35:03 tom Exp $")
71 #define doPut(mode) TPUTS_TRACE(#mode); tputs(mode, 1, outc)
73 #define TurnOn(mask,mode) \
74 if ((turn_on & mask) && mode) { doPut(mode); }
76 #define TurnOff(mask,mode) \
77 if ((turn_off & mask) && mode) { doPut(mode); turn_off &= ~mask; }
79 /* if there is no current screen, assume we *can* do color */
80 #define SetColorsIf(why,old_attr) \
81 if (can_color && (why)) { \
82 int old_pair = PAIR_NUMBER(old_attr); \
83 TR(TRACE_ATTRS, ("old pair = %d -- new pair = %d", old_pair, pair)); \
84 if ((pair != old_pair) \
85 || (fix_pair0 && (pair == 0)) \
86 || (reverse ^ ((old_attr & A_REVERSE) != 0))) { \
87 _nc_do_color(old_pair, pair, reverse, outc); \
91 #define PreviousAttr _nc_prescreen.previous_attr
94 NCURSES_SP_NAME(vidputs) (NCURSES_SP_DCLx
98 attr_t turn_on, turn_off;
100 bool reverse = FALSE;
101 bool can_color = (SP_PARM == 0 || SP_PARM->_coloron);
102 #if NCURSES_EXT_FUNCS
103 bool fix_pair0 = (SP_PARM != 0 && SP_PARM->_coloron && !SP_PARM->_default_color);
105 #define fix_pair0 FALSE
108 newmode &= A_ATTRIBUTES;
109 T((T_CALLED("vidputs(%s)"), _traceattr(newmode)));
111 /* this allows us to go on whether or not newterm() has been called */
113 PreviousAttr = AttrOf(SCREEN_ATTRS(SP_PARM));
115 TR(TRACE_ATTRS, ("previous attribute was %s", _traceattr(PreviousAttr)));
118 && (magic_cookie_glitch > 0)) {
120 static const chtype table[] =
133 int limit = (max_attributes <= 0) ? 1 : max_attributes;
137 * Limit the number of attribute bits set in the newmode according to
138 * the terminfo max_attributes value.
140 for (n = 0; n < SIZEOF(table); ++n) {
141 if ((table[n] & SP_PARM->_ok_attributes) == 0) {
142 newmode &= ~table[n];
143 } else if ((table[n] & newmode) != 0) {
144 if (used++ >= limit) {
145 newmode &= ~table[n];
146 if (newmode == retain)
154 newmode &= ~(SP_PARM->_xmc_suppress);
156 TR(TRACE_ATTRS, ("suppressed attribute is %s", _traceattr(newmode)));
160 * If we have a terminal that cannot combine color with video
161 * attributes, use the colors in preference.
163 if (((newmode & A_COLOR) != 0
165 && (no_color_video > 0)) {
167 * If we had chosen the A_xxx definitions to correspond to the
168 * no_color_video mask, we could simply shift it up and mask off the
169 * attributes. But we did not (actually copied Solaris' definitions).
170 * However, this is still simpler/faster than a lookup table.
172 * The 63 corresponds to A_STANDOUT, A_UNDERLINE, A_REVERSE, A_BLINK,
173 * A_DIM, A_BOLD which are 1:1 with no_color_video. The bits that
174 * correspond to A_INVIS, A_PROTECT (192) must be shifted up 1 and
175 * A_ALTCHARSET (256) down 2 to line up. We use the NCURSES_BITS
176 * macro so this will work properly for the wide-character layout.
178 unsigned value = no_color_video;
179 attr_t mask = NCURSES_BITS((value & 63)
180 | ((value & 192) << 1)
181 | ((value & 256) >> 2), 8);
183 if ((mask & A_REVERSE) != 0
184 && (newmode & A_REVERSE) != 0) {
191 if (newmode == PreviousAttr)
194 pair = PAIR_NUMBER(newmode);
197 newmode &= ~A_REVERSE;
200 turn_off = (~newmode & PreviousAttr) & ALL_BUT_COLOR;
201 turn_on = (newmode & ~PreviousAttr) & ALL_BUT_COLOR;
203 SetColorsIf(((pair == 0) && !fix_pair0), PreviousAttr);
205 if (newmode == A_NORMAL) {
206 if ((PreviousAttr & A_ALTCHARSET) && exit_alt_charset_mode) {
207 doPut(exit_alt_charset_mode);
208 PreviousAttr &= ~A_ALTCHARSET;
211 if (exit_attribute_mode) {
212 doPut(exit_attribute_mode);
214 if (!SP_PARM || SP_PARM->_use_rmul) {
215 TurnOff(A_UNDERLINE, exit_underline_mode);
217 if (!SP_PARM || SP_PARM->_use_rmso) {
218 TurnOff(A_STANDOUT, exit_standout_mode);
221 PreviousAttr &= ALL_BUT_COLOR;
224 SetColorsIf((pair != 0) || fix_pair0, PreviousAttr);
225 } else if (set_attributes) {
226 if (turn_on || turn_off) {
227 TPUTS_TRACE("set_attributes");
228 tputs(tparm(set_attributes,
229 (newmode & A_STANDOUT) != 0,
230 (newmode & A_UNDERLINE) != 0,
231 (newmode & A_REVERSE) != 0,
232 (newmode & A_BLINK) != 0,
233 (newmode & A_DIM) != 0,
234 (newmode & A_BOLD) != 0,
235 (newmode & A_INVIS) != 0,
236 (newmode & A_PROTECT) != 0,
237 (newmode & A_ALTCHARSET) != 0), 1, outc);
238 PreviousAttr &= ALL_BUT_COLOR;
240 SetColorsIf((pair != 0) || fix_pair0, PreviousAttr);
243 TR(TRACE_ATTRS, ("turning %s off", _traceattr(turn_off)));
245 TurnOff(A_ALTCHARSET, exit_alt_charset_mode);
247 if (!SP_PARM || SP_PARM->_use_rmul) {
248 TurnOff(A_UNDERLINE, exit_underline_mode);
251 if (!SP_PARM || SP_PARM->_use_rmso) {
252 TurnOff(A_STANDOUT, exit_standout_mode);
255 if (turn_off && exit_attribute_mode) {
256 doPut(exit_attribute_mode);
257 turn_on |= (newmode & ALL_BUT_COLOR);
258 PreviousAttr &= ALL_BUT_COLOR;
260 SetColorsIf((pair != 0) || fix_pair0, PreviousAttr);
262 TR(TRACE_ATTRS, ("turning %s on", _traceattr(turn_on)));
264 TurnOn(A_ALTCHARSET, enter_alt_charset_mode);
265 TurnOn(A_BLINK, enter_blink_mode);
266 TurnOn(A_BOLD, enter_bold_mode);
267 TurnOn(A_DIM, enter_dim_mode);
268 TurnOn(A_REVERSE, enter_reverse_mode);
269 TurnOn(A_STANDOUT, enter_standout_mode);
270 TurnOn(A_PROTECT, enter_protected_mode);
271 TurnOn(A_INVIS, enter_secure_mode);
272 TurnOn(A_UNDERLINE, enter_underline_mode);
273 #if USE_WIDEC_SUPPORT
274 TurnOn(A_HORIZONTAL, enter_horizontal_hl_mode);
275 TurnOn(A_LEFT, enter_left_hl_mode);
276 TurnOn(A_LOW, enter_low_hl_mode);
277 TurnOn(A_RIGHT, enter_right_hl_mode);
278 TurnOn(A_TOP, enter_top_hl_mode);
279 TurnOn(A_VERTICAL, enter_vertical_hl_mode);
286 newmode |= A_REVERSE;
289 SetAttr(SCREEN_ATTRS(SP_PARM), newmode);
291 PreviousAttr = newmode;
297 NCURSES_SP_NAME(vidattr) (NCURSES_SP_DCLx
300 return NCURSES_SP_NAME(vidputs) (NCURSES_SP_ARGx newmode, _nc_outch);
305 vidputs(chtype newmode, int (*outc) (int))
307 return NCURSES_SP_NAME(vidputs) (CURRENT_SCREEN, newmode, outc);
311 vidattr(chtype newmode)
313 T((T_CALLED("vidattr(%s)"), _traceattr(newmode)));
315 returnCode(vidputs(newmode, _nc_outch));
319 NCURSES_EXPORT(chtype)
320 NCURSES_SP_NAME(termattrs) (NCURSES_SP_DCL0)
322 chtype attrs = A_NORMAL;
324 T((T_CALLED("termattrs()")));
325 if (enter_alt_charset_mode)
326 attrs |= A_ALTCHARSET;
328 if (enter_blink_mode)
337 if (enter_reverse_mode)
340 if (enter_standout_mode)
343 if (enter_protected_mode)
346 if (enter_secure_mode)
349 if (enter_underline_mode)
350 attrs |= A_UNDERLINE;
352 if (SP_PARM->_coloron)
359 NCURSES_EXPORT(chtype)
362 return NCURSES_SP_NAME(termattrs) (CURRENT_SCREEN);