-
Notifications
You must be signed in to change notification settings - Fork 43
/
Copy pathmain.go
181 lines (153 loc) · 4.43 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
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
package main
import (
"flag"
"io/ioutil"
"log"
"path/filepath"
"strconv"
"time"
"github.com/ClarkGuan/go-sdl2/sdl"
"github.com/ClarkGuan/scrcpy-go/scrcpy"
"gopkg.in/yaml.v2"
)
type EntryFile struct {
Entries []*Entry `yaml:"keys"`
Args []*Arg `yaml:"args"`
Hits []int `yaml:"hits"`
Stables []*Stable `yaml:"stable"`
}
type Stable struct {
Pixel int `yaml:"pixel"`
Delay int `yaml:"delay"`
}
type Arg struct {
Name string `yaml:"name"`
Value string `yaml:"value"`
}
type Entry struct {
Code string `yaml:"code"`
Point *EntryPoint `yaml:"point"`
Comment string `yaml:"comment"`
Macro []*EntryMacro `yaml:"macro"`
ShowPointer bool `yaml:"show_pointer"`
Type string `yaml:"type"`
}
type EntryPoint struct {
X int `yaml:"x"`
Y int `yaml:"y"`
}
type EntryMacro struct {
Point *EntryPoint `yaml:"point"`
Delay int `yaml:"delay"`
}
func main() {
log.Printf("SDL %d.%d.%d\n", sdl.MAJOR_VERSION, sdl.MINOR_VERSION, sdl.PATCHLEVEL)
var debugLevel int
var bitRate int
var port int
var settingFile string
var sensitive float64
var overTcp bool
flag.IntVar(&debugLevel, "log", 0, "日志等级设置")
flag.IntVar(&bitRate, "bitrate", 8000000, "视频码率")
flag.IntVar(&port, "port", 27183, "adb 端口号")
flag.StringVar(&settingFile, "cfg", filepath.Join(sdl.GetBasePath(), "res", "settings.yml"), "配置文件路径")
flag.Float64Var(&sensitive, "sens", scrcpy.DefaultMouseSensitive, "鼠标精度")
flag.BoolVar(&overTcp, "overtcp", false, "通过局域网连接")
flag.Parse()
content, err := ioutil.ReadFile(settingFile)
if err != nil {
log.Fatalln(err)
}
var entryFile EntryFile
if err = yaml.Unmarshal(content, &entryFile); err != nil {
log.Fatalln(err)
}
keyMap, ctrlKeyMap := make(map[int]scrcpy.UserOperation), make(map[int]scrcpy.UserOperation)
mouseKeyMap := make(map[uint8]scrcpy.UserOperation)
for _, entry := range entryFile.Entries {
switch entry.Type {
case "":
if keyCode, ok := scrcpy.KeyCodeConstMap[entry.Code]; ok {
keyMap[keyCode] = parseUserOperation(entry)
} else {
keyCode = int(sdl.GetKeyFromName(entry.Code))
if keyCode == sdl.K_UNKNOWN {
log.Fatalln("unknown key code:", entry.Code)
}
keyMap[keyCode] = parseUserOperation(entry)
}
case "ctrl":
if keyCode, ok := scrcpy.KeyCodeConstMap[entry.Code]; ok {
ctrlKeyMap[keyCode] = parseUserOperation(entry)
} else {
keyCode = int(sdl.GetKeyFromName(entry.Code))
if keyCode == sdl.K_UNKNOWN {
log.Fatalln("unknown key code:", entry.Code)
}
ctrlKeyMap[keyCode] = parseUserOperation(entry)
}
case "mouse":
if keyCode, ok := scrcpy.MouseButtonMap[entry.Code]; ok {
mouseKeyMap[keyCode] = parseUserOperation(entry)
} else {
log.Fatalln("unknown mouse code:", entry.Code)
}
default:
panic("can't reach here")
}
}
for _, arg := range entryFile.Args {
switch arg.Name {
case "log":
debugLevel, _ = strconv.Atoi(arg.Value)
case "bitrate":
bitRate, _ = strconv.Atoi(arg.Value)
case "port":
port, _ = strconv.Atoi(arg.Value)
case "sens":
sensitive, _ = strconv.ParseFloat(arg.Value, 64)
case "overtcp":
overTcp = true
}
}
if overTcp && port == 27183 {
port = 10240
}
option := scrcpy.Option{
Debug: scrcpy.DebugLevelWrap(debugLevel),
BitRate: bitRate,
Port: port,
KeyMap: keyMap,
CtrlKeyMap: ctrlKeyMap,
MouseKeyMap: mouseKeyMap,
MouseSensitive: sensitive,
OverTcp: overTcp,
}
for _, n := range entryFile.Hits {
option.Hits = append(option.Hits, time.Duration(n)*time.Millisecond)
}
for _, s := range entryFile.Stables {
option.Stables = append(option.Stables, &scrcpy.GunPressConfig{Delta: int32(s.Pixel), Interval: time.Duration(s.Delay) * time.Millisecond})
}
log.Println(scrcpy.Main(&option))
}
func parseUserOperation(entry *Entry) scrcpy.UserOperation {
if entry.Point != nil {
if entry.ShowPointer {
return &scrcpy.SPoint{X: uint16(entry.Point.X), Y: uint16(entry.Point.Y)}
} else {
return &scrcpy.Point{uint16(entry.Point.X), uint16(entry.Point.Y)}
}
} else if len(entry.Macro) > 0 {
var list []*scrcpy.PointMacro
for _, m := range entry.Macro {
list = append(list, &scrcpy.PointMacro{
Point: scrcpy.Point{X: uint16(m.Point.X), Y: uint16(m.Point.Y)},
Interval: time.Duration(m.Delay) * time.Millisecond})
}
return list
} else {
panic("can't reach here")
}
}