1 /****************************************************************************
2 * Copyright 2019,2020 Thomas E. Dickey *
3 * Copyright 1998-2016,2017 Free Software Foundation, Inc. *
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: *
13 * The above copyright notice and this permission notice shall be included *
14 * in all copies or substantial portions of the Software. *
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. *
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 *
28 ****************************************************************************/
33 ** The routine waddch().
37 #include <curses.priv.h>
40 MODULE_ID("$Id: lib_addch.c,v 1.134 2020/02/02 23:34:34 tom Exp $")
42 static const NCURSES_CH_T blankchar = NewChar(BLANK_TEXT);
45 * Ugly microtweaking alert. Everything from here to end of module is
46 * likely to be speed-critical -- profiling data sure says it is!
47 * Most of the important screen-painting functions are shells around
48 * waddch(). So we make every effort to reduce function-call overhead
49 * by inlining stuff, even at the cost of making wrapped copies for
50 * export. Also we supply some internal versions that don't call the
51 * window sync hook, for use by string-put functions.
54 /* Return bit mask for clearing color pair number if given ch has color */
55 #define COLOR_MASK(ch) (~(attr_t)(((ch) & A_COLOR) ? A_COLOR : 0))
57 static NCURSES_INLINE NCURSES_CH_T
58 render_char(WINDOW *win, NCURSES_CH_T ch)
59 /* compute a rendition of the given char correct for the current context */
61 attr_t a = WINDOW_ATTRS(win);
62 int pair = GetPair(ch);
65 && AttrOf(ch) == A_NORMAL
67 /* color/pair in attrs has precedence over bkgrnd */
69 SetAttr(ch, a | AttrOf(win->_nc_bkgd));
70 if ((pair = GET_WINDOW_PAIR(win)) == 0)
71 pair = GetPair(win->_nc_bkgd);
74 /* color in attrs has precedence over bkgrnd */
75 a |= AttrOf(win->_nc_bkgd) & COLOR_MASK(a);
76 /* color in ch has precedence */
78 if ((pair = GET_WINDOW_PAIR(win)) == 0)
79 pair = GetPair(win->_nc_bkgd);
81 AddAttr(ch, (a & COLOR_MASK(AttrOf(ch))));
86 ("render_char bkg %s (%d), attrs %s (%d) -> ch %s (%d)",
87 _tracech_t2(1, CHREF(win->_nc_bkgd)),
88 GetPair(win->_nc_bkgd),
89 _traceattr(WINDOW_ATTRS(win)),
91 _tracech_t2(3, CHREF(ch)),
97 NCURSES_EXPORT(NCURSES_CH_T)
98 _nc_render(WINDOW *win, NCURSES_CH_T ch)
99 /* make render_char() visible while still allowing us to inline it below */
101 return render_char(win, ch);
104 /* check if position is legal; if not, return error */
105 #ifndef NDEBUG /* treat this like an assertion */
106 #define CHECK_POSITION(win, x, y) \
111 TR(TRACE_VIRTPUT, ("Alert! Win=%p _curx = %d, _cury = %d " \
112 "(_maxx = %d, _maxy = %d)", win, x, y, \
113 win->_maxx, win->_maxy)); \
117 #define CHECK_POSITION(win, x, y) /* nothing */
121 newline_forces_scroll(WINDOW *win, NCURSES_SIZE_T *ypos)
125 if (*ypos >= win->_regtop && *ypos <= win->_regbottom) {
126 if (*ypos == win->_regbottom) {
127 *ypos = win->_regbottom;
129 } else if (*ypos < win->_maxy) {
130 *ypos = (NCURSES_SIZE_T) (*ypos + 1);
132 } else if (*ypos < win->_maxy) {
133 *ypos = (NCURSES_SIZE_T) (*ypos + 1);
139 * The _WRAPPED flag is useful only for telling an application that we've just
140 * wrapped the cursor. We don't do anything with this flag except set it when
141 * wrapping, and clear it whenever we move the cursor. If we try to wrap at
142 * the lower-right corner of a window, we cannot move the cursor (since that
143 * wouldn't be legal). So we return an error (which is what SVr4 does).
144 * Unlike SVr4, we can successfully add a character to the lower-right corner
145 * (Solaris 2.6 does this also, however).
148 wrap_to_next_line(WINDOW *win)
150 win->_flags |= _WRAPPED;
151 if (newline_forces_scroll(win, &(win->_cury))) {
152 win->_curx = win->_maxx;
161 #if USE_WIDEC_SUPPORT
162 static int waddch_literal(WINDOW *, NCURSES_CH_T);
164 * Fill the given number of cells with blanks using the current background
165 * rendition. This saves/restores the current x-position.
168 fill_cells(WINDOW *win, int count)
170 NCURSES_CH_T blank = blankchar;
171 int save_x = win->_curx;
172 int save_y = win->_cury;
174 while (count-- > 0) {
175 if (waddch_literal(win, blank) == ERR)
178 win->_curx = (NCURSES_SIZE_T) save_x;
179 win->_cury = (NCURSES_SIZE_T) save_y;
184 * Build up the bytes for a multibyte character, returning the length when
185 * complete (a positive number), -1 for error and -2 for incomplete.
187 #if USE_WIDEC_SUPPORT
189 _nc_build_wch(WINDOW *win, ARG_CH_T ch)
191 char *buffer = WINDOW_EXT(win, addch_work);
198 if ((WINDOW_EXT(win, addch_used) != 0) &&
199 (WINDOW_EXT(win, addch_x) != x ||
200 WINDOW_EXT(win, addch_y) != y)) {
201 /* discard the incomplete multibyte character */
202 WINDOW_EXT(win, addch_used) = 0;
204 ("Alert discarded multibyte on move (%d,%d) -> (%d,%d)",
205 WINDOW_EXT(win, addch_y), WINDOW_EXT(win, addch_x),
208 WINDOW_EXT(win, addch_x) = x;
209 WINDOW_EXT(win, addch_y) = y;
212 buffer[WINDOW_EXT(win, addch_used)] = (char) CharOf(CHDEREF(ch));
213 WINDOW_EXT(win, addch_used) += 1;
214 buffer[WINDOW_EXT(win, addch_used)] = '\0';
215 if ((len = (int) mbrtowc(&result,
217 (size_t) WINDOW_EXT(win, addch_used),
219 attr_t attrs = AttrOf(CHDEREF(ch));
220 if_EXT_COLORS(int pair = GetPair(CHDEREF(ch)));
221 SetChar(CHDEREF(ch), result, attrs);
222 if_EXT_COLORS(SetPair(CHDEREF(ch), pair));
223 WINDOW_EXT(win, addch_used) = 0;
224 } else if (len == -1) {
226 * An error occurred. We could either discard everything,
227 * or assume that the error was in the previous input.
230 TR(TRACE_VIRTPUT, ("Alert! mbrtowc returns error"));
231 /* handle this with unctrl() */
232 WINDOW_EXT(win, addch_used) = 0;
236 #endif /* USE_WIDEC_SUPPORT */
239 #if !USE_WIDEC_SUPPORT /* cannot be inline if it is recursive */
243 waddch_literal(WINDOW *win, NCURSES_CH_T ch)
252 CHECK_POSITION(win, x, y);
254 ch = render_char(win, ch);
256 line = win->_line + y;
258 CHANGED_CELL(line, x);
261 * Build up multibyte characters until we have a wide-character.
264 #define DeriveSP() SCREEN *sp = _nc_screen_of(win);
266 #define DeriveSP() /*nothing */
270 if (WINDOW_EXT(win, addch_used) != 0 || !Charable(ch)) {
271 int len = _nc_build_wch(win, CHREF(ch));
274 attr_t attr = AttrOf(ch);
276 /* handle EILSEQ (i.e., when len >= -1) */
277 if (len == -1 && is8bits(CharOf(ch))) {
278 const char *s = NCURSES_SP_NAME(unctrl)
279 (NCURSES_SP_ARGx (chtype) CharOf(ch));
284 rc = waddch(win, UChar(*s) | attr);
293 return waddch(win, ' ' | attr);
301 * Non-spacing characters are added to the current cell.
303 * Spacing characters that are wider than one column require some display
307 int len = _nc_wacs_width(CharOf(ch));
312 if (len == 0) { /* non-spacing */
313 if ((x > 0 && y >= 0)
314 || (win->_maxx >= 0 && win->_cury >= 1)) {
316 chars = (win->_line[y].text[x - 1].chars);
318 chars = (win->_line[y - 1].text[win->_maxx].chars);
319 for (i = 0; i < CCHARW_MAX; ++i) {
322 ("added non-spacing %d: %x",
323 x, (int) CharOf(ch)));
324 chars[i] = CharOf(ch);
330 } else if (len > 1) { /* multi-column characters */
332 * Check if the character will fit on the current line. If it does
333 * not fit, fill in the remainder of the line with blanks. and
334 * move to the next line.
336 if (len > win->_maxx + 1) {
337 TR(TRACE_VIRTPUT, ("character will not fit"));
339 } else if (x + len > win->_maxx + 1) {
340 int count = win->_maxx + 1 - x;
341 TR(TRACE_VIRTPUT, ("fill %d remaining cells", count));
342 fill_cells(win, count);
343 if (wrap_to_next_line(win) == ERR)
347 CHECK_POSITION(win, x, y);
348 line = win->_line + y;
351 * Check for cells which are orphaned by adding this character, set
354 * FIXME: this actually could fill j-i cells, more complicated to
357 for (i = 0; i < len; ++i) {
358 if (isWidecBase(win->_line[y].text[x + i])) {
360 } else if (isWidecExt(win->_line[y].text[x + i])) {
361 for (j = i; x + j <= win->_maxx; ++j) {
362 if (!isWidecExt(win->_line[y].text[x + j])) {
363 TR(TRACE_VIRTPUT, ("fill %d orphan cells", j));
372 * Finally, add the cells for this character.
374 for (i = 0; i < len; ++i) {
375 NCURSES_CH_T value = ch;
376 SetWidecExt(value, i);
377 TR(TRACE_VIRTPUT, ("multicolumn %d:%d (%d,%d)",
379 win->_begy + y, win->_begx + x));
380 line->text[x] = value;
381 CHANGED_CELL(line, x);
389 * Single-column characters.
391 line->text[x++] = ch;
393 * This label is used only for wide-characters.
399 TR(TRACE_VIRTPUT, ("cell (%ld, %ld..%d) = %s",
400 (long) win->_cury, (long) win->_curx, x - 1,
401 _tracech_t(CHREF(ch))));
403 if (x > win->_maxx) {
404 return wrap_to_next_line(win);
406 win->_curx = (NCURSES_SIZE_T) x;
410 static NCURSES_INLINE int
411 waddch_nosync(WINDOW *win, const NCURSES_CH_T ch)
412 /* the workhorse function -- add a character to the given window */
415 chtype t = (chtype) CharOf(ch);
416 #if USE_WIDEC_SUPPORT || NCURSES_SP_FUNCS || USE_REENTRANT
417 SCREEN *sp = _nc_screen_of(win);
419 const char *s = NCURSES_SP_NAME(unctrl) (NCURSES_SP_ARGx t);
423 * If we are using the alternate character set, forget about locale.
424 * Otherwise, if unctrl() returns a single-character or the locale
425 * claims the code is printable (and not also a control character),
428 if ((AttrOf(ch) & A_ALTCHARSET)
430 #if USE_WIDEC_SUPPORT
431 (sp != 0 && sp->_legacy_coding) &&
436 (isprint((int) t) && !iscntrl((int) t))
437 #if USE_WIDEC_SUPPORT
438 || ((sp == 0 || !sp->_legacy_coding) &&
439 (WINDOW_EXT(win, addch_used)
440 || !_nc_is_charable(CharOf(ch))))
443 return waddch_literal(win, ch);
447 * Handle carriage control and other codes that are not printable, or are
448 * known to expand to more than one character according to unctrl().
452 CHECK_POSITION(win, x, y);
457 tabsize = *ptrTabsize(sp);
461 x = (NCURSES_SIZE_T) (x + (tabsize - (x % tabsize)));
463 * Space-fill the tab on the bottom line so that we'll get the
464 * "correct" cursor position.
466 if ((!win->_scroll && (y == win->_regbottom))
467 || (x <= win->_maxx)) {
468 NCURSES_CH_T blank = blankchar;
469 AddAttr(blank, AttrOf(ch));
470 while (win->_curx < x) {
471 if (waddch_literal(win, blank) == ERR)
477 win->_flags |= _WRAPPED;
478 if (newline_forces_scroll(win, &y)) {
491 if (newline_forces_scroll(win, &y)) {
500 win->_flags &= ~_WRAPPED;
506 win->_flags &= ~_WRAPPED;
511 SetChar(sch, UChar(*s++), AttrOf(ch));
512 if_EXT_COLORS(SetPair(sch, GetPair(ch)));
513 if (waddch_literal(win, sch) == ERR)
526 _nc_waddch_nosync(WINDOW *win, const NCURSES_CH_T c)
527 /* export copy of waddch_nosync() so the string-put functions can use it */
529 return (waddch_nosync(win, c));
533 * The versions below call _nc_synchook(). We wanted to avoid this in the
534 * version exported for string puts; they'll call _nc_synchook once at end
538 /* These are actual entry points */
541 waddch(WINDOW *win, const chtype ch)
547 TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_CALLED("waddch(%p, %s)"), (void *) win,
550 if (win && (waddch_nosync(win, wch) != ERR)) {
555 TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_RETURN("%d"), code));
560 wechochar(WINDOW *win, const chtype ch)
566 TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_CALLED("wechochar(%p, %s)"),
570 if (win && (waddch_nosync(win, wch) != ERR)) {
571 bool save_immed = win->_immed;
574 win->_immed = save_immed;
577 TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_RETURN("%d"), code));