#include <sys/cdefs.h>
#ifndef lint
__RCSID("$NetBSD: attributes.c,v 1.37 2026/04/01 20:32:56 hgutch Exp $");
#endif
#include "curses.h"
#include "curses_private.h"
static int __wattr_off(WINDOW *, attr_t);
static int __wattr_on(WINDOW *, attr_t);
static void __wcolor_set(WINDOW *, attr_t);
#ifndef _CURSES_USE_MACROS
#ifdef HAVE_WCHAR
int
attr_get(attr_t *attr, short *pair, void *opts)
{
return wattr_get(stdscr, attr, pair, opts);
}
int
attr_on(attr_t attr, void *opts)
{
return wattr_on(stdscr, attr, opts);
}
int
attr_off(attr_t attr, void *opts)
{
return wattr_off(stdscr, attr, opts);
}
int
attr_set(attr_t attr, short pair, void *opts)
{
return wattr_set(stdscr, attr, pair, opts);
}
int
color_set(short pair, void *opts)
{
return wcolor_set(stdscr, pair, opts);
}
#endif
int
attron(int attr)
{
return wattron(stdscr, attr);
}
int
attroff(int attr)
{
return wattroff(stdscr, attr);
}
int
attrset(int attr)
{
return wattrset(stdscr, attr);
}
#endif
#ifdef HAVE_WCHAR
int
wattr_get(WINDOW *win, attr_t *attr, short *pair, void *opts)
{
if (__predict_false(win == NULL))
return ERR;
__CTRACE(__CTRACE_ATTR, "wattr_get: win %p\n", win);
if (attr != NULL) {
*attr = win->wattr;
#ifdef HAVE_WCHAR
*attr &= WA_ATTRIBUTES;
#endif
}
if (pair != NULL)
*pair = PAIR_NUMBER(win->wattr);
return OK;
}
int
wattr_on(WINDOW *win, attr_t attr, void *opts)
{
if (__predict_false(opts != NULL))
return ERR;
return __wattr_on(win, attr);
}
int
wattr_off(WINDOW *win, attr_t attr, void *opts)
{
if (__predict_false(opts != NULL))
return ERR;
return __wattr_off(win, attr);
}
int
wattr_set(WINDOW *win, attr_t attr, short pair, void *opts)
{
__CTRACE(__CTRACE_ATTR, "wattr_set: win %p, attr %08x, pair %d\n",
win, attr, pair);
if (__predict_false(opts != NULL))
return ERR;
attr = (attr & ~__COLOR) | COLOR_PAIR(pair);
__wattr_off(win, WA_ATTRIBUTES);
__wattr_on(win, attr);
return OK;
}
int
wcolor_set(WINDOW *win, short pair, void *opts)
{
__CTRACE(__CTRACE_COLOR, "wcolor_set: win %p, pair %d\n", win, pair);
if ((__predict_false(win == NULL)) || (__predict_false(opts != NULL)))
return ERR;
__wcolor_set(win, (attr_t) COLOR_PAIR(pair));
return OK;
}
#endif
chtype
getattrs(WINDOW *win)
{
if (__predict_false(win == NULL))
return ERR;
__CTRACE(__CTRACE_ATTR, "getattrs: win %p\n", win);
return((chtype) win->wattr);
}
int
wattron(WINDOW *win, int attr)
{
__CTRACE(__CTRACE_ATTR, "wattron: win %p, attr %08x\n", win, attr);
return __wattr_on(win, (attr_t) attr);
}
int
wattroff(WINDOW *win, int attr)
{
__CTRACE(__CTRACE_ATTR, "wattroff: win %p, attr %08x\n", win, attr);
return __wattr_off(win, (attr_t) attr);
}
int
wattrset(WINDOW *win, int attr)
{
__CTRACE(__CTRACE_ATTR, "wattrset: win %p, attr %08x\n", win, attr);
__wattr_off(win, __ATTRIBUTES);
__wattr_on(win, (attr_t) attr);
return OK;
}
chtype
termattrs(void)
{
chtype ch = 0;
__CTRACE(__CTRACE_ATTR, "termattrs\n");
if (exit_attribute_mode != NULL) {
__CTRACE(__CTRACE_ATTR, "termattrs: have exit attribute mode\n");
if (enter_blink_mode != NULL)
ch |= __BLINK;
if (enter_bold_mode != NULL)
ch |= __BOLD;
if (enter_dim_mode != NULL)
ch |= __DIM;
if (enter_secure_mode != NULL)
ch |= __BLANK;
if (enter_protected_mode != NULL)
ch |= __PROTECT;
if (enter_reverse_mode != NULL)
ch |= __REVERSE;
}
if (enter_standout_mode != NULL && exit_standout_mode != NULL)
ch |= __STANDOUT;
if (enter_underline_mode != NULL && exit_underline_mode != NULL)
ch |= __UNDERSCORE;
if (enter_alt_charset_mode != NULL && exit_alt_charset_mode != NULL)
ch |= __ALTCHARSET;
return ch;
}
#ifdef HAVE_WCHAR
attr_t
term_attrs(void)
{
attr_t attr = 0;
__CTRACE(__CTRACE_ATTR, "term_attrs\n");
if (exit_attribute_mode != NULL) {
if (enter_blink_mode != NULL)
attr |= __BLINK;
if (enter_bold_mode != NULL)
attr |= __BOLD;
if (enter_dim_mode != NULL)
attr |= __DIM;
if (enter_secure_mode != NULL)
attr |= __BLANK;
if (enter_protected_mode != NULL)
attr |= __PROTECT;
if (enter_reverse_mode != NULL)
attr |= __REVERSE;
#ifdef HAVE_WCHAR
if (enter_low_hl_mode != NULL)
attr |= WA_LOW;
if (enter_top_hl_mode != NULL)
attr |= WA_TOP;
if (enter_left_hl_mode != NULL)
attr |= WA_LEFT;
if (enter_right_hl_mode != NULL)
attr |= WA_RIGHT;
if (enter_horizontal_hl_mode != NULL)
attr |= WA_HORIZONTAL;
if (enter_vertical_hl_mode != NULL)
attr |= WA_VERTICAL;
#endif
}
if (enter_standout_mode != NULL && exit_standout_mode != NULL)
attr |= __STANDOUT;
if (enter_underline_mode != NULL && exit_underline_mode != NULL)
attr |= __UNDERSCORE;
if (enter_alt_charset_mode != NULL && exit_alt_charset_mode != NULL)
attr |= __ALTCHARSET;
return attr;
}
#endif
static int
__wattr_on(WINDOW *win, attr_t attr)
{
const TERMINAL *t;
if (__predict_false(win == NULL))
return ERR;
t = win->screen->term;
__CTRACE(__CTRACE_ATTR, "wattr_on: win %p, attr %08x\n", win, attr);
if (t_exit_attribute_mode(t) != NULL) {
if (attr & __BLINK && t_enter_blink_mode(t) != NULL)
win->wattr |= __BLINK;
if (attr & __BOLD && t_enter_bold_mode(t) != NULL)
win->wattr |= __BOLD;
if (attr & __DIM && t_enter_dim_mode(t) != NULL)
win->wattr |= __DIM;
if (attr & __BLANK && t_enter_secure_mode(t) != NULL)
win->wattr |= __BLANK;
if (attr & __PROTECT && t_enter_protected_mode(t) != NULL)
win->wattr |= __PROTECT;
if (attr & __REVERSE && t_enter_reverse_mode(t) != NULL)
win->wattr |= __REVERSE;
#ifdef HAVE_WCHAR
if (attr & WA_LOW && t_enter_low_hl_mode(t) != NULL)
win->wattr |= WA_LOW;
if (attr & WA_TOP && t_enter_top_hl_mode(t) != NULL)
win->wattr |= WA_TOP;
if (attr & WA_LEFT && t_enter_left_hl_mode(t) != NULL)
win->wattr |= WA_LEFT;
if (attr & WA_RIGHT && t_enter_right_hl_mode(t) != NULL)
win->wattr |= WA_RIGHT;
if (attr & WA_HORIZONTAL && t_enter_horizontal_hl_mode(t) != NULL)
win->wattr |= WA_HORIZONTAL;
if (attr & WA_VERTICAL && t_enter_vertical_hl_mode(t) != NULL)
win->wattr |= WA_VERTICAL;
#endif
}
if (attr & __STANDOUT && t_enter_standout_mode(t) != NULL &&
t_exit_standout_mode(t) != NULL)
wstandout(win);
if (attr & __UNDERSCORE && t_enter_underline_mode(t) != NULL &&
t_exit_underline_mode(t) != NULL)
wunderscore(win);
if (attr & __COLOR)
__wcolor_set(win, attr);
return OK;
}
static int
__wattr_off(WINDOW *win, attr_t attr)
{
const TERMINAL *t;
if (__predict_false(win == NULL))
return ERR;
t = win->screen->term;
__CTRACE(__CTRACE_ATTR, "wattr_off: win %p, attr %08x\n", win, attr);
if (t_exit_attribute_mode(t) != NULL) {
if (attr & __BLINK)
win->wattr &= ~__BLINK;
if (attr & __BOLD)
win->wattr &= ~__BOLD;
if (attr & __DIM)
win->wattr &= ~__DIM;
if (attr & __BLANK)
win->wattr &= ~__BLANK;
if (attr & __PROTECT)
win->wattr &= ~__PROTECT;
if (attr & __REVERSE)
win->wattr &= ~__REVERSE;
#ifdef HAVE_WCHAR
if (attr & WA_LOW)
win->wattr &= ~WA_LOW;
if (attr & WA_TOP)
win->wattr &= ~WA_TOP;
if (attr & WA_LEFT)
win->wattr &= ~WA_LEFT;
if (attr & WA_RIGHT)
win->wattr &= ~WA_RIGHT;
if (attr & WA_HORIZONTAL)
win->wattr &= ~WA_HORIZONTAL;
if (attr & WA_VERTICAL)
win->wattr &= ~WA_VERTICAL;
#endif
}
if (attr & __STANDOUT)
wstandend(win);
if (attr & __UNDERSCORE)
wunderend(win);
if (attr & __COLOR) {
if (max_colors != 0)
win->wattr &= ~__COLOR;
}
return OK;
}
static void
__wcolor_set(WINDOW *win, attr_t attr)
{
const TERMINAL *t;
if (__predict_false(win == NULL))
return;
t = win->screen->term;
win->wattr &= ~__COLOR;
if (t_max_colors(t) != 0)
win->wattr |= attr & __COLOR;
}