-
Notifications
You must be signed in to change notification settings - Fork 0
/
glippy_linux.go
204 lines (174 loc) · 4.82 KB
/
glippy_linux.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
//go:build (linux && !android) || freebsd
// +build linux,!android freebsd
package glippy
import (
"fmt"
"os"
"time"
"github.com/jezek/xgb"
"github.com/jezek/xgb/xproto"
)
const debugClipboardRequests = false
var X *xgb.Conn
var win xproto.Window
var clipboardText string
var selnotify chan bool
var clipboardAtom, primaryAtom, textAtom, targetsAtom, atomAtom xproto.Atom
var targetAtoms []xproto.Atom
var clipboardAtomCache = map[xproto.Atom]string{}
func start() {
var err error
X, err = xgb.NewConnDisplay("")
if err != nil {
panic(err)
}
selnotify = make(chan bool, 1)
win, err = xproto.NewWindowId(X)
if err != nil {
panic(err)
}
setup := xproto.Setup(X)
s := setup.DefaultScreen(X)
err = xproto.CreateWindowChecked(X, s.RootDepth, win, s.Root, 100, 100, 1, 1, 0, xproto.WindowClassInputOutput, s.RootVisual, 0, []uint32{}).Check()
if err != nil {
panic(err)
}
clipboardAtom = internAtom(X, "CLIPBOARD")
primaryAtom = internAtom(X, "PRIMARY")
textAtom = internAtom(X, "UTF8_STRING")
targetsAtom = internAtom(X, "TARGETS")
atomAtom = internAtom(X, "ATOM")
targetAtoms = []xproto.Atom{targetsAtom, textAtom}
go eventLoop()
}
func set(text string) error {
clipboardText = text
ssoc := xproto.SetSelectionOwnerChecked(X, win, clipboardAtom, xproto.TimeCurrentTime)
if err := ssoc.Check(); err != nil {
fmt.Fprintf(os.Stderr, "Error setting clipboard: %v", err)
}
ssoc = xproto.SetSelectionOwnerChecked(X, win, primaryAtom, xproto.TimeCurrentTime)
if err := ssoc.Check(); err != nil {
fmt.Fprintf(os.Stderr, "Error setting primary selection: %v", err)
}
return nil
}
func get() (string, error) {
return getSelection(clipboardAtom), nil
}
/*func getPrimary() string {
return getSelection(primaryAtom)
}*/
func getSelection(selAtom xproto.Atom) string {
csc := xproto.ConvertSelectionChecked(X, win, selAtom, textAtom, selAtom, xproto.TimeCurrentTime)
err := csc.Check()
if err != nil {
fmt.Fprintln(os.Stderr, err)
return ""
}
select {
case r := <-selnotify:
if !r {
return ""
}
gpc := xproto.GetProperty(X, true, win, selAtom, textAtom, 0, 5*1024*1024)
gpr, err := gpc.Reply()
if err != nil {
fmt.Fprintln(os.Stderr, err)
return ""
}
if gpr.BytesAfter != 0 {
fmt.Fprintln(os.Stderr, "Clipboard too large")
return ""
}
return string(gpr.Value[:gpr.ValueLen])
case <-time.After(1 * time.Second):
fmt.Fprintln(os.Stderr, "Clipboard retrieval failed, timeout")
return ""
}
}
func eventLoop() {
for {
e, err := X.WaitForEvent()
if err != nil {
continue
}
switch e := e.(type) {
case xproto.SelectionRequestEvent:
if debugClipboardRequests {
tgtname := lookupAtom(e.Target)
fmt.Fprintln(os.Stderr, "SelectionRequest", e, textAtom, tgtname, "isPrimary:", e.Selection == primaryAtom, "isClipboard:", e.Selection == clipboardAtom)
}
t := clipboardText
switch e.Target {
case textAtom:
if debugClipboardRequests {
fmt.Fprintln(os.Stderr, "Sending as text")
}
cpc := xproto.ChangePropertyChecked(X, xproto.PropModeReplace, e.Requestor, e.Property, textAtom, 8, uint32(len(t)), []byte(t))
err := cpc.Check()
if err == nil {
sendSelectionNotify(e)
} else {
fmt.Fprintln(os.Stderr, 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(X, xproto.PropModeReplace, e.Requestor, e.Property, atomAtom, 32, uint32(len(targetAtoms)), buf).Check()
if err == nil {
sendSelectionNotify(e)
} else {
fmt.Fprintln(os.Stderr, err)
}
default:
if debugClipboardRequests {
fmt.Fprintln(os.Stderr, "Skipping")
}
e.Property = 0
sendSelectionNotify(e)
}
case xproto.SelectionNotifyEvent:
selnotify <- (e.Property == clipboardAtom) || (e.Property == primaryAtom)
}
}
}
func lookupAtom(at xproto.Atom) string {
if s, ok := clipboardAtomCache[at]; ok {
return s
}
reply, err := xproto.GetAtomName(X, at).Reply()
if err != nil {
panic(err)
}
// If we're here, it means we didn't have the ATOM id cached. So cache it.
atomName := string(reply.Name)
clipboardAtomCache[at] = atomName
return atomName
}
func sendSelectionNotify(e xproto.SelectionRequestEvent) {
sn := xproto.SelectionNotifyEvent{
Time: xproto.TimeCurrentTime,
Requestor: e.Requestor,
Selection: e.Selection,
Target: e.Target,
Property: e.Property}
sec := xproto.SendEventChecked(X, false, e.Requestor, 0, string(sn.Bytes()))
err := sec.Check()
if err != nil {
fmt.Fprintln(os.Stderr, err)
}
}
func internAtom(conn *xgb.Conn, n string) xproto.Atom {
iac := xproto.InternAtom(conn, true, uint16(len(n)), n)
iar, err := iac.Reply()
if err != nil {
panic(err)
}
return iar.Atom
}