/
copy.go
209 lines (183 loc) · 4.87 KB
/
copy.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
package copypaste
import (
"bytes"
"encoding/binary"
"fmt"
"github.com/BurntSushi/xgb"
"github.com/BurntSushi/xgb/xproto"
"github.com/jmigpin/editor/driver/xgbutil"
"github.com/jmigpin/editor/util/uiutil/event"
"github.com/pkg/errors"
)
type Copy struct {
conn *xgb.Conn
win xproto.Window
reply chan *xproto.SelectionNotifyEvent
// Data to transfer
clipboardStr string
primaryStr string
}
func NewCopy(conn *xgb.Conn, win xproto.Window) (*Copy, error) {
c := &Copy{conn: conn, win: win}
if err := xgbutil.LoadAtoms(conn, &CopyAtoms, false); err != nil {
return nil, err
}
return c, nil
}
//----------
func (c *Copy) Set(i event.CopyPasteIndex, str string) error {
switch i {
case event.CPIPrimary:
c.primaryStr = str
return c.set(xproto.AtomPrimary)
case event.CPIClipboard:
c.clipboardStr = str
return c.set(CopyAtoms.Clipboard)
}
panic("unhandled index")
}
func (c *Copy) set(selection xproto.Atom) error {
cookie := xproto.SetSelectionOwnerChecked(c.conn, c.win, selection, 0)
return cookie.Check()
}
//----------
// Another application is asking for the data
func (c *Copy) OnSelectionRequest(ev *xproto.SelectionRequestEvent, events chan<- interface{}) {
//// DEBUG
//target, _ := xgbutil.GetAtomName(c.conn, ev.Target)
//sel, _ := xgbutil.GetAtomName(c.conn, ev.Selection)
//prop, _ := xgbutil.GetAtomName(c.conn, ev.Property)
//log.Printf("on selection request: %v %v %v", target, sel, prop)
switch ev.Target {
case CopyAtoms.Utf8String,
CopyAtoms.Text,
CopyAtoms.TextPlain,
CopyAtoms.TextPlainCharsetUtf8,
CopyAtoms.GtkTextBufferContents:
if err := c.transferBytes(ev); err != nil {
events <- err
}
case CopyAtoms.Targets:
if err := c.transferTargets(ev); err != nil {
events <- err
}
default:
c.debugRequest(ev, events)
// try to transfer bytes anyway
if err := c.transferBytes(ev); err != nil {
events <- err
}
}
}
func (c *Copy) debugRequest(ev *xproto.SelectionRequestEvent, events chan<- interface{}) {
// atom name
name, err := xgbutil.GetAtomName(c.conn, ev.Target)
if err != nil {
events <- errors.Wrap(err, "cpcopy selectionrequest atom name for target")
}
// debug
msg := fmt.Sprintf("cpcopy: non-standard external request for type %v %q\n", ev.Target, name)
events <- errors.New(msg)
}
//----------
func (c *Copy) transferBytes(ev *xproto.SelectionRequestEvent) error {
var b []byte
switch ev.Selection {
case xproto.AtomPrimary:
b = []byte(c.primaryStr)
case CopyAtoms.Clipboard:
b = []byte(c.clipboardStr)
default:
return fmt.Errorf("unhandled selection: %v", ev.Selection)
}
// change property on the requestor
c1 := xproto.ChangePropertyChecked(
c.conn,
xproto.PropModeReplace,
ev.Requestor, // requestor window
ev.Property, // property
ev.Target,
8, // format
uint32(len(b)),
b)
if err := c1.Check(); err != nil {
return err
}
// notify the server
sne := xproto.SelectionNotifyEvent{
Requestor: ev.Requestor,
Selection: ev.Selection,
Target: ev.Target,
Property: ev.Property,
Time: ev.Time,
}
c2 := xproto.SendEventChecked(
c.conn,
false,
sne.Requestor,
xproto.EventMaskNoEvent,
string(sne.Bytes()))
return c2.Check()
}
//----------
func (c *Copy) transferTargets(ev *xproto.SelectionRequestEvent) error {
targets := []xproto.Atom{
CopyAtoms.Utf8String,
CopyAtoms.Text,
CopyAtoms.TextPlain,
CopyAtoms.TextPlainCharsetUtf8,
}
tbuf := new(bytes.Buffer)
for _, t := range targets {
binary.Write(tbuf, binary.LittleEndian, t)
}
// change property on the requestor
c1 := xproto.ChangePropertyChecked(
c.conn,
xproto.PropModeReplace,
ev.Requestor, // requestor window
ev.Property, // property
xproto.AtomAtom, // (would not work in some cases with ev.Target)
32, // format
uint32(len(targets)),
tbuf.Bytes())
if err := c1.Check(); err != nil {
return err
}
// notify the server
sne := xproto.SelectionNotifyEvent{
Requestor: ev.Requestor,
Selection: ev.Selection,
Target: ev.Target,
Property: ev.Property,
Time: ev.Time,
}
c2 := xproto.SendEventChecked(
c.conn,
false,
sne.Requestor,
xproto.EventMaskNoEvent,
string(sne.Bytes()))
return c2.Check()
}
//----------
// Another application now owns the selection.
func (c *Copy) OnSelectionClear(ev *xproto.SelectionClearEvent) {
switch ev.Selection {
case xproto.AtomPrimary:
c.primaryStr = ""
case CopyAtoms.Clipboard:
c.clipboardStr = ""
}
}
//----------
var CopyAtoms struct {
XSelData xproto.Atom `loadAtoms:"XSEL_DATA"`
Clipboard xproto.Atom `loadAtoms:"CLIPBOARD"`
Targets xproto.Atom `loadAtoms:"TARGETS"`
Utf8String xproto.Atom `loadAtoms:"UTF8_STRING"`
Text xproto.Atom `loadAtoms:"TEXT"`
TextPlain xproto.Atom `loadAtoms:"text/plain"`
TextPlainCharsetUtf8 xproto.Atom `loadAtoms:"text/plain;charset=utf-8"`
GtkTextBufferContents xproto.Atom `loadAtoms:"GTK_TEXT_BUFFER_CONTENTS"`
}