1 /****************************************************************************
2 * Copyright (c) 2002-2006,2007 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 MODULE_ID("$Id: lib_vid_attr.c,v 1.5 2007/06/30 22:03:02 tom Exp $")
38 #define doPut(mode) TPUTS_TRACE(#mode); tputs(mode, 1, outc)
40 #define TurnOn(mask,mode) \
41 if ((turn_on & mask) && mode) { doPut(mode); }
43 #define TurnOff(mask,mode) \
44 if ((turn_off & mask) && mode) { doPut(mode); turn_off &= ~mask; }
46 /* if there is no current screen, assume we *can* do color */
47 #define SetColorsIf(why, old_attr, old_pair) \
48 if (can_color && (why)) { \
49 TR(TRACE_ATTRS, ("old pair = %d -- new pair = %d", old_pair, pair)); \
50 if ((pair != old_pair) \
51 || (fix_pair0 && (pair == 0)) \
52 || (reverse ^ ((old_attr & A_REVERSE) != 0))) { \
53 _nc_do_color(old_pair, pair, reverse, outc); \
57 #define set_color(mode, pair) mode &= ALL_BUT_COLOR; mode |= COLOR_PAIR(pair)
60 vid_puts(attr_t newmode, short pair, void *opts GCC_UNUSED, int (*outc) (int))
62 #if NCURSES_EXT_COLORS
63 static attr_t previous_attr = A_NORMAL;
64 static int previous_pair = 0;
66 attr_t turn_on, turn_off;
68 bool can_color = (SP == 0 || SP->_coloron);
70 bool fix_pair0 = (SP != 0 && SP->_coloron && !SP->_default_color);
72 #define fix_pair0 FALSE
75 newmode &= A_ATTRIBUTES;
76 T((T_CALLED("vid_puts(%s,%d)"), _traceattr(newmode), pair));
78 /* this allows us to go on whether or not newterm() has been called */
80 previous_attr = AttrOf(SCREEN_ATTRS(SP));
81 previous_pair = GetPair(SCREEN_ATTRS(SP));
84 TR(TRACE_ATTRS, ("previous attribute was %s, %d",
85 _traceattr(previous_attr), previous_pair));
89 && (magic_cookie_glitch > 0))
90 newmode &= ~(SP->_xmc_suppress);
94 * If we have a terminal that cannot combine color with video
95 * attributes, use the colors in preference.
99 && (no_color_video > 0)) {
101 * If we had chosen the A_xxx definitions to correspond to the
102 * no_color_video mask, we could simply shift it up and mask off the
103 * attributes. But we did not (actually copied Solaris' definitions).
104 * However, this is still simpler/faster than a lookup table.
106 * The 63 corresponds to A_STANDOUT, A_UNDERLINE, A_REVERSE, A_BLINK,
107 * A_DIM, A_BOLD which are 1:1 with no_color_video. The bits that
108 * correspond to A_INVIS, A_PROTECT (192) must be shifted up 1 and
109 * A_ALTCHARSET (256) down 2 to line up. We use the NCURSES_BITS
110 * macro so this will work properly for the wide-character layout.
112 unsigned value = no_color_video;
113 attr_t mask = NCURSES_BITS((value & 63)
114 | ((value & 192) << 1)
115 | ((value & 256) >> 2), 8);
117 if ((mask & A_REVERSE) != 0
118 && (newmode & A_REVERSE) != 0) {
125 if (newmode == previous_attr
126 && pair == previous_pair)
130 newmode &= ~A_REVERSE;
133 turn_off = (~newmode & previous_attr) & ALL_BUT_COLOR;
134 turn_on = (newmode & ~previous_attr) & ALL_BUT_COLOR;
136 SetColorsIf(((pair == 0) && !fix_pair0), previous_attr, previous_pair);
138 if (newmode == A_NORMAL) {
139 if ((previous_attr & A_ALTCHARSET) && exit_alt_charset_mode) {
140 doPut(exit_alt_charset_mode);
141 previous_attr &= ~A_ALTCHARSET;
144 if (exit_attribute_mode) {
145 doPut(exit_attribute_mode);
147 if (!SP || SP->_use_rmul) {
148 TurnOff(A_UNDERLINE, exit_underline_mode);
150 if (!SP || SP->_use_rmso) {
151 TurnOff(A_STANDOUT, exit_standout_mode);
154 previous_attr &= ALL_BUT_COLOR;
158 SetColorsIf((pair != 0) || fix_pair0, previous_attr, previous_pair);
159 } else if (set_attributes) {
160 if (turn_on || turn_off) {
161 TPUTS_TRACE("set_attributes");
162 tputs(TPARM_9(set_attributes,
163 (newmode & A_STANDOUT) != 0,
164 (newmode & A_UNDERLINE) != 0,
165 (newmode & A_REVERSE) != 0,
166 (newmode & A_BLINK) != 0,
167 (newmode & A_DIM) != 0,
168 (newmode & A_BOLD) != 0,
169 (newmode & A_INVIS) != 0,
170 (newmode & A_PROTECT) != 0,
171 (newmode & A_ALTCHARSET) != 0), 1, outc);
172 previous_attr &= ALL_BUT_COLOR;
175 SetColorsIf((pair != 0) || fix_pair0, previous_attr, previous_pair);
178 TR(TRACE_ATTRS, ("turning %s off", _traceattr(turn_off)));
180 TurnOff(A_ALTCHARSET, exit_alt_charset_mode);
182 if (!SP || SP->_use_rmul) {
183 TurnOff(A_UNDERLINE, exit_underline_mode);
186 if (!SP || SP->_use_rmso) {
187 TurnOff(A_STANDOUT, exit_standout_mode);
190 if (turn_off && exit_attribute_mode) {
191 doPut(exit_attribute_mode);
192 turn_on |= (newmode & ALL_BUT_COLOR);
193 previous_attr &= ALL_BUT_COLOR;
196 SetColorsIf((pair != 0) || fix_pair0, previous_attr, previous_pair);
198 TR(TRACE_ATTRS, ("turning %s on", _traceattr(turn_on)));
200 TurnOn(A_ALTCHARSET, enter_alt_charset_mode);
201 TurnOn(A_BLINK, enter_blink_mode);
202 TurnOn(A_BOLD, enter_bold_mode);
203 TurnOn(A_DIM, enter_dim_mode);
204 TurnOn(A_REVERSE, enter_reverse_mode);
205 TurnOn(A_STANDOUT, enter_standout_mode);
206 TurnOn(A_PROTECT, enter_protected_mode);
207 TurnOn(A_INVIS, enter_secure_mode);
208 TurnOn(A_UNDERLINE, enter_underline_mode);
209 #if USE_WIDEC_SUPPORT
210 TurnOn(A_HORIZONTAL, enter_horizontal_hl_mode);
211 TurnOn(A_LEFT, enter_left_hl_mode);
212 TurnOn(A_LOW, enter_low_hl_mode);
213 TurnOn(A_RIGHT, enter_right_hl_mode);
214 TurnOn(A_TOP, enter_top_hl_mode);
215 TurnOn(A_VERTICAL, enter_vertical_hl_mode);
222 newmode |= A_REVERSE;
225 SetAttr(SCREEN_ATTRS(SP), newmode);
226 SetPair(SCREEN_ATTRS(SP), pair);
228 previous_attr = newmode;
229 previous_pair = pair;
234 T((T_CALLED("vid_puts(%s,%d)"), _traceattr(newmode), pair));
235 set_color(newmode, pair);
236 returnCode(vidputs(newmode, outc));
242 vid_attr(attr_t newmode, short pair, void *opts)
244 T((T_CALLED("vid_attr(%s,%d)"), _traceattr(newmode), pair));
245 returnCode(vid_puts(newmode, pair, opts, _nc_outch));
249 * This implementation uses the same mask values for A_xxx and WA_xxx, so
250 * we can use termattrs() for part of the logic.
252 NCURSES_EXPORT(attr_t)
257 T((T_CALLED("term_attrs()")));
260 /* these are only supported for wide-character mode */
261 if (enter_horizontal_hl_mode)
262 attrs |= WA_HORIZONTAL;
263 if (enter_left_hl_mode)
265 if (enter_low_hl_mode)
267 if (enter_right_hl_mode)
269 if (enter_top_hl_mode)
271 if (enter_vertical_hl_mode)
272 attrs |= WA_VERTICAL;