#include "cell.h" #include // Forward declaration for joystick function extern SDL_Joystick *js2SDL_Joystick(JSContext *js, JSValue val); // SDL_Haptic class void SDL_Haptic_free(JSRuntime *rt, SDL_Haptic *haptic) { if (haptic) SDL_CloseHaptic(haptic); } QJSCLASS(SDL_Haptic,) // SDL_GetHaptics() -> array of haptic IDs JSC_CCALL(haptic_get_haptics, int count = 0; SDL_HapticID *haptics = SDL_GetHaptics(&count); if (!haptics) return JS_NewArray(js); JSValue arr = JS_NewArray(js); for (int i = 0; i < count; i++) { JS_SetPropertyUint32(js, arr, i, JS_NewUint32(js, haptics[i])); } SDL_free(haptics); return arr; ) // SDL_GetHapticNameForID(id) -> string JSC_CCALL(haptic_get_name_for_id, uint32_t id; JS_ToUint32(js, &id, argv[0]); const char *name = SDL_GetHapticNameForID(id); return name ? JS_NewString(js, name) : JS_NULL; ) // SDL_OpenHaptic(id) -> Haptic object JSC_CCALL(haptic_open, uint32_t id; JS_ToUint32(js, &id, argv[0]); SDL_Haptic *haptic = SDL_OpenHaptic(id); if (!haptic) return JS_NULL; return SDL_Haptic2js(js, haptic); ) // SDL_OpenHapticFromJoystick(joystick) -> Haptic object JSC_CCALL(haptic_open_from_joystick, SDL_Joystick *joystick = js2SDL_Joystick(js, argv[0]); if (!joystick) return JS_NULL; SDL_Haptic *haptic = SDL_OpenHapticFromJoystick(joystick); if (!haptic) return JS_NULL; return SDL_Haptic2js(js, haptic); ) // SDL_OpenHapticFromMouse() -> Haptic object JSC_CCALL(haptic_open_from_mouse, SDL_Haptic *haptic = SDL_OpenHapticFromMouse(); if (!haptic) return JS_NULL; return SDL_Haptic2js(js, haptic); ) // SDL_IsMouseHaptic() -> bool JSC_CCALL(haptic_is_mouse_haptic, return JS_NewBool(js, SDL_IsMouseHaptic()); ) // Haptic instance methods JSC_CCALL(haptic_get_id, SDL_Haptic *haptic = js2SDL_Haptic(js, self); return JS_NewUint32(js, SDL_GetHapticID(haptic)); ) JSC_CCALL(haptic_get_name, SDL_Haptic *haptic = js2SDL_Haptic(js, self); const char *name = SDL_GetHapticName(haptic); return name ? JS_NewString(js, name) : JS_NULL; ) JSC_CCALL(haptic_get_features, SDL_Haptic *haptic = js2SDL_Haptic(js, self); return JS_NewUint32(js, SDL_GetHapticFeatures(haptic)); ) JSC_CCALL(haptic_get_num_axes, SDL_Haptic *haptic = js2SDL_Haptic(js, self); return JS_NewInt32(js, SDL_GetNumHapticAxes(haptic)); ) JSC_CCALL(haptic_get_max_effects, SDL_Haptic *haptic = js2SDL_Haptic(js, self); return JS_NewInt32(js, SDL_GetMaxHapticEffects(haptic)); ) JSC_CCALL(haptic_get_max_effects_playing, SDL_Haptic *haptic = js2SDL_Haptic(js, self); return JS_NewInt32(js, SDL_GetMaxHapticEffectsPlaying(haptic)); ) JSC_CCALL(haptic_rumble_supported, SDL_Haptic *haptic = js2SDL_Haptic(js, self); return JS_NewBool(js, SDL_HapticRumbleSupported(haptic)); ) JSC_CCALL(haptic_init_rumble, SDL_Haptic *haptic = js2SDL_Haptic(js, self); return JS_NewBool(js, SDL_InitHapticRumble(haptic)); ) JSC_CCALL(haptic_play_rumble, SDL_Haptic *haptic = js2SDL_Haptic(js, self); float strength = js2number(js, argv[0]); uint32_t length; JS_ToUint32(js, &length, argv[1]); return JS_NewBool(js, SDL_PlayHapticRumble(haptic, strength, length)); ) JSC_CCALL(haptic_stop_rumble, SDL_Haptic *haptic = js2SDL_Haptic(js, self); return JS_NewBool(js, SDL_StopHapticRumble(haptic)); ) JSC_CCALL(haptic_set_gain, SDL_Haptic *haptic = js2SDL_Haptic(js, self); int gain; JS_ToInt32(js, &gain, argv[0]); return JS_NewBool(js, SDL_SetHapticGain(haptic, gain)); ) JSC_CCALL(haptic_set_autocenter, SDL_Haptic *haptic = js2SDL_Haptic(js, self); int autocenter; JS_ToInt32(js, &autocenter, argv[0]); return JS_NewBool(js, SDL_SetHapticAutocenter(haptic, autocenter)); ) JSC_CCALL(haptic_pause, SDL_Haptic *haptic = js2SDL_Haptic(js, self); return JS_NewBool(js, SDL_PauseHaptic(haptic)); ) JSC_CCALL(haptic_resume, SDL_Haptic *haptic = js2SDL_Haptic(js, self); return JS_NewBool(js, SDL_ResumeHaptic(haptic)); ) JSC_CCALL(haptic_stop_all, SDL_Haptic *haptic = js2SDL_Haptic(js, self); return JS_NewBool(js, SDL_StopHapticEffects(haptic)); ) JSC_CCALL(haptic_close, SDL_Haptic *haptic = js2SDL_Haptic(js, self); SDL_CloseHaptic(haptic); return JS_NULL; ) static const JSCFunctionListEntry js_SDL_Haptic_funcs[] = { MIST_FUNC_DEF(haptic, get_id, 0), MIST_FUNC_DEF(haptic, get_name, 0), MIST_FUNC_DEF(haptic, get_features, 0), MIST_FUNC_DEF(haptic, get_num_axes, 0), MIST_FUNC_DEF(haptic, get_max_effects, 0), MIST_FUNC_DEF(haptic, get_max_effects_playing, 0), MIST_FUNC_DEF(haptic, rumble_supported, 0), MIST_FUNC_DEF(haptic, init_rumble, 0), MIST_FUNC_DEF(haptic, play_rumble, 2), MIST_FUNC_DEF(haptic, stop_rumble, 0), MIST_FUNC_DEF(haptic, set_gain, 1), MIST_FUNC_DEF(haptic, set_autocenter, 1), MIST_FUNC_DEF(haptic, pause, 0), MIST_FUNC_DEF(haptic, resume, 0), MIST_FUNC_DEF(haptic, stop_all, 0), MIST_FUNC_DEF(haptic, close, 0), }; static const JSCFunctionListEntry js_haptic_funcs[] = { MIST_FUNC_DEF(haptic, get_haptics, 0), MIST_FUNC_DEF(haptic, get_name_for_id, 1), MIST_FUNC_DEF(haptic, open, 1), MIST_FUNC_DEF(haptic, open_from_joystick, 1), MIST_FUNC_DEF(haptic, open_from_mouse, 0), MIST_FUNC_DEF(haptic, is_mouse_haptic, 0), }; CELL_USE_INIT( SDL_Init(SDL_INIT_HAPTIC); QJSCLASSPREP_FUNCS(SDL_Haptic); JSValue ret = JS_NewObject(js); JS_SetPropertyFunctionList(js, ret, js_haptic_funcs, countof(js_haptic_funcs)); // Export feature constants JS_SetPropertyStr(js, ret, "CONSTANT", JS_NewUint32(js, SDL_HAPTIC_CONSTANT)); JS_SetPropertyStr(js, ret, "SINE", JS_NewUint32(js, SDL_HAPTIC_SINE)); JS_SetPropertyStr(js, ret, "SQUARE", JS_NewUint32(js, SDL_HAPTIC_SQUARE)); JS_SetPropertyStr(js, ret, "TRIANGLE", JS_NewUint32(js, SDL_HAPTIC_TRIANGLE)); JS_SetPropertyStr(js, ret, "SAWTOOTHUP", JS_NewUint32(js, SDL_HAPTIC_SAWTOOTHUP)); JS_SetPropertyStr(js, ret, "SAWTOOTHDOWN", JS_NewUint32(js, SDL_HAPTIC_SAWTOOTHDOWN)); JS_SetPropertyStr(js, ret, "RAMP", JS_NewUint32(js, SDL_HAPTIC_RAMP)); JS_SetPropertyStr(js, ret, "SPRING", JS_NewUint32(js, SDL_HAPTIC_SPRING)); JS_SetPropertyStr(js, ret, "DAMPER", JS_NewUint32(js, SDL_HAPTIC_DAMPER)); JS_SetPropertyStr(js, ret, "INERTIA", JS_NewUint32(js, SDL_HAPTIC_INERTIA)); JS_SetPropertyStr(js, ret, "FRICTION", JS_NewUint32(js, SDL_HAPTIC_FRICTION)); JS_SetPropertyStr(js, ret, "LEFTRIGHT", JS_NewUint32(js, SDL_HAPTIC_LEFTRIGHT)); JS_SetPropertyStr(js, ret, "CUSTOM", JS_NewUint32(js, SDL_HAPTIC_CUSTOM)); JS_SetPropertyStr(js, ret, "GAIN", JS_NewUint32(js, SDL_HAPTIC_GAIN)); JS_SetPropertyStr(js, ret, "AUTOCENTER", JS_NewUint32(js, SDL_HAPTIC_AUTOCENTER)); JS_SetPropertyStr(js, ret, "STATUS", JS_NewUint32(js, SDL_HAPTIC_STATUS)); JS_SetPropertyStr(js, ret, "PAUSE", JS_NewUint32(js, SDL_HAPTIC_PAUSE)); JS_SetPropertyStr(js, ret, "INFINITY", JS_NewUint32(js, SDL_HAPTIC_INFINITY)); return ret; )