1 /**************************************************************************** 2 * Copyright (c) 1998-2009,2010 Free Software Foundation, Inc. * 3 * * 4 * Permission is hereby granted, free of charge, to any person obtaining a * 5 * copy of this software and associated documentation files (the * 6 * "Software"), to deal in the Software without restriction, including * 7 * without limitation the rights to use, copy, modify, merge, publish, * 8 * distribute, distribute with modifications, sublicense, and/or sell * 9 * copies of the Software, and to permit persons to whom the Software is * 10 * furnished to do so, subject to the following conditions: * 11 * * 12 * The above copyright notice and this permission notice shall be included * 13 * in all copies or substantial portions of the Software. * 14 * * 15 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS * 16 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF * 17 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. * 18 * IN NO EVENT SHALL THE ABOVE COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, * 19 * DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR * 20 * OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR * 21 * THE USE OR OTHER DEALINGS IN THE SOFTWARE. * 22 * * 23 * Except as contained in this notice, the name(s) of the above copyright * 24 * holders shall not be used in advertising or otherwise to promote the * 25 * sale, use or other dealings in this Software without prior written * 26 * authorization. * 27 ****************************************************************************/ 28 29 /* 30 ** lib_addch.c 31 ** 32 ** The routine waddch(). 33 ** 34 */ 35 36 #include <curses.priv.h> 37 #include <ctype.h> 38 39 MODULE_ID("$Id: lib_addch.c,v 1.124 2010/04/24 22:41:05 tom Exp $") 40 41 static const NCURSES_CH_T blankchar = NewChar(BLANK_TEXT); 42 43 /* 44 * Ugly microtweaking alert. Everything from here to end of module is 45 * likely to be speed-critical -- profiling data sure says it is! 46 * Most of the important screen-painting functions are shells around 47 * waddch(). So we make every effort to reduce function-call overhead 48 * by inlining stuff, even at the cost of making wrapped copies for 49 * export. Also we supply some internal versions that don't call the 50 * window sync hook, for use by string-put functions. 51 */ 52 53 /* Return bit mask for clearing color pair number if given ch has color */ 54 #define COLOR_MASK(ch) (~(attr_t)((ch) & A_COLOR ? A_COLOR : 0)) 55 56 static NCURSES_INLINE NCURSES_CH_T 57 render_char(WINDOW *win, NCURSES_CH_T ch) 58 /* compute a rendition of the given char correct for the current context */ 59 { 60 attr_t a = WINDOW_ATTRS(win); 61 int pair = GetPair(ch); 62 63 if (ISBLANK(ch) 64 && AttrOf(ch) == A_NORMAL 65 && pair == 0) { 66 /* color/pair in attrs has precedence over bkgrnd */ 67 ch = win->_nc_bkgd; 68 SetAttr(ch, a | AttrOf(win->_nc_bkgd)); 69 if ((pair = GET_WINDOW_PAIR(win)) == 0) 70 pair = GetPair(win->_nc_bkgd); 71 SetPair(ch, pair); 72 } else { 73 /* color in attrs has precedence over bkgrnd */ 74 a |= AttrOf(win->_nc_bkgd) & COLOR_MASK(a); 75 /* color in ch has precedence */ 76 if (pair == 0) { 77 if ((pair = GET_WINDOW_PAIR(win)) == 0) 78 pair = GetPair(win->_nc_bkgd); 79 } 80 AddAttr(ch, (a & COLOR_MASK(AttrOf(ch)))); 81 SetPair(ch, pair); 82 } 83 84 TR(TRACE_VIRTPUT, 85 ("render_char bkg %s (%d), attrs %s (%d) -> ch %s (%d)", 86 _tracech_t2(1, CHREF(win->_nc_bkgd)), 87 GetPair(win->_nc_bkgd), 88 _traceattr(WINDOW_ATTRS(win)), 89 GET_WINDOW_PAIR(win), 90 _tracech_t2(3, CHREF(ch)), 91 GetPair(ch))); 92 93 return (ch); 94 } 95 96 NCURSES_EXPORT(NCURSES_CH_T) 97 _nc_render(WINDOW *win, NCURSES_CH_T ch) 98 /* make render_char() visible while still allowing us to inline it below */ 99 { 100 return render_char(win, 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 #if USE_WIDEC_SUPPORT 157 static int waddch_literal(WINDOW *, NCURSES_CH_T); 158 /* 159 * Fill the given number of cells with blanks using the current background 160 * rendition. This saves/restores the current x-position. 161 */ 162 static void 163 fill_cells(WINDOW *win, int count) 164 { 165 NCURSES_CH_T blank = blankchar; 166 int save_x = win->_curx; 167 int save_y = win->_cury; 168 169 while (count-- > 0) { 170 if (waddch_literal(win, blank) == ERR) 171 break; 172 } 173 win->_curx = (NCURSES_SIZE_T) save_x; 174 win->_cury = (NCURSES_SIZE_T) save_y; 175 } 176 #endif 177 178 /* 179 * Build up the bytes for a multibyte character, returning the length when 180 * complete (a positive number), -1 for error and -2 for incomplete. 181 */ 182 #if USE_WIDEC_SUPPORT 183 NCURSES_EXPORT(int) 184 _nc_build_wch(WINDOW *win, ARG_CH_T ch) 185 { 186 char *buffer = WINDOW_EXT(win, addch_work); 187 int len; 188 int x = win->_curx; 189 int y = win->_cury; 190 mbstate_t state; 191 wchar_t result; 192 193 if ((WINDOW_EXT(win, addch_used) != 0) && 194 (WINDOW_EXT(win, addch_x) != x || 195 WINDOW_EXT(win, addch_y) != y)) { 196 /* discard the incomplete multibyte character */ 197 WINDOW_EXT(win, addch_used) = 0; 198 TR(TRACE_VIRTPUT, 199 ("Alert discarded multibyte on move (%d,%d) -> (%d,%d)", 200 WINDOW_EXT(win, addch_y), WINDOW_EXT(win, addch_x), 201 y, x)); 202 } 203 WINDOW_EXT(win, addch_x) = x; 204 WINDOW_EXT(win, addch_y) = y; 205 206 init_mb(state); 207 buffer[WINDOW_EXT(win, addch_used)] = (char) CharOf(CHDEREF(ch)); 208 WINDOW_EXT(win, addch_used) += 1; 209 buffer[WINDOW_EXT(win, addch_used)] = '\0'; 210 if ((len = (int) mbrtowc(&result, 211 buffer, 212 WINDOW_EXT(win, addch_used), &state)) > 0) { 213 attr_t attrs = AttrOf(CHDEREF(ch)); 214 if_EXT_COLORS(int pair = GetPair(CHDEREF(ch))); 215 SetChar(CHDEREF(ch), result, attrs); 216 if_EXT_COLORS(SetPair(CHDEREF(ch), pair)); 217 WINDOW_EXT(win, addch_used) = 0; 218 } else if (len == -1) { 219 /* 220 * An error occurred. We could either discard everything, 221 * or assume that the error was in the previous input. 222 * Try the latter. 223 */ 224 TR(TRACE_VIRTPUT, ("Alert! mbrtowc returns error")); 225 /* handle this with unctrl() */ 226 WINDOW_EXT(win, addch_used) = 0; 227 } 228 return len; 229 } 230 #endif /* USE_WIDEC_SUPPORT */ 231 232 static 233 #if !USE_WIDEC_SUPPORT /* cannot be inline if it is recursive */ 234 NCURSES_INLINE 235 #endif 236 int 237 waddch_literal(WINDOW *win, NCURSES_CH_T ch) 238 { 239 int x; 240 int y; 241 struct ldat *line; 242 243 x = win->_curx; 244 y = win->_cury; 245 246 CHECK_POSITION(win, x, y); 247 248 ch = render_char(win, ch); 249 250 line = win->_line + y; 251 252 CHANGED_CELL(line, x); 253 254 /* 255 * Build up multibyte characters until we have a wide-character. 256 */ 257 #if NCURSES_SP_FUNCS 258 #define DeriveSP() SCREEN *sp = _nc_screen_of(win); 259 #else 260 #define DeriveSP() /*nothing */ 261 #endif 262 if_WIDEC({ 263 DeriveSP(); 264 if (WINDOW_EXT(win, addch_used) != 0 || !Charable(ch)) { 265 int len = _nc_build_wch(win, CHREF(ch)); 266 267 if (len >= -1) { 268 attr_t attr = AttrOf(ch); 269 270 /* handle EILSEQ (i.e., when len >= -1) */ 271 if (len == -1 && is8bits(CharOf(ch))) { 272 int rc = OK; 273 const char *s = NCURSES_SP_NAME(unctrl) 274 (NCURSES_SP_ARGx (chtype) CharOf(ch)); 275 276 if (s[1] != '\0') { 277 while (*s != '\0') { 278 rc = waddch(win, UChar(*s) | attr); 279 if (rc != OK) 280 break; 281 ++s; 282 } 283 return rc; 284 } 285 } 286 if (len == -1) 287 return waddch(win, ' ' | attr); 288 } else { 289 return OK; 290 } 291 } 292 }); 293 294 /* 295 * Non-spacing characters are added to the current cell. 296 * 297 * Spacing characters that are wider than one column require some display 298 * adjustments. 299 */ 300 if_WIDEC({ 301 int len = wcwidth(CharOf(ch)); 302 int i; 303 int j; 304 wchar_t *chars; 305 306 if (len == 0) { /* non-spacing */ 307 if ((x > 0 && y >= 0) 308 || (win->_maxx >= 0 && win->_cury >= 1)) { 309 if (x > 0 && y >= 0) 310 chars = (win->_line[y].text[x - 1].chars); 311 else 312 chars = (win->_line[y - 1].text[win->_maxx].chars); 313 for (i = 0; i < CCHARW_MAX; ++i) { 314 if (chars[i] == 0) { 315 TR(TRACE_VIRTPUT, 316 ("added non-spacing %d: %x", 317 x, (int) CharOf(ch))); 318 chars[i] = CharOf(ch); 319 break; 320 } 321 } 322 } 323 goto testwrapping; 324 } else if (len > 1) { /* multi-column characters */ 325 /* 326 * Check if the character will fit on the current line. If it does 327 * not fit, fill in the remainder of the line with blanks. and 328 * move to the next line. 329 */ 330 if (len > win->_maxx + 1) { 331 TR(TRACE_VIRTPUT, ("character will not fit")); 332 return ERR; 333 } else if (x + len > win->_maxx + 1) { 334 int count = win->_maxx + 1 - x; 335 TR(TRACE_VIRTPUT, ("fill %d remaining cells", count)); 336 fill_cells(win, count); 337 if (wrap_to_next_line(win) == ERR) 338 return ERR; 339 x = win->_curx; 340 y = win->_cury; 341 line = win->_line + y; 342 } 343 /* 344 * Check for cells which are orphaned by adding this character, set 345 * those to blanks. 346 * 347 * FIXME: this actually could fill j-i cells, more complicated to 348 * setup though. 349 */ 350 for (i = 0; i < len; ++i) { 351 if (isWidecBase(win->_line[y].text[x + i])) { 352 break; 353 } else if (isWidecExt(win->_line[y].text[x + i])) { 354 for (j = i; x + j <= win->_maxx; ++j) { 355 if (!isWidecExt(win->_line[y].text[x + j])) { 356 TR(TRACE_VIRTPUT, ("fill %d orphan cells", j)); 357 fill_cells(win, j); 358 break; 359 } 360 } 361 break; 362 } 363 } 364 /* 365 * Finally, add the cells for this character. 366 */ 367 for (i = 0; i < len; ++i) { 368 NCURSES_CH_T value = ch; 369 SetWidecExt(value, i); 370 TR(TRACE_VIRTPUT, ("multicolumn %d:%d (%d,%d)", 371 i + 1, len, 372 win->_begy + y, win->_begx + x)); 373 line->text[x] = value; 374 CHANGED_CELL(line, x); 375 ++x; 376 } 377 goto testwrapping; 378 } 379 }); 380 381 /* 382 * Single-column characters. 383 */ 384 line->text[x++] = ch; 385 /* 386 * This label is used only for wide-characters. 387 */ 388 if_WIDEC( 389 testwrapping: 390 ); 391 392 TR(TRACE_VIRTPUT, ("cell (%ld, %ld..%d) = %s", 393 (long) win->_cury, (long) win->_curx, x - 1, 394 _tracech_t(CHREF(ch)))); 395 396 if (x > win->_maxx) { 397 return wrap_to_next_line(win); 398 } 399 win->_curx = (NCURSES_SIZE_T) x; 400 return OK; 401 } 402 403 static NCURSES_INLINE int 404 waddch_nosync(WINDOW *win, const NCURSES_CH_T ch) 405 /* the workhorse function -- add a character to the given window */ 406 { 407 NCURSES_SIZE_T x, y; 408 chtype t = (chtype) CharOf(ch); 409 #if USE_WIDEC_SUPPORT || NCURSES_SP_FUNCS || USE_REENTRANT 410 SCREEN *sp = _nc_screen_of(win); 411 #endif 412 const char *s = NCURSES_SP_NAME(unctrl) (NCURSES_SP_ARGx t); 413 int tabsize = 8; 414 /* 415 * If we are using the alternate character set, forget about locale. 416 * Otherwise, if unctrl() returns a single-character or the locale 417 * claims the code is printable, treat it that way. 418 */ 419 if ((AttrOf(ch) & A_ALTCHARSET) 420 || ( 421 #if USE_WIDEC_SUPPORT 422 (sp != 0 && sp->_legacy_coding) && 423 #endif 424 s[1] == 0 425 ) 426 || ( 427 isprint(t) 428 #if USE_WIDEC_SUPPORT 429 || ((sp == 0 || !sp->_legacy_coding) && 430 (WINDOW_EXT(win, addch_used) 431 || !_nc_is_charable(CharOf(ch)))) 432 #endif 433 )) 434 return waddch_literal(win, ch); 435 436 /* 437 * Handle carriage control and other codes that are not printable, or are 438 * known to expand to more than one character according to unctrl(). 439 */ 440 x = win->_curx; 441 y = win->_cury; 442 443 switch (t) { 444 case '\t': 445 #if USE_REENTRANT 446 tabsize = *ptrTabsize(sp); 447 #else 448 tabsize = TABSIZE; 449 #endif 450 x = (NCURSES_SIZE_T) (x + (tabsize - (x % tabsize))); 451 /* 452 * Space-fill the tab on the bottom line so that we'll get the 453 * "correct" cursor position. 454 */ 455 if ((!win->_scroll && (y == win->_regbottom)) 456 || (x <= win->_maxx)) { 457 NCURSES_CH_T blank = blankchar; 458 AddAttr(blank, AttrOf(ch)); 459 while (win->_curx < x) { 460 if (waddch_literal(win, blank) == ERR) 461 return (ERR); 462 } 463 break; 464 } else { 465 wclrtoeol(win); 466 win->_flags |= _WRAPPED; 467 if (newline_forces_scroll(win, &y)) { 468 x = win->_maxx; 469 if (win->_scroll) { 470 scroll(win); 471 x = 0; 472 } 473 } else { 474 x = 0; 475 } 476 } 477 break; 478 case '\n': 479 wclrtoeol(win); 480 if (newline_forces_scroll(win, &y)) { 481 if (win->_scroll) 482 scroll(win); 483 else 484 return (ERR); 485 } 486 /* FALLTHRU */ 487 case '\r': 488 x = 0; 489 win->_flags &= ~_WRAPPED; 490 break; 491 case '\b': 492 if (x == 0) 493 return (OK); 494 x--; 495 win->_flags &= ~_WRAPPED; 496 break; 497 default: 498 while (*s) { 499 NCURSES_CH_T sch; 500 SetChar(sch, *s++, AttrOf(ch)); 501 if_EXT_COLORS(SetPair(sch, GetPair(ch))); 502 if (waddch_literal(win, sch) == ERR) 503 return ERR; 504 } 505 return (OK); 506 } 507 508 win->_curx = x; 509 win->_cury = y; 510 511 return (OK); 512 } 513 514 NCURSES_EXPORT(int) 515 _nc_waddch_nosync(WINDOW *win, const NCURSES_CH_T c) 516 /* export copy of waddch_nosync() so the string-put functions can use it */ 517 { 518 return (waddch_nosync(win, c)); 519 } 520 521 /* 522 * The versions below call _nc_synchook(). We wanted to avoid this in the 523 * version exported for string puts; they'll call _nc_synchook once at end 524 * of run. 525 */ 526 527 /* These are actual entry points */ 528 529 NCURSES_EXPORT(int) 530 waddch(WINDOW *win, const chtype ch) 531 { 532 int code = ERR; 533 NCURSES_CH_T wch; 534 SetChar2(wch, ch); 535 536 TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_CALLED("waddch(%p, %s)"), (void *) win, 537 _tracechtype(ch))); 538 539 if (win && (waddch_nosync(win, wch) != ERR)) { 540 _nc_synchook(win); 541 code = OK; 542 } 543 544 TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_RETURN("%d"), code)); 545 return (code); 546 } 547 548 NCURSES_EXPORT(int) 549 wechochar(WINDOW *win, const chtype ch) 550 { 551 int code = ERR; 552 NCURSES_CH_T wch; 553 SetChar2(wch, ch); 554 555 TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_CALLED("wechochar(%p, %s)"), 556 (void *) win, 557 _tracechtype(ch))); 558 559 if (win && (waddch_nosync(win, wch) != ERR)) { 560 bool save_immed = win->_immed; 561 win->_immed = TRUE; 562 _nc_synchook(win); 563 win->_immed = save_immed; 564 code = OK; 565 } 566 TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_RETURN("%d"), code)); 567 return (code); 568 } 569