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