-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.go
150 lines (123 loc) · 3.04 KB
/
main.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
package main
import (
"bytes"
"fmt"
"image"
"image/png"
"log"
"os/exec"
"strings"
"time"
"github.com/disintegration/imaging"
"github.com/faiface/pixel"
"github.com/faiface/pixel/pixelgl"
"golang.org/x/image/colornames"
)
type ScreenSize struct {
Width uint16
Height uint16
}
type ScreenPoint struct {
X int32
Y int32
}
func main() {
pixelgl.Run(run)
}
func run() {
cfg := pixelgl.WindowConfig{
Title: "gomirror",
Bounds: pixel.R(0, 0, 400, 855),
VSync: true,
}
win, err := pixelgl.NewWindow(cfg)
if err != nil {
log.Fatal(err)
}
var (
sprite *pixel.Sprite
prevScreen image.Image
screenWidth = float64(win.Bounds().W())
screenHeight = float64(win.Bounds().H())
screenOffsetX = win.Bounds().Min.X
screenOffsetY = win.Bounds().Min.Y
screenToWindow = pixel.IM.Moved(win.Bounds().Center())
screenSize ScreenSize
)
for !win.Closed() {
img, err := captureScreen()
if err != nil {
log.Fatal(err)
}
img = imaging.Resize(img, int(screenWidth), int(screenHeight), imaging.Lanczos)
if prevScreen == nil || !imagesEqual(prevScreen, img) {
prevScreen = img
pic := pixel.PictureDataFromImage(img)
if sprite == nil {
sprite = pixel.NewSprite(pic, pic.Bounds())
} else {
sprite.Set(pic, pic.Bounds())
}
win.Clear(colornames.Black)
sprite.Draw(win, screenToWindow)
win.Update()
if win.JustPressed(pixelgl.MouseButtonLeft) {
mousePos := win.MousePosition()
clickX := int(mousePos.X - screenOffsetX)
clickY := int(mousePos.Y - screenOffsetY)
screenX, screenY := convertToScreenCoordinates(clickX, clickY, screenSize.Width, screenSize.Height)
err := tapScreen(screenX, screenY)
if err != nil {
log.Println(err)
}
}
} else {
time.Sleep(time.Millisecond * 10)
}
}
}
func captureScreen() (image.Image, error) {
cmd := exec.Command("adb", "exec-out", "screencap", "-p")
output, err := cmd.Output()
if err != nil {
return nil, err
}
if !strings.HasPrefix(string(output), "\x89PNG") {
return nil, fmt.Errorf("erro ao receber a captura de tela")
}
img, err := png.Decode(bytes.NewReader(output))
if err != nil {
return nil, err
}
return img, nil
}
func convertToScreenCoordinates(clickX, clickY int, screenWidth, screenHeight uint16) (int, int) {
screenWidthFloat := float64(screenWidth)
screenHeightFloat := float64(screenHeight)
screenX := int((float64(clickX) / screenWidthFloat) * screenWidthFloat)
screenY := int((float64(clickY) / screenHeightFloat) * screenHeightFloat)
return screenX, screenY
}
func tapScreen(x, y int) error {
cmd := exec.Command("adb", "shell", "input", "tap", fmt.Sprintf("%d", x), fmt.Sprintf("%d", y))
err := cmd.Run()
if err != nil {
return err
}
return nil
}
func imagesEqual(img1, img2 image.Image) bool {
bounds1 := img1.Bounds()
bounds2 := img2.Bounds()
if bounds1 != bounds2 {
return false
}
for y := bounds1.Min.Y; y < bounds1.Max.Y; y++ {
for x := bounds1.Min.X; x < bounds1.Max.X; x++ {
if img1.At(x, y) != img2.At(x, y) {
return false
}
}
}
return true
}