-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathRegistry.go
126 lines (102 loc) · 2.36 KB
/
Registry.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
package gowl
import (
"bytes"
)
var _ bytes.Buffer
type Registry struct {
id int32
globalListeners []chan RegistryGlobal
globalRemoveListeners []chan RegistryGlobalRemove
events []func(r *Registry, msg message) error
name string
}
func NewRegistry() (r *Registry) {
r = new(Registry)
r.name = "Registry"
r.globalListeners = make([]chan RegistryGlobal, 0)
r.globalRemoveListeners = make([]chan RegistryGlobalRemove, 0)
r.events = append(r.events, registryGlobal)
r.events = append(r.events, registryGlobalRemove)
return
}
func (r *Registry) HandleEvent(msg message) {
if r.events[msg.opcode] != nil {
r.events[msg.opcode](r, msg)
}
}
func (r *Registry) SetId(id int32) {
r.id = id
}
func (r *Registry) Id() int32 {
return r.id
}
func (r *Registry) Name() string {
return r.name
}
////
//// REQUESTS
////
func (r *Registry) Bind(name uint32, iface string, version uint32, id Object) {
sendrequest(r, "wl_registry_bind", name, iface, version, id)
}
////
//// EVENTS
////
type RegistryGlobal struct {
Name uint32
Iface string
Version uint32
}
func (r *Registry) AddGlobalListener(channel chan RegistryGlobal) {
r.globalListeners = append(r.globalListeners, channel)
}
func registryGlobal(r *Registry, msg message) (err error) {
var data RegistryGlobal
// Read name
name,err := readUint32(msg.buf)
if err != nil {
return
}
data.Name = name
// Read iface
iface,err := readString(msg.buf)
if err != nil {
return
}
data.Iface = iface
// Read version
version,err := readUint32(msg.buf)
if err != nil {
return
}
data.Version = version
// Dispatch events
for _,channel := range r.globalListeners {
go func () {
channel <- data
} ()
}
return
}
type RegistryGlobalRemove struct {
Name uint32
}
func (r *Registry) AddGlobalRemoveListener(channel chan RegistryGlobalRemove) {
r.globalRemoveListeners = append(r.globalRemoveListeners, channel)
}
func registryGlobalRemove(r *Registry, msg message) (err error) {
var data RegistryGlobalRemove
// Read name
name,err := readUint32(msg.buf)
if err != nil {
return
}
data.Name = name
// Dispatch events
for _,channel := range r.globalRemoveListeners {
go func () {
channel <- data
} ()
}
return
}