iPXE
ansi_screen.c
Go to the documentation of this file.
00001 #include <stdio.h>
00002 #include <curses.h>
00003 #include <ipxe/ansicol.h>
00004 #include <ipxe/console.h>
00005 
00006 FILE_LICENCE ( GPL2_OR_LATER_OR_UBDL );
00007 
00008 static void ansiscr_reset(struct _curses_screen *scr) __nonnull;
00009 static void ansiscr_movetoyx(struct _curses_screen *scr,
00010                                unsigned int y, unsigned int x) __nonnull;
00011 static void ansiscr_putc(struct _curses_screen *scr, chtype c) __nonnull;
00012 
00013 static unsigned int saved_usage;
00014 
00015 static void ansiscr_attrs ( struct _curses_screen *scr, attr_t attrs ) {
00016         int bold = ( attrs & A_BOLD );
00017         attr_t cpair = PAIR_NUMBER ( attrs );
00018 
00019         if ( scr->attrs != attrs ) {
00020                 scr->attrs = attrs;
00021                 /* Reset attributes and set/clear bold as appropriate */
00022                 printf ( "\033[0;%dm", ( bold ? 1 : 22 ) );
00023                 /* Set foreground and background colours */
00024                 ansicol_set_pair ( cpair );
00025         }
00026 }
00027 
00028 static void ansiscr_reset ( struct _curses_screen *scr ) {
00029         /* Reset terminal attributes and clear screen */
00030         scr->attrs = 0;
00031         scr->curs_x = 0;
00032         scr->curs_y = 0;
00033         printf ( "\0330m" );
00034         ansicol_set_pair ( CPAIR_DEFAULT );
00035         printf ( "\033[2J" );
00036 }
00037 
00038 static void ansiscr_init ( struct _curses_screen *scr ) {
00039         saved_usage = console_set_usage ( CONSOLE_USAGE_TUI );
00040         ansiscr_reset ( scr );
00041 }
00042 
00043 static void ansiscr_exit ( struct _curses_screen *scr ) {
00044         ansiscr_reset ( scr );
00045         console_set_usage ( saved_usage );
00046 }
00047 
00048 static void ansiscr_erase ( struct _curses_screen *scr, attr_t attrs ) {
00049         ansiscr_attrs ( scr, attrs );
00050         printf ( "\033[2J" );
00051 }
00052 
00053 static void ansiscr_movetoyx ( struct _curses_screen *scr,
00054                                unsigned int y, unsigned int x ) {
00055         if ( ( x != scr->curs_x ) || ( y != scr->curs_y ) ) {
00056                 /* ANSI escape sequence to update cursor position */
00057                 printf ( "\033[%d;%dH", ( y + 1 ), ( x + 1 ) );
00058                 scr->curs_x = x;
00059                 scr->curs_y = y;
00060         }
00061 }
00062 
00063 static void ansiscr_putc ( struct _curses_screen *scr, chtype c ) {
00064         unsigned int character = ( c & A_CHARTEXT );
00065         attr_t attrs = ( c & ( A_ATTRIBUTES | A_COLOR ) );
00066 
00067         /* Update attributes if changed */
00068         ansiscr_attrs ( scr, attrs );
00069 
00070         /* Print the actual character */
00071         putchar ( character );
00072 
00073         /* Update expected cursor position */
00074         if ( ++(scr->curs_x) == COLS ) {
00075                 scr->curs_x = 0;
00076                 ++scr->curs_y;
00077         }
00078 }
00079 
00080 static int ansiscr_getc ( struct _curses_screen *scr __unused ) {
00081         return getchar();
00082 }
00083 
00084 static bool ansiscr_peek ( struct _curses_screen *scr __unused ) {
00085         return iskey();
00086 }
00087 
00088 static void ansiscr_cursor ( struct _curses_screen *scr __unused,
00089                              int visibility ) {
00090         printf ( "\033[?25%c", ( visibility ? 'h' : 'l' ) );
00091 }
00092 
00093 SCREEN _ansi_screen = {
00094         .init           = ansiscr_init,
00095         .exit           = ansiscr_exit,
00096         .erase          = ansiscr_erase,
00097         .movetoyx       = ansiscr_movetoyx,
00098         .putc           = ansiscr_putc,
00099         .getc           = ansiscr_getc,
00100         .peek           = ansiscr_peek,
00101         .cursor         = ansiscr_cursor,
00102 };