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