Add LibTemple/Input functions

This commit is contained in:
Alec Murphy 2025-06-10 13:34:00 -04:00
parent e6cff10e60
commit cc4f8dfa51
5 changed files with 51 additions and 2 deletions

12
System/LibTemple/Input.HC Normal file
View file

@ -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
}

View file

@ -5,6 +5,7 @@
#include "FFI/ELF64";
// LibTemple support files
#include "LibTemple/Input";
#include "LibTemple/OS";
#include "MuJS";

1
src/libtemple/input.h Normal file
View file

@ -0,0 +1 @@
uint64_t input_key_down(uint64_t sc);

View file

@ -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; }

View file

@ -1,6 +1,7 @@
#include <stdbool.h>
#include <inttypes.h>
#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()