feat: native clipboard image sync (Linux, opt-in sync_images)
This commit is contained in:
+44
-15
@@ -4,6 +4,9 @@ package daemon
|
||||
|
||||
import (
|
||||
"context"
|
||||
"crypto/sha256"
|
||||
"encoding/base64"
|
||||
"encoding/hex"
|
||||
"log"
|
||||
"os"
|
||||
"sync"
|
||||
@@ -153,6 +156,10 @@ func (d *Daemon) Run(ctx context.Context) error {
|
||||
defer ticker.Stop()
|
||||
|
||||
var lastText string
|
||||
var lastImageHash string
|
||||
|
||||
// Optional image sync (Linux only today); requires platform support.
|
||||
imageCB, _ := d.clipboard.(clipboard.ImageClipboard)
|
||||
|
||||
log.Printf("clip-sync: polling clipboard every %v, %d peer(s) configured",
|
||||
d.cfg.PollInterval(), len(d.cfg.Peers))
|
||||
@@ -168,26 +175,48 @@ func (d *Daemon) Run(ctx context.Context) error {
|
||||
text, err := d.clipboard.Read()
|
||||
if err != nil {
|
||||
log.Printf("clip-sync: clipboard read: %v", err)
|
||||
continue
|
||||
} else if text != "" && text != lastText {
|
||||
lastText = text
|
||||
|
||||
// Record our write so the filter can block echoes.
|
||||
d.filter.MarkWritten(text)
|
||||
|
||||
if d.history != nil {
|
||||
d.history.Append(history.Entry{Text: text, Origin: d.origin, Ts: time.Now().UnixNano()})
|
||||
}
|
||||
|
||||
if len(d.cfg.Peers) > 0 || d.discovery != nil {
|
||||
d.broadcaster.Broadcast(text)
|
||||
}
|
||||
}
|
||||
|
||||
if text == "" || text == lastText {
|
||||
continue
|
||||
}
|
||||
// Image sync (opt-in).
|
||||
if d.cfg.Daemon.SyncImages && imageCB != nil {
|
||||
mime, data, err := imageCB.ReadImage()
|
||||
if err != nil || len(data) == 0 {
|
||||
continue
|
||||
}
|
||||
h := hashBytes(data)
|
||||
if h == lastImageHash {
|
||||
continue
|
||||
}
|
||||
lastImageHash = h
|
||||
|
||||
lastText = text
|
||||
d.filter.MarkWrittenPayload(dedup.Payload{
|
||||
Mime: mime,
|
||||
Data: base64.StdEncoding.EncodeToString(data),
|
||||
})
|
||||
|
||||
// Record our write so the filter can block echoes.
|
||||
d.filter.MarkWritten(text)
|
||||
|
||||
if d.history != nil {
|
||||
d.history.Append(history.Entry{Text: text, Origin: d.origin, Ts: time.Now().UnixNano()})
|
||||
}
|
||||
|
||||
// Broadcast to all peers.
|
||||
if len(d.cfg.Peers) > 0 || d.discovery != nil {
|
||||
d.broadcaster.Broadcast(text)
|
||||
if len(d.cfg.Peers) > 0 || d.discovery != nil {
|
||||
d.broadcaster.BroadcastImage(mime, data)
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// hashBytes returns a short hex digest of b, used to detect clipboard changes.
|
||||
func hashBytes(b []byte) string {
|
||||
sum := sha256.Sum256(b)
|
||||
return hex.EncodeToString(sum[:])
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user