1 /* ----------------------------------------------------------------------- *
3 * Copyright 2009 Erwan Velu - All Rights Reserved
5 * Permission is hereby granted, free of charge, to any person
6 * obtaining a copy of this software and associated documentation
7 * files (the "Software"), to deal in the Software without
8 * restriction, including without limitation the rights to use,
9 * copy, modify, merge, publish, distribute, sublicense, and/or
10 * sell copies of the Software, and to permit persons to whom
11 * the Software is furnished to do so, subject to the following
14 * The above copyright notice and this permission notice shall
15 * be included in all copies or substantial portions of the Software.
17 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
18 * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES
19 * OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
20 * NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT
21 * HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
22 * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
23 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
24 * OTHER DEALINGS IN THE SOFTWARE.
26 * -----------------------------------------------------------------------
27 * Ansi Sequences can be found here :
28 * http://ascii-table.com/ansi-escape-sequences-vt-100.php
29 * http://en.wikipedia.org/wiki/ANSI_escape_code
40 void display_cursor(bool status
)
43 fputs(CSI
"?25h", stdout
);
45 fputs(CSI
"?25l", stdout
);
49 void clear_end_of_line(void)
51 fputs(CSI
"0K", stdout
);
54 void move_cursor_left(int count
)
57 memset(buffer
,0,sizeof(buffer
));
58 sprintf(buffer
,CSI
"%dD",count
);
59 fputs(buffer
, stdout
);
62 void move_cursor_right(int count
)
65 memset(buffer
,0,sizeof(buffer
));
66 sprintf(buffer
, CSI
"%dC", count
);
67 fputs(buffer
, stdout
);
70 void set_cursor_blink(bool status
) {
72 fputs("\033[05m",stdout
);
74 fputs("\033[0m",stdout
);
79 fputs(CSI
"2K", stdout
);
82 void clear_beginning_of_line(void)
84 fputs(CSI
"1K", stdout
);
87 void move_cursor_to_column(int count
)
90 memset(buffer
,0,sizeof(buffer
));
91 sprintf(buffer
, CSI
"%dG", count
);
92 fputs(buffer
, stdout
);
95 void move_cursor_to_next_line(void)
97 fputs("\033e", stdout
);
100 void disable_utf8(void)
102 fputs("\033%@", stdout
);
105 void set_g1_special_char(void){
106 fputs("\033)0", stdout
);
109 void set_us_g0_charset(void)
111 fputs("\033(B\1#0", stdout
);
114 void clear_entire_screen(void)
116 fputs(CSI
"2J", stdout
);
120 * cprint_vga2ansi - given a VGA attribute, print a character
121 * @chr: character to print
122 * @attr: vga attribute
124 * Convert the VGA attribute @attr to an ANSI escape sequence and
126 * For performance, SGR parameters are cached. To reset them,
127 * call cprint_vga2ansi('0', '0').
129 static void cprint_vga2ansi(const char chr
, const char attr
)
131 static const char ansi_char
[8] = "04261537";
132 static uint16_t last_attr
= 0x300;
135 if (chr
== '0' && attr
== '0') {
140 if (attr
!= last_attr
) {
146 if (last_attr
& ~attr
& 0x88) {
149 /* Reset last_attr to unknown to handle
150 * background/foreground attributes correctly */
162 if (reset
|| (attr
^ last_attr
) & 0x07) {
164 *p
++ = ansi_char
[attr
& 7];
167 if (reset
|| (attr
^ last_attr
) & 0x70) {
169 *p
++ = ansi_char
[(attr
>> 4) & 7];
172 p
[-1] = 'm'; /* We'll have generated at least one semicolon */
184 * cls - clear and initialize the entire screen
186 * Note: when initializing xterm, one has to specify that
187 * G1 points to the alternate character set (this is not true
188 * by default). Without the initial printf "\033)0", line drawing
189 * characters won't be displayed.
193 fputs("\033e\033%@\033)0\033(B\1#0\033[?25l\033[2J", stdout
);
195 /* Reset SGR parameters cache */
196 cprint_vga2ansi('0', '0');
199 void reset_colors(void)
201 csprint(CSI
"1D", 0x07);
205 * cprint - given a VGA attribute, print a single character at cursor
206 * @chr: character to print
207 * @attr: VGA attribute
208 * @times: number of times to print @chr
210 * Note: @attr is a VGA attribute.
212 void cprint(const char chr
, const char attr
, unsigned int times
)
215 cprint_vga2ansi(chr
, attr
);
219 * csprint - given a VGA attribute, print a NULL-terminated string
220 * @str: string to print
221 * @attr: VGA attribute
223 void csprint(const char *str
, const char attr
)
226 cprint(*str
, attr
, 1);
232 * clearwindow - fill a given a region on the screen
233 * @top, @left, @bot, @right: coordinates to fill
234 * @fillchar: character to use to fill the region
235 * @fillattr: character attribute (VGA)
237 void clearwindow(const char top
, const char left
, const char bot
,
238 const char right
, const char fillchar
, const char fillattr
)
241 for (x
= top
; x
< bot
+ 1; x
++) {
243 cprint(fillchar
, fillattr
, right
- left
+ 1);