Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add poll interval and setter #54

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 9 additions & 0 deletions clipboard.go
Original file line number Diff line number Diff line change
Expand Up @@ -61,13 +61,15 @@ import (
"fmt"
"os"
"sync"
"time"
)

var (
// activate only for running tests.
debug = false
errUnavailable = errors.New("clipboard unavailable")
errUnsupported = errors.New("unsupported format")
poll_interval = time.Second
)

// Format represents the format of clipboard data.
Expand Down Expand Up @@ -109,6 +111,13 @@ func Init() error {
return initError
}

// Sets the time between checking for clipboard updates
// in the Watch function and for Write's changed channel (on some platforms)
// the interval is by default 1 second
func SetPollingInterval(interval time.Duration) {
poll_interval = interval
}

// Read returns a chunk of bytes of the clipboard data if it presents
// in the desired format t presents. Otherwise, it returns nil.
func Read(t Format) []byte {
Expand Down
2 changes: 1 addition & 1 deletion clipboard_android.go
Original file line number Diff line number Diff line change
Expand Up @@ -78,7 +78,7 @@ func write(t Format, buf []byte) (<-chan struct{}, error) {

func watch(ctx context.Context, t Format) <-chan []byte {
recv := make(chan []byte, 1)
ti := time.NewTicker(time.Second)
ti := time.NewTicker(poll_interval)
last := Read(t)
go func() {
for {
Expand Down
4 changes: 2 additions & 2 deletions clipboard_darwin.go
Original file line number Diff line number Diff line change
Expand Up @@ -82,7 +82,7 @@ func write(t Format, buf []byte) (<-chan struct{}, error) {
go func() {
for {
// not sure if we are too slow or the user too fast :)
time.Sleep(time.Second)
time.Sleep(poll_interval)
cur := C.long(C.clipboard_change_count())
if cnt != cur {
changed <- struct{}{}
Expand All @@ -97,7 +97,7 @@ func write(t Format, buf []byte) (<-chan struct{}, error) {
func watch(ctx context.Context, t Format) <-chan []byte {
recv := make(chan []byte, 1)
// not sure if we are too slow or the user too fast :)
ti := time.NewTicker(time.Second)
ti := time.NewTicker(poll_interval)
lastCount := C.long(C.clipboard_change_count())
go func() {
for {
Expand Down
2 changes: 1 addition & 1 deletion clipboard_ios.go
Original file line number Diff line number Diff line change
Expand Up @@ -56,7 +56,7 @@ func write(t Format, buf []byte) (<-chan struct{}, error) {

func watch(ctx context.Context, t Format) <-chan []byte {
recv := make(chan []byte, 1)
ti := time.NewTicker(time.Second)
ti := time.NewTicker(poll_interval)
last := Read(t)
go func() {
for {
Expand Down
2 changes: 1 addition & 1 deletion clipboard_linux.go
Original file line number Diff line number Diff line change
Expand Up @@ -135,7 +135,7 @@ func write(t Format, buf []byte) (<-chan struct{}, error) {

func watch(ctx context.Context, t Format) <-chan []byte {
recv := make(chan []byte, 1)
ti := time.NewTicker(time.Second)
ti := time.NewTicker(poll_interval)
last := Read(t)
go func() {
for {
Expand Down
4 changes: 2 additions & 2 deletions clipboard_windows.go
Original file line number Diff line number Diff line change
Expand Up @@ -382,7 +382,7 @@ func write(t Format, buf []byte) (<-chan struct{}, error) {
cnt, _, _ := getClipboardSequenceNumber.Call()
errch <- nil
for {
time.Sleep(time.Second)
time.Sleep(poll_interval)
cur, _, _ := getClipboardSequenceNumber.Call()
if cur != cnt {
changed <- struct{}{}
Expand All @@ -403,7 +403,7 @@ func watch(ctx context.Context, t Format) <-chan []byte {
ready := make(chan struct{})
go func() {
// not sure if we are too slow or the user too fast :)
ti := time.NewTicker(time.Second)
ti := time.NewTicker(poll_interval)
cnt, _, _ := getClipboardSequenceNumber.Call()
ready <- struct{}{}
for {
Expand Down