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