From cc4f8dfa516ca65dd610fbcfd6edc1dab201224f Mon Sep 17 00:00:00 2001 From: Alec Murphy Date: Tue, 10 Jun 2025 13:34:00 -0400 Subject: [PATCH] Add LibTemple/Input functions --- System/LibTemple/Input.HC | 12 ++++++++++++ System/MakeSystem.HC | 1 + src/libtemple/input.h | 1 + src/libtemple/libtemple.c | 2 ++ src/mujs/jslibtemple.c | 37 +++++++++++++++++++++++++++++++++++-- 5 files changed, 51 insertions(+), 2 deletions(-) create mode 100644 System/LibTemple/Input.HC create mode 100644 src/libtemple/input.h diff --git a/System/LibTemple/Input.HC b/System/LibTemple/Input.HC new file mode 100644 index 0000000..43f1083 --- /dev/null +++ b/System/LibTemple/Input.HC @@ -0,0 +1,12 @@ +Bool @input_key_down(I64 sc) +{ + return Bt(kbd.down_bitmap, sc); +} + +U0 input_key_down() +{ + PUSH_SYSV_REGS + GET_SYSV_ARGS + @input_key_down(p0); + POP_SYSV_REGS +} diff --git a/System/MakeSystem.HC b/System/MakeSystem.HC index fbb1cbd..63860be 100644 --- a/System/MakeSystem.HC +++ b/System/MakeSystem.HC @@ -5,6 +5,7 @@ #include "FFI/ELF64"; // LibTemple support files +#include "LibTemple/Input"; #include "LibTemple/OS"; #include "MuJS"; diff --git a/src/libtemple/input.h b/src/libtemple/input.h new file mode 100644 index 0000000..eea94ec --- /dev/null +++ b/src/libtemple/input.h @@ -0,0 +1 @@ +uint64_t input_key_down(uint64_t sc); diff --git a/src/libtemple/libtemple.c b/src/libtemple/libtemple.c index 5dad194..5b51dd1 100644 --- a/src/libtemple/libtemple.c +++ b/src/libtemple/libtemple.c @@ -1,3 +1,5 @@ +unsigned long input_key_down(unsigned long sc) { return 0; } + unsigned long os_call_ext_str(char const* func) { return 0; } unsigned long os_call_ext_str_1(char const* func, unsigned long arg1) { return 0; } diff --git a/src/mujs/jslibtemple.c b/src/mujs/jslibtemple.c index 6036612..aa5251c 100644 --- a/src/mujs/jslibtemple.c +++ b/src/mujs/jslibtemple.c @@ -1,6 +1,7 @@ #include #include +#include "../libtemple/input.h" #include "../libtemple/os.h" #define value_or_number(v, x) js_isundefined(J, x) ? v : js_tonumber(J, x) @@ -168,6 +169,22 @@ uint64_t get_glbl_var_addr(char* name) return addr ? *(uint64_t*)(addr + 0x78) : 0; } +void Input_get_char(js_State *J) +{ + js_pushnumber(J, os_call_ext_str_3("GetChar", 0, 0, 0)); +} + +void Input_get_string(js_State *J) +{ + char* res = (char*)os_call_ext_str_3("GetStr", js_isundefined(J, 1) ? 0 : (uint64_t)js_tostring(J, 1), 0, 0); + if (!res) { + js_pushundefined(J); + return; + } + js_pushstring(J, res); + free(res); +} + void Input_mouse_x(js_State *J) { uint64_t* res = (uint64_t*)get_glbl_var_addr("ms"); @@ -192,16 +209,31 @@ void Input_mouse_rb(js_State *J) js_pushboolean(J, res[0xa1]); // offset(CMsStateGlbls.rb) } +void Input_key_down(js_State *J) +{ + js_pushboolean(J, input_key_down(js_tonumber(J, 1))); +} + +void Input_press_a_key(js_State *J) +{ + js_pushnumber(J, os_call_ext_str("PressAKey")); +} + void jsLT_initinput(js_State *J) { js_pushobject(J, jsV_newobject(J, -1, J->Object_prototype)); { + jsB_propf(J, "Input.get_char", Input_get_char, 0); + jsB_propf(J, "Input.get_string", Input_get_string, 1); jsB_propf(J, "Input.mouse_x", Input_mouse_x, 0); jsB_propf(J, "Input.mouse_y", Input_mouse_y, 0); jsB_propf(J, "Input.mouse_lb", Input_mouse_lb, 0); jsB_propf(J, "Input.mouse_rb", Input_mouse_rb, 0); + jsB_propf(J, "Input.press_a_key", Input_press_a_key, 0); + jsB_propf(J, "Input.key_down", Input_key_down, 1); } js_defglobal(J, "Input", JS_DONTENUM); + js_dostring(J, "Input.key_up = function(sc) { return !Input.key_down(sc) }"); } /** OS **/ @@ -261,8 +293,9 @@ void OS_read_file_as_string(js_State *J) return; } const char* filename = js_tostring(J, 1); - long file_as_string = os_call_ext_str_3("FileRead", (uint64_t)filename, 0, 0); - js_pushstring(J, file_as_string ? (char*)file_as_string : ""); + char* file_as_string = (char*)os_call_ext_str_3("FileRead", (uint64_t)filename, 0, 0); + js_pushstring(J, file_as_string ? file_as_string : ""); + free(file_as_string); } // U0 Reboot()