Skip to content

Commit

Permalink
update android platform code
Browse files Browse the repository at this point in the history
  • Loading branch information
qimiko committed May 19, 2024
1 parent b1f7c36 commit 1c50e85
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 10 deletions.
62 changes: 53 additions & 9 deletions src/android.cpp
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
#include <Geode/loader/Dispatch.hpp>
#include <Geode/cocos/platform/android/jni/JniHelper.h>
#include <time.h>

#include "input.hpp"
Expand All @@ -9,14 +9,58 @@ std::uint64_t platform_get_time() {
return (now.tv_sec * 1000) + (now.tv_nsec / 1'000'000);
}

$execute {
using namespace geode;
void clearJNIExceptions() {
auto vm = cocos2d::JniHelper::getJavaVM();

JNIEnv* env;
if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) == JNI_OK) {
env->ExceptionClear();
}
}

bool reportPlatformCapability(std::string id) {
cocos2d::JniMethodInfo t;
if (cocos2d::JniHelper::getStaticMethodInfo(t, "com/geode/launcher/utils/GeodeUtils", "reportPlatformCapability", "(Ljava/lang/String;)Z")) {
jstring stringArg1 = t.env->NewStringUTF(id.c_str());

auto r = t.env->CallStaticBooleanMethod(t.classID, t.methodID, stringArg1);

t.env->DeleteLocalRef(stringArg1);
t.env->DeleteLocalRef(t.classID);

return r;
} else {
clearJNIExceptions();
}

// TODO: stabilze some sort of input timestamp api
new EventListener(+[](std::uint64_t timestamp) {
ExtendedCCKeyboardDispatcher::setTimestamp(timestamp);
ExtendedCCTouchDispatcher::setTimestamp(timestamp);
return false;
}

void JNICALL JNI_setNextInputTimestamp(JNIEnv* env, jobject, jlong timestamp) {
auto timestampMs = timestamp / 1'000'000;
ExtendedCCKeyboardDispatcher::setTimestamp(timestampMs);
ExtendedCCTouchDispatcher::setTimestamp(timestampMs);
}

static JNINativeMethod methods[] = {
{
"setNextInputTimestamp",
"(J)V",
reinterpret_cast<void*>(&JNI_setNextInputTimestamp)
},
};

$execute {
auto vm = cocos2d::JniHelper::getJavaVM();

return ListenerResult::Propagate;
}, DispatchFilter<std::uint64_t>("geode.loader/android-next-input-timestamp"));
JNIEnv* env;
if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) == JNI_OK) {
auto clazz = env->FindClass("com/geode/launcher/utils/GeodeUtils");
if (env->RegisterNatives(clazz, methods, 1) != 0) {
// method was not found
clearJNIExceptions();
} else {
reportPlatformCapability("timestamp_inputs");
}
}
}
2 changes: 1 addition & 1 deletion src/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ void CustomGJBaseGameLayer::queueButton(int btnType, bool push, bool secondPlaye
#endif

#if DEBUG_STEPS
geode::log::debug("queueing input type={} down={} p2={} at time {}", btnType, push, secondPlayer, currentTime);
geode::log::debug("queueing input type={} down={} p2={} at time {} (ts {} -> {})", btnType, push, secondPlayer, currentTime, timeRelativeBegin, inputTimestamp);
#endif

// if you felt like it, you could calculate the step too
Expand Down

0 comments on commit 1c50e85

Please sign in to comment.