forked from golang-design/clipboard
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcacooclip_darwin.m
48 lines (40 loc) · 1.44 KB
/
cacooclip_darwin.m
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
// Copyright 2021 The golang.design Initiative Authors, 2023 Gabriele V.
// All rights reserved. Use of this source code is governed
// by a MIT license that can be found in the LICENSE file.
//
// Original code written by Changkun Ou <changkun.de>
// Modified to support chromium web custom MIME type by Gabriel V. <[email protected]>
//go:build darwin && !ios
// Interact with NSPasteboard using Objective-C
// https://developer.apple.com/documentation/appkit/nspasteboard?language=objc
#import <Foundation/Foundation.h>
#import <Cocoa/Cocoa.h>
NSString *const chromiumWebCustomData = @"org.chromium.web-custom-data";
int clipboard_read_cacoo(void **out) {
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
NSArray *dataTypes = [pasteboard types];
if (![dataTypes containsObject:chromiumWebCustomData]) {
return -1;
}
NSData *data = [pasteboard dataForType:chromiumWebCustomData];
if (data == nil) {
return 0;
}
NSUInteger siz = [data length];
*out = malloc(siz);
[data getBytes: *out length: siz];
return siz;
}
int clipboard_write_cacoo(const void *bytes, NSInteger n) {
NSPasteboard *pasteboard = [NSPasteboard generalPasteboard];
NSData *data = [NSData dataWithBytes: bytes length: n];
[pasteboard clearContents];
BOOL ok = [pasteboard setData: data forType:chromiumWebCustomData];
if (!ok) {
return -1;
}
return 0;
}
NSInteger clipboard_change_count() {
return [[NSPasteboard generalPasteboard] changeCount];
}