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