summaryrefslogtreecommitdiff
path: root/Source/Android/jni/Input/InputDetector.cpp
blob: 03ad75ba1e253493c66995754b2e678790940da6 (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
// Copyright 2025 Dolphin Emulator Project
// SPDX-License-Identifier: GPL-2.0-or-later

#include <jni.h>

#include "InputCommon/ControllerInterface/ControllerInterface.h"
#include "InputCommon/ControllerInterface/CoreDevice.h"
#include "InputCommon/ControllerInterface/MappingCommon.h"
#include "jni/AndroidCommon/AndroidCommon.h"
#include "jni/AndroidCommon/IDCache.h"

namespace
{
constexpr auto INPUT_DETECT_INITIAL_TIME = std::chrono::seconds(3);
constexpr auto INPUT_DETECT_CONFIRMATION_TIME = std::chrono::milliseconds(0);
constexpr auto INPUT_DETECT_MAXIMUM_TIME = std::chrono::seconds(5);
}  // namespace

static ciface::Core::InputDetector* GetPointer(JNIEnv* env, jobject obj)
{
  return reinterpret_cast<ciface::Core::InputDetector*>(
      env->GetLongField(obj, IDCache::GetInputDetectorPointer()));
}

extern "C" {

JNIEXPORT void JNICALL
Java_org_dolphinemu_dolphinemu_features_input_model_InputDetector_finalize(JNIEnv* env, jobject obj)
{
  delete GetPointer(env, obj);
}

JNIEXPORT jlong JNICALL
Java_org_dolphinemu_dolphinemu_features_input_model_InputDetector_createNew(JNIEnv*, jobject)
{
  return reinterpret_cast<jlong>(new ciface::Core::InputDetector);
}

JNIEXPORT void JNICALL Java_org_dolphinemu_dolphinemu_features_input_model_InputDetector_start(
    JNIEnv* env, jobject obj, jstring j_default_device, jboolean all_devices)
{
  std::vector<std::string> device_strings;
  if (all_devices)
    device_strings = g_controller_interface.GetAllDeviceStrings();
  else
    device_strings = {GetJString(env, j_default_device)};

  GetPointer(env, obj)->Start(g_controller_interface, device_strings);
}

JNIEXPORT void JNICALL
Java_org_dolphinemu_dolphinemu_features_input_model_InputDetector_update(JNIEnv* env, jobject obj)
{
  GetPointer(env, obj)->Update(INPUT_DETECT_INITIAL_TIME, INPUT_DETECT_CONFIRMATION_TIME,
                               INPUT_DETECT_MAXIMUM_TIME);
}

JNIEXPORT jboolean JNICALL
Java_org_dolphinemu_dolphinemu_features_input_model_InputDetector_isComplete(JNIEnv* env,
                                                                             jobject obj)
{
  return GetPointer(env, obj)->IsComplete();
}

JNIEXPORT jstring JNICALL
Java_org_dolphinemu_dolphinemu_features_input_model_InputDetector_takeResults(
    JNIEnv* env, jobject obj, jstring j_default_device)
{
  ciface::Core::DeviceQualifier default_device;
  default_device.FromString(GetJString(env, j_default_device));

  auto detections = GetPointer(env, obj)->TakeResults();

  ciface::MappingCommon::RemoveSpuriousTriggerCombinations(&detections);

  return ToJString(env, ciface::MappingCommon::BuildExpression(detections, default_device,
                                                               ciface::MappingCommon::Quote::On));
}
}