-
Notifications
You must be signed in to change notification settings - Fork 8
/
pointersocket.go
89 lines (76 loc) · 1.93 KB
/
pointersocket.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
package webostv
import (
"fmt"
"github.com/gorilla/websocket"
"sync"
)
type PointerSocket struct {
Address string
ws *websocket.Conn
sync.Mutex
}
func (dialer *Dialer) DialPointerSocket(address string) (ps *PointerSocket, err error) {
wsDialer := dialer.WebsocketDialer
if wsDialer == nil {
wsDialer = websocket.DefaultDialer
}
ws, resp, err := wsDialer.Dial(address, nil)
if err != nil {
return nil, err
}
err = resp.Body.Close()
if err != nil {
return nil, err
}
return &PointerSocket{
Address: address,
ws: ws,
}, nil
}
func (tv *Tv) NewPointerSocket() (ps *PointerSocket, err error) {
socketPath, err := tv.GetPointerInputSocket()
if err != nil {
return nil, err
}
return DefaultDialer.DialPointerSocket(socketPath)
}
func (ps *PointerSocket) MessageHandler() (err error) {
for {
_, _, err = ps.ws.ReadMessage()
if err != nil {
return err
}
}
// not reached
}
func (ps *PointerSocket) Close() (err error) {
ps.Lock()
defer ps.Unlock()
if ps.ws != nil {
err = ps.ws.Close()
ps.ws = nil
}
return err
}
func (ps *PointerSocket) writeMessage(messageType int, data []byte) error {
ps.Lock()
defer ps.Unlock()
return ps.ws.WriteMessage(messageType, data)
}
func (ps *PointerSocket) Input(btype, bname string) (err error) {
msg := "type:" + btype + "\n" + "name:" + bname + "\n\n"
return ps.writeMessage(websocket.TextMessage, []byte(msg))
}
// UP DOWN LEFT RIGHT HOME BACK DASH and numbers
func (ps *PointerSocket) Move(dx, dy int) (err error) {
msg := fmt.Sprintf("type:move\ndx:%d\ndy:%d\ndown:0\n\n", dx, dy)
return ps.writeMessage(websocket.TextMessage, []byte(msg))
}
func (ps *PointerSocket) Scroll(dx, dy int) (err error) {
msg := fmt.Sprintf("type:scroll\ndx:%d\ndy:%d\ndown:0\n\n", dx, dy)
return ps.writeMessage(websocket.TextMessage, []byte(msg))
}
func (ps *PointerSocket) Click() (err error) {
msg := "type: click\n\n"
return ps.writeMessage(websocket.TextMessage, []byte(msg))
}