-
Notifications
You must be signed in to change notification settings - Fork 8
/
Copy pathDataSource.go
146 lines (116 loc) · 2.81 KB
/
DataSource.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
package gowl
import (
"bytes"
)
var _ bytes.Buffer
type DataSource struct {
id int32
targetListeners []chan DataSourceTarget
sendListeners []chan DataSourceSend
cancelledListeners []chan DataSourceCancelled
events []func(d *DataSource, msg message) error
name string
}
func NewDataSource() (d *DataSource) {
d = new(DataSource)
d.name = "DataSource"
d.targetListeners = make([]chan DataSourceTarget, 0)
d.sendListeners = make([]chan DataSourceSend, 0)
d.cancelledListeners = make([]chan DataSourceCancelled, 0)
d.events = append(d.events, dataSourceTarget)
d.events = append(d.events, dataSourceSend)
d.events = append(d.events, dataSourceCancelled)
return
}
func (d *DataSource) HandleEvent(msg message) {
if d.events[msg.opcode] != nil {
d.events[msg.opcode](d, msg)
}
}
func (d *DataSource) SetId(id int32) {
d.id = id
}
func (d *DataSource) Id() int32 {
return d.id
}
func (d *DataSource) Name() string {
return d.name
}
////
//// REQUESTS
////
func (d *DataSource) Offer(typ string) {
sendrequest(d, "wl_data_source_offer", typ)
}
func (d *DataSource) Destroy() {
sendrequest(d, "wl_data_source_destroy", )
}
////
//// EVENTS
////
type DataSourceTarget struct {
MimeType string
}
func (d *DataSource) AddTargetListener(channel chan DataSourceTarget) {
d.targetListeners = append(d.targetListeners, channel)
}
func dataSourceTarget(d *DataSource, msg message) (err error) {
var data DataSourceTarget
// Read mime_type
mime_type,err := readString(msg.buf)
if err != nil {
return
}
data.MimeType = mime_type
// Dispatch events
for _,channel := range d.targetListeners {
go func () {
channel <- data
} ()
}
return
}
type DataSourceSend struct {
MimeType string
Fd uintptr
}
func (d *DataSource) AddSendListener(channel chan DataSourceSend) {
d.sendListeners = append(d.sendListeners, channel)
}
func dataSourceSend(d *DataSource, msg message) (err error) {
var data DataSourceSend
// Read mime_type
mime_type,err := readString(msg.buf)
if err != nil {
return
}
data.MimeType = mime_type
// Read fd
fd,err := msg.fd, nil
if err != nil {
return
}
data.Fd = fd
// Dispatch events
for _,channel := range d.sendListeners {
go func () {
channel <- data
} ()
}
return
}
type DataSourceCancelled struct {
}
func (d *DataSource) AddCancelledListener(channel chan DataSourceCancelled) {
d.cancelledListeners = append(d.cancelledListeners, channel)
}
func dataSourceCancelled(d *DataSource, msg message) (err error) {
var data DataSourceCancelled
// Dispatch events
for _,channel := range d.cancelledListeners {
go func () {
channel <- data
} ()
}
return
}