]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tty/lib_vidattr.c
ncurses 5.7 - patch 20090221
[ncurses.git] / ncurses / tty / lib_vidattr.c
1 /****************************************************************************
2  * Copyright (c) 1998-2007,2009 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: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
31  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
32  *     and: Thomas E. Dickey                        1996-on                 *
33  *     and: Juergen Pfeifer                         2009                    *
34  ****************************************************************************/
35
36 /*
37  *      vidputs(newmode, outc)
38  *
39  *      newmode is taken to be the logical 'or' of the symbols in curses.h
40  *      representing graphic renditions.  The terminal is set to be in all of
41  *      the given modes, if possible.
42  *
43  *      if the new attribute is normal
44  *              if exit-alt-char-set exists
45  *                      emit it
46  *              emit exit-attribute-mode
47  *      else if set-attributes exists
48  *              use it to set exactly what you want
49  *      else
50  *              if exit-attribute-mode exists
51  *                      turn off everything
52  *              else
53  *                      turn off those which can be turned off and aren't in
54  *                      newmode.
55  *              turn on each mode which should be on and isn't, one by one
56  *
57  *      NOTE that this algorithm won't achieve the desired mix of attributes
58  *      in some cases, but those are probably just those cases in which it is
59  *      actually impossible, anyway, so...
60  *
61  *      NOTE that we cannot assume that there's no interaction between color
62  *      and other attribute resets.  So each time we reset color (or other
63  *      attributes) we'll have to be prepared to restore the other.
64  */
65
66 #include <curses.priv.h>
67 #include <term.h>
68
69 MODULE_ID("$Id: lib_vidattr.c,v 1.51 2009/02/21 21:43:40 tom Exp $")
70
71 #define doPut(mode) TPUTS_TRACE(#mode); tputs(mode, 1, outc)
72
73 #define TurnOn(mask,mode) \
74         if ((turn_on & mask) && mode) { doPut(mode); }
75
76 #define TurnOff(mask,mode) \
77         if ((turn_off & mask) && mode) { doPut(mode); turn_off &= ~mask; }
78
79         /* if there is no current screen, assume we *can* do color */
80 #define SetColorsIf(why,old_attr) \
81         if (can_color && (why)) { \
82                 int old_pair = PAIR_NUMBER(old_attr); \
83                 TR(TRACE_ATTRS, ("old pair = %d -- new pair = %d", old_pair, pair)); \
84                 if ((pair != old_pair) \
85                  || (fix_pair0 && (pair == 0)) \
86                  || (reverse ^ ((old_attr & A_REVERSE) != 0))) { \
87                         _nc_do_color(old_pair, pair, reverse, outc); \
88                 } \
89         }
90
91 #define PreviousAttr _nc_prescreen.previous_attr
92
93 NCURSES_EXPORT(int)
94 vidputs(chtype newmode, int (*outc) (int))
95 {
96     attr_t turn_on, turn_off;
97     int pair;
98     bool reverse = FALSE;
99     bool can_color = (SP == 0 || SP->_coloron);
100 #if NCURSES_EXT_FUNCS
101     bool fix_pair0 = (SP != 0 && SP->_coloron && !SP->_default_color);
102 #else
103 #define fix_pair0 FALSE
104 #endif
105
106     newmode &= A_ATTRIBUTES;
107     T((T_CALLED("vidputs(%s)"), _traceattr(newmode)));
108
109     /* this allows us to go on whether or not newterm() has been called */
110     if (SP)
111         PreviousAttr = AttrOf(SCREEN_ATTRS(SP));
112
113     TR(TRACE_ATTRS, ("previous attribute was %s", _traceattr(PreviousAttr)));
114
115     if ((SP != 0)
116         && (magic_cookie_glitch > 0)) {
117 #if USE_XMC_SUPPORT
118         static const chtype table[] =
119         {
120             A_STANDOUT,
121             A_UNDERLINE,
122             A_REVERSE,
123             A_BLINK,
124             A_DIM,
125             A_BOLD,
126             A_INVIS,
127             A_PROTECT,
128         };
129         unsigned n;
130         int used = 0;
131         int limit = (max_attributes <= 0) ? 1 : max_attributes;
132         chtype retain = 0;
133
134         /*
135          * Limit the number of attribute bits set in the newmode according to
136          * the terminfo max_attributes value.
137          */
138         for (n = 0; n < SIZEOF(table); ++n) {
139             if ((table[n] & SP->_ok_attributes) == 0) {
140                 newmode &= ~table[n];
141             } else if ((table[n] & newmode) != 0) {
142                 if (used++ >= limit) {
143                     newmode &= ~table[n];
144                     if (newmode == retain)
145                         break;
146                 } else {
147                     retain = newmode;
148                 }
149             }
150         }
151 #else
152         newmode &= ~(SP->_xmc_suppress);
153 #endif
154         TR(TRACE_ATTRS, ("suppressed attribute is %s", _traceattr(newmode)));
155     }
156
157     /*
158      * If we have a terminal that cannot combine color with video
159      * attributes, use the colors in preference.
160      */
161     if (((newmode & A_COLOR) != 0
162          || fix_pair0)
163         && (no_color_video > 0)) {
164         /*
165          * If we had chosen the A_xxx definitions to correspond to the
166          * no_color_video mask, we could simply shift it up and mask off the
167          * attributes.  But we did not (actually copied Solaris' definitions).
168          * However, this is still simpler/faster than a lookup table.
169          *
170          * The 63 corresponds to A_STANDOUT, A_UNDERLINE, A_REVERSE, A_BLINK,
171          * A_DIM, A_BOLD which are 1:1 with no_color_video.  The bits that
172          * correspond to A_INVIS, A_PROTECT (192) must be shifted up 1 and
173          * A_ALTCHARSET (256) down 2 to line up.  We use the NCURSES_BITS
174          * macro so this will work properly for the wide-character layout.
175          */
176         unsigned value = no_color_video;
177         attr_t mask = NCURSES_BITS((value & 63)
178                                    | ((value & 192) << 1)
179                                    | ((value & 256) >> 2), 8);
180
181         if ((mask & A_REVERSE) != 0
182             && (newmode & A_REVERSE) != 0) {
183             reverse = TRUE;
184             mask &= ~A_REVERSE;
185         }
186         newmode &= ~mask;
187     }
188
189     if (newmode == PreviousAttr)
190         returnCode(OK);
191
192     pair = PAIR_NUMBER(newmode);
193
194     if (reverse) {
195         newmode &= ~A_REVERSE;
196     }
197
198     turn_off = (~newmode & PreviousAttr) & ALL_BUT_COLOR;
199     turn_on = (newmode & ~PreviousAttr) & ALL_BUT_COLOR;
200
201     SetColorsIf(((pair == 0) && !fix_pair0), PreviousAttr);
202
203     if (newmode == A_NORMAL) {
204         if ((PreviousAttr & A_ALTCHARSET) && exit_alt_charset_mode) {
205             doPut(exit_alt_charset_mode);
206             PreviousAttr &= ~A_ALTCHARSET;
207         }
208         if (PreviousAttr) {
209             if (exit_attribute_mode) {
210                 doPut(exit_attribute_mode);
211             } else {
212                 if (!SP || SP->_use_rmul) {
213                     TurnOff(A_UNDERLINE, exit_underline_mode);
214                 }
215                 if (!SP || SP->_use_rmso) {
216                     TurnOff(A_STANDOUT, exit_standout_mode);
217                 }
218             }
219             PreviousAttr &= ALL_BUT_COLOR;
220         }
221
222         SetColorsIf((pair != 0) || fix_pair0, PreviousAttr);
223     } else if (set_attributes) {
224         if (turn_on || turn_off) {
225             TPUTS_TRACE("set_attributes");
226             tputs(tparm(set_attributes,
227                         (newmode & A_STANDOUT) != 0,
228                         (newmode & A_UNDERLINE) != 0,
229                         (newmode & A_REVERSE) != 0,
230                         (newmode & A_BLINK) != 0,
231                         (newmode & A_DIM) != 0,
232                         (newmode & A_BOLD) != 0,
233                         (newmode & A_INVIS) != 0,
234                         (newmode & A_PROTECT) != 0,
235                         (newmode & A_ALTCHARSET) != 0), 1, outc);
236             PreviousAttr &= ALL_BUT_COLOR;
237         }
238         SetColorsIf((pair != 0) || fix_pair0, PreviousAttr);
239     } else {
240
241         TR(TRACE_ATTRS, ("turning %s off", _traceattr(turn_off)));
242
243         TurnOff(A_ALTCHARSET, exit_alt_charset_mode);
244
245         if (!SP || SP->_use_rmul) {
246             TurnOff(A_UNDERLINE, exit_underline_mode);
247         }
248
249         if (!SP || SP->_use_rmso) {
250             TurnOff(A_STANDOUT, exit_standout_mode);
251         }
252
253         if (turn_off && exit_attribute_mode) {
254             doPut(exit_attribute_mode);
255             turn_on |= (newmode & ALL_BUT_COLOR);
256             PreviousAttr &= ALL_BUT_COLOR;
257         }
258         SetColorsIf((pair != 0) || fix_pair0, PreviousAttr);
259
260         TR(TRACE_ATTRS, ("turning %s on", _traceattr(turn_on)));
261         /* *INDENT-OFF* */
262         TurnOn(A_ALTCHARSET,    enter_alt_charset_mode);
263         TurnOn(A_BLINK,         enter_blink_mode);
264         TurnOn(A_BOLD,          enter_bold_mode);
265         TurnOn(A_DIM,           enter_dim_mode);
266         TurnOn(A_REVERSE,       enter_reverse_mode);
267         TurnOn(A_STANDOUT,      enter_standout_mode);
268         TurnOn(A_PROTECT,       enter_protected_mode);
269         TurnOn(A_INVIS,         enter_secure_mode);
270         TurnOn(A_UNDERLINE,     enter_underline_mode);
271 #if USE_WIDEC_SUPPORT
272         TurnOn(A_HORIZONTAL,    enter_horizontal_hl_mode);
273         TurnOn(A_LEFT,          enter_left_hl_mode);
274         TurnOn(A_LOW,           enter_low_hl_mode);
275         TurnOn(A_RIGHT,         enter_right_hl_mode);
276         TurnOn(A_TOP,           enter_top_hl_mode);
277         TurnOn(A_VERTICAL,      enter_vertical_hl_mode);
278 #endif
279         /* *INDENT-ON* */
280
281     }
282
283     if (reverse)
284         newmode |= A_REVERSE;
285
286     if (SP)
287         SetAttr(SCREEN_ATTRS(SP), newmode);
288     else
289         PreviousAttr = newmode;
290
291     returnCode(OK);
292 }
293
294 NCURSES_EXPORT(int)
295 vidattr(chtype newmode)
296 {
297     T((T_CALLED("vidattr(%s)"), _traceattr(newmode)));
298
299     returnCode(vidputs(newmode, _nc_outch));
300 }
301
302 NCURSES_EXPORT(chtype)
303 NCURSES_SP_NAME(termattrs) (NCURSES_SP_DCL0)
304 {
305     chtype attrs = A_NORMAL;
306
307     T((T_CALLED("termattrs()")));
308     if (enter_alt_charset_mode)
309         attrs |= A_ALTCHARSET;
310
311     if (enter_blink_mode)
312         attrs |= A_BLINK;
313
314     if (enter_bold_mode)
315         attrs |= A_BOLD;
316
317     if (enter_dim_mode)
318         attrs |= A_DIM;
319
320     if (enter_reverse_mode)
321         attrs |= A_REVERSE;
322
323     if (enter_standout_mode)
324         attrs |= A_STANDOUT;
325
326     if (enter_protected_mode)
327         attrs |= A_PROTECT;
328
329     if (enter_secure_mode)
330         attrs |= A_INVIS;
331
332     if (enter_underline_mode)
333         attrs |= A_UNDERLINE;
334
335     if (SP_PARM->_coloron)
336         attrs |= A_COLOR;
337
338     returnChtype(attrs);
339 }
340
341 #if NCURSES_SP_FUNCS
342 NCURSES_EXPORT(chtype)
343 termattrs(void)
344 {
345     return NCURSES_SP_NAME(termattrs) (CURRENT_SCREEN);
346 }
347 #endif