-
Notifications
You must be signed in to change notification settings - Fork 3
/
clipboard_darwin.go
81 lines (72 loc) · 1.15 KB
/
clipboard_darwin.go
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
package clipboard
// #cgo CFLAGS: -x objective-c
// #cgo LDFLAGS: -framework Cocoa
//
// #include <stdlib.h>
//
// long count();
// char *get();
// int set(const char *);
import "C"
import (
"errors"
"github.com/tjgq/ticker"
"time"
"unsafe"
)
const (
pollInterval = time.Second
)
var tick = ticker.New(pollInterval)
var seq C.long
func init() {
go loop()
}
func loop() {
for {
select {
case <-tick.C:
poll()
case r := <-ctl:
handle(r)
}
}
}
func handle(r request) {
switch r.t {
case watchRequest:
seq = C.count()
tick.Start()
r.c <- reply{}
case unwatchRequest:
tick.Stop()
r.c <- reply{}
case getRequest:
s := C.get()
if s == nil {
r.c <- reply{"", errors.New("clipboard read error")}
} else {
r.c <- reply{C.GoString(s), nil}
C.free(unsafe.Pointer(s))
}
case setRequest:
s := C.CString(r.s)
if C.set(s) == 0 {
r.c <- reply{"", errors.New("clipboard write error")}
} else {
r.c <- reply{"", nil}
}
C.free(unsafe.Pointer(s))
}
}
func poll() {
newseq := C.count()
if newseq != seq {
seq = newseq
s := C.get()
if s != nil {
broadcast(C.GoString(s))
C.free(unsafe.Pointer(s))
}
}
}