2020-11-02 04:09:48 +13:00
|
|
|
package desktop
|
|
|
|
|
|
|
|
import (
|
2021-01-29 09:19:31 +13:00
|
|
|
"fmt"
|
|
|
|
"bytes"
|
2021-01-29 09:12:35 +13:00
|
|
|
"os/exec"
|
|
|
|
"strings"
|
2020-11-02 04:09:48 +13:00
|
|
|
)
|
|
|
|
|
2021-01-29 10:51:55 +13:00
|
|
|
func (manager *DesktopManagerCtx) ClipboardGetBinary(mime string) ([]byte, error) {
|
2021-01-29 12:03:22 +13:00
|
|
|
cmd := exec.Command("xclip", "-selection", "clipboard", "-out", "-target", mime)
|
2021-01-29 09:19:31 +13:00
|
|
|
|
|
|
|
var stdout, stderr bytes.Buffer
|
|
|
|
cmd.Stdout = &stdout
|
|
|
|
cmd.Stderr = &stderr
|
|
|
|
|
|
|
|
err := cmd.Run()
|
|
|
|
if err != nil {
|
|
|
|
msg := strings.TrimSpace(string(stderr.Bytes()))
|
2021-01-29 10:51:55 +13:00
|
|
|
return nil, fmt.Errorf("%s", msg)
|
2021-01-29 09:19:31 +13:00
|
|
|
}
|
|
|
|
|
2021-01-29 10:51:55 +13:00
|
|
|
return stdout.Bytes(), nil
|
2020-11-02 04:09:48 +13:00
|
|
|
}
|
|
|
|
|
2021-01-29 10:51:55 +13:00
|
|
|
func (manager *DesktopManagerCtx) ClipboardSetBinary(mime string, data []byte) error {
|
2021-01-29 12:03:22 +13:00
|
|
|
cmd := exec.Command("xclip", "-selection", "clipboard", "-in", "-target", mime)
|
2021-01-29 09:19:31 +13:00
|
|
|
|
|
|
|
var stderr bytes.Buffer
|
|
|
|
cmd.Stderr = &stderr
|
|
|
|
|
2021-01-29 12:03:22 +13:00
|
|
|
stdin, err := cmd.StdinPipe()
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = cmd.Start()
|
2021-01-29 09:19:31 +13:00
|
|
|
if err != nil {
|
|
|
|
msg := strings.TrimSpace(string(stderr.Bytes()))
|
|
|
|
return fmt.Errorf("%s", msg)
|
|
|
|
}
|
2021-01-29 12:03:22 +13:00
|
|
|
|
|
|
|
stdin.Write(data)
|
|
|
|
stdin.Close()
|
2021-01-29 09:19:31 +13:00
|
|
|
|
2021-01-29 12:03:22 +13:00
|
|
|
// TODO: Refactor.
|
|
|
|
// cmd.Wait()
|
2021-01-29 09:19:31 +13:00
|
|
|
return nil
|
2020-11-02 04:09:48 +13:00
|
|
|
}
|
2021-01-29 10:51:55 +13:00
|
|
|
|
|
|
|
func (manager *DesktopManagerCtx) ClipboardGetTargets() ([]string, error) {
|
2021-01-29 12:03:22 +13:00
|
|
|
cmd := exec.Command("xclip", "-selection", "clipboard", "-out", "-target", "TARGETS")
|
2021-01-29 10:51:55 +13:00
|
|
|
|
|
|
|
var stdout, stderr bytes.Buffer
|
|
|
|
cmd.Stdout = &stdout
|
|
|
|
cmd.Stderr = &stderr
|
|
|
|
|
|
|
|
err := cmd.Run()
|
|
|
|
if err != nil {
|
|
|
|
msg := strings.TrimSpace(string(stderr.Bytes()))
|
|
|
|
return nil, fmt.Errorf("%s", msg)
|
|
|
|
}
|
|
|
|
|
|
|
|
var response []string
|
|
|
|
targets := strings.Split(string(stdout.Bytes()), "\n")
|
|
|
|
for _, target := range targets {
|
|
|
|
if target == "" {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if !strings.Contains(target, "/") {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
response = append(response, target)
|
|
|
|
}
|
|
|
|
|
|
|
|
return response, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *DesktopManagerCtx) ClipboardGetPlainText() (string, error) {
|
|
|
|
bytes, err := manager.ClipboardGetBinary("STRING")
|
|
|
|
return string(bytes), err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *DesktopManagerCtx) ClipboardSetPlainText(data string) error {
|
|
|
|
return manager.ClipboardSetBinary("STRING", []byte(data))
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *DesktopManagerCtx) ClipboardGetRichText() (string, error) {
|
|
|
|
bytes, err := manager.ClipboardGetBinary("text/html")
|
|
|
|
return string(bytes), err
|
|
|
|
}
|
|
|
|
|
|
|
|
func (manager *DesktopManagerCtx) ClipboardSetRichText(data string) error {
|
|
|
|
return manager.ClipboardSetBinary("text/html", []byte(data))
|
|
|
|
}
|