1 /****************************************************************************
2 * Copyright (c) 2002-2012,2013 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.17 2013/01/12 18:01:35 tom Exp $")
41 #define doPut(mode) TPUTS_TRACE(#mode); NCURSES_SP_NAME(tputs)(NCURSES_SP_ARGx mode, 1, outc)
43 #define TurnOn(mask,mode) \
44 if ((turn_on & mask) && mode) { doPut(mode); }
46 #define TurnOff(mask,mode) \
47 if ((turn_off & mask) && mode) { doPut(mode); turn_off &= ~mask; }
49 /* if there is no current screen, assume we *can* do color */
50 #define SetColorsIf(why, old_attr, old_pair) \
51 if (can_color && (why)) { \
52 TR(TRACE_ATTRS, ("old pair = %d -- new pair = %d", old_pair, pair)); \
53 if ((pair != old_pair) \
54 || (fix_pair0 && (pair == 0)) \
55 || (reverse ^ ((old_attr & A_REVERSE) != 0))) { \
56 NCURSES_SP_NAME(_nc_do_color) (NCURSES_SP_ARGx \
62 #define set_color(mode, pair) \
63 mode &= ALL_BUT_COLOR; \
64 mode |= (attr_t) ColorPair(pair)
67 NCURSES_SP_NAME(vid_puts) (NCURSES_SP_DCLx
70 void *opts GCC_UNUSED,
73 #if NCURSES_EXT_COLORS
74 static attr_t previous_attr = A_NORMAL;
75 static int previous_pair = 0;
77 attr_t turn_on, turn_off;
79 bool can_color = (SP_PARM == 0 || SP_PARM->_coloron);
81 bool fix_pair0 = (SP_PARM != 0 && SP_PARM->_coloron && !SP_PARM->_default_color);
83 #define fix_pair0 FALSE
86 newmode &= A_ATTRIBUTES;
87 T((T_CALLED("vid_puts(%s,%d)"), _traceattr(newmode), pair));
89 /* this allows us to go on whether or not newterm() has been called */
91 previous_attr = AttrOf(SCREEN_ATTRS(SP_PARM));
92 previous_pair = GetPair(SCREEN_ATTRS(SP_PARM));
95 TR(TRACE_ATTRS, ("previous attribute was %s, %d",
96 _traceattr(previous_attr), previous_pair));
100 && (magic_cookie_glitch > 0))
101 newmode &= ~(SP_PARM->_xmc_suppress);
105 * If we have a terminal that cannot combine color with video
106 * attributes, use the colors in preference.
110 && (no_color_video > 0)) {
112 * If we had chosen the A_xxx definitions to correspond to the
113 * no_color_video mask, we could simply shift it up and mask off the
114 * attributes. But we did not (actually copied Solaris' definitions).
115 * However, this is still simpler/faster than a lookup table.
117 * The 63 corresponds to A_STANDOUT, A_UNDERLINE, A_REVERSE, A_BLINK,
118 * A_DIM, A_BOLD which are 1:1 with no_color_video. The bits that
119 * correspond to A_INVIS, A_PROTECT (192) must be shifted up 1 and
120 * A_ALTCHARSET (256) down 2 to line up. We use the NCURSES_BITS
121 * macro so this will work properly for the wide-character layout.
123 unsigned value = (unsigned) no_color_video;
124 attr_t mask = NCURSES_BITS((value & 63)
125 | ((value & 192) << 1)
126 | ((value & 256) >> 2), 8);
128 if ((mask & A_REVERSE) != 0
129 && (newmode & A_REVERSE) != 0) {
136 if (newmode == previous_attr
137 && pair == previous_pair)
141 newmode &= ~A_REVERSE;
144 turn_off = (~newmode & previous_attr) & ALL_BUT_COLOR;
145 turn_on = (newmode & ~previous_attr) & ALL_BUT_COLOR;
147 SetColorsIf(((pair == 0) && !fix_pair0), previous_attr, previous_pair);
149 if (newmode == A_NORMAL) {
150 if ((previous_attr & A_ALTCHARSET) && exit_alt_charset_mode) {
151 doPut(exit_alt_charset_mode);
152 previous_attr &= ~A_ALTCHARSET;
155 if (exit_attribute_mode) {
156 doPut(exit_attribute_mode);
158 if (!SP_PARM || SP_PARM->_use_rmul) {
159 TurnOff(A_UNDERLINE, exit_underline_mode);
161 if (!SP_PARM || SP_PARM->_use_rmso) {
162 TurnOff(A_STANDOUT, exit_standout_mode);
165 previous_attr &= ALL_BUT_COLOR;
169 SetColorsIf((pair != 0) || fix_pair0, previous_attr, previous_pair);
170 } else if (set_attributes) {
171 if (turn_on || turn_off) {
172 TPUTS_TRACE("set_attributes");
173 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
174 TPARM_9(set_attributes,
175 (newmode & A_STANDOUT) != 0,
176 (newmode & A_UNDERLINE) != 0,
177 (newmode & A_REVERSE) != 0,
178 (newmode & A_BLINK) != 0,
179 (newmode & A_DIM) != 0,
180 (newmode & A_BOLD) != 0,
181 (newmode & A_INVIS) != 0,
182 (newmode & A_PROTECT) != 0,
183 (newmode & A_ALTCHARSET) != 0),
185 previous_attr &= ALL_BUT_COLOR;
188 SetColorsIf((pair != 0) || fix_pair0, previous_attr, previous_pair);
191 TR(TRACE_ATTRS, ("turning %s off", _traceattr(turn_off)));
193 TurnOff(A_ALTCHARSET, exit_alt_charset_mode);
195 if (!SP_PARM || SP_PARM->_use_rmul) {
196 TurnOff(A_UNDERLINE, exit_underline_mode);
199 if (!SP_PARM || SP_PARM->_use_rmso) {
200 TurnOff(A_STANDOUT, exit_standout_mode);
203 if (turn_off && exit_attribute_mode) {
204 doPut(exit_attribute_mode);
205 turn_on |= (newmode & ALL_BUT_COLOR);
206 previous_attr &= ALL_BUT_COLOR;
209 SetColorsIf((pair != 0) || fix_pair0, previous_attr, previous_pair);
211 TR(TRACE_ATTRS, ("turning %s on", _traceattr(turn_on)));
213 TurnOn(A_ALTCHARSET, enter_alt_charset_mode);
214 TurnOn(A_BLINK, enter_blink_mode);
215 TurnOn(A_BOLD, enter_bold_mode);
216 TurnOn(A_DIM, enter_dim_mode);
217 TurnOn(A_REVERSE, enter_reverse_mode);
218 TurnOn(A_STANDOUT, enter_standout_mode);
219 TurnOn(A_PROTECT, enter_protected_mode);
220 TurnOn(A_INVIS, enter_secure_mode);
221 TurnOn(A_UNDERLINE, enter_underline_mode);
222 #if USE_WIDEC_SUPPORT
223 TurnOn(A_HORIZONTAL, enter_horizontal_hl_mode);
224 TurnOn(A_LEFT, enter_left_hl_mode);
225 TurnOn(A_LOW, enter_low_hl_mode);
226 TurnOn(A_RIGHT, enter_right_hl_mode);
227 TurnOn(A_TOP, enter_top_hl_mode);
228 TurnOn(A_VERTICAL, enter_vertical_hl_mode);
235 newmode |= A_REVERSE;
238 SetAttr(SCREEN_ATTRS(SP_PARM), newmode);
239 SetPair(SCREEN_ATTRS(SP_PARM), pair);
241 previous_attr = newmode;
242 previous_pair = pair;
247 T((T_CALLED("vid_puts(%s,%d)"), _traceattr(newmode), pair));
248 set_color(newmode, pair);
249 returnCode(NCURSES_SP_NAME(vidputs) (NCURSES_SP_ARGx newmode, outc));
255 vid_puts(attr_t newmode,
257 void *opts GCC_UNUSED,
260 SetSafeOutcWrapper(outc);
261 return NCURSES_SP_NAME(vid_puts) (CURRENT_SCREEN,
271 NCURSES_SP_NAME(vid_attr) (NCURSES_SP_DCLx
276 T((T_CALLED("vid_attr(%s,%d)"), _traceattr(newmode), pair));
277 returnCode(NCURSES_SP_NAME(vid_puts) (NCURSES_SP_ARGx
281 NCURSES_SP_NAME(_nc_putchar)));
286 vid_attr(attr_t newmode, short pair, void *opts)
288 return NCURSES_SP_NAME(vid_attr) (CURRENT_SCREEN, newmode, pair, opts);
293 * This implementation uses the same mask values for A_xxx and WA_xxx, so
294 * we can use termattrs() for part of the logic.
296 NCURSES_EXPORT(attr_t)
297 NCURSES_SP_NAME(term_attrs) (NCURSES_SP_DCL0)
301 T((T_CALLED("term_attrs()")));
303 attrs = NCURSES_SP_NAME(termattrs) (NCURSES_SP_ARG);
305 /* these are only supported for wide-character mode */
306 if (enter_horizontal_hl_mode)
307 attrs |= WA_HORIZONTAL;
308 if (enter_left_hl_mode)
310 if (enter_low_hl_mode)
312 if (enter_right_hl_mode)
314 if (enter_top_hl_mode)
316 if (enter_vertical_hl_mode)
317 attrs |= WA_VERTICAL;
324 NCURSES_EXPORT(attr_t)
327 return NCURSES_SP_NAME(term_attrs) (CURRENT_SCREEN);