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