From f852221246834ce5fb12620eb2fc39bb4639fdda Mon Sep 17 00:00:00 2001 From: MistEO Date: Wed, 15 Nov 2023 00:38:52 +0800 Subject: [PATCH] =?UTF-8?q?feat:=20=E5=88=9D=E6=AD=A5=E5=AE=8C=E6=88=90win?= =?UTF-8?q?32=20toolkit?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- include/MaaFramework/Instance/MaaController.h | 2 +- include/MaaFramework/MaaDef.h | 1 + include/MaaToolKit/MaaToolKitAPI.h | 1 + sample/cpp/main.cpp | 53 +++++++---- .../LibraryHolder/ControlUnit/ControlUnit.cpp | 2 +- source/MaaFramework/API/MaaController.cpp | 2 +- .../MaaToolKit/API/MaaToolKitWin32Window.cpp | 34 +++++++ .../Win32Window/InvalidWindowFinder.cpp | 41 +++++++++ .../MaaToolKit/Win32Window/Win32WindowAPI.h | 19 ++++ .../Win32Window/Win32WindowFinder.cpp | 90 +++++++++++++++++++ .../Win32Window/Win32WindowFinder.h | 38 ++++++++ .../API/Win32ControlUnitAPI.cpp | 3 +- source/include/Conf/Conf.h | 6 ++ .../include/ControlUnit/Win32ControlUnitAPI.h | 2 +- source/include/LibraryHolder/ControlUnit.h | 2 +- 15 files changed, 274 insertions(+), 22 deletions(-) create mode 100644 source/MaaToolKit/API/MaaToolKitWin32Window.cpp create mode 100644 source/MaaToolKit/Win32Window/InvalidWindowFinder.cpp create mode 100644 source/MaaToolKit/Win32Window/Win32WindowAPI.h create mode 100644 source/MaaToolKit/Win32Window/Win32WindowFinder.cpp create mode 100644 source/MaaToolKit/Win32Window/Win32WindowFinder.h diff --git a/include/MaaFramework/Instance/MaaController.h b/include/MaaFramework/Instance/MaaController.h index f2e4265b1..ededdf318 100644 --- a/include/MaaFramework/Instance/MaaController.h +++ b/include/MaaFramework/Instance/MaaController.h @@ -14,7 +14,7 @@ extern "C" MaaControllerCallback callback, MaaCallbackTransparentArg callback_arg); MaaControllerHandle MAA_FRAMEWORK_API MaaWin32ControllerCreate( // - void* hWnd, MaaWin32ControllerType type, MaaControllerCallback callback, + MaaWin32Hwnd hWnd, MaaWin32ControllerType type, MaaControllerCallback callback, MaaCallbackTransparentArg callback_arg); MaaControllerHandle MAA_FRAMEWORK_API MaaAdbControllerCreateV2( // diff --git a/include/MaaFramework/MaaDef.h b/include/MaaFramework/MaaDef.h index d37896d51..f0eff90a0 100644 --- a/include/MaaFramework/MaaDef.h +++ b/include/MaaFramework/MaaDef.h @@ -179,6 +179,7 @@ enum MaaWin32ControllerTypeEnum MaaWin32ControllerType_Screencap_HWND = 1 << 16, MaaWin32ControllerType_Screencap_Mask = 0xFF0000, }; +typedef void* MaaWin32Hwnd; typedef void* MaaTransparentArg; typedef MaaTransparentArg MaaCallbackTransparentArg; diff --git a/include/MaaToolKit/MaaToolKitAPI.h b/include/MaaToolKit/MaaToolKitAPI.h index 15b003f32..16dc43ba8 100644 --- a/include/MaaToolKit/MaaToolKitAPI.h +++ b/include/MaaToolKit/MaaToolKitAPI.h @@ -4,3 +4,4 @@ #include "Config/MaaToolKitConfig.h" #include "Device/MaaToolKitDevice.h" +#include "Win32/MaaToolKitWin32Window.h" diff --git a/sample/cpp/main.cpp b/sample/cpp/main.cpp index 3ddfddd68..ab9be390f 100644 --- a/sample/cpp/main.cpp +++ b/sample/cpp/main.cpp @@ -3,6 +3,10 @@ #include #include +#ifdef _WIN32 +#include +#endif + #include "MaaFramework/MaaAPI.h" #include "MaaToolKit/MaaToolKitAPI.h" @@ -12,23 +16,16 @@ #pragma warning(disable : 4189) // local variable is initialized but not referenced #endif +MaaControllerHandle create_adb_controller(); +MaaControllerHandle create_win32_controller(); void register_my_recognizer(MaaInstanceHandle maa_handle); int main([[maybe_unused]] int argc, char** argv) { MaaToolKitInit(); - auto device_size = MaaToolKitFindDevice(); - if (device_size == 0) { - std::cout << "No device found" << std::endl; - return 0; - } - const int kIndex = 0; // for demo, we just use the first device - std::string agent_path = "share/MaaAgentBinary"; - auto controller_handle = - MaaAdbControllerCreateV2(MaaToolKitGetDeviceAdbPath(kIndex), MaaToolKitGetDeviceAdbSerial(kIndex), - MaaToolKitGetDeviceAdbControllerType(kIndex), MaaToolKitGetDeviceAdbConfig(kIndex), - agent_path.c_str(), nullptr, nullptr); + // auto controller_handle = create_adb_controller(); + auto controller_handle = create_win32_controller(); auto ctrl_id = MaaControllerPostConnection(controller_handle); auto resource_handle = MaaResourceCreate(nullptr, nullptr); @@ -49,12 +46,12 @@ int main([[maybe_unused]] int argc, char** argv) MaaToolKitUninit(); }; - if (!MaaInited(maa_handle)) { - std::cout << "Failed to init MAA" << std::endl; + // if (!MaaInited(maa_handle)) { + // std::cout << "Failed to init MAA" << std::endl; - destroy(); - return -1; - } + // destroy(); + // return -1; + //} register_my_recognizer(maa_handle); @@ -66,6 +63,30 @@ int main([[maybe_unused]] int argc, char** argv) return 0; } +MaaControllerHandle create_adb_controller() +{ + auto device_size = MaaToolKitFindDevice(); + if (device_size == 0) { + std::cout << "No device found" << std::endl; + return nullptr; + } + + const int kIndex = 0; // for demo, we just use the first device + std::string agent_path = "share/MaaAgentBinary"; + auto controller_handle = MaaAdbControllerCreateV2( // + MaaToolKitGetDeviceAdbPath(kIndex), MaaToolKitGetDeviceAdbSerial(kIndex), + MaaToolKitGetDeviceAdbControllerType(kIndex), MaaToolKitGetDeviceAdbConfig(kIndex), agent_path.c_str(), nullptr, + nullptr); + return controller_handle; +} + +MaaControllerHandle create_win32_controller() +{ + auto hwnd = MaaToolKitGetCursorWindow(); + auto type = MaaWin32ControllerType_Touch_SendMessage | MaaWin32ControllerType_Screencap_HWND; + return MaaWin32ControllerCreate(hwnd, type, nullptr, nullptr); +} + MaaBool my_analyze(MaaSyncContextHandle sync_context, const MaaImageBufferHandle image, MaaStringView task_name, MaaStringView custom_recognition_param, MaaTransparentArg arg, /*out*/ MaaRectHandle out_box, diff --git a/source/LibraryHolder/ControlUnit/ControlUnit.cpp b/source/LibraryHolder/ControlUnit/ControlUnit.cpp index c7722a397..478129a66 100644 --- a/source/LibraryHolder/ControlUnit/ControlUnit.cpp +++ b/source/LibraryHolder/ControlUnit/ControlUnit.cpp @@ -66,7 +66,7 @@ std::shared_ptr AdbControlUnitLibraryHolder::c } std::shared_ptr Win32ControlUnitLibraryHolder::create_control_unit( - void* hWnd, MaaWin32ControllerType type, MaaControllerCallback callback, MaaCallbackTransparentArg callback_arg) + MaaWin32Hwnd hWnd, MaaWin32ControllerType type, MaaControllerCallback callback, MaaCallbackTransparentArg callback_arg) { if (!load_library(libname_)) { LogError << "Failed to load library" << VAR(libname_); diff --git a/source/MaaFramework/API/MaaController.cpp b/source/MaaFramework/API/MaaController.cpp index fd190b066..83977eb9e 100644 --- a/source/MaaFramework/API/MaaController.cpp +++ b/source/MaaFramework/API/MaaController.cpp @@ -38,7 +38,7 @@ MaaControllerHandle MaaAdbControllerCreateV2( // } MaaControllerHandle MaaWin32ControllerCreate( // - void* hWnd, MaaWin32ControllerType type, MaaControllerCallback callback, MaaCallbackTransparentArg callback_arg) + MaaWin32Hwnd hWnd, MaaWin32ControllerType type, MaaControllerCallback callback, MaaCallbackTransparentArg callback_arg) { LogFunc << VAR_VOIDP(hWnd) << VAR(type) << VAR_VOIDP(callback) << VAR_VOIDP(callback_arg); diff --git a/source/MaaToolKit/API/MaaToolKitWin32Window.cpp b/source/MaaToolKit/API/MaaToolKitWin32Window.cpp new file mode 100644 index 000000000..6bbaabe7d --- /dev/null +++ b/source/MaaToolKit/API/MaaToolKitWin32Window.cpp @@ -0,0 +1,34 @@ +#include "MaaToolKit/Win32/MaaToolKitWin32Window.h" + +#include + +#include "Utils/Logger.h" +#include "Win32Window/Win32WindowFinder.h" + +auto& win32_mgr = MAA_TOOLKIT_WIN32_NS::Win32WindowFinder::get_instance(); + +MaaSize MaaToolKitFindWindow(MaaStringView class_name, MaaStringView window_name) +{ + LogInfo << VAR(class_name) << VAR(window_name); + + return win32_mgr.find_window(class_name, window_name); +} + +MaaSize MaaToolKitSearchWindow(MaaStringView class_name, MaaStringView window_name) +{ + LogInfo << VAR(class_name) << VAR(window_name); + + return win32_mgr.search_window(class_name, window_name); +} + +MaaWin32Hwnd MaaToolKitGetWindow(MaaSize index) +{ + return win32_mgr.found_window().at(index); +} + +MaaWin32Hwnd MaaToolKitGetCursorWindow() +{ + LogInfo; + + return win32_mgr.get_cursor_window(); +} diff --git a/source/MaaToolKit/Win32Window/InvalidWindowFinder.cpp b/source/MaaToolKit/Win32Window/InvalidWindowFinder.cpp new file mode 100644 index 000000000..53bd3aec7 --- /dev/null +++ b/source/MaaToolKit/Win32Window/InvalidWindowFinder.cpp @@ -0,0 +1,41 @@ +#ifndef _WIN32 + +#include "Win32WindowFinder.h" + +#include "Utils/Logger.h" + +MAA_TOOLKIT_WIN32_NS_BEGIN + +std::ostream& operator<<(std::ostream& os, const Win32WindowFinder::Window& w) +{ + os << VAR_VOIDP_RAW(w.hwnd) << VAR_RAW(w.class_name) << VAR_RAW(w.window_name); + return os; +} + +size_t Win32WindowFinder::find_window(std::string_view class_name, std::string_view window_name) +{ + LogError << "Not implemented" << VAR(class_name) << VAR(window_name); + return 0; +} + +size_t Win32WindowFinder::search_window(std::string_view class_name, std::string_view window_name) +{ + LogError << "Not implemented" << VAR(class_name) << VAR(window_name); + return 0; +} + +MaaWin32Hwnd Win32WindowFinder::get_cursor_window() const +{ + LogError << "Not implemented"; + return nullptr; +} + +std::vector Win32WindowFinder::list_windows() const +{ + LogError << "Not implemented"; + return {}; +} + +MAA_TOOLKIT_WIN32_NS_END + +#endif diff --git a/source/MaaToolKit/Win32Window/Win32WindowAPI.h b/source/MaaToolKit/Win32Window/Win32WindowAPI.h new file mode 100644 index 000000000..fb4c82fee --- /dev/null +++ b/source/MaaToolKit/Win32Window/Win32WindowAPI.h @@ -0,0 +1,19 @@ +#pragma once + +#include "Conf/Conf.h" + +#include +#include + +#include "MaaToolKit/MaaToolKitDef.h" + +class MaaWin32WindowAPI +{ +public: + virtual ~MaaWin32WindowAPI() = default; + + virtual size_t find_window(std::string_view class_name, std::string_view window_name) = 0; + virtual size_t search_window(std::string_view class_name, std::string_view window_name) = 0; + virtual std::vector found_window() const = 0; + virtual MaaWin32Hwnd get_cursor_window() const = 0; +}; diff --git a/source/MaaToolKit/Win32Window/Win32WindowFinder.cpp b/source/MaaToolKit/Win32Window/Win32WindowFinder.cpp new file mode 100644 index 000000000..28c38ca5c --- /dev/null +++ b/source/MaaToolKit/Win32Window/Win32WindowFinder.cpp @@ -0,0 +1,90 @@ +#ifdef _WIN32 + +#include "Win32WindowFinder.h" + +#include "Utils/Logger.h" +#include "Utils/Platform.h" + +MAA_TOOLKIT_WIN32_NS_BEGIN + +std::ostream& operator<<(std::ostream& os, const Win32WindowFinder::Window& w) +{ + os << VAR_VOIDP_RAW(w.hwnd) << VAR_RAW(w.class_name) << VAR_RAW(w.window_name); + return os; +} + +size_t Win32WindowFinder::find_window(std::string_view class_name, std::string_view window_name) +{ + auto windows = list_windows(); + + windows_.clear(); + for (const auto& w : windows) { + bool same_class = class_name.empty() || w.class_name == class_name; + bool same_window = window_name.empty() || w.window_name == window_name; + if (same_class && same_window) { + windows_.emplace_back(w.hwnd); + } + } + return windows_.size(); +} + +size_t Win32WindowFinder::search_window(std::string_view class_name, std::string_view window_name) +{ + auto windows = list_windows(); + + windows_.clear(); + for (const auto& w : windows) { + bool same_class = class_name.empty() || w.class_name.find(class_name) != std::string::npos; + bool same_window = window_name.empty() || w.window_name.find(window_name) != std::string::npos; + if (same_class && same_window) { + windows_.emplace_back(w.hwnd); + } + } + return windows_.size(); +} + +MaaWin32Hwnd Win32WindowFinder::get_cursor_window() const +{ + POINT pt; + if (!GetCursorPos(&pt)) { + return nullptr; + } + + HWND hwnd = WindowFromPoint(pt); + if (hwnd == NULL) { + return nullptr; + } + + return reinterpret_cast(hwnd); +} + +std::vector Win32WindowFinder::list_windows() const +{ + std::vector windows; + + for (HWND hwnd = GetTopWindow(NULL); hwnd != NULL; hwnd = GetNextWindow(hwnd, GW_HWNDNEXT)) { + if (!IsWindowVisible(hwnd)) { + continue; + } + + std::string class_name(256, '\0'); + GetClassName(hwnd, class_name.data(), static_cast(class_name.size())); + + std::string window_name(256, '\0'); + GetWindowText(hwnd, window_name.data(), static_cast(window_name.size())); + + windows.emplace_back(Window { .hwnd = reinterpret_cast(hwnd), + .class_name = std::move(class_name), + .window_name = std::move(window_name) }); + } + +#ifdef MAA_DEBUG + LogInfo << "Window list:" << windows; +#endif + + return windows; +} + +MAA_TOOLKIT_WIN32_NS_END + +#endif diff --git a/source/MaaToolKit/Win32Window/Win32WindowFinder.h b/source/MaaToolKit/Win32Window/Win32WindowFinder.h new file mode 100644 index 000000000..7810bdf50 --- /dev/null +++ b/source/MaaToolKit/Win32Window/Win32WindowFinder.h @@ -0,0 +1,38 @@ +#pragma once + +#include "Win32WindowAPI.h" + +#include "Utils/Platform.h" +#include "Utils/SingletonHolder.hpp" + +MAA_TOOLKIT_WIN32_NS_BEGIN + +class Win32WindowFinder : public MaaWin32WindowAPI, public SingletonHolder +{ +public: + struct Window + { + MaaWin32Hwnd hwnd; + std::string class_name; + std::string window_name; + }; + +public: + virtual ~Win32WindowFinder() = default; + + virtual size_t find_window(std::string_view class_name, std::string_view window_name) override; + virtual size_t search_window(std::string_view class_name, std::string_view window_name) override; + virtual std::vector found_window() const override { return windows_; } + + virtual MaaWin32Hwnd get_cursor_window() const override; + +private: + std::vector list_windows() const; + +private: + std::vector windows_; +}; + +std::ostream& operator<<(std::ostream& os, const Win32WindowFinder::Window& window); + +MAA_TOOLKIT_WIN32_NS_END diff --git a/source/MaaWin32ControlUnit/API/Win32ControlUnitAPI.cpp b/source/MaaWin32ControlUnit/API/Win32ControlUnitAPI.cpp index ff9ddf298..5dafd0102 100644 --- a/source/MaaWin32ControlUnit/API/Win32ControlUnitAPI.cpp +++ b/source/MaaWin32ControlUnit/API/Win32ControlUnitAPI.cpp @@ -15,7 +15,8 @@ MaaStringView MaaWin32ControlUnitGetVersion() } MaaControlUnitHandle MaaWin32ControlUnitCreate( // - void* hWnd, MaaWin32ControllerType type, MaaControllerCallback callback, MaaCallbackTransparentArg callback_arg) + MaaWin32Hwnd hWnd, MaaWin32ControllerType type, MaaControllerCallback callback, + MaaCallbackTransparentArg callback_arg) { using namespace MAA_CTRL_UNIT_NS; diff --git a/source/include/Conf/Conf.h b/source/include/Conf/Conf.h index 642f7b1b6..8092ab2fe 100644 --- a/source/include/Conf/Conf.h +++ b/source/include/Conf/Conf.h @@ -142,3 +142,9 @@ namespace MAA_TOOLKIT_CONFIG_NS \ { #define MAA_TOOLKIT_CONFIG_NS_END } + +#define MAA_TOOLKIT_WIN32_NS MAA_TOOLKIT_NS::Win32NS +#define MAA_TOOLKIT_WIN32_NS_BEGIN \ + namespace MAA_TOOLKIT_WIN32_NS \ + { +#define MAA_TOOLKIT_WIN32_NS_END } diff --git a/source/include/ControlUnit/Win32ControlUnitAPI.h b/source/include/ControlUnit/Win32ControlUnitAPI.h index 8fdc098a7..c7011e7d2 100644 --- a/source/include/ControlUnit/Win32ControlUnitAPI.h +++ b/source/include/ControlUnit/Win32ControlUnitAPI.h @@ -14,7 +14,7 @@ extern "C" MaaStringView MAA_CONTROL_UNIT_API MaaWin32ControlUnitGetVersion(); MaaControlUnitHandle MAA_CONTROL_UNIT_API MaaWin32ControlUnitCreate( // - void* hWnd, MaaWin32ControllerType type, MaaControllerCallback callback, + MaaWin32Hwnd hWnd, MaaWin32ControllerType type, MaaControllerCallback callback, MaaCallbackTransparentArg callback_arg); void MAA_CONTROL_UNIT_API MaaWin32ControlUnitDestroy(MaaControlUnitHandle handle); diff --git a/source/include/LibraryHolder/ControlUnit.h b/source/include/LibraryHolder/ControlUnit.h index ded079c2a..9825ad1a9 100644 --- a/source/include/LibraryHolder/ControlUnit.h +++ b/source/include/LibraryHolder/ControlUnit.h @@ -27,7 +27,7 @@ class Win32ControlUnitLibraryHolder : public LibraryHolder create_control_unit( // - void* hWnd, MaaWin32ControllerType type, MaaControllerCallback callback, + MaaWin32Hwnd hWnd, MaaWin32ControllerType type, MaaControllerCallback callback, MaaCallbackTransparentArg callback_arg); private: