]> ncurses.scripts.mit.edu Git - ncurses.git/blob - ncurses/widechar/lib_vid_attr.c
ncurses 6.1 - patch 20191005
[ncurses.git] / ncurses / widechar / lib_vid_attr.c
1 /****************************************************************************
2  * Copyright (c) 2002-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: Thomas E. Dickey                                                *
31  ****************************************************************************/
32
33 #include <curses.priv.h>
34
35 #ifndef CUR
36 #define CUR SP_TERMTYPE
37 #endif
38
39 MODULE_ID("$Id: lib_vid_attr.c,v 1.27 2019/04/13 22:40:48 tom Exp $")
40
41 #define doPut(mode) \
42         TPUTS_TRACE(#mode); \
43         NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx mode, 1, outc)
44
45 #define TurnOn(mask, mode) \
46         if ((turn_on & mask) && mode) { \
47             TPUTS_TRACE(#mode); \
48             NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx mode, 1, outc); \
49         }
50
51 #define TurnOff(mask, mode) \
52         if ((turn_off & mask) && mode) { \
53             TPUTS_TRACE(#mode); \
54             NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx mode, 1, outc); \
55             turn_off &= ~mask; \
56         }
57
58         /* if there is no current screen, assume we *can* do color */
59 #define SetColorsIf(why, old_attr, old_pair) \
60         if (can_color && (why)) { \
61                 TR(TRACE_ATTRS, ("old pair = %d -- new pair = %d", old_pair, color_pair)); \
62                 if ((color_pair != old_pair) \
63                  || (fix_pair0 && (color_pair == 0)) \
64                  || (reverse ^ ((old_attr & A_REVERSE) != 0))) { \
65                     NCURSES_SP_NAME(_nc_do_color) (NCURSES_SP_ARGx \
66                                                    old_pair, color_pair, \
67                                                    reverse, outc); \
68                 } \
69         }
70
71 #define set_color(mode, pair) \
72         mode &= ALL_BUT_COLOR; \
73         mode |= (attr_t) ColorPair(pair)
74
75 NCURSES_EXPORT(int)
76 NCURSES_SP_NAME(vid_puts) (NCURSES_SP_DCLx
77                            attr_t newmode,
78                            NCURSES_PAIRS_T pair_arg,
79                            void *opts OPTIONAL_PAIR,
80                            NCURSES_SP_OUTC outc)
81 {
82     int color_pair = pair_arg;
83 #if NCURSES_EXT_COLORS
84     static attr_t previous_attr = A_NORMAL;
85     static int previous_pair = 0;
86
87     attr_t turn_on, turn_off;
88     bool reverse = FALSE;
89     bool can_color = (SP_PARM == 0 || SP_PARM->_coloron);
90 #if NCURSES_EXT_FUNCS
91     bool fix_pair0 = (SP_PARM != 0 && SP_PARM->_coloron && !SP_PARM->_default_color);
92 #else
93 #define fix_pair0 FALSE
94 #endif
95
96     if (!IsValidTIScreen(SP_PARM))
97         returnCode(ERR);
98
99     newmode &= A_ATTRIBUTES;
100     set_extended_pair(opts, color_pair);
101     T((T_CALLED("vid_puts(%s,%d)"), _traceattr(newmode), color_pair));
102
103     /* this allows us to go on whether or not newterm() has been called */
104     if (SP_PARM) {
105         previous_attr = AttrOf(SCREEN_ATTRS(SP_PARM));
106         previous_pair = GetPair(SCREEN_ATTRS(SP_PARM));
107     }
108
109     TR(TRACE_ATTRS, ("previous attribute was %s, %d",
110                      _traceattr(previous_attr), previous_pair));
111
112 #if !USE_XMC_SUPPORT
113     if ((SP_PARM != 0)
114         && (magic_cookie_glitch > 0))
115         newmode &= ~(SP_PARM->_xmc_suppress);
116 #endif
117
118     /*
119      * If we have a terminal that cannot combine color with video
120      * attributes, use the colors in preference.
121      */
122     if ((color_pair != 0
123          || fix_pair0)
124         && (no_color_video > 0)) {
125         /*
126          * If we had chosen the A_xxx definitions to correspond to the
127          * no_color_video mask, we could simply shift it up and mask off the
128          * attributes.  But we did not (actually copied Solaris' definitions).
129          * However, this is still simpler/faster than a lookup table.
130          *
131          * The 63 corresponds to A_STANDOUT, A_UNDERLINE, A_REVERSE, A_BLINK,
132          * A_DIM, A_BOLD which are 1:1 with no_color_video.  The bits that
133          * correspond to A_INVIS, A_PROTECT (192) must be shifted up 1 and
134          * A_ALTCHARSET (256) down 2 to line up.  We use the NCURSES_BITS
135          * macro so this will work properly for the wide-character layout.
136          */
137         unsigned value = (unsigned) no_color_video;
138         attr_t mask = NCURSES_BITS((value & 63)
139                                    | ((value & 192) << 1)
140                                    | ((value & 256) >> 2), 8);
141
142         if ((mask & A_REVERSE) != 0
143             && (newmode & A_REVERSE) != 0) {
144             reverse = TRUE;
145             mask &= ~A_REVERSE;
146         }
147         newmode &= ~mask;
148     }
149
150     if (newmode == previous_attr
151         && color_pair == previous_pair)
152         returnCode(OK);
153
154     if (reverse) {
155         newmode &= ~A_REVERSE;
156     }
157
158     turn_off = (~newmode & previous_attr) & ALL_BUT_COLOR;
159     turn_on = (newmode & ~(previous_attr & TPARM_ATTR)) & ALL_BUT_COLOR;
160
161     SetColorsIf(((color_pair == 0) && !fix_pair0), previous_attr, previous_pair);
162
163     if (newmode == A_NORMAL) {
164         if ((previous_attr & A_ALTCHARSET) && exit_alt_charset_mode) {
165             doPut(exit_alt_charset_mode);
166             previous_attr &= ~A_ALTCHARSET;
167         }
168         if (previous_attr) {
169             if (exit_attribute_mode) {
170                 doPut(exit_attribute_mode);
171             } else {
172                 if (!SP_PARM || SP_PARM->_use_rmul) {
173                     TurnOff(A_UNDERLINE, exit_underline_mode);
174                 }
175                 if (!SP_PARM || SP_PARM->_use_rmso) {
176                     TurnOff(A_STANDOUT, exit_standout_mode);
177                 }
178 #if USE_ITALIC
179                 if (!SP_PARM || SP_PARM->_use_ritm) {
180                     TurnOff(A_ITALIC, exit_italics_mode);
181                 }
182 #endif
183             }
184             previous_attr &= ALL_BUT_COLOR;
185             previous_pair = 0;
186         }
187
188         SetColorsIf((color_pair != 0) || fix_pair0, previous_attr, previous_pair);
189     } else if (set_attributes) {
190         if (turn_on || turn_off) {
191             TPUTS_TRACE("set_attributes");
192             NCURSES_SP_NAME(tputs) (NCURSES_SP_ARGx
193                                     TPARM_9(set_attributes,
194                                             (newmode & A_STANDOUT) != 0,
195                                             (newmode & A_UNDERLINE) != 0,
196                                             (newmode & A_REVERSE) != 0,
197                                             (newmode & A_BLINK) != 0,
198                                             (newmode & A_DIM) != 0,
199                                             (newmode & A_BOLD) != 0,
200                                             (newmode & A_INVIS) != 0,
201                                             (newmode & A_PROTECT) != 0,
202                                             (newmode & A_ALTCHARSET) != 0),
203                                     1, outc);
204             previous_attr &= ALL_BUT_COLOR;
205             previous_pair = 0;
206         }
207 #if USE_ITALIC
208         if (!SP_PARM || SP_PARM->_use_ritm) {
209             if (turn_on & A_ITALIC) {
210                 TurnOn(A_ITALIC, enter_italics_mode);
211             } else if (turn_off & A_ITALIC) {
212                 TurnOff(A_ITALIC, exit_italics_mode);
213             }
214         }
215 #endif
216         SetColorsIf((color_pair != 0) || fix_pair0, previous_attr, previous_pair);
217     } else {
218
219         TR(TRACE_ATTRS, ("turning %s off", _traceattr(turn_off)));
220
221         TurnOff(A_ALTCHARSET, exit_alt_charset_mode);
222
223         if (!SP_PARM || SP_PARM->_use_rmul) {
224             TurnOff(A_UNDERLINE, exit_underline_mode);
225         }
226
227         if (!SP_PARM || SP_PARM->_use_rmso) {
228             TurnOff(A_STANDOUT, exit_standout_mode);
229         }
230 #if USE_ITALIC
231         if (!SP_PARM || SP_PARM->_use_ritm) {
232             TurnOff(A_ITALIC, exit_italics_mode);
233         }
234 #endif
235         if (turn_off && exit_attribute_mode) {
236             doPut(exit_attribute_mode);
237             turn_on |= (newmode & ALL_BUT_COLOR);
238             previous_attr &= ALL_BUT_COLOR;
239             previous_pair = 0;
240         }
241         SetColorsIf((color_pair != 0) || fix_pair0, previous_attr, previous_pair);
242
243         TR(TRACE_ATTRS, ("turning %s on", _traceattr(turn_on)));
244         /* *INDENT-OFF* */
245         TurnOn(A_ALTCHARSET,    enter_alt_charset_mode);
246         TurnOn(A_BLINK,         enter_blink_mode);
247         TurnOn(A_BOLD,          enter_bold_mode);
248         TurnOn(A_DIM,           enter_dim_mode);
249         TurnOn(A_REVERSE,       enter_reverse_mode);
250         TurnOn(A_STANDOUT,      enter_standout_mode);
251         TurnOn(A_PROTECT,       enter_protected_mode);
252         TurnOn(A_INVIS,         enter_secure_mode);
253         TurnOn(A_UNDERLINE,     enter_underline_mode);
254 #if USE_ITALIC
255         TurnOn(A_ITALIC,        enter_italics_mode);
256 #endif
257 #if USE_WIDEC_SUPPORT && defined(enter_horizontal_hl_mode)
258         TurnOn(A_HORIZONTAL,    enter_horizontal_hl_mode);
259         TurnOn(A_LEFT,          enter_left_hl_mode);
260         TurnOn(A_LOW,           enter_low_hl_mode);
261         TurnOn(A_RIGHT,         enter_right_hl_mode);
262         TurnOn(A_TOP,           enter_top_hl_mode);
263         TurnOn(A_VERTICAL,      enter_vertical_hl_mode);
264 #endif
265         /* *INDENT-ON* */
266
267     }
268
269     if (reverse)
270         newmode |= A_REVERSE;
271
272     if (SP_PARM) {
273         SetAttr(SCREEN_ATTRS(SP_PARM), newmode);
274         SetPair(SCREEN_ATTRS(SP_PARM), color_pair);
275     } else {
276         previous_attr = newmode;
277         previous_pair = color_pair;
278     }
279
280     returnCode(OK);
281 #else
282     T((T_CALLED("vid_puts(%s,%d)"), _traceattr(newmode), color_pair));
283     (void) opts;
284     set_color(newmode, color_pair);
285     returnCode(NCURSES_SP_NAME(vidputs) (NCURSES_SP_ARGx newmode, outc));
286 #endif
287 }
288
289 #if NCURSES_SP_FUNCS
290 NCURSES_EXPORT(int)
291 vid_puts(attr_t newmode,
292          NCURSES_PAIRS_T pair_arg,
293          void *opts GCC_UNUSED,
294          NCURSES_OUTC outc)
295 {
296     SetSafeOutcWrapper(outc);
297     return NCURSES_SP_NAME(vid_puts) (CURRENT_SCREEN,
298                                       newmode,
299                                       pair_arg,
300                                       opts,
301                                       _nc_outc_wrapper);
302 }
303 #endif
304
305 #undef vid_attr
306 NCURSES_EXPORT(int)
307 NCURSES_SP_NAME(vid_attr) (NCURSES_SP_DCLx
308                            attr_t newmode,
309                            NCURSES_PAIRS_T pair_arg,
310                            void *opts)
311 {
312     T((T_CALLED("vid_attr(%s,%d)"), _traceattr(newmode), (int) pair_arg));
313     returnCode(NCURSES_SP_NAME(vid_puts) (NCURSES_SP_ARGx
314                                           newmode,
315                                           pair_arg,
316                                           opts,
317                                           NCURSES_SP_NAME(_nc_putchar)));
318 }
319
320 #if NCURSES_SP_FUNCS
321 NCURSES_EXPORT(int)
322 vid_attr(attr_t newmode, NCURSES_PAIRS_T pair_arg, void *opts)
323 {
324     return NCURSES_SP_NAME(vid_attr) (CURRENT_SCREEN, newmode, pair_arg, opts);
325 }
326 #endif
327
328 /*
329  * This implementation uses the same mask values for A_xxx and WA_xxx, so
330  * we can use termattrs() for part of the logic.
331  */
332 NCURSES_EXPORT(attr_t)
333 NCURSES_SP_NAME(term_attrs) (NCURSES_SP_DCL0)
334 {
335     attr_t attrs = 0;
336
337     T((T_CALLED("term_attrs()")));
338     if (SP_PARM) {
339         attrs = NCURSES_SP_NAME(termattrs) (NCURSES_SP_ARG);
340
341 #if USE_WIDEC_SUPPORT && defined(enter_horizontal_hl_mode)
342         /* these are only supported for wide-character mode */
343         if (enter_horizontal_hl_mode)
344             attrs |= WA_HORIZONTAL;
345         if (enter_left_hl_mode)
346             attrs |= WA_LEFT;
347         if (enter_low_hl_mode)
348             attrs |= WA_LOW;
349         if (enter_right_hl_mode)
350             attrs |= WA_RIGHT;
351         if (enter_top_hl_mode)
352             attrs |= WA_TOP;
353         if (enter_vertical_hl_mode)
354             attrs |= WA_VERTICAL;
355 #endif
356     }
357
358     returnAttr(attrs);
359 }
360
361 #if NCURSES_SP_FUNCS
362 NCURSES_EXPORT(attr_t)
363 term_attrs(void)
364 {
365     return NCURSES_SP_NAME(term_attrs) (CURRENT_SCREEN);
366 }
367 #endif