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