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