-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathclipboard_darwin.go
More file actions
236 lines (197 loc) · 5.55 KB
/
Copy pathclipboard_darwin.go
File metadata and controls
236 lines (197 loc) · 5.55 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
// Copyright 2025 Ayman Bagabas
// SPDX-License-Identifier: MIT
//go:build darwin && !ios
package nativeclipboard
import (
"context"
"runtime"
"time"
"unsafe"
"github.com/ebitengine/purego"
"github.com/ebitengine/purego/objc"
)
var (
// Classes
nsPasteboardClass objc.Class
nsDataClass objc.Class
// Selectors
sel_generalPasteboard objc.SEL
sel_dataForType objc.SEL
sel_clearContents objc.SEL
sel_setData_forType objc.SEL
sel_changeCount objc.SEL
sel_dataWithBytes_length objc.SEL
sel_bytes objc.SEL
sel_length objc.SEL
// Pasteboard types (NSString constants)
NSPasteboardTypeString objc.ID
NSPasteboardTypePNG objc.ID
)
func initialize() error {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
// Load AppKit framework for NSPasteboard
appkit, err := purego.Dlopen("/System/Library/Frameworks/AppKit.framework/AppKit", purego.RTLD_NOW|purego.RTLD_GLOBAL)
if err != nil {
return err
}
// Get classes
nsPasteboardClass = objc.GetClass("NSPasteboard")
nsDataClass = objc.GetClass("NSData")
// Register selectors
sel_generalPasteboard = objc.RegisterName("generalPasteboard")
sel_dataForType = objc.RegisterName("dataForType:")
sel_clearContents = objc.RegisterName("clearContents")
sel_setData_forType = objc.RegisterName("setData:forType:")
sel_changeCount = objc.RegisterName("changeCount")
sel_dataWithBytes_length = objc.RegisterName("dataWithBytes:length:")
sel_bytes = objc.RegisterName("bytes")
sel_length = objc.RegisterName("length")
// Get pasteboard type constants from AppKit
// NSPasteboardTypeString and NSPasteboardTypePNG are NSString constants
typeStringPtr, err := purego.Dlsym(appkit, "NSPasteboardTypeString")
if err != nil {
return err
}
typePNGPtr, err := purego.Dlsym(appkit, "NSPasteboardTypePNG")
if err != nil {
return err
}
// Dereference the pointers to get the actual NSString objects
NSPasteboardTypeString = objc.ID(*(*uintptr)(unsafe.Pointer(typeStringPtr)))
NSPasteboardTypePNG = objc.ID(*(*uintptr)(unsafe.Pointer(typePNGPtr)))
return nil
}
func read(t Format) ([]byte, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
// Get general pasteboard: [NSPasteboard generalPasteboard]
pasteboard := objc.ID(nsPasteboardClass).Send(sel_generalPasteboard)
if pasteboard == 0 {
return nil, ErrUnavailable
}
// Determine the pasteboard type
var pasteboardType objc.ID
switch t {
case Text:
pasteboardType = NSPasteboardTypeString
case Image:
pasteboardType = NSPasteboardTypePNG
default:
return nil, ErrUnsupported
}
// Get data: [pasteboard dataForType:type]
data := pasteboard.Send(sel_dataForType, pasteboardType)
if data == 0 {
return nil, ErrUnavailable
}
// Get length: [data length]
length := objc.Send[uint64](data, sel_length)
if length == 0 {
return nil, nil
}
// Get bytes: [data bytes]
bytes := data.Send(sel_bytes)
if bytes == 0 {
return nil, ErrUnavailable
}
// Copy data to Go slice
result := make([]byte, length)
copyBytes(result, uintptr(bytes), int(length))
return result, nil
}
func write(t Format, buf []byte) (<-chan struct{}, error) {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
// Get general pasteboard
pasteboard := objc.ID(nsPasteboardClass).Send(sel_generalPasteboard)
if pasteboard == 0 {
return nil, ErrUnavailable
}
// Clear contents: [pasteboard clearContents]
pasteboard.Send(sel_clearContents)
if len(buf) == 0 {
return nil, nil
}
// Create NSData from bytes: [NSData dataWithBytes:buf length:len(buf)]
var data objc.ID
if len(buf) > 0 {
data = objc.ID(nsDataClass).Send(sel_dataWithBytes_length, unsafe.Pointer(&buf[0]), uint64(len(buf)))
} else {
data = objc.ID(nsDataClass).Send(sel_dataWithBytes_length, unsafe.Pointer(nil), uint64(0))
}
if data == 0 {
return nil, ErrUnavailable
}
// Determine the pasteboard type
var pasteboardType objc.ID
switch t {
case Text:
pasteboardType = NSPasteboardTypeString
case Image:
pasteboardType = NSPasteboardTypePNG
default:
return nil, ErrUnsupported
}
// Set data: [pasteboard setData:data forType:type]
ok := objc.Send[bool](pasteboard, sel_setData_forType, data, pasteboardType)
if !ok {
return nil, ErrUnavailable
}
// Monitor for changes
changed := make(chan struct{}, 1)
initialCount := objc.Send[int64](pasteboard, sel_changeCount)
go func() {
for {
time.Sleep(time.Second)
pb := objc.ID(nsPasteboardClass).Send(sel_generalPasteboard)
if pb == 0 {
continue
}
count := objc.Send[int64](pb, sel_changeCount)
if count != initialCount {
changed <- struct{}{}
close(changed)
return
}
}
}()
return changed, nil
}
func watch(ctx context.Context, t Format) <-chan []byte {
recv := make(chan []byte, 1)
ticker := time.NewTicker(time.Second)
go func() {
runtime.LockOSThread()
defer runtime.UnlockOSThread()
pasteboard := objc.ID(nsPasteboardClass).Send(sel_generalPasteboard)
lastCount := objc.Send[int64](pasteboard, sel_changeCount)
for {
select {
case <-ctx.Done():
ticker.Stop()
close(recv)
return
case <-ticker.C:
pb := objc.ID(nsPasteboardClass).Send(sel_generalPasteboard)
currentCount := objc.Send[int64](pb, sel_changeCount)
if currentCount != lastCount {
b, _ := read(t)
if b != nil {
recv <- b
}
lastCount = currentCount
}
}
}
}()
return recv
}
func copyBytes(dst []byte, src uintptr, length int) {
if length == 0 {
return
}
// Use unsafe to copy from C memory to Go slice
srcSlice := unsafe.Slice((*byte)(unsafe.Pointer(src)), length)
copy(dst, srcSlice)
}