add device vibration option for cores that support rumble (with initial android implementation)

This commit is contained in:
Brad Parker 2019-03-12 13:07:16 -04:00
parent 34f4d5ed0e
commit 954c54baee
12 changed files with 96 additions and 10 deletions

View File

@ -821,6 +821,7 @@ static const unsigned midi_volume = 100;
static const bool sustained_performance_mode = false;
static const bool vibrate_on_keypress = false;
static const bool enable_device_vibration = false;
#if defined(HAKCHI)
static char buildbot_server_url[] = "http://hakchicloud.com/Libretro_Cores/";

View File

@ -1587,6 +1587,7 @@ static struct config_bool_setting *populate_settings_bool(settings_t *settings,
SETTING_BOOL("quit_press_twice", &settings->bools.quit_press_twice, true, quit_press_twice, false);
SETTING_BOOL("vibrate_on_keypress", &settings->bools.vibrate_on_keypress, true, vibrate_on_keypress, false);
SETTING_BOOL("enable_device_vibration", &settings->bools.enable_device_vibration, true, enable_device_vibration, false);
#ifdef HAVE_OZONE
SETTING_BOOL("ozone_collapse_sidebar", &settings->bools.ozone_collapse_sidebar, true, ozone_collapse_sidebar, false);

View File

@ -316,6 +316,7 @@ typedef struct settings
bool quit_press_twice;
bool vibrate_on_keypress;
bool enable_device_vibration;
#ifdef HAVE_OZONE
bool ozone_collapse_sidebar;
#endif

View File

@ -2037,7 +2037,7 @@ static void frontend_unix_init(void *data)
GET_METHOD_ID(env, android_app->setScreenOrientation, class,
"setScreenOrientation", "(I)V");
GET_METHOD_ID(env, android_app->doVibrate, class,
"doVibrate", "()V");
"doVibrate", "(III)V");
CALL_OBJ_METHOD(env, obj, android_app->activity->clazz,
android_app->getIntent);

View File

@ -425,9 +425,11 @@ static void android_input_poll_main_cmd(void)
{
JNIEnv *env = (JNIEnv*)jni_thread_getenv();
if (env && g_android)
CALL_VOID_METHOD(env, g_android->activity->clazz,
g_android->doVibrate);
if (env && g_android && g_android->doVibrate)
{
CALL_VOID_METHOD_PARAM(env, g_android->activity->clazz,
g_android->doVibrate, RETRO_RUMBLE_STRONG, 255, 1);
}
break;
}
}
@ -1630,10 +1632,47 @@ static void android_input_grab_mouse(void *data, bool state)
static bool android_input_set_rumble(void *data, unsigned port,
enum retro_rumble_effect effect, uint16_t strength)
{
settings_t *settings = config_get_ptr();
(void)data;
(void)port;
(void)effect;
(void)strength;
if (settings->bools.enable_device_vibration)
{
JNIEnv *env = (JNIEnv*)jni_thread_getenv();
struct android_app *android_app = (struct android_app*)g_android;
if (env && g_android && g_android->doVibrate)
{
static uint16_t last_strength_strong = 0;
static uint16_t last_strength_weak = 0;
static uint16_t last_strength = 0;
uint16_t new_strength = 0;
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, RETRO_RUMBLE_STRONG, strength_final, 0);
last_strength = new_strength;
}
}
return true;
}
return false;
}

View File

@ -1807,3 +1807,5 @@ MSG_HASH(MENU_ENUM_LABEL_HELP_SEND_DEBUG_INFO,
"help_send_debug_info")
MSG_HASH(MENU_ENUM_LABEL_VIBRATE_ON_KEYPRESS,
"vibrate_on_keypress")
MSG_HASH(MENU_ENUM_LABEL_ENABLE_DEVICE_VIBRATION,
"enable_device_vibration")

View File

@ -8450,3 +8450,7 @@ MSG_HASH(
MENU_ENUM_LABEL_VALUE_VIBRATE_ON_KEYPRESS,
"Vibrate on key press"
)
MSG_HASH(
MENU_ENUM_LABEL_VALUE_ENABLE_DEVICE_VIBRATION,
"Enable device vibration (for supported cores)"
)

View File

@ -7083,6 +7083,9 @@ bool menu_displaylist_ctl(enum menu_displaylist_ctl_state type, menu_displaylist
ret = menu_displaylist_parse_settings_enum(menu, info,
MENU_ENUM_LABEL_VIBRATE_ON_KEYPRESS,
PARSE_ONLY_BOOL, false);
ret = menu_displaylist_parse_settings_enum(menu, info,
MENU_ENUM_LABEL_ENABLE_DEVICE_VIBRATION,
PARSE_ONLY_BOOL, false);
if (menu_displaylist_parse_settings_enum(menu, info,
MENU_ENUM_LABEL_INPUT_POLL_TYPE_BEHAVIOR,
PARSE_ONLY_UINT, false) == 0)

View File

@ -6959,6 +6959,22 @@ static bool setting_append_list(
SD_FLAG_NONE
);
CONFIG_BOOL(
list, list_info,
&settings->bools.enable_device_vibration,
MENU_ENUM_LABEL_ENABLE_DEVICE_VIBRATION,
MENU_ENUM_LABEL_VALUE_ENABLE_DEVICE_VIBRATION,
enable_device_vibration,
MENU_ENUM_LABEL_VALUE_OFF,
MENU_ENUM_LABEL_VALUE_ON,
&group_info,
&subgroup_info,
parent_group,
general_write_handler,
general_read_handler,
SD_FLAG_NONE
);
CONFIG_UINT(
list, list_info,
&settings->uints.input_poll_type_behavior,

View File

@ -2312,6 +2312,7 @@ enum msg_hash_enums
MSG_PRESS_ONE_MORE_TIME_TO_SEND_DEBUG_INFO,
MENU_LABEL(VIBRATE_ON_KEYPRESS),
MENU_LABEL(ENABLE_DEVICE_VIBRATION),
MSG_LAST
};

View File

@ -18,7 +18,7 @@ import android.os.PowerManager;
import android.os.Vibrator;
import android.os.VibrationEffect;
import android.util.Log;
import java.lang.Math;
import java.util.concurrent.CountDownLatch;
/**
@ -35,17 +35,32 @@ public class RetroActivityCommon extends RetroActivityLocation
public static int FRONTEND_ORIENTATION_90 = 1;
public static int FRONTEND_ORIENTATION_180 = 2;
public static int FRONTEND_ORIENTATION_270 = 3;
public static int RETRO_RUMBLE_STRONG = 0;
public static int RETRO_RUMBLE_WEAK = 1;
public boolean sustainedPerformanceMode = true;
public int screenOrientation = ActivityInfo.SCREEN_ORIENTATION_UNSPECIFIED;
public void doVibrate()
public void doVibrate(int effect, int strength, int oneShot)
{
Vibrator vibrator = (Vibrator)getSystemService(Context.VIBRATOR_SERVICE);
int repeat = 0;
long[] pattern = {16};
int[] strengths = {strength};
if (strength == 0) {
vibrator.cancel();
return;
}
if (oneShot > 0)
repeat = -1;
else
pattern[0] = 1000;
if (Build.VERSION.SDK_INT >= 26) {
vibrator.vibrate(VibrationEffect.createOneShot(33, VibrationEffect.DEFAULT_AMPLITUDE), new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_ASSISTANCE_SONIFICATION).setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION).build());
vibrator.vibrate(VibrationEffect.createWaveform(pattern, strengths, repeat), new AudioAttributes.Builder().setUsage(AudioAttributes.USAGE_GAME).setContentType(AudioAttributes.CONTENT_TYPE_SONIFICATION).build());
}else{
vibrator.vibrate(33);
vibrator.vibrate(pattern, repeat);
}
}

View File

@ -906,3 +906,6 @@
# content_runtime_log = false
# vibrate_on_keypress = false
# Enable device vibration for supported cores
# enable_device_vibration = false