attributes.c revision 1.32 1 1.32 rin /* $NetBSD: attributes.c,v 1.32 2021/09/06 07:03:49 rin Exp $ */
2 1.1 mrg
3 1.3 jdc /*-
4 1.3 jdc * Copyright (c) 1999 The NetBSD Foundation, Inc.
5 1.1 mrg * All rights reserved.
6 1.1 mrg *
7 1.3 jdc * This code is derived from software contributed to The NetBSD Foundation
8 1.3 jdc * by Julian Coleman.
9 1.3 jdc *
10 1.1 mrg * Redistribution and use in source and binary forms, with or without
11 1.1 mrg * modification, are permitted provided that the following conditions
12 1.1 mrg * are met:
13 1.1 mrg * 1. Redistributions of source code must retain the above copyright
14 1.1 mrg * notice, this list of conditions and the following disclaimer.
15 1.3 jdc * 2. Redistributions in binary form must reproduce the above copyright
16 1.3 jdc * notice, this list of conditions and the following disclaimer in the
17 1.3 jdc * documentation and/or other materials provided with the distribution.
18 1.1 mrg *
19 1.3 jdc * THIS SOFTWARE IS PROVIDED BY THE NETBSD FOUNDATION, INC. AND CONTRIBUTORS
20 1.3 jdc * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED
21 1.3 jdc * TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
22 1.3 jdc * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE FOUNDATION OR CONTRIBUTORS
23 1.3 jdc * BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
24 1.3 jdc * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
25 1.3 jdc * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
26 1.3 jdc * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
27 1.3 jdc * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
28 1.3 jdc * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
29 1.3 jdc * POSSIBILITY OF SUCH DAMAGE.
30 1.1 mrg */
31 1.6 blymn
32 1.6 blymn #include <sys/cdefs.h>
33 1.6 blymn #ifndef lint
34 1.32 rin __RCSID("$NetBSD: attributes.c,v 1.32 2021/09/06 07:03:49 rin Exp $");
35 1.6 blymn #endif /* not lint */
36 1.1 mrg
37 1.1 mrg #include "curses.h"
38 1.2 blymn #include "curses_private.h"
39 1.1 mrg
40 1.27 uwe static int __wattr_off(WINDOW *, attr_t);
41 1.27 uwe static int __wattr_on(WINDOW *, attr_t);
42 1.25 uwe static void __wcolor_set(WINDOW *, attr_t);
43 1.12 jdc
44 1.27 uwe
45 1.4 blymn #ifndef _CURSES_USE_MACROS
46 1.31 uwe #ifdef HAVE_WCHAR
47 1.4 blymn /*
48 1.12 jdc * attr_get --
49 1.16 jdc * Get wide attributes and color pair from stdscr
50 1.12 jdc */
51 1.12 jdc /* ARGSUSED */
52 1.12 jdc int
53 1.28 uwe attr_get(attr_t *attr, short *pair, void *opts)
54 1.12 jdc {
55 1.28 uwe return wattr_get(stdscr, attr, pair, opts);
56 1.12 jdc }
57 1.12 jdc
58 1.12 jdc /*
59 1.12 jdc * attr_on --
60 1.16 jdc * Test and set wide attributes on stdscr
61 1.12 jdc */
62 1.12 jdc /* ARGSUSED */
63 1.12 jdc int
64 1.28 uwe attr_on(attr_t attr, void *opts)
65 1.12 jdc {
66 1.28 uwe return wattr_on(stdscr, attr, opts);
67 1.12 jdc }
68 1.12 jdc
69 1.12 jdc /*
70 1.12 jdc * attr_off --
71 1.16 jdc * Test and unset wide attributes on stdscr
72 1.12 jdc */
73 1.12 jdc /* ARGSUSED */
74 1.12 jdc int
75 1.28 uwe attr_off(attr_t attr, void *opts)
76 1.12 jdc {
77 1.28 uwe return wattr_off(stdscr, attr, opts);
78 1.12 jdc }
79 1.12 jdc
80 1.12 jdc /*
81 1.12 jdc * attr_set --
82 1.16 jdc * Set wide attributes and color pair on stdscr
83 1.12 jdc */
84 1.12 jdc /* ARGSUSED */
85 1.12 jdc int
86 1.28 uwe attr_set(attr_t attr, short pair, void *opts)
87 1.12 jdc {
88 1.28 uwe return wattr_set(stdscr, attr, pair, opts);
89 1.12 jdc }
90 1.12 jdc
91 1.12 jdc /*
92 1.12 jdc * color_set --
93 1.12 jdc * Set color pair on stdscr
94 1.12 jdc */
95 1.12 jdc /* ARGSUSED */
96 1.12 jdc int
97 1.28 uwe color_set(short pair, void *opts)
98 1.12 jdc {
99 1.28 uwe return wcolor_set(stdscr, pair, opts);
100 1.12 jdc }
101 1.31 uwe #endif /* HAVE_WCHAR */
102 1.12 jdc
103 1.12 jdc /*
104 1.8 jdc * attron --
105 1.4 blymn * Test and set attributes on stdscr
106 1.4 blymn */
107 1.4 blymn int
108 1.4 blymn attron(int attr)
109 1.4 blymn {
110 1.30 uwe return wattron(stdscr, attr);
111 1.4 blymn }
112 1.4 blymn
113 1.4 blymn /*
114 1.8 jdc * attroff --
115 1.4 blymn * Test and unset attributes on stdscr.
116 1.4 blymn */
117 1.4 blymn int
118 1.4 blymn attroff(int attr)
119 1.4 blymn {
120 1.30 uwe return wattroff(stdscr, attr);
121 1.4 blymn }
122 1.4 blymn
123 1.4 blymn /*
124 1.8 jdc * attrset --
125 1.4 blymn * Set specific attribute modes.
126 1.4 blymn * Unset others. On stdscr.
127 1.4 blymn */
128 1.4 blymn int
129 1.4 blymn attrset(int attr)
130 1.4 blymn {
131 1.4 blymn return wattrset(stdscr, attr);
132 1.4 blymn }
133 1.12 jdc #endif /* _CURSES_USE_MACROS */
134 1.4 blymn
135 1.31 uwe
136 1.31 uwe #ifdef HAVE_WCHAR
137 1.12 jdc /*
138 1.12 jdc * wattr_get --
139 1.16 jdc * Get wide attributes and colour pair from window
140 1.12 jdc * Note that attributes also includes colour.
141 1.12 jdc */
142 1.12 jdc /* ARGSUSED */
143 1.12 jdc int
144 1.28 uwe wattr_get(WINDOW *win, attr_t *attr, short *pair, void *opts)
145 1.12 jdc {
146 1.14 jdc __CTRACE(__CTRACE_ATTR, "wattr_get: win %p\n", win);
147 1.15 blymn if (attr != NULL) {
148 1.12 jdc *attr = win->wattr;
149 1.15 blymn #ifdef HAVE_WCHAR
150 1.15 blymn *attr &= WA_ATTRIBUTES;
151 1.15 blymn #endif
152 1.15 blymn }
153 1.15 blymn
154 1.12 jdc if (pair != NULL)
155 1.12 jdc *pair = PAIR_NUMBER(win->wattr);
156 1.12 jdc return OK;
157 1.12 jdc }
158 1.4 blymn
159 1.1 mrg /*
160 1.12 jdc * wattr_on --
161 1.16 jdc * Test and set wide attributes on window
162 1.1 mrg */
163 1.1 mrg int
164 1.28 uwe wattr_on(WINDOW *win, attr_t attr, void *opts)
165 1.1 mrg {
166 1.28 uwe if (__predict_false(opts != NULL))
167 1.27 uwe return ERR;
168 1.23 roy
169 1.27 uwe return __wattr_on(win, attr);
170 1.1 mrg }
171 1.1 mrg
172 1.1 mrg /*
173 1.12 jdc * wattr_off --
174 1.16 jdc * Test and unset wide attributes on window
175 1.1 mrg *
176 1.1 mrg * Note that the 'me' sequence unsets all attributes. We handle
177 1.1 mrg * which attributes should really be set in refresh.c:makech().
178 1.1 mrg */
179 1.1 mrg int
180 1.28 uwe wattr_off(WINDOW *win, attr_t attr, void *opts)
181 1.1 mrg {
182 1.28 uwe if (__predict_false(opts != NULL))
183 1.27 uwe return ERR;
184 1.23 roy
185 1.27 uwe return __wattr_off(win, attr);
186 1.12 jdc }
187 1.12 jdc
188 1.27 uwe
189 1.12 jdc /*
190 1.12 jdc * wattr_set --
191 1.16 jdc * Set wide attributes and color pair on window
192 1.12 jdc */
193 1.12 jdc int
194 1.28 uwe wattr_set(WINDOW *win, attr_t attr, short pair, void *opts)
195 1.12 jdc {
196 1.14 jdc __CTRACE(__CTRACE_ATTR, "wattr_set: win %p, attr %08x, pair %d\n",
197 1.14 jdc win, attr, pair);
198 1.28 uwe if (__predict_false(opts != NULL))
199 1.27 uwe return ERR;
200 1.27 uwe
201 1.12 jdc /*
202 1.15 blymn * This overwrites any colour setting from the attributes
203 1.12 jdc * and is compatible with ncurses.
204 1.12 jdc */
205 1.22 roy attr = (attr & ~__COLOR) | COLOR_PAIR(pair);
206 1.27 uwe
207 1.29 uwe __wattr_off(win, WA_ATTRIBUTES);
208 1.27 uwe __wattr_on(win, attr);
209 1.12 jdc return OK;
210 1.12 jdc }
211 1.12 jdc
212 1.12 jdc /*
213 1.25 uwe * wcolor_set --
214 1.25 uwe * Set color pair on window
215 1.25 uwe */
216 1.25 uwe /* ARGSUSED */
217 1.25 uwe int
218 1.28 uwe wcolor_set(WINDOW *win, short pair, void *opts)
219 1.25 uwe {
220 1.25 uwe __CTRACE(__CTRACE_COLOR, "wolor_set: win %p, pair %d\n", win, pair);
221 1.25 uwe __wcolor_set(win, (attr_t) COLOR_PAIR(pair));
222 1.25 uwe return OK;
223 1.25 uwe }
224 1.31 uwe #endif /* HAVE_WCHAR */
225 1.31 uwe
226 1.25 uwe
227 1.25 uwe /*
228 1.25 uwe * getattrs --
229 1.25 uwe * Get window attributes.
230 1.25 uwe */
231 1.25 uwe chtype
232 1.25 uwe getattrs(WINDOW *win)
233 1.25 uwe {
234 1.25 uwe __CTRACE(__CTRACE_ATTR, "getattrs: win %p\n", win);
235 1.25 uwe return((chtype) win->wattr);
236 1.25 uwe }
237 1.25 uwe
238 1.25 uwe /*
239 1.12 jdc * wattron --
240 1.12 jdc * Test and set attributes.
241 1.12 jdc */
242 1.12 jdc int
243 1.12 jdc wattron(WINDOW *win, int attr)
244 1.12 jdc {
245 1.14 jdc __CTRACE(__CTRACE_ATTR, "wattron: win %p, attr %08x\n", win, attr);
246 1.27 uwe return __wattr_on(win, (attr_t) attr);
247 1.12 jdc }
248 1.12 jdc
249 1.12 jdc /*
250 1.12 jdc * wattroff --
251 1.12 jdc * Test and unset attributes.
252 1.12 jdc */
253 1.12 jdc int
254 1.12 jdc wattroff(WINDOW *win, int attr)
255 1.12 jdc {
256 1.14 jdc __CTRACE(__CTRACE_ATTR, "wattroff: win %p, attr %08x\n", win, attr);
257 1.27 uwe return __wattr_off(win, (attr_t) attr);
258 1.1 mrg }
259 1.1 mrg
260 1.1 mrg /*
261 1.8 jdc * wattrset --
262 1.1 mrg * Set specific attribute modes.
263 1.1 mrg * Unset others.
264 1.1 mrg */
265 1.1 mrg int
266 1.4 blymn wattrset(WINDOW *win, int attr)
267 1.1 mrg {
268 1.14 jdc __CTRACE(__CTRACE_ATTR, "wattrset: win %p, attr %08x\n", win, attr);
269 1.27 uwe __wattr_off(win, __ATTRIBUTES);
270 1.27 uwe __wattr_on(win, (attr_t) attr);
271 1.12 jdc return OK;
272 1.12 jdc }
273 1.12 jdc
274 1.12 jdc /*
275 1.16 jdc * termattrs --
276 1.16 jdc * Get terminal attributes
277 1.16 jdc */
278 1.16 jdc chtype
279 1.16 jdc termattrs(void)
280 1.16 jdc {
281 1.19 yamt chtype ch = 0;
282 1.19 yamt
283 1.16 jdc __CTRACE(__CTRACE_ATTR, "termattrs\n");
284 1.20 roy if (exit_attribute_mode != NULL) {
285 1.21 blymn __CTRACE(__CTRACE_ATTR, "termattrs: have exit attribute mode\n");
286 1.20 roy if (enter_blink_mode != NULL)
287 1.16 jdc ch |= __BLINK;
288 1.20 roy if (enter_bold_mode != NULL)
289 1.16 jdc ch |= __BOLD;
290 1.20 roy if (enter_dim_mode != NULL)
291 1.16 jdc ch |= __DIM;
292 1.20 roy if (enter_secure_mode != NULL)
293 1.16 jdc ch |= __BLANK;
294 1.20 roy if (enter_protected_mode != NULL)
295 1.16 jdc ch |= __PROTECT;
296 1.20 roy if (enter_reverse_mode != NULL)
297 1.16 jdc ch |= __REVERSE;
298 1.16 jdc }
299 1.20 roy if (enter_standout_mode != NULL && exit_standout_mode != NULL)
300 1.16 jdc ch |= __STANDOUT;
301 1.20 roy if (enter_underline_mode != NULL && exit_underline_mode != NULL)
302 1.16 jdc ch |= __UNDERSCORE;
303 1.20 roy if (enter_alt_charset_mode != NULL && exit_alt_charset_mode != NULL)
304 1.16 jdc ch |= __ALTCHARSET;
305 1.16 jdc
306 1.16 jdc return ch;
307 1.16 jdc }
308 1.16 jdc
309 1.31 uwe
310 1.31 uwe #ifdef HAVE_WCHAR
311 1.16 jdc /*
312 1.16 jdc * term_attrs --
313 1.16 jdc * Get terminal wide attributes
314 1.16 jdc */
315 1.16 jdc attr_t
316 1.16 jdc term_attrs(void)
317 1.16 jdc {
318 1.19 yamt attr_t attr = 0;
319 1.19 yamt
320 1.16 jdc __CTRACE(__CTRACE_ATTR, "term_attrs\n");
321 1.20 roy if (exit_attribute_mode != NULL) {
322 1.20 roy if (enter_blink_mode != NULL)
323 1.17 jdc attr |= __BLINK;
324 1.20 roy if (enter_bold_mode != NULL)
325 1.17 jdc attr |= __BOLD;
326 1.20 roy if (enter_dim_mode != NULL)
327 1.17 jdc attr |= __DIM;
328 1.20 roy if (enter_secure_mode != NULL)
329 1.17 jdc attr |= __BLANK;
330 1.20 roy if (enter_protected_mode != NULL)
331 1.17 jdc attr |= __PROTECT;
332 1.20 roy if (enter_reverse_mode != NULL)
333 1.17 jdc attr |= __REVERSE;
334 1.16 jdc #ifdef HAVE_WCHAR
335 1.20 roy if (enter_low_hl_mode != NULL)
336 1.16 jdc attr |= WA_LOW;
337 1.20 roy if (enter_top_hl_mode != NULL)
338 1.16 jdc attr |= WA_TOP;
339 1.20 roy if (enter_left_hl_mode != NULL)
340 1.16 jdc attr |= WA_LEFT;
341 1.20 roy if (enter_right_hl_mode != NULL)
342 1.16 jdc attr |= WA_RIGHT;
343 1.20 roy if (enter_horizontal_hl_mode != NULL)
344 1.16 jdc attr |= WA_HORIZONTAL;
345 1.20 roy if (enter_vertical_hl_mode != NULL)
346 1.16 jdc attr |= WA_VERTICAL;
347 1.16 jdc #endif /* HAVE_WCHAR */
348 1.16 jdc }
349 1.20 roy if (enter_standout_mode != NULL && exit_standout_mode != NULL)
350 1.17 jdc attr |= __STANDOUT;
351 1.20 roy if (enter_underline_mode != NULL && exit_underline_mode != NULL)
352 1.17 jdc attr |= __UNDERSCORE;
353 1.20 roy if (enter_alt_charset_mode != NULL && exit_alt_charset_mode != NULL)
354 1.17 jdc attr |= __ALTCHARSET;
355 1.16 jdc
356 1.16 jdc return attr;
357 1.16 jdc }
358 1.31 uwe #endif /* HAVE_WCHAR */
359 1.16 jdc
360 1.27 uwe
361 1.27 uwe static int
362 1.27 uwe __wattr_on(WINDOW *win, attr_t attr)
363 1.27 uwe {
364 1.27 uwe const TERMINAL *t = win->screen->term;
365 1.27 uwe
366 1.27 uwe __CTRACE(__CTRACE_ATTR, "wattr_on: win %p, attr %08x\n", win, attr);
367 1.27 uwe /* If can enter modes, set the relevent attribute bits. */
368 1.27 uwe if (t_exit_attribute_mode(t) != NULL) {
369 1.27 uwe if (attr & __BLINK && t_enter_blink_mode(t) != NULL)
370 1.27 uwe win->wattr |= __BLINK;
371 1.27 uwe if (attr & __BOLD && t_enter_bold_mode(t) != NULL)
372 1.27 uwe win->wattr |= __BOLD;
373 1.27 uwe if (attr & __DIM && t_enter_dim_mode(t) != NULL)
374 1.27 uwe win->wattr |= __DIM;
375 1.27 uwe if (attr & __BLANK && t_enter_secure_mode(t) != NULL)
376 1.27 uwe win->wattr |= __BLANK;
377 1.27 uwe if (attr & __PROTECT && t_enter_protected_mode(t) != NULL)
378 1.27 uwe win->wattr |= __PROTECT;
379 1.27 uwe if (attr & __REVERSE && t_enter_reverse_mode(t) != NULL)
380 1.27 uwe win->wattr |= __REVERSE;
381 1.27 uwe #ifdef HAVE_WCHAR
382 1.27 uwe if (attr & WA_LOW && t_enter_low_hl_mode(t) != NULL)
383 1.27 uwe win->wattr |= WA_LOW;
384 1.27 uwe if (attr & WA_TOP && t_enter_top_hl_mode(t) != NULL)
385 1.27 uwe win->wattr |= WA_TOP;
386 1.27 uwe if (attr & WA_LEFT && t_enter_left_hl_mode(t) != NULL)
387 1.27 uwe win->wattr |= WA_LEFT;
388 1.27 uwe if (attr & WA_RIGHT && t_enter_right_hl_mode(t) != NULL)
389 1.27 uwe win->wattr |= WA_RIGHT;
390 1.27 uwe if (attr & WA_HORIZONTAL && t_enter_horizontal_hl_mode(t) != NULL)
391 1.27 uwe win->wattr |= WA_HORIZONTAL;
392 1.27 uwe if (attr & WA_VERTICAL && t_enter_vertical_hl_mode(t) != NULL)
393 1.27 uwe win->wattr |= WA_VERTICAL;
394 1.27 uwe #endif /* HAVE_WCHAR */
395 1.27 uwe }
396 1.27 uwe if (attr & __STANDOUT && t_enter_standout_mode(t) != NULL &&
397 1.27 uwe t_exit_standout_mode(t) != NULL)
398 1.27 uwe wstandout(win);
399 1.27 uwe if (attr & __UNDERSCORE && t_enter_underline_mode(t) != NULL &&
400 1.27 uwe t_exit_underline_mode(t) != NULL)
401 1.27 uwe wunderscore(win);
402 1.27 uwe if (attr & __COLOR)
403 1.27 uwe __wcolor_set(win, attr);
404 1.27 uwe return OK;
405 1.27 uwe }
406 1.27 uwe
407 1.27 uwe
408 1.27 uwe static int
409 1.27 uwe __wattr_off(WINDOW *win, attr_t attr)
410 1.27 uwe {
411 1.27 uwe const TERMINAL *t = win->screen->term;
412 1.27 uwe
413 1.27 uwe __CTRACE(__CTRACE_ATTR, "wattr_off: win %p, attr %08x\n", win, attr);
414 1.27 uwe /* If can do exit modes, unset the relevent attribute bits. */
415 1.27 uwe if (t_exit_attribute_mode(t) != NULL) {
416 1.27 uwe if (attr & __BLINK)
417 1.27 uwe win->wattr &= ~__BLINK;
418 1.27 uwe if (attr & __BOLD)
419 1.27 uwe win->wattr &= ~__BOLD;
420 1.27 uwe if (attr & __DIM)
421 1.27 uwe win->wattr &= ~__DIM;
422 1.27 uwe if (attr & __BLANK)
423 1.27 uwe win->wattr &= ~__BLANK;
424 1.27 uwe if (attr & __PROTECT)
425 1.27 uwe win->wattr &= ~__PROTECT;
426 1.27 uwe if (attr & __REVERSE)
427 1.27 uwe win->wattr &= ~__REVERSE;
428 1.27 uwe #ifdef HAVE_WCHAR
429 1.27 uwe if (attr & WA_LOW)
430 1.27 uwe win->wattr &= ~WA_LOW;
431 1.27 uwe if (attr & WA_TOP)
432 1.27 uwe win->wattr &= ~WA_TOP;
433 1.27 uwe if (attr & WA_LEFT)
434 1.27 uwe win->wattr &= ~WA_LEFT;
435 1.27 uwe if (attr & WA_RIGHT)
436 1.27 uwe win->wattr &= ~WA_RIGHT;
437 1.27 uwe if (attr & WA_HORIZONTAL)
438 1.27 uwe win->wattr &= ~WA_HORIZONTAL;
439 1.27 uwe if (attr & WA_VERTICAL)
440 1.27 uwe win->wattr &= ~WA_VERTICAL;
441 1.27 uwe #endif /* HAVE_WCHAR */
442 1.27 uwe }
443 1.27 uwe if (attr & __STANDOUT)
444 1.27 uwe wstandend(win);
445 1.27 uwe if (attr & __UNDERSCORE)
446 1.27 uwe wunderend(win);
447 1.27 uwe if (attr & __COLOR) {
448 1.27 uwe if (max_colors != 0)
449 1.27 uwe win->wattr &= ~__COLOR;
450 1.27 uwe }
451 1.27 uwe return OK;
452 1.27 uwe }
453 1.27 uwe
454 1.27 uwe
455 1.25 uwe static void
456 1.12 jdc __wcolor_set(WINDOW *win, attr_t attr)
457 1.12 jdc {
458 1.23 roy const TERMINAL *t = win->screen->term;
459 1.23 roy
460 1.12 jdc /* If another color pair is set, turn that off first. */
461 1.12 jdc win->wattr &= ~__COLOR;
462 1.12 jdc /* If can do color video, set the color pair bits. */
463 1.23 roy if (t_max_colors(t) != 0 && attr & __COLOR)
464 1.12 jdc win->wattr |= attr & __COLOR;
465 1.1 mrg }
466