refactoring tui

This commit is contained in:
Gwenhael Le Moine 2023-09-27 11:11:38 +02:00
parent ec1d3bd4b9
commit a740c97db6
No known key found for this signature in database
GPG key ID: FDFE3669426707A7

View file

@ -111,6 +111,62 @@ static short lcd_pixels_buffer[ LCD_WIDTH ][ LCD_HEIGHT ];
/****************************/
/* functions implementation */
/****************************/
/* TODO: not specific to tui */
static inline void press_button( int b )
{
// Check not already pressed (may be important: avoids a useless do_kbd_int)
if ( buttons[ b ].pressed == 1 )
return;
buttons[ b ].pressed = 1;
int code = buttons[ b ].code;
if ( code == 0x8000 ) {
for ( int i = 0; i < 9; i++ )
saturn.keybuf.rows[ i ] |= 0x8000;
do_kbd_int();
} else {
int r = code >> 4;
int c = 1 << ( code & 0xf );
if ( ( saturn.keybuf.rows[ r ] & c ) == 0 ) {
if ( saturn.kbd_ien )
do_kbd_int();
if ( ( saturn.keybuf.rows[ r ] & c ) )
fprintf( stderr, "bug\n" );
saturn.keybuf.rows[ r ] |= c;
}
}
}
/* TODO: not specific to tui */
static inline void release_button( int b )
{
// Check not already released (not critical)
if ( buttons[ b ].pressed == 0 )
return;
buttons[ b ].pressed = 0;
int code = buttons[ b ].code;
if ( code == 0x8000 ) {
for ( int i = 0; i < 9; i++ )
saturn.keybuf.rows[ i ] &= ~0x8000;
} else {
int r = code >> 4;
int c = 1 << ( code & 0xf );
saturn.keybuf.rows[ r ] &= ~c;
}
}
/* TODO: not specific to tui */
static inline void release_all_buttons( void )
{
for ( int b = FIRST_BUTTON; b <= LAST_BUTTON; b++ )
if ( buttons[ b ].pressed )
release_button( b );
}
static inline void tranlate_nibble_into_lcd_pixels_buffer( int nibble, int initial_column, int row )
{
for ( int x = 0; x < 4; x++ ) {
@ -153,134 +209,7 @@ static inline void ncurses_draw_lcd_pixels_buffer( void )
refresh();
}
static inline void ncurses_init_ui( void )
{
setlocale( LC_ALL, "" );
initscr(); /* initialize the curses library */
keypad( stdscr, TRUE ); /* enable keyboard mapping */
nodelay( stdscr, TRUE );
curs_set( 0 );
cbreak(); /* take input chars one at a time, no wait for \n */
noecho();
nonl(); /* tell curses not to do NL->CR/NL on output */
if ( !mono && has_colors() ) {
start_color();
if ( gray ) {
init_color( LCD_COLOR_BG, 205, 205, 205 );
init_color( LCD_COLOR_FG, 20, 20, 20 );
} else {
init_color( LCD_COLOR_BG, 202, 221, 92 );
init_color( LCD_COLOR_FG, 0, 0, 128 );
}
init_pair( LCD_PIXEL_OFF, LCD_COLOR_BG, LCD_COLOR_BG );
init_pair( LCD_PIXEL_ON, LCD_COLOR_FG, LCD_COLOR_FG );
}
mvaddch( 0, 0, ACS_ULCORNER );
mvaddch( LCD_BOTTOM, 0, ACS_LLCORNER );
mvaddch( 0, LCD_RIGHT, ACS_URCORNER );
mvaddch( LCD_BOTTOM, LCD_RIGHT, ACS_LRCORNER );
mvhline( 0, 1, ACS_HLINE, LCD_RIGHT - 1 );
mvhline( LCD_BOTTOM, 1, ACS_HLINE, LCD_RIGHT - 1 );
mvvline( 1, 0, ACS_VLINE, LCD_BOTTOM - 1 );
mvvline( 1, LCD_RIGHT, ACS_VLINE, LCD_BOTTOM - 1 );
mvprintw( 0, 2, "[ | | | | | ]" ); /* annunciators */
mvprintw( LCD_BOTTOM, 2, "[ wire: %s ]-[ IR: %s ]", wire_name, ir_name );
}
static inline void draw_nibble( int col, int row, int val )
{
int c = ( col * 4 ), // c: start in pixels,
r = row; // r: start in pixels
if ( row <= display.lines )
c -= 2 * display.offset;
val &= 0x0f;
tranlate_nibble_into_lcd_pixels_buffer( val, c, r );
}
static inline void draw_row( long addr, int row )
{
int v;
int line_length = NIBBLES_PER_ROW;
if ( ( display.offset > 3 ) && ( row <= display.lines ) )
line_length += 2;
for ( int i = 0; i < line_length; i++ ) {
v = read_nibble( addr + i );
if ( v == lcd_nibbles_buffer[ row ][ i ] )
break;
lcd_nibbles_buffer[ row ][ i ] = v;
draw_nibble( i, row, v );
}
}
static void press_button( int b )
{
// Check not already pressed (may be important: avoids a useless do_kbd_int)
if ( buttons[ b ].pressed == 1 )
return;
buttons[ b ].pressed = 1;
int code = buttons[ b ].code;
if ( code == 0x8000 ) {
for ( int i = 0; i < 9; i++ )
saturn.keybuf.rows[ i ] |= 0x8000;
do_kbd_int();
} else {
int r = code >> 4;
int c = 1 << ( code & 0xf );
if ( ( saturn.keybuf.rows[ r ] & c ) == 0 ) {
if ( saturn.kbd_ien )
do_kbd_int();
if ( ( saturn.keybuf.rows[ r ] & c ) )
fprintf( stderr, "bug\n" );
saturn.keybuf.rows[ r ] |= c;
}
}
}
static void release_button( int b )
{
// Check not already released (not critical)
if ( buttons[ b ].pressed == 0 )
return;
buttons[ b ].pressed = 0;
int code = buttons[ b ].code;
if ( code == 0x8000 ) {
for ( int i = 0; i < 9; i++ )
saturn.keybuf.rows[ i ] &= ~0x8000;
} else {
int r = code >> 4;
int c = 1 << ( code & 0xf );
saturn.keybuf.rows[ r ] &= ~c;
}
}
static void release_all_buttons( void )
{
for ( int b = FIRST_BUTTON; b <= LAST_BUTTON; b++ )
if ( buttons[ b ].pressed )
release_button( b );
}
/**********/
/* public */
/**********/
int text_get_event( void )
static inline int ncurses_get_event( void )
{
int hpkey = -1;
uint32_t k;
@ -288,7 +217,7 @@ int text_get_event( void )
/* Start fresh and mark all keys as released */
release_all_buttons();
/* Iterate over all currently pressed keys and mark it as pressed */
/* Iterate over all currently pressed keys and mark them as pressed */
while ( ( k = getch() ) ) {
if ( k == ( uint32_t )ERR )
break;
@ -492,8 +421,87 @@ int text_get_event( void )
return 1;
}
static inline void ncurses_init_ui( void )
{
setlocale( LC_ALL, "" );
initscr(); /* initialize the curses library */
keypad( stdscr, TRUE ); /* enable keyboard mapping */
nodelay( stdscr, TRUE );
curs_set( 0 );
cbreak(); /* take input chars one at a time, no wait for \n */
noecho();
nonl(); /* tell curses not to do NL->CR/NL on output */
if ( !mono && has_colors() ) {
start_color();
if ( gray ) {
init_color( LCD_COLOR_BG, 205, 205, 205 );
init_color( LCD_COLOR_FG, 20, 20, 20 );
} else {
init_color( LCD_COLOR_BG, 202, 221, 92 );
init_color( LCD_COLOR_FG, 0, 0, 128 );
}
init_pair( LCD_PIXEL_OFF, LCD_COLOR_BG, LCD_COLOR_BG );
init_pair( LCD_PIXEL_ON, LCD_COLOR_FG, LCD_COLOR_FG );
}
mvaddch( 0, 0, ACS_ULCORNER );
mvaddch( LCD_BOTTOM, 0, ACS_LLCORNER );
mvaddch( 0, LCD_RIGHT, ACS_URCORNER );
mvaddch( LCD_BOTTOM, LCD_RIGHT, ACS_LRCORNER );
mvhline( 0, 1, ACS_HLINE, LCD_RIGHT - 1 );
mvhline( LCD_BOTTOM, 1, ACS_HLINE, LCD_RIGHT - 1 );
mvvline( 1, 0, ACS_VLINE, LCD_BOTTOM - 1 );
mvvline( 1, LCD_RIGHT, ACS_VLINE, LCD_BOTTOM - 1 );
mvprintw( 0, 2, "[ | | | | | ]" ); /* annunciators */
mvprintw( LCD_BOTTOM, 2, "[ wire: %s ]-[ IR: %s ]", wire_name, ir_name );
}
/* TODO: not specific to tui */
static inline void draw_nibble( int col, int row, int val )
{
int c = ( col * 4 ), // c: start in pixels,
r = row; // r: start in pixels
if ( row <= display.lines )
c -= 2 * display.offset;
val &= 0x0f;
tranlate_nibble_into_lcd_pixels_buffer( val, c, r );
}
/* TODO: not specific to tui */
static inline void draw_row( long addr, int row )
{
int v;
int line_length = NIBBLES_PER_ROW;
if ( ( display.offset > 3 ) && ( row <= display.lines ) )
line_length += 2;
for ( int i = 0; i < line_length; i++ ) {
v = read_nibble( addr + i );
if ( v == lcd_nibbles_buffer[ row ][ i ] )
break;
lcd_nibbles_buffer[ row ][ i ] = v;
draw_nibble( i, row, v );
}
}
/**********/
/* public */
/**********/
int text_get_event( void ) { return ncurses_get_event(); }
void text_adjust_contrast() { text_update_LCD(); }
/* TODO: not specific to tui */
void text_init_LCD( void )
{
init_display();
@ -502,6 +510,7 @@ void text_init_LCD( void )
memset( lcd_pixels_buffer, 0xf0, sizeof( lcd_pixels_buffer ) );
}
/* TODO: not specific to tui */
void text_update_LCD( void )
{
if ( display.on ) {
@ -542,6 +551,7 @@ void text_update_LCD( void )
void text_refresh_LCD( void ) {}
/* TODO: not specific to tui */
void text_disp_draw_nibble( word_20 addr, word_4 val )
{
long offset;
@ -572,6 +582,7 @@ void text_disp_draw_nibble( word_20 addr, word_4 val )
}
}
/* TODO: not specific to tui */
void text_menu_draw_nibble( word_20 addr, word_4 val )
{
long offset;