Skip to content

Commit

Permalink
Updates to internal APIs
Browse files Browse the repository at this point in the history
Update APIs for Speed
  • Loading branch information
Brandon-T committed Jan 23, 2024
1 parent 7e487dc commit 6f3c1b7
Show file tree
Hide file tree
Showing 15 changed files with 459 additions and 1,877 deletions.
1,381 changes: 52 additions & 1,329 deletions RemoteInput/JVM.cxx

Large diffs are not rendered by default.

236 changes: 1 addition & 235 deletions RemoteInput/JVM.hxx

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion RemoteInput/Platform/Platform.hxx
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
#include "Reflection.hxx"

void GetDesktopResolution(int &width, int &height) noexcept;
Reflection* GetNativeReflector() noexcept;
std::unique_ptr<Reflection> GetNativeReflector() noexcept;

std::int32_t GetCurrentThreadID() noexcept;

Expand Down
53 changes: 37 additions & 16 deletions RemoteInput/Platform/Platform_Darwin.mm
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,7 @@ bool IsThreadAlive(std::int32_t tid) noexcept
{
std::vector<std::int32_t> result;
std::vector<std::int32_t> pids = get_pids(process_name);

for (std::int32_t pid : pids)
{
if (InjectProcess(pid) != -1)
Expand Down Expand Up @@ -226,7 +227,7 @@ @interface NSView (MDRecursiveSubviews)
- (jobject)awtComponent:(JNIEnv*)env;
@end

Reflection* GetNativeReflector() noexcept
std::unique_ptr<Reflection> GetNativeReflector() noexcept
{
// auto ModuleLoaded = [](std::string name) -> bool {
// void* lib = dlopen(name.c_str(), RTLD_GLOBAL | RTLD_NOLOAD);
Expand Down Expand Up @@ -256,8 +257,11 @@ - (jobject)awtComponent:(JNIEnv*)env;
// return nullptr;
// }

auto IsValidFrame = [&](Reflection* reflection, jobject object) -> bool {
return reflection->IsDecendentOf(object, "java/awt/Frame");
auto IsValidFrame = [](JNIEnv* env, jobject object) -> bool {
jclass cls = env->FindClass("java/awt/Frame");
bool result = env->IsInstanceOf(object, cls);
env->DeleteLocalRef(cls);
return result;
};

auto GetWindowViews = [&]() -> std::vector<NSView*> {
Expand All @@ -281,12 +285,12 @@ - (jobject)awtComponent:(JNIEnv*)env;
return nullptr;
}

Reflection* reflection = new Reflection();

if (reflection->Attach())
JVM vm = JVM();
JNIEnv* env = nullptr;
if (vm.AttachCurrentThread(&env) == JNI_OK)
{
std::unique_ptr<Reflection> reflection;
auto hasReflection = TimeOut(20, [&]{
JNIEnv* env = reflection->getEnv();
jclass cls = env->FindClass("java/awt/Frame");
if (!cls)
{
Expand All @@ -300,6 +304,8 @@ - (jobject)awtComponent:(JNIEnv*)env;
}

jobjectArray frames = static_cast<jobjectArray>(env->CallStaticObjectMethod(cls, method));
env->DeleteLocalRef(cls);

if (!frames)
{
return false;
Expand All @@ -309,38 +315,53 @@ - (jobject)awtComponent:(JNIEnv*)env;
for (jsize i = 0; i < size; ++i)
{
jobject frame = env->GetObjectArrayElement(frames, i);
if (IsValidFrame(reflection, frame) && reflection->Initialize(frame))
if (frame)
{
return true;
if (IsValidFrame(env, frame))
{
reflection = Reflection::Create(frame);
if (reflection)
{
env->DeleteLocalRef(frames);
return true;
}
}

env->DeleteLocalRef(frame);
}
}

env->DeleteLocalRef(frames);
return false;
});

auto hasReflection2 = TimeOut(20, [&]{
for (auto&& view : GetWindowViews())
{
//TODO: Check if we can call "awt_GetComponent" from the JDK like on Linux
jobject object = [view awtComponent:reflection->getEnv()]; //java.awt.Frame
if (IsValidFrame(reflection, object) && reflection->Initialize(object))
jobject frame = [view awtComponent:env]; //java.awt.Frame
if (frame)
{
return true;
if (IsValidFrame(env, frame))
{
reflection = Reflection::Create(frame);
if (reflection)
{
return true;
}
}
}
}
return false;
});

if (hasReflection || hasReflection2)
{
reflection->Detach();
return reflection;
}

reflection->Detach();
}

delete reflection;
vm.DetachCurrentThread();
return nullptr;
}

Expand Down
57 changes: 43 additions & 14 deletions RemoteInput/Platform/Platform_Linux.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -490,7 +490,7 @@ bool EnumWindowsProc(Display* display, Window window, void* other) noexcept
return true;
}

Reflection* GetNativeReflector() noexcept
std::unique_ptr<Reflection> GetNativeReflector() noexcept
{
auto ModuleLoaded = [](std::string name) -> bool {
void* lib = dlopen(name.c_str(), RTLD_LAZY | RTLD_NOLOAD);
Expand All @@ -516,8 +516,11 @@ Reflection* GetNativeReflector() noexcept
return true;
};

auto IsValidFrame = [&](Reflection* reflection, jobject object) -> bool {
return reflection->IsDecendentOf(object, "java/awt/Frame");
auto IsValidFrame = [](JNIEnv* env, jobject object) -> bool {
jclass cls = env->FindClass("java/awt/Frame");
bool result = env->IsInstanceOf(object, cls);
env->DeleteLocalRef(cls);
return result;
};

if (!TimeOut(5, [&]{ return JVM().getVM() != nullptr; }))
Expand All @@ -537,11 +540,17 @@ Reflection* GetNativeReflector() noexcept
return 0;
};

Reflection* reflection = new Reflection();
if (reflection->Attach())
JVM vm = JVM();
if (!vm)
{
return nullptr;
}

JNIEnv* env = nullptr;
if (vm.AttachCurrentThread(&env) == JNI_OK)
{
std::unique_ptr<Reflection> reflection;
auto hasReflection = TimeOut(20, [&]{
JNIEnv* env = reflection->getEnv();
jclass cls = env->FindClass("java/awt/Frame");
if (!cls)
{
Expand All @@ -555,6 +564,7 @@ Reflection* GetNativeReflector() noexcept
}

jobjectArray frames = static_cast<jobjectArray>(env->CallStaticObjectMethod(cls, method));
env->DeleteLocalRef(cls);
if (!frames)
{
return false;
Expand All @@ -564,11 +574,23 @@ Reflection* GetNativeReflector() noexcept
for (jsize i = 0; i < size; ++i)
{
jobject frame = env->GetObjectArrayElement(frames, i);
if (IsValidFrame(reflection, frame) && reflection->Initialize(frame))
if (frame)
{
return true;
if (IsValidFrame(env, frame))
{
reflection = Reflection::Create(frame);
if (reflection)
{
env->DeleteLocalRef(frames);
return true;
}
}

env->DeleteLocalRef(frame);
}
}

env->DeleteLocalRef(frames);
return false;
});

Expand All @@ -588,23 +610,30 @@ Reflection* GetNativeReflector() noexcept
void* windowFrame = reinterpret_cast<void*>(GetMainWindow());
if (windowFrame)
{
jobject object = awt_GetComponent(reflection->getEnv(), windowFrame); //java.awt.Frame
return IsValidFrame(reflection, object) && reflection->Initialize(object);
jobject frame = awt_GetComponent(reflection->getEnv(), windowFrame); //java.awt.Frame
if (frame)
{
if (IsValidFrame(env, frame))
{
reflection = Reflection::Create(frame);
if (reflection)
{
return true;
}
}
}
}
return false;
});
});

if (hasReflection || hasReflection2)
{
reflection->Detach();
return reflection;
}

reflection->Detach();
}

delete reflection;
vm.DetachCurrentThread();
return nullptr;
}
#endif // defined
55 changes: 41 additions & 14 deletions RemoteInput/Platform/Platform_Windows.cxx
Original file line number Diff line number Diff line change
Expand Up @@ -337,7 +337,7 @@ BOOL CALLBACK EnumWindowsProc(HWND hwnd, LPARAM lParam) noexcept
return TRUE;
}

Reflection* GetNativeReflector() noexcept
std::unique_ptr<Reflection> GetNativeReflector() noexcept
{
auto TimeOut = [&](std::uint32_t time, std::function<bool()> &&run) -> bool {
auto start = std::chrono::high_resolution_clock::now();
Expand All @@ -353,8 +353,11 @@ Reflection* GetNativeReflector() noexcept
return true;
};

auto IsValidFrame = [&](Reflection* reflection, jobject object) -> bool {
return reflection->IsDecendentOf(object, "java/awt/Frame");
auto IsValidFrame = [](JNIEnv* env, jobject object) -> bool {
jclass cls = env->FindClass("java/awt/Frame");
bool result = env->IsInstanceOf(object, cls);
env->DeleteLocalRef(cls);
return result;
};

auto GetMainWindow = [&] {
Expand All @@ -374,12 +377,17 @@ Reflection* GetNativeReflector() noexcept
return nullptr;
}

Reflection* reflection = new Reflection();
JVM vm = JVM();
if (!vm)
{
return nullptr;
}

if (reflection->Attach())
JNIEnv* env = nullptr;
if (vm.AttachCurrentThread(&env) == JNI_OK)
{
std::unique_ptr<Reflection> reflection;
auto hasReflection = TimeOut(20, [&]{
JNIEnv* env = reflection->getEnv();
jclass cls = env->FindClass("java/awt/Frame");
if (!cls)
{
Expand All @@ -393,6 +401,7 @@ Reflection* GetNativeReflector() noexcept
}

jobjectArray frames = static_cast<jobjectArray>(env->CallStaticObjectMethod(cls, method));
env->DeleteLocalRef(cls);
if (!frames)
{
return false;
Expand All @@ -402,12 +411,23 @@ Reflection* GetNativeReflector() noexcept
for (jsize i = 0; i < size; ++i)
{
jobject frame = env->GetObjectArrayElement(frames, i);
if (IsValidFrame(reflection, frame) && reflection->Initialize(frame))
if (frame)
{
return true;
if (IsValidFrame(env, frame))
{
reflection = Reflection::Create(frame);
if (reflection)
{
env->DeleteLocalRef(frames);
return true;
}
}

env->DeleteLocalRef(frame);
}
}

env->DeleteLocalRef(frames);
return false;
});

Expand All @@ -434,23 +454,30 @@ Reflection* GetNativeReflector() noexcept
HWND windowFrame = GetMainWindow();
if (windowFrame)
{
jobject object = DSGetComponent(reflection->getEnv(), windowFrame); //java.awt.Frame
return IsValidFrame(reflection, object) && reflection->Initialize(object);
jobject frame = DSGetComponent(reflection->getEnv(), windowFrame); //java.awt.Frame
if (frame)
{
if (IsValidFrame(env, frame))
{
reflection = Reflection::Create(frame);
if (reflection)
{
return true;
}
}
}
}
return false;
});
});

if (hasReflection || hasReflection2)
{
reflection->Detach();
return reflection;
}

reflection->Detach();
}

delete reflection;
vm.DetachCurrentThread();
return nullptr;
}
#endif // defined
Loading

0 comments on commit 6f3c1b7

Please sign in to comment.