-
Notifications
You must be signed in to change notification settings - Fork 18
/
Copy pathrobotgo.go
546 lines (430 loc) · 11.6 KB
/
robotgo.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
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
// Copyright 2016 The go-vgo Project Developers. See the COPYRIGHT
// file at the top-level directory of this distribution and at
// https://github.com/go-vgo/robotgo/blob/master/LICENSE
//
// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
// option. This file may not be copied, modified, or distributed
// except according to those terms.
/*
Package robotgo Go native cross-platform system automation.
Please make sure Golang, GCC is installed correctly before installing RobotGo;
See Requirements:
https://github.com/go-vgo/robotgo#requirements
Installation:
go get -u github.com/go-vgo/robotgo
C-shared:
go get -v github.com/vcaesar/gocs
gocs -n robotgo
*/
package main
import "C"
import (
"fmt"
"strings"
"github.com/go-vgo/robotgo"
)
func ch(str string) *C.char {
return C.CString(str)
}
func str(ch *C.char) string {
return C.GoString(ch)
}
func sf(err error) string {
return fmt.Sprintf("%s", err)
}
func ech(err error) *C.char {
return ch(sf(err))
}
func toStr(arr interface{}) string {
return strings.Trim(fmt.Sprint(arr), "[]")
}
//export GetVersion
func GetVersion() *C.char {
s := robotgo.GetVersion()
return ch(s)
}
//export Sleep
func Sleep(tm int) {
robotgo.Sleep(tm)
}
//export MilliSleep
func MilliSleep(tm int) {
robotgo.MilliSleep(tm)
}
//export MSleep
func MSleep(tm float64) {
robotgo.MicroSleep(tm)
}
/*
_______. ______ .______ _______ _______ .__ __.
/ | / || _ \ | ____|| ____|| \ | |
| (----`| ,----'| |_) | | |__ | |__ | \| |
\ \ | | | / | __| | __| | . ` |
.----) | | `----.| |\ \----.| |____ | |____ | |\ |
|_______/ \______|| _| `._____||_______||_______||__| \__|
*/
//export GetPixelColor
func GetPixelColor(x, y int) *C.char {
s := robotgo.GetPixelColor(x, y)
return ch(s)
}
//export GetMouseColor
func GetMouseColor() *C.char {
s := robotgo.GetMouseColor()
return ch(s)
}
//export GetScreenSize
func GetScreenSize() (int, int) {
return robotgo.GetScreenSize()
}
//export GetScaleSize
func GetScaleSize() (int, int) {
return robotgo.GetScaleSize()
}
//export CaptureScreen
func CaptureScreen(x, y, w, h int) (*uint8, int, int,
int, uint8, uint8) {
var bit robotgo.Bitmap
if x == -1 {
bit = robotgo.GoCaptureScreen()
} else {
bit = robotgo.GoCaptureScreen(x, y, w, h)
}
return bit.ImgBuf, bit.Width, bit.Height,
bit.Bytewidth, bit.BitsPixel, bit.BytesPerPixel
}
//export SaveCapture
func SaveCapture(path *C.char, x, y, w, h int) {
if x == -1 {
robotgo.SaveCapture(str(path))
return
}
robotgo.SaveCapture(str(path), x, y, w, h)
}
/*
.___ ___. ______ __ __ _______. _______
| \/ | / __ \ | | | | / || ____|
| \ / | | | | | | | | | | (----`| |__
| |\/| | | | | | | | | | \ \ | __|
| | | | | `--' | | `--' | .----) | | |____
|__| |__| \______/ \______/ |_______/ |_______|
*/
//export MoveMouse
func MoveMouse(x, y int) {
robotgo.Move(x, y)
}
//export DragMouse
func DragMouse(x, y int, args *C.char) {
robotgo.Drag(x, y, str(args))
}
//export MoveSmooth
func MoveSmooth(x, y int, low, high float64) bool {
return robotgo.MoveSmooth(x, y, low, high)
}
//export GetMousePos
func GetMousePos() (int, int) {
return robotgo.GetMousePos()
}
//export Click
func Click(btn *C.char, doublec bool) {
robotgo.Click(str(btn), doublec)
}
//export MouseToggle
func MouseToggle(key, btn *C.char) {
robotgo.MouseToggle(str(key), str(btn))
}
//export Scroll
func Scroll(x, y int) {
robotgo.Scroll(x, y)
}
/*
__ ___ ___________ ____ .______ ______ ___ .______ _______
| |/ / | ____\ \ / / | _ \ / __ \ / \ | _ \ | \
| ' / | |__ \ \/ / | |_) | | | | | / ^ \ | |_) | | .--. |
| < | __| \_ _/ | _ < | | | | / /_\ \ | / | | | |
| . \ | |____ | | | |_) | | `--' | / _____ \ | |\ \----.| '--' |
|__|\__\ |_______| |__| |______/ \______/ /__/ \__\ | _| `._____||_______/
*/
//export KeyTap
func KeyTap(key *C.char, vals *C.char) *C.char {
arr := strings.Split(str(vals), ",")
s := robotgo.KeyTap(str(key), arr)
return ch(s)
}
//export KeyToggle
func KeyToggle(key *C.char, vals *C.char) *C.char {
arr := strings.Split(str(vals), ",")
s := robotgo.KeyToggle(str(key), arr...)
return ch(s)
}
//export TypeStr
func TypeStr(c *C.char, args float64) {
robotgo.TypeStr(str(c), args)
}
//export ReadAll
func ReadAll() (*C.char, *C.char) {
s, err := robotgo.ReadAll()
if err != nil {
return ch(s), ech(err)
}
return ch(s), ch("")
}
func errStr(err error) *C.char {
if err != nil {
return ech(err)
}
return ch("")
}
//export WriteAll
func WriteAll(text *C.char) *C.char {
err := robotgo.WriteAll(str(text))
return errStr(err)
}
//export PasteStr
func PasteStr(text *C.char) {
robotgo.PasteStr(str(text))
}
/*
.______ __ .___________..___ ___. ___ .______
| _ \ | | | || \/ | / \ | _ \
| |_) | | | `---| |----`| \ / | / ^ \ | |_) |
| _ < | | | | | |\/| | / /_\ \ | ___/
| |_) | | | | | | | | | / _____ \ | |
|______/ |__| |__| |__| |__| /__/ \__\ | _|
*/
func toBitmap(imgBuf *uint8, width, height, bytewidth int,
bitsPixel, bytesPerPixel uint8) robotgo.Bitmap {
return robotgo.Bitmap{
ImgBuf: imgBuf,
Width: width,
Height: height,
Bytewidth: bytewidth,
BitsPixel: bitsPixel,
BytesPerPixel: bytesPerPixel,
}
}
//export GetText
func GetText(path *C.char) (*C.char, *C.char) {
s, err := robotgo.GetText(str(path))
if err != nil {
return ch(s), ech(err)
}
return ch(s), ch("")
}
//export OpenBitmapArgs
func OpenBitmapArgs(path *C.char) (*uint8, int, int, int,
uint8, uint8) {
cbit := robotgo.OpenBitmap(str(path))
gbit := robotgo.ToBitmap(cbit)
return gbit.ImgBuf, gbit.Width, gbit.Height, gbit.Bytewidth,
gbit.BitsPixel, gbit.BytesPerPixel
}
//export FreeBitmap
func FreeBitmap(imgBuf *uint8, width, height, bytewidth int,
bitsPixel, bytesPerPixel uint8) {
gbit := toBitmap(imgBuf, width, height, bytewidth, bitsPixel, bytesPerPixel)
cbit := robotgo.ToCBitmap(gbit)
robotgo.FreeBitmap(cbit)
}
//export FindBitmapArgs
func FindBitmapArgs(imgBuf *uint8, width, height, bytewidth int,
bitsPixel, bytesPerPixel uint8) (int, int) {
gbit := toBitmap(imgBuf, width, height, bytewidth, bitsPixel, bytesPerPixel)
cbit := robotgo.ToCBitmap(gbit)
return robotgo.FindBitmap(cbit)
}
//export SaveBitmapArgs
func SaveBitmapArgs(path *C.char, imgBuf *uint8, width, height, bytewidth int,
bitsPixel, bytesPerPixel uint8) *C.char {
gbit := toBitmap(imgBuf, width, height, bytewidth, bitsPixel, bytesPerPixel)
cbit := robotgo.ToCBitmap(gbit)
s := robotgo.SaveBitmap(cbit, str(path))
return ch(s)
}
//export ToStrBitmap
func ToStrBitmap(imgBuf *uint8, width, height, bytewidth int,
bitsPixel, bytesPerPixel uint8) *C.char {
gbit := toBitmap(imgBuf, width, height, bytewidth, bitsPixel, bytesPerPixel)
cbit := robotgo.ToCBitmap(gbit)
b := robotgo.TostringBitmap(cbit)
return ch(b)
}
//export BitmapFromStr
func BitmapFromStr(bit *C.char) (*uint8, int, int, int,
uint8, uint8) {
cbit := robotgo.BitmapFromStr(str(bit))
gbit := robotgo.ToBitmap(cbit)
return gbit.ImgBuf, gbit.Width, gbit.Height, gbit.Bytewidth,
gbit.BitsPixel, gbit.BytesPerPixel
}
//export CaptureBitmapStr
func CaptureBitmapStr(x, y, w, h int) *C.char {
bit := robotgo.CaptureScreen(x, y, w, h)
str := robotgo.TostringBitmap(bit)
return ch(str)
}
//export OpenBitmapStr
func OpenBitmapStr(path *C.char) *C.char {
bit := robotgo.OpenBitmap(str(path))
s := robotgo.TostringBitmap(bit)
return ch(s)
}
//export SaveBitmapStr
func SaveBitmapStr(bit, path *C.char) *C.char {
bitmap := robotgo.BitmapStr(str(bit))
err := robotgo.SaveBitmap(bitmap, str(path))
return ch(err)
}
//export FindBitmapStr
func FindBitmapStr(c *C.char) (int, int) {
bit := robotgo.BitmapStr(str(c))
return robotgo.FindBitmap(bit)
}
//export FindPic
func FindPic(path *C.char) (int, int) {
return robotgo.FindPic(str(path))
}
//export GetImgSize
func GetImgSize(path *C.char) (int, int) {
return robotgo.GetImgSize(str(path))
}
//export FindColor
func FindColor(color uint32) (int, int) {
x, y := robotgo.FindColor(robotgo.UintToHex(color))
return x, y
}
//export FindColorCS
func FindColorCS(color uint32, x, y, w, h int) (int, int) {
fx, fy := robotgo.FindColorCS(robotgo.UintToHex(color), x, y, w, h)
return fx, fy
}
/*
___________ ____ _______ .__ __. .___________.
| ____\ \ / / | ____|| \ | | | |
| |__ \ \/ / | |__ | \| | `---| |----`
| __| \ / | __| | . ` | | |
| |____ \ / | |____ | |\ | | |
|_______| \__/ |_______||__| \__| |__|
*/
//export AddEvent
func AddEvent(key *C.char) bool {
return robotgo.AddEvent(str(key))
}
//export StopEvent
func StopEvent() {
robotgo.StopEvent()
}
//export AddEvents
func AddEvents(key, args *C.char) bool {
arr := strings.Split(str(args), ",")
return robotgo.AddEvents(str(key), arr...)
}
//export End
func End() {
robotgo.End()
}
//export AddMouse
func AddMouse(btn *C.char, x, y int16) bool {
if x == -1 {
b := robotgo.AddMouse(str(btn))
return b
}
b := robotgo.AddMouse(str(btn), x, y)
return b
}
//export AddMousePos
func AddMousePos(x, y int16) bool {
b := robotgo.AddMousePos(x, y)
return b
}
/*
____ __ ____ __ .__ __. _______ ______ ____ __ ____
\ \ / \ / / | | | \ | | | \ / __ \ \ \ / \ / /
\ \/ \/ / | | | \| | | .--. | | | | \ \/ \/ /
\ / | | | . ` | | | | | | | | \ /
\ /\ / | | | |\ | | '--' | `--' | \ /\ /
\__/ \__/ |__| |__| \__| |_______/ \______/ \__/ \__/
*/
//export ShowAlert
func ShowAlert(title, msg *C.char) bool {
return robotgo.ShowAlert(str(title), str(msg))
}
//export GetTitle
func GetTitle(pid int32) *C.char {
if pid == -1 {
title := robotgo.GetTitle()
return ch(title)
}
title := robotgo.GetTitle(pid)
return ch(title)
}
//export GetBounds
func GetBounds(pid int32) (int, int, int, int) {
return robotgo.GetBounds(pid)
}
//export PidExists
func PidExists(pid int32) (bool, *C.char) {
b, err := robotgo.PidExists(pid)
if err != nil {
return b, ech(err)
}
return b, ch("")
}
//export FindIds
func FindIds(name *C.char) (*C.char, *C.char) {
arr, err := robotgo.FindIds(str(name))
sb := toStr(arr)
if err != nil {
return ch(sb), ech(err)
}
return ch(sb), ch("")
}
//export FindName
func FindName(pid int32) (*C.char, *C.char) {
sb, err := robotgo.FindName(pid)
if err != nil {
return ch(sb), ech(err)
}
return ch(sb), ch("")
}
//export FindNames
func FindNames() (*C.char, *C.char) {
arr, err := robotgo.FindNames()
sb := toStr(arr)
if err != nil {
return ch(sb), ech(err)
}
return ch(sb), ch("")
}
//export FindPath
func FindPath(pid int32) (*C.char, *C.char) {
sb, err := robotgo.FindPath(pid)
if err != nil {
return ch(sb), ech(err)
}
return ch(sb), ch("")
}
//export ActivePID
func ActivePID(pid int32) (c *C.char) {
err := robotgo.ActivePID(pid)
if err != nil {
c = ech(err)
}
return
}
//export ActiveName
func ActiveName(name *C.char) (c *C.char) {
err := robotgo.ActiveName(str(name))
if err != nil {
c = ech(err)
}
return
}
//export Kill
func Kill(pid int32) *C.char {
err := robotgo.Kill(pid)
return errStr(err)
}
func main() {} // Required but ignored