-
Notifications
You must be signed in to change notification settings - Fork 11
/
clipboard.go
253 lines (235 loc) · 6.68 KB
/
clipboard.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 library
import (
"bytes"
"fmt"
lua "github.com/anaminus/gopher-lua"
"github.com/anaminus/rbxmk"
"github.com/anaminus/rbxmk/dump"
"github.com/anaminus/rbxmk/dump/dt"
"github.com/anaminus/rbxmk/library/internal/clipboard"
"github.com/anaminus/rbxmk/reflect"
"github.com/anaminus/rbxmk/rtypes"
"github.com/robloxapi/types"
)
func init() { register(Clipboard) }
var Clipboard = rbxmk.Library{
Name: "clipboard",
Import: []string{"clipboard"},
Priority: 10,
Open: openClipboard,
Dump: dumpClipboard,
Types: []func() rbxmk.Reflector{
reflect.FormatSelector,
reflect.Variant,
},
}
func openClipboard(s rbxmk.State) *lua.LTable {
lib := s.L.CreateTable(0, 2)
lib.RawSetString("read", s.WrapFunc(clipboardRead))
lib.RawSetString("write", s.WrapFunc(clipboardWrite))
return lib
}
// getFormatSelectors produces a list of FormatSelectors from arguments.
func getFormatSelectors(s rbxmk.State, n int) (selectors []rtypes.FormatSelector) {
c := s.Count()
selectors = make([]rtypes.FormatSelector, 0, c-n+1)
for i := n; i <= c; i++ {
selector := s.Pull(i, rtypes.T_FormatSelector).(rtypes.FormatSelector)
selectors = append(selectors, selector)
}
return selectors
}
func clipboardRead(s rbxmk.State) int {
selectors := getFormatSelectors(s, 1)
v, err := ClipboardSource{World: s.World}.Read(selectors...)
if err != nil {
return s.RaiseError("%s", err)
}
if v == nil {
return s.Push(rtypes.Nil)
}
return s.Push(v)
}
func clipboardWrite(s rbxmk.State) int {
value := s.Pull(1, rtypes.T_Variant)
selectors := getFormatSelectors(s, 2)
err := ClipboardSource{World: s.World}.Write(value, selectors...)
if err != nil {
return s.RaiseError("%s", err)
}
return 0
}
func dumpClipboard(s rbxmk.State) dump.Library {
return dump.Library{
Struct: dump.Struct{
Fields: dump.Fields{
"read": dump.Function{
Parameters: dump.Parameters{
{Name: "...", Type: dt.Prim(rtypes.T_FormatSelector)},
},
Returns: dump.Parameters{
{Name: "value", Type: dt.Optional(dt.Prim(rtypes.T_Any))},
},
CanError: true,
Summary: "Libraries/clipboard:Fields/read/Summary",
Description: "Libraries/clipboard:Fields/read/Description",
},
"write": dump.Function{
Parameters: dump.Parameters{
{Name: "value", Type: dt.Prim(rtypes.T_Any)},
{Name: "...", Type: dt.Prim(rtypes.T_FormatSelector)},
},
CanError: true,
Summary: "Libraries/clipboard:Fields/write/Summary",
Description: "Libraries/clipboard:Fields/write/Description",
},
},
Summary: "Libraries/clipboard:Summary",
Description: "Libraries/clipboard:Description",
},
}
}
// ClipboardSource provides access to the clipboard of the operating system.
type ClipboardSource struct {
*rbxmk.World
}
// formatOptions implements rbxmk.FormatOptions.
type formatOptions struct {
Format rbxmk.Format
Options rtypes.Dictionary
}
// ValueOf returns the value of field. Returns nil if the value does not exist.
func (f formatOptions) ValueOf(field string) types.Value {
return f.Options[field]
}
// Read reads a value from the clipboard according to the given formats. If no
// formats are given, or no data is found, then nil is returned with no error.
func (s ClipboardSource) Read(formats ...rtypes.FormatSelector) (v types.Value, err error) {
if len(formats) == 0 {
return nil, nil
}
options := make([]formatOptions, 0, len(formats))
loop:
for _, selector := range formats {
format := s.Format(selector.Format)
if format.Name == "" {
return nil, fmt.Errorf("unknown format %q", selector.Format)
}
if format.Decode == nil {
return nil, fmt.Errorf("cannot decode with format %s", format.Name)
}
for _, option := range options {
if format.Name == option.Format.Name {
// Skip duplicate formats.
continue loop
}
}
options = append(options, formatOptions{
Format: format,
Options: selector.Options,
})
}
// Get list of media types from each format.
mediaTypes := []string{}
mediaFormats := []formatOptions{}
mediaDefined := map[string]struct{}{}
for _, option := range options {
for _, mediaType := range option.Format.MediaTypes {
if _, ok := mediaDefined[mediaType]; ok {
continue
}
mediaTypes = append(mediaTypes, mediaType)
mediaFormats = append(mediaFormats, option)
mediaDefined[mediaType] = struct{}{}
}
}
// Read and decode.
f, b, err := clipboard.Read(mediaTypes...)
if err != nil {
if clipboard.IsNoData(err) {
return nil, nil
}
return nil, err
}
if f < 0 {
return nil, nil
}
option := mediaFormats[f]
if option.Format.Decode == nil {
return nil, fmt.Errorf("cannot decode with format %s", option.Format.Name)
}
v, err = option.Format.Decode(s.Global, option, bytes.NewReader(b))
if err != nil {
return nil, err
}
return v, nil
}
// Write writes a value to the clipboard according to the given formats. If no
// formats are given, then the clipboard is cleared.
func (s ClipboardSource) Write(value types.Value, formats ...rtypes.FormatSelector) error {
if len(formats) == 0 {
if err := clipboard.Clear(); err != nil {
if clipboard.IsNoData(err) {
return nil
}
return err
}
}
options := make([]formatOptions, 0, len(formats))
loop:
for _, selector := range formats {
format := s.Format(selector.Format)
if format.Name == "" {
return fmt.Errorf("unknown format %q", selector.Format)
}
if format.Encode == nil {
return fmt.Errorf("cannot encode with format %s", format.Name)
}
for _, option := range options {
if format.Name == option.Format.Name {
// Skip duplicate formats.
continue loop
}
}
options = append(options, formatOptions{
Format: format,
Options: selector.Options,
})
}
// Get list of media types and content from each format. The same content is
// written for each media type defined by a format. Only the first content
// for each media type is written.
clipboardFormats := []clipboard.Format{}
mediaDefined := map[string]struct{}{}
for _, option := range options {
var w bytes.Buffer
var written bool
for _, mediaType := range option.Format.MediaTypes {
if _, ok := mediaDefined[mediaType]; ok {
continue
}
if !written {
if option.Format.Encode == nil {
return fmt.Errorf("cannot encode with format %s", option.Format.Name)
}
if err := option.Format.Encode(s.Global, option, &w, value); err != nil {
return err
}
written = true
}
clipboardFormats = append(clipboardFormats, clipboard.Format{
Name: mediaType,
Content: w.Bytes(),
})
mediaDefined[mediaType] = struct{}{}
}
}
// Write to clipboard.
if err := clipboard.Write(clipboardFormats); err != nil {
if clipboard.IsNoData(err) {
return nil
}
return err
}
return nil
}