Debian Codegraph @

winapi.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-EXTRA file.
4
5 //go:build windows
6
7 package clipper
8
9 import (
10 "runtime"
11 "syscall"
12 "time"
13 "unsafe"
14 )
15
16 type WinApi struct{}
17
18 const (
19 cfUnicodetext = 13
20 gmemMoveable = 0x0002
21 )
22
23 var (
24 user32 = syscall.MustLoadDLL("user32")
25 isClipboardFormatAvailable = user32.MustFindProc("IsClipboardFormatAvailable")
26 openClipboard = user32.MustFindProc("OpenClipboard")
27 closeClipboard = user32.MustFindProc("CloseClipboard")
28 emptyClipboard = user32.MustFindProc("EmptyClipboard")
29 getClipboardData = user32.MustFindProc("GetClipboardData")
30 setClipboardData = user32.MustFindProc("SetClipboardData")
31
32 kernel32 = syscall.NewLazyDLL("kernel32")
33 globalAlloc = kernel32.NewProc("GlobalAlloc")
34 globalFree = kernel32.NewProc("GlobalFree")
35 globalLock = kernel32.NewProc("GlobalLock")
36 globalUnlock = kernel32.NewProc("GlobalUnlock")
37 lstrcpy = kernel32.NewProc("lstrcpyW")
38 )
39
40 // waitOpenClipboard opens the clipboard, waiting for up to a second to do so.
41 func waitOpenClipboard() error {
42 started := time.Now()
43 limit := started.Add(time.Second)
44 var r uintptr
45 var err error
46 for time.Now().Before(limit) {
47 r, _, err = openClipboard.Call(0)
48 if r != 0 {
49 return nil
50 }
51 time.Sleep(time.Millisecond)
52 }
53 return err
54 }
55
56 func (w *WinApi) Init() error {
57 return nil
58 }
59
60 func (w *WinApi) ReadAll(reg string) ([]byte, error) {
61 // LockOSThread ensure that the whole method will keep executing on the same thread from begin to end (it actually locks the goroutine thread attribution).
62 // 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.
63 runtime.LockOSThread()
64 defer runtime.UnlockOSThread()
65 if formatAvailable, _, err := isClipboardFormatAvailable.Call(cfUnicodetext); formatAvailable == 0 {
66 return nil, err
67 }
68 if reg != RegClipboard {
69 return nil, &ErrInvalidReg{
70 Reg: reg,
71 }
72 }
73 err := waitOpenClipboard()
74 if err != nil {
75 return nil, err
76 }
77
78 h, _, err := getClipboardData.Call(cfUnicodetext)
79 if h == 0 {
80 _, _, _ = closeClipboard.Call()
81 return nil, err
82 }
83
84 l, _, err := globalLock.Call(h)
85 if l == 0 {
86 _, _, _ = closeClipboard.Call()
87 return nil, err
88 }
89
90 text := syscall.UTF16ToString((*[1 << 20]uint16)(unsafe.Pointer(l))[:])
91
92 r, _, err := globalUnlock.Call(h)
93 if r == 0 {
94 _, _, _ = closeClipboard.Call()
95 return nil, err
96 }
97
98 closed, _, err := closeClipboard.Call()
99 if closed == 0 {
100 return nil, err
101 }
102 return []byte(text), nil
103 }
104
105 func (w *WinApi) WriteAll(reg string, text []byte) error {
106 // LockOSThread ensure that the whole method will keep executing on the same thread from begin to end (it actually locks the goroutine thread attribution).
107 // 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.
108 runtime.LockOSThread()
109 defer runtime.UnlockOSThread()
110
111 if reg != RegClipboard {
112 return &ErrInvalidReg{
113 Reg: reg,
114 }
115 }
116
117 err := waitOpenClipboard()
118 if err != nil {
119 return err
120 }
121
122 r, _, err := emptyClipboard.Call(0)
123 if r == 0 {
124 _, _, _ = closeClipboard.Call()
125 return err
126 }
127
128 data := syscall.StringToUTF16(string(text))
129
130 // "If the hMem parameter identifies a memory object, the object must have
131 // been allocated using the function with the GMEM_MOVEABLE flag."
132 h, _, err := globalAlloc.Call(gmemMoveable, uintptr(len(data)*int(unsafe.Sizeof(data[0]))))
133 if h == 0 {
134 _, _, _ = closeClipboard.Call()
135 return err
136 }
137 defer func() {
138 if h != 0 {
139 globalFree.Call(h)
140 }
141 }()
142
143 l, _, err := globalLock.Call(h)
144 if l == 0 {
145 _, _, _ = closeClipboard.Call()
146 return err
147 }
148
149 r, _, err = lstrcpy.Call(l, uintptr(unsafe.Pointer(&data[0])))
150 if r == 0 {
151 _, _, _ = closeClipboard.Call()
152 return err
153 }
154
155 r, _, err = globalUnlock.Call(h)
156 if r == 0 {
157 if err.(syscall.Errno) != 0 {
158 _, _, _ = closeClipboard.Call()
159 return err
160 }
161 }
162
163 r, _, err = setClipboardData.Call(cfUnicodetext, h)
164 if r == 0 {
165 _, _, _ = closeClipboard.Call()
166 return err
167 }
168 h = 0 // suppress deferred cleanup
169 closed, _, err := closeClipboard.Call()
170 if closed == 0 {
171 return err
172 }
173 return nil
174 }