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 #define CUR SP_TERMTYPE
72 MODULE_ID("$Id: lib_vidattr.c,v 1.59 2009/10/24 22:12:21 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 = PAIR_NUMBER(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 \
93 old_pair, pair, reverse, outc); \
97 #define PreviousAttr _nc_prescreen.previous_attr
100 NCURSES_SP_NAME(vidputs) (NCURSES_SP_DCLx
102 NCURSES_SP_OUTC outc)
104 attr_t turn_on, turn_off;
106 bool reverse = FALSE;
107 bool can_color = (SP_PARM == 0 || SP_PARM->_coloron);
108 #if NCURSES_EXT_FUNCS
109 bool fix_pair0 = (SP_PARM != 0 && SP_PARM->_coloron && !SP_PARM->_default_color);
111 #define fix_pair0 FALSE
114 newmode &= A_ATTRIBUTES;
116 T((T_CALLED("vidputs(%p,%s)"), (void *) SP_PARM, _traceattr(newmode)));
118 if (!IsTermInfo(SP_PARM))
121 /* this allows us to go on whether or not newterm() has been called */
123 PreviousAttr = AttrOf(SCREEN_ATTRS(SP_PARM));
125 TR(TRACE_ATTRS, ("previous attribute was %s", _traceattr(PreviousAttr)));
128 && (magic_cookie_glitch > 0)) {
130 static const chtype table[] =
143 int limit = (max_attributes <= 0) ? 1 : max_attributes;
147 * Limit the number of attribute bits set in the newmode according to
148 * the terminfo max_attributes value.
150 for (n = 0; n < SIZEOF(table); ++n) {
151 if ((table[n] & SP_PARM->_ok_attributes) == 0) {
152 newmode &= ~table[n];
153 } else if ((table[n] & newmode) != 0) {
154 if (used++ >= limit) {
155 newmode &= ~table[n];
156 if (newmode == retain)
164 newmode &= ~(SP_PARM->_xmc_suppress);
166 TR(TRACE_ATTRS, ("suppressed attribute is %s", _traceattr(newmode)));
170 * If we have a terminal that cannot combine color with video
171 * attributes, use the colors in preference.
173 if (((newmode & A_COLOR) != 0
175 && (no_color_video > 0)) {
177 * If we had chosen the A_xxx definitions to correspond to the
178 * no_color_video mask, we could simply shift it up and mask off the
179 * attributes. But we did not (actually copied Solaris' definitions).
180 * However, this is still simpler/faster than a lookup table.
182 * The 63 corresponds to A_STANDOUT, A_UNDERLINE, A_REVERSE, A_BLINK,
183 * A_DIM, A_BOLD which are 1:1 with no_color_video. The bits that
184 * correspond to A_INVIS, A_PROTECT (192) must be shifted up 1 and
185 * A_ALTCHARSET (256) down 2 to line up. We use the NCURSES_BITS
186 * macro so this will work properly for the wide-character layout.
188 unsigned value = no_color_video;
189 attr_t mask = NCURSES_BITS((value & 63)
190 | ((value & 192) << 1)
191 | ((value & 256) >> 2), 8);
193 if ((mask & A_REVERSE) != 0
194 && (newmode & A_REVERSE) != 0) {
201 if (newmode == PreviousAttr)
204 pair = PAIR_NUMBER(newmode);
207 newmode &= ~A_REVERSE;
210 turn_off = (~newmode & PreviousAttr) & ALL_BUT_COLOR;
211 turn_on = (newmode & ~PreviousAttr) & ALL_BUT_COLOR;
213 SetColorsIf(((pair == 0) && !fix_pair0), PreviousAttr);
215 if (newmode == A_NORMAL) {
216 if ((PreviousAttr & A_ALTCHARSET) && exit_alt_charset_mode) {
217 doPut(exit_alt_charset_mode);
218 PreviousAttr &= ~A_ALTCHARSET;
221 if (exit_attribute_mode) {
222 doPut(exit_attribute_mode);
224 if (!SP_PARM || SP_PARM->_use_rmul) {
225 TurnOff(A_UNDERLINE, exit_underline_mode);
227 if (!SP_PARM || SP_PARM->_use_rmso) {
228 TurnOff(A_STANDOUT, exit_standout_mode);
231 PreviousAttr &= ALL_BUT_COLOR;
234 SetColorsIf((pair != 0) || fix_pair0, PreviousAttr);
235 } else if (set_attributes) {
236 if (turn_on || turn_off) {
237 TPUTS_TRACE("set_attributes");
238 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
239 tparm(set_attributes,
240 (newmode & A_STANDOUT) != 0,
241 (newmode & A_UNDERLINE) != 0,
242 (newmode & A_REVERSE) != 0,
243 (newmode & A_BLINK) != 0,
244 (newmode & A_DIM) != 0,
245 (newmode & A_BOLD) != 0,
246 (newmode & A_INVIS) != 0,
247 (newmode & A_PROTECT) != 0,
248 (newmode & A_ALTCHARSET) != 0),
250 PreviousAttr &= ALL_BUT_COLOR;
252 SetColorsIf((pair != 0) || fix_pair0, PreviousAttr);
255 TR(TRACE_ATTRS, ("turning %s off", _traceattr(turn_off)));
257 TurnOff(A_ALTCHARSET, exit_alt_charset_mode);
259 if (!SP_PARM || SP_PARM->_use_rmul) {
260 TurnOff(A_UNDERLINE, exit_underline_mode);
263 if (!SP_PARM || SP_PARM->_use_rmso) {
264 TurnOff(A_STANDOUT, exit_standout_mode);
267 if (turn_off && exit_attribute_mode) {
268 doPut(exit_attribute_mode);
269 turn_on |= (newmode & ALL_BUT_COLOR);
270 PreviousAttr &= ALL_BUT_COLOR;
272 SetColorsIf((pair != 0) || fix_pair0, PreviousAttr);
274 TR(TRACE_ATTRS, ("turning %s on", _traceattr(turn_on)));
276 TurnOn(A_ALTCHARSET, enter_alt_charset_mode);
277 TurnOn(A_BLINK, enter_blink_mode);
278 TurnOn(A_BOLD, enter_bold_mode);
279 TurnOn(A_DIM, enter_dim_mode);
280 TurnOn(A_REVERSE, enter_reverse_mode);
281 TurnOn(A_STANDOUT, enter_standout_mode);
282 TurnOn(A_PROTECT, enter_protected_mode);
283 TurnOn(A_INVIS, enter_secure_mode);
284 TurnOn(A_UNDERLINE, enter_underline_mode);
285 #if USE_WIDEC_SUPPORT
286 TurnOn(A_HORIZONTAL, enter_horizontal_hl_mode);
287 TurnOn(A_LEFT, enter_left_hl_mode);
288 TurnOn(A_LOW, enter_low_hl_mode);
289 TurnOn(A_RIGHT, enter_right_hl_mode);
290 TurnOn(A_TOP, enter_top_hl_mode);
291 TurnOn(A_VERTICAL, enter_vertical_hl_mode);
298 newmode |= A_REVERSE;
301 SetAttr(SCREEN_ATTRS(SP_PARM), newmode);
303 PreviousAttr = newmode;
310 vidputs(chtype newmode, NCURSES_OUTC outc)
312 SetSafeOutcWrapper(outc);
313 return NCURSES_SP_NAME(vidputs) (CURRENT_SCREEN,
320 NCURSES_SP_NAME(vidattr) (NCURSES_SP_DCLx chtype newmode)
322 T((T_CALLED("vidattr(%p,%s)"), (void *) SP_PARM, _traceattr(newmode)));
323 returnCode(NCURSES_SP_NAME(vidputs) (NCURSES_SP_ARGx
325 NCURSES_SP_NAME(_nc_outch)));
330 vidattr(chtype newmode)
332 return NCURSES_SP_NAME(vidattr) (CURRENT_SCREEN, newmode);
336 NCURSES_EXPORT(chtype)
337 NCURSES_SP_NAME(termattrs) (NCURSES_SP_DCL0)
339 chtype attrs = A_NORMAL;
341 T((T_CALLED("termattrs(%p)"), (void *) SP_PARM));
342 #ifdef USE_TERM_DRIVER
343 if (HasTerminal(SP_PARM))
344 attrs = CallDriver(SP_PARM, conattr);
347 if (enter_alt_charset_mode)
348 attrs |= A_ALTCHARSET;
350 if (enter_blink_mode)
359 if (enter_reverse_mode)
362 if (enter_standout_mode)
365 if (enter_protected_mode)
368 if (enter_secure_mode)
371 if (enter_underline_mode)
372 attrs |= A_UNDERLINE;
374 if (SP_PARM->_coloron)
382 NCURSES_EXPORT(chtype)
385 return NCURSES_SP_NAME(termattrs) (CURRENT_SCREEN);