Files
cell-sdl3/touch.c
2025-12-11 10:39:24 -06:00

74 lines
2.3 KiB
C

#include "cell.h"
#include <SDL3/SDL.h>
// SDL_GetTouchDevices() -> array of touch device IDs
JSC_CCALL(touch_get_devices,
int count = 0;
SDL_TouchID *devices = SDL_GetTouchDevices(&count);
if (!devices) return JS_NewArray(js);
JSValue arr = JS_NewArray(js);
for (int i = 0; i < count; i++) {
JS_SetPropertyUint32(js, arr, i, JS_NewInt64(js, devices[i]));
}
SDL_free(devices);
return arr;
)
// SDL_GetTouchDeviceName(touchID) -> string
JSC_CCALL(touch_get_device_name,
int64_t touchID;
JS_ToInt64(js, &touchID, argv[0]);
const char *name = SDL_GetTouchDeviceName((SDL_TouchID)touchID);
return name ? JS_NewString(js, name) : JS_NULL;
)
// SDL_GetTouchDeviceType(touchID) -> string
static const char *touch_device_type_to_string(SDL_TouchDeviceType type) {
switch (type) {
case SDL_TOUCH_DEVICE_DIRECT: return "direct";
case SDL_TOUCH_DEVICE_INDIRECT_ABSOLUTE: return "indirect_absolute";
case SDL_TOUCH_DEVICE_INDIRECT_RELATIVE: return "indirect_relative";
default: return "invalid";
}
}
JSC_CCALL(touch_get_device_type,
int64_t touchID;
JS_ToInt64(js, &touchID, argv[0]);
SDL_TouchDeviceType type = SDL_GetTouchDeviceType((SDL_TouchID)touchID);
return JS_NewString(js, touch_device_type_to_string(type));
)
// SDL_GetTouchFingers(touchID) -> array of finger objects
JSC_CCALL(touch_get_fingers,
int64_t touchID;
JS_ToInt64(js, &touchID, argv[0]);
int count = 0;
SDL_Finger **fingers = SDL_GetTouchFingers((SDL_TouchID)touchID, &count);
if (!fingers) return JS_NewArray(js);
JSValue arr = JS_NewArray(js);
for (int i = 0; i < count; i++) {
SDL_Finger *f = fingers[i];
JSValue finger = JS_NewObject(js);
JS_SetPropertyStr(js, finger, "id", JS_NewInt64(js, f->id));
JS_SetPropertyStr(js, finger, "x", JS_NewFloat64(js, f->x));
JS_SetPropertyStr(js, finger, "y", JS_NewFloat64(js, f->y));
JS_SetPropertyStr(js, finger, "pressure", JS_NewFloat64(js, f->pressure));
JS_SetPropertyUint32(js, arr, i, finger);
}
SDL_free(fingers);
return arr;
)
static const JSCFunctionListEntry js_touch_funcs[] = {
MIST_FUNC_DEF(touch, get_devices, 0),
MIST_FUNC_DEF(touch, get_device_name, 1),
MIST_FUNC_DEF(touch, get_device_type, 1),
MIST_FUNC_DEF(touch, get_fingers, 1),
};
CELL_USE_FUNCS(js_touch_funcs)