summaryrefslogtreecommitdiff
path: root/Source/Core/InputCommon/ControllerInterface/Android/Android.cpp
blob: 681641c2e3b4c92ba83cdcb18f6fefa4073ab7db (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
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
// Copyright 2021 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include "InputCommon/ControllerInterface/Android/Android.h"

#include <memory>
#include <string>
#include <unordered_map>
#include <utility>

#include <fmt/format.h>

#include <android/input.h>
#include <android/keycodes.h>
#include <jni.h>

#include "Common/Logging/Log.h"

#include "InputCommon/ControllerInterface/ControllerInterface.h"

#include "jni/AndroidCommon/AndroidCommon.h"
#include "jni/AndroidCommon/IDCache.h"

namespace
{
jclass s_list_class;
jmethodID s_list_get;
jmethodID s_list_size;

jclass s_input_device_class;
jmethodID s_input_device_get_device_ids;
jmethodID s_input_device_get_device;
jmethodID s_input_device_get_controller_number;
jmethodID s_input_device_get_motion_ranges;
jmethodID s_input_device_get_name;
jmethodID s_input_device_get_sources;
jmethodID s_input_device_has_keys;

jclass s_motion_range_class;
jmethodID s_motion_range_get_axis;
jmethodID s_motion_range_get_max;
jmethodID s_motion_range_get_min;
jmethodID s_motion_range_get_source;

jintArray s_keycodes_array;

constexpr int MAX_KEYCODE = AKEYCODE_PROFILE_SWITCH;  // Up to date as of SDK 31

const std::array<std::string_view, MAX_KEYCODE + 1> KEYCODE_NAMES = {
    "Unknown",
    "Soft Left",
    "Soft Right",
    "Home",
    "Back",
    "Call",
    "End Call",
    "0",
    "1",
    "2",
    "3",
    "4",
    "5",
    "6",
    "7",
    "8",
    "9",
    "Star",
    "Pound",
    "Up",
    "Down",
    "Left",
    "Right",
    "Center",
    "Volume Up",
    "Volume Down",
    "Power",
    "Camera",
    "Clear",
    "A",
    "B",
    "C",
    "D",
    "E",
    "F",
    "G",
    "H",
    "I",
    "J",
    "K",
    "L",
    "M",
    "N",
    "O",
    "P",
    "Q",
    "R",
    "S",
    "T",
    "U",
    "V",
    "W",
    "X",
    "Y",
    "Z",
    "Comma",
    "Period",
    "Left Alt",
    "Right Alt",
    "Left Shift",
    "Right Shift",
    "Tab",
    "Space",
    "Sym",
    "Explorer",
    "Envelope",
    "Enter",
    "Backspace",
    "Grave",
    "Minus",
    "Equals",
    "Left Bracket",
    "Right Bracket",
    "Backslash",
    "Semicolon",
    "Apostrophe",
    "Slash",
    "At",
    "Num",
    "Headset Hook",
    "Focus",
    "Plus",
    "Menu",
    "Notification",
    "Search",
    "Play Pause",
    "Stop",
    "Next",
    "Previous",
    "Rewind",
    "Fast Forward",
    "Mute",
    "Page Up",
    "Page Down",
    "Emoji",
    "Switch Charset",
    "Button A",
    "Button B",
    "Button C",
    "Button X",
    "Button Y",
    "Button Z",
    "Button L1",
    "Button R1",
    "Button L2",
    "Button R2",
    "Button L3",
    "Button R3",
    "Start",
    "Select",
    "Mode",
    "Escape",
    "Delete",
    "Left Ctrl",
    "Right Ctrl",
    "Caps Lock",
    "Scroll Lock",
    "Left Meta",
    "Right Meta",
    "Fn",
    "PrtSc SysRq",
    "Pause Break",
    "Move Home",
    "Move End",
    "Insert",
    "Forward",
    "Play",
    "Pause",
    "Close",
    "Eject",
    "Record",
    "F1",
    "F2",
    "F3",
    "F4",
    "F5",
    "F6",
    "F7",
    "F8",
    "F9",
    "F10",
    "F11",
    "F12",
    "Num Lock",
    "Numpad 0",
    "Numpad 1",
    "Numpad 2",
    "Numpad 3",
    "Numpad 4",
    "Numpad 5",
    "Numpad 6",
    "Numpad 7",
    "Numpad 8",
    "Numpad 9",
    "Numpad Divide",
    "Numpad Multiply",
    "Numpad Subtract",
    "Numpad Add",
    "Numpad Dot",
    "Numpad Comma",
    "Numpad Enter",
    "Numpad Equals",
    "Numpad Left Paren",
    "Numpad Right Paren",
    "Volume Mute",
    "Info",
    "Channel Up",
    "Channel Down",
    "Zoom In",
    "Zoom Out",
    "TV",
    "Window",
    "Guide",
    "DVR",
    "Bookmark",
    "Captions",
    "Settings",
    "TV Power",
    "TV Input",
    "STB Power",
    "STB Input",
    "AVR Power",
    "AVR Input",
    "Prog Red",
    "Prog Green",
    "Prog Yellow",
    "Prog Blue",
    "App Switch",
    "Button 1",
    "Button 2",
    "Button 3",
    "Button 4",
    "Button 5",
    "Button 6",
    "Button 7",
    "Button 8",
    "Button 9",
    "Button 10",
    "Button 11",
    "Button 12",
    "Button 13",
    "Button 14",
    "Button 15",
    "Button 16",
    "Language Switch",
    "Manner Mode",
    "3D Mode",
    "Contacts",
    "Calendar",
    "Music",
    "Calculator",
    "Zenkaku Hankaku",
    "Eisu",
    "Henkan",
    "Muhenkan",
    "Katakana Hiragana",
    "Yen",
    "Ro",
    "Kana",
    "Assist",
    "Brightness Down",
    "Brightness Up",
    "Audio Track",
    "Sleep",
    "Wakeup",
    "Pairing",
    "Top Menu",
    "11",
    "12",
    "Last Channel",
    "Data Service",
    "Voice Assist",
    "Radio Service",
    "Teletext",
    "Number Entry",
    "Terrestrial Analog",
    "Terrestrial Digital",
    "Satellite",
    "Satellite BS",
    "Satellite CS",
    "Satellite Service",
    "Network",
    "Antenna Cable",
    "Input HDMI 1",
    "Input HDMI 2",
    "Input HDMI 3",
    "Input HDMI 4",
    "Input Composite 1",
    "Input Composite 2",
    "Input Component 1",
    "Input Component 2",
    "Input VGA 1",
    "Audio Description",
    "Audio Description Mix Up",
    "Audio Description Mix Down",
    "Zoom Mode",
    "Contents Menu",
    "Media Context Menu",
    "Timer Programming",
    "Help",
    "Navigate Previous",
    "Navigate Next",
    "Navigate In",
    "Navigate Out",
    "Stem Primary",
    "Stem 1",
    "Stem 2",
    "Stem 3",
    "Up Left",
    "Down Left",
    "Up Right",
    "Down Right",
    "Skip Forward",
    "Skip Backward",
    "Step Forward",
    "Step Backward",
    "Soft Sleep",
    "Cut",
    "Copy",
    "Paste",
    "System Navigation Up",
    "System Navigation Down",
    "System Navigation Left",
    "System Navigation Right",
    "All Apps",
    "Refresh",
    "Thumbs Up",
    "Thumbs Down",
    "Profile Switch",
};

std::string ConstructKeyName(int keycode)
{
  return std::string(KEYCODE_NAMES[keycode]);
}

std::string ConstructAxisNamePrefix(int source)
{
  // A device is allowed to have two axes with the same axis ID but different source IDs,
  // so we have to make sure to include the source in the axis name.

  static const std::unordered_map<int, std::string> source_names{
      {AINPUT_SOURCE_KEYBOARD, "Keyboard"},
      {AINPUT_SOURCE_DPAD, "Dpad"},
      {AINPUT_SOURCE_GAMEPAD, "Gamepad"},
      {AINPUT_SOURCE_TOUCHSCREEN, "Touch"},
      {AINPUT_SOURCE_MOUSE, "Cursor"},
      {AINPUT_SOURCE_STYLUS, "Stylus"},
      {AINPUT_SOURCE_BLUETOOTH_STYLUS, "BTStylus"},
      {AINPUT_SOURCE_TRACKBALL, "Trackball"},
      {AINPUT_SOURCE_MOUSE_RELATIVE, "Mouse"},
      {AINPUT_SOURCE_TOUCHPAD, "Touchpad"},
      {AINPUT_SOURCE_TOUCH_NAVIGATION, "Touchnav"},
      {AINPUT_SOURCE_JOYSTICK, "Axis"},  // The typical source for all axes on a gamepad
      {AINPUT_SOURCE_HDMI, "HDMI"},
      {AINPUT_SOURCE_SENSOR, "Sensor"},
      {AINPUT_SOURCE_ROTARY_ENCODER, "Rotary"},
  };

  const auto it = source_names.find(source);
  if (it != source_names.end())
    return fmt::format("{} ", it->second);
  else
    return fmt::format("Axis {:08x}/", source);
}

std::string ConstructAxisName(int source, int axis, bool negative)
{
  const char sign = negative ? '-' : '+';
  return fmt::format("{}{}{}", ConstructAxisNamePrefix(source), axis, sign);
}

}  // namespace

namespace ciface::Android
{
class AndroidKey final : public Core::Device::Input
{
public:
  explicit AndroidKey(int keycode) : m_name(ConstructKeyName(keycode))
  {
    DEBUG_LOG_FMT(CONTROLLERINTERFACE, "Created {}", m_name);
  }

  std::string GetName() const override { return m_name; }

  ControlState GetState() const override
  {
    return 0;  // TODO
  }

private:
  std::string m_name;
};

class AndroidAxis final : public Core::Device::Input
{
public:
  AndroidAxis(int source, int axis, bool negative)
      : m_name(ConstructAxisName(source, axis, negative))
  {
    DEBUG_LOG_FMT(CONTROLLERINTERFACE, "Created {}", m_name);
  }

  std::string GetName() const override { return m_name; };

  ControlState GetState() const override
  {
    return 0;  // TODO
  };

private:
  std::string m_name;
};

class AndroidDevice final : public Core::Device
{
public:
  AndroidDevice(JNIEnv* env, jobject input_device)
      : m_source(env->CallIntMethod(input_device, s_input_device_get_sources)),
        m_controller_number(env->CallIntMethod(input_device, s_input_device_get_controller_number)
  {
    jstring j_name =
        reinterpret_cast<jstring>(env->CallObjectMethod(input_device, s_input_device_get_name));
    m_name = GetJString(env, j_name);
    env->DeleteLocalRef(j_name);

    DEBUG_LOG_FMT(CONTROLLERINTERFACE, "Sources for {}: {:08x}", GetQualifiedName(), m_source);

    AddKeys(env, input_device);
    AddAxes(env, input_device);
  }

  std::string GetName() const override { return m_name; }

  std::string GetSource() const override { return "Android"; }

  std::optional<int> GetPreferredId() const override
  {
    return m_controller_number != 0 ? std::make_optional(m_controller_number) : std::nullopt;
  }

  int GetSortPriority() const override
  {
    // If m_controller_number is non-zero, Android considers this to be a gamepad
    if (m_controller_number != 0)
      return 0;

    if ((m_source & AINPUT_SOURCE_KEYBOARD) != 0)
      return -1;

    if ((m_source & (AINPUT_SOURCE_MOUSE | AINPUT_SOURCE_MOUSE_RELATIVE)) != 0)
      return -2;

    return -3;
  }

private:
  void AddKeys(JNIEnv* env, jobject input_device)
  {
    jbooleanArray keys_array = reinterpret_cast<jbooleanArray>(
        env->CallObjectMethod(input_device, s_input_device_has_keys, s_keycodes_array));
    jboolean* keys = env->GetBooleanArrayElements(keys_array, nullptr);
    jsize keys_count = env->GetArrayLength(keys_array);
    for (jsize i = 0; i < keys_count; ++i)
    {
      // These specific keys never get delivered to applications,
      // so there's no point in letting users try to map them
      if (i == AKEYCODE_HOME || i == AKEYCODE_ASSIST || i == AKEYCODE_VOICE_ASSIST)
        continue;

      if (keys[i])
        AddInput(new AndroidKey(i));
    }
    env->ReleaseBooleanArrayElements(keys_array, keys, JNI_ABORT);
    env->DeleteLocalRef(keys_array);
  }

  void AddAxes(JNIEnv* env, jobject input_device)
  {
    jobject motion_ranges_list =
        env->CallObjectMethod(input_device, s_input_device_get_motion_ranges);
    jint motion_ranges_count = env->CallIntMethod(motion_ranges_list, s_list_size);
    for (jint i = 0; i < motion_ranges_count; ++i)
    {
      jobject motion_range = env->CallObjectMethod(motion_ranges_list, s_list_get, i);

      jint source = env->CallIntMethod(motion_range, s_motion_range_get_source);
      jint axis = env->CallIntMethod(motion_range, s_motion_range_get_axis);
      jfloat min = env->CallFloatMethod(motion_range, s_motion_range_get_min);
      jfloat max = env->CallFloatMethod(motion_range, s_motion_range_get_max);

      env->DeleteLocalRef(motion_range);

      AndroidAxis* positive = nullptr;
      AndroidAxis* negative = nullptr;
      if (max > 0)
        positive = new AndroidAxis(source, axis, false);
      if (min < 0)
        negative = new AndroidAxis(source, axis, true);

      if (positive && negative)
        AddAnalogInputs(positive, negative);
      else if (positive || negative)
        AddInput(positive ? positive : negative);
    }
    env->DeleteLocalRef(motion_ranges_list);
  }

  const int m_source;
  const int m_controller_number;
  std::string m_name;
};

// Creates an array that contains every possible keycode
static jintArray CreateKeyCodesArray(JNIEnv* env)
{
  jintArray keycodes_array = env->NewIntArray(MAX_KEYCODE + 1);

  int* keycodes = env->GetIntArrayElements(keycodes_array, nullptr);
  for (int i = 0; i <= MAX_KEYCODE; ++i)
    keycodes[i] = i;
  env->ReleaseIntArrayElements(keycodes_array, keycodes, 0);

  return keycodes_array;
}

void Init()
{
  JNIEnv* env = IDCache::GetEnvForThread();

  const jclass list_class = env->FindClass("java/util/List");
  s_list_class = reinterpret_cast<jclass>(env->NewGlobalRef(list_class));
  s_list_get = env->GetMethodID(s_list_class, "get", "(I)Ljava/lang/Object;");
  s_list_size = env->GetMethodID(s_list_class, "size", "()I");
  env->DeleteLocalRef(list_class);

  const jclass input_device_class = env->FindClass("android/view/InputDevice");
  s_input_device_class = reinterpret_cast<jclass>(env->NewGlobalRef(input_device_class));
  s_input_device_get_device_ids =
      env->GetStaticMethodID(s_input_device_class, "getDeviceIds", "()[I");
  s_input_device_get_device =
      env->GetStaticMethodID(s_input_device_class, "getDevice", "(I)Landroid/view/InputDevice;");
  s_input_device_get_controller_number =
      env->GetMethodID(s_input_device_class, "getControllerNumber", "()I");
  s_input_device_get_motion_ranges =
      env->GetMethodID(s_input_device_class, "getMotionRanges", "()Ljava/util/List;");
  s_input_device_get_name =
      env->GetMethodID(s_input_device_class, "getName", "()Ljava/lang/String;");
  s_input_device_get_sources = env->GetMethodID(s_input_device_class, "getSources", "()I");
  s_input_device_has_keys = env->GetMethodID(s_input_device_class, "hasKeys", "([I)[Z");
  env->DeleteLocalRef(input_device_class);

  const jclass motion_range_class = env->FindClass("android/view/InputDevice$MotionRange");
  s_motion_range_class = reinterpret_cast<jclass>(env->NewGlobalRef(motion_range_class));
  s_motion_range_get_axis = env->GetMethodID(s_motion_range_class, "getAxis", "()I");
  s_motion_range_get_max = env->GetMethodID(s_motion_range_class, "getMax", "()F");
  s_motion_range_get_min = env->GetMethodID(s_motion_range_class, "getMin", "()F");
  s_motion_range_get_source = env->GetMethodID(s_motion_range_class, "getSource", "()I");
  env->DeleteLocalRef(motion_range_class);

  jintArray keycodes_array = CreateKeyCodesArray(env);
  s_keycodes_array = reinterpret_cast<jintArray>(env->NewGlobalRef(keycodes_array));
  env->DeleteLocalRef(keycodes_array);
}

void Shutdown()
{
  JNIEnv* env = IDCache::GetEnvForThread();

  env->DeleteGlobalRef(s_input_device_class);
  env->DeleteGlobalRef(s_motion_range_class);
  env->DeleteGlobalRef(s_keycodes_array);
}

static void AddDevice(JNIEnv* env, int device_id)
{
  jobject input_device =
      env->CallStaticObjectMethod(s_input_device_class, s_input_device_get_device, device_id);

  auto device = std::make_shared<AndroidDevice>(env, input_device);

  env->DeleteLocalRef(input_device);

  if (!device->Inputs().empty() || !device->Outputs().empty())
    g_controller_interface.AddDevice(std::move(device));
}

void PopulateDevices()
{
  INFO_LOG_FMT(CONTROLLERINTERFACE, "Android populating devices");

  JNIEnv* env = IDCache::GetEnvForThread();

  jintArray device_ids_array = reinterpret_cast<jintArray>(
      env->CallStaticObjectMethod(s_input_device_class, s_input_device_get_device_ids));
  int* device_ids = env->GetIntArrayElements(device_ids_array, nullptr);
  jsize device_ids_count = env->GetArrayLength(device_ids_array);
  for (jsize i = 0; i < device_ids_count; ++i)
    AddDevice(env, device_ids[i]);
  env->ReleaseIntArrayElements(device_ids_array, device_ids, JNI_ABORT);
  env->DeleteLocalRef(device_ids_array);
}

}  // namespace ciface::Android