1 /****************************************************************************
2 * Copyright (c) 2002-2007,2009 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>
37 #define CUR SP_TERMTYPE
40 MODULE_ID("$Id: lib_vid_attr.c,v 1.11 2009/05/10 00:48:29 tom Exp $")
42 #define doPut(mode) TPUTS_TRACE(#mode); NCURSES_SP_NAME(tputs)(NCURSES_SP_ARGx mode, 1, outc)
44 #define TurnOn(mask,mode) \
45 if ((turn_on & mask) && mode) { doPut(mode); }
47 #define TurnOff(mask,mode) \
48 if ((turn_off & mask) && mode) { doPut(mode); turn_off &= ~mask; }
50 /* if there is no current screen, assume we *can* do color */
51 #define SetColorsIf(why, old_attr, old_pair) \
52 if (can_color && (why)) { \
53 TR(TRACE_ATTRS, ("old pair = %d -- new pair = %d", old_pair, pair)); \
54 if ((pair != old_pair) \
55 || (fix_pair0 && (pair == 0)) \
56 || (reverse ^ ((old_attr & A_REVERSE) != 0))) { \
57 NCURSES_SP_NAME(_nc_do_color) (NCURSES_SP_ARGx \
63 #define set_color(mode, pair) mode &= ALL_BUT_COLOR; mode |= COLOR_PAIR(pair)
66 NCURSES_SP_NAME(vid_puts) (NCURSES_SP_DCLx
69 void *opts GCC_UNUSED,
72 #if NCURSES_EXT_COLORS
73 static attr_t previous_attr = A_NORMAL;
74 static int previous_pair = 0;
76 attr_t turn_on, turn_off;
78 bool can_color = (SP_PARM == 0 || SP_PARM->_coloron);
80 bool fix_pair0 = (SP_PARM != 0 && SP_PARM->_coloron && !SP_PARM->_default_color);
82 #define fix_pair0 FALSE
85 newmode &= A_ATTRIBUTES;
86 T((T_CALLED("vid_puts(%s,%d)"), _traceattr(newmode), pair));
88 /* this allows us to go on whether or not newterm() has been called */
90 previous_attr = AttrOf(SCREEN_ATTRS(SP_PARM));
91 previous_pair = GetPair(SCREEN_ATTRS(SP_PARM));
94 TR(TRACE_ATTRS, ("previous attribute was %s, %d",
95 _traceattr(previous_attr), previous_pair));
99 && (magic_cookie_glitch > 0))
100 newmode &= ~(SP_PARM->_xmc_suppress);
104 * If we have a terminal that cannot combine color with video
105 * attributes, use the colors in preference.
109 && (no_color_video > 0)) {
111 * If we had chosen the A_xxx definitions to correspond to the
112 * no_color_video mask, we could simply shift it up and mask off the
113 * attributes. But we did not (actually copied Solaris' definitions).
114 * However, this is still simpler/faster than a lookup table.
116 * The 63 corresponds to A_STANDOUT, A_UNDERLINE, A_REVERSE, A_BLINK,
117 * A_DIM, A_BOLD which are 1:1 with no_color_video. The bits that
118 * correspond to A_INVIS, A_PROTECT (192) must be shifted up 1 and
119 * A_ALTCHARSET (256) down 2 to line up. We use the NCURSES_BITS
120 * macro so this will work properly for the wide-character layout.
122 unsigned value = no_color_video;
123 attr_t mask = NCURSES_BITS((value & 63)
124 | ((value & 192) << 1)
125 | ((value & 256) >> 2), 8);
127 if ((mask & A_REVERSE) != 0
128 && (newmode & A_REVERSE) != 0) {
135 if (newmode == previous_attr
136 && pair == previous_pair)
140 newmode &= ~A_REVERSE;
143 turn_off = (~newmode & previous_attr) & ALL_BUT_COLOR;
144 turn_on = (newmode & ~previous_attr) & ALL_BUT_COLOR;
146 SetColorsIf(((pair == 0) && !fix_pair0), previous_attr, previous_pair);
148 if (newmode == A_NORMAL) {
149 if ((previous_attr & A_ALTCHARSET) && exit_alt_charset_mode) {
150 doPut(exit_alt_charset_mode);
151 previous_attr &= ~A_ALTCHARSET;
154 if (exit_attribute_mode) {
155 doPut(exit_attribute_mode);
157 if (!SP_PARM || SP_PARM->_use_rmul) {
158 TurnOff(A_UNDERLINE, exit_underline_mode);
160 if (!SP_PARM || SP_PARM->_use_rmso) {
161 TurnOff(A_STANDOUT, exit_standout_mode);
164 previous_attr &= ALL_BUT_COLOR;
168 SetColorsIf((pair != 0) || fix_pair0, previous_attr, previous_pair);
169 } else if (set_attributes) {
170 if (turn_on || turn_off) {
171 TPUTS_TRACE("set_attributes");
172 NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
173 TPARM_9(set_attributes,
174 (newmode & A_STANDOUT) != 0,
175 (newmode & A_UNDERLINE) != 0,
176 (newmode & A_REVERSE) != 0,
177 (newmode & A_BLINK) != 0,
178 (newmode & A_DIM) != 0,
179 (newmode & A_BOLD) != 0,
180 (newmode & A_INVIS) != 0,
181 (newmode & A_PROTECT) != 0,
182 (newmode & A_ALTCHARSET) != 0),
184 previous_attr &= ALL_BUT_COLOR;
187 SetColorsIf((pair != 0) || fix_pair0, previous_attr, previous_pair);
190 TR(TRACE_ATTRS, ("turning %s off", _traceattr(turn_off)));
192 TurnOff(A_ALTCHARSET, exit_alt_charset_mode);
194 if (!SP_PARM || SP_PARM->_use_rmul) {
195 TurnOff(A_UNDERLINE, exit_underline_mode);
198 if (!SP_PARM || SP_PARM->_use_rmso) {
199 TurnOff(A_STANDOUT, exit_standout_mode);
202 if (turn_off && exit_attribute_mode) {
203 doPut(exit_attribute_mode);
204 turn_on |= (newmode & ALL_BUT_COLOR);
205 previous_attr &= ALL_BUT_COLOR;
208 SetColorsIf((pair != 0) || fix_pair0, previous_attr, previous_pair);
210 TR(TRACE_ATTRS, ("turning %s on", _traceattr(turn_on)));
212 TurnOn(A_ALTCHARSET, enter_alt_charset_mode);
213 TurnOn(A_BLINK, enter_blink_mode);
214 TurnOn(A_BOLD, enter_bold_mode);
215 TurnOn(A_DIM, enter_dim_mode);
216 TurnOn(A_REVERSE, enter_reverse_mode);
217 TurnOn(A_STANDOUT, enter_standout_mode);
218 TurnOn(A_PROTECT, enter_protected_mode);
219 TurnOn(A_INVIS, enter_secure_mode);
220 TurnOn(A_UNDERLINE, enter_underline_mode);
221 #if USE_WIDEC_SUPPORT
222 TurnOn(A_HORIZONTAL, enter_horizontal_hl_mode);
223 TurnOn(A_LEFT, enter_left_hl_mode);
224 TurnOn(A_LOW, enter_low_hl_mode);
225 TurnOn(A_RIGHT, enter_right_hl_mode);
226 TurnOn(A_TOP, enter_top_hl_mode);
227 TurnOn(A_VERTICAL, enter_vertical_hl_mode);
234 newmode |= A_REVERSE;
237 SetAttr(SCREEN_ATTRS(SP_PARM), newmode);
238 SetPair(SCREEN_ATTRS(SP_PARM), pair);
240 previous_attr = newmode;
241 previous_pair = pair;
246 T((T_CALLED("vid_puts(%s,%d)"), _traceattr(newmode), pair));
247 set_color(newmode, pair);
248 returnCode(NCURSES_SP_NAME(vidputs) (NCURSES_SP_ARGx newmode, outc));
254 vid_puts(attr_t newmode,
256 void *opts GCC_UNUSED,
259 SetSafeOutcWrapper(outc);
260 return NCURSES_SP_NAME(vid_puts) (CURRENT_SCREEN,
270 NCURSES_SP_NAME(vid_attr) (NCURSES_SP_DCLx
275 T((T_CALLED("vid_attr(%s,%d)"), _traceattr(newmode), pair));
276 returnCode(NCURSES_SP_NAME(vid_puts) (NCURSES_SP_ARGx
280 NCURSES_SP_NAME(_nc_outch)));
285 vid_attr(attr_t newmode, short pair, void *opts)
287 return NCURSES_SP_NAME(vid_attr) (CURRENT_SCREEN, newmode, pair, opts);
292 * This implementation uses the same mask values for A_xxx and WA_xxx, so
293 * we can use termattrs() for part of the logic.
295 NCURSES_EXPORT(attr_t)
296 NCURSES_SP_NAME(term_attrs) (NCURSES_SP_DCL0)
300 T((T_CALLED("term_attrs()")));
301 attrs = SP_PARM ? NCURSES_SP_NAME(termattrs) (NCURSES_SP_ARG) : 0;
303 /* these are only supported for wide-character mode */
304 if (enter_horizontal_hl_mode)
305 attrs |= WA_HORIZONTAL;
306 if (enter_left_hl_mode)
308 if (enter_low_hl_mode)
310 if (enter_right_hl_mode)
312 if (enter_top_hl_mode)
314 if (enter_vertical_hl_mode)
315 attrs |= WA_VERTICAL;
321 NCURSES_EXPORT(attr_t)
324 return NCURSES_SP_NAME(term_attrs) (CURRENT_SCREEN);