144 lines
4.2 KiB
C
144 lines
4.2 KiB
C
#include "cell.h"
|
|
#include <SDL3/SDL.h>
|
|
|
|
// SDL_HasKeyboard() -> bool
|
|
JSC_CCALL(keyboard_has,
|
|
return JS_NewBool(js, SDL_HasKeyboard());
|
|
)
|
|
|
|
// SDL_GetKeyboards() -> array of keyboard IDs
|
|
JSC_CCALL(keyboard_get_keyboards,
|
|
int count = 0;
|
|
SDL_KeyboardID *keyboards = SDL_GetKeyboards(&count);
|
|
if (!keyboards) return JS_NewArray(js);
|
|
|
|
JSValue arr = JS_NewArray(js);
|
|
for (int i = 0; i < count; i++) {
|
|
JS_SetPropertyUint32(js, arr, i, JS_NewUint32(js, keyboards[i]));
|
|
}
|
|
SDL_free(keyboards);
|
|
return arr;
|
|
)
|
|
|
|
// SDL_GetKeyboardNameForID(id) -> string
|
|
JSC_SCALL(keyboard_get_name,
|
|
uint32_t id;
|
|
JS_ToUint32(js, &id, argv[0]);
|
|
const char *name = SDL_GetKeyboardNameForID(id);
|
|
return name ? JS_NewString(js, name) : JS_NewString(js, "");
|
|
)
|
|
|
|
// SDL_GetKeyboardState() -> object with key states
|
|
JSC_CCALL(keyboard_get_state,
|
|
int numkeys = 0;
|
|
const bool *state = SDL_GetKeyboardState(&numkeys);
|
|
if (!state) return JS_NULL;
|
|
|
|
JSValue arr = JS_NewArray(js);
|
|
for (int i = 0; i < numkeys; i++) {
|
|
JS_SetPropertyUint32(js, arr, i, JS_NewBool(js, state[i]));
|
|
}
|
|
return arr;
|
|
)
|
|
|
|
// SDL_ResetKeyboard()
|
|
JSC_CCALL(keyboard_reset,
|
|
SDL_ResetKeyboard();
|
|
return JS_NULL;
|
|
)
|
|
|
|
// SDL_GetModState() -> number (bitmask)
|
|
JSC_CCALL(keyboard_get_mod_state,
|
|
return JS_NewUint32(js, SDL_GetModState());
|
|
)
|
|
|
|
// SDL_SetModState(modstate)
|
|
JSC_CCALL(keyboard_set_mod_state,
|
|
uint32_t modstate;
|
|
JS_ToUint32(js, &modstate, argv[0]);
|
|
SDL_SetModState((SDL_Keymod)modstate);
|
|
return JS_NULL;
|
|
)
|
|
|
|
// SDL_GetKeyFromScancode(scancode, modstate, key_event) -> keycode
|
|
JSC_CCALL(keyboard_get_key_from_scancode,
|
|
int scancode;
|
|
uint32_t modstate = 0;
|
|
int key_event = 0;
|
|
JS_ToInt32(js, &scancode, argv[0]);
|
|
if (argc > 1) JS_ToUint32(js, &modstate, argv[1]);
|
|
if (argc > 2) key_event = JS_ToBool(js, argv[2]);
|
|
return JS_NewUint32(js, SDL_GetKeyFromScancode((SDL_Scancode)scancode, (SDL_Keymod)modstate, key_event));
|
|
)
|
|
|
|
// SDL_GetScancodeFromKey(key) -> scancode
|
|
JSC_CCALL(keyboard_get_scancode_from_key,
|
|
uint32_t key;
|
|
JS_ToUint32(js, &key, argv[0]);
|
|
SDL_Keymod modstate;
|
|
SDL_Scancode scancode = SDL_GetScancodeFromKey((SDL_Keycode)key, &modstate);
|
|
|
|
JSValue result = JS_NewObject(js);
|
|
JS_SetPropertyStr(js, result, "scancode", JS_NewInt32(js, scancode));
|
|
JS_SetPropertyStr(js, result, "modstate", JS_NewUint32(js, modstate));
|
|
return result;
|
|
)
|
|
|
|
// SDL_GetScancodeName(scancode) -> string
|
|
JSC_CCALL(keyboard_get_scancode_name,
|
|
int scancode;
|
|
JS_ToInt32(js, &scancode, argv[0]);
|
|
const char *name = SDL_GetScancodeName((SDL_Scancode)scancode);
|
|
return JS_NewString(js, name ? name : "");
|
|
)
|
|
|
|
// SDL_GetScancodeFromName(name) -> scancode
|
|
JSC_SCALL(keyboard_get_scancode_from_name,
|
|
const char *name = JS_ToCString(js, argv[0]);
|
|
if (!name) return JS_EXCEPTION;
|
|
SDL_Scancode scancode = SDL_GetScancodeFromName(name);
|
|
JS_FreeCString(js, name);
|
|
return JS_NewInt32(js, scancode);
|
|
)
|
|
|
|
// SDL_GetKeyName(key) -> string
|
|
JSC_CCALL(keyboard_get_key_name,
|
|
uint32_t key;
|
|
JS_ToUint32(js, &key, argv[0]);
|
|
const char *name = SDL_GetKeyName((SDL_Keycode)key);
|
|
return JS_NewString(js, name ? name : "");
|
|
)
|
|
|
|
// SDL_GetKeyFromName(name) -> keycode
|
|
JSC_SCALL(keyboard_get_key_from_name,
|
|
const char *name = JS_ToCString(js, argv[0]);
|
|
if (!name) return JS_EXCEPTION;
|
|
SDL_Keycode key = SDL_GetKeyFromName(name);
|
|
JS_FreeCString(js, name);
|
|
return JS_NewUint32(js, key);
|
|
)
|
|
|
|
// SDL_HasScreenKeyboardSupport() -> bool
|
|
JSC_CCALL(keyboard_has_screen_support,
|
|
return JS_NewBool(js, SDL_HasScreenKeyboardSupport());
|
|
)
|
|
|
|
static const JSCFunctionListEntry js_keyboard_funcs[] = {
|
|
MIST_FUNC_DEF(keyboard, has, 0),
|
|
MIST_FUNC_DEF(keyboard, get_keyboards, 0),
|
|
MIST_FUNC_DEF(keyboard, get_name, 1),
|
|
MIST_FUNC_DEF(keyboard, get_state, 0),
|
|
MIST_FUNC_DEF(keyboard, reset, 0),
|
|
MIST_FUNC_DEF(keyboard, get_mod_state, 0),
|
|
MIST_FUNC_DEF(keyboard, set_mod_state, 1),
|
|
MIST_FUNC_DEF(keyboard, get_key_from_scancode, 3),
|
|
MIST_FUNC_DEF(keyboard, get_scancode_from_key, 1),
|
|
MIST_FUNC_DEF(keyboard, get_scancode_name, 1),
|
|
MIST_FUNC_DEF(keyboard, get_scancode_from_name, 1),
|
|
MIST_FUNC_DEF(keyboard, get_key_name, 1),
|
|
MIST_FUNC_DEF(keyboard, get_key_from_name, 1),
|
|
MIST_FUNC_DEF(keyboard, has_screen_support, 0),
|
|
};
|
|
|
|
CELL_USE_FUNCS(js_keyboard_funcs)
|