Skip to content

Commit

Permalink
Merge pull request atotto#26 from ewxrjk/winapi
Browse files Browse the repository at this point in the history
Windows clipboard fixes
  • Loading branch information
atotto authored Mar 22, 2018
2 parents bc5958e + 47157e2 commit 210bc23
Showing 1 changed file with 37 additions and 10 deletions.
47 changes: 37 additions & 10 deletions clipboard_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,13 @@ package clipboard

import (
"syscall"
"time"
"unsafe"
)

const (
cfUnicodetext = 13
gmemFixed = 0x0000
gmemMoveable = 0x0002
)

var (
Expand All @@ -32,15 +33,31 @@ var (
lstrcpy = kernel32.NewProc("lstrcpyW")
)

// waitOpenClipboard opens the clipboard, waiting for up to a second to do so.
func waitOpenClipboard() error {
started := time.Now()
limit := started.Add(time.Second)
var r uintptr
var err error
for time.Now().Before(limit) {
r, _, err = openClipboard.Call(0)
if r != 0 {
return nil
}
time.Sleep(time.Millisecond)
}
return err
}

func readAll() (string, error) {
r, _, err := openClipboard.Call(0)
if r == 0 {
err := waitOpenClipboard()
if err != nil {
return "", err
}
defer closeClipboard.Call()

h, _, err := getClipboardData.Call(cfUnicodetext)
if r == 0 {
if h == 0 {
return "", err
}

Expand All @@ -51,7 +68,7 @@ func readAll() (string, error) {

text := syscall.UTF16ToString((*[1 << 20]uint16)(unsafe.Pointer(l))[:])

r, _, err = globalUnlock.Call(h)
r, _, err := globalUnlock.Call(h)
if r == 0 {
return "", err
}
Expand All @@ -60,23 +77,30 @@ func readAll() (string, error) {
}

func writeAll(text string) error {
r, _, err := openClipboard.Call(0)
if r == 0 {
err := waitOpenClipboard()
if err != nil {
return err
}
defer closeClipboard.Call()

r, _, err = emptyClipboard.Call(0)
r, _, err := emptyClipboard.Call(0)
if r == 0 {
return err
}

data := syscall.StringToUTF16(text)

h, _, err := globalAlloc.Call(gmemFixed, uintptr(len(data)*int(unsafe.Sizeof(data[0]))))
// "If the hMem parameter identifies a memory object, the object must have
// been allocated using the function with the GMEM_MOVEABLE flag."
h, _, err := globalAlloc.Call(gmemMoveable, uintptr(len(data)*int(unsafe.Sizeof(data[0]))))
if h == 0 {
return err
}
defer func() {
if h != 0 {
globalFree.Call(h)
}
}()

l, _, err := globalLock.Call(h)
if l == 0 {
Expand All @@ -90,12 +114,15 @@ func writeAll(text string) error {

r, _, err = globalUnlock.Call(h)
if r == 0 {
return err
if err.(syscall.Errno) != 0 {
return err
}
}

r, _, err = setClipboardData.Call(cfUnicodetext, h)
if r == 0 {
return err
}
h = 0 // suppress deferred cleanup
return nil
}

0 comments on commit 210bc23

Please sign in to comment.