2007-07-03 19:20:22 +02:00
|
|
|
#include <stdio.h>
|
2006-12-19 01:27:25 +01:00
|
|
|
#include <curses.h>
|
2011-03-09 21:09:26 +01:00
|
|
|
#include <ipxe/console.h>
|
2006-12-19 01:27:25 +01:00
|
|
|
|
2009-05-01 16:41:06 +02:00
|
|
|
FILE_LICENCE ( GPL2_OR_LATER );
|
|
|
|
|
2007-08-20 00:32:51 +02:00
|
|
|
static void ansiscr_reset(struct _curses_screen *scr) __nonnull;
|
|
|
|
static void ansiscr_movetoyx(struct _curses_screen *scr,
|
|
|
|
unsigned int y, unsigned int x) __nonnull;
|
|
|
|
static void ansiscr_putc(struct _curses_screen *scr, chtype c) __nonnull;
|
|
|
|
|
2006-12-19 01:27:25 +01:00
|
|
|
unsigned short _COLS = 80;
|
2006-12-19 02:16:43 +01:00
|
|
|
unsigned short _LINES = 24;
|
2006-12-19 01:27:25 +01:00
|
|
|
|
2012-03-26 20:02:30 +02:00
|
|
|
static unsigned int saved_usage;
|
|
|
|
|
2013-11-28 06:01:29 +01:00
|
|
|
static void ansiscr_attrs ( struct _curses_screen *scr, attr_t attrs ) {
|
|
|
|
int bold = ( attrs & A_BOLD );
|
|
|
|
attr_t cpair = PAIR_NUMBER ( attrs );
|
|
|
|
short fcol;
|
|
|
|
short bcol;
|
|
|
|
|
|
|
|
if ( scr->attrs != attrs ) {
|
|
|
|
scr->attrs = attrs;
|
|
|
|
pair_content ( cpair, &fcol, &bcol );
|
|
|
|
/* ANSI escape sequence to update character attributes */
|
|
|
|
printf ( "\033[0;%d;3%d;4%dm", ( bold ? 1 : 22 ), fcol, bcol );
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2006-12-20 02:30:40 +01:00
|
|
|
static void ansiscr_reset ( struct _curses_screen *scr ) {
|
2006-12-19 01:33:36 +01:00
|
|
|
/* Reset terminal attributes and clear screen */
|
2006-12-19 01:51:32 +01:00
|
|
|
scr->attrs = 0;
|
|
|
|
scr->curs_x = 0;
|
|
|
|
scr->curs_y = 0;
|
2013-11-28 06:01:29 +01:00
|
|
|
printf ( "\033[0m\033[2J" );
|
2006-12-19 01:27:25 +01:00
|
|
|
}
|
|
|
|
|
2012-03-26 20:02:30 +02:00
|
|
|
static void ansiscr_init ( struct _curses_screen *scr ) {
|
|
|
|
saved_usage = console_set_usage ( CONSOLE_USAGE_TUI );
|
|
|
|
ansiscr_reset ( scr );
|
|
|
|
}
|
|
|
|
|
|
|
|
static void ansiscr_exit ( struct _curses_screen *scr ) {
|
|
|
|
ansiscr_reset ( scr );
|
|
|
|
console_set_usage ( saved_usage );
|
|
|
|
}
|
|
|
|
|
2013-11-28 06:01:29 +01:00
|
|
|
static void ansiscr_erase ( struct _curses_screen *scr, attr_t attrs ) {
|
|
|
|
ansiscr_attrs ( scr, attrs );
|
|
|
|
printf ( "\033[2J" );
|
|
|
|
}
|
|
|
|
|
2006-12-19 01:51:32 +01:00
|
|
|
static void ansiscr_movetoyx ( struct _curses_screen *scr,
|
2006-12-19 01:27:25 +01:00
|
|
|
unsigned int y, unsigned int x ) {
|
2006-12-19 01:51:32 +01:00
|
|
|
if ( ( x != scr->curs_x ) || ( y != scr->curs_y ) ) {
|
|
|
|
/* ANSI escape sequence to update cursor position */
|
|
|
|
printf ( "\033[%d;%dH", ( y + 1 ), ( x + 1 ) );
|
|
|
|
scr->curs_x = x;
|
|
|
|
scr->curs_y = y;
|
|
|
|
}
|
2006-12-19 01:27:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static void ansiscr_putc ( struct _curses_screen *scr, chtype c ) {
|
|
|
|
unsigned int character = ( c & A_CHARTEXT );
|
|
|
|
attr_t attrs = ( c & ( A_ATTRIBUTES | A_COLOR ) );
|
|
|
|
|
2006-12-19 01:51:32 +01:00
|
|
|
/* Update attributes if changed */
|
2013-11-28 06:01:29 +01:00
|
|
|
ansiscr_attrs ( scr, attrs );
|
2006-12-19 01:51:32 +01:00
|
|
|
|
|
|
|
/* Print the actual character */
|
2006-12-19 01:27:25 +01:00
|
|
|
putchar ( character );
|
2006-12-19 01:51:32 +01:00
|
|
|
|
|
|
|
/* Update expected cursor position */
|
|
|
|
if ( ++(scr->curs_x) == _COLS ) {
|
|
|
|
scr->curs_x = 0;
|
|
|
|
++scr->curs_y;
|
|
|
|
}
|
2006-12-19 01:27:25 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static int ansiscr_getc ( struct _curses_screen *scr __unused ) {
|
|
|
|
return getchar();
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool ansiscr_peek ( struct _curses_screen *scr __unused ) {
|
|
|
|
return iskey();
|
|
|
|
}
|
|
|
|
|
2013-12-02 18:23:09 +01:00
|
|
|
static void ansiscr_cursor ( struct _curses_screen *scr __unused,
|
|
|
|
int visibility ) {
|
|
|
|
printf ( "\033[?25%c", ( visibility ? 'h' : 'l' ) );
|
|
|
|
}
|
|
|
|
|
2006-12-19 01:27:25 +01:00
|
|
|
SCREEN _ansi_screen = {
|
2012-03-26 20:02:30 +02:00
|
|
|
.init = ansiscr_init,
|
|
|
|
.exit = ansiscr_exit,
|
2013-11-28 06:01:29 +01:00
|
|
|
.erase = ansiscr_erase,
|
2006-12-19 01:27:25 +01:00
|
|
|
.movetoyx = ansiscr_movetoyx,
|
|
|
|
.putc = ansiscr_putc,
|
|
|
|
.getc = ansiscr_getc,
|
|
|
|
.peek = ansiscr_peek,
|
2013-12-02 18:23:09 +01:00
|
|
|
.cursor = ansiscr_cursor,
|
2006-12-19 01:27:25 +01:00
|
|
|
};
|