planned a way to do glfw callbacks
authorBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 7 Dec 2021 04:26:08 +0000 (22:26 -0600)
committerBrendan Hansen <brendan.f.hansen@gmail.com>
Tue, 7 Dec 2021 04:26:08 +0000 (22:26 -0600)
include/onyx_library.h
modules/glfw3/module.onyx
modules/glfw3/onyx_glfw3.c
modules/glfw3/onyx_glfw3.so
src/wasm_runtime.c

index c2e3027fb0b00703247d444d71b88523cd794de9..ded009a348c3be7b28a93025ec0746a5cabed2a3 100644 (file)
@@ -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;
index 1eed1df010f5f249a032ca244244be4b5beed125..f7e98b0a94427d1c3b07be1254195e746f40178f 100644 (file)
@@ -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
index 6e3145f18af97e409c0cdecef057928ce7fb1bbf..d160b1b66f4f61c3de2c3e101b42e07a3beac687 100644 (file)
@@ -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
index 147878724327831d26c7ddd917ae834c9d9429aa..d8c8fb0fc0609775072cb74d8ab351632e9b27d2 100755 (executable)
Binary files a/modules/glfw3/onyx_glfw3.so and b/modules/glfw3/onyx_glfw3.so differ
index 9a4513a2fcd04af94838dff4f306eb47d717a7e4..ae94a45a0d7c721a93201190425bada1f6b5e0ef 100644 (file)
@@ -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);