blob: 184d9b976d5492f65789b2af606d8a3e5ad41203 [file] [log] [blame]
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05301/****************************************************************************
Steve Kondikae271bc2015-11-15 02:50:53 +01002 * Copyright (c) 1998-2013,2014 Free Software Foundation, Inc. *
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +05303 * *
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 ****************************************************************************/
28
29/****************************************************************************
30 * Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995 *
31 * and: Eric S. Raymond <esr@snark.thyrsus.com> *
Steve Kondikae271bc2015-11-15 02:50:53 +010032 * and: Thomas E. Dickey 1996-on *
33 * and: Juergen Pfeifer 2009 *
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053034 ****************************************************************************/
35
36/*
37 * vidputs(newmode, outc)
38 *
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.
42 *
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
56 *
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...
60 *
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.
64 */
65
66#include <curses.priv.h>
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053067
Steve Kondikae271bc2015-11-15 02:50:53 +010068#ifndef CUR
69#define CUR SP_TERMTYPE
70#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053071
Steve Kondikae271bc2015-11-15 02:50:53 +010072MODULE_ID("$Id: lib_vidattr.c,v 1.71 2014/09/04 22:01:27 tom Exp $")
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053073
Steve Kondikae271bc2015-11-15 02:50:53 +010074#define doPut(mode) \
75 TPUTS_TRACE(#mode); \
76 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx mode, 1, outc)
77
78#define TurnOn(mask, mode) \
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053079 if ((turn_on & mask) && mode) { doPut(mode); }
80
Steve Kondikae271bc2015-11-15 02:50:53 +010081#define TurnOff(mask, mode) \
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053082 if ((turn_off & mask) && mode) { doPut(mode); turn_off &= ~mask; }
83
84 /* if there is no current screen, assume we *can* do color */
Steve Kondikae271bc2015-11-15 02:50:53 +010085#define SetColorsIf(why, old_attr) \
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053086 if (can_color && (why)) { \
Steve Kondikae271bc2015-11-15 02:50:53 +010087 int old_pair = PairNumber(old_attr); \
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053088 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))) { \
Steve Kondikae271bc2015-11-15 02:50:53 +010092 NCURSES_SP_NAME(_nc_do_color) (NCURSES_SP_ARGx \
93 (short) old_pair, \
94 (short) pair, \
95 reverse, outc); \
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +053096 } \
97 }
98
99#define PreviousAttr _nc_prescreen.previous_attr
100
101NCURSES_EXPORT(int)
Steve Kondikae271bc2015-11-15 02:50:53 +0100102NCURSES_SP_NAME(vidputs) (NCURSES_SP_DCLx
103 chtype newmode,
104 NCURSES_SP_OUTC outc)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530105{
106 attr_t turn_on, turn_off;
107 int pair;
108 bool reverse = FALSE;
Steve Kondikae271bc2015-11-15 02:50:53 +0100109 bool can_color = (SP_PARM == 0 || SP_PARM->_coloron);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530110#if NCURSES_EXT_FUNCS
Steve Kondikae271bc2015-11-15 02:50:53 +0100111 bool fix_pair0 = (SP_PARM != 0 && SP_PARM->_coloron && !SP_PARM->_default_color);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530112#else
113#define fix_pair0 FALSE
114#endif
115
116 newmode &= A_ATTRIBUTES;
Steve Kondikae271bc2015-11-15 02:50:53 +0100117
118 T((T_CALLED("vidputs(%p,%s)"), (void *) SP_PARM, _traceattr(newmode)));
119
120 if (!IsTermInfo(SP_PARM))
121 returnCode(ERR);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530122
123 /* this allows us to go on whether or not newterm() has been called */
Steve Kondikae271bc2015-11-15 02:50:53 +0100124 if (SP_PARM)
125 PreviousAttr = AttrOf(SCREEN_ATTRS(SP_PARM));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530126
127 TR(TRACE_ATTRS, ("previous attribute was %s", _traceattr(PreviousAttr)));
128
Steve Kondikae271bc2015-11-15 02:50:53 +0100129 if ((SP_PARM != 0)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530130 && (magic_cookie_glitch > 0)) {
131#if USE_XMC_SUPPORT
132 static const chtype table[] =
133 {
134 A_STANDOUT,
135 A_UNDERLINE,
136 A_REVERSE,
137 A_BLINK,
138 A_DIM,
139 A_BOLD,
140 A_INVIS,
141 A_PROTECT,
Steve Kondikae271bc2015-11-15 02:50:53 +0100142#if USE_ITALIC
143 A_ITALIC,
144#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530145 };
146 unsigned n;
147 int used = 0;
148 int limit = (max_attributes <= 0) ? 1 : max_attributes;
149 chtype retain = 0;
150
151 /*
152 * Limit the number of attribute bits set in the newmode according to
153 * the terminfo max_attributes value.
154 */
155 for (n = 0; n < SIZEOF(table); ++n) {
Steve Kondikae271bc2015-11-15 02:50:53 +0100156 if ((table[n] & SP_PARM->_ok_attributes) == 0) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530157 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;
165 }
166 }
167 }
168#else
Steve Kondikae271bc2015-11-15 02:50:53 +0100169 newmode &= ~(SP_PARM->_xmc_suppress);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530170#endif
171 TR(TRACE_ATTRS, ("suppressed attribute is %s", _traceattr(newmode)));
172 }
173
174 /*
175 * If we have a terminal that cannot combine color with video
176 * attributes, use the colors in preference.
177 */
178 if (((newmode & A_COLOR) != 0
179 || fix_pair0)
180 && (no_color_video > 0)) {
181 /*
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.
186 *
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.
192 */
Steve Kondikae271bc2015-11-15 02:50:53 +0100193 unsigned value = (unsigned) no_color_video;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530194 attr_t mask = NCURSES_BITS((value & 63)
195 | ((value & 192) << 1)
196 | ((value & 256) >> 2), 8);
197
198 if ((mask & A_REVERSE) != 0
199 && (newmode & A_REVERSE) != 0) {
200 reverse = TRUE;
201 mask &= ~A_REVERSE;
202 }
203 newmode &= ~mask;
204 }
205
206 if (newmode == PreviousAttr)
207 returnCode(OK);
208
Steve Kondikae271bc2015-11-15 02:50:53 +0100209 pair = PairNumber(newmode);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530210
211 if (reverse) {
212 newmode &= ~A_REVERSE;
213 }
214
215 turn_off = (~newmode & PreviousAttr) & ALL_BUT_COLOR;
Steve Kondikae271bc2015-11-15 02:50:53 +0100216 turn_on = (newmode & ~(PreviousAttr & TPARM_ATTR)) & ALL_BUT_COLOR;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530217
218 SetColorsIf(((pair == 0) && !fix_pair0), PreviousAttr);
219
220 if (newmode == A_NORMAL) {
221 if ((PreviousAttr & A_ALTCHARSET) && exit_alt_charset_mode) {
222 doPut(exit_alt_charset_mode);
223 PreviousAttr &= ~A_ALTCHARSET;
224 }
225 if (PreviousAttr) {
226 if (exit_attribute_mode) {
227 doPut(exit_attribute_mode);
228 } else {
Steve Kondikae271bc2015-11-15 02:50:53 +0100229 if (!SP_PARM || SP_PARM->_use_rmul) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530230 TurnOff(A_UNDERLINE, exit_underline_mode);
231 }
Steve Kondikae271bc2015-11-15 02:50:53 +0100232 if (!SP_PARM || SP_PARM->_use_rmso) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530233 TurnOff(A_STANDOUT, exit_standout_mode);
234 }
Steve Kondikae271bc2015-11-15 02:50:53 +0100235#if USE_ITALIC
236 if (!SP_PARM || SP_PARM->_use_ritm) {
237 TurnOff(A_ITALIC, exit_italics_mode);
238 }
239#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530240 }
241 PreviousAttr &= ALL_BUT_COLOR;
242 }
243
244 SetColorsIf((pair != 0) || fix_pair0, PreviousAttr);
245 } else if (set_attributes) {
246 if (turn_on || turn_off) {
247 TPUTS_TRACE("set_attributes");
Steve Kondikae271bc2015-11-15 02:50:53 +0100248 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);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530260 PreviousAttr &= ALL_BUT_COLOR;
261 }
Steve Kondikae271bc2015-11-15 02:50:53 +0100262#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);
268 }
269 }
270#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530271 SetColorsIf((pair != 0) || fix_pair0, PreviousAttr);
272 } else {
273
274 TR(TRACE_ATTRS, ("turning %s off", _traceattr(turn_off)));
275
276 TurnOff(A_ALTCHARSET, exit_alt_charset_mode);
277
Steve Kondikae271bc2015-11-15 02:50:53 +0100278 if (!SP_PARM || SP_PARM->_use_rmul) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530279 TurnOff(A_UNDERLINE, exit_underline_mode);
280 }
281
Steve Kondikae271bc2015-11-15 02:50:53 +0100282 if (!SP_PARM || SP_PARM->_use_rmso) {
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530283 TurnOff(A_STANDOUT, exit_standout_mode);
284 }
Steve Kondikae271bc2015-11-15 02:50:53 +0100285#if USE_ITALIC
286 if (!SP_PARM || SP_PARM->_use_ritm) {
287 TurnOff(A_ITALIC, exit_italics_mode);
288 }
289#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530290 if (turn_off && exit_attribute_mode) {
291 doPut(exit_attribute_mode);
292 turn_on |= (newmode & ALL_BUT_COLOR);
293 PreviousAttr &= ALL_BUT_COLOR;
294 }
295 SetColorsIf((pair != 0) || fix_pair0, PreviousAttr);
296
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);
Steve Kondikae271bc2015-11-15 02:50:53 +0100308#if USE_ITALIC
309 TurnOn(A_ITALIC, enter_italics_mode);
310#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530311#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* */
320
321 }
322
323 if (reverse)
324 newmode |= A_REVERSE;
325
Steve Kondikae271bc2015-11-15 02:50:53 +0100326 if (SP_PARM)
327 SetAttr(SCREEN_ATTRS(SP_PARM), newmode);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530328 else
329 PreviousAttr = newmode;
330
331 returnCode(OK);
332}
333
Steve Kondikae271bc2015-11-15 02:50:53 +0100334#if NCURSES_SP_FUNCS
335NCURSES_EXPORT(int)
336vidputs(chtype newmode, NCURSES_OUTC outc)
337{
338 SetSafeOutcWrapper(outc);
339 return NCURSES_SP_NAME(vidputs) (CURRENT_SCREEN,
340 newmode,
341 _nc_outc_wrapper);
342}
343#endif
344
345NCURSES_EXPORT(int)
346NCURSES_SP_NAME(vidattr) (NCURSES_SP_DCLx chtype newmode)
347{
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)));
352}
353
354#if NCURSES_SP_FUNCS
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530355NCURSES_EXPORT(int)
356vidattr(chtype newmode)
357{
Steve Kondikae271bc2015-11-15 02:50:53 +0100358 return NCURSES_SP_NAME(vidattr) (CURRENT_SCREEN, newmode);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530359}
Steve Kondikae271bc2015-11-15 02:50:53 +0100360#endif
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530361
362NCURSES_EXPORT(chtype)
Steve Kondikae271bc2015-11-15 02:50:53 +0100363NCURSES_SP_NAME(termattrs) (NCURSES_SP_DCL0)
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530364{
365 chtype attrs = A_NORMAL;
366
Steve Kondikae271bc2015-11-15 02:50:53 +0100367 T((T_CALLED("termattrs(%p)"), (void *) SP_PARM));
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530368
Steve Kondikae271bc2015-11-15 02:50:53 +0100369 if (HasTerminal(SP_PARM)) {
370#ifdef USE_TERM_DRIVER
371 attrs = CallDriver(SP_PARM, td_conattr);
372#else /* ! USE_TERM_DRIVER */
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530373
Steve Kondikae271bc2015-11-15 02:50:53 +0100374 if (enter_alt_charset_mode)
375 attrs |= A_ALTCHARSET;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530376
Steve Kondikae271bc2015-11-15 02:50:53 +0100377 if (enter_blink_mode)
378 attrs |= A_BLINK;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530379
Steve Kondikae271bc2015-11-15 02:50:53 +0100380 if (enter_bold_mode)
381 attrs |= A_BOLD;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530382
Steve Kondikae271bc2015-11-15 02:50:53 +0100383 if (enter_dim_mode)
384 attrs |= A_DIM;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530385
Steve Kondikae271bc2015-11-15 02:50:53 +0100386 if (enter_reverse_mode)
387 attrs |= A_REVERSE;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530388
Steve Kondikae271bc2015-11-15 02:50:53 +0100389 if (enter_standout_mode)
390 attrs |= A_STANDOUT;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530391
Steve Kondikae271bc2015-11-15 02:50:53 +0100392 if (enter_protected_mode)
393 attrs |= A_PROTECT;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530394
Steve Kondikae271bc2015-11-15 02:50:53 +0100395 if (enter_secure_mode)
396 attrs |= A_INVIS;
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530397
Steve Kondikae271bc2015-11-15 02:50:53 +0100398 if (enter_underline_mode)
399 attrs |= A_UNDERLINE;
400
401 if (SP_PARM->_coloron)
402 attrs |= A_COLOR;
403
404#if USE_ITALIC
405 if (enter_italics_mode)
406 attrs |= A_ITALIC;
407#endif
408
409#endif /* USE_TERM_DRIVER */
410 }
411 returnChtype(attrs);
Amit Daniel Kachhape6a01f52011-07-20 11:45:59 +0530412}
Steve Kondikae271bc2015-11-15 02:50:53 +0100413
414#if NCURSES_SP_FUNCS
415NCURSES_EXPORT(chtype)
416termattrs(void)
417{
418 return NCURSES_SP_NAME(termattrs) (CURRENT_SCREEN);
419}
420#endif