1 /****************************************************************************
2  * Copyright (c) 2002-2005,2006 Free Software Foundation, Inc.              *
3  *                                                                          *
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: Thomas E. Dickey                                                *
31  ****************************************************************************/
32 
33 #include <curses.priv.h>
34 #include <term.h>
35 
36 MODULE_ID("$Id: lib_vid_attr.c,v 1.4 2006/11/26 00:26:00 tom Exp $")
37 
38 #define doPut(mode) TPUTS_TRACE(#mode); tputs(mode, 1, outc)
39 
40 #define TurnOn(mask,mode) \
41 	if ((turn_on & mask) && mode) { doPut(mode); }
42 
43 #define TurnOff(mask,mode) \
44 	if ((turn_off & mask) && mode) { doPut(mode); turn_off &= ~mask; }
45 
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); \
54 		} \
55 	}
56 
57 #define set_color(mode, pair) mode &= ALL_BUT_COLOR; mode |= COLOR_PAIR(pair)
58 
59 NCURSES_EXPORT(int)
60 vid_puts(attr_t newmode, short pair, void *opts GCC_UNUSED, int (*outc) (int))
61 {
62 #if NCURSES_EXT_COLORS
63     static attr_t previous_attr = A_NORMAL;
64     static int previous_pair = 0;
65 
66     attr_t turn_on, turn_off;
67     bool reverse = FALSE;
68     bool can_color = (SP == 0 || SP->_coloron);
69 #if NCURSES_EXT_FUNCS
70     bool fix_pair0 = (SP != 0 && SP->_coloron && !SP->_default_color);
71 #else
72 #define fix_pair0 FALSE
73 #endif
74 
75     T((T_CALLED("vid_puts(%s,%d)"), _traceattr(newmode), pair));
76 
77     /* this allows us to go on whether or not newterm() has been called */
78     if (SP) {
79 	previous_attr = AttrOf(SCREEN_ATTRS(SP));
80 	previous_pair = GetPair(SCREEN_ATTRS(SP));
81     }
82 
83     TR(TRACE_ATTRS, ("previous attribute was %s, %d",
84 		     _traceattr(previous_attr), previous_pair));
85 
86 #if !USE_XMC_SUPPORT
87     if ((SP != 0)
88 	&& (magic_cookie_glitch > 0))
89 	newmode &= ~(SP->_xmc_suppress);
90 #endif
91 
92     /*
93      * If we have a terminal that cannot combine color with video
94      * attributes, use the colors in preference.
95      */
96     if ((pair != 0
97 	 || fix_pair0)
98 	&& (no_color_video > 0)) {
99 	/*
100 	 * If we had chosen the A_xxx definitions to correspond to the
101 	 * no_color_video mask, we could simply shift it up and mask off the
102 	 * attributes.  But we did not (actually copied Solaris' definitions).
103 	 * However, this is still simpler/faster than a lookup table.
104 	 *
105 	 * The 63 corresponds to A_STANDOUT, A_UNDERLINE, A_REVERSE, A_BLINK,
106 	 * A_DIM, A_BOLD which are 1:1 with no_color_video.  The bits that
107 	 * correspond to A_INVIS, A_PROTECT (192) must be shifted up 1 and
108 	 * A_ALTCHARSET (256) down 2 to line up.  We use the NCURSES_BITS
109 	 * macro so this will work properly for the wide-character layout.
110 	 */
111 	unsigned value = no_color_video;
112 	attr_t mask = NCURSES_BITS((value & 63)
113 				   | ((value & 192) << 1)
114 				   | ((value & 256) >> 2), 8);
115 
116 	if ((mask & A_REVERSE) != 0
117 	    && (newmode & A_REVERSE) != 0) {
118 	    reverse = TRUE;
119 	    mask &= ~A_REVERSE;
120 	}
121 	newmode &= ~mask;
122     }
123 
124     if (newmode == previous_attr
125 	&& pair == previous_pair)
126 	returnCode(OK);
127 
128     if (reverse) {
129 	newmode &= ~A_REVERSE;
130     }
131 
132     turn_off = (~newmode & previous_attr) & ALL_BUT_COLOR;
133     turn_on = (newmode & ~previous_attr) & ALL_BUT_COLOR;
134 
135     SetColorsIf(((pair == 0) && !fix_pair0), previous_attr, previous_pair);
136 
137     if (newmode == A_NORMAL) {
138 	if ((previous_attr & A_ALTCHARSET) && exit_alt_charset_mode) {
139 	    doPut(exit_alt_charset_mode);
140 	    previous_attr &= ~A_ALTCHARSET;
141 	}
142 	if (previous_attr) {
143 	    if (exit_attribute_mode) {
144 		doPut(exit_attribute_mode);
145 	    } else {
146 		if (!SP || SP->_use_rmul) {
147 		    TurnOff(A_UNDERLINE, exit_underline_mode);
148 		}
149 		if (!SP || SP->_use_rmso) {
150 		    TurnOff(A_STANDOUT, exit_standout_mode);
151 		}
152 	    }
153 	    previous_attr &= ALL_BUT_COLOR;
154 	    previous_pair = 0;
155 	}
156 
157 	SetColorsIf((pair != 0) || fix_pair0, previous_attr, previous_pair);
158     } else if (set_attributes) {
159 	if (turn_on || turn_off) {
160 	    TPUTS_TRACE("set_attributes");
161 	    tputs(TPARM_9(set_attributes,
162 			  (newmode & A_STANDOUT) != 0,
163 			  (newmode & A_UNDERLINE) != 0,
164 			  (newmode & A_REVERSE) != 0,
165 			  (newmode & A_BLINK) != 0,
166 			  (newmode & A_DIM) != 0,
167 			  (newmode & A_BOLD) != 0,
168 			  (newmode & A_INVIS) != 0,
169 			  (newmode & A_PROTECT) != 0,
170 			  (newmode & A_ALTCHARSET) != 0), 1, outc);
171 	    previous_attr &= ALL_BUT_COLOR;
172 	    previous_pair = 0;
173 	}
174 	SetColorsIf((pair != 0) || fix_pair0, previous_attr, previous_pair);
175     } else {
176 
177 	TR(TRACE_ATTRS, ("turning %s off", _traceattr(turn_off)));
178 
179 	TurnOff(A_ALTCHARSET, exit_alt_charset_mode);
180 
181 	if (!SP || SP->_use_rmul) {
182 	    TurnOff(A_UNDERLINE, exit_underline_mode);
183 	}
184 
185 	if (!SP || SP->_use_rmso) {
186 	    TurnOff(A_STANDOUT, exit_standout_mode);
187 	}
188 
189 	if (turn_off && exit_attribute_mode) {
190 	    doPut(exit_attribute_mode);
191 	    turn_on |= (newmode & ALL_BUT_COLOR);
192 	    previous_attr &= ALL_BUT_COLOR;
193 	    previous_pair = 0;
194 	}
195 	SetColorsIf((pair != 0) || fix_pair0, previous_attr, previous_pair);
196 
197 	TR(TRACE_ATTRS, ("turning %s on", _traceattr(turn_on)));
198 	/* *INDENT-OFF* */
199 	TurnOn(A_ALTCHARSET,	enter_alt_charset_mode);
200 	TurnOn(A_BLINK,		enter_blink_mode);
201 	TurnOn(A_BOLD,		enter_bold_mode);
202 	TurnOn(A_DIM,		enter_dim_mode);
203 	TurnOn(A_REVERSE,	enter_reverse_mode);
204 	TurnOn(A_STANDOUT,	enter_standout_mode);
205 	TurnOn(A_PROTECT,	enter_protected_mode);
206 	TurnOn(A_INVIS,		enter_secure_mode);
207 	TurnOn(A_UNDERLINE,	enter_underline_mode);
208 #if USE_WIDEC_SUPPORT
209 	TurnOn(A_HORIZONTAL,	enter_horizontal_hl_mode);
210 	TurnOn(A_LEFT,		enter_left_hl_mode);
211 	TurnOn(A_LOW,		enter_low_hl_mode);
212 	TurnOn(A_RIGHT,		enter_right_hl_mode);
213 	TurnOn(A_TOP,		enter_top_hl_mode);
214 	TurnOn(A_VERTICAL,	enter_vertical_hl_mode);
215 #endif
216 	/* *INDENT-ON* */
217 
218     }
219 
220     if (reverse)
221 	newmode |= A_REVERSE;
222 
223     if (SP) {
224 	SetAttr(SCREEN_ATTRS(SP), newmode);
225 	SetPair(SCREEN_ATTRS(SP), pair);
226     } else {
227 	previous_attr = newmode;
228 	previous_pair = pair;
229     }
230 
231     returnCode(OK);
232 #else
233     T((T_CALLED("vid_puts(%s,%d)"), _traceattr(newmode), pair));
234     set_color(newmode, pair);
235     returnCode(vidputs(newmode, outc));
236 #endif
237 }
238 
239 #undef vid_attr
240 NCURSES_EXPORT(int)
241 vid_attr(attr_t newmode, short pair, void *opts)
242 {
243     T((T_CALLED("vid_attr(%s,%d)"), _traceattr(newmode), pair));
244     returnCode(vid_puts(newmode, pair, opts, _nc_outch));
245 }
246 
247 /*
248  * This implementation uses the same mask values for A_xxx and WA_xxx, so
249  * we can use termattrs() for part of the logic.
250  */
251 NCURSES_EXPORT(attr_t)
252 term_attrs(void)
253 {
254     attr_t attrs;
255 
256     T((T_CALLED("term_attrs()")));
257     attrs = termattrs();
258 
259     /* these are only supported for wide-character mode */
260     if (enter_horizontal_hl_mode)
261 	attrs |= WA_HORIZONTAL;
262     if (enter_left_hl_mode)
263 	attrs |= WA_LEFT;
264     if (enter_low_hl_mode)
265 	attrs |= WA_LOW;
266     if (enter_right_hl_mode)
267 	attrs |= WA_RIGHT;
268     if (enter_top_hl_mode)
269 	attrs |= WA_TOP;
270     if (enter_vertical_hl_mode)
271 	attrs |= WA_VERTICAL;
272 
273     returnAttr(attrs);
274 }
275