background.c revision 1.33 1 1.33 blymn /* $NetBSD: background.c,v 1.33 2022/10/19 06:09:27 blymn Exp $ */
2 1.1 jdc
3 1.1 jdc /*-
4 1.1 jdc * Copyright (c) 2000 The NetBSD Foundation, Inc.
5 1.1 jdc * All rights reserved.
6 1.1 jdc *
7 1.1 jdc * This code is derived from software contributed to The NetBSD Foundation
8 1.1 jdc * by Julian Coleman.
9 1.1 jdc *
10 1.1 jdc * Redistribution and use in source and binary forms, with or without
11 1.1 jdc * modification, are permitted provided that the following conditions
12 1.1 jdc * are met:
13 1.1 jdc * 1. Redistributions of source code must retain the above copyright
14 1.1 jdc * notice, this list of conditions and the following disclaimer.
15 1.1 jdc * 2. Redistributions in binary form must reproduce the above copyright
16 1.1 jdc * notice, this list of conditions and the following disclaimer in the
17 1.1 jdc * documentation and/or other materials provided with the distribution.
18 1.1 jdc *
19 1.1 jdc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.1 jdc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.1 jdc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.1 jdc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.1 jdc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.1 jdc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.1 jdc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.1 jdc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.1 jdc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.1 jdc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.1 jdc * POSSIBILITY OF SUCH DAMAGE.
30 1.1 jdc */
31 1.6 blymn
32 1.6 blymn #include <sys/cdefs.h>
33 1.6 blymn #ifndef lint
34 1.33 blymn __RCSID("$NetBSD: background.c,v 1.33 2022/10/19 06:09:27 blymn Exp $");
35 1.6 blymn #endif /* not lint */
36 1.1 jdc
37 1.11 blymn #include <stdlib.h>
38 1.1 jdc #include "curses.h"
39 1.1 jdc #include "curses_private.h"
40 1.1 jdc
41 1.1 jdc /*
42 1.4 jdc * bkgdset
43 1.4 jdc * Set new background attributes on stdscr.
44 1.4 jdc */
45 1.4 jdc void
46 1.4 jdc bkgdset(chtype ch)
47 1.4 jdc {
48 1.4 jdc wbkgdset(stdscr, ch);
49 1.4 jdc }
50 1.4 jdc
51 1.4 jdc /*
52 1.4 jdc * bkgd --
53 1.19 uwe * Set new background attributes on stdscr and apply them to its
54 1.19 uwe * contents.
55 1.4 jdc */
56 1.4 jdc int
57 1.4 jdc bkgd(chtype ch)
58 1.4 jdc {
59 1.4 jdc return(wbkgd(stdscr, ch));
60 1.4 jdc }
61 1.4 jdc
62 1.4 jdc /*
63 1.1 jdc * wbkgdset
64 1.19 uwe * Set new background attributes on the specified window.
65 1.1 jdc */
66 1.1 jdc void
67 1.3 blymn wbkgdset(WINDOW *win, chtype ch)
68 1.1 jdc {
69 1.10 jdc __CTRACE(__CTRACE_ATTR, "wbkgdset: (%p), '%s', %08x\n",
70 1.18 uwe win, unctrl(ch & __CHARTEXT), ch & __ATTRIBUTES);
71 1.7 jdc
72 1.7 jdc /* Background character. */
73 1.5 jdc if (ch & __CHARTEXT)
74 1.5 jdc win->bch = (wchar_t) ch & __CHARTEXT;
75 1.7 jdc
76 1.7 jdc /* Background attributes (check colour). */
77 1.7 jdc if (__using_color && !(ch & __COLOR))
78 1.7 jdc ch |= __default_color;
79 1.5 jdc win->battr = (attr_t) ch & __ATTRIBUTES;
80 1.1 jdc }
81 1.1 jdc
82 1.1 jdc /*
83 1.1 jdc * wbkgd --
84 1.19 uwe * Set new background attributes on the specified window and
85 1.19 uwe * apply them to its contents.
86 1.1 jdc */
87 1.1 jdc int
88 1.3 blymn wbkgd(WINDOW *win, chtype ch)
89 1.1 jdc {
90 1.25 uwe int y, x;
91 1.5 jdc
92 1.10 jdc __CTRACE(__CTRACE_ATTR, "wbkgd: (%p), '%s', %08x\n",
93 1.18 uwe win, unctrl(ch & __CHARTEXT), ch & __ATTRIBUTES);
94 1.20 uwe wbkgdset(win, ch);
95 1.7 jdc
96 1.25 uwe for (y = 0; y < win->maxy; y++) {
97 1.5 jdc for (x = 0; x < win->maxx; x++) {
98 1.25 uwe __LDATA *cp = &win->alines[y]->line[x];
99 1.25 uwe
100 1.25 uwe /* Update/switch background characters */
101 1.31 blymn if (cp->cflags & CA_BACKGROUND)
102 1.25 uwe cp->ch = win->bch;
103 1.25 uwe
104 1.25 uwe /* Update/merge attributes */
105 1.25 uwe cp->attr = win->battr | (cp->attr & __ALTCHARSET);
106 1.13 jdc #ifdef HAVE_WCHAR
107 1.28 blymn cp->wcols = 1;
108 1.13 jdc #endif
109 1.5 jdc }
110 1.25 uwe }
111 1.29 blymn __touchwin(win, 1);
112 1.25 uwe return OK;
113 1.1 jdc }
114 1.1 jdc
115 1.1 jdc /*
116 1.1 jdc * getbkgd --
117 1.1 jdc * Get current background attributes.
118 1.11 blymn */
119 1.1 jdc chtype
120 1.3 blymn getbkgd(WINDOW *win)
121 1.1 jdc {
122 1.7 jdc attr_t battr;
123 1.7 jdc
124 1.7 jdc /* Background attributes (check colour). */
125 1.7 jdc battr = win->battr & A_ATTRIBUTES;
126 1.7 jdc if (__using_color && ((battr & __COLOR) == __default_color))
127 1.23 uwe battr &= ~__COLOR;
128 1.7 jdc
129 1.7 jdc return ((chtype) ((win->bch & A_CHARTEXT) | battr));
130 1.1 jdc }
131 1.11 blymn
132 1.21 uwe
133 1.21 uwe #ifdef HAVE_WCHAR
134 1.21 uwe
135 1.21 uwe void
136 1.21 uwe bkgrndset(const cchar_t *wch)
137 1.11 blymn {
138 1.22 uwe wbkgrndset(stdscr, wch);
139 1.11 blymn }
140 1.11 blymn
141 1.21 uwe
142 1.21 uwe int
143 1.22 uwe bkgrnd(const cchar_t *wch)
144 1.11 blymn {
145 1.22 uwe return wbkgrnd(stdscr, wch);
146 1.11 blymn }
147 1.11 blymn
148 1.21 uwe
149 1.21 uwe int
150 1.22 uwe getbkgrnd(cchar_t *wch)
151 1.11 blymn {
152 1.22 uwe return wgetbkgrnd(stdscr, wch);
153 1.11 blymn }
154 1.11 blymn
155 1.21 uwe
156 1.21 uwe void
157 1.21 uwe wbkgrndset(WINDOW *win, const cchar_t *wch)
158 1.11 blymn {
159 1.11 blymn attr_t battr;
160 1.11 blymn nschar_t *np, *tnp;
161 1.30 blymn int i, wy, wx;
162 1.30 blymn __LDATA obkgrnd, nbkgrnd;
163 1.30 blymn __LINE *wlp;
164 1.11 blymn
165 1.11 blymn __CTRACE(__CTRACE_ATTR, "wbkgrndset: (%p), '%s', %x\n",
166 1.27 rin win, (const char *)wunctrl(wch), wch->attributes);
167 1.11 blymn
168 1.11 blymn /* ignore multi-column characters */
169 1.17 roy if (!wch->elements || wcwidth(wch->vals[0]) > 1)
170 1.11 blymn return;
171 1.11 blymn
172 1.30 blymn /* get a copy of the old background, we will need it. */
173 1.30 blymn obkgrnd.ch = win->bch;
174 1.30 blymn obkgrnd.attr = win->battr;
175 1.33 blymn obkgrnd.cflags = CA_BACKGROUND;
176 1.30 blymn obkgrnd.wcols = win->wcols;
177 1.30 blymn obkgrnd.nsp = NULL;
178 1.30 blymn _cursesi_copy_nsp(win->bnsp, &obkgrnd);
179 1.30 blymn
180 1.11 blymn /* Background character. */
181 1.11 blymn tnp = np = win->bnsp;
182 1.17 roy if (wcwidth( wch->vals[0]))
183 1.17 roy win->bch = wch->vals[0];
184 1.11 blymn else {
185 1.17 roy if (!np) {
186 1.16 christos np = malloc(sizeof(nschar_t));
187 1.11 blymn if (!np)
188 1.11 blymn return;
189 1.11 blymn np->next = NULL;
190 1.11 blymn win->bnsp = np;
191 1.11 blymn }
192 1.17 roy np->ch = wch->vals[0];
193 1.11 blymn tnp = np;
194 1.11 blymn np = np->next;
195 1.11 blymn }
196 1.11 blymn /* add non-spacing characters */
197 1.17 roy if (wch->elements > 1) {
198 1.17 roy for (i = 1; i < wch->elements; i++) {
199 1.11 blymn if ( !np ) {
200 1.16 christos np = malloc(sizeof(nschar_t));
201 1.11 blymn if (!np)
202 1.11 blymn return;
203 1.11 blymn np->next = NULL;
204 1.17 roy if (tnp)
205 1.11 blymn tnp->next = np;
206 1.11 blymn else
207 1.11 blymn win->bnsp = np;
208 1.11 blymn }
209 1.17 roy np->ch = wch->vals[i];
210 1.11 blymn tnp = np;
211 1.11 blymn np = np->next;
212 1.11 blymn }
213 1.11 blymn }
214 1.11 blymn /* clear the old non-spacing characters */
215 1.30 blymn __cursesi_free_nsp(np);
216 1.11 blymn
217 1.11 blymn /* Background attributes (check colour). */
218 1.11 blymn battr = wch->attributes & WA_ATTRIBUTES;
219 1.11 blymn if (__using_color && !( battr & __COLOR))
220 1.11 blymn battr |= __default_color;
221 1.11 blymn win->battr = battr;
222 1.28 blymn win->wcols = 1;
223 1.30 blymn
224 1.30 blymn nbkgrnd.ch = win->bch;
225 1.30 blymn nbkgrnd.attr = win->battr;
226 1.33 blymn nbkgrnd.cflags = CA_BACKGROUND;
227 1.30 blymn nbkgrnd.wcols = win->wcols;
228 1.30 blymn nbkgrnd.nsp = NULL;
229 1.30 blymn _cursesi_copy_nsp(win->bnsp, &nbkgrnd);
230 1.30 blymn
231 1.31 blymn /* if the background is already this char then skip updating */
232 1.31 blymn if (_cursesi_celleq(&obkgrnd, &nbkgrnd))
233 1.31 blymn return;
234 1.31 blymn
235 1.31 blymn /*
236 1.31 blymn * Now do the dirty work of updating all the locations
237 1.31 blymn * that have the old background character with the new.
238 1.31 blymn */
239 1.31 blymn
240 1.30 blymn for (wy = 0; wy < win->maxy; wy++) {
241 1.30 blymn wlp = win->alines[wy];
242 1.30 blymn for (wx = 0; wx < win->maxx; wx++) {
243 1.31 blymn if (wlp->line[wx].cflags & CA_BACKGROUND) {
244 1.30 blymn _cursesi_copy_wchar(&nbkgrnd, &wlp->line[wx]);
245 1.30 blymn }
246 1.30 blymn }
247 1.30 blymn }
248 1.32 blymn __touchwin(win, 0);
249 1.30 blymn
250 1.11 blymn }
251 1.11 blymn
252 1.21 uwe
253 1.21 uwe int
254 1.22 uwe wbkgrnd(WINDOW *win, const cchar_t *wch)
255 1.22 uwe {
256 1.22 uwe __CTRACE(__CTRACE_ATTR, "wbkgrnd: (%p), '%s', %x\n",
257 1.27 rin win, (const char *)wunctrl(wch), wch->attributes);
258 1.22 uwe
259 1.22 uwe /* ignore multi-column characters */
260 1.22 uwe if (!wch->elements || wcwidth( wch->vals[ 0 ]) > 1)
261 1.22 uwe return ERR;
262 1.22 uwe
263 1.22 uwe wbkgrndset(win, wch);
264 1.29 blymn __touchwin(win, 1);
265 1.22 uwe return OK;
266 1.22 uwe }
267 1.22 uwe
268 1.22 uwe
269 1.22 uwe int
270 1.21 uwe wgetbkgrnd(WINDOW *win, cchar_t *wch)
271 1.11 blymn {
272 1.11 blymn nschar_t *np;
273 1.11 blymn
274 1.11 blymn /* Background attributes (check colour). */
275 1.11 blymn wch->attributes = win->battr & WA_ATTRIBUTES;
276 1.23 uwe if (__using_color && ((wch->attributes & __COLOR) == __default_color))
277 1.23 uwe wch->attributes &= ~__COLOR;
278 1.17 roy wch->vals[0] = win->bch;
279 1.11 blymn wch->elements = 1;
280 1.11 blymn np = win->bnsp;
281 1.11 blymn if (np) {
282 1.17 roy while (np && wch->elements < CURSES_CCHAR_MAX) {
283 1.17 roy wch->vals[wch->elements++] = np->ch;
284 1.11 blymn np = np->next;
285 1.11 blymn }
286 1.11 blymn }
287 1.11 blymn
288 1.11 blymn return OK;
289 1.11 blymn }
290 1.21 uwe
291 1.21 uwe #else /* !HAVE_WCHAR */
292 1.21 uwe
293 1.21 uwe void
294 1.21 uwe bkgrndset(const cchar_t *wch)
295 1.21 uwe {
296 1.21 uwe return;
297 1.21 uwe }
298 1.21 uwe
299 1.21 uwe int
300 1.22 uwe bkgrnd(const cchar_t *wch)
301 1.21 uwe {
302 1.21 uwe return ERR;
303 1.21 uwe }
304 1.21 uwe
305 1.21 uwe
306 1.21 uwe int
307 1.22 uwe getbkgrnd(cchar_t *wch)
308 1.21 uwe {
309 1.21 uwe return ERR;
310 1.21 uwe }
311 1.21 uwe
312 1.21 uwe
313 1.21 uwe void
314 1.21 uwe wbkgrndset(WINDOW *win, const cchar_t *wch)
315 1.21 uwe {
316 1.21 uwe return;
317 1.21 uwe }
318 1.21 uwe
319 1.21 uwe
320 1.21 uwe int
321 1.22 uwe wbkgrnd(WINDOW *win, const cchar_t *wch)
322 1.22 uwe {
323 1.22 uwe return ERR;
324 1.22 uwe }
325 1.22 uwe
326 1.22 uwe
327 1.22 uwe int
328 1.21 uwe wgetbkgrnd(WINDOW *win, cchar_t *wch)
329 1.21 uwe {
330 1.21 uwe return ERR;
331 1.21 uwe }
332 1.21 uwe
333 1.21 uwe #endif /* !HAVE_WCHAR */
334