Async commands in SDL port, better handling of ^C and ^D

This commit is contained in:
Lior Halphon 2016-07-18 13:10:19 +03:00
parent aa6438fa06
commit b30822fd0b
2 changed files with 32 additions and 2 deletions

View File

@ -7,6 +7,7 @@
#include <errno.h> #include <errno.h>
#include <stdarg.h> #include <stdarg.h>
#include <sys/time.h> #include <sys/time.h>
#include <sys/select.h>
#include "gb.h" #include "gb.h"
#include "memory.h" #include "memory.h"
#include "timing.h" #include "timing.h"
@ -52,8 +53,12 @@ static char *default_input_callback(GB_gameboy_t *gb)
{ {
char *expression = NULL; char *expression = NULL;
size_t size = 0; size_t size = 0;
printf(">");
getline(&expression, &size, stdin); if (getline(&expression, &size, stdin) == -1) {
/* The user doesn't have STDIN or used ^D. We make sure the program keeps running. */
GB_set_async_input_callback(gb, NULL); /* Disable async input */
return strdup("c");
}
if (!expression) { if (!expression) {
return strdup(""); return strdup("");
@ -66,6 +71,22 @@ static char *default_input_callback(GB_gameboy_t *gb)
return expression; return expression;
} }
static char *default_async_input_callback(GB_gameboy_t *gb)
{
fd_set set;
FD_ZERO(&set);
FD_SET(STDIN_FILENO, &set);
struct timeval time = {0,};
if (select(1, &set, NULL, NULL, &time) == 1) {
if (feof(stdin)) {
GB_set_async_input_callback(gb, NULL); /* Disable async input */
return NULL;
}
return default_input_callback(gb);
}
return NULL;
}
void GB_init(GB_gameboy_t *gb) void GB_init(GB_gameboy_t *gb)
{ {
memset(gb, 0, sizeof(*gb)); memset(gb, 0, sizeof(*gb));
@ -88,6 +109,7 @@ void GB_init(GB_gameboy_t *gb)
gb->sprite_palletes_rgb[5] = gb->sprite_palletes_rgb[1] = gb->background_palletes_rgb[1] = 0xAAAAAAAA; gb->sprite_palletes_rgb[5] = gb->sprite_palletes_rgb[1] = gb->background_palletes_rgb[1] = 0xAAAAAAAA;
gb->sprite_palletes_rgb[6] = gb->sprite_palletes_rgb[2] = gb->background_palletes_rgb[2] = 0x55555555; gb->sprite_palletes_rgb[6] = gb->sprite_palletes_rgb[2] = gb->background_palletes_rgb[2] = 0x55555555;
gb->input_callback = default_input_callback; gb->input_callback = default_input_callback;
gb->async_input_callback = default_async_input_callback;
gb->cartridge_type = &GB_cart_defs[0]; // Default cartridge type gb->cartridge_type = &GB_cart_defs[0]; // Default cartridge type
gb->io_registers[GB_IO_JOYP] = 0xF; gb->io_registers[GB_IO_JOYP] = 0xF;
@ -112,6 +134,7 @@ void GB_init_cgb(GB_gameboy_t *gb)
gb->last_vblank = clock(); gb->last_vblank = clock();
gb->cgb_ram_bank = 1; gb->cgb_ram_bank = 1;
gb->input_callback = default_input_callback; gb->input_callback = default_input_callback;
gb->async_input_callback = default_async_input_callback;
gb->cartridge_type = &GB_cart_defs[0]; // Default cartridge type gb->cartridge_type = &GB_cart_defs[0]; // Default cartridge type
gb->io_registers[GB_IO_JOYP] = 0xF; gb->io_registers[GB_IO_JOYP] = 0xF;
@ -427,6 +450,9 @@ void GB_set_log_callback(GB_gameboy_t *gb, GB_log_callback_t callback)
void GB_set_input_callback(GB_gameboy_t *gb, GB_input_callback_t callback) void GB_set_input_callback(GB_gameboy_t *gb, GB_input_callback_t callback)
{ {
if (gb->input_callback == default_input_callback) {
gb->async_input_callback = NULL;
}
gb->input_callback = callback; gb->input_callback = callback;
} }

View File

@ -134,6 +134,10 @@ GB_gameboy_t gb;
static void debugger_interrupt(int ignore) static void debugger_interrupt(int ignore)
{ {
/* ^C twice to exit */
if (gb.debug_stopped) {
exit(0);
}
gb.debug_stopped = true; gb.debug_stopped = true;
} }