]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/tty/lib_vidattr.c
ncurses 5.7 - patch 20100130
[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
68 #ifndef CUR
69 #define CUR SP_TERMTYPE
70 #endif
71
72 MODULE_ID("$Id: lib_vidattr.c,v 1.59 2009/10/24 22:12:21 tom Exp $")
73
74 #define doPut(mode) \
75         TPUTS_TRACE(#mode); \
76         NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx mode, 1, outc)
77
78 #define TurnOn(mask,mode) \
79         if ((turn_on & mask) && mode) { doPut(mode); }
80
81 #define TurnOff(mask,mode) \
82         if ((turn_off & mask) && mode) { doPut(mode); turn_off &= ~mask; }
83
84         /* if there is no current screen, assume we *can* do color */
85 #define SetColorsIf(why,old_attr) \
86         if (can_color && (why)) { \
87                 int old_pair = PAIR_NUMBER(old_attr); \
88                 TR(TRACE_ATTRS, ("old pair = %d -- new pair = %d", old_pair, pair)); \
89                 if ((pair != old_pair) \
90                  || (fix_pair0 && (pair == 0)) \
91                  || (reverse ^ ((old_attr & A_REVERSE) != 0))) { \
92                      NCURSES_SP_NAME(_nc_do_color)(NCURSES_SP_ARGx \
93                                      old_pair, pair, reverse, outc); \
94                 } \
95         }
96
97 #define PreviousAttr _nc_prescreen.previous_attr
98
99 NCURSES_EXPORT(int)
100 NCURSES_SP_NAME(vidputs) (NCURSES_SP_DCLx
101                           chtype newmode,
102                           NCURSES_SP_OUTC outc)
103 {
104     attr_t turn_on, turn_off;
105     int pair;
106     bool reverse = FALSE;
107     bool can_color = (SP_PARM == 0 || SP_PARM->_coloron);
108 #if NCURSES_EXT_FUNCS
109     bool fix_pair0 = (SP_PARM != 0 && SP_PARM->_coloron && !SP_PARM->_default_color);
110 #else
111 #define fix_pair0 FALSE
112 #endif
113
114     newmode &= A_ATTRIBUTES;
115
116     T((T_CALLED("vidputs(%p,%s)"), (void *) SP_PARM, _traceattr(newmode)));
117
118     if (!IsTermInfo(SP_PARM))
119         returnCode(ERR);
120
121     /* this allows us to go on whether or not newterm() has been called */
122     if (SP_PARM)
123         PreviousAttr = AttrOf(SCREEN_ATTRS(SP_PARM));
124
125     TR(TRACE_ATTRS, ("previous attribute was %s", _traceattr(PreviousAttr)));
126
127     if ((SP_PARM != 0)
128         && (magic_cookie_glitch > 0)) {
129 #if USE_XMC_SUPPORT
130         static const chtype table[] =
131         {
132             A_STANDOUT,
133             A_UNDERLINE,
134             A_REVERSE,
135             A_BLINK,
136             A_DIM,
137             A_BOLD,
138             A_INVIS,
139             A_PROTECT,
140         };
141         unsigned n;
142         int used = 0;
143         int limit = (max_attributes <= 0) ? 1 : max_attributes;
144         chtype retain = 0;
145
146         /*
147          * Limit the number of attribute bits set in the newmode according to
148          * the terminfo max_attributes value.
149          */
150         for (n = 0; n < SIZEOF(table); ++n) {
151             if ((table[n] & SP_PARM->_ok_attributes) == 0) {
152                 newmode &= ~table[n];
153             } else if ((table[n] & newmode) != 0) {
154                 if (used++ >= limit) {
155                     newmode &= ~table[n];
156                     if (newmode == retain)
157                         break;
158                 } else {
159                     retain = newmode;
160                 }
161             }
162         }
163 #else
164         newmode &= ~(SP_PARM->_xmc_suppress);
165 #endif
166         TR(TRACE_ATTRS, ("suppressed attribute is %s", _traceattr(newmode)));
167     }
168
169     /*
170      * If we have a terminal that cannot combine color with video
171      * attributes, use the colors in preference.
172      */
173     if (((newmode & A_COLOR) != 0
174          || fix_pair0)
175         && (no_color_video > 0)) {
176         /*
177          * If we had chosen the A_xxx definitions to correspond to the
178          * no_color_video mask, we could simply shift it up and mask off the
179          * attributes.  But we did not (actually copied Solaris' definitions).
180          * However, this is still simpler/faster than a lookup table.
181          *
182          * The 63 corresponds to A_STANDOUT, A_UNDERLINE, A_REVERSE, A_BLINK,
183          * A_DIM, A_BOLD which are 1:1 with no_color_video.  The bits that
184          * correspond to A_INVIS, A_PROTECT (192) must be shifted up 1 and
185          * A_ALTCHARSET (256) down 2 to line up.  We use the NCURSES_BITS
186          * macro so this will work properly for the wide-character layout.
187          */
188         unsigned value = no_color_video;
189         attr_t mask = NCURSES_BITS((value & 63)
190                                    | ((value & 192) << 1)
191                                    | ((value & 256) >> 2), 8);
192
193         if ((mask & A_REVERSE) != 0
194             && (newmode & A_REVERSE) != 0) {
195             reverse = TRUE;
196             mask &= ~A_REVERSE;
197         }
198         newmode &= ~mask;
199     }
200
201     if (newmode == PreviousAttr)
202         returnCode(OK);
203
204     pair = PAIR_NUMBER(newmode);
205
206     if (reverse) {
207         newmode &= ~A_REVERSE;
208     }
209
210     turn_off = (~newmode & PreviousAttr) & ALL_BUT_COLOR;
211     turn_on = (newmode & ~PreviousAttr) & ALL_BUT_COLOR;
212
213     SetColorsIf(((pair == 0) && !fix_pair0), PreviousAttr);
214
215     if (newmode == A_NORMAL) {
216         if ((PreviousAttr & A_ALTCHARSET) && exit_alt_charset_mode) {
217             doPut(exit_alt_charset_mode);
218             PreviousAttr &= ~A_ALTCHARSET;
219         }
220         if (PreviousAttr) {
221             if (exit_attribute_mode) {
222                 doPut(exit_attribute_mode);
223             } else {
224                 if (!SP_PARM || SP_PARM->_use_rmul) {
225                     TurnOff(A_UNDERLINE, exit_underline_mode);
226                 }
227                 if (!SP_PARM || SP_PARM->_use_rmso) {
228                     TurnOff(A_STANDOUT, exit_standout_mode);
229                 }
230             }
231             PreviousAttr &= ALL_BUT_COLOR;
232         }
233
234         SetColorsIf((pair != 0) || fix_pair0, PreviousAttr);
235     } else if (set_attributes) {
236         if (turn_on || turn_off) {
237             TPUTS_TRACE("set_attributes");
238             NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
239                                     tparm(set_attributes,
240                                           (newmode & A_STANDOUT) != 0,
241                                           (newmode & A_UNDERLINE) != 0,
242                                           (newmode & A_REVERSE) != 0,
243                                           (newmode & A_BLINK) != 0,
244                                           (newmode & A_DIM) != 0,
245                                           (newmode & A_BOLD) != 0,
246                                           (newmode & A_INVIS) != 0,
247                                           (newmode & A_PROTECT) != 0,
248                                           (newmode & A_ALTCHARSET) != 0),
249                                     1, outc);
250             PreviousAttr &= ALL_BUT_COLOR;
251         }
252         SetColorsIf((pair != 0) || fix_pair0, PreviousAttr);
253     } else {
254
255         TR(TRACE_ATTRS, ("turning %s off", _traceattr(turn_off)));
256
257         TurnOff(A_ALTCHARSET, exit_alt_charset_mode);
258
259         if (!SP_PARM || SP_PARM->_use_rmul) {
260             TurnOff(A_UNDERLINE, exit_underline_mode);
261         }
262
263         if (!SP_PARM || SP_PARM->_use_rmso) {
264             TurnOff(A_STANDOUT, exit_standout_mode);
265         }
266
267         if (turn_off && exit_attribute_mode) {
268             doPut(exit_attribute_mode);
269             turn_on |= (newmode & ALL_BUT_COLOR);
270             PreviousAttr &= ALL_BUT_COLOR;
271         }
272         SetColorsIf((pair != 0) || fix_pair0, PreviousAttr);
273
274         TR(TRACE_ATTRS, ("turning %s on", _traceattr(turn_on)));
275         /* *INDENT-OFF* */
276         TurnOn(A_ALTCHARSET,    enter_alt_charset_mode);
277         TurnOn(A_BLINK,         enter_blink_mode);
278         TurnOn(A_BOLD,          enter_bold_mode);
279         TurnOn(A_DIM,           enter_dim_mode);
280         TurnOn(A_REVERSE,       enter_reverse_mode);
281         TurnOn(A_STANDOUT,      enter_standout_mode);
282         TurnOn(A_PROTECT,       enter_protected_mode);
283         TurnOn(A_INVIS,         enter_secure_mode);
284         TurnOn(A_UNDERLINE,     enter_underline_mode);
285 #if USE_WIDEC_SUPPORT
286         TurnOn(A_HORIZONTAL,    enter_horizontal_hl_mode);
287         TurnOn(A_LEFT,          enter_left_hl_mode);
288         TurnOn(A_LOW,           enter_low_hl_mode);
289         TurnOn(A_RIGHT,         enter_right_hl_mode);
290         TurnOn(A_TOP,           enter_top_hl_mode);
291         TurnOn(A_VERTICAL,      enter_vertical_hl_mode);
292 #endif
293         /* *INDENT-ON* */
294
295     }
296
297     if (reverse)
298         newmode |= A_REVERSE;
299
300     if (SP_PARM)
301         SetAttr(SCREEN_ATTRS(SP_PARM), newmode);
302     else
303         PreviousAttr = newmode;
304
305     returnCode(OK);
306 }
307
308 #if NCURSES_SP_FUNCS
309 NCURSES_EXPORT(int)
310 vidputs(chtype newmode, NCURSES_OUTC outc)
311 {
312     SetSafeOutcWrapper(outc);
313     return NCURSES_SP_NAME(vidputs) (CURRENT_SCREEN,
314                                      newmode,
315                                      _nc_outc_wrapper);
316 }
317 #endif
318
319 NCURSES_EXPORT(int)
320 NCURSES_SP_NAME(vidattr) (NCURSES_SP_DCLx chtype newmode)
321 {
322     T((T_CALLED("vidattr(%p,%s)"), (void *) SP_PARM, _traceattr(newmode)));
323     returnCode(NCURSES_SP_NAME(vidputs) (NCURSES_SP_ARGx
324                                          newmode,
325                                          NCURSES_SP_NAME(_nc_outch)));
326 }
327
328 #if NCURSES_SP_FUNCS
329 NCURSES_EXPORT(int)
330 vidattr(chtype newmode)
331 {
332     return NCURSES_SP_NAME(vidattr) (CURRENT_SCREEN, newmode);
333 }
334 #endif
335
336 NCURSES_EXPORT(chtype)
337 NCURSES_SP_NAME(termattrs) (NCURSES_SP_DCL0)
338 {
339     chtype attrs = A_NORMAL;
340
341     T((T_CALLED("termattrs(%p)"), (void *) SP_PARM));
342 #ifdef USE_TERM_DRIVER
343     if (HasTerminal(SP_PARM))
344         attrs = CallDriver(SP_PARM, conattr);
345 #else
346
347     if (enter_alt_charset_mode)
348         attrs |= A_ALTCHARSET;
349
350     if (enter_blink_mode)
351         attrs |= A_BLINK;
352
353     if (enter_bold_mode)
354         attrs |= A_BOLD;
355
356     if (enter_dim_mode)
357         attrs |= A_DIM;
358
359     if (enter_reverse_mode)
360         attrs |= A_REVERSE;
361
362     if (enter_standout_mode)
363         attrs |= A_STANDOUT;
364
365     if (enter_protected_mode)
366         attrs |= A_PROTECT;
367
368     if (enter_secure_mode)
369         attrs |= A_INVIS;
370
371     if (enter_underline_mode)
372         attrs |= A_UNDERLINE;
373
374     if (SP_PARM->_coloron)
375         attrs |= A_COLOR;
376
377 #endif
378     returnChtype(attrs);
379 }
380
381 #if NCURSES_SP_FUNCS
382 NCURSES_EXPORT(chtype)
383 termattrs(void)
384 {
385     return NCURSES_SP_NAME(termattrs) (CURRENT_SCREEN);
386 }
387 #endif