-
Notifications
You must be signed in to change notification settings - Fork 15
/
main.go
110 lines (95 loc) · 2.24 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
// Currently this is a sample
package main
import (
"flag"
"io/ioutil"
"log"
"net/http"
"net/url"
"os"
"strings"
"github.com/netease/airinput/go-airinput"
"github.com/sevlyar/go-daemon"
)
var (
// m = macaron.Classic()
)
const SAVEFILE = "/data/local/tmp/cache-airinput.txt"
var (
addr = flag.String("addr", ":21000", "listen address")
debug = flag.Bool("debug", false, "enable debug")
isDaemon = flag.Bool("daemon", false, "run as daemon")
fix = flag.Bool("fix", false, "fix unexpected problem caused by airinput")
tpevent = flag.String("i", "", "touchpad event, eg: /dev/input/event1")
remote = flag.String("remote", "", "remote control center, eg: 10.0.0.1:9000")
runjs = flag.String("runjs", "", "javascript code to run")
test = flag.Bool("test", false, "just test for develop")
quite = flag.Bool("q", false, "donot show debug info")
)
func lprintf(format string, v ...interface{}) {
if !*quite {
log.Printf(format, v...)
}
}
func main() {
flag.Parse()
airinput.Debug(*debug)
if *fix {
airinput.Release()
return
}
if *tpevent == "" {
var err error
var data []byte
if data, err = ioutil.ReadFile(SAVEFILE); err == nil {
if strings.HasPrefix(string(data), "/dev/input/") {
*tpevent = strings.TrimSpace(string(data))
}
} else {
if *tpevent, err = airinput.GuessTouchpad(); err != nil {
log.Fatal(err)
}
if fd, err := os.Create(SAVEFILE); err == nil {
defer fd.Close()
fd.Write([]byte(*tpevent))
}
}
lprintf("Use tpd event: %s\n", *tpevent)
}
// initial
if err := airinput.Init(*tpevent); err != nil {
lprintf("initial\n")
log.Fatal(err)
}
if *test {
lprintf("rotation: %d", airinput.Rotation())
return
}
if *runjs != "" {
RunJS(*runjs)
return
}
if *remote != "" {
r, err := http.Get("http://" + *remote + "/connect?serialno=" + url.QueryEscape(SerialNo()))
if err == nil {
lprintf("Remote connected\n")
r.Body.Close()
}
}
ipinfo, _ := MyIP()
lprintf("IP: %v\n", ipinfo)
lprintf("Listen on: %v\n", *addr)
if *isDaemon {
context := new(daemon.Context)
child, _ := context.Reborn()
if child != nil {
println("daemon started")
} else {
defer context.Release()
ServeWeb(*addr)
}
} else {
ServeWeb(*addr)
return
}
}