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