2020-04-19 23:04:05 +02:00
|
|
|
// SPDX-License-Identifier: MPL-2.0
|
2020-03-27 20:36:02 +01:00
|
|
|
// Copyright © 2020 Skyline Team and Contributors (https://github.com/skyline-emu/)
|
|
|
|
|
2019-12-05 16:35:34 +01:00
|
|
|
#pragma once
|
|
|
|
|
2020-04-26 01:34:35 +02:00
|
|
|
#include "common.h"
|
2019-12-05 16:35:34 +01:00
|
|
|
#include <jni.h>
|
|
|
|
|
|
|
|
namespace skyline {
|
|
|
|
/**
|
|
|
|
* @brief The JvmManager class is used to simplify transactions with the Java component
|
|
|
|
*/
|
|
|
|
class JvmManager {
|
|
|
|
public:
|
2019-12-26 19:10:29 +01:00
|
|
|
JavaVM *vm{}; //!< A pointer to the Java VM
|
2019-12-05 16:35:34 +01:00
|
|
|
jobject instance; //!< A reference to the activity
|
|
|
|
jclass instanceClass; //!< The class of the activity
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @param env A pointer to the JNI environment
|
|
|
|
* @param instance A reference to the activity
|
|
|
|
*/
|
|
|
|
JvmManager(JNIEnv *env, jobject instance);
|
|
|
|
|
2020-09-05 01:06:07 +02:00
|
|
|
~JvmManager();
|
|
|
|
|
2019-12-26 19:10:29 +01:00
|
|
|
/**
|
|
|
|
* @brief Attach the current thread to the Java VM
|
|
|
|
*/
|
|
|
|
void AttachThread();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Detach the current thread to the Java VM
|
|
|
|
*/
|
|
|
|
void DetachThread();
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Returns a pointer to the JNI environment for the current thread
|
|
|
|
*/
|
2020-08-21 13:14:27 +02:00
|
|
|
static JNIEnv *GetEnv();
|
2019-12-26 19:10:29 +01:00
|
|
|
|
2019-12-05 16:35:34 +01:00
|
|
|
/**
|
|
|
|
* @brief Retrieves a specific field of the given type from the activity
|
|
|
|
* @tparam objectType The type of the object in the field
|
|
|
|
* @param key The name of the field in the activity class
|
|
|
|
* @return The contents of the field as objectType
|
|
|
|
*/
|
|
|
|
template<typename objectType>
|
|
|
|
inline objectType GetField(const char *key) {
|
2019-12-26 19:10:29 +01:00
|
|
|
JNIEnv *env = GetEnv();
|
2019-12-05 16:35:34 +01:00
|
|
|
if constexpr(std::is_same<objectType, jboolean>())
|
|
|
|
return env->GetBooleanField(instance, env->GetFieldID(instanceClass, key, "Z"));
|
|
|
|
else if constexpr(std::is_same<objectType, jbyte>())
|
|
|
|
return env->GetByteField(instance, env->GetFieldID(instanceClass, key, "B"));
|
|
|
|
else if constexpr(std::is_same<objectType, jchar>())
|
|
|
|
return env->GetCharField(instance, env->GetFieldID(instanceClass, key, "C"));
|
|
|
|
else if constexpr(std::is_same<objectType, jshort>())
|
|
|
|
return env->GetShortField(instance, env->GetFieldID(instanceClass, key, "S"));
|
|
|
|
else if constexpr(std::is_same<objectType, jint>())
|
|
|
|
return env->GetIntField(instance, env->GetFieldID(instanceClass, key, "I"));
|
|
|
|
else if constexpr(std::is_same<objectType, jlong>())
|
|
|
|
return env->GetLongField(instance, env->GetFieldID(instanceClass, key, "J"));
|
|
|
|
else if constexpr(std::is_same<objectType, jfloat>())
|
|
|
|
return env->GetFloatField(instance, env->GetFieldID(instanceClass, key, "F"));
|
|
|
|
else if constexpr(std::is_same<objectType, jdouble>())
|
|
|
|
return env->GetDoubleField(instance, env->GetFieldID(instanceClass, key, "D"));
|
|
|
|
}
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Retrieves a specific field from the activity as a jobject
|
|
|
|
* @param key The name of the field in the activity class
|
|
|
|
* @param signature The signature of the field
|
|
|
|
* @return A jobject of the contents of the field
|
|
|
|
*/
|
|
|
|
jobject GetField(const char *key, const char *signature);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Checks if a specific field from the activity is null or not
|
|
|
|
* @param key The name of the field in the activity class
|
|
|
|
* @param signature The signature of the field
|
|
|
|
* @return If the field is null or not
|
|
|
|
*/
|
|
|
|
bool CheckNull(const char *key, const char *signature);
|
2019-12-26 19:10:29 +01:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief Checks if a specific jobject is null or not
|
|
|
|
* @param object The jobject to check
|
|
|
|
* @return If the object is null or not
|
|
|
|
*/
|
2020-08-21 13:14:27 +02:00
|
|
|
static bool CheckNull(jobject &object);
|
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief A call to EmulationActivity.initializeControllers in Kotlin
|
|
|
|
*/
|
|
|
|
void InitializeControllers();
|
|
|
|
|
2020-09-05 01:06:07 +02:00
|
|
|
/**
|
|
|
|
* @brief A call to EmulationActivity.vibrateDevice in Kotlin
|
|
|
|
*/
|
2020-09-25 02:05:12 +02:00
|
|
|
void VibrateDevice(jint index, const span<jlong> &timings, const span<jint> &litudes);
|
2020-09-05 01:06:07 +02:00
|
|
|
|
|
|
|
/**
|
|
|
|
* @brief A call to EmulationActivity.clearVibrationDevice in Kotlin
|
|
|
|
*/
|
|
|
|
void ClearVibrationDevice(jint index);
|
|
|
|
|
2020-08-21 13:14:27 +02:00
|
|
|
private:
|
|
|
|
jmethodID initializeControllersId;
|
2020-09-05 01:06:07 +02:00
|
|
|
jmethodID vibrateDeviceId;
|
|
|
|
jmethodID clearVibrationDeviceId;
|
2019-12-05 16:35:34 +01:00
|
|
|
};
|
|
|
|
}
|