missing ncurses sources
[tomato.git] / release / src / router / libncurses / ncurses / tty / lib_vidattr.c
blob9656b3c70ee6fde5ab4fe850b4f229c20e3e286b
1 /****************************************************************************
2 * Copyright (c) 1998-2009,2010 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.61 2010/06/05 22:22:04 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,
143 unsigned n;
144 int used = 0;
145 int limit = (max_attributes <= 0) ? 1 : max_attributes;
146 chtype retain = 0;
149 * Limit the number of attribute bits set in the newmode according to
150 * the terminfo max_attributes value.
152 for (n = 0; n < SIZEOF(table); ++n) {
153 if ((table[n] & SP_PARM->_ok_attributes) == 0) {
154 newmode &= ~table[n];
155 } else if ((table[n] & newmode) != 0) {
156 if (used++ >= limit) {
157 newmode &= ~table[n];
158 if (newmode == retain)
159 break;
160 } else {
161 retain = newmode;
165 #else
166 newmode &= ~(SP_PARM->_xmc_suppress);
167 #endif
168 TR(TRACE_ATTRS, ("suppressed attribute is %s", _traceattr(newmode)));
172 * If we have a terminal that cannot combine color with video
173 * attributes, use the colors in preference.
175 if (((newmode & A_COLOR) != 0
176 || fix_pair0)
177 && (no_color_video > 0)) {
179 * If we had chosen the A_xxx definitions to correspond to the
180 * no_color_video mask, we could simply shift it up and mask off the
181 * attributes. But we did not (actually copied Solaris' definitions).
182 * However, this is still simpler/faster than a lookup table.
184 * The 63 corresponds to A_STANDOUT, A_UNDERLINE, A_REVERSE, A_BLINK,
185 * A_DIM, A_BOLD which are 1:1 with no_color_video. The bits that
186 * correspond to A_INVIS, A_PROTECT (192) must be shifted up 1 and
187 * A_ALTCHARSET (256) down 2 to line up. We use the NCURSES_BITS
188 * macro so this will work properly for the wide-character layout.
190 unsigned value = (unsigned) no_color_video;
191 attr_t mask = NCURSES_BITS((value & 63)
192 | ((value & 192) << 1)
193 | ((value & 256) >> 2), 8);
195 if ((mask & A_REVERSE) != 0
196 && (newmode & A_REVERSE) != 0) {
197 reverse = TRUE;
198 mask &= ~A_REVERSE;
200 newmode &= ~mask;
203 if (newmode == PreviousAttr)
204 returnCode(OK);
206 pair = PairNumber(newmode);
208 if (reverse) {
209 newmode &= ~A_REVERSE;
212 turn_off = (~newmode & PreviousAttr) & ALL_BUT_COLOR;
213 turn_on = (newmode & ~PreviousAttr) & ALL_BUT_COLOR;
215 SetColorsIf(((pair == 0) && !fix_pair0), PreviousAttr);
217 if (newmode == A_NORMAL) {
218 if ((PreviousAttr & A_ALTCHARSET) && exit_alt_charset_mode) {
219 doPut(exit_alt_charset_mode);
220 PreviousAttr &= ~A_ALTCHARSET;
222 if (PreviousAttr) {
223 if (exit_attribute_mode) {
224 doPut(exit_attribute_mode);
225 } else {
226 if (!SP_PARM || SP_PARM->_use_rmul) {
227 TurnOff(A_UNDERLINE, exit_underline_mode);
229 if (!SP_PARM || SP_PARM->_use_rmso) {
230 TurnOff(A_STANDOUT, exit_standout_mode);
233 PreviousAttr &= ALL_BUT_COLOR;
236 SetColorsIf((pair != 0) || fix_pair0, PreviousAttr);
237 } else if (set_attributes) {
238 if (turn_on || turn_off) {
239 TPUTS_TRACE("set_attributes");
240 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
241 tparm(set_attributes,
242 (newmode & A_STANDOUT) != 0,
243 (newmode & A_UNDERLINE) != 0,
244 (newmode & A_REVERSE) != 0,
245 (newmode & A_BLINK) != 0,
246 (newmode & A_DIM) != 0,
247 (newmode & A_BOLD) != 0,
248 (newmode & A_INVIS) != 0,
249 (newmode & A_PROTECT) != 0,
250 (newmode & A_ALTCHARSET) != 0),
251 1, outc);
252 PreviousAttr &= ALL_BUT_COLOR;
254 SetColorsIf((pair != 0) || fix_pair0, PreviousAttr);
255 } else {
257 TR(TRACE_ATTRS, ("turning %s off", _traceattr(turn_off)));
259 TurnOff(A_ALTCHARSET, exit_alt_charset_mode);
261 if (!SP_PARM || SP_PARM->_use_rmul) {
262 TurnOff(A_UNDERLINE, exit_underline_mode);
265 if (!SP_PARM || SP_PARM->_use_rmso) {
266 TurnOff(A_STANDOUT, exit_standout_mode);
269 if (turn_off && exit_attribute_mode) {
270 doPut(exit_attribute_mode);
271 turn_on |= (newmode & ALL_BUT_COLOR);
272 PreviousAttr &= ALL_BUT_COLOR;
274 SetColorsIf((pair != 0) || fix_pair0, PreviousAttr);
276 TR(TRACE_ATTRS, ("turning %s on", _traceattr(turn_on)));
277 /* *INDENT-OFF* */
278 TurnOn(A_ALTCHARSET, enter_alt_charset_mode);
279 TurnOn(A_BLINK, enter_blink_mode);
280 TurnOn(A_BOLD, enter_bold_mode);
281 TurnOn(A_DIM, enter_dim_mode);
282 TurnOn(A_REVERSE, enter_reverse_mode);
283 TurnOn(A_STANDOUT, enter_standout_mode);
284 TurnOn(A_PROTECT, enter_protected_mode);
285 TurnOn(A_INVIS, enter_secure_mode);
286 TurnOn(A_UNDERLINE, enter_underline_mode);
287 #if USE_WIDEC_SUPPORT
288 TurnOn(A_HORIZONTAL, enter_horizontal_hl_mode);
289 TurnOn(A_LEFT, enter_left_hl_mode);
290 TurnOn(A_LOW, enter_low_hl_mode);
291 TurnOn(A_RIGHT, enter_right_hl_mode);
292 TurnOn(A_TOP, enter_top_hl_mode);
293 TurnOn(A_VERTICAL, enter_vertical_hl_mode);
294 #endif
295 /* *INDENT-ON* */
299 if (reverse)
300 newmode |= A_REVERSE;
302 if (SP_PARM)
303 SetAttr(SCREEN_ATTRS(SP_PARM), newmode);
304 else
305 PreviousAttr = newmode;
307 returnCode(OK);
310 #if NCURSES_SP_FUNCS
311 NCURSES_EXPORT(int)
312 vidputs(chtype newmode, NCURSES_OUTC outc)
314 SetSafeOutcWrapper(outc);
315 return NCURSES_SP_NAME(vidputs) (CURRENT_SCREEN,
316 newmode,
317 _nc_outc_wrapper);
319 #endif
321 NCURSES_EXPORT(int)
322 NCURSES_SP_NAME(vidattr) (NCURSES_SP_DCLx chtype newmode)
324 T((T_CALLED("vidattr(%p,%s)"), (void *) SP_PARM, _traceattr(newmode)));
325 returnCode(NCURSES_SP_NAME(vidputs) (NCURSES_SP_ARGx
326 newmode,
327 NCURSES_SP_NAME(_nc_outch)));
330 #if NCURSES_SP_FUNCS
331 NCURSES_EXPORT(int)
332 vidattr(chtype newmode)
334 return NCURSES_SP_NAME(vidattr) (CURRENT_SCREEN, newmode);
336 #endif
338 NCURSES_EXPORT(chtype)
339 NCURSES_SP_NAME(termattrs) (NCURSES_SP_DCL0)
341 chtype attrs = A_NORMAL;
343 T((T_CALLED("termattrs(%p)"), (void *) SP_PARM));
344 #ifdef USE_TERM_DRIVER
345 if (HasTerminal(SP_PARM))
346 attrs = CallDriver(SP_PARM, conattr);
347 #else
349 if (enter_alt_charset_mode)
350 attrs |= A_ALTCHARSET;
352 if (enter_blink_mode)
353 attrs |= A_BLINK;
355 if (enter_bold_mode)
356 attrs |= A_BOLD;
358 if (enter_dim_mode)
359 attrs |= A_DIM;
361 if (enter_reverse_mode)
362 attrs |= A_REVERSE;
364 if (enter_standout_mode)
365 attrs |= A_STANDOUT;
367 if (enter_protected_mode)
368 attrs |= A_PROTECT;
370 if (enter_secure_mode)
371 attrs |= A_INVIS;
373 if (enter_underline_mode)
374 attrs |= A_UNDERLINE;
376 if (SP_PARM->_coloron)
377 attrs |= A_COLOR;
379 #endif
380 returnChtype(attrs);
383 #if NCURSES_SP_FUNCS
384 NCURSES_EXPORT(chtype)
385 termattrs(void)
387 return NCURSES_SP_NAME(termattrs) (CURRENT_SCREEN);
389 #endif