1+ // Copyright 2021 The golang.design Initiative authors.
2+ // All rights reserved. Use of this source code is governed
3+ // by a GNU GPL-3 license that can be found in the LICENSE file.
4+ //
5+ // Written by Changkun Ou <changkun.de>
6+
7+ // Interact with NSPasteboard using Objective-C
8+ // https://developer.apple.com/documentation/appkit/nspasteboard?language=objc
9+
10+ #import < Foundation/Foundation.h>
11+ #import < Cocoa/Cocoa.h>
12+
13+ unsigned int clipboard_read_string (void **out) {
14+ NSPasteboard * pasteboard = [NSPasteboard generalPasteboard ];
15+ NSData *data = [pasteboard dataForType: NSPasteboardTypeString ];
16+ if (data == nil ) {
17+ return 0 ;
18+ }
19+ NSUInteger siz = [data length ];
20+ *out = malloc (siz);
21+ [data getBytes: *out length: siz];
22+ return siz;
23+ }
24+
25+ unsigned int clipboard_read_image (void **out) {
26+ NSPasteboard * pasteboard = [NSPasteboard generalPasteboard ];
27+ NSData *data = [pasteboard dataForType: NSPasteboardTypePNG ];
28+ if (data == nil ) {
29+ return 0 ;
30+ }
31+ NSUInteger siz = [data length ];
32+ *out = malloc (siz);
33+ [data getBytes: *out length: siz];
34+ return siz;
35+ }
36+
37+ int clipboard_write_string (const void *bytes, NSInteger n) {
38+ NSPasteboard *pasteboard = [NSPasteboard generalPasteboard ];
39+ NSData *data = [NSData dataWithBytes: bytes length: n];
40+ [pasteboard clearContents ];
41+ BOOL ok = [pasteboard setData: data forType: NSPasteboardTypeString ];
42+ if (!ok) {
43+ return -1 ;
44+ }
45+ return 0 ;
46+ }
47+ int clipboard_write_image (const void *bytes, NSInteger n) {
48+ NSPasteboard *pasteboard = [NSPasteboard generalPasteboard ];
49+ NSData *data = [NSData dataWithBytes: bytes length: n];
50+ [pasteboard clearContents ];
51+ BOOL ok = [pasteboard setData: data forType: NSPasteboardTypePNG ];
52+ if (!ok) {
53+ return -1 ;
54+ }
55+ return 0 ;
56+ }
0 commit comments