UI handling out of main loop

This commit is contained in:
Gwenhael Le Moine 2024-09-18 16:09:59 +02:00
parent 2e6c2b826b
commit 7c2753e49a
No known key found for this signature in database
GPG key ID: FDFE3669426707A7

View file

@ -1,9 +1,34 @@
#include <signal.h>
#include <stdbool.h>
#include <unistd.h>
#include <stdio.h>
#include <stdlib.h>
#include <time.h>
#include <sys/time.h>
#include "emulator.h"
#include "ui.h"
#define SPEED_HZ_UI 64
void signal_handler( int sig )
{
switch ( sig ) {
/* case SIGINT: /\* Ctrl-C *\/ */
/* enter_debugger |= USER_INTERRUPT; */
/* break; */
case SIGALRM:
ui_get_event();
ui_update_display();
break;
case SIGPIPE:
ui_stop();
exit_emulator();
exit( 0 );
default:
break;
}
}
int main( int argc, char** argv )
{
config_t* config = config_init( argc, argv );
@ -14,11 +39,33 @@ int main( int argc, char** argv )
setup_ui( config );
ui_start( config );
while ( true ) {
ui_get_event();
ui_update_display();
sigset_t set;
struct sigaction sa;
sigemptyset( &set );
sigaddset( &set, SIGALRM );
sa.sa_handler = signal_handler;
sa.sa_mask = set;
#ifdef SA_RESTART
sa.sa_flags = SA_RESTART;
#endif
sigaction( SIGALRM, &sa, ( struct sigaction* )0 );
usleep( 1000000 / 64 );
/************************************/
/* set the real time interval timer */
/************************************/
/*
Every <interval>µs setitimer will trigger a SIGALRM
which will getUI events and refresh UI in signal_handler
*/
struct itimerval it;
it.it_interval.tv_sec = 0;
it.it_interval.tv_usec = 1000000 / SPEED_HZ_UI;
it.it_value.tv_sec = 0;
it.it_value.tv_usec = 1000000 / SPEED_HZ_UI;
setitimer( ITIMER_REAL, &it, ( struct itimerval* )0 );
while ( true ) {
// fprintf( stderr, "." );
}
/* Never reached */