-
Notifications
You must be signed in to change notification settings - Fork 7
/
snarf_unix.go
244 lines (211 loc) · 6.7 KB
/
snarf_unix.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
// This is a modified version of the X11 clipboard implementation in nucular
// (https://github.com/aarzilli/nucular/blob/7a48478aebff2ca5dadd95d52d34be4cb24af4ec/clipboard/clipboard_linux.go)
// distributed under the following license:
//
// The MIT License (MIT)
//
// Copyright (c) 2016 Alessandro Arzilli
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of
// this software and associated documentation files (the "Software"), to deal in
// the Software without restriction, including without limitation the rights to
// use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of
// the Software, and to permit persons to whom the Software is furnished to do so,
// subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all
// copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS
// FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR
// COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER
// IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
// +build freebsd linux,!android netbsd openbsd solaris dragonfly
package duitdraw
import (
"fmt"
"os"
"sync"
"time"
"github.com/BurntSushi/xgb"
"github.com/BurntSushi/xgb/xproto"
)
func (d *Display) readSnarf(buf []byte) (int, int, error) {
xc, err := getXClip()
if err != nil {
return 0, 0, err
}
xc.mu.Lock()
defer xc.mu.Unlock()
n, size, err := xc.getSelection(primaryAtom, buf)
if size > 0 {
return n, size, err
}
return xc.getSelection(clipboardAtom, buf)
}
func (d *Display) writeSnarf(text []byte) error {
xc, err := getXClip()
if err != nil {
return err
}
xc.mu.Lock()
defer xc.mu.Unlock()
xc.text = text
ssoc := xproto.SetSelectionOwnerChecked(xc.conn, xc.win, clipboardAtom, xproto.TimeCurrentTime)
if err := ssoc.Check(); err != nil {
return fmt.Errorf("error setting clipboard: %v", err)
}
ssoc = xproto.SetSelectionOwnerChecked(xc.conn, xc.win, primaryAtom, xproto.TimeCurrentTime)
if err := ssoc.Check(); err != nil {
return fmt.Errorf("error setting primary selection: %v", err)
}
return nil
}
const debugClipboardRequests = false
type xClip struct {
conn *xgb.Conn
win xproto.Window
text []byte
selnotify chan bool
err error
mu sync.Mutex
}
var clipboardAtom, primaryAtom, textAtom, targetsAtom, atomAtom xproto.Atom
var (
xclip *xClip
xclipOnce sync.Once
)
func getXClip() (*xClip, error) {
xclipOnce.Do(func() {
var xc xClip
xclip = &xc
xc.conn, xc.err = xgb.NewConnDisplay("")
if xc.err != nil {
return
}
xc.selnotify = make(chan bool, 1)
xc.win, xc.err = xproto.NewWindowId(xc.conn)
if xc.err != nil {
return
}
setup := xproto.Setup(xc.conn)
s := setup.DefaultScreen(xc.conn)
xc.err = xproto.CreateWindowChecked(xc.conn, s.RootDepth, xc.win, s.Root, 100, 100, 1, 1, 0, xproto.WindowClassInputOutput, s.RootVisual, 0, []uint32{}).Check()
if xc.err != nil {
return
}
clipboardAtom = xc.internAtom("CLIPBOARD")
primaryAtom = xc.internAtom("PRIMARY")
textAtom = xc.internAtom("UTF8_STRING")
targetsAtom = xc.internAtom("TARGETS")
atomAtom = xc.internAtom("ATOM")
go xc.eventLoop()
})
return xclip, xclip.err
}
func (xc *xClip) setError(err error) {
if xc.err == nil && err != nil {
xc.err = err
}
}
func (xc *xClip) getSelection(selAtom xproto.Atom, buf []byte) (int, int, error) {
err := xproto.ConvertSelectionChecked(xc.conn, xc.win, selAtom, textAtom, selAtom, xproto.TimeCurrentTime).Check()
if err != nil {
return 0, 0, err
}
select {
case r := <-xc.selnotify:
if !r {
return 0, 0, fmt.Errorf("bad response from selection owner")
}
gpr, err := xproto.GetProperty(xc.conn, true, xc.win, selAtom, textAtom, 0, uint32(len(buf))).Reply()
if err != nil {
return 0, 0, err
}
n := copy(buf, gpr.Value[:gpr.ValueLen])
if n < int(gpr.ValueLen) || gpr.BytesAfter != 0 {
return n, int(gpr.ValueLen + gpr.BytesAfter), errShortSnarfBuffer
}
return n, n, nil
case <-time.After(1 * time.Second):
return 0, 0, fmt.Errorf("clipboard retrieval failed, timeout")
}
}
func (xc *xClip) eventLoop() {
targetAtoms := []xproto.Atom{targetsAtom, textAtom}
for {
e, err := xc.conn.WaitForEvent()
if err != nil {
continue
}
switch e := e.(type) {
case xproto.SelectionRequestEvent: // write snarf
if debugClipboardRequests {
tgtname := xc.lookupAtom(e.Target)
fmt.Fprintln(os.Stderr, "SelectionRequest", e, textAtom, tgtname, "isPrimary:", e.Selection == primaryAtom, "isClipboard:", e.Selection == clipboardAtom)
}
t := xc.text
switch e.Target {
case textAtom:
if debugClipboardRequests {
fmt.Fprintln(os.Stderr, "Sending as text")
}
err := xproto.ChangePropertyChecked(xc.conn, xproto.PropModeReplace, e.Requestor, e.Property, textAtom, 8, uint32(len(t)), []byte(t)).Check()
if err == nil {
xc.sendSelectionNotify(e)
} else {
fmt.Fprintf(os.Stderr, "duitdraw: %v\n", err)
}
case targetsAtom:
if debugClipboardRequests {
fmt.Fprintln(os.Stderr, "Sending targets")
}
buf := make([]byte, len(targetAtoms)*4)
for i, atom := range targetAtoms {
xgb.Put32(buf[i*4:], uint32(atom))
}
xproto.ChangePropertyChecked(xc.conn, xproto.PropModeReplace, e.Requestor, e.Property, atomAtom, 32, uint32(len(targetAtoms)), buf).Check()
if err == nil {
xc.sendSelectionNotify(e)
} else {
fmt.Fprintf(os.Stderr, "duitdraw: %v\n", err)
}
default:
if debugClipboardRequests {
fmt.Fprintln(os.Stderr, "Skipping")
}
e.Property = 0
xc.sendSelectionNotify(e)
}
case xproto.SelectionNotifyEvent: // read snarf
xc.selnotify <- (e.Property == clipboardAtom) || (e.Property == primaryAtom)
}
}
}
func (xc *xClip) sendSelectionNotify(e xproto.SelectionRequestEvent) {
sn := xproto.SelectionNotifyEvent{
Time: xproto.TimeCurrentTime,
Requestor: e.Requestor,
Selection: e.Selection,
Target: e.Target,
Property: e.Property,
}
err := xproto.SendEventChecked(xc.conn, false, e.Requestor, 0, string(sn.Bytes())).Check()
if err != nil {
fmt.Fprintf(os.Stderr, "duitdraw: %v\n", err)
}
}
func (xc *xClip) internAtom(n string) xproto.Atom {
iar, err := xproto.InternAtom(xc.conn, true, uint16(len(n)), n).Reply()
xc.setError(err)
return iar.Atom
}
func (xc *xClip) lookupAtom(at xproto.Atom) string {
reply, err := xproto.GetAtomName(xc.conn, at).Reply()
if err != nil {
panic(err)
}
return string(reply.Name)
}