From e1afca53929880c6efaa6e1f6f3f524b154b8df0 Mon Sep 17 00:00:00 2001 From: Joe Osborn Date: Fri, 24 Feb 2023 13:06:04 -0800 Subject: [PATCH] add command and stdin_cmd features to emscripten RA. update libretro.js to show how commands could be sent over emscripten stdin. (#15017) Co-authored-by: Joseph C. Osborn --- Makefile.emscripten | 2 ++ pkg/emscripten/libretro/libretro.js | 31 +++++++++++++++++++++++++++-- 2 files changed, 31 insertions(+), 2 deletions(-) diff --git a/Makefile.emscripten b/Makefile.emscripten index 7a67c5b8ea..2dae31fbba 100644 --- a/Makefile.emscripten +++ b/Makefile.emscripten @@ -41,6 +41,8 @@ HAVE_STATIC_VIDEO_FILTERS = 1 HAVE_STATIC_AUDIO_FILTERS = 1 HAVE_STB_FONT = 1 HAVE_CONFIGFILE = 1 +HAVE_COMMAND = 1 +HAVE_STDIN_CMD = 1 HAVE_CHEATS = 1 HAVE_IBXM = 1 HAVE_CORE_INFO_CACHE = 1 diff --git a/pkg/emscripten/libretro/libretro.js b/pkg/emscripten/libretro/libretro.js index 5240f07eff..48a3e233c8 100644 --- a/pkg/emscripten/libretro/libretro.js +++ b/pkg/emscripten/libretro/libretro.js @@ -191,16 +191,43 @@ function uploadData(data,name) FS.unlink(name); } +var encoder = new TextEncoder(); +var message_queue = []; + +function retroArchSend(msg) { + var bytes = encoder.encode(msg+"\n"); + message_queue.push([bytes,0]); +} + var Module = { noInitialRun: true, arguments: ["-v", "--menu"], - preRun: [], + preRun: [ + function() { + function stdin() { + // Return ASCII code of character, or null if no input + while(message_queue.length > 0){ + var msg = message_queue[0][0]; + var index = message_queue[0][1]; + if(index >= msg.length) { + message_queue.shift(); + } else { + message_queue[0][1] = index+1; + // assumption: msg is a uint8array + return msg[index]; + } + } + return null; + } + FS.init(stdin); + } + ], postRun: [], onRuntimeInitialized: function() { appInitialized(); - }, + }, print: function(text) { console.log(text);