summaryrefslogtreecommitdiffstats
path: root/mobile/android/geckoview/src/main/java/org/mozilla/gecko/AndroidGamepadManager.java
blob: 44aa7bc46126d344d9b814df7399ebd88056aed2 (plain)
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
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
/* -*- Mode: Java; c-basic-offset: 4; tab-width: 4; indent-tabs-mode: nil; -*-
 * This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

package org.mozilla.gecko;

import android.content.Context;
import android.hardware.input.InputManager;
import android.util.SparseArray;
import android.view.InputDevice;
import android.view.KeyEvent;
import android.view.MotionEvent;
import java.util.ArrayList;
import java.util.List;
import java.util.Timer;
import org.mozilla.gecko.annotation.WrapForJNI;
import org.mozilla.gecko.util.ThreadUtils;

public class AndroidGamepadManager {
  // This is completely arbitrary.
  private static final float TRIGGER_PRESSED_THRESHOLD = 0.25f;
  private static final long POLL_TIMER_PERIOD = 1000; // milliseconds

  private static enum Axis {
    X(MotionEvent.AXIS_X),
    Y(MotionEvent.AXIS_Y),
    Z(MotionEvent.AXIS_Z),
    RZ(MotionEvent.AXIS_RZ);

    public final int axis;

    private Axis(final int axis) {
      this.axis = axis;
    }
  }

  // A list of gamepad button mappings. Axes are determined at
  // runtime, as they vary by Android version.
  private static enum Trigger {
    Left(6),
    Right(7);

    public final int button;

    private Trigger(final int button) {
      this.button = button;
    }
  }

  private static final int FIRST_DPAD_BUTTON = 12;

  // A list of axis number, gamepad button mappings for negative, positive.
  // Button mappings are added to FIRST_DPAD_BUTTON.
  private static enum DpadAxis {
    UpDown(MotionEvent.AXIS_HAT_Y, 0, 1),
    LeftRight(MotionEvent.AXIS_HAT_X, 2, 3);

    public final int axis;
    public final int negativeButton;
    public final int positiveButton;

    private DpadAxis(final int axis, final int negativeButton, final int positiveButton) {
      this.axis = axis;
      this.negativeButton = negativeButton;
      this.positiveButton = positiveButton;
    }
  }

  private static enum Button {
    A(KeyEvent.KEYCODE_BUTTON_A),
    B(KeyEvent.KEYCODE_BUTTON_B),
    X(KeyEvent.KEYCODE_BUTTON_X),
    Y(KeyEvent.KEYCODE_BUTTON_Y),
    L1(KeyEvent.KEYCODE_BUTTON_L1),
    R1(KeyEvent.KEYCODE_BUTTON_R1),
    L2(KeyEvent.KEYCODE_BUTTON_L2),
    R2(KeyEvent.KEYCODE_BUTTON_R2),
    SELECT(KeyEvent.KEYCODE_BUTTON_SELECT),
    START(KeyEvent.KEYCODE_BUTTON_START),
    THUMBL(KeyEvent.KEYCODE_BUTTON_THUMBL),
    THUMBR(KeyEvent.KEYCODE_BUTTON_THUMBR),
    DPAD_UP(KeyEvent.KEYCODE_DPAD_UP),
    DPAD_DOWN(KeyEvent.KEYCODE_DPAD_DOWN),
    DPAD_LEFT(KeyEvent.KEYCODE_DPAD_LEFT),
    DPAD_RIGHT(KeyEvent.KEYCODE_DPAD_RIGHT);

    public final int button;

    private Button(final int button) {
      this.button = button;
    }
  }

  private static class Gamepad {
    // ID from GamepadService
    public byte[] handle;
    // Retain axis state so we can determine changes.
    public float axes[];
    public boolean dpad[];
    public int triggerAxes[];
    public float triggers[];

    public Gamepad(final byte[] handle, final int deviceId) {
      this.handle = handle;
      axes = new float[Axis.values().length];
      dpad = new boolean[4];
      triggers = new float[2];

      final InputDevice device = InputDevice.getDevice(deviceId);
      if (device != null) {
        // LTRIGGER/RTRIGGER don't seem to be exposed on older
        // versions of Android.
        if (device.getMotionRange(MotionEvent.AXIS_LTRIGGER) != null
            && device.getMotionRange(MotionEvent.AXIS_RTRIGGER) != null) {
          triggerAxes = new int[] {MotionEvent.AXIS_LTRIGGER, MotionEvent.AXIS_RTRIGGER};
        } else if (device.getMotionRange(MotionEvent.AXIS_BRAKE) != null
            && device.getMotionRange(MotionEvent.AXIS_GAS) != null) {
          triggerAxes = new int[] {MotionEvent.AXIS_BRAKE, MotionEvent.AXIS_GAS};
        } else {
          triggerAxes = null;
        }
      }
    }
  }

  @WrapForJNI(calledFrom = "ui")
  private static native byte[] nativeAddGamepad();

  @WrapForJNI(calledFrom = "ui")
  private static native void nativeRemoveGamepad(byte[] aGamepadHandle);

  @WrapForJNI(calledFrom = "ui")
  private static native void onButtonChange(
      byte[] aGamepadHandle, int aButton, boolean aPressed, float aValue);

  @WrapForJNI(calledFrom = "ui")
  private static native void onAxisChange(byte[] aGamepadHandle, boolean[] aValid, float[] aValues);

  private static boolean sStarted;
  private static final SparseArray<Gamepad> sGamepads = new SparseArray<>();
  private static final SparseArray<List<KeyEvent>> sPendingGamepads = new SparseArray<>();
  private static InputManager.InputDeviceListener sListener;
  private static Timer sPollTimer;

  private AndroidGamepadManager() {}

  @WrapForJNI
  private static void start(final Context context) {
    ThreadUtils.runOnUiThread(
        new Runnable() {
          @Override
          public void run() {
            doStart(context);
          }
        });
  }

  /* package */ static void doStart(final Context context) {
    ThreadUtils.assertOnUiThread();
    if (!sStarted) {
      scanForGamepads();
      addDeviceListener(context);
      sStarted = true;
    }
  }

  @WrapForJNI
  private static void stop(final Context context) {
    ThreadUtils.runOnUiThread(
        new Runnable() {
          @Override
          public void run() {
            doStop(context);
          }
        });
  }

  /* package */ static void doStop(final Context context) {
    ThreadUtils.assertOnUiThread();
    if (sStarted) {
      removeDeviceListener(context);
      sPendingGamepads.clear();
      sGamepads.clear();
      sStarted = false;
    }
  }

  /* package */ static void handleGamepadAdded(final int deviceId, final byte[] gamepadHandle) {
    ThreadUtils.assertOnUiThread();
    if (!sStarted) {
      return;
    }

    final List<KeyEvent> pending = sPendingGamepads.get(deviceId);
    if (pending == null) {
      removeGamepad(deviceId);
      return;
    }

    sPendingGamepads.remove(deviceId);
    sGamepads.put(deviceId, new Gamepad(gamepadHandle, deviceId));
    // Handle queued KeyEvents
    for (final KeyEvent ev : pending) {
      handleKeyEvent(ev);
    }
  }

  private static float sDeadZoneThresholdOverride = 1e-2f;

  private static boolean isValueInDeadZone(final MotionEvent event, final int axis) {
    final float threshold;
    if (sDeadZoneThresholdOverride >= 0) {
      threshold = sDeadZoneThresholdOverride;
    } else {
      final InputDevice.MotionRange range = event.getDevice().getMotionRange(axis);
      threshold = range.getFlat() + range.getFuzz();
    }
    final float value = event.getAxisValue(axis);
    return (Math.abs(value) < threshold);
  }

  private static float deadZone(final MotionEvent ev, final int axis) {
    if (isValueInDeadZone(ev, axis)) {
      return 0.0f;
    }
    return ev.getAxisValue(axis);
  }

  private static void mapDpadAxis(
      final Gamepad gamepad, final boolean pressed, final float value, final int which) {
    if (pressed != gamepad.dpad[which]) {
      gamepad.dpad[which] = pressed;
      onButtonChange(gamepad.handle, FIRST_DPAD_BUTTON + which, pressed, Math.abs(value));
    }
  }

  public static boolean handleMotionEvent(final MotionEvent ev) {
    ThreadUtils.assertOnUiThread();
    if (!sStarted) {
      return false;
    }

    final Gamepad gamepad = sGamepads.get(ev.getDeviceId());
    if (gamepad == null) {
      // Not a device we care about.
      return false;
    }

    // First check the analog stick axes
    final boolean[] valid = new boolean[Axis.values().length];
    final float[] axes = new float[Axis.values().length];
    boolean anyValidAxes = false;
    for (final Axis axis : Axis.values()) {
      final float value = deadZone(ev, axis.axis);
      final int i = axis.ordinal();
      if (value != gamepad.axes[i]) {
        axes[i] = value;
        gamepad.axes[i] = value;
        valid[i] = true;
        anyValidAxes = true;
      }
    }
    if (anyValidAxes) {
      // Send an axismove event.
      onAxisChange(gamepad.handle, valid, axes);
    }

    // Map triggers to buttons.
    if (gamepad.triggerAxes != null) {
      for (final Trigger trigger : Trigger.values()) {
        final int i = trigger.ordinal();
        final int axis = gamepad.triggerAxes[i];
        final float value = deadZone(ev, axis);
        if (value != gamepad.triggers[i]) {
          gamepad.triggers[i] = value;
          final boolean pressed = value > TRIGGER_PRESSED_THRESHOLD;
          onButtonChange(gamepad.handle, trigger.button, pressed, value);
        }
      }
    }
    // Map d-pad to buttons.
    for (final DpadAxis dpadaxis : DpadAxis.values()) {
      final float value = deadZone(ev, dpadaxis.axis);
      mapDpadAxis(gamepad, value < 0.0f, value, dpadaxis.negativeButton);
      mapDpadAxis(gamepad, value > 0.0f, value, dpadaxis.positiveButton);
    }
    return true;
  }

  public static boolean handleKeyEvent(final KeyEvent ev) {
    ThreadUtils.assertOnUiThread();
    if (!sStarted) {
      return false;
    }

    final int deviceId = ev.getDeviceId();
    final List<KeyEvent> pendingGamepad = sPendingGamepads.get(deviceId);
    if (pendingGamepad != null) {
      // Queue up key events for pending devices.
      pendingGamepad.add(ev);
      return true;
    }

    if (sGamepads.get(deviceId) == null) {
      final InputDevice device = ev.getDevice();
      if (device != null
          && (device.getSources() & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD) {
        // This is a gamepad we haven't seen yet.
        addGamepad(device);
        sPendingGamepads.get(deviceId).add(ev);
        return true;
      }
      // Not a device we care about.
      return false;
    }

    int key = -1;
    for (final Button button : Button.values()) {
      if (button.button == ev.getKeyCode()) {
        key = button.ordinal();
        break;
      }
    }
    if (key == -1) {
      // Not a key we know how to handle.
      return false;
    }
    if (ev.getRepeatCount() > 0) {
      // We would handle this key, but we're not interested in
      // repeats. Eat it.
      return true;
    }

    final Gamepad gamepad = sGamepads.get(deviceId);
    final boolean pressed = ev.getAction() == KeyEvent.ACTION_DOWN;
    onButtonChange(gamepad.handle, key, pressed, pressed ? 1.0f : 0.0f);
    return true;
  }

  private static void scanForGamepads() {
    final int[] deviceIds = InputDevice.getDeviceIds();
    if (deviceIds == null) {
      return;
    }
    for (int i = 0; i < deviceIds.length; i++) {
      final InputDevice device = InputDevice.getDevice(deviceIds[i]);
      if (device == null) {
        continue;
      }
      if ((device.getSources() & InputDevice.SOURCE_GAMEPAD) != InputDevice.SOURCE_GAMEPAD) {
        continue;
      }
      addGamepad(device);
    }
  }

  private static void addGamepad(final InputDevice device) {
    sPendingGamepads.put(device.getId(), new ArrayList<KeyEvent>());
    final byte[] gamepadId = nativeAddGamepad();
    ThreadUtils.runOnUiThread(
        new Runnable() {
          @Override
          public void run() {
            handleGamepadAdded(device.getId(), gamepadId);
          }
        });
  }

  private static void removeGamepad(final int deviceId) {
    final Gamepad gamepad = sGamepads.get(deviceId);
    nativeRemoveGamepad(gamepad.handle);
    sGamepads.remove(deviceId);
  }

  private static void addDeviceListener(final Context context) {
    sListener =
        new InputManager.InputDeviceListener() {
          @Override
          public void onInputDeviceAdded(final int deviceId) {
            final InputDevice device = InputDevice.getDevice(deviceId);
            if (device == null) {
              return;
            }
            if ((device.getSources() & InputDevice.SOURCE_GAMEPAD) == InputDevice.SOURCE_GAMEPAD) {
              addGamepad(device);
            }
          }

          @Override
          public void onInputDeviceRemoved(final int deviceId) {
            if (sPendingGamepads.get(deviceId) != null) {
              // Got removed before Gecko's ack reached us.
              // gamepadAdded will deal with it.
              sPendingGamepads.remove(deviceId);
              return;
            }
            if (sGamepads.get(deviceId) != null) {
              removeGamepad(deviceId);
            }
          }

          @Override
          public void onInputDeviceChanged(final int deviceId) {}
        };
    final InputManager im = (InputManager) context.getSystemService(Context.INPUT_SERVICE);
    im.registerInputDeviceListener(sListener, ThreadUtils.getUiHandler());
  }

  private static void removeDeviceListener(final Context context) {
    final InputManager im = (InputManager) context.getSystemService(Context.INPUT_SERVICE);
    im.unregisterInputDeviceListener(sListener);
    sListener = null;
  }
}