Skip to content

Commit 4cdf6cd

Browse files
committed
clipboard: close clipboard when not needed
This CL closes the clipboard when the clipboard is not needed. This fixes a problem where other thrid party application cannot obtain the clipboard. Another fix is that writing an image in a LCS_sRGB format, because it allows Chrome web clipboard standard to paste an image data. Fixes #3
1 parent 0e3d9ae commit 4cdf6cd

File tree

1 file changed

+21
-4
lines changed

1 file changed

+21
-4
lines changed

clipboard_windows.go

Lines changed: 21 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -132,6 +132,11 @@ func readImage() ([]byte, error) {
132132
for y := 0; y < int(info.Height); y++ {
133133
for x := 0; x < int(info.Width); x++ {
134134
idx := offset + 4*(y*stride+x)
135+
136+
// FIXME: It seems that reading from clipboard data causes 3 pixels
137+
// offset. I don't have a clear evidence on the root reason yet.
138+
// xhat := (x + int(info.Width-3)) % int(info.Width)
139+
135140
xhat := (x + int(info.Width)) % int(info.Width)
136141
yhat := int(info.Height) - y
137142
r := data[idx+2]
@@ -172,6 +177,13 @@ func writeImage(buf []byte) error {
172177
for y := 0; y < height; y++ {
173178
for x := 0; x < width; x++ {
174179
idx := int(offset) + 4*(y*width+x)
180+
181+
// FIXME: It seems that reading from clipboard data causes 3 pixels
182+
// offset. I don't have a clear evidence on the root reason yet.
183+
// xhat := (x + int(width) - 3) % int(width)
184+
// yhat := int(height) - y
185+
// r, g, b, a := img.At(xhat, yhat).RGBA()
186+
175187
r, g, b, a := img.At(x, height-y).RGBA()
176188
data[idx+2] = uint8(r)
177189
data[idx+1] = uint8(g)
@@ -198,7 +210,7 @@ func writeImage(buf []byte) error {
198210
// - LCS_sRGB = 0x73524742
199211
// - LCS_WINDOWS_COLOR_SPACE = 0x57696E20
200212
// https://docs.microsoft.com/en-us/openspecs/windows_protocols/ms-wmf/eb4bbd50-b3ce-4917-895c-be31f214797f
201-
info.CSType = 0 // 1934772034
213+
info.CSType = 0x73524742
202214
// Use GL_IMAGES for GamutMappingIntent
203215
// Other options:
204216
// - LCS_GM_ABS_COLORIMETRIC = 0x00000008
@@ -286,8 +298,8 @@ func write(t Format, buf []byte) (<-chan struct{}, error) {
286298
errch := make(chan error)
287299
changed := make(chan struct{}, 1)
288300
go func() {
289-
// make sure GetClipboardSequenceNumber happens with OpenClipboard
290-
// on the same thread.
301+
// make sure GetClipboardSequenceNumber happens with
302+
// OpenClipboard on the same thread.
291303
runtime.LockOSThread()
292304
defer runtime.UnlockOSThread()
293305
for {
@@ -297,14 +309,14 @@ func write(t Format, buf []byte) (<-chan struct{}, error) {
297309
}
298310
break
299311
}
300-
defer closeClipboard.Call()
301312

302313
// var param uintptr
303314
switch t {
304315
case FmtImage:
305316
err := writeImage(buf)
306317
if err != nil {
307318
errch <- err
319+
closeClipboard.Call()
308320
return
309321
}
310322
case FmtText:
@@ -314,9 +326,14 @@ func write(t Format, buf []byte) (<-chan struct{}, error) {
314326
err := writeText(buf)
315327
if err != nil {
316328
errch <- err
329+
closeClipboard.Call()
317330
return
318331
}
319332
}
333+
// Close the clipboard otherwise other applications cannot
334+
// paste the data.
335+
closeClipboard.Call()
336+
320337
cnt, _, _ := getClipboardSequenceNumber.Call()
321338
errch <- nil
322339
for {

0 commit comments

Comments
 (0)