package clipboard import ( "errors" "runtime" "time" "unsafe" "golang.org/x/sys/windows" ) const ( cfUnicodeText = 13 gmemMoveable = 0x0002 ) var ( user32 = windows.NewLazySystemDLL("user32.dll") kernel32 = windows.NewLazySystemDLL("kernel32.dll") openClipboard = user32.NewProc("OpenClipboard") closeClipboard = user32.NewProc("CloseClipboard") emptyClipboard = user32.NewProc("EmptyClipboard") getClipboardData = user32.NewProc("GetClipboardData") setClipboardData = user32.NewProc("SetClipboardData") isClipboardFormatAvailable = user32.NewProc("IsClipboardFormatAvailable") registerClipboardFormat = user32.NewProc("RegisterClipboardFormatW") globalAlloc = kernel32.NewProc("GlobalAlloc") globalFree = kernel32.NewProc("GlobalFree") globalLock = kernel32.NewProc("GlobalLock") globalUnlock = kernel32.NewProc("GlobalUnlock") ) // Formats that password managers and other tools set to keep content out of // clipboard history and sync tools (documented by Microsoft for cloud clipboard). var concealedFormats = []string{ "ExcludeClipboardContentFromMonitorProcessing", "Clipboard Viewer Ignore", } type windowsClipboard struct { concealed []uintptr } func New() (Clipboard, error) { c := &windowsClipboard{} for _, name := range concealedFormats { ptr, err := windows.UTF16PtrFromString(name) if err != nil { return nil, err } if id, _, _ := registerClipboardFormat.Call(uintptr(unsafe.Pointer(ptr))); id != 0 { c.concealed = append(c.concealed, id) } } return c, nil } // lockedMemory turns the address returned by GlobalLock into a pointer. The memory is // owned by Windows and locked, not part of the Go heap, so the garbage collector cannot // move it; reading the uintptr through a pointer keeps "go vet" from flagging the // (here intended) uintptr conversion. func lockedMemory(addr uintptr) unsafe.Pointer { return *(*unsafe.Pointer)(unsafe.Pointer(&addr)) } // open retries briefly: another program may hold the clipboard for a moment. func open() error { for i := 0; i < 10; i++ { if r, _, _ := openClipboard.Call(0); r != 0 { return nil } time.Sleep(20 * time.Millisecond) } return errors.New("Zwischenablage ist belegt") } func available(format uintptr) bool { r, _, _ := isClipboardFormatAvailable.Call(format) return r != 0 } func (c *windowsClipboard) Read() (Content, error) { runtime.LockOSThread() defer runtime.UnlockOSThread() if err := open(); err != nil { return Content{}, err } defer closeClipboard.Call() for _, format := range c.concealed { if available(format) { return Content{Concealed: true}, nil } } if !available(cfUnicodeText) { return Content{}, nil } handle, _, _ := getClipboardData.Call(cfUnicodeText) if handle == 0 { return Content{}, nil } ptr, _, _ := globalLock.Call(handle) if ptr == 0 { return Content{}, errors.New("Zwischenablage nicht lesbar") } defer globalUnlock.Call(handle) return Content{Text: windows.UTF16PtrToString((*uint16)(lockedMemory(ptr)))}, nil } func (c *windowsClipboard) Write(text string) error { runtime.LockOSThread() defer runtime.UnlockOSThread() data, err := windows.UTF16FromString(text) if err != nil { return err } if err := open(); err != nil { return err } defer closeClipboard.Call() emptyClipboard.Call() size := uintptr(len(data) * 2) handle, _, _ := globalAlloc.Call(gmemMoveable, size) if handle == 0 { return errors.New("kein Speicher für die Zwischenablage") } ptr, _, _ := globalLock.Call(handle) if ptr == 0 { globalFree.Call(handle) return errors.New("kein Speicher für die Zwischenablage") } copy(unsafe.Slice((*uint16)(lockedMemory(ptr)), len(data)), data) globalUnlock.Call(handle) // On success the system owns the memory; only free it if the hand-over failed if r, _, _ := setClipboardData.Call(cfUnicodeText, handle); r == 0 { globalFree.Call(handle) return errors.New("Zwischenablage konnte nicht gesetzt werden") } return nil }