Now basically functioning on ANSI-supporting consoles.

This commit is contained in:
Michael Brown
2006-12-19 00:27:25 +00:00
parent 09f9142cbf
commit edbdce6e3d
8 changed files with 215 additions and 95 deletions

View File

@@ -0,0 +1,51 @@
#include <curses.h>
#include <console.h>
unsigned short _COLS = 80;
unsigned short _LINES = 25;
static void ansiscr_init ( struct _curses_screen *scr __unused ) {
}
static void ansiscr_exit ( struct _curses_screen *scr __unused ) {
}
static void ansiscr_movetoyx ( struct _curses_screen *scr __unused,
unsigned int y, unsigned int x ) {
/* ANSI escape sequence to update cursor position */
printf ( "\033[%d;%dH", ( y + 1 ), ( x + 1 ) );
}
static void ansiscr_putc ( struct _curses_screen *scr, chtype c ) {
unsigned int character = ( c & A_CHARTEXT );
attr_t attrs = ( c & ( A_ATTRIBUTES | A_COLOR ) );
int bold = ( attrs & A_BOLD );
attr_t cpair = PAIR_NUMBER ( attrs );
short fcol;
short bcol;
if ( attrs != scr->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 );
}
putchar ( character );
}
static int ansiscr_getc ( struct _curses_screen *scr __unused ) {
return getchar();
}
static bool ansiscr_peek ( struct _curses_screen *scr __unused ) {
return iskey();
}
SCREEN _ansi_screen = {
.init = ansiscr_init,
.exit = ansiscr_exit,
.movetoyx = ansiscr_movetoyx,
.putc = ansiscr_putc,
.getc = ansiscr_getc,
.peek = ansiscr_peek,
};

View File

@@ -1,14 +1,13 @@
#include <curses.h>
/**
* Indicates whether the underlying terminal device is capable of
* having colours redefined
*
* @ret bool returns boolean
*/
bool can_change_colour ( void ) {
return (bool)TRUE;
}
struct colour_pair {
short fcol;
short bcol;
};
static struct colour_pair cpairs[COLOUR_PAIRS] = {
[0] = { COLOUR_WHITE, COLOUR_BLACK },
};
/**
* Identify the RGB components of a given colour value
@@ -20,31 +19,46 @@ bool can_change_colour ( void ) {
* @ret rc return status code
*/
int colour_content ( short colour, short *red, short *green, short *blue ) {
/* we do not have a particularly large range of colours (3
primary, 3 secondary and black), so let's just put in a
basic switch... */
switch(colour) {
case COLOUR_BLACK:
*red = 0; *green = 0; *blue = 0;
break;
case COLOUR_BLUE:
*red = 0; *green = 0; *blue = 1000;
break;
case COLOUR_GREEN:
*red = 0; *green = 1000; *blue = 0;
break;
case COLOUR_CYAN:
*red = 0; *green = 1000; *blue = 1000;
break;
case COLOUR_RED:
*red = 1000; *green = 0; *blue = 0;
break;
case COLOUR_MAGENTA:
*red = 1000; *green = 0; *blue = 1000;
break;
case COLOUR_YELLOW:
*red = 1000; *green = 1000; *blue = 0;
break;
}
*red = ( ( colour & COLOUR_RED ) ? 1 : 0 );
*green = ( ( colour & COLOUR_GREEN ) ? 1 : 0 );
*blue = ( ( colour & COLOUR_BLUE ) ? 1 : 0 );
return OK;
}
/**
* Initialise colour pair
*
* @v pair colour pair number
* @v fcol foreground colour
* @v bcol background colour
*/
int init_pair ( short pair, short fcol, short bcol ) {
struct colour_pair *cpair;
if ( ( pair < 1 ) || ( pair >= COLOUR_PAIRS ) )
return ERR;
cpair = &cpairs[pair];
cpair->fcol = fcol;
cpair->bcol = bcol;
return OK;
}
/**
* Get colours of colour pair
*
* @v pair colour pair number
* @ret fcol foreground colour
* @ret bcol background colour
*/
int pair_content ( short pair, short *fcol, short *bcol ) {
struct colour_pair *cpair;
if ( ( pair < 0 ) || ( pair >= COLOUR_PAIRS ) )
return ERR;
cpair = &cpairs[pair];
*fcol = cpair->fcol;
*bcol = cpair->bcol;
return OK;
}

View File

@@ -1,7 +1,6 @@
#include <curses.h>
#include <stddef.h>
#include <timer.h>
#include <console.h>
#include "mucurses.h"
/** @file
@@ -38,7 +37,7 @@ int _wgetc ( WINDOW *win ) {
return ERR;
timer = INPUT_DELAY_TIMEOUT;
while ( ! iskey() ) {
while ( ! win->scr->peek( win->scr ) ) {
if ( m_delay == 0 ) // non-blocking read
return ERR;
if ( timer > 0 ) { // time-limited blocking read
@@ -48,7 +47,7 @@ int _wgetc ( WINDOW *win ) {
} else { return ERR; } // non-blocking read
}
c = getchar();
c = win->scr->getc( win->scr );
if ( m_echo && ( c >= 32 && c <= 126 ) ) // printable ASCII characters
_wputch( win, (chtype) ( c | win->attrs ), WRAP );

View File

@@ -1,3 +1,4 @@
#include <console.h>
#include <curses.h>
#include "mucurses.h"
@@ -13,13 +14,23 @@ WINDOW _stdscr = {
.ori_x = 0,
.curs_y = 0,
.curs_x = 0,
.scr = curscr,
.scr = &_ansi_screen,
};
/*
* Primitives
*/
/**
* Update cursor position
*
* @v *win window in which to update position
*/
static void _wupdcurs ( WINDOW *win ) {
win->scr->movetoyx ( win->scr, win->ori_y + win->curs_y,
win->ori_x + win->curs_x );
}
/**
* Write a single character rendition to a window
*
@@ -30,8 +41,7 @@ WINDOW _stdscr = {
void _wputch ( WINDOW *win, chtype ch, int wrap ) {
/* make sure we set the screen cursor to the right position
first! */
win->scr->movetoyx( win->scr, win->ori_y + win->curs_y,
win->ori_x + win->curs_x );
_wupdcurs(win);
win->scr->putc(win->scr, ch);
if ( ++(win->curs_x) - win->width == 0 ) {
if ( wrap == WRAP ) {
@@ -63,8 +73,7 @@ void _wcursback ( WINDOW *win ) {
win->curs_x--;
}
win->scr->movetoyx( win->scr, win->ori_y + win->curs_y,
win->ori_x + win->curs_x );
_wupdcurs(win);
}
/**
@@ -112,7 +121,6 @@ int wmove ( WINDOW *win, int y, int x ) {
win->curs_y = y;
win->curs_x = x;
win->scr->movetoyx( win->scr, win->ori_y + win->curs_y,
win->ori_x + win->curs_x );
_wupdcurs(win);
return OK;
}

View File

@@ -10,9 +10,11 @@
#define WRAP 0
#define NOWRAP 1
void _wputch ( WINDOW *win, chtype ch, int wrap );
void _wputchstr ( WINDOW *win, const chtype *chstr, int wrap, int n );
void _wputstr ( WINDOW *win, const char *str, int wrap, int n );
void _wcursback ( WINDOW *win );
extern SCREEN _ansi_screen;
extern void _wputch ( WINDOW *win, chtype ch, int wrap );
extern void _wputchstr ( WINDOW *win, const chtype *chstr, int wrap, int n );
extern void _wputstr ( WINDOW *win, const char *str, int wrap, int n );
extern void _wcursback ( WINDOW *win );
#endif /* _MUCURSES_H */

View File

@@ -64,7 +64,7 @@ int wattrset ( WINDOW *win, int attrs ) {
int wattr_get ( WINDOW *win, attr_t *attrs, short *pair,
void *opts __unused ) {
*attrs = win->attrs & A_ATTRIBUTES;
*pair = (short)(( win->attrs & A_COLOR ) >> CPAIR_SHIFT);
*pair = PAIR_NUMBER ( win->attrs );
return OK;
}
@@ -107,7 +107,7 @@ int wattr_on ( WINDOW *win, attr_t attrs,
*/
int wattr_set ( WINDOW *win, attr_t attrs, short cpair,
void *opts __unused ) {
wattrset( win, attrs | ( ( (unsigned short)cpair ) << CPAIR_SHIFT ) );
wattrset( win, attrs | COLOUR_PAIR ( cpair ) );
return OK;
}
@@ -121,11 +121,11 @@ int wattr_set ( WINDOW *win, attr_t attrs, short cpair,
*/
int wcolour_set ( WINDOW *win, short colour_pair_number,
void *opts __unused ) {
if ( ( unsigned short )colour_pair_number > COLORS )
if ( ( unsigned short )colour_pair_number > COLOUR_PAIRS )
return ERR;
win->attrs = ( (unsigned short)colour_pair_number << CPAIR_SHIFT ) |
( win->attrs & A_ATTRIBUTES );
win->attrs = ( ( win->attrs & A_ATTRIBUTES ) |
COLOUR_PAIR ( colour_pair_number ) );
return OK;
}

View File

@@ -14,10 +14,19 @@
WINDOW *initscr ( void ) {
/* determine console size */
/* initialise screen */
curscr->init( curscr );
stdscr->scr->init( stdscr->scr );
stdscr->height = LINES;
stdscr->width = COLS;
werase( stdscr );
return stdscr;
}
/**
* Finalise console environment
*
*/
int endwin ( void ) {
stdscr->scr->exit( stdscr->scr );
return OK;
}