From: Brendan Hansen Date: Tue, 7 Dec 2021 04:26:08 +0000 (-0600) Subject: planned a way to do glfw callbacks X-Git-Url: https://git.brendanfh.com/?a=commitdiff_plain;h=df82fa4eca0c520052983c06dc20e2b14504935b;p=onyx.git planned a way to do glfw callbacks --- diff --git a/include/onyx_library.h b/include/onyx_library.h index c2e3027f..ded009a3 100644 --- a/include/onyx_library.h +++ b/include/onyx_library.h @@ -1,7 +1,10 @@ #include "wasm.h" +extern wasm_instance_t* wasm_instance; +extern wasm_module_t* wasm_module; extern wasm_memory_t* wasm_memory; +extern wasm_extern_t* wasm_extern_lookup_by_name(wasm_module_t* module, wasm_instance_t* instance, const char* name); typedef struct WasmValkindBuffer { unsigned int count; diff --git a/modules/glfw3/module.onyx b/modules/glfw3/module.onyx index 1eed1df0..f7e98b0a 100644 --- a/modules/glfw3/module.onyx +++ b/modules/glfw3/module.onyx @@ -56,7 +56,7 @@ GLFWgammaramp :: struct { glfwCreateStandardCursor :: (shape: i32) -> GLFWcursor_p --- glfwDestroyCursor :: (cursor: GLFWcursor_p) -> void --- glfwSetCursor :: (window: GLFWwindow_p, cursor: GLFWcursor_p) -> void --- - // glfwSetKeyCallback + glfwSetKeyCallback :: (window: GLFWwindow_p, export_name: str) -> void --- // glfwSetCharCallback // glfwSetCharModsCallback // glfwSetMouseButtonCallback diff --git a/modules/glfw3/onyx_glfw3.c b/modules/glfw3/onyx_glfw3.c index 6e3145f1..d160b1b6 100644 --- a/modules/glfw3/onyx_glfw3.c +++ b/modules/glfw3/onyx_glfw3.c @@ -231,9 +231,6 @@ ONYX_DEF(glfwSwapBuffers, (LONG), ()) { return NULL; } -// glfwGetInputMode :: (window: GLFWwindow_p, mode: i32) -> i32 --- -// glfwSetInputMode :: (window: GLFWwindow_p, mode, value: i32) -> void --- -// glfwRawMouseMotionSupported :: () -> i32 --- ONYX_DEF(glfwGetInputMode, (LONG, INT), (INT)) { GLFWwindow *window = (GLFWwindow *) params->data[0].of.i64; results->data[0] = WASM_I32_VAL(glfwGetInputMode(window, params->data[1].of.i32)); @@ -311,7 +308,28 @@ ONYX_DEF(glfwSetCursor, (LONG, LONG), ()) { return NULL; } -// // glfwSetKeyCallback +wasm_func_t* __key_callback_func; +static void __glfw_key_callback(GLFWwindow *window, int key, int scancode, int action, int mods) { + wasm_val_t args[] = { WASM_I64_VAL((unsigned long) window), WASM_I32_VAL(key), WASM_I32_VAL(scancode), WASM_I32_VAL(action), WASM_I32_VAL(mods) }; + wasm_val_vec_t args_array = WASM_ARRAY_VEC(args); + wasm_val_vec_t results; + + wasm_func_call(__key_callback_func, &args_array, &results); +} + +ONYX_DEF(glfwSetKeyCallback, (LONG, PTR, INT), ()) { + GLFWwindow *window = (GLFWwindow *) params->data[0].of.i64; + + char name[512]; + strncpy(name, ONYX_PTR(params->data[1].of.i32), params->data[2].of.i32); + name[params->data[2].of.i32] = '\0'; + + __key_callback_func = wasm_extern_as_func(wasm_extern_lookup_by_name(wasm_module, wasm_instance, name)); + + glfwSetKeyCallback(window, __glfw_key_callback); + return NULL; +} + // // glfwSetCharCallback // // glfwSetCharModsCallback // // glfwSetMouseButtonCallback @@ -504,10 +522,10 @@ ONYX_LIBRARY { ONYX_FUNC(glfwSetMonitorUserPointer) ONYX_FUNC(glfwGetMonitorUserPointer) + ONYX_FUNC(glfwSetKeyCallback) // // glfwGetKeyName :: (key, scancode: i32) -> cstr --- -// // glfwSetKeyCallback // // glfwSetCharCallback // // glfwSetCharModsCallback // // glfwSetMouseButtonCallback diff --git a/modules/glfw3/onyx_glfw3.so b/modules/glfw3/onyx_glfw3.so index 14787872..d8c8fb0f 100755 Binary files a/modules/glfw3/onyx_glfw3.so and b/modules/glfw3/onyx_glfw3.so differ diff --git a/src/wasm_runtime.c b/src/wasm_runtime.c index 9a4513a2..ae94a45a 100644 --- a/src/wasm_runtime.c +++ b/src/wasm_runtime.c @@ -22,7 +22,8 @@ static wasi_env_t* wasi_env; static wasm_engine_t* wasm_engine; static wasm_store_t* wasm_store; static wasm_extern_vec_t wasm_imports; -static wasm_module_t* wasm_module; +wasm_instance_t* wasm_instance; +wasm_module_t* wasm_module; wasm_memory_t* wasm_memory; b32 wasm_name_equals(const wasm_name_t* name1, const wasm_name_t* name2) { @@ -574,7 +575,6 @@ b32 onyx_run_wasm(bh_buffer wasm_bytes) { onyx_lookup_and_load_custom_libraries(wasm_bytes); - wasm_instance_t* instance = NULL; wasmer_features_t* features = NULL; wasm_trap_t* run_trap = NULL; @@ -774,10 +774,10 @@ b32 onyx_run_wasm(bh_buffer wasm_bytes) { wasm_trap_t* traps = NULL; - instance = wasm_instance_new(wasm_store, wasm_module, &wasm_imports, &traps); - if (!instance) goto error_handling; + wasm_instance = wasm_instance_new(wasm_store, wasm_module, &wasm_imports, &traps); + if (!wasm_instance) goto error_handling; - wasm_extern_t* start_extern = wasm_extern_lookup_by_name(wasm_module, instance, "_start"); + wasm_extern_t* start_extern = wasm_extern_lookup_by_name(wasm_module, wasm_instance, "_start"); wasm_func_t* start_func = wasm_extern_as_func(start_extern); wasm_val_vec_t args; @@ -807,7 +807,7 @@ error_handling: bh_printf("%b\n", buf, len); cleanup: - if (instance) wasm_instance_delete(instance); + if (wasm_instance) wasm_instance_delete(wasm_instance); if (wasm_module) wasm_module_delete(wasm_module); if (wasm_store) wasm_store_delete(wasm_store); if (wasm_engine) wasm_engine_delete(wasm_engine);