-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtoolkit.go
More file actions
121 lines (107 loc) · 4.05 KB
/
toolkit.go
File metadata and controls
121 lines (107 loc) · 4.05 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
package maa
import (
"errors"
"unsafe"
"github.com/MaaXYZ/maa-framework-go/v4/controller/adb"
"github.com/MaaXYZ/maa-framework-go/v4/internal/native"
)
// AdbDevice represents a single ADB device with various properties about its information.
type AdbDevice struct {
Name string
AdbPath string
Address string
ScreencapMethod adb.ScreencapMethod
InputMethod adb.InputMethod
Config string
}
// DesktopWindow represents a single desktop window with various properties about its information.
type DesktopWindow struct {
Handle unsafe.Pointer
ClassName string
WindowName string
}
// ConfigInitOption inits the toolkit config option.
func ConfigInitOption(userPath, defaultJson string) error {
if !native.MaaToolkitConfigInitOption(userPath, defaultJson) {
return errors.New("failed to init toolkit config option")
}
return nil
}
// FindAdbDevices finds adb devices.
func FindAdbDevices(specifiedAdb ...string) ([]*AdbDevice, error) {
listHandle := native.MaaToolkitAdbDeviceListCreate()
defer native.MaaToolkitAdbDeviceListDestroy(listHandle)
var got bool
if len(specifiedAdb) > 0 {
got = native.MaaToolkitAdbDeviceFindSpecified(specifiedAdb[0], listHandle)
} else {
got = native.MaaToolkitAdbDeviceFind(listHandle)
}
if !got {
return nil, errors.New("failed to find adb devices")
}
size := native.MaaToolkitAdbDeviceListSize(listHandle)
list := make([]*AdbDevice, size)
for i := uint64(0); i < size; i++ {
deviceHandle := native.MaaToolkitAdbDeviceListAt(listHandle, i)
name := native.MaaToolkitAdbDeviceGetName(deviceHandle)
adbPath := native.MaaToolkitAdbDeviceGetAdbPath(deviceHandle)
address := native.MaaToolkitAdbDeviceGetAddress(deviceHandle)
screencapMethod := adb.ScreencapMethod(native.MaaToolkitAdbDeviceGetScreencapMethods(deviceHandle))
inputMethod := adb.InputMethod(native.MaaToolkitAdbDeviceGetInputMethods(deviceHandle))
config := native.MaaToolkitAdbDeviceGetConfig(deviceHandle)
list[i] = &AdbDevice{
Name: name,
AdbPath: adbPath,
Address: address,
ScreencapMethod: screencapMethod,
InputMethod: inputMethod,
Config: config,
}
}
return list, nil
}
// FindDesktopWindows finds desktop windows.
func FindDesktopWindows() ([]*DesktopWindow, error) {
listHandle := native.MaaToolkitDesktopWindowListCreate()
defer native.MaaToolkitDesktopWindowListDestroy(listHandle)
got := native.MaaToolkitDesktopWindowFindAll(listHandle)
if !got {
return nil, errors.New("failed to find desktop windows")
}
size := native.MaaToolkitDesktopWindowListSize(listHandle)
list := make([]*DesktopWindow, size)
for i := uint64(0); i < size; i++ {
windowHandle := native.MaaToolkitDesktopWindowListAt(listHandle, i)
handle := native.MaaToolkitDesktopWindowGetHandle(windowHandle)
className := native.MaaToolkitDesktopWindowGetClassName(windowHandle)
windowName := native.MaaToolkitDesktopWindowGetWindowName(windowHandle)
list[i] = &DesktopWindow{
Handle: handle,
ClassName: className,
WindowName: windowName,
}
}
return list, nil
}
// MacOSPermission defines a macOS permission type.
type MacOSPermission = native.MaaMacOSPermission
// MacOS permission constants.
const (
MacOSPermissionScreenCapture MacOSPermission = native.MaaMacOSPermissionScreenCapture
MacOSPermissionAccessibility MacOSPermission = native.MaaMacOSPermissionAccessibility
)
// MacOSCheckPermission checks whether the given macOS permission has been granted.
func MacOSCheckPermission(perm MacOSPermission) bool {
return native.MaaToolkitMacOSCheckPermission(perm)
}
// MacOSRequestPermission requests the given macOS permission from the user.
// Returns true if the permission was granted, false otherwise.
func MacOSRequestPermission(perm MacOSPermission) bool {
return native.MaaToolkitMacOSRequestPermission(perm)
}
// MacOSRevealPermissionSettings opens the System Settings page for the given macOS permission.
// Returns true on success.
func MacOSRevealPermissionSettings(perm MacOSPermission) bool {
return native.MaaToolkitMacOSRevealPermissionSettings(perm)
}