2019-11-17 05:40:03 +03:00
|
|
|
module clipboard
|
|
|
|
|
|
|
|
#include <libkern/OSAtomic.h>
|
|
|
|
#include <Cocoa/Cocoa.h>
|
|
|
|
#flag -framework Cocoa
|
2020-01-09 14:00:39 +03:00
|
|
|
pub struct Clipboard {
|
2020-11-03 01:00:29 +03:00
|
|
|
pb voidptr
|
|
|
|
last_cb_serial i64
|
2019-12-07 15:22:28 +03:00
|
|
|
mut:
|
2020-11-03 01:00:29 +03:00
|
|
|
foo int // TODO remove, for mut hack
|
2019-11-17 05:40:03 +03:00
|
|
|
}
|
|
|
|
|
2020-11-03 01:00:29 +03:00
|
|
|
fn new_clipboard() &Clipboard {
|
2019-12-07 15:22:28 +03:00
|
|
|
pb := voidptr(0)
|
2019-11-17 05:40:03 +03:00
|
|
|
#pb = [NSPasteboard generalPasteboard];
|
|
|
|
cb := &Clipboard{
|
|
|
|
pb: pb
|
|
|
|
}
|
|
|
|
return cb
|
|
|
|
}
|
|
|
|
|
|
|
|
fn (cb &Clipboard) check_availability() bool {
|
|
|
|
return cb.pb != C.NULL
|
|
|
|
}
|
|
|
|
|
2020-11-03 01:00:29 +03:00
|
|
|
fn (mut cb Clipboard) clear() {
|
2019-12-07 15:22:28 +03:00
|
|
|
cb.foo = 0
|
2019-11-17 05:40:03 +03:00
|
|
|
#[cb->pb clearContents];
|
|
|
|
}
|
|
|
|
|
2020-11-03 01:00:29 +03:00
|
|
|
fn (mut cb Clipboard) free() {
|
2019-12-07 15:22:28 +03:00
|
|
|
cb.foo = 0
|
2020-11-03 01:00:29 +03:00
|
|
|
// nothing to free
|
2019-11-17 05:40:03 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
fn (cb &Clipboard) has_ownership() bool {
|
2020-11-03 01:00:29 +03:00
|
|
|
if cb.last_cb_serial == 0 {
|
|
|
|
return false
|
|
|
|
}
|
2019-11-17 05:40:03 +03:00
|
|
|
#return [cb->pb changeCount] == cb->last_cb_serial;
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-11-24 06:27:02 +03:00
|
|
|
fn C.OSAtomicCompareAndSwapLong()
|
|
|
|
|
2020-05-17 14:51:18 +03:00
|
|
|
fn (mut cb Clipboard) set_text(text string) bool {
|
2019-12-07 15:22:28 +03:00
|
|
|
cb.foo = 0
|
2019-11-17 05:40:03 +03:00
|
|
|
#NSString *ns_clip;
|
2019-12-07 15:22:28 +03:00
|
|
|
ret := false
|
2019-11-17 05:40:03 +03:00
|
|
|
#ns_clip = [[ NSString alloc ] initWithBytesNoCopy:text.str length:text.len encoding:NSUTF8StringEncoding freeWhenDone: false];
|
|
|
|
#[cb->pb declareTypes:[NSArray arrayWithObject:NSStringPboardType] owner:nil];
|
|
|
|
#ret = [cb->pb setString:ns_clip forType:NSStringPboardType];
|
|
|
|
#[ns_clip release];
|
|
|
|
mut serial := 0
|
|
|
|
#serial = [cb->pb changeCount];
|
|
|
|
C.OSAtomicCompareAndSwapLong(cb.last_cb_serial, serial, &cb.last_cb_serial)
|
|
|
|
return ret
|
|
|
|
}
|
|
|
|
|
2020-05-17 14:51:18 +03:00
|
|
|
fn (mut cb Clipboard) get_text() string {
|
2019-12-07 15:22:28 +03:00
|
|
|
cb.foo = 0
|
2019-11-17 05:40:03 +03:00
|
|
|
#NSString *ns_clip;
|
2019-12-07 15:22:28 +03:00
|
|
|
utf8_clip := byteptr(0)
|
2019-11-17 05:40:03 +03:00
|
|
|
#ns_clip = [cb->pb stringForType:NSStringPboardType]; //NSPasteboardTypeString
|
|
|
|
#if (ns_clip == nil) {
|
2020-11-03 01:00:29 +03:00
|
|
|
#return tos3(""); //in case clipboard is empty
|
2019-11-17 05:40:03 +03:00
|
|
|
#}
|
|
|
|
#utf8_clip = [ns_clip UTF8String];
|
2020-11-03 01:00:29 +03:00
|
|
|
return unsafe {utf8_clip.vstring()}
|
2019-11-18 13:10:31 +03:00
|
|
|
}
|
2019-12-27 19:59:04 +03:00
|
|
|
|
|
|
|
pub fn new_primary() &Clipboard {
|
|
|
|
panic('Primary clipboard is not supported on non-Linux systems.')
|
|
|
|
}
|