ansi_screen.c
Go to the documentation of this file.00001 #include <stdio.h>
00002 #include <curses.h>
00003 #include <console.h>
00004
00005 FILE_LICENCE ( GPL2_OR_LATER );
00006
00007 static void ansiscr_reset(struct _curses_screen *scr) __nonnull;
00008 static void ansiscr_movetoyx(struct _curses_screen *scr,
00009 unsigned int y, unsigned int x) __nonnull;
00010 static void ansiscr_putc(struct _curses_screen *scr, chtype c) __nonnull;
00011
00012 unsigned short _COLS = 80;
00013 unsigned short _LINES = 24;
00014
00015 static void ansiscr_reset ( struct _curses_screen *scr ) {
00016
00017 scr->attrs = 0;
00018 scr->curs_x = 0;
00019 scr->curs_y = 0;
00020 printf ( "\033[0m" );
00021 }
00022
00023 static void ansiscr_movetoyx ( struct _curses_screen *scr,
00024 unsigned int y, unsigned int x ) {
00025 if ( ( x != scr->curs_x ) || ( y != scr->curs_y ) ) {
00026
00027 printf ( "\033[%d;%dH", ( y + 1 ), ( x + 1 ) );
00028 scr->curs_x = x;
00029 scr->curs_y = y;
00030 }
00031 }
00032
00033 static void ansiscr_putc ( struct _curses_screen *scr, chtype c ) {
00034 unsigned int character = ( c & A_CHARTEXT );
00035 attr_t attrs = ( c & ( A_ATTRIBUTES | A_COLOR ) );
00036 int bold = ( attrs & A_BOLD );
00037 attr_t cpair = PAIR_NUMBER ( attrs );
00038 short fcol;
00039 short bcol;
00040
00041
00042 if ( attrs != scr->attrs ) {
00043 scr->attrs = attrs;
00044 pair_content ( cpair, &fcol, &bcol );
00045
00046 printf ( "\033[0;%d;3%d;4%dm", ( bold ? 1 : 22 ), fcol, bcol );
00047 }
00048
00049
00050 putchar ( character );
00051
00052
00053 if ( ++(scr->curs_x) == _COLS ) {
00054 scr->curs_x = 0;
00055 ++scr->curs_y;
00056 }
00057 }
00058
00059 static int ansiscr_getc ( struct _curses_screen *scr __unused ) {
00060 return getchar();
00061 }
00062
00063 static bool ansiscr_peek ( struct _curses_screen *scr __unused ) {
00064 return iskey();
00065 }
00066
00067 SCREEN _ansi_screen = {
00068 .init = ansiscr_reset,
00069 .exit = ansiscr_reset,
00070 .movetoyx = ansiscr_movetoyx,
00071 .putc = ansiscr_putc,
00072 .getc = ansiscr_getc,
00073 .peek = ansiscr_peek,
00074 };