libncurses: updated to 6.0
[tomato.git] / release / src / router / libncurses / ncurses / tty / lib_vidattr.c
blob184d9b976d5492f65789b2af606d8a3e5ad41203
1 /****************************************************************************
2 * Copyright (c) 1998-2013,2014 Free Software Foundation, Inc. *
3 * *
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: *
11 * *
12 * The above copyright notice and this permission notice shall be included *
13 * in all copies or substantial portions of the Software. *
14 * *
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. *
22 * *
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 *
26 * authorization. *
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
45 * emit it
46 * emit exit-attribute-mode
47 * else if set-attributes exists
48 * use it to set exactly what you want
49 * else
50 * if exit-attribute-mode exists
51 * turn off everything
52 * else
53 * turn off those which can be turned off and aren't in
54 * newmode.
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>
68 #ifndef CUR
69 #define CUR SP_TERMTYPE
70 #endif
72 MODULE_ID("$Id: lib_vidattr.c,v 1.71 2014/09/04 22:01:27 tom Exp $")
74 #define doPut(mode) \
75 TPUTS_TRACE(#mode); \
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 \
93 (short) old_pair, \
94 (short) pair, \
95 reverse, outc); \
96 } \
99 #define PreviousAttr _nc_prescreen.previous_attr
101 NCURSES_EXPORT(int)
102 NCURSES_SP_NAME(vidputs) (NCURSES_SP_DCLx
103 chtype newmode,
104 NCURSES_SP_OUTC outc)
106 attr_t turn_on, turn_off;
107 int pair;
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);
112 #else
113 #define fix_pair0 FALSE
114 #endif
116 newmode &= A_ATTRIBUTES;
118 T((T_CALLED("vidputs(%p,%s)"), (void *) SP_PARM, _traceattr(newmode)));
120 if (!IsTermInfo(SP_PARM))
121 returnCode(ERR);
123 /* this allows us to go on whether or not newterm() has been called */
124 if (SP_PARM)
125 PreviousAttr = AttrOf(SCREEN_ATTRS(SP_PARM));
127 TR(TRACE_ATTRS, ("previous attribute was %s", _traceattr(PreviousAttr)));
129 if ((SP_PARM != 0)
130 && (magic_cookie_glitch > 0)) {
131 #if USE_XMC_SUPPORT
132 static const chtype table[] =
134 A_STANDOUT,
135 A_UNDERLINE,
136 A_REVERSE,
137 A_BLINK,
138 A_DIM,
139 A_BOLD,
140 A_INVIS,
141 A_PROTECT,
142 #if USE_ITALIC
143 A_ITALIC,
144 #endif
146 unsigned n;
147 int used = 0;
148 int limit = (max_attributes <= 0) ? 1 : max_attributes;
149 chtype retain = 0;
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)
162 break;
163 } else {
164 retain = newmode;
168 #else
169 newmode &= ~(SP_PARM->_xmc_suppress);
170 #endif
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
179 || fix_pair0)
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) {
200 reverse = TRUE;
201 mask &= ~A_REVERSE;
203 newmode &= ~mask;
206 if (newmode == PreviousAttr)
207 returnCode(OK);
209 pair = PairNumber(newmode);
211 if (reverse) {
212 newmode &= ~A_REVERSE;
215 turn_off = (~newmode & PreviousAttr) & ALL_BUT_COLOR;
216 turn_on = (newmode & ~(PreviousAttr & TPARM_ATTR)) & 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;
225 if (PreviousAttr) {
226 if (exit_attribute_mode) {
227 doPut(exit_attribute_mode);
228 } else {
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);
235 #if USE_ITALIC
236 if (!SP_PARM || SP_PARM->_use_ritm) {
237 TurnOff(A_ITALIC, exit_italics_mode);
239 #endif
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),
259 1, outc);
260 PreviousAttr &= ALL_BUT_COLOR;
262 #if USE_ITALIC
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);
270 #endif
271 SetColorsIf((pair != 0) || fix_pair0, PreviousAttr);
272 } else {
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);
285 #if USE_ITALIC
286 if (!SP_PARM || SP_PARM->_use_ritm) {
287 TurnOff(A_ITALIC, exit_italics_mode);
289 #endif
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)));
298 /* *INDENT-OFF* */
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);
308 #if USE_ITALIC
309 TurnOn(A_ITALIC, enter_italics_mode);
310 #endif
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);
318 #endif
319 /* *INDENT-ON* */
323 if (reverse)
324 newmode |= A_REVERSE;
326 if (SP_PARM)
327 SetAttr(SCREEN_ATTRS(SP_PARM), newmode);
328 else
329 PreviousAttr = newmode;
331 returnCode(OK);
334 #if NCURSES_SP_FUNCS
335 NCURSES_EXPORT(int)
336 vidputs(chtype newmode, NCURSES_OUTC outc)
338 SetSafeOutcWrapper(outc);
339 return NCURSES_SP_NAME(vidputs) (CURRENT_SCREEN,
340 newmode,
341 _nc_outc_wrapper);
343 #endif
345 NCURSES_EXPORT(int)
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
350 newmode,
351 NCURSES_SP_NAME(_nc_putchar)));
354 #if NCURSES_SP_FUNCS
355 NCURSES_EXPORT(int)
356 vidattr(chtype newmode)
358 return NCURSES_SP_NAME(vidattr) (CURRENT_SCREEN, newmode);
360 #endif
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, td_conattr);
372 #else /* ! USE_TERM_DRIVER */
374 if (enter_alt_charset_mode)
375 attrs |= A_ALTCHARSET;
377 if (enter_blink_mode)
378 attrs |= A_BLINK;
380 if (enter_bold_mode)
381 attrs |= A_BOLD;
383 if (enter_dim_mode)
384 attrs |= A_DIM;
386 if (enter_reverse_mode)
387 attrs |= A_REVERSE;
389 if (enter_standout_mode)
390 attrs |= A_STANDOUT;
392 if (enter_protected_mode)
393 attrs |= A_PROTECT;
395 if (enter_secure_mode)
396 attrs |= A_INVIS;
398 if (enter_underline_mode)
399 attrs |= A_UNDERLINE;
401 if (SP_PARM->_coloron)
402 attrs |= A_COLOR;
404 #if USE_ITALIC
405 if (enter_italics_mode)
406 attrs |= A_ITALIC;
407 #endif
409 #endif /* USE_TERM_DRIVER */
411 returnChtype(attrs);
414 #if NCURSES_SP_FUNCS
415 NCURSES_EXPORT(chtype)
416 termattrs(void)
418 return NCURSES_SP_NAME(termattrs) (CURRENT_SCREEN);
420 #endif