Update main.cpp

This commit is contained in:
gggaa2152 2026-04-06 18:30:29 +08:00 committed by GitHub
parent 06dc4f89c4
commit 9bff1dff46
No known key found for this signature in database
GPG Key ID: B5690EEEBB952194

115
main.cpp
View File

@ -9,53 +9,64 @@
#include "imgui_impl_opengl3.h"
#include "dobby.h"
#include <android/log.h>
#include <android/input.h>
#define LOG_TAG "AndKitty_Menu"
#define LOG_TAG "AndKitty_Final"
#define LOGI(...) __android_log_print(ANDROID_LOG_INFO, LOG_TAG, __VA_ARGS__)
// --- 全局状态 ---
// --- 状态存储 ---
static bool g_Initialized = false;
static bool g_ShowMenu = true;
static float g_Width = 0, g_Height = 0;
// 记录触摸
struct {
float x, y;
bool down;
} g_Touch = {0, 0, false};
} g_TouchData = {0, 0, false};
// --- 原函数指针 ---
typedef int32_t (*p_getPointerId)(const AInputEvent* motion_event, size_t pointer_index);
static p_getPointerId old_getPointerId = nullptr;
// 对应日志中的符号: android::InputConsumer::consume(...)
typedef int (*p_InputConsumer_consume)(void* instance, void* factory, bool consumeBatches, nsecs_t frameTime, uint32_t* outSeq, void** outEvent);
static p_InputConsumer_consume old_consume = nullptr;
typedef EGLBoolean (*p_eglSwapBuffers)(EGLDisplay dpy, EGLSurface surface);
static p_eglSwapBuffers old_eglSwapBuffers = nullptr;
// --- 稳定的坐标获取方案 ---
int32_t hook_getPointerId(const AInputEvent* event, size_t index) {
if (g_ShowMenu && event != nullptr) {
if (AInputEvent_getType(event) == AINPUT_EVENT_TYPE_MOTION) {
int32_t action = AMotionEvent_getAction(event) & AMOTION_EVENT_ACTION_MASK;
// 获取手指 0 的绝对坐标
float x = AMotionEvent_getX(event, 0);
float y = AMotionEvent_getY(event, 0);
// --- 核心底层 Hook ---
int hook_InputConsumer_consume(void* instance, void* factory, bool consumeBatches, nsecs_t frameTime, uint32_t* outSeq, void** outEvent) {
// 先执行原函数,获取事件
int result = old_consume(instance, factory, consumeBatches, frameTime, outSeq, outEvent);
// 过滤非法值,防止之前出现的天文数字
if (x >= 0 && y >= 0 && x < 4000 && y < 4000) {
g_Touch.x = x;
g_Touch.y = y;
// result == 0 说明成功消耗了一个事件 (OK)
if (result == 0 && outEvent && *outEvent) {
void* event = *outEvent;
// 这里的 event 实际上是 android::MotionEvent*
// 我们通过导入 libandroid.so 的函数来解析这个对象,这是最稳妥的,不需要管偏移
typedef int (*p_getType)(void*);
typedef int (*p_getAction)(void*);
typedef float (*p_getX)(void*, size_t);
typedef float (*p_getY)(void*, size_t);
if (action == AMOTION_EVENT_ACTION_DOWN || action == AMOTION_EVENT_ACTION_POINTER_DOWN) {
g_Touch.down = true;
} else if (action == AMOTION_EVENT_ACTION_UP || action == AMOTION_EVENT_ACTION_POINTER_UP || action == AMOTION_EVENT_ACTION_CANCEL) {
g_Touch.down = false;
}
static p_getType _getType = (p_getType)dlsym(RTLD_DEFAULT, "AInputEvent_getType");
static p_getAction _getAction = (p_getAction)dlsym(RTLD_DEFAULT, "AMotionEvent_getAction");
static p_getX _getX = (p_getX)dlsym(RTLD_DEFAULT, "AMotionEvent_getX");
static p_getY _getY = (p_getY)dlsym(RTLD_DEFAULT, "AMotionEvent_getY");
if (_getType && _getType(event) == 1) { // AINPUT_EVENT_TYPE_MOTION
int action = _getAction(event) & 0xFF; // AMOTION_EVENT_ACTION_MASK
float x = _getX(event, 0);
float y = _getY(event, 0);
if (x >= 0 && y >= 0) {
g_TouchData.x = x;
g_TouchData.y = y;
if (action == 0) g_TouchData.down = true; // ACTION_DOWN
else if (action == 1) g_TouchData.down = false; // ACTION_UP
else if (action == 3) g_TouchData.down = false; // ACTION_CANCEL
}
}
}
return old_getPointerId(event, index);
return result;
}
// --- 渲染逻辑 ---
@ -72,42 +83,27 @@ EGLBoolean hook_eglSwapBuffers(EGLDisplay dpy, EGLSurface surface) {
ImGuiIO& io = ImGui::GetIO();
io.DisplaySize = ImVec2(g_Width, g_Height);
io.ConfigFlags |= ImGuiConfigFlags_IsTouchScreen;
ImGui_ImplOpenGL3_Init("#version 300 es");
// 注意:不调用 ImGui_ImplAndroid_Init,我们手动处理 IO
ImGui::StyleColorsDark();
ImGui::GetStyle().ScaleAllSizes(3.0f);
g_Initialized = true;
LOGI("ImGui Simple Ready: %.fx%.f", g_Width, g_Height);
}
// 核心:强制同步 IO 状态
// 强制同步底层拦截到的坐标
ImGuiIO& io = ImGui::GetIO();
io.MousePos = ImVec2(g_Touch.x, g_Touch.y);
io.MouseDown[0] = g_Touch.down;
// 固定的 DeltaTime 保证拖拽逻辑不失效
io.MousePos = ImVec2(g_TouchData.x, g_TouchData.y);
io.MouseDown[0] = g_TouchData.down;
io.DeltaTime = 1.0f / 60.0f;
ImGui_ImplOpenGL3_NewFrame();
ImGui::NewFrame();
if (g_ShowMenu) {
ImGui::SetNextWindowPos(ImVec2(100, 100), ImGuiCond_FirstUseEver);
ImGui::SetNextWindowSize(ImVec2(600, 450), ImGuiCond_FirstUseEver);
if (ImGui::Begin("AndKitty SGame Lite", &g_ShowMenu)) {
ImGui::Text("Diagnosis:");
ImGui::BulletText("Pos: %.1f, %.1f", io.MousePos.x, io.MousePos.y);
ImGui::BulletText("Down: %s", io.MouseDown[0] ? "True" : "False");
ImGui::Separator();
static float f = 0.5f;
ImGui::SliderFloat("Slider Test", &f, 0.0f, 1.0f);
if (ImGui::Button("Close Menu")) g_ShowMenu = false;
ImGui::SetNextWindowSize(ImVec2(500, 400), ImGuiCond_FirstUseEver);
if (ImGui::Begin("AndKitty SGame Final", &g_ShowMenu)) {
ImGui::Text("Touch Pos: %.1f, %.1f", g_TouchData.x, g_TouchData.y);
ImGui::Text("Status: %s", g_TouchData.down ? "DOWN" : "UP");
static float val = 0.5f;
ImGui::SliderFloat("Slider", &val, 0.0f, 1.0f);
}
ImGui::End();
}
@ -118,19 +114,20 @@ EGLBoolean hook_eglSwapBuffers(EGLDisplay dpy, EGLSurface surface) {
}
void* init_thread(void*) {
// 注入后只等 2 秒,确保 libandroid.so 加载
sleep(2);
sleep(5);
// 1. Hook 渲染
void* egl = dlsym(RTLD_DEFAULT, "eglSwapBuffers");
if (egl) DobbyHook(egl, (void*)hook_eglSwapBuffers, (void**)&old_eglSwapBuffers);
void* libandroid = dlopen("libandroid.so", RTLD_NOW);
if (libandroid) {
void* getPointerId = dlsym(libandroid, "AMotionEvent_getPointerId");
if (getPointerId) {
DobbyHook(getPointerId, (void*)hook_getPointerId, (void**)&old_getPointerId);
LOGI("Touch Hook Applied.");
}
// 2. Hook 底层输入 (使用日志中发现的符号)
// 注意:libinput.so 在不同系统可能路径不同,直接从内存搜索符号最稳
void* consume_addr = dlsym(RTLD_DEFAULT, "_ZN7android13InputConsumer7consumeEPNS_26InputEventFactoryInterfaceEblPjPPNS_10InputEventE");
if (consume_addr) {
DobbyHook(consume_addr, (void*)hook_InputConsumer_consume, (void**)&old_consume);
LOGI("Successfully Hooked InputConsumer::consume");
} else {
LOGI("Failed to find consume symbol via RTLD_DEFAULT");
}
return nullptr;