-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcontroller.go
427 lines (367 loc) · 12 KB
/
controller.go
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
package maa
import (
"image"
"sync"
"time"
"unsafe"
"github.com/MaaXYZ/maa-framework-go/internal/buffer"
"github.com/MaaXYZ/maa-framework-go/internal/maa"
"github.com/MaaXYZ/maa-framework-go/internal/store"
)
// Controller is an interface that defines various methods for MAA controller.
type Controller interface {
Destroy()
Handle() uintptr
SetScreenshotTargetLongSide(targetLongSide int32) bool
SetScreenshotTargetShortSide(targetShortSide int32) bool
SetScreenshotUseRawSize(enabled bool) bool
SetRecording(enabled bool) bool
PostConnect() *Job
PostClick(x, y int32) *Job
PostSwipe(x1, y1, x2, y2 int32, duration time.Duration) *Job
PostPressKey(keycode int32) *Job
PostInputText(text string) *Job
PostStartApp(intent string) *Job
PostStopApp(intent string) *Job
PostTouchDown(contact, x, y, pressure int32) *Job
PostTouchMove(contact, x, y, pressure int32) *Job
PostTouchUp(contact int32) *Job
PostScreencap() *Job
Connected() bool
CacheImage() image.Image
GetUUID() (string, bool)
}
type controllerStoreValue struct {
NotificationCallbackID uint64
CustomControllerCallbacksID uint64
}
var (
controllerStore = store.New[controllerStoreValue]()
controllerStoreMutex sync.RWMutex
)
// controller is a concrete implementation of the Controller interface.
type controller struct {
handle uintptr
}
// AdbScreencapMethod
//
// Use bitwise OR to set the method you need,
// MaaFramework will test their speed and use the fastest one.
type AdbScreencapMethod uint64
// AdbScreencapMethod
const (
AdbScreencapMethodNone AdbScreencapMethod = 0
AdbScreencapMethodEncodeToFileAndPull AdbScreencapMethod = 1
AdbScreencapMethodEncode AdbScreencapMethod = 1 << 1
AdbScreencapMethodRawWithGzip AdbScreencapMethod = 1 << 2
AdbScreencapMethodRawByNetcat AdbScreencapMethod = 1 << 3
AdbScreencapMethodMinicapDirect AdbScreencapMethod = 1 << 4
AdbScreencapMethodMinicapStream AdbScreencapMethod = 1 << 5
AdbScreencapMethodEmulatorExtras AdbScreencapMethod = 1 << 6
AdbScreencapMethodAll = ^AdbScreencapMethodNone
AdbScreencapMethodDefault = AdbScreencapMethodAll & (^AdbScreencapMethodRawByNetcat) & (^AdbScreencapMethodMinicapDirect) & (^AdbScreencapMethodMinicapStream)
)
// AdbInputMethod
//
// Use bitwise OR to set the method you need,
// MaaFramework will select the available ones according to priority.
// The priority is: EmulatorExtras > Maatouch > MinitouchAndAdbKey > AdbShell
type AdbInputMethod uint64
// AdbInputMethod
const (
AdbInputMethodNone AdbInputMethod = 0
AdbInputMethodAdbShell AdbInputMethod = 1
AdbInputMethodMinitouchAndAdbKey AdbInputMethod = 1 << 1
AdbInputMethodMaatouch AdbInputMethod = 1 << 2
AdbInputMethodEmulatorExtras AdbInputMethod = 1 << 3
AdbInputMethodAll = ^AdbInputMethodNone
AdbInputMethodDefault = AdbInputMethodAll & (^AdbInputMethodEmulatorExtras)
)
// NewAdbController creates an ADB controller instance.
func NewAdbController(
adbPath, address string,
screencapMethod AdbScreencapMethod,
inputMethod AdbInputMethod,
config, agentPath string,
notify Notification,
) Controller {
id := registerNotificationCallback(notify)
handle := maa.MaaAdbControllerCreate(
adbPath,
address,
maa.MaaAdbScreencapMethod(screencapMethod),
maa.MaaAdbInputMethod(inputMethod),
config,
agentPath,
_MaaNotificationCallbackAgent,
// Here, we are simply passing the uint64 value as a pointer
// and will not actually dereference this pointer.
uintptr(id),
)
if handle == 0 {
return nil
}
controllerStoreMutex.Lock()
controllerStore.Set(handle, controllerStoreValue{
NotificationCallbackID: id,
})
controllerStoreMutex.Unlock()
return &controller{handle: handle}
}
// Win32ScreencapMethod
//
// No bitwise OR, just set it.
type Win32ScreencapMethod uint64
// Win32ScreencapMethod
const (
Win32ScreencapMethodNone Win32ScreencapMethod = 0
Win32ScreencapMethodGDI Win32ScreencapMethod = 1
Win32ScreencapMethodFramePool Win32ScreencapMethod = 1 << 1
Win32ScreencapMethodDXGIDesktopDup Win32ScreencapMethod = 1 << 2
)
// Win32InputMethod
//
// No bitwise OR, just set it.
type Win32InputMethod uint64
// Win32InputMethod
const (
Win32InputMethodNone Win32InputMethod = 0
Win32InputMethodSeize Win32InputMethod = 1
Win32InputMethodSendMessage Win32InputMethod = 1 << 1
)
// NewWin32Controller creates a win32 controller instance.
func NewWin32Controller(
hWnd unsafe.Pointer,
screencapMethod Win32ScreencapMethod,
inputMethod Win32InputMethod,
notify Notification,
) Controller {
id := registerNotificationCallback(notify)
handle := maa.MaaWin32ControllerCreate(
hWnd,
maa.MaaWin32ScreencapMethod(screencapMethod),
maa.MaaWin32InputMethod(inputMethod),
_MaaNotificationCallbackAgent,
// Here, we are simply passing the uint64 value as a pointer
// and will not actually dereference this pointer.
uintptr(id),
)
if handle == 0 {
return nil
}
controllerStoreMutex.Lock()
controllerStore.Set(handle, controllerStoreValue{
NotificationCallbackID: id,
})
controllerStoreMutex.Unlock()
return &controller{handle: handle}
}
// DbgControllerType
//
// No bitwise OR, just set it.
type DbgControllerType uint64
// DbgControllerType
const (
DbgControllerTypeNone DbgControllerType = 0
DbgControllerTypeCarouselImage DbgControllerType = 1
DbgControllerTypeReplayRecording DbgControllerType = 1 << 1
)
// NewDbgController creates a DBG controller instance.
func NewDbgController(
readPath, writePath string,
dbgCtrlType DbgControllerType,
config string,
notify Notification,
) Controller {
id := registerNotificationCallback(notify)
handle := maa.MaaDbgControllerCreate(
readPath,
writePath,
maa.MaaDbgControllerType(dbgCtrlType),
config,
_MaaNotificationCallbackAgent,
// Here, we are simply passing the uint64 value as a pointer
// and will not actually dereference this pointer.
uintptr(id),
)
if handle == 0 {
return nil
}
controllerStoreMutex.Lock()
controllerStore.Set(handle, controllerStoreValue{
NotificationCallbackID: id,
})
controllerStoreMutex.Unlock()
return &controller{handle: handle}
}
// NewCustomController creates a custom controller instance.
func NewCustomController(
ctrl CustomController,
notify Notification,
) Controller {
ctrlID := registerCustomControllerCallbacks(ctrl)
notifyID := registerNotificationCallback(notify)
handle := maa.MaaCustomControllerCreate(
uintptr(ctrl.Handle()),
// Here, we are simply passing the uint64 value as a pointer
// and will not actually dereference this pointer.
uintptr(ctrlID),
_MaaNotificationCallbackAgent,
// Here, we are simply passing the uint64 value as a pointer
// and will not actually dereference this pointer.
uintptr(notifyID),
)
if handle == 0 {
return nil
}
controllerStoreMutex.Lock()
controllerStore.Set(handle, controllerStoreValue{
NotificationCallbackID: notifyID,
CustomControllerCallbacksID: ctrlID,
})
controllerStoreMutex.Unlock()
return &controller{handle: handle}
}
// Destroy frees the controller instance.
func (c *controller) Destroy() {
controllerStoreMutex.Lock()
value := controllerStore.Get(c.handle)
unregisterNotificationCallback(value.NotificationCallbackID)
unregisterCustomControllerCallbacks(value.CustomControllerCallbacksID)
controllerStore.Del(c.handle)
controllerStoreMutex.Unlock()
maa.MaaControllerDestroy(c.handle)
}
// Handle returns controller handle.
func (c *controller) Handle() uintptr {
return c.handle
}
// setOption sets options for controller instance.
func (c *controller) setOption(key maa.MaaCtrlOption, value unsafe.Pointer, valSize uintptr) bool {
return maa.MaaControllerSetOption(c.handle, key, value, uint64(valSize))
}
// SetScreenshotTargetLongSide sets screenshot target long side.
// Only one of long and short side can be set, and the other is automatically scaled according to the aspect ratio.
//
// eg: 1280
func (c *controller) SetScreenshotTargetLongSide(targetLongSide int32) bool {
return c.setOption(
maa.MaaCtrlOption_ScreenshotTargetLongSide,
unsafe.Pointer(&targetLongSide),
unsafe.Sizeof(targetLongSide),
)
}
// SetScreenshotTargetShortSide sets screenshot target short side.
// Only one of long and short side can be set, and the other is automatically scaled according to the aspect ratio.
//
// eg: 720
func (c *controller) SetScreenshotTargetShortSide(targetShortSide int32) bool {
return c.setOption(
maa.MaaCtrlOption_ScreenshotTargetShortSide,
unsafe.Pointer(&targetShortSide),
unsafe.Sizeof(targetShortSide),
)
}
// SetScreenshotUseRawSize sets whether the screenshot uses the raw size without scaling.
func (c *controller) SetScreenshotUseRawSize(enabled bool) bool {
return c.setOption(
maa.MaaCtrlOption_ScreenshotUseRawSize,
unsafe.Pointer(&enabled),
unsafe.Sizeof(enabled),
)
}
// SetRecording sets whether to dump all screenshots and actions.
func (c *controller) SetRecording(enabled bool) bool {
return c.setOption(
maa.MaaCtrlOption_Recording,
unsafe.Pointer(&enabled),
unsafe.Sizeof(enabled),
)
}
// PostConnect posts a connection.
func (c *controller) PostConnect() *Job {
id := maa.MaaControllerPostConnection(c.handle)
return NewJob(id, c.status, c.wait)
}
// PostClick posts a click.
func (c *controller) PostClick(x, y int32) *Job {
id := maa.MaaControllerPostClick(c.handle, x, y)
return NewJob(id, c.status, c.wait)
}
// PostSwipe posts a swipe.
func (c *controller) PostSwipe(x1, y1, x2, y2 int32, duration time.Duration) *Job {
id := maa.MaaControllerPostSwipe(c.handle, x1, y1, x2, y2, int32(duration.Milliseconds()))
return NewJob(id, c.status, c.wait)
}
// PostPressKey posts a press key.
func (c *controller) PostPressKey(keycode int32) *Job {
id := maa.MaaControllerPostPressKey(c.handle, keycode)
return NewJob(id, c.status, c.wait)
}
// PostInputText posts an input text.
func (c *controller) PostInputText(text string) *Job {
id := maa.MaaControllerPostInputText(c.handle, text)
return NewJob(id, c.status, c.wait)
}
// PostStartApp posts a start app.
func (c *controller) PostStartApp(intent string) *Job {
id := maa.MaaControllerPostStartApp(c.handle, intent)
return NewJob(id, c.status, c.wait)
}
// PostStopApp posts a stop app.
func (c *controller) PostStopApp(intent string) *Job {
id := maa.MaaControllerPostStopApp(c.handle, intent)
return NewJob(id, c.status, c.wait)
}
// PostTouchDown posts a touch-down.
func (c *controller) PostTouchDown(contact, x, y, pressure int32) *Job {
id := maa.MaaControllerPostTouchDown(c.handle, contact, x, y, pressure)
return NewJob(id, c.status, c.wait)
}
// PostTouchMove posts a touch-move.
func (c *controller) PostTouchMove(contact, x, y, pressure int32) *Job {
id := maa.MaaControllerPostTouchMove(c.handle, contact, x, y, pressure)
return NewJob(id, c.status, c.wait)
}
// PostTouchUp posts a touch-up.
func (c *controller) PostTouchUp(contact int32) *Job {
id := maa.MaaControllerPostTouchUp(c.handle, contact)
return NewJob(id, c.status, c.wait)
}
// PostScreencap posts a screencap.
func (c *controller) PostScreencap() *Job {
id := maa.MaaControllerPostScreencap(c.handle)
return NewJob(id, c.status, c.wait)
}
// status gets the status of a request identified by the given id.
func (c *controller) status(id int64) Status {
return Status(maa.MaaControllerStatus(c.handle, id))
}
func (c *controller) wait(id int64) Status {
return Status(maa.MaaControllerWait(c.handle, id))
}
// Connected checks if the controller is connected.
func (c *controller) Connected() bool {
return maa.MaaControllerConnected(c.handle)
}
// CacheImage gets the image buffer of the last screencap request.
func (c *controller) CacheImage() image.Image {
imgBuffer := buffer.NewImageBuffer()
defer imgBuffer.Destroy()
got := maa.MaaControllerCachedImage(c.handle, uintptr(imgBuffer.Handle()))
if !got {
return nil
}
img := imgBuffer.Get()
return img
}
// GetUUID gets the UUID of the controller.
func (c *controller) GetUUID() (string, bool) {
uuid := buffer.NewStringBuffer()
defer uuid.Destroy()
got := maa.MaaControllerGetUuid(c.handle, uintptr(uuid.Handle()))
if !got {
return "", false
}
return uuid.Get(), true
}