summaryrefslogtreecommitdiffstats
path: root/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/InputHandler.kt
blob: 4303939efd1084a8386e525aaa2ee25ae730e932 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
package org.yuzu.yuzu_emu.utils

import android.view.KeyEvent
import android.view.MotionEvent
import org.yuzu.yuzu_emu.NativeLibrary
import kotlin.math.sqrt

class InputHandler {
    fun initialize() {
        // Connect first controller
        NativeLibrary.onGamePadConnectEvent(getPlayerNumber(NativeLibrary.Player1Device))
    }

    fun dispatchKeyEvent(event: KeyEvent): Boolean {
        val button: Int = when (event.device.vendorId) {
            0x045E -> getInputXboxButtonKey(event.keyCode)
            0x054C -> getInputDS5ButtonKey(event.keyCode)
            0x057E -> getInputJoyconButtonKey(event.keyCode)
            0x1532 -> getInputRazerButtonKey(event.keyCode)
            else -> getInputGenericButtonKey(event.keyCode)
        }

        val action = when (event.action) {
            KeyEvent.ACTION_DOWN -> NativeLibrary.ButtonState.PRESSED
            KeyEvent.ACTION_UP -> NativeLibrary.ButtonState.RELEASED
            else -> return false
        }

        // Ignore invalid buttons
        if (button < 0) {
            return false
        }

        return NativeLibrary.onGamePadButtonEvent(
            getPlayerNumber(event.device.controllerNumber),
            button,
            action
        )
    }

    fun dispatchGenericMotionEvent(event: MotionEvent): Boolean {
        val device = event.device
        // Check every axis input available on the controller
        for (range in device.motionRanges) {
            val axis = range.axis
            when (device.vendorId) {
                0x045E -> setGenericAxisInput(event, axis)
                0x054C -> setGenericAxisInput(event, axis)
                0x057E -> setJoyconAxisInput(event, axis)
                0x1532 -> setRazerAxisInput(event, axis)
                else -> setGenericAxisInput(event, axis)
            }
        }

        return true
    }

    private fun getPlayerNumber(index: Int): Int {
        // TODO: Joycons are handled as different controllers. Find a way to merge them.
        return when (index) {
            2 -> NativeLibrary.Player2Device
            3 -> NativeLibrary.Player3Device
            4 -> NativeLibrary.Player4Device
            5 -> NativeLibrary.Player5Device
            6 -> NativeLibrary.Player6Device
            7 -> NativeLibrary.Player7Device
            8 -> NativeLibrary.Player8Device
            else -> if (NativeLibrary.isHandheldOnly()) NativeLibrary.ConsoleDevice else NativeLibrary.Player1Device
        }
    }

    private fun setStickState(playerNumber: Int, index: Int, xAxis: Float, yAxis: Float) {
        // Calculate vector size
        val r2 = xAxis * xAxis + yAxis * yAxis
        var r = sqrt(r2.toDouble()).toFloat()

        // Adjust range of joystick
        val deadzone = 0.15f
        val deadzoneFactor = 1.0f / r * (r - deadzone) / (1.0f - deadzone)
        var x = xAxis * deadzoneFactor
        var y = yAxis * deadzoneFactor
        r *= deadzoneFactor

        // Normalize joystick
        if (r > 1.0f) {
            x /= r
            y /= r
        }

        NativeLibrary.onGamePadJoystickEvent(
            playerNumber,
            index,
            x,
            -y
        )
    }

    private fun getAxisToButton(axis: Float): Int {
        return if (axis > 0.5f) NativeLibrary.ButtonState.PRESSED else NativeLibrary.ButtonState.RELEASED
    }

    private fun setAxisDpadState(playerNumber: Int, xAxis: Float, yAxis: Float) {
        NativeLibrary.onGamePadButtonEvent(
            playerNumber,
            NativeLibrary.ButtonType.DPAD_UP,
            getAxisToButton(-yAxis)
        )
        NativeLibrary.onGamePadButtonEvent(
            playerNumber,
            NativeLibrary.ButtonType.DPAD_DOWN,
            getAxisToButton(yAxis)
        )
        NativeLibrary.onGamePadButtonEvent(
            playerNumber,
            NativeLibrary.ButtonType.DPAD_LEFT,
            getAxisToButton(-xAxis)
        )
        NativeLibrary.onGamePadButtonEvent(
            playerNumber,
            NativeLibrary.ButtonType.DPAD_RIGHT,
            getAxisToButton(xAxis)
        )
    }

    private fun getInputDS5ButtonKey(key: Int): Int {
        // The missing ds5 buttons are axis
        return when (key) {
            KeyEvent.KEYCODE_BUTTON_A -> NativeLibrary.ButtonType.BUTTON_B
            KeyEvent.KEYCODE_BUTTON_B -> NativeLibrary.ButtonType.BUTTON_A
            KeyEvent.KEYCODE_BUTTON_X -> NativeLibrary.ButtonType.BUTTON_Y
            KeyEvent.KEYCODE_BUTTON_Y -> NativeLibrary.ButtonType.BUTTON_X
            KeyEvent.KEYCODE_BUTTON_L1 -> NativeLibrary.ButtonType.TRIGGER_L
            KeyEvent.KEYCODE_BUTTON_R1 -> NativeLibrary.ButtonType.TRIGGER_R
            KeyEvent.KEYCODE_BUTTON_THUMBL -> NativeLibrary.ButtonType.STICK_L
            KeyEvent.KEYCODE_BUTTON_THUMBR -> NativeLibrary.ButtonType.STICK_R
            KeyEvent.KEYCODE_BUTTON_START -> NativeLibrary.ButtonType.BUTTON_PLUS
            KeyEvent.KEYCODE_BUTTON_SELECT -> NativeLibrary.ButtonType.BUTTON_MINUS
            else -> -1
        }
    }

    private fun getInputJoyconButtonKey(key: Int): Int {
        // Joycon support is half dead. A lot of buttons can't be mapped
        return when (key) {
            KeyEvent.KEYCODE_BUTTON_A -> NativeLibrary.ButtonType.BUTTON_B
            KeyEvent.KEYCODE_BUTTON_B -> NativeLibrary.ButtonType.BUTTON_A
            KeyEvent.KEYCODE_BUTTON_X -> NativeLibrary.ButtonType.BUTTON_X
            KeyEvent.KEYCODE_BUTTON_Y -> NativeLibrary.ButtonType.BUTTON_Y
            KeyEvent.KEYCODE_DPAD_UP -> NativeLibrary.ButtonType.DPAD_UP
            KeyEvent.KEYCODE_DPAD_DOWN -> NativeLibrary.ButtonType.DPAD_DOWN
            KeyEvent.KEYCODE_DPAD_LEFT -> NativeLibrary.ButtonType.DPAD_LEFT
            KeyEvent.KEYCODE_DPAD_RIGHT -> NativeLibrary.ButtonType.DPAD_RIGHT
            KeyEvent.KEYCODE_BUTTON_L1 -> NativeLibrary.ButtonType.TRIGGER_L
            KeyEvent.KEYCODE_BUTTON_R1 -> NativeLibrary.ButtonType.TRIGGER_R
            KeyEvent.KEYCODE_BUTTON_L2 -> NativeLibrary.ButtonType.TRIGGER_ZL
            KeyEvent.KEYCODE_BUTTON_R2 -> NativeLibrary.ButtonType.TRIGGER_ZR
            KeyEvent.KEYCODE_BUTTON_THUMBL -> NativeLibrary.ButtonType.STICK_L
            KeyEvent.KEYCODE_BUTTON_THUMBR -> NativeLibrary.ButtonType.STICK_R
            KeyEvent.KEYCODE_BUTTON_START -> NativeLibrary.ButtonType.BUTTON_PLUS
            KeyEvent.KEYCODE_BUTTON_SELECT -> NativeLibrary.ButtonType.BUTTON_MINUS
            else -> -1
        }
    }

    private fun getInputXboxButtonKey(key: Int): Int {
        // The missing xbox buttons are axis
        return when (key) {
            KeyEvent.KEYCODE_BUTTON_A -> NativeLibrary.ButtonType.BUTTON_A
            KeyEvent.KEYCODE_BUTTON_B -> NativeLibrary.ButtonType.BUTTON_B
            KeyEvent.KEYCODE_BUTTON_X -> NativeLibrary.ButtonType.BUTTON_X
            KeyEvent.KEYCODE_BUTTON_Y -> NativeLibrary.ButtonType.BUTTON_Y
            KeyEvent.KEYCODE_BUTTON_L1 -> NativeLibrary.ButtonType.TRIGGER_L
            KeyEvent.KEYCODE_BUTTON_R1 -> NativeLibrary.ButtonType.TRIGGER_R
            KeyEvent.KEYCODE_BUTTON_THUMBL -> NativeLibrary.ButtonType.STICK_L
            KeyEvent.KEYCODE_BUTTON_THUMBR -> NativeLibrary.ButtonType.STICK_R
            KeyEvent.KEYCODE_BUTTON_START -> NativeLibrary.ButtonType.BUTTON_PLUS
            KeyEvent.KEYCODE_BUTTON_SELECT -> NativeLibrary.ButtonType.BUTTON_MINUS
            else -> -1
        }
    }

    private fun getInputRazerButtonKey(key: Int): Int {
        // The missing xbox buttons are axis
        return when (key) {
            KeyEvent.KEYCODE_BUTTON_A -> NativeLibrary.ButtonType.BUTTON_B
            KeyEvent.KEYCODE_BUTTON_B -> NativeLibrary.ButtonType.BUTTON_A
            KeyEvent.KEYCODE_BUTTON_X -> NativeLibrary.ButtonType.BUTTON_Y
            KeyEvent.KEYCODE_BUTTON_Y -> NativeLibrary.ButtonType.BUTTON_X
            KeyEvent.KEYCODE_BUTTON_L1 -> NativeLibrary.ButtonType.TRIGGER_L
            KeyEvent.KEYCODE_BUTTON_R1 -> NativeLibrary.ButtonType.TRIGGER_R
            KeyEvent.KEYCODE_BUTTON_THUMBL -> NativeLibrary.ButtonType.STICK_L
            KeyEvent.KEYCODE_BUTTON_THUMBR -> NativeLibrary.ButtonType.STICK_R
            KeyEvent.KEYCODE_BUTTON_START -> NativeLibrary.ButtonType.BUTTON_PLUS
            KeyEvent.KEYCODE_BUTTON_SELECT -> NativeLibrary.ButtonType.BUTTON_MINUS
            else -> -1
        }
    }

    private fun getInputGenericButtonKey(key: Int): Int {
        return when (key) {
            KeyEvent.KEYCODE_BUTTON_A -> NativeLibrary.ButtonType.BUTTON_A
            KeyEvent.KEYCODE_BUTTON_B -> NativeLibrary.ButtonType.BUTTON_B
            KeyEvent.KEYCODE_BUTTON_X -> NativeLibrary.ButtonType.BUTTON_X
            KeyEvent.KEYCODE_BUTTON_Y -> NativeLibrary.ButtonType.BUTTON_Y
            KeyEvent.KEYCODE_DPAD_UP -> NativeLibrary.ButtonType.DPAD_UP
            KeyEvent.KEYCODE_DPAD_DOWN -> NativeLibrary.ButtonType.DPAD_DOWN
            KeyEvent.KEYCODE_DPAD_LEFT -> NativeLibrary.ButtonType.DPAD_LEFT
            KeyEvent.KEYCODE_DPAD_RIGHT -> NativeLibrary.ButtonType.DPAD_RIGHT
            KeyEvent.KEYCODE_BUTTON_L1 -> NativeLibrary.ButtonType.TRIGGER_L
            KeyEvent.KEYCODE_BUTTON_R1 -> NativeLibrary.ButtonType.TRIGGER_R
            KeyEvent.KEYCODE_BUTTON_L2 -> NativeLibrary.ButtonType.TRIGGER_ZL
            KeyEvent.KEYCODE_BUTTON_R2 -> NativeLibrary.ButtonType.TRIGGER_ZR
            KeyEvent.KEYCODE_BUTTON_THUMBL -> NativeLibrary.ButtonType.STICK_L
            KeyEvent.KEYCODE_BUTTON_THUMBR -> NativeLibrary.ButtonType.STICK_R
            KeyEvent.KEYCODE_BUTTON_START -> NativeLibrary.ButtonType.BUTTON_PLUS
            KeyEvent.KEYCODE_BUTTON_SELECT -> NativeLibrary.ButtonType.BUTTON_MINUS
            else -> -1
        }
    }

    private fun setGenericAxisInput(event: MotionEvent, axis: Int) {
        val playerNumber = getPlayerNumber(event.device.controllerNumber)

        when (axis) {
            MotionEvent.AXIS_X, MotionEvent.AXIS_Y ->
                setStickState(
                    playerNumber,
                    NativeLibrary.StickType.STICK_L,
                    event.getAxisValue(MotionEvent.AXIS_X),
                    event.getAxisValue(MotionEvent.AXIS_Y)
                )
            MotionEvent.AXIS_RX, MotionEvent.AXIS_RY ->
                setStickState(
                    playerNumber,
                    NativeLibrary.StickType.STICK_R,
                    event.getAxisValue(MotionEvent.AXIS_RX),
                    event.getAxisValue(MotionEvent.AXIS_RY)
                )
            MotionEvent.AXIS_Z, MotionEvent.AXIS_RZ ->
                setStickState(
                    playerNumber,
                    NativeLibrary.StickType.STICK_R,
                    event.getAxisValue(MotionEvent.AXIS_Z),
                    event.getAxisValue(MotionEvent.AXIS_RZ)
                )
            MotionEvent.AXIS_LTRIGGER ->
                NativeLibrary.onGamePadButtonEvent(
                    playerNumber,
                    NativeLibrary.ButtonType.TRIGGER_ZL,
                    getAxisToButton(event.getAxisValue(MotionEvent.AXIS_LTRIGGER))
                )
            MotionEvent.AXIS_BRAKE ->
                NativeLibrary.onGamePadButtonEvent(
                    playerNumber,
                    NativeLibrary.ButtonType.TRIGGER_ZL,
                    getAxisToButton(event.getAxisValue(MotionEvent.AXIS_BRAKE))
                )
            MotionEvent.AXIS_RTRIGGER ->
                NativeLibrary.onGamePadButtonEvent(
                    playerNumber,
                    NativeLibrary.ButtonType.TRIGGER_ZR,
                    getAxisToButton(event.getAxisValue(MotionEvent.AXIS_RTRIGGER))
                )
            MotionEvent.AXIS_GAS ->
                NativeLibrary.onGamePadButtonEvent(
                    playerNumber,
                    NativeLibrary.ButtonType.TRIGGER_ZR,
                    getAxisToButton(event.getAxisValue(MotionEvent.AXIS_GAS))
                )
            MotionEvent.AXIS_HAT_X, MotionEvent.AXIS_HAT_Y ->
                setAxisDpadState(
                    playerNumber,
                    event.getAxisValue(MotionEvent.AXIS_HAT_X),
                    event.getAxisValue(MotionEvent.AXIS_HAT_Y)
                )
        }
    }


    private fun setJoyconAxisInput(event: MotionEvent, axis: Int) {
        // Joycon support is half dead. Right joystick doesn't work
        val playerNumber = getPlayerNumber(event.device.controllerNumber)

        when (axis) {
            MotionEvent.AXIS_X, MotionEvent.AXIS_Y ->
                setStickState(
                    playerNumber,
                    NativeLibrary.StickType.STICK_L,
                    event.getAxisValue(MotionEvent.AXIS_X),
                    event.getAxisValue(MotionEvent.AXIS_Y)
                )
            MotionEvent.AXIS_Z, MotionEvent.AXIS_RZ ->
                setStickState(
                    playerNumber,
                    NativeLibrary.StickType.STICK_R,
                    event.getAxisValue(MotionEvent.AXIS_Z),
                    event.getAxisValue(MotionEvent.AXIS_RZ)
                )
            MotionEvent.AXIS_RX, MotionEvent.AXIS_RY ->
                setStickState(
                    playerNumber,
                    NativeLibrary.StickType.STICK_R,
                    event.getAxisValue(MotionEvent.AXIS_RX),
                    event.getAxisValue(MotionEvent.AXIS_RY)
                )
        }
    }

    private fun setRazerAxisInput(event: MotionEvent, axis: Int) {
        val playerNumber = getPlayerNumber(event.device.controllerNumber)

        when (axis) {
            MotionEvent.AXIS_X, MotionEvent.AXIS_Y ->
                setStickState(
                    playerNumber,
                    NativeLibrary.StickType.STICK_L,
                    event.getAxisValue(MotionEvent.AXIS_X),
                    event.getAxisValue(MotionEvent.AXIS_Y)
                )
            MotionEvent.AXIS_Z, MotionEvent.AXIS_RZ ->
                setStickState(
                    playerNumber,
                    NativeLibrary.StickType.STICK_R,
                    event.getAxisValue(MotionEvent.AXIS_Z),
                    event.getAxisValue(MotionEvent.AXIS_RZ)
                )
            MotionEvent.AXIS_BRAKE ->
                NativeLibrary.onGamePadButtonEvent(
                    playerNumber,
                    NativeLibrary.ButtonType.TRIGGER_ZL,
                    getAxisToButton(event.getAxisValue(MotionEvent.AXIS_BRAKE))
                )
            MotionEvent.AXIS_GAS ->
                NativeLibrary.onGamePadButtonEvent(
                    playerNumber,
                    NativeLibrary.ButtonType.TRIGGER_ZR,
                    getAxisToButton(event.getAxisValue(MotionEvent.AXIS_GAS))
                )
            MotionEvent.AXIS_HAT_X, MotionEvent.AXIS_HAT_Y ->
                setAxisDpadState(
                    playerNumber,
                    event.getAxisValue(MotionEvent.AXIS_HAT_X),
                    event.getAxisValue(MotionEvent.AXIS_HAT_Y)
                )
        }
    }


}