diff --git a/src/uxncli.c b/src/uxncli.c index 1dd30e4..a828809 100644 --- a/src/uxncli.c +++ b/src/uxncli.c @@ -104,7 +104,7 @@ start(Uxn *u) { if(!evaluxn(u, PAGE_PROGRAM)) return error("Reset", "Failed"); - while(mempeek16(devconsole->dat, 0)) + if(mempeek16(devconsole->dat, 0)) while(read(0, &devconsole->dat[0x2], 1) > 0) evaluxn(u, mempeek16(devconsole->dat, 0)); return 1; diff --git a/src/uxnemu.c b/src/uxnemu.c index f4ad95e..2f9be40 100644 --- a/src/uxnemu.c +++ b/src/uxnemu.c @@ -1,7 +1,6 @@ #include #include #include -#include #include "uxn.h" #pragma GCC diagnostic push @@ -30,6 +29,7 @@ static SDL_Rect gRect; static Ppu ppu; static Apu apu[POLYPHONY]; static Device *devscreen, *devmouse, *devctrl, *devaudio0, *devconsole; +static Uint32 stdin_event; #define PAD 16 @@ -329,6 +329,17 @@ nil_talk(Device *d, Uint8 b0, Uint8 w) #pragma mark - Generics +static int +in_reader(void *p) +{ + SDL_Event event; + event.type = stdin_event; + while(read(0, &event.cbutton.button, 1) > 0) + SDL_PushEvent(&event); + return 0; + (void)p; +} + static int start(Uxn *u) { @@ -367,10 +378,13 @@ start(Uxn *u) if(event.window.event == SDL_WINDOWEVENT_EXPOSED) redraw(u); break; + default: + if(event.type == stdin_event) { + devconsole->dat[0x2] = event.cbutton.button; + evaluxn(u, mempeek16(devconsole->dat, 0)); + } } } - while(read(0, &devconsole->dat[0x2], 1) > 0) - evaluxn(u, mempeek16(devconsole->dat, 0)); evaluxn(u, mempeek16(devscreen->dat, 0)); if(reqdraw) redraw(u); @@ -388,8 +402,8 @@ main(int argc, char **argv) Uxn u; zoom = 2; - /* set stdin nonblocking */ - fcntl(0, F_SETFL, fcntl(0, F_GETFL, 0) | O_NONBLOCK); + stdin_event = SDL_RegisterEvents(1); + SDL_CreateThread(in_reader, "stdin", NULL); if(argc < 2) return error("Input", "Missing");