lib_addch.c revision b7ada7f2444f41b672faef4f93e446bdf8584cf9
1/****************************************************************************
2 * Copyright (c) 1998,1999,2000 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 *  Author: Zeyd M. Ben-Halim <zmbenhal@netcom.com> 1992,1995               *
31 *     and: Eric S. Raymond <esr@snark.thyrsus.com>                         *
32 ****************************************************************************/
33
34/*
35**	lib_addch.c
36**
37**	The routine waddch().
38**
39*/
40
41#include <curses.priv.h>
42#include <ctype.h>
43
44MODULE_ID("$Id: lib_addch.c,v 1.47 2000/12/10 02:43:26 tom Exp $")
45
46/*
47 * Ugly microtweaking alert.  Everything from here to end of module is
48 * likely to be speed-critical -- profiling data sure says it is!
49 * Most of the important screen-painting functions are shells around
50 * waddch().  So we make every effort to reduce function-call overhead
51 * by inlining stuff, even at the cost of making wrapped copies for
52 * export.  Also we supply some internal versions that don't call the
53 * window sync hook, for use by string-put functions.
54 */
55
56/* Return bit mask for clearing color pair number if given ch has color */
57#define COLOR_MASK(ch) (~(chtype)((ch)&A_COLOR?A_COLOR:0))
58
59static inline chtype
60render_char(WINDOW *win, chtype ch)
61/* compute a rendition of the given char correct for the current context */
62{
63    chtype a = win->_attrs;
64
65    if (ch == ' ') {
66	/* color in attrs has precedence over bkgd */
67	ch = a | (win->_bkgd & COLOR_MASK(a));
68    } else {
69	/* color in attrs has precedence over bkgd */
70	a |= (win->_bkgd & A_ATTRIBUTES) & COLOR_MASK(a);
71	/* color in ch has precedence */
72	ch |= (a & COLOR_MASK(ch));
73    }
74
75    TR(TRACE_VIRTPUT, ("bkg = %lx, attrs = %lx -> ch = %lx", win->_bkgd,
76		       win->_attrs, ch));
77
78    return (ch);
79}
80
81NCURSES_EXPORT(chtype)
82_nc_background
83(WINDOW *win)
84/* make render_char() visible while still allowing us to inline it below */
85{
86    return (win->_bkgd);
87}
88
89NCURSES_EXPORT(chtype)
90_nc_render
91(WINDOW *win, chtype ch)
92/* make render_char() visible while still allowing us to inline it below */
93{
94    return render_char(win, ch);
95}
96
97/* check if position is legal; if not, return error */
98#ifndef NDEBUG			/* treat this like an assertion */
99#define CHECK_POSITION(win, x, y) \
100	if (y > win->_maxy \
101	 || x > win->_maxx \
102	 || y < 0 \
103	 || x < 0) { \
104		TR(TRACE_VIRTPUT, ("Alert! Win=%p _curx = %d, _cury = %d " \
105				   "(_maxx = %d, _maxy = %d)", win, x, y, \
106				   win->_maxx, win->_maxy)); \
107		return(ERR); \
108	}
109#else
110#define CHECK_POSITION(win, x, y)	/* nothing */
111#endif
112
113static inline int
114waddch_literal(WINDOW *win, chtype ch)
115{
116    int x;
117    struct ldat *line;
118
119    x = win->_curx;
120
121    CHECK_POSITION(win, x, win->_cury);
122
123    /*
124     * If we're trying to add a character at the lower-right corner more
125     * than once, fail.  (Moving the cursor will clear the flag).
126     */
127#if 0				/* Solaris 2.6 allows updating the corner more than once */
128    if (win->_flags & _WRAPPED) {
129	if (x >= win->_maxx)
130	    return (ERR);
131	win->_flags &= ~_WRAPPED;
132    }
133#endif
134
135    ch = render_char(win, ch);
136    TR(TRACE_VIRTPUT, ("win attr = %s", _traceattr(win->_attrs)));
137
138    line = win->_line + win->_cury;
139
140    CHANGED_CELL(line, x);
141
142    line->text[x++] = ch;
143
144    TR(TRACE_VIRTPUT, ("(%d, %d) = %s", win->_cury, x, _tracechtype(ch)));
145    if (x > win->_maxx) {
146	/*
147	 * The _WRAPPED flag is useful only for telling an application that
148	 * we've just wrapped the cursor.  We don't do anything with this flag
149	 * except set it when wrapping, and clear it whenever we move the
150	 * cursor.  If we try to wrap at the lower-right corner of a window, we
151	 * cannot move the cursor (since that wouldn't be legal).  So we return
152	 * an error (which is what SVr4 does).  Unlike SVr4, we can
153	 * successfully add a character to the lower-right corner (Solaris 2.6
154	 * does this also, however).
155	 */
156	win->_flags |= _WRAPPED;
157	if (++win->_cury > win->_regbottom) {
158	    win->_cury = win->_regbottom;
159	    win->_curx = win->_maxx;
160	    if (!win->_scroll)
161		return (ERR);
162	    scroll(win);
163	}
164	win->_curx = 0;
165	return (OK);
166    }
167    win->_curx = x;
168    return OK;
169}
170
171static inline int
172waddch_nosync(WINDOW *win, const chtype ch)
173/* the workhorse function -- add a character to the given window */
174{
175    int x, y;
176    chtype t = 0;
177    const char *s = 0;
178
179    if ((ch & A_ALTCHARSET)
180	|| ((t = TextOf(ch)) > 127)
181	|| ((s = unctrl(t))[1] == 0))
182	return waddch_literal(win, ch);
183
184    x = win->_curx;
185    y = win->_cury;
186
187    switch (t) {
188    case '\t':
189	x += (TABSIZE - (x % TABSIZE));
190
191	/*
192	 * Space-fill the tab on the bottom line so that we'll get the
193	 * "correct" cursor position.
194	 */
195	if ((!win->_scroll && (y == win->_regbottom))
196	    || (x <= win->_maxx)) {
197	    chtype blank = (' ' | AttrOf(ch));
198	    while (win->_curx < x) {
199		if (waddch_literal(win, blank) == ERR)
200		    return (ERR);
201	    }
202	    break;
203	} else {
204	    wclrtoeol(win);
205	    win->_flags |= _WRAPPED;
206	    if (++y > win->_regbottom) {
207		x = win->_maxx;
208		y--;
209		if (win->_scroll) {
210		    scroll(win);
211		    x = 0;
212		}
213	    } else {
214		x = 0;
215	    }
216	}
217	break;
218    case '\n':
219	wclrtoeol(win);
220	if (++y > win->_regbottom) {
221	    y--;
222	    if (win->_scroll)
223		scroll(win);
224	    else
225		return (ERR);
226	}
227	/* FALLTHRU */
228    case '\r':
229	x = 0;
230	win->_flags &= ~_WRAPPED;
231	break;
232    case '\b':
233	if (x == 0)
234	    return (OK);
235	x--;
236	win->_flags &= ~_WRAPPED;
237	break;
238    default:
239	while (*s)
240	    if (waddch_literal(win, (*s++) | AttrOf(ch)) == ERR)
241		return ERR;
242	return (OK);
243    }
244
245    win->_curx = x;
246    win->_cury = y;
247
248    return (OK);
249}
250
251NCURSES_EXPORT(int)
252_nc_waddch_nosync
253(WINDOW *win, const chtype c)
254/* export copy of waddch_nosync() so the string-put functions can use it */
255{
256    return (waddch_nosync(win, c));
257}
258
259/*
260 * The versions below call _nc_synhook().  We wanted to avoid this in the
261 * version exported for string puts; they'll call _nc_synchook once at end
262 * of run.
263 */
264
265/* These are actual entry points */
266
267NCURSES_EXPORT(int)
268waddch
269(WINDOW *win, const chtype ch)
270{
271    int code = ERR;
272
273    TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_CALLED("waddch(%p, %s)"), win,
274				      _tracechtype(ch)));
275
276    if (win && (waddch_nosync(win, ch) != ERR)) {
277	_nc_synchook(win);
278	code = OK;
279    }
280
281    TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_RETURN("%d"), code));
282    return (code);
283}
284
285NCURSES_EXPORT(int)
286wechochar
287(WINDOW *win, const chtype ch)
288{
289    int code = ERR;
290
291    TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_CALLED("wechochar(%p, %s)"), win,
292				      _tracechtype(ch)));
293
294    if (win && (waddch_nosync(win, ch) != ERR)) {
295	bool save_immed = win->_immed;
296	win->_immed = TRUE;
297	_nc_synchook(win);
298	win->_immed = save_immed;
299	code = OK;
300    }
301    TR(TRACE_VIRTPUT | TRACE_CCALLS, (T_RETURN("%d"), code));
302    return (code);
303}
304