mirror of
https://github.com/libretro/RetroArch
synced 2025-04-10 15:45:19 +00:00
(Android) Refactor android_input_set_rumble to be safer
This commit is contained in:
parent
f9f118271a
commit
61746c8179
@ -1714,79 +1714,88 @@ static void android_input_grab_mouse(void *data, bool state)
|
|||||||
(void)state;
|
(void)state;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
static void android_input_set_rumble_internal(
|
||||||
|
uint16_t strength,
|
||||||
|
uint16_t *last_strength_strong,
|
||||||
|
uint16_t *last_strength_weak,
|
||||||
|
uint16_t *last_strength,
|
||||||
|
int8_t id,
|
||||||
|
enum retro_rumble_effect effect
|
||||||
|
)
|
||||||
|
{
|
||||||
|
JNIEnv *env = (JNIEnv*)jni_thread_getenv();
|
||||||
|
uint16_t new_strength = 0;
|
||||||
|
|
||||||
|
if (!env)
|
||||||
|
return;
|
||||||
|
|
||||||
|
if (effect == RETRO_RUMBLE_STRONG)
|
||||||
|
{
|
||||||
|
new_strength = strength | *last_strength_weak;
|
||||||
|
*last_strength_strong = strength;
|
||||||
|
}
|
||||||
|
else if (effect == RETRO_RUMBLE_WEAK)
|
||||||
|
{
|
||||||
|
new_strength = strength | *last_strength_strong;
|
||||||
|
*last_strength_weak = strength;
|
||||||
|
}
|
||||||
|
|
||||||
|
if (new_strength != *last_strength)
|
||||||
|
{
|
||||||
|
/* trying to send this value as a JNI param without
|
||||||
|
* storing it first was causing 0 to be seen on the other side ?? */
|
||||||
|
int strength_final = (255.0f / 65535.0f) * (float)new_strength;
|
||||||
|
|
||||||
|
CALL_VOID_METHOD_PARAM(env, g_android->activity->clazz,
|
||||||
|
g_android->doVibrate, (jint)id, (jint)RETRO_RUMBLE_STRONG, (jint)strength_final, (jint)0);
|
||||||
|
|
||||||
|
*last_strength = new_strength;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
static bool android_input_set_rumble(void *data, unsigned port,
|
static bool android_input_set_rumble(void *data, unsigned port,
|
||||||
enum retro_rumble_effect effect, uint16_t strength)
|
enum retro_rumble_effect effect, uint16_t strength)
|
||||||
{
|
{
|
||||||
android_input_t *android = (android_input_t*)data;
|
|
||||||
settings_t *settings = config_get_ptr();
|
settings_t *settings = config_get_ptr();
|
||||||
JNIEnv *env = (JNIEnv*)jni_thread_getenv();
|
|
||||||
|
|
||||||
if (!android || !env || !g_android || !g_android->doVibrate)
|
if (!g_android || !g_android->doVibrate)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (settings->bools.enable_device_vibration)
|
if (settings->bools.enable_device_vibration)
|
||||||
{
|
{
|
||||||
static uint16_t last_strength_strong = 0;
|
static uint16_t last_strength_strong = 0;
|
||||||
static uint16_t last_strength_weak = 0;
|
static uint16_t last_strength_weak = 0;
|
||||||
static uint16_t last_strength = 0;
|
static uint16_t last_strength = 0;
|
||||||
uint16_t new_strength = 0;
|
|
||||||
|
|
||||||
if (port != 0)
|
if (port != 0)
|
||||||
return false;
|
return false;
|
||||||
|
|
||||||
if (effect == RETRO_RUMBLE_STRONG)
|
android_input_set_rumble_internal(
|
||||||
{
|
strength,
|
||||||
new_strength = strength | last_strength_weak;
|
&last_strength_strong,
|
||||||
last_strength_strong = strength;
|
&last_strength_weak,
|
||||||
}
|
&last_strength,
|
||||||
else if (effect == RETRO_RUMBLE_WEAK)
|
-1,
|
||||||
{
|
effect);
|
||||||
new_strength = strength | last_strength_strong;
|
|
||||||
last_strength_weak = strength;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (new_strength != last_strength)
|
|
||||||
{
|
|
||||||
/* trying to send this value as a JNI param without storing it first was causing 0 to be seen on the other side ?? */
|
|
||||||
int strength_final = (255.0f / 65535.0f) * (float)new_strength;
|
|
||||||
|
|
||||||
CALL_VOID_METHOD_PARAM(env, g_android->activity->clazz,
|
|
||||||
g_android->doVibrate, (jint)-1, (jint)RETRO_RUMBLE_STRONG, (jint)strength_final, (jint)0);
|
|
||||||
|
|
||||||
last_strength = new_strength;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
uint16_t new_strength = 0;
|
android_input_t *android = (android_input_t*)data;
|
||||||
state_device_t *state = &android->pad_states[port];
|
state_device_t *state = android ? &android->pad_states[port] : NULL;
|
||||||
|
|
||||||
if (effect == RETRO_RUMBLE_STRONG)
|
if (state)
|
||||||
{
|
{
|
||||||
new_strength = strength | state->rumble_last_strength_weak;
|
android_input_set_rumble_internal(
|
||||||
state->rumble_last_strength_strong = strength;
|
strength,
|
||||||
|
&state->rumble_last_strength_strong,
|
||||||
|
&state->rumble_last_strength_weak,
|
||||||
|
&state->rumble_last_strength,
|
||||||
|
state->id,
|
||||||
|
effect);
|
||||||
|
return true;
|
||||||
}
|
}
|
||||||
else if (effect == RETRO_RUMBLE_WEAK)
|
|
||||||
{
|
|
||||||
new_strength = strength | state->rumble_last_strength_strong;
|
|
||||||
state->rumble_last_strength_weak = strength;
|
|
||||||
}
|
|
||||||
|
|
||||||
if (new_strength != state->rumble_last_strength)
|
|
||||||
{
|
|
||||||
/* trying to send this value as a JNI param without storing it first was causing 0 to be seen on the other side ?? */
|
|
||||||
int strength_final = (255.0f / 65535.0f) * (float)new_strength;
|
|
||||||
int id = state->id;
|
|
||||||
|
|
||||||
CALL_VOID_METHOD_PARAM(env, g_android->activity->clazz,
|
|
||||||
g_android->doVibrate, (jint)id, (jint)RETRO_RUMBLE_STRONG, (jint)strength_final, (jint)0);
|
|
||||||
|
|
||||||
state->rumble_last_strength = new_strength;
|
|
||||||
}
|
|
||||||
|
|
||||||
return true;
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return false;
|
return false;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user