diff options
| author | 2023-04-03 23:08:11 -0600 | |
|---|---|---|
| committer | 2023-06-03 00:05:56 -0700 | |
| commit | 166bff88b852037c92c267bcc22e1f48ac0121c1 (patch) | |
| tree | 8c542f9af097f5ef22610fe378d9d1ef8e676769 /src/android | |
| parent | core: hid: Finish linking motion from virtual controllers (diff) | |
| download | yuzu-166bff88b852037c92c267bcc22e1f48ac0121c1.tar.gz yuzu-166bff88b852037c92c267bcc22e1f48ac0121c1.tar.xz yuzu-166bff88b852037c92c267bcc22e1f48ac0121c1.zip | |
android: Move motion listener to emulation activity
Diffstat (limited to 'src/android')
| -rw-r--r-- | src/android/app/src/main/java/org/yuzu/yuzu_emu/activities/EmulationActivity.kt | 71 | ||||
| -rw-r--r-- | src/android/app/src/main/java/org/yuzu/yuzu_emu/overlay/InputOverlay.kt | 64 |
2 files changed, 71 insertions, 64 deletions
diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/activities/EmulationActivity.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/activities/EmulationActivity.kt index 4670bc375..974e8b7a8 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/activities/EmulationActivity.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/activities/EmulationActivity.kt | |||
| @@ -4,9 +4,14 @@ | |||
| 4 | package org.yuzu.yuzu_emu.activities | 4 | package org.yuzu.yuzu_emu.activities |
| 5 | 5 | ||
| 6 | import android.app.Activity | 6 | import android.app.Activity |
| 7 | import android.content.Context | ||
| 7 | import android.content.DialogInterface | 8 | import android.content.DialogInterface |
| 8 | import android.content.Intent | 9 | import android.content.Intent |
| 9 | import android.graphics.Rect | 10 | import android.graphics.Rect |
| 11 | import android.hardware.Sensor | ||
| 12 | import android.hardware.SensorEvent | ||
| 13 | import android.hardware.SensorEventListener | ||
| 14 | import android.hardware.SensorManager | ||
| 10 | import android.os.Bundle | 15 | import android.os.Bundle |
| 11 | import android.view.* | 16 | import android.view.* |
| 12 | import android.view.KeyEvent | 17 | import android.view.KeyEvent |
| @@ -29,7 +34,7 @@ import org.yuzu.yuzu_emu.utils.SerializableHelper.parcelable | |||
| 29 | import org.yuzu.yuzu_emu.utils.ThemeHelper | 34 | import org.yuzu.yuzu_emu.utils.ThemeHelper |
| 30 | import kotlin.math.roundToInt | 35 | import kotlin.math.roundToInt |
| 31 | 36 | ||
| 32 | open class EmulationActivity : AppCompatActivity() { | 37 | open class EmulationActivity : AppCompatActivity(), SensorEventListener { |
| 33 | private var controllerMappingHelper: ControllerMappingHelper? = null | 38 | private var controllerMappingHelper: ControllerMappingHelper? = null |
| 34 | 39 | ||
| 35 | // TODO(bunnei): Disable notifications until we support app suspension. | 40 | // TODO(bunnei): Disable notifications until we support app suspension. |
| @@ -41,6 +46,10 @@ open class EmulationActivity : AppCompatActivity() { | |||
| 41 | private lateinit var nfcReader: NfcReader | 46 | private lateinit var nfcReader: NfcReader |
| 42 | private lateinit var inputHandler: InputHandler | 47 | private lateinit var inputHandler: InputHandler |
| 43 | 48 | ||
| 49 | private val gyro = FloatArray(3) | ||
| 50 | private val accel = FloatArray(3) | ||
| 51 | private var motionTimestamp: Long = 0 | ||
| 52 | |||
| 44 | private lateinit var game: Game | 53 | private lateinit var game: Game |
| 45 | 54 | ||
| 46 | override fun onDestroy() { | 55 | override fun onDestroy() { |
| @@ -160,6 +169,49 @@ open class EmulationActivity : AppCompatActivity() { | |||
| 160 | return inputHandler.dispatchGenericMotionEvent(event) | 169 | return inputHandler.dispatchGenericMotionEvent(event) |
| 161 | } | 170 | } |
| 162 | 171 | ||
| 172 | override fun onSensorChanged(event: SensorEvent) { | ||
| 173 | if (event.sensor.type == Sensor.TYPE_ACCELEROMETER) { | ||
| 174 | accel[0] = -event.values[1] / SensorManager.GRAVITY_EARTH | ||
| 175 | accel[1] = event.values[0] / SensorManager.GRAVITY_EARTH | ||
| 176 | accel[2] = -event.values[2] / SensorManager.GRAVITY_EARTH | ||
| 177 | } | ||
| 178 | if (event.sensor.type == Sensor.TYPE_GYROSCOPE) { | ||
| 179 | // Investigate why sensor value is off by 6x | ||
| 180 | gyro[0] = event.values[1] / 6.0f | ||
| 181 | gyro[1] = -event.values[0] / 6.0f | ||
| 182 | gyro[2] = event.values[2] / 6.0f | ||
| 183 | } | ||
| 184 | |||
| 185 | // Only update state on accelerometer data | ||
| 186 | if (event.sensor.type != Sensor.TYPE_ACCELEROMETER) { | ||
| 187 | return | ||
| 188 | } | ||
| 189 | val deltaTimestamp = (event.timestamp - motionTimestamp) / 1000 | ||
| 190 | motionTimestamp = event.timestamp | ||
| 191 | NativeLibrary.onGamePadMotionEvent( | ||
| 192 | NativeLibrary.Player1Device, | ||
| 193 | deltaTimestamp, | ||
| 194 | gyro[0], | ||
| 195 | gyro[1], | ||
| 196 | gyro[2], | ||
| 197 | accel[0], | ||
| 198 | accel[1], | ||
| 199 | accel[2] | ||
| 200 | ) | ||
| 201 | NativeLibrary.onGamePadMotionEvent( | ||
| 202 | NativeLibrary.ConsoleDevice, | ||
| 203 | deltaTimestamp, | ||
| 204 | gyro[0], | ||
| 205 | gyro[1], | ||
| 206 | gyro[2], | ||
| 207 | accel[0], | ||
| 208 | accel[1], | ||
| 209 | accel[2] | ||
| 210 | ) | ||
| 211 | } | ||
| 212 | |||
| 213 | override fun onAccuracyChanged(sensor: Sensor, i: Int) {} | ||
| 214 | |||
| 163 | private fun restoreState(savedInstanceState: Bundle) { | 215 | private fun restoreState(savedInstanceState: Bundle) { |
| 164 | game = savedInstanceState.parcelable(EXTRA_SELECTED_GAME)!! | 216 | game = savedInstanceState.parcelable(EXTRA_SELECTED_GAME)!! |
| 165 | } | 217 | } |
| @@ -212,6 +264,23 @@ open class EmulationActivity : AppCompatActivity() { | |||
| 212 | .show() | 264 | .show() |
| 213 | } | 265 | } |
| 214 | 266 | ||
| 267 | private fun startMotionSensorListener() { | ||
| 268 | val sensorManager = this.getSystemService(Context.SENSOR_SERVICE) as SensorManager | ||
| 269 | val gyroSensor = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE) | ||
| 270 | val accelSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) | ||
| 271 | sensorManager.registerListener(this, gyroSensor, SensorManager.SENSOR_DELAY_GAME) | ||
| 272 | sensorManager.registerListener(this, accelSensor, SensorManager.SENSOR_DELAY_GAME) | ||
| 273 | } | ||
| 274 | |||
| 275 | private fun stopMotionSensorListener() { | ||
| 276 | val sensorManager = this.getSystemService(Context.SENSOR_SERVICE) as SensorManager | ||
| 277 | val gyroSensor = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE) | ||
| 278 | val accelSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) | ||
| 279 | |||
| 280 | sensorManager.unregisterListener(this, gyroSensor) | ||
| 281 | sensorManager.unregisterListener(this, accelSensor) | ||
| 282 | } | ||
| 283 | |||
| 215 | private fun setControlScale(scale: Int) { | 284 | private fun setControlScale(scale: Int) { |
| 216 | PreferenceManager.getDefaultSharedPreferences(applicationContext).edit() | 285 | PreferenceManager.getDefaultSharedPreferences(applicationContext).edit() |
| 217 | .putInt(Settings.PREF_CONTROL_SCALE, scale) | 286 | .putInt(Settings.PREF_CONTROL_SCALE, scale) |
diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/overlay/InputOverlay.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/overlay/InputOverlay.kt index c2adf0ec6..5c3d79a16 100644 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/overlay/InputOverlay.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/overlay/InputOverlay.kt | |||
| @@ -12,10 +12,6 @@ import android.graphics.Canvas | |||
| 12 | import android.graphics.Rect | 12 | import android.graphics.Rect |
| 13 | import android.graphics.drawable.Drawable | 13 | import android.graphics.drawable.Drawable |
| 14 | import android.graphics.drawable.VectorDrawable | 14 | import android.graphics.drawable.VectorDrawable |
| 15 | import android.hardware.Sensor | ||
| 16 | import android.hardware.SensorEvent | ||
| 17 | import android.hardware.SensorEventListener | ||
| 18 | import android.hardware.SensorManager | ||
| 19 | import android.os.Build | 15 | import android.os.Build |
| 20 | import android.util.AttributeSet | 16 | import android.util.AttributeSet |
| 21 | import android.view.MotionEvent | 17 | import android.view.MotionEvent |
| @@ -41,7 +37,7 @@ import kotlin.math.min | |||
| 41 | * [SurfaceView] that is rendering emulation. | 37 | * [SurfaceView] that is rendering emulation. |
| 42 | */ | 38 | */ |
| 43 | class InputOverlay(context: Context, attrs: AttributeSet?) : SurfaceView(context, attrs), | 39 | class InputOverlay(context: Context, attrs: AttributeSet?) : SurfaceView(context, attrs), |
| 44 | OnTouchListener, SensorEventListener { | 40 | OnTouchListener { |
| 45 | private val overlayButtons: MutableSet<InputOverlayDrawableButton> = HashSet() | 41 | private val overlayButtons: MutableSet<InputOverlayDrawableButton> = HashSet() |
| 46 | private val overlayDpads: MutableSet<InputOverlayDrawableDpad> = HashSet() | 42 | private val overlayDpads: MutableSet<InputOverlayDrawableDpad> = HashSet() |
| 47 | private val overlayJoysticks: MutableSet<InputOverlayDrawableJoystick> = HashSet() | 43 | private val overlayJoysticks: MutableSet<InputOverlayDrawableJoystick> = HashSet() |
| @@ -54,21 +50,8 @@ class InputOverlay(context: Context, attrs: AttributeSet?) : SurfaceView(context | |||
| 54 | private val preferences: SharedPreferences = | 50 | private val preferences: SharedPreferences = |
| 55 | PreferenceManager.getDefaultSharedPreferences(YuzuApplication.appContext) | 51 | PreferenceManager.getDefaultSharedPreferences(YuzuApplication.appContext) |
| 56 | 52 | ||
| 57 | private val gyro = FloatArray(3) | ||
| 58 | private val accel = FloatArray(3) | ||
| 59 | private var motionTimestamp: Long = 0 | ||
| 60 | |||
| 61 | private lateinit var windowInsets: WindowInsets | 53 | private lateinit var windowInsets: WindowInsets |
| 62 | 54 | ||
| 63 | private fun setMotionSensorListener(context: Context) { | ||
| 64 | val sensorManager = context.getSystemService(Context.SENSOR_SERVICE) as SensorManager | ||
| 65 | val gyroSensor = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE) | ||
| 66 | val accelSensor = sensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER) | ||
| 67 | |||
| 68 | sensorManager.registerListener(this, gyroSensor, SensorManager.SENSOR_DELAY_GAME) | ||
| 69 | sensorManager.registerListener(this, accelSensor, SensorManager.SENSOR_DELAY_GAME) | ||
| 70 | } | ||
| 71 | |||
| 72 | override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) { | 55 | override fun onLayout(changed: Boolean, left: Int, top: Int, right: Int, bottom: Int) { |
| 73 | super.onLayout(changed, left, top, right, bottom) | 56 | super.onLayout(changed, left, top, right, bottom) |
| 74 | 57 | ||
| @@ -81,9 +64,6 @@ class InputOverlay(context: Context, attrs: AttributeSet?) : SurfaceView(context | |||
| 81 | // Load the controls. | 64 | // Load the controls. |
| 82 | refreshControls() | 65 | refreshControls() |
| 83 | 66 | ||
| 84 | // Set the on motion sensor listener. | ||
| 85 | setMotionSensorListener(context) | ||
| 86 | |||
| 87 | // Set the on touch listener. | 67 | // Set the on touch listener. |
| 88 | setOnTouchListener(this) | 68 | setOnTouchListener(this) |
| 89 | 69 | ||
| @@ -338,48 +318,6 @@ class InputOverlay(context: Context, attrs: AttributeSet?) : SurfaceView(context | |||
| 338 | return true | 318 | return true |
| 339 | } | 319 | } |
| 340 | 320 | ||
| 341 | override fun onSensorChanged(event: SensorEvent) { | ||
| 342 | if (event.sensor.type == Sensor.TYPE_ACCELEROMETER) { | ||
| 343 | accel[0] = -event.values[1] / SensorManager.GRAVITY_EARTH | ||
| 344 | accel[1] = event.values[0] / SensorManager.GRAVITY_EARTH | ||
| 345 | accel[2] = -event.values[2] / SensorManager.GRAVITY_EARTH | ||
| 346 | } | ||
| 347 | if (event.sensor.type == Sensor.TYPE_GYROSCOPE) { | ||
| 348 | // Investigate why sensor value is off by 12x | ||
| 349 | gyro[0] = event.values[1] / 12.0f | ||
| 350 | gyro[1] = -event.values[0] / 12.0f | ||
| 351 | gyro[2] = event.values[2] / 12.0f | ||
| 352 | } | ||
| 353 | |||
| 354 | // Only update state on accelerometer data | ||
| 355 | if (event.sensor.type != Sensor.TYPE_ACCELEROMETER) { | ||
| 356 | return | ||
| 357 | } | ||
| 358 | val deltaTimestamp = (event.timestamp - motionTimestamp) / 1000 | ||
| 359 | motionTimestamp = event.timestamp | ||
| 360 | NativeLibrary.onGamePadMotionEvent( | ||
| 361 | NativeLibrary.Player1Device, | ||
| 362 | deltaTimestamp, | ||
| 363 | gyro[0], | ||
| 364 | gyro[1], | ||
| 365 | gyro[2], | ||
| 366 | accel[0], | ||
| 367 | accel[1], | ||
| 368 | accel[2] | ||
| 369 | ) | ||
| 370 | NativeLibrary.onGamePadMotionEvent( | ||
| 371 | NativeLibrary.ConsoleDevice, | ||
| 372 | deltaTimestamp, | ||
| 373 | gyro[0], | ||
| 374 | gyro[1], | ||
| 375 | gyro[2], | ||
| 376 | accel[0], | ||
| 377 | accel[1], | ||
| 378 | accel[2] | ||
| 379 | ) | ||
| 380 | } | ||
| 381 | |||
| 382 | override fun onAccuracyChanged(sensor: Sensor, i: Int) {} | ||
| 383 | private fun addOverlayControls(orientation: String) { | 321 | private fun addOverlayControls(orientation: String) { |
| 384 | if (preferences.getBoolean(Settings.PREF_BUTTON_TOGGLE_0, true)) { | 322 | if (preferences.getBoolean(Settings.PREF_BUTTON_TOGGLE_0, true)) { |
| 385 | overlayButtons.add( | 323 | overlayButtons.add( |