This repository has been archived by the owner on Sep 14, 2019. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 1
/
main.go
253 lines (227 loc) · 5.87 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
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
package main
import (
"flag"
"fmt"
"html/template"
"net"
"net/http"
"path/filepath"
"regexp"
"strconv"
"strings"
control "github.com/go-vgo/robotgo"
"github.com/gorilla/websocket"
)
// Config is used to decode the config.json
// file into a Config variable
type Config struct {
ScreenWidth float64
ScreenHeight float64
Port string
IP string
}
type finger struct {
direction int
swiping bool
initX float64
initY float64
}
var (
upgrader websocket.Upgrader
pressing bool
config Config
// IPPattern is used by regexp to check if it is a valid IP
IPPattern = "((25[0-5]|2[0-4][0-9]|[01]?[0-9][0-9]?)(\\.|$)){4}"
// DeviceWidth refers to the phone or tablet
// screen width
DeviceWidth float64
// DeviceHeight refers to the phone or tablet
// screen height
DeviceHeight float64
fingers [2]finger
)
func moveMouse(x, y float64) {
// height ratio * screen width
tx := int((y / DeviceHeight) * config.ScreenWidth)
// width ratio (inverted) * screen width
ty := int(((DeviceWidth - x) / DeviceWidth) * config.ScreenHeight)
control.Move(tx, ty)
}
func setPressing(b bool) {
pressing = b
if b {
control.MouseToggle("down")
} else {
control.MouseToggle("up")
}
}
func zoom(dir string) {
control.KeyToggle("control", "down")
control.ScrollMouse(1, dir)
control.KeyToggle("control", "up")
}
func swipe(direction int, initX float64, initY float64, id int) {
fingers[id].direction = direction
fingers[id].swiping = true
fingers[id].initX = initX
fingers[id].initY = initY
if fingers[0].swiping && fingers[1].swiping { // both are swiping
if (fingers[0].direction+fingers[1].direction)%2 == 0 {
switch fingers[0].direction {
case 4:
if fingers[0].initY < fingers[1].initY {
zoom("up")
} else {
zoom("down")
}
case 2:
if fingers[0].initY > fingers[1].initY {
zoom("up")
} else {
zoom("down")
}
case 1:
if fingers[0].initX > fingers[1].initX {
zoom("up")
} else {
zoom("down")
}
case 3:
if fingers[0].initX < fingers[1].initX {
zoom("up")
} else {
zoom("down")
}
}
}
}
}
func selectIP() string {
interfaces, err := net.Interfaces()
if err != nil {
panic(err)
}
var ips []string
added := make(map[string]bool)
fmt.Println("Choose your local IP\n===================")
for _, i := range interfaces {
addrs, err := i.Addrs()
if err != nil {
// ignore for now
}
for _, addr := range addrs {
var ip net.IP
switch v := addr.(type) {
case *net.IPNet:
ip = v.IP
}
// check if ip is in the form 255.255.255.255
if matched, err := regexp.Match(IPPattern, []byte(ip.String())); !matched || err != nil {
continue
}
// Print and add to list
if _, found := added[ip.String()]; !found {
ips = append(ips, ip.String())
added[ip.String()] = true
fmt.Printf("[%d] %s\n", len(ips)-1, ip.String())
}
}
}
// read user input
fmt.Print("\n> ")
var index int
fmt.Scan(&index)
if index < 0 || index >= len(ips) {
panic(fmt.Errorf("Index out of bounds"))
}
return ips[index]
}
func loadConfig() {
// iterate all network interfaces and let the user select it
config.IP = selectIP()
// parse command line flags
flag.Float64Var(&config.ScreenWidth, "w", 1920, "screen width")
flag.Float64Var(&config.ScreenHeight, "h", 1080, "screen height")
flag.StringVar(&config.Port, "p", "8080", "website port")
flag.Parse()
}
func main() {
fingers[0].direction = 0
fingers[1].direction = 0
http.HandleFunc("/favicon.ico", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, filepath.Join("public", "static", "favicon.ico"))
})
http.HandleFunc("/static/spen-events.js", func(w http.ResponseWriter, r *http.Request) {
http.ServeFile(w, r, filepath.Join("public", "static", "spen-events.js"))
})
http.HandleFunc("/", func(w http.ResponseWriter, r *http.Request) {
// filepath.Join to automatically use the separator your OS uses
t, _ := template.ParseFiles(filepath.Join("public", "templates", "index.html"))
if t != nil {
t.Execute(w, config)
} else {
fmt.Println("Error creating template")
}
})
http.HandleFunc("/spen", func(w http.ResponseWriter, r *http.Request) {
conn, _ := upgrader.Upgrade(w, r, nil)
go func(conn *websocket.Conn) {
for {
_, msg, err := conn.ReadMessage()
if err != nil {
conn.Close()
break
}
input := strings.Split(string(msg), ",")
if len(input) == 2 {
x, _ := strconv.ParseFloat(input[0], 64)
y, _ := strconv.ParseFloat(input[1], 64)
moveMouse(x, y)
} else if input[0] == "pressing" {
setPressing(true)
} else if input[0] == "stoppressing" {
setPressing(false)
} else if input[0] == "screen" {
// Init device screen size
tempWidth, _ := strconv.Atoi(input[1])
DeviceWidth = float64(tempWidth)
tempHeight, _ := strconv.Atoi(input[2])
DeviceHeight = float64(tempHeight)
}
}
}(conn)
})
// This WebSocket will receive only the touch events from the finger
http.HandleFunc("/finger", func(w http.ResponseWriter, r *http.Request) {
conn, _ := upgrader.Upgrade(w, r, nil)
go func(conn *websocket.Conn) {
for {
_, msg, err := conn.ReadMessage()
if err != nil {
conn.Close()
break
}
//fmt.Println(string(msg))
input := strings.Split(string(msg), ",")
if len(input) == 5 {
if input[0] == "swipe" {
initX, _ := strconv.ParseFloat(input[1], 32)
initY, _ := strconv.ParseFloat(input[2], 32)
direction, _ := strconv.Atoi(input[3])
id, _ := strconv.Atoi(input[4])
swipe(direction, initX, initY, id)
}
} else if len(input) == 2 {
if input[0] == "stop" {
id, _ := strconv.Atoi(input[1])
fingers[id].swiping = false
}
}
}
}(conn)
})
loadConfig()
// run the server
fmt.Println("Server running at", config.IP+":"+config.Port)
http.ListenAndServe(config.IP+":"+config.Port, nil)
}