1 /****************************************************************************
2 * Copyright (c) 2002-2014,2017 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: Thomas E. Dickey *
31 ****************************************************************************/
33 #include <curses.priv.h>
36 #define CUR SP_TERMTYPE
39 MODULE_ID("$Id: lib_vid_attr.c,v 1.24 2017/03/17 00:17:32 tom Exp $")
43 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx mode, 1, outc)
45 #define TurnOn(mask, mode) \
46 if ((turn_on & mask) && mode) { doPut(mode); }
48 #define TurnOff(mask, mode) \
49 if ((turn_off & mask) && mode) { doPut(mode); turn_off &= ~mask; }
51 /* if there is no current screen, assume we *can* do color */
52 #define SetColorsIf(why, old_attr, old_pair) \
53 if (can_color && (why)) { \
54 TR(TRACE_ATTRS, ("old pair = %d -- new pair = %d", old_pair, color_pair)); \
55 if ((color_pair != old_pair) \
56 || (fix_pair0 && (color_pair == 0)) \
57 || (reverse ^ ((old_attr & A_REVERSE) != 0))) { \
58 NCURSES_SP_NAME(_nc_do_color) (NCURSES_SP_ARGx \
59 old_pair, color_pair, \
64 #define set_color(mode, pair) \
65 mode &= ALL_BUT_COLOR; \
66 mode |= (attr_t) ColorPair(pair)
69 NCURSES_SP_NAME(vid_puts) (NCURSES_SP_DCLx
71 NCURSES_PAIRS_T pair_arg,
72 void *opts OPTIONAL_PAIR,
75 int color_pair = pair_arg;
76 #if NCURSES_EXT_COLORS
77 static attr_t previous_attr = A_NORMAL;
78 static int previous_pair = 0;
80 attr_t turn_on, turn_off;
82 bool can_color = (SP_PARM == 0 || SP_PARM->_coloron);
84 bool fix_pair0 = (SP_PARM != 0 && SP_PARM->_coloron && !SP_PARM->_default_color);
86 #define fix_pair0 FALSE
89 newmode &= A_ATTRIBUTES;
90 set_extended_pair(opts, color_pair);
91 T((T_CALLED("vid_puts(%s,%d)"), _traceattr(newmode), color_pair));
93 /* this allows us to go on whether or not newterm() has been called */
95 previous_attr = AttrOf(SCREEN_ATTRS(SP_PARM));
96 previous_pair = GetPair(SCREEN_ATTRS(SP_PARM));
99 TR(TRACE_ATTRS, ("previous attribute was %s, %d",
100 _traceattr(previous_attr), previous_pair));
104 && (magic_cookie_glitch > 0))
105 newmode &= ~(SP_PARM->_xmc_suppress);
109 * If we have a terminal that cannot combine color with video
110 * attributes, use the colors in preference.
114 && (no_color_video > 0)) {
116 * If we had chosen the A_xxx definitions to correspond to the
117 * no_color_video mask, we could simply shift it up and mask off the
118 * attributes. But we did not (actually copied Solaris' definitions).
119 * However, this is still simpler/faster than a lookup table.
121 * The 63 corresponds to A_STANDOUT, A_UNDERLINE, A_REVERSE, A_BLINK,
122 * A_DIM, A_BOLD which are 1:1 with no_color_video. The bits that
123 * correspond to A_INVIS, A_PROTECT (192) must be shifted up 1 and
124 * A_ALTCHARSET (256) down 2 to line up. We use the NCURSES_BITS
125 * macro so this will work properly for the wide-character layout.
127 unsigned value = (unsigned) no_color_video;
128 attr_t mask = NCURSES_BITS((value & 63)
129 | ((value & 192) << 1)
130 | ((value & 256) >> 2), 8);
132 if ((mask & A_REVERSE) != 0
133 && (newmode & A_REVERSE) != 0) {
140 if (newmode == previous_attr
141 && color_pair == previous_pair)
145 newmode &= ~A_REVERSE;
148 turn_off = (~newmode & previous_attr) & ALL_BUT_COLOR;
149 turn_on = (newmode & ~(previous_attr & TPARM_ATTR)) & ALL_BUT_COLOR;
151 SetColorsIf(((color_pair == 0) && !fix_pair0), previous_attr, previous_pair);
153 if (newmode == A_NORMAL) {
154 if ((previous_attr & A_ALTCHARSET) && exit_alt_charset_mode) {
155 doPut(exit_alt_charset_mode);
156 previous_attr &= ~A_ALTCHARSET;
159 if (exit_attribute_mode) {
160 doPut(exit_attribute_mode);
162 if (!SP_PARM || SP_PARM->_use_rmul) {
163 TurnOff(A_UNDERLINE, exit_underline_mode);
165 if (!SP_PARM || SP_PARM->_use_rmso) {
166 TurnOff(A_STANDOUT, exit_standout_mode);
169 if (!SP_PARM || SP_PARM->_use_ritm) {
170 TurnOff(A_ITALIC, exit_italics_mode);
174 previous_attr &= ALL_BUT_COLOR;
178 SetColorsIf((color_pair != 0) || fix_pair0, previous_attr, previous_pair);
179 } else if (set_attributes) {
180 if (turn_on || turn_off) {
181 TPUTS_TRACE("set_attributes");
182 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
183 TPARM_9(set_attributes,
184 (newmode & A_STANDOUT) != 0,
185 (newmode & A_UNDERLINE) != 0,
186 (newmode & A_REVERSE) != 0,
187 (newmode & A_BLINK) != 0,
188 (newmode & A_DIM) != 0,
189 (newmode & A_BOLD) != 0,
190 (newmode & A_INVIS) != 0,
191 (newmode & A_PROTECT) != 0,
192 (newmode & A_ALTCHARSET) != 0),
194 previous_attr &= ALL_BUT_COLOR;
198 if (!SP_PARM || SP_PARM->_use_ritm) {
199 if (turn_on & A_ITALIC) {
200 TurnOn(A_ITALIC, enter_italics_mode);
201 } else if (turn_off & A_ITALIC) {
202 TurnOff(A_ITALIC, exit_italics_mode);
206 SetColorsIf((color_pair != 0) || fix_pair0, previous_attr, previous_pair);
209 TR(TRACE_ATTRS, ("turning %s off", _traceattr(turn_off)));
211 TurnOff(A_ALTCHARSET, exit_alt_charset_mode);
213 if (!SP_PARM || SP_PARM->_use_rmul) {
214 TurnOff(A_UNDERLINE, exit_underline_mode);
217 if (!SP_PARM || SP_PARM->_use_rmso) {
218 TurnOff(A_STANDOUT, exit_standout_mode);
221 if (!SP_PARM || SP_PARM->_use_ritm) {
222 TurnOff(A_ITALIC, exit_italics_mode);
225 if (turn_off && exit_attribute_mode) {
226 doPut(exit_attribute_mode);
227 turn_on |= (newmode & ALL_BUT_COLOR);
228 previous_attr &= ALL_BUT_COLOR;
231 SetColorsIf((color_pair != 0) || fix_pair0, previous_attr, previous_pair);
233 TR(TRACE_ATTRS, ("turning %s on", _traceattr(turn_on)));
235 TurnOn(A_ALTCHARSET, enter_alt_charset_mode);
236 TurnOn(A_BLINK, enter_blink_mode);
237 TurnOn(A_BOLD, enter_bold_mode);
238 TurnOn(A_DIM, enter_dim_mode);
239 TurnOn(A_REVERSE, enter_reverse_mode);
240 TurnOn(A_STANDOUT, enter_standout_mode);
241 TurnOn(A_PROTECT, enter_protected_mode);
242 TurnOn(A_INVIS, enter_secure_mode);
243 TurnOn(A_UNDERLINE, enter_underline_mode);
245 TurnOn(A_ITALIC, enter_italics_mode);
247 #if USE_WIDEC_SUPPORT
248 TurnOn(A_HORIZONTAL, enter_horizontal_hl_mode);
249 TurnOn(A_LEFT, enter_left_hl_mode);
250 TurnOn(A_LOW, enter_low_hl_mode);
251 TurnOn(A_RIGHT, enter_right_hl_mode);
252 TurnOn(A_TOP, enter_top_hl_mode);
253 TurnOn(A_VERTICAL, enter_vertical_hl_mode);
260 newmode |= A_REVERSE;
263 SetAttr(SCREEN_ATTRS(SP_PARM), newmode);
264 SetPair(SCREEN_ATTRS(SP_PARM), color_pair);
266 previous_attr = newmode;
267 previous_pair = color_pair;
272 T((T_CALLED("vid_puts(%s,%d)"), _traceattr(newmode), color_pair));
274 set_color(newmode, color_pair);
275 returnCode(NCURSES_SP_NAME(vidputs) (NCURSES_SP_ARGx newmode, outc));
281 vid_puts(attr_t newmode,
282 NCURSES_PAIRS_T pair_arg,
283 void *opts GCC_UNUSED,
286 SetSafeOutcWrapper(outc);
287 return NCURSES_SP_NAME(vid_puts) (CURRENT_SCREEN,
297 NCURSES_SP_NAME(vid_attr) (NCURSES_SP_DCLx
299 NCURSES_PAIRS_T pair_arg,
302 T((T_CALLED("vid_attr(%s,%d)"), _traceattr(newmode), (int) pair_arg));
303 returnCode(NCURSES_SP_NAME(vid_puts) (NCURSES_SP_ARGx
307 NCURSES_SP_NAME(_nc_putchar)));
312 vid_attr(attr_t newmode, NCURSES_PAIRS_T pair_arg, void *opts)
314 return NCURSES_SP_NAME(vid_attr) (CURRENT_SCREEN, newmode, pair_arg, opts);
319 * This implementation uses the same mask values for A_xxx and WA_xxx, so
320 * we can use termattrs() for part of the logic.
322 NCURSES_EXPORT(attr_t)
323 NCURSES_SP_NAME(term_attrs) (NCURSES_SP_DCL0)
327 T((T_CALLED("term_attrs()")));
329 attrs = NCURSES_SP_NAME(termattrs) (NCURSES_SP_ARG);
331 /* these are only supported for wide-character mode */
332 if (enter_horizontal_hl_mode)
333 attrs |= WA_HORIZONTAL;
334 if (enter_left_hl_mode)
336 if (enter_low_hl_mode)
338 if (enter_right_hl_mode)
340 if (enter_top_hl_mode)
342 if (enter_vertical_hl_mode)
343 attrs |= WA_VERTICAL;
350 NCURSES_EXPORT(attr_t)
353 return NCURSES_SP_NAME(term_attrs) (CURRENT_SCREEN);