2
0
mirror of https://github.com/xcat2/xNBA.git synced 2024-12-14 07:11:32 +00:00

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;
}

View File

@ -28,6 +28,9 @@ typedef uint32_t attr_t;
/** Curses SCREEN object */
typedef struct _curses_screen {
/** Current attribute */
attr_t attrs;
void ( *init ) ( struct _curses_screen *scr );
void ( *exit ) ( struct _curses_screen *scr );
/**
@ -46,6 +49,21 @@ typedef struct _curses_screen {
* @v c character to be written
*/
void ( * putc ) ( struct _curses_screen *scr, chtype c );
/**
* Pop a character from the keyboard input stream
*
* @v scr screen on which to operate
* @ret c popped character
*/
int ( * getc ) ( struct _curses_screen *scr );
/**
* Checks to see whether a character is waiting in the input stream
*
* @v scr screen on which to operate
* @ret TRUE character waiting in stream
* @ret FALSE no character waiting in stream
*/
bool ( *peek ) ( struct _curses_screen *scr );
} SCREEN;
/** Curses Window struct */
@ -69,55 +87,57 @@ typedef struct _curses_window {
} WINDOW;
extern WINDOW _stdscr;
extern SCREEN _curscr;
extern unsigned short _COLS;
extern unsigned short _LINES;
extern unsigned int _COLOURS;
extern unsigned int _COLOUR_PAIRS;
#define stdscr ( &_stdscr )
#define curscr ( &_curscr )
#define COLS _COLS
#define LINES _LINES
#define COLORS _COLOURS
#define COLOR_PAIRS _COLOUR_PAIRS
#define MUCURSES_BITS( mask, shift ) (( mask ) << (shift))
#define CPAIR_SHIFT 8
#define ATTRS_SHIFT 16
#define A_DEFAULT ( 1UL - 1UL )
#define A_ALTCHARSET MUCURSES_BITS( 1UL, ATTRS_SHIFT + 0 )
#define A_BLINK MUCURSES_BITS( 1UL, ATTRS_SHIFT + 1 )
#define A_BOLD MUCURSES_BITS( 1UL, ATTRS_SHIFT + 2 )
#define A_DIM MUCURSES_BITS( 1UL, ATTRS_SHIFT + 3 )
#define A_INVIS MUCURSES_BITS( 1UL, ATTRS_SHIFT + 4 )
#define A_PROTECT MUCURSES_BITS( 1UL, ATTRS_SHIFT + 5 )
#define A_REVERSE MUCURSES_BITS( 1UL, ATTRS_SHIFT + 6 )
#define A_STANDOUT MUCURSES_BITS( 1UL, ATTRS_SHIFT + 7 )
#define A_UNDERLINE MUCURSES_BITS( 1UL, ATTRS_SHIFT + 8 )
#define WA_DEFAULT ( 0x0000 << ATTRS_SHIFT )
#define WA_ALTCHARSET ( 0x0001 << ATTRS_SHIFT )
#define WA_BLINK ( 0x0002 << ATTRS_SHIFT )
#define WA_BOLD ( 0x0004 << ATTRS_SHIFT )
#define WA_DIM ( 0x0008 << ATTRS_SHIFT )
#define WA_INVIS ( 0x0010 << ATTRS_SHIFT )
#define WA_PROTECT ( 0x0020 << ATTRS_SHIFT )
#define WA_REVERSE ( 0x0040 << ATTRS_SHIFT )
#define WA_STANDOUT ( 0x0080 << ATTRS_SHIFT )
#define WA_UNDERLINE ( 0x0100 << ATTRS_SHIFT )
#define WA_HORIZONTAL ( 0x0200 << ATTRS_SHIFT )
#define WA_VERTICAL ( 0x0400 << ATTRS_SHIFT )
#define WA_LEFT ( 0x0800 << ATTRS_SHIFT )
#define WA_RIGHT ( 0x1000 << ATTRS_SHIFT )
#define WA_LOW ( 0x2000 << ATTRS_SHIFT )
#define WA_TOP ( 0x4000 << ATTRS_SHIFT )
#define WA_ALTCHARSET A_ALTCHARSET
#define WA_BLINK A_BLINK
#define WA_BOLD A_BOLD
#define WA_DIM A_DIM
#define WA_INVIS A_INVIS
#define WA_PROTECT A_PROTECT
#define WA_REVERSE A_REVERSE
#define WA_STANDOUT A_STANDOUT
#define WA_UNDERLINE A_UNDERLINE
#define WA_HORIZONTAL MUCURSES_BITS( 1UL, ATTRS_SHIFT + 9 )
#define WA_VERTICAL MUCURSES_BITS( 1UL, ATTRS_SHIFT + 10 )
#define WA_LEFT MUCURSES_BITS( 1UL, ATTRS_SHIFT + 11 )
#define WA_RIGHT MUCURSES_BITS( 1UL, ATTRS_SHIFT + 12 )
#define WA_LOW MUCURSES_BITS( 1UL, ATTRS_SHIFT + 13 )
#define WA_TOP MUCURSES_BITS( 1UL, ATTRS_SHIFT + 14 )
#define A_DEFAULT WA_DEFAULT
#define A_ALTCHARSET WA_ALTCHARSET
#define A_BLINK WA_BLINK
#define A_BOLD WA_BOLD
#define A_DIM WA_DIM
#define A_INVIS WA_INVIS
#define A_PROTECT WA_PROTECT
#define A_REVERSE WA_REVERSE
#define A_STANDOUT WA_STANDOUT
#define A_UNDERLINE WA_UNDERLINE
#define A_ATTRIBUTES ( MUCURSES_BITS( 1UL, ATTRS_SHIFT ) - 1UL )
#define A_CHARTEXT ( MUCURSES_BITS( 1UL, 0 ) - 1UL )
#define A_COLOUR MUCURSES_BITS( ( 1UL << 8 ) - 1UL, CPAIR_SHIFT )
#define A_ATTRIBUTES ( 0xffff << ATTRS_SHIFT )
#define A_CHARTEXT ( 0xff )
#define A_COLOUR ( 0xff << CPAIR_SHIFT )
#define A_COLOR A_COLOUR
#define COLOUR_PAIR(n) ( (n) << CPAIR_SHIFT )
#define COLOR_PAIR(n) COLOUR_PAIR(n)
#define PAIR_NUMBER(attrs) ( ( (attrs) & A_COLOUR ) >> CPAIR_SHIFT )
#define COLOUR_PAIRS 4 /* Arbitrary limit */
#define COLOR_PAIRS COLOUR_PAIRS
#define ACS_ULCORNER '+'
#define ACS_LLCORNER '+'
#define ACS_URCORNER '+'
@ -152,6 +172,7 @@ extern unsigned int _COLOUR_PAIRS;
#define COLOUR_MAGENTA 5
#define COLOUR_CYAN 6
#define COLOUR_WHITE 7
#define COLOURS 7
#define COLOUR_FG 30
#define COLOUR_BG 40
@ -166,6 +187,7 @@ extern unsigned int _COLOUR_PAIRS;
#define COLOR_MAGENTA COLOUR_MAGENTA
#define COLOR_YELLOW COLOUR_YELLOW
#define COLOR_WHITE COLOUR_WHITE
#define COLORS COLOURS
/*
* KEY code constants
@ -281,7 +303,7 @@ extern int beep ( void );
/*extern int border ( chtype, chtype, chtype, chtype, chtype, chtype, chtype,
chtype );*/
extern int box ( WINDOW *, chtype, chtype );
extern bool can_change_colour ( void );
//extern bool can_change_colour ( void );
#define can_change_color() can_change_colour()
extern int cbreak ( void );
//extern int clrtobot ( void );
@ -289,7 +311,7 @@ extern int cbreak ( void );
extern int colour_content ( short, short *, short *, short * );
#define color_content( c, r, g, b ) colour_content( (c), (r), (g), (b) )
//extern int colour_set ( short, void * );
//#define color_set( cpno, opts ) colour_set( (cpno), (opts) )
#define color_set( cpno, opts ) colour_set( (cpno), (opts) )
extern int copywin ( const WINDOW *, WINDOW *, int, int, int,
int, int, int, int );
extern int curs_set ( int );
@ -316,7 +338,7 @@ extern chtype getbkgd ( WINDOW * );
//extern int getnstr ( char *, int );
//extern int getstr ( char * );
extern int halfdelay ( int );
extern bool has_colors ( void );
//extern bool has_colors ( void );
extern bool has_ic ( void );
extern bool has_il ( void );
//extern int hline ( chtype, int );
@ -405,7 +427,6 @@ extern int notimeout ( WINDOW *, bool );
extern int overlay ( const WINDOW *, WINDOW * );
extern int overwrite ( const WINDOW *, WINDOW * );
extern int pair_content ( short, short *, short * );
extern int PAIR_NUMBER ( int );
//extern int pechochar ( WINDOW *, chtype );
//extern int pnoutrefresh ( WINDOW *, int, int, int, int, int, int );
//extern int prefresh ( WINDOW *, int, int, int, int, int, int );
@ -445,7 +466,7 @@ extern int slk_set ( int, const char *, int );
extern int slk_touch ( void );
extern int standend ( void );
extern int standout ( void );
extern int start_colour ( void );
//extern int start_colour ( void );
#define start_color() start_colour()
//extern WINDOW *subpad ( WINDOW *, int, int, int, int );
extern WINDOW *subwin ( WINDOW *, int, int, int, int );
@ -492,7 +513,7 @@ extern int wborder ( WINDOW *, chtype, chtype, chtype, chtype, chtype, chtype,
extern int wclrtobot ( WINDOW * );
extern int wclrtoeol ( WINDOW * );
extern void wcursyncup ( WINDOW * );
//extern int wcolor_set ( WINDOW *, short, void * );
extern int wcolour_set ( WINDOW *, short, void * );
#define wcolor_set(w,s,v) wcolour_set((w),(s),(v))
extern int wdelch ( WINDOW * );
extern int wdeleteln ( WINDOW * );
@ -574,6 +595,10 @@ static inline int border ( chtype ls, chtype rs, chtype ts, chtype bs,
return wborder ( stdscr, ls, rs, ts, bs, tl, tr, bl, br );
}
static inline bool can_change_colour ( void ) {
return FALSE;
}
static inline int clrtobot ( void ) {
return wclrtobot( stdscr );
}
@ -582,6 +607,10 @@ static inline int clrtoeol ( void ) {
return wclrtoeol( stdscr );
}
static inline int colour_set ( short colour_pair_number, void *opts ) {
return wcolour_set ( stdscr, colour_pair_number, opts );
}
static inline int delch ( void ) {
return wdelch ( stdscr );
}
@ -606,6 +635,10 @@ static inline int getstr ( char *str ) {
return wgetnstr ( stdscr, str, -1 );
}
static inline bool has_colors ( void ) {
return TRUE;
}
static inline int hline ( chtype ch, int n ) {
return whline ( stdscr, ch, n );
}
@ -745,6 +778,10 @@ static inline int slk_refresh ( void ) {
#define standend() wstandend( stdscr )
#define standout() wstandout( stdscr )
static inline int start_colour ( void ) {
return OK;
}
static inline int vline ( chtype ch, int n ) {
return wvline ( stdscr, ch, n );
}