1 /* $OpenBSD: lib_add_wch.c,v 1.2 2023/10/17 09:52:09 nicm Exp $ */ 2 3 /**************************************************************************** 4 * Copyright 2019-2021,2023 Thomas E. Dickey * 5 * Copyright 2004-2011,2016 Free Software Foundation, Inc. * 6 * * 7 * Permission is hereby granted, free of charge, to any person obtaining a * 8 * copy of this software and associated documentation files (the * 9 * "Software"), to deal in the Software without restriction, including * 10 * without limitation the rights to use, copy, modify, merge, publish, * 11 * distribute, distribute with modifications, sublicense, and/or sell * 12 * copies of the Software, and to permit persons to whom the Software is * 13 * furnished to do so, subject to the following conditions: * 14 * * 15 * The above copyright notice and this permission notice shall be included * 16 * in all copies or substantial portions of the Software. * 17 * * 18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * 19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * 20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * 21 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 22 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 23 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * 24 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. * 25 * * 26 * Except as contained in this notice, the name(s) of the above copyright * 27 * holders shall not be used in advertising or otherwise to promote the * 28 * sale, use or other dealings in this Software without prior written * 29 * authorization. * 30 ****************************************************************************/ 31 32 /* 33 ** lib_add_wch.c 34 ** 35 ** The routine wadd_wch(). 36 ** 37 */ 38 39 #include <curses.priv.h> 40 41 #if HAVE_WCTYPE_H 42 #include <wctype.h> 43 #endif 44 45 MODULE_ID("$Id: lib_add_wch.c,v 1.2 2023/10/17 09:52:09 nicm Exp $") 46 47 /* clone/adapt lib_addch.c */ 48 static const cchar_t blankchar = NewChar(BLANK_TEXT); 49 50 /* 51 * Ugly microtweaking alert. Everything from here to end of module is 52 * likely to be speed-critical -- profiling data sure says it is! 53 * Most of the important screen-painting functions are shells around 54 * wadd_wch(). So we make every effort to reduce function-call overhead 55 * by inlining stuff, even at the cost of making wrapped copies for 56 * export. Also we supply some internal versions that don't call the 57 * window sync hook, for use by string-put functions. 58 */ 59 60 /* Return bit mask for clearing color pair number if given ch has color */ 61 #define COLOR_MASK(ch) (~(attr_t)(((ch) & A_COLOR) ? A_COLOR : 0)) 62 63 static NCURSES_INLINE cchar_t 64 render_char(WINDOW *win, cchar_t ch) 65 /* compute a rendition of the given char correct for the current context */ 66 { 67 attr_t a = WINDOW_ATTRS(win); 68 int pair = GetPair(ch); 69 70 if (ISBLANK(ch) 71 && AttrOf(ch) == A_NORMAL 72 && pair == 0) { 73 /* color/pair in attrs has precedence over bkgrnd */ 74 ch = win->_nc_bkgd; 75 SetAttr(ch, a | AttrOf(win->_nc_bkgd)); 76 if ((pair = GET_WINDOW_PAIR(win)) == 0) 77 pair = GetPair(win->_nc_bkgd); 78 SetPair(ch, pair); 79 } else { 80 /* color in attrs has precedence over bkgrnd */ 81 a |= AttrOf(win->_nc_bkgd) & COLOR_MASK(a); 82 /* color in ch has precedence */ 83 if (pair == 0) { 84 if ((pair = GET_WINDOW_PAIR(win)) == 0) 85 pair = GetPair(win->_nc_bkgd); 86 } 87 AddAttr(ch, (a & COLOR_MASK(AttrOf(ch)))); 88 SetPair(ch, pair); 89 } 90 91 TR(TRACE_VIRTPUT, 92 ("render_char bkg %s (%d), attrs %s (%d) -> ch %s (%d)", 93 _tracech_t2(1, CHREF(win->_nc_bkgd)), 94 GetPair(win->_nc_bkgd), 95 _traceattr(WINDOW_ATTRS(win)), 96 GET_WINDOW_PAIR(win), 97 _tracech_t2(3, CHREF(ch)), 98 GetPair(ch))); 99 100 return (ch); 101 } 102 103 /* check if position is legal; if not, return error */ 104 #ifndef NDEBUG /* treat this like an assertion */ 105 #define CHECK_POSITION(win, x, y) \ 106 if (y > win->_maxy \ 107 || x > win->_maxx \ 108 || y < 0 \ 109 || x < 0) { \ 110 TR(TRACE_VIRTPUT, ("Alert! Win=%p _curx = %d, _cury = %d " \ 111 "(_maxx = %d, _maxy = %d)", win, x, y, \ 112 win->_maxx, win->_maxy)); \ 113 return(ERR); \ 114 } 115 #else 116 #define CHECK_POSITION(win, x, y) /* nothing */ 117 #endif 118 119 static bool 120 newline_forces_scroll(WINDOW *win, NCURSES_SIZE_T *ypos) 121 { 122 bool result = FALSE; 123 124 if (*ypos >= win->_regtop && *ypos == win->_regbottom) { 125 *ypos = win->_regbottom; 126 result = TRUE; 127 } else { 128 *ypos = (NCURSES_SIZE_T) (*ypos + 1); 129 } 130 return result; 131 } 132 133 /* 134 * The _WRAPPED flag is useful only for telling an application that we've just 135 * wrapped the cursor. We don't do anything with this flag except set it when 136 * wrapping, and clear it whenever we move the cursor. If we try to wrap at 137 * the lower-right corner of a window, we cannot move the cursor (since that 138 * wouldn't be legal). So we return an error (which is what SVr4 does). 139 * Unlike SVr4, we can successfully add a character to the lower-right corner 140 * (Solaris 2.6 does this also, however). 141 */ 142 static int 143 wrap_to_next_line(WINDOW *win) 144 { 145 win->_flags |= _WRAPPED; 146 if (newline_forces_scroll(win, &(win->_cury))) { 147 win->_curx = win->_maxx; 148 if (!win->_scroll) 149 return (ERR); 150 scroll(win); 151 } 152 win->_curx = 0; 153 return (OK); 154 } 155 156 static int wadd_wch_literal(WINDOW *, cchar_t); 157 /* 158 * Fill the given number of cells with blanks using the current background 159 * rendition. This saves/restores the current x-position. 160 */ 161 static void 162 fill_cells(WINDOW *win, int count) 163 { 164 cchar_t blank = blankchar; 165 int save_x = win->_curx; 166 int save_y = win->_cury; 167 168 while (count-- > 0) { 169 if (wadd_wch_literal(win, blank) == ERR) 170 break; 171 } 172 win->_curx = (NCURSES_SIZE_T) save_x; 173 win->_cury = (NCURSES_SIZE_T) save_y; 174 } 175 176 static int 177 wadd_wch_literal(WINDOW *win, cchar_t ch) 178 { 179 int x; 180 int y; 181 struct ldat *line; 182 183 x = win->_curx; 184 y = win->_cury; 185 186 CHECK_POSITION(win, x, y); 187 188 ch = render_char(win, ch); 189 190 line = win->_line + y; 191 192 CHANGED_CELL(line, x); 193 194 /* 195 * Non-spacing characters are added to the current cell. 196 * 197 * Spacing characters that are wider than one column require some display 198 * adjustments. 199 */ 200 { 201 int len = _nc_wacs_width(CharOf(ch)); 202 int i; 203 int j; 204 wchar_t *chars; 205 206 if (len == 0) { /* non-spacing */ 207 if ((x > 0 && y >= 0) 208 || (win->_maxx >= 0 && win->_cury >= 1)) { 209 if (x > 0 && y >= 0) { 210 for (j = x - 1; j > 0; --j) { 211 if (!isWidecExt(win->_line[y].text[j])) { 212 break; 213 } 214 } 215 chars = (win->_line[y].text[j].chars); 216 } else { 217 chars = (win->_line[y - 1].text[win->_maxx].chars); 218 } 219 for (i = 0; i < CCHARW_MAX; ++i) { 220 if (chars[i] == 0) { 221 TR(TRACE_VIRTPUT, 222 ("added non-spacing %d: %x", 223 x, (int) CharOf(ch))); 224 chars[i] = CharOf(ch); 225 break; 226 } 227 } 228 } 229 goto testwrapping; 230 } else if (len > 1) { /* multi-column characters */ 231 /* 232 * Check if the character will fit on the current line. If it does 233 * not fit, fill in the remainder of the line with blanks. and 234 * move to the next line. 235 */ 236 if (len > win->_maxx + 1) { 237 TR(TRACE_VIRTPUT, ("character will not fit")); 238 return ERR; 239 } else if (x + len > win->_maxx + 1) { 240 int count = win->_maxx + 1 - x; 241 TR(TRACE_VIRTPUT, ("fill %d remaining cells", count)); 242 fill_cells(win, count); 243 if (wrap_to_next_line(win) == ERR) 244 return ERR; 245 x = win->_curx; 246 y = win->_cury; 247 line = win->_line + y; 248 } 249 /* 250 * Check for cells which are orphaned by adding this character, set 251 * those to blanks. 252 * 253 * FIXME: this actually could fill j-i cells, more complicated to 254 * setup though. 255 */ 256 for (i = 0; i < len; ++i) { 257 if (isWidecBase(win->_line[y].text[x + i])) { 258 break; 259 } else if (isWidecExt(win->_line[y].text[x + i])) { 260 for (j = i; x + j <= win->_maxx; ++j) { 261 if (!isWidecExt(win->_line[y].text[x + j])) { 262 TR(TRACE_VIRTPUT, ("fill %d orphan cells", j)); 263 fill_cells(win, j); 264 break; 265 } 266 } 267 break; 268 } 269 } 270 /* 271 * Finally, add the cells for this character. 272 */ 273 for (i = 0; i < len; ++i) { 274 cchar_t value = ch; 275 SetWidecExt(value, i); 276 TR(TRACE_VIRTPUT, ("multicolumn %d:%d (%d,%d)", 277 i + 1, len, 278 win->_begy + y, win->_begx + x)); 279 line->text[x] = value; 280 CHANGED_CELL(line, x); 281 ++x; 282 } 283 goto testwrapping; 284 } 285 } 286 287 /* 288 * Single-column characters. 289 */ 290 line->text[x++] = ch; 291 /* 292 * This label is used only for wide-characters. 293 */ 294 testwrapping: 295 296 TR(TRACE_VIRTPUT, ("cell (%ld, %ld..%d) = %s", 297 (long) win->_cury, (long) win->_curx, x - 1, 298 _tracech_t(CHREF(ch)))); 299 300 if (x > win->_maxx) { 301 return wrap_to_next_line(win); 302 } 303 win->_curx = (NCURSES_SIZE_T) x; 304 return OK; 305 } 306 307 static NCURSES_INLINE int 308 wadd_wch_nosync(WINDOW *win, cchar_t ch) 309 /* the workhorse function -- add a character to the given window */ 310 { 311 NCURSES_SIZE_T x, y; 312 wchar_t *s; 313 int tabsize = 8; 314 #if USE_REENTRANT 315 SCREEN *sp = _nc_screen_of(win); 316 #endif 317 318 /* 319 * If we are using the alternate character set, forget about locale. 320 * Otherwise, if the locale claims the code is printable, treat it that 321 * way. 322 */ 323 if ((AttrOf(ch) & A_ALTCHARSET) 324 || iswprint((wint_t) CharOf(ch))) 325 return wadd_wch_literal(win, ch); 326 327 /* 328 * Handle carriage control and other codes that are not printable, or are 329 * known to expand to more than one character according to unctrl(). 330 */ 331 x = win->_curx; 332 y = win->_cury; 333 334 switch (CharOf(ch)) { 335 case '\t': 336 #if USE_REENTRANT 337 tabsize = *ptrTabsize(sp); 338 #else 339 tabsize = TABSIZE; 340 #endif 341 x = (NCURSES_SIZE_T) (x + (tabsize - (x % tabsize))); 342 /* 343 * Space-fill the tab on the bottom line so that we'll get the 344 * "correct" cursor position. 345 */ 346 if ((!win->_scroll && (y == win->_regbottom)) 347 || (x <= win->_maxx)) { 348 cchar_t blank = blankchar; 349 AddAttr(blank, AttrOf(ch)); 350 while (win->_curx < x) { 351 if (wadd_wch_literal(win, blank) == ERR) 352 return (ERR); 353 } 354 break; 355 } else { 356 wclrtoeol(win); 357 win->_flags |= _WRAPPED; 358 if (newline_forces_scroll(win, &y)) { 359 x = win->_maxx; 360 if (win->_scroll) { 361 scroll(win); 362 x = 0; 363 } 364 } else { 365 x = 0; 366 } 367 } 368 break; 369 case '\n': 370 wclrtoeol(win); 371 if (newline_forces_scroll(win, &y)) { 372 if (win->_scroll) 373 scroll(win); 374 else 375 return (ERR); 376 } 377 /* FALLTHRU */ 378 case '\r': 379 x = 0; 380 win->_flags &= ~_WRAPPED; 381 break; 382 case '\b': 383 if (x == 0) 384 return (OK); 385 x--; 386 win->_flags &= ~_WRAPPED; 387 break; 388 default: 389 if ((s = wunctrl(&ch)) != 0) { 390 while (*s) { 391 cchar_t sch; 392 SetChar(sch, *s++, AttrOf(ch)); 393 if_EXT_COLORS(SetPair(sch, GetPair(ch))); 394 if (wadd_wch_literal(win, sch) == ERR) 395 return ERR; 396 } 397 return OK; 398 } 399 return ERR; 400 } 401 402 win->_curx = x; 403 win->_cury = y; 404 405 return OK; 406 } 407 408 /* 409 * The versions below call _nc_synchook(). We wanted to avoid this in the 410 * version exported for string puts; they'll call _nc_synchook once at end 411 * of run. 412 */ 413 414 /* These are actual entry points */ 415 416 NCURSES_EXPORT(int) 417 wadd_wch(WINDOW *win, const cchar_t *wch) 418 { 419 int code = ERR; 420 421 TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_CALLED("wadd_wch(%p, %s)"), 422 (void *) win, 423 _tracecchar_t(wch))); 424 425 if (win && (wadd_wch_nosync(win, *wch) != ERR)) { 426 _nc_synchook(win); 427 code = OK; 428 } 429 430 TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_RETURN("%d"), code)); 431 return (code); 432 } 433 434 NCURSES_EXPORT(int) 435 wecho_wchar(WINDOW *win, const cchar_t *wch) 436 { 437 int code = ERR; 438 439 TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_CALLED("wechochar(%p, %s)"), 440 (void *) win, 441 _tracecchar_t(wch))); 442 443 if (win && (wadd_wch_nosync(win, *wch) != ERR)) { 444 bool save_immed = win->_immed; 445 win->_immed = TRUE; 446 _nc_synchook(win); 447 win->_immed = save_immed; 448 code = OK; 449 } 450 TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_RETURN("%d"), code)); 451 return (code); 452 } 453