Debian Codegraph @

clipboard_windows.go

raw symbols SCIP
1 // Copyright 2013 @atotto. All rights reserved.
2 // Use of this source code is governed by a BSD-style
3 // license that can be found in the LICENSE file.
4
5 //go:build windows
6 // +build windows
7
8 package clipboard
9
10 import (
11 "context"
12 "runtime"
13 "syscall"
14 "time"
15 "unsafe"
16
17 "golang.org/x/sys/windows"
18 )
19
20 const (
21 cfUnicodetext = 13
22 gmemMoveable = 0x0002
23 )
24
25 var (
26 user32 = windows.NewLazySystemDLL("user32.dll")
27 isClipboardFormatAvailable = user32.NewProc("IsClipboardFormatAvailable")
28 openClipboard = user32.NewProc("OpenClipboard")
29 closeClipboard = user32.NewProc("CloseClipboard")
30 emptyClipboard = user32.NewProc("EmptyClipboard")
31 getClipboardData = user32.NewProc("GetClipboardData")
32 setClipboardData = user32.NewProc("SetClipboardData")
33
34 kernel32 = windows.NewLazySystemDLL("kernel32")
35 globalAlloc = kernel32.NewProc("GlobalAlloc")
36 globalFree = kernel32.NewProc("GlobalFree")
37 globalLock = kernel32.NewProc("GlobalLock")
38 globalUnlock = kernel32.NewProc("GlobalUnlock")
39 lstrcpy = kernel32.NewProc("lstrcpyW")
40 )
41
42 // waitOpenClipboard opens the clipboard, waiting for up to a second to do so.
43 func waitOpenClipboard() error {
44 started := time.Now()
45 limit := started.Add(time.Second)
46 var r uintptr
47 var err error
48 for time.Now().Before(limit) {
49 r, _, err = openClipboard.Call(0)
50 if r != 0 {
51 return nil
52 }
53 time.Sleep(time.Millisecond)
54 }
55 return err
56 }
57
58 func readAll(_ context.Context) ([]byte, error) {
59 // LockOSThread ensure that the whole method will keep executing on the same thread from begin to end (it actually locks the goroutine thread attribution).
60 // Otherwise if the goroutine switch thread during execution (which is a common practice), the OpenClipboard and CloseClipboard will happen on two different threads, and it will result in a clipboard deadlock.
61 runtime.LockOSThread()
62 defer runtime.UnlockOSThread()
63 if formatAvailable, _, err := isClipboardFormatAvailable.Call(cfUnicodetext); formatAvailable == 0 {
64 return nil, err
65 }
66 err := waitOpenClipboard()
67 if err != nil {
68 return nil, err
69 }
70
71 h, _, err := getClipboardData.Call(cfUnicodetext)
72 if h == 0 {
73 _, _, _ = closeClipboard.Call()
74 return nil, err
75 }
76
77 l, _, err := globalLock.Call(h)
78 if l == 0 {
79 _, _, _ = closeClipboard.Call()
80 return nil, err
81 }
82
83 text := syscall.UTF16ToString((*[1 << 20]uint16)(unsafe.Pointer(l))[:]) //nolint:unsafeptr
84
85 r, _, err := globalUnlock.Call(h)
86 if r == 0 {
87 _, _, _ = closeClipboard.Call()
88 return nil, err
89 }
90
91 closed, _, err := closeClipboard.Call()
92 if closed == 0 {
93 return nil, err
94 }
95 return []byte(text), nil
96 }
97
98 func writeAll(_ context.Context, text []byte, _ bool) error {
99 // LockOSThread ensure that the whole method will keep executing on the same thread from begin to end (it actually locks the goroutine thread attribution).
100 // Otherwise if the goroutine switch thread during execution (which is a common practice), the OpenClipboard and CloseClipboard will happen on two different threads, and it will result in a clipboard deadlock.
101 runtime.LockOSThread()
102 defer runtime.UnlockOSThread()
103
104 err := waitOpenClipboard()
105 if err != nil {
106 return err
107 }
108
109 r, _, err := emptyClipboard.Call(0)
110 if r == 0 {
111 _, _, _ = closeClipboard.Call()
112 return err
113 }
114
115 data, err := syscall.UTF16FromString(string(text))
116 if err != nil {
117 _, _, _ = closeClipboard.Call()
118 return err
119 }
120
121 // "If the hMem parameter identifies a memory object, the object must have
122 // been allocated using the function with the GMEM_MOVEABLE flag."
123 h, _, err := globalAlloc.Call(gmemMoveable, uintptr(len(data)*int(unsafe.Sizeof(data[0]))))
124 if h == 0 {
125 _, _, _ = closeClipboard.Call()
126 return err
127 }
128 defer func() {
129 if h != 0 {
130 globalFree.Call(h)
131 }
132 }()
133
134 l, _, err := globalLock.Call(h)
135 if l == 0 {
136 _, _, _ = closeClipboard.Call()
137 return err
138 }
139
140 r, _, err = lstrcpy.Call(l, uintptr(unsafe.Pointer(&data[0])))
141 if r == 0 {
142 _, _, _ = closeClipboard.Call()
143 return err
144 }
145
146 r, _, err = globalUnlock.Call(h)
147 if r == 0 {
148 if err.(syscall.Errno) != 0 {
149 _, _, _ = closeClipboard.Call()
150 return err
151 }
152 }
153
154 r, _, err = setClipboardData.Call(cfUnicodetext, h)
155 if r == 0 {
156 _, _, _ = closeClipboard.Call()
157 return err
158 }
159 h = 0 // suppress deferred cleanup
160 closed, _, err := closeClipboard.Call()
161 if closed == 0 {
162 return err
163 }
164 return nil
165 }
166
167 func unsupported() bool {
168 return false
169 }