1 /****************************************************************************
2  * Copyright 2018-2019,2020 Thomas E. Dickey                                *
3  * Copyright 1998-2014,2016 Free Software Foundation, Inc.                  *
4  *                                                                          *
5  * Permission is hereby granted, free of charge, to any person obtaining a  *
6  * copy of this software and associated documentation files (the            *
7  * "Software"), to deal in the Software without restriction, including      *
8  * without limitation the rights to use, copy, modify, merge, publish,      *
9  * distribute, distribute with modifications, sublicense, and/or sell       *
10  * copies of the Software, and to permit persons to whom the Software is    *
11  * furnished to do so, subject to the following conditions:                 *
12  *                                                                          *
13  * The above copyright notice and this permission notice shall be included  *
14  * in all copies or substantial portions of the Software.                   *
15  *                                                                          *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS  *
17  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF               *
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.   *
19  * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM,   *
20  * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR    *
21  * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR    *
22  * THE USE OR OTHER DEALINGS IN THE SOFTWARE.                               *
23  *                                                                          *
24  * Except as contained in this notice, the name(s) of the above copyright   *
25  * holders shall not be used in advertising or otherwise to promote the     *
26  * sale, use or other dealings in this Software without prior written       *
27  * authorization.                                                           *
28  ****************************************************************************/
29 
30 /****************************************************************************
31  *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
32  *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
33  *     and: Juergen Pfeifer                         1997                    *
34  *     and: Sven Verdoolaege                        2000                    *
35  *     and: Thomas E. Dickey                        1996-on                 *
36  ****************************************************************************/
37 
38 #include <curses.priv.h>
39 
40 MODULE_ID("$Id: lib_bkgd.c,v 1.54 2020/02/02 23:34:34 tom Exp $")
41 
42 /*
43  * Set the window's background information.
44  */
45 #if USE_WIDEC_SUPPORT
46 NCURSES_EXPORT(void)
47 #else
48 static NCURSES_INLINE void
49 #endif
50 wbkgrndset(WINDOW *win, const ARG_CH_T ch)
51 {
52     T((T_CALLED("wbkgdset(%p,%s)"), (void *) win, _tracech_t(ch)));
53 
54     if (win) {
55 	attr_t off = AttrOf(win->_nc_bkgd);
56 	attr_t on = AttrOf(CHDEREF(ch));
57 
58 	toggle_attr_off(WINDOW_ATTRS(win), off);
59 	toggle_attr_on(WINDOW_ATTRS(win), on);
60 
61 #if NCURSES_EXT_COLORS
62 	{
63 	    int pair;
64 
65 	    if ((pair = GetPair(win->_nc_bkgd)) != 0)
66 		SET_WINDOW_PAIR(win, 0);
67 	    if ((pair = GetPair(CHDEREF(ch))) != 0)
68 		SET_WINDOW_PAIR(win, pair);
69 	}
70 #endif
71 
72 	if (CharOf(CHDEREF(ch)) == L('\0')) {
73 	    SetChar(win->_nc_bkgd, BLANK_TEXT, AttrOf(CHDEREF(ch)));
74 	    if_EXT_COLORS(SetPair(win->_nc_bkgd, GetPair(CHDEREF(ch))));
75 	} else {
76 	    win->_nc_bkgd = CHDEREF(ch);
77 	}
78 #if USE_WIDEC_SUPPORT
79 	/*
80 	 * If we're compiled for wide-character support, _bkgrnd is the
81 	 * preferred location for the background information since it stores
82 	 * more than _bkgd.  Update _bkgd each time we modify _bkgrnd, so the
83 	 * macro getbkgd() will work.
84 	 */
85 	{
86 	    cchar_t wch;
87 	    int tmp;
88 
89 	    memset(&wch, 0, sizeof(wch));
90 	    (void) wgetbkgrnd(win, &wch);
91 	    tmp = _nc_to_char((wint_t) CharOf(wch));
92 
93 	    win->_bkgd = (((tmp == EOF) ? ' ' : (chtype) tmp)
94 			  | (AttrOf(wch) & ALL_BUT_COLOR)
95 			  | (chtype) ColorPair(GET_WINDOW_PAIR(win)));
96 	}
97 #endif
98     }
99     returnVoid;
100 }
101 
102 NCURSES_EXPORT(void)
103 wbkgdset(WINDOW *win, chtype ch)
104 {
105     NCURSES_CH_T wch;
106     SetChar2(wch, ch);
107     wbkgrndset(win, CHREF(wch));
108 }
109 
110 /*
111  * Set the window's background information and apply it to each cell.
112  */
113 #if USE_WIDEC_SUPPORT
114 NCURSES_EXPORT(int)
115 #else
116 static NCURSES_INLINE int
117 #undef wbkgrnd
118 #endif
119 wbkgrnd(WINDOW *win, const ARG_CH_T ch)
120 {
121 #undef  SP_PARM
122 #define SP_PARM SP		/* to use Charable() */
123     int code = ERR;
124 
125     T((T_CALLED("wbkgd(%p,%s)"), (void *) win, _tracech_t(ch)));
126 
127     if (SP == 0) {
128 	;
129     } else if (win) {
130 	NCURSES_CH_T new_bkgd = CHDEREF(ch);
131 	NCURSES_CH_T old_bkgd;
132 	int y;
133 	NCURSES_CH_T old_char;
134 	attr_t old_attr;
135 	int old_pair;
136 	NCURSES_CH_T new_char;
137 	attr_t new_attr;
138 	int new_pair;
139 
140 	/* SVr4 trims color info if non-color terminal */
141 	if (!SP->_pair_limit) {
142 	    RemAttr(new_bkgd, A_COLOR);
143 	    SetPair(new_bkgd, 0);
144 	}
145 
146 	memset(&old_bkgd, 0, sizeof(old_bkgd));
147 	(void) wgetbkgrnd(win, &old_bkgd);
148 
149 	if (!memcmp(&old_bkgd, &new_bkgd, sizeof(new_bkgd))) {
150 	    T(("...unchanged"));
151 	    returnCode(OK);
152 	}
153 
154 	old_char = old_bkgd;
155 	RemAttr(old_char, ~A_CHARTEXT);
156 	old_attr = AttrOf(old_bkgd);
157 	old_pair = GetPair(old_bkgd);
158 
159 	if (!(old_attr & A_COLOR)) {
160 	    old_pair = 0;
161 	}
162 	T(("... old background char %s, attr %s, pair %d",
163 	   _tracechar(CharOf(old_char)), _traceattr(old_attr), old_pair));
164 
165 	new_char = new_bkgd;
166 	RemAttr(new_char, ~A_CHARTEXT);
167 	new_attr = AttrOf(new_bkgd);
168 	new_pair = GetPair(new_bkgd);
169 
170 	/* SVr4 limits background character to printable 7-bits */
171 	if (!Charable(new_bkgd)) {
172 	    new_char = old_char;
173 	}
174 	if (!(new_attr & A_COLOR)) {
175 	    new_pair = 0;
176 	}
177 	T(("... new background char %s, attr %s, pair %d",
178 	   _tracechar(CharOf(new_char)), _traceattr(new_attr), new_pair));
179 
180 	(void) wbkgrndset(win, CHREF(new_bkgd));
181 
182 	/* SVr4 updates color pair if old/new match, otherwise just attrs */
183 	if ((new_pair != 0) && (new_pair == old_pair)) {
184 	    WINDOW_ATTRS(win) = new_attr;
185 	    SET_WINDOW_PAIR(win, new_pair);
186 	} else {
187 	    WINDOW_ATTRS(win) = new_attr;
188 	}
189 
190 	for (y = 0; y <= win->_maxy; y++) {
191 	    int x;
192 
193 	    for (x = 0; x <= win->_maxx; x++) {
194 		NCURSES_CH_T *cp = &(win->_line[y].text[x]);
195 		int tmp_pair = GetPair(*cp);
196 		attr_t tmp_attr = AttrOf(*cp);
197 
198 		if (CharEq(*cp, old_bkgd)) {
199 		    SetChar2(*cp, CharOf(new_char));
200 		}
201 		if (tmp_pair != 0) {
202 		    if (tmp_pair == old_pair) {
203 			SetAttr(*cp, (tmp_attr & ~old_attr) | new_attr);
204 			SetPair(*cp, new_pair);
205 		    } else {
206 			SetAttr(*cp,
207 				(tmp_attr & (~old_attr | A_COLOR))
208 				| (new_attr & ALL_BUT_COLOR));
209 		    }
210 		} else {
211 		    SetAttr(*cp, (tmp_attr & ~old_attr) | new_attr);
212 		    SetPair(*cp, new_pair);
213 		}
214 	    }
215 	}
216 	touchwin(win);
217 	_nc_synchook(win);
218 	code = OK;
219     }
220     returnCode(code);
221 }
222 
223 NCURSES_EXPORT(int)
224 wbkgd(WINDOW *win, chtype ch)
225 {
226     NCURSES_CH_T wch;
227     SetChar2(wch, ch);
228     return wbkgrnd(win, CHREF(wch));
229 }
230