Skip to content

Commit

Permalink
feat: 初步完成win32 toolkit
Browse files Browse the repository at this point in the history
  • Loading branch information
MistEO committed Nov 14, 2023
1 parent 46347bf commit 6285b05
Show file tree
Hide file tree
Showing 16 changed files with 291 additions and 22 deletions.
2 changes: 1 addition & 1 deletion include/MaaFramework/Instance/MaaController.h
Original file line number Diff line number Diff line change
Expand Up @@ -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( //
Expand Down
1 change: 1 addition & 0 deletions include/MaaFramework/MaaDef.h
Original file line number Diff line number Diff line change
Expand Up @@ -179,6 +179,7 @@ enum MaaWin32ControllerTypeEnum
MaaWin32ControllerType_Screencap_HWND = 1 << 16,
MaaWin32ControllerType_Screencap_Mask = 0xFF0000,
};
typedef void* MaaWin32Hwnd;

typedef void* MaaTransparentArg;
typedef MaaTransparentArg MaaCallbackTransparentArg;
Expand Down
1 change: 1 addition & 0 deletions include/MaaToolKit/MaaToolKitAPI.h
Original file line number Diff line number Diff line change
Expand Up @@ -4,3 +4,4 @@

#include "Config/MaaToolKitConfig.h"
#include "Device/MaaToolKitDevice.h"
#include "Win32/MaaToolKitWin32Window.h"
17 changes: 17 additions & 0 deletions include/MaaToolKit/Win32/MaaToolKitWin32Window.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include "../MaaToolKitDef.h"

#ifdef __cplusplus
extern "C"
{
#endif

MaaSize MAA_TOOLKIT_API MaaToolKitFindWindow(MaaStringView class_name, MaaStringView window_name);
MaaSize MAA_TOOLKIT_API MaaToolKitSearchWindow(MaaStringView class_name, MaaStringView window_name);
MaaWin32Hwnd MAA_TOOLKIT_API MaaToolKitGetWindow(MaaSize index);
MaaWin32Hwnd MAA_TOOLKIT_API MaaToolKitGetCursorWindow();

#ifdef __cplusplus
}
#endif
53 changes: 37 additions & 16 deletions sample/cpp/main.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,10 @@
#include <iostream>
#include <string>

#ifdef _WIN32
#include <Windows.h>
#endif

#include "MaaFramework/MaaAPI.h"
#include "MaaToolKit/MaaToolKitAPI.h"

Expand All @@ -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);
Expand All @@ -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);

Expand All @@ -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,
Expand Down
2 changes: 1 addition & 1 deletion source/LibraryHolder/ControlUnit/ControlUnit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -66,7 +66,7 @@ std::shared_ptr<MAA_CTRL_UNIT_NS::ControlUnitAPI> AdbControlUnitLibraryHolder::c
}

std::shared_ptr<MAA_CTRL_UNIT_NS::ControlUnitAPI> 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_);
Expand Down
2 changes: 1 addition & 1 deletion source/MaaFramework/API/MaaController.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
34 changes: 34 additions & 0 deletions source/MaaToolKit/API/MaaToolKitWin32Window.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
#include "MaaToolKit/Win32/MaaToolKitWin32Window.h"

#include <string_view>

#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();
}
41 changes: 41 additions & 0 deletions source/MaaToolKit/Win32Window/InvalidWindowFinder.cpp
Original file line number Diff line number Diff line change
@@ -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::Window> Win32WindowFinder::list_windows() const
{
LogError << "Not implemented";
return {};
}

MAA_TOOLKIT_WIN32_NS_END

#endif
19 changes: 19 additions & 0 deletions source/MaaToolKit/Win32Window/Win32WindowAPI.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
#pragma once

#include "Conf/Conf.h"

#include <string_view>
#include <vector>

#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<MaaWin32Hwnd> found_window() const = 0;
virtual MaaWin32Hwnd get_cursor_window() const = 0;
};
90 changes: 90 additions & 0 deletions source/MaaToolKit/Win32Window/Win32WindowFinder.cpp
Original file line number Diff line number Diff line change
@@ -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<MaaWin32Hwnd>(hwnd);
}

std::vector<Win32WindowFinder::Window> Win32WindowFinder::list_windows() const
{
std::vector<Window> 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<int>(class_name.size()));

std::string window_name(256, '\0');
GetWindowText(hwnd, window_name.data(), static_cast<int>(window_name.size()));

windows.emplace_back(Window { .hwnd = reinterpret_cast<MaaWin32Hwnd>(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
38 changes: 38 additions & 0 deletions source/MaaToolKit/Win32Window/Win32WindowFinder.h
Original file line number Diff line number Diff line change
@@ -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<Win32WindowFinder>
{
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<MaaWin32Hwnd> found_window() const override { return windows_; }

virtual MaaWin32Hwnd get_cursor_window() const override;

private:
std::vector<Window> list_windows() const;

private:
std::vector<MaaWin32Hwnd> windows_;
};

std::ostream& operator<<(std::ostream& os, const Win32WindowFinder::Window& window);

MAA_TOOLKIT_WIN32_NS_END
3 changes: 2 additions & 1 deletion source/MaaWin32ControlUnit/API/Win32ControlUnitAPI.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
6 changes: 6 additions & 0 deletions source/include/Conf/Conf.h
Original file line number Diff line number Diff line change
Expand Up @@ -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 }
Loading

0 comments on commit 6285b05

Please sign in to comment.