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