soc/intel/xeon_sp/util: Enhance lock_pam0123
[coreboot2.git] / payloads / libpayload / curses / PDCurses / pdcurses / addch.c
blob12f767f7e6d0b2e57febdd96e3bce6a42fe3f9cc
1 /* Public Domain Curses */
3 #include <curspriv.h>
5 RCSID("$Id: addch.c,v 1.54 2008/07/13 16:08:17 wmcbrine Exp $")
7 /*man-start**************************************************************
9 Name: addch
11 Synopsis:
12 int addch(const chtype ch);
13 int waddch(WINDOW *win, const chtype ch);
14 int mvaddch(int y, int x, const chtype ch);
15 int mvwaddch(WINDOW *win, int y, int x, const chtype ch);
16 int echochar(const chtype ch);
17 int wechochar(WINDOW *win, const chtype ch);
19 int addrawch(chtype ch);
20 int waddrawch(WINDOW *win, chtype ch);
21 int mvaddrawch(int y, int x, chtype ch);
22 int mvwaddrawch(WINDOW *win, int y, int x, chtype ch);
24 int add_wch(const cchar_t *wch);
25 int wadd_wch(WINDOW *win, const cchar_t *wch);
26 int mvadd_wch(int y, int x, const cchar_t *wch);
27 int mvwadd_wch(WINDOW *win, int y, int x, const cchar_t *wch);
28 int echo_wchar(const cchar_t *wch);
29 int wecho_wchar(WINDOW *win, const cchar_t *wch);
31 Description:
32 addch() adds the chtype ch to the default window (stdscr) at the
33 current cursor position, and advances the cursor. Note that
34 chtypes can convey both text (a single character) and
35 attributes, including a color pair. add_wch() is the wide-
36 character version of this function, taking a pointer to a
37 cchar_t instead of a chtype.
39 waddch() is like addch(), but also lets you specify the window.
40 (This is in fact the core output routine.) wadd_wch() is the
41 wide version.
43 mvaddch() moves the cursor to the specified (y, x) position, and
44 adds ch to stdscr. mvadd_wch() is the wide version.
46 mvwaddch() moves the cursor to the specified position and adds
47 ch to the specified window. mvwadd_wch() is the wide version.
49 echochar() adds ch to stdscr at the current cursor position and
50 calls refresh(). echo_wchar() is the wide version.
52 wechochar() adds ch to the specified window and calls
53 wrefresh(). wecho_wchar() is the wide version.
55 addrawch(), waddrawch(), mvaddrawch() and mvwaddrawch() are
56 PDCurses-specific wrappers for addch() etc. that disable the
57 translation of control characters.
59 The following applies to all these functions:
61 If the cursor moves on to the right margin, an automatic newline
62 is performed. If scrollok is enabled, and a character is added
63 to the bottom right corner of the window, the scrolling region
64 will be scrolled up one line. If scrolling is not allowed, ERR
65 will be returned.
67 If ch is a tab, newline, or backspace, the cursor will be moved
68 appropriately within the window. If ch is a newline, the
69 clrtoeol routine is called before the cursor is moved to the
70 beginning of the next line. If newline mapping is off, the
71 cursor will be moved to the next line, but the x coordinate will
72 be unchanged. If ch is a tab the cursor is moved to the next
73 tab position within the window. If ch is another control
74 character, it will be drawn in the ^X notation. Calling the
75 inch() routine after adding a control character returns the
76 representation of the control character, not the control
77 character.
79 Video attributes can be combined with a character by ORing them
80 into the parameter. Text, including attributes, can be copied
81 from one place to another by using inch() and addch().
83 Note that in PDCurses, for now, a cchar_t and a chtype are the
84 same. The text field is 16 bits wide, and is treated as Unicode
85 (UCS-2) when PDCurses is built with wide-character support
86 (define PDC_WIDE). So, in functions that take a chtype, like
87 addch(), both the wide and narrow versions will handle Unicode.
88 But for portability, you should use the wide functions.
90 Return Value:
91 All functions return OK on success and ERR on error.
93 Portability X/Open BSD SYS V
94 addch Y Y Y
95 waddch Y Y Y
96 mvaddch Y Y Y
97 mvwaddch Y Y Y
98 echochar Y - 3.0
99 wechochar Y - 3.0
100 addrawch - - -
101 waddrawch - - -
102 mvaddrawch - - -
103 mvwaddrawch - - -
104 add_wch Y
105 wadd_wch Y
106 mvadd_wch Y
107 mvwadd_wch Y
108 echo_wchar Y
109 wecho_wchar Y
111 **man-end****************************************************************/
113 int waddch(WINDOW *win, const chtype ch)
115 int x, y;
116 chtype text, attr;
117 bool xlat;
119 PDC_LOG(("waddch() - called: win=%p ch=%x (text=%c attr=0x%x)\n",
120 win, ch, ch & A_CHARTEXT, ch & A_ATTRIBUTES));
122 if (!win)
123 return ERR;
125 x = win->_curx;
126 y = win->_cury;
128 if (y > win->_maxy || x > win->_maxx || y < 0 || x < 0)
129 return ERR;
131 xlat = !SP->raw_out && !(ch & A_ALTCHARSET);
132 text = ch & A_CHARTEXT;
133 attr = ch & A_ATTRIBUTES;
135 if (xlat && (text < ' ' || text == 0x7f))
137 int x2;
139 switch (text)
141 case '\t':
142 for (x2 = ((x / TABSIZE) + 1) * TABSIZE; x < x2; x++)
144 if (waddch(win, attr | ' ') == ERR)
145 return ERR;
147 /* if tab to next line, exit the loop */
149 if (!win->_curx)
150 break;
152 return OK;
154 case '\n':
155 /* if lf -> crlf */
157 if (!SP->raw_out)
158 x = 0;
160 wclrtoeol(win);
162 if (++y > win->_bmarg)
164 y--;
166 if (wscrl(win, 1) == ERR)
167 return ERR;
170 break;
172 case '\b':
173 /* don't back over left margin */
175 if (--x < 0)
176 x = 0;
177 break;
178 case '\r':
179 x = 0;
181 break;
183 case 0x7f:
184 if (waddch(win, attr | '^') == ERR)
185 return ERR;
187 return waddch(win, attr | '?');
189 default:
190 /* handle control chars */
192 if (waddch(win, attr | '^') == ERR)
193 return ERR;
195 return waddch(win, ch + '@');
198 else
200 /* If the incoming character doesn't have its own attribute,
201 then use the current attributes for the window. If it has
202 attributes but not a color component, OR the attributes to
203 the current attributes for the window. If it has a color
204 component, use the attributes solely from the incoming
205 character. */
207 if (!(attr & A_COLOR))
208 attr |= win->_attrs;
210 /* wrs (4/10/93): Apply the same sort of logic for the window
211 background, in that it only takes precedence if other color
212 attributes are not there and that the background character
213 will only print if the printing character is blank. */
215 if (!(attr & A_COLOR))
216 attr |= win->_bkgd & A_ATTRIBUTES;
217 else
218 attr |= win->_bkgd & (A_ATTRIBUTES ^ A_COLOR);
220 if (text == ' ')
221 text = win->_bkgd & A_CHARTEXT;
223 /* Add the attribute back into the character. */
225 text |= attr;
227 /* Only change _firstch/_lastch if the character to be added is
228 different from the character/attribute that is already in
229 that position in the window. */
231 if (win->_y[y][x] != text)
233 if (win->_firstch[y] == _NO_CHANGE)
234 win->_firstch[y] = win->_lastch[y] = x;
235 else
236 if (x < win->_firstch[y])
237 win->_firstch[y] = x;
238 else
239 if (x > win->_lastch[y])
240 win->_lastch[y] = x;
242 win->_y[y][x] = text;
245 if (++x >= win->_maxx)
247 /* wrap around test */
249 x = 0;
251 if (++y > win->_bmarg)
253 y--;
255 if (wscrl(win, 1) == ERR)
257 PDC_sync(win);
258 return ERR;
264 win->_curx = x;
265 win->_cury = y;
267 if (win->_immed)
268 wrefresh(win);
269 if (win->_sync)
270 wsyncup(win);
272 return OK;
275 int addch(const chtype ch)
277 PDC_LOG(("addch() - called: ch=%x\n", ch));
279 return waddch(stdscr, ch);
282 int mvaddch(int y, int x, const chtype ch)
284 PDC_LOG(("mvaddch() - called: y=%d x=%d ch=%x\n", y, x, ch));
286 if (move(y,x) == ERR)
287 return ERR;
289 return waddch(stdscr, ch);
292 int mvwaddch(WINDOW *win, int y, int x, const chtype ch)
294 PDC_LOG(("mvwaddch() - called: win=%p y=%d x=%d ch=%d\n", win, y, x, ch));
296 if (wmove(win, y, x) == ERR)
297 return ERR;
299 return waddch(win, ch);
302 int echochar(const chtype ch)
304 PDC_LOG(("echochar() - called: ch=%x\n", ch));
306 return wechochar(stdscr, ch);
309 int wechochar(WINDOW *win, const chtype ch)
311 PDC_LOG(("wechochar() - called: win=%p ch=%x\n", win, ch));
313 if (waddch(win, ch) == ERR)
314 return ERR;
316 return wrefresh(win);
319 int waddrawch(WINDOW *win, chtype ch)
321 PDC_LOG(("waddrawch() - called: win=%p ch=%x (text=%c attr=0x%x)\n",
322 win, ch, ch & A_CHARTEXT, ch & A_ATTRIBUTES));
324 if ((ch & A_CHARTEXT) < ' ' || (ch & A_CHARTEXT) == 0x7f)
325 ch |= A_ALTCHARSET;
327 return waddch(win, ch);
330 int addrawch(chtype ch)
332 PDC_LOG(("addrawch() - called: ch=%x\n", ch));
334 return waddrawch(stdscr, ch);
337 int mvaddrawch(int y, int x, chtype ch)
339 PDC_LOG(("mvaddrawch() - called: y=%d x=%d ch=%d\n", y, x, ch));
341 if (move(y, x) == ERR)
342 return ERR;
344 return waddrawch(stdscr, ch);
347 int mvwaddrawch(WINDOW *win, int y, int x, chtype ch)
349 PDC_LOG(("mvwaddrawch() - called: win=%p y=%d x=%d ch=%d\n",
350 win, y, x, ch));
352 if (wmove(win, y, x) == ERR)
353 return ERR;
355 return waddrawch(win, ch);
358 #ifdef PDC_WIDE
359 int wadd_wch(WINDOW *win, const cchar_t *wch)
361 PDC_LOG(("wadd_wch() - called: win=%p wch=%x\n", win, *wch));
363 return wch ? waddch(win, *wch) : ERR;
366 int add_wch(const cchar_t *wch)
368 PDC_LOG(("add_wch() - called: wch=%x\n", *wch));
370 return wadd_wch(stdscr, wch);
373 int mvadd_wch(int y, int x, const cchar_t *wch)
375 PDC_LOG(("mvaddch() - called: y=%d x=%d wch=%x\n", y, x, *wch));
377 if (move(y,x) == ERR)
378 return ERR;
380 return wadd_wch(stdscr, wch);
383 int mvwadd_wch(WINDOW *win, int y, int x, const cchar_t *wch)
385 PDC_LOG(("mvwaddch() - called: win=%p y=%d x=%d wch=%d\n",
386 win, y, x, *wch));
388 if (wmove(win, y, x) == ERR)
389 return ERR;
391 return wadd_wch(win, wch);
394 int echo_wchar(const cchar_t *wch)
396 PDC_LOG(("echo_wchar() - called: wch=%x\n", *wch));
398 return wecho_wchar(stdscr, wch);
401 int wecho_wchar(WINDOW *win, const cchar_t *wch)
403 PDC_LOG(("wecho_wchar() - called: win=%p wch=%x\n", win, *wch));
405 if (!wch || (wadd_wch(win, wch) == ERR))
406 return ERR;
408 return wrefresh(win);
410 #endif