diff --git a/main.cpp b/main.cpp index 7f0a1898a..28b94e66f 100644 --- a/main.cpp +++ b/main.cpp @@ -3,7 +3,6 @@ #include #include #include "imgui.h" -#include "imgui_impl_android.h" #include "imgui_impl_opengl3.h" #include "dobby.h" #include @@ -21,7 +20,6 @@ static float g_Width = 0, g_Height = 0; struct TouchState { float x, y; bool down; - // 用于简单去抖或日志 int frameCounter; } g_Touch = {0, 0, false, 0}; @@ -39,18 +37,14 @@ typedef EGLBoolean (*p_eglSwapBuffers)(EGLDisplay dpy, EGLSurface surface); static p_eglSwapBuffers old_eglSwapBuffers = nullptr; // --------------------------------------------------------------------- -// 真正实时更新坐标和触摸状态的 Hook 函数 +// 实时更新坐标和触摸状态 // --------------------------------------------------------------------- - -// 辅助函数:从事件中提取动作并更新 down 状态 static void UpdateTouchFromEvent(const AInputEvent* event, size_t pointer_index) { if (!event || AInputEvent_getType(event) != AINPUT_EVENT_TYPE_MOTION) return; int32_t action = AMotionEvent_getAction(event) & AMOTION_EVENT_ACTION_MASK; - int32_t pointerId = AMotionEvent_getPointerId(event, pointer_index); - // 简单起见,只跟踪第一个手指 (pointer_index == 0) - // 如果游戏使用多指,可以根据需要扩展 + // 只跟踪第一个手指(通常菜单只需单点) if (pointer_index == 0) { if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_POINTER_DOWN) { g_Touch.down = true; @@ -58,39 +52,30 @@ static void UpdateTouchFromEvent(const AInputEvent* event, size_t pointer_index) action == AMOTION_EVENT_ACTION_CANCEL) { g_Touch.down = false; } - // MOVE 事件时保持 down 状态不变(已经为 true) } } -// Hook getRawX: 每次游戏获取 X 坐标时更新 x 和触摸状态 float hook_getRawX(const AInputEvent* event, size_t index) { if (event && old_getRawX) { - // 先获取原始 X 坐标 float x = old_getRawX(event, index); - // 更新全局触摸坐标 g_Touch.x = x; - // 更新触摸状态(down/up) UpdateTouchFromEvent(event, index); return x; } return 0.0f; } -// Hook getRawY: 每次游戏获取 Y 坐标时更新 y float hook_getRawY(const AInputEvent* event, size_t index) { if (event && old_getRawY) { float y = old_getRawY(event, index); g_Touch.y = y; - // 再次更新状态(可选,和上面重复但无害) UpdateTouchFromEvent(event, index); return y; } return 0.0f; } -// 保留对 getPointerId 的 Hook,但只转发调用,不再更新坐标(避免重复) int32_t hook_getPointerId(const AInputEvent* event, size_t index) { - // 简单转发,不做额外处理 return old_getPointerId ? old_getPointerId(event, index) : 0; } @@ -98,7 +83,6 @@ int32_t hook_getPointerId(const AInputEvent* event, size_t index) { // 渲染 Hook // --------------------------------------------------------------------- EGLBoolean hook_eglSwapBuffers(EGLDisplay dpy, EGLSurface surface) { - // 获取屏幕尺寸(仅一次或尺寸改变时更新) if (g_Width <= 0 || g_Height <= 0) { EGLint w, h; eglQuerySurface(dpy, surface, EGL_WIDTH, &w); @@ -108,33 +92,26 @@ EGLBoolean hook_eglSwapBuffers(EGLDisplay dpy, EGLSurface surface) { LOGI("Screen size: %.0f x %.0f", g_Width, g_Height); } - // 初始化 ImGui(只一次) if (!g_Initialized) { ImGui::CreateContext(); ImGui_ImplOpenGL3_Init("#version 300 es"); - // 可选:调整样式以适应触摸屏 - ImGui::GetStyle().TouchPadding = ImVec2(10, 10); - ImGui::GetStyle().ScaleAllSizes(2.5f); + // 移除不兼容的样式设置(TouchPadding 和 ScaleAllSizes) g_Initialized = true; LOGI("ImGui initialized"); } - // 开始 ImGui 帧 ImGui_ImplOpenGL3_NewFrame(); ImGui::NewFrame(); - // 更新 ImGui IO 的鼠标输入 ImGuiIO& io = ImGui::GetIO(); io.DisplaySize = ImVec2(g_Width, g_Height); io.MousePos = ImVec2(g_Touch.x, g_Touch.y); io.MouseDown[0] = g_Touch.down; - // 可选:打印调试信息(每 120 帧打印一次,避免刷屏) if (g_Touch.frameCounter++ % 120 == 0) { LOGI("Touch: (%.1f, %.1f) down=%d", g_Touch.x, g_Touch.y, g_Touch.down); } - // 绘制菜单 if (g_ShowMenu) { ImGui::SetNextWindowSize(ImVec2(500, 400), ImGuiCond_FirstUseEver); ImGui::SetNextWindowPos(ImVec2(50, 50), ImGuiCond_FirstUseEver); @@ -154,9 +131,7 @@ EGLBoolean hook_eglSwapBuffers(EGLDisplay dpy, EGLSurface surface) { } ImGui::End(); } else { - // 可选:显示一个小的重新打开按钮(例如通过音量键或触摸区域,这里简单通过坐标判断) - // 为了演示,添加一个“Show”按钮在左上角(即使菜单关闭也显示一个小区域) - // 实际使用时建议用系统按键回调,这里简化处理 + // 小窗口用于重新显示菜单 ImGui::SetNextWindowSize(ImVec2(100, 50), ImGuiCond_Always); ImGui::SetNextWindowPos(ImVec2(10, 10), ImGuiCond_Always); if (ImGui::Begin("Menu Controller", nullptr, ImGuiWindowFlags_NoTitleBar | ImGuiWindowFlags_NoResize)) { @@ -168,11 +143,9 @@ EGLBoolean hook_eglSwapBuffers(EGLDisplay dpy, EGLSurface surface) { ImGui::End(); } - // 渲染 ImGui::Render(); ImGui_ImplOpenGL3_RenderDrawData(ImGui::GetDrawData()); - // 调用原始 eglSwapBuffers return old_eglSwapBuffers(dpy, surface); } @@ -182,7 +155,6 @@ EGLBoolean hook_eglSwapBuffers(EGLDisplay dpy, EGLSurface surface) { void* init_thread(void*) { LOGI("Init thread started"); - // 1. Hook eglSwapBuffers(渲染) void* eglFunc = dlsym(RTLD_DEFAULT, "eglSwapBuffers"); if (eglFunc) { DobbyHook(eglFunc, (void*)hook_eglSwapBuffers, (void**)&old_eglSwapBuffers); @@ -191,47 +163,32 @@ void* init_thread(void*) { LOGE("Failed to find eglSwapBuffers"); } - // 2. Hook Android 输入函数(关键修复点) void* libandroid = dlopen("libandroid.so", RTLD_NOW); if (libandroid) { - // 获取需要 Hook 的函数地址 void* getPointerId = dlsym(libandroid, "AMotionEvent_getPointerId"); void* getRawX = dlsym(libandroid, "AMotionEvent_getRawX"); void* getRawY = dlsym(libandroid, "AMotionEvent_getRawY"); - // 如果找不到 RawX/RawY,回退到普通 getX/getY(相对坐标,可能不准) - if (!getRawX) { - getRawX = dlsym(libandroid, "AMotionEvent_getX"); - LOGI("Using AMotionEvent_getX as fallback"); - } - if (!getRawY) { - getRawY = dlsym(libandroid, "AMotionEvent_getY"); - LOGI("Using AMotionEvent_getY as fallback"); - } + if (!getRawX) getRawX = dlsym(libandroid, "AMotionEvent_getX"); + if (!getRawY) getRawY = dlsym(libandroid, "AMotionEvent_getY"); - // Hook getRawX 和 getRawY(这是实时更新坐标的关键) if (getRawX) { DobbyHook(getRawX, (void*)hook_getRawX, (void**)&old_getRawX); LOGI("Hooked getRawX at %p", getRawX); } else { - LOGE("Cannot find getX/getRawX function"); + LOGE("Cannot find getX/getRawX"); } if (getRawY) { DobbyHook(getRawY, (void*)hook_getRawY, (void**)&old_getRawY); LOGI("Hooked getRawY at %p", getRawY); } else { - LOGE("Cannot find getY/getRawY function"); + LOGE("Cannot find getY/getRawY"); } - - // Hook getPointerId 保持兼容(可选) if (getPointerId) { DobbyHook(getPointerId, (void*)hook_getPointerId, (void**)&old_getPointerId); LOGI("Hooked getPointerId at %p", getPointerId); } - - dlclose(libandroid); // 注意:dlclose 可能使 dlsym 返回的地址失效,但 DobbyHook 已复制指令,通常安全。建议不要 dlclose。 - // 更安全的做法是不调用 dlclose,保持 libandroid 加载。这里注释掉 dlclose。 - // dlclose(libandroid); + // 注意:不要 dlclose(libandroid),避免地址失效 } else { LOGE("Failed to load libandroid.so"); } @@ -240,12 +197,9 @@ void* init_thread(void*) { return nullptr; } -// --------------------------------------------------------------------- -// 入口点(构造函数) -// --------------------------------------------------------------------- __attribute__((constructor)) void entry() { pthread_t thread; pthread_create(&thread, nullptr, init_thread, nullptr); - pthread_detach(thread); // 让线程自动回收 + pthread_detach(thread); }