xclip multiple clipboard content types.

This commit is contained in:
Miroslav Šedivý 2021-01-28 22:51:55 +01:00
parent 4602f5f6cf
commit 2d11b4fa56
6 changed files with 112 additions and 18 deletions

View File

@ -7,11 +7,22 @@ import (
) )
type ClipboardPayload struct { type ClipboardPayload struct {
Text string `json:"text"` Text string `json:"text,omitempty"`
HTML string `json:"html,omitempty"`
} }
func (h *RoomHandler) clipboardRead(w http.ResponseWriter, r *http.Request) { func (h *RoomHandler) clipboardGetTargets(w http.ResponseWriter, r *http.Request) {
text, err := h.desktop.ReadClipboard() targets, err := h.desktop.ClipboardGetTargets()
if err != nil {
utils.HttpInternalServerError(w, err)
return
}
utils.HttpSuccess(w, targets)
}
func (h *RoomHandler) clipboardGetPlainText(w http.ResponseWriter, r *http.Request) {
text, err := h.desktop.ClipboardGetPlainText()
if err != nil { if err != nil {
utils.HttpInternalServerError(w, err) utils.HttpInternalServerError(w, err)
return return
@ -22,13 +33,40 @@ func (h *RoomHandler) clipboardRead(w http.ResponseWriter, r *http.Request) {
}) })
} }
func (h *RoomHandler) clipboardWrite(w http.ResponseWriter, r *http.Request) { func (h *RoomHandler) clipboardSetPlainText(w http.ResponseWriter, r *http.Request) {
data := &ClipboardPayload{} data := &ClipboardPayload{}
if !utils.HttpJsonRequest(w, r, data) { if !utils.HttpJsonRequest(w, r, data) {
return return
} }
err := h.desktop.WriteClipboard(data.Text) err := h.desktop.ClipboardSetPlainText(data.Text)
if err != nil {
utils.HttpInternalServerError(w, err)
return
}
utils.HttpSuccess(w)
}
func (h *RoomHandler) clipboardGetRichText(w http.ResponseWriter, r *http.Request) {
html, err := h.desktop.ClipboardGetRichText()
if err != nil {
utils.HttpInternalServerError(w, err)
return
}
utils.HttpSuccess(w, ClipboardPayload{
HTML: html,
})
}
func (h *RoomHandler) clipboardSetRichText(w http.ResponseWriter, r *http.Request) {
data := &ClipboardPayload{}
if !utils.HttpJsonRequest(w, r, data) {
return
}
err := h.desktop.ClipboardSetRichText(data.HTML)
if err != nil { if err != nil {
utils.HttpInternalServerError(w, err) utils.HttpInternalServerError(w, err)
return return

View File

@ -38,8 +38,11 @@ func (h *RoomHandler) Route(r chi.Router) {
}) })
r.With(auth.HostsOnly).Route("/clipboard", func(r chi.Router) { r.With(auth.HostsOnly).Route("/clipboard", func(r chi.Router) {
r.Get("/", h.clipboardRead) r.Get("/targets", h.clipboardGetTargets)
r.Post("/", h.clipboardWrite) r.Get("/text", h.clipboardGetPlainText)
r.Post("/text", h.clipboardSetPlainText)
r.Get("/html", h.clipboardGetRichText)
r.Post("/html", h.clipboardSetRichText)
}) })
r.Route("/keyboard", func(r chi.Router) { r.Route("/keyboard", func(r chi.Router) {

View File

@ -7,8 +7,8 @@ import (
"strings" "strings"
) )
func (manager *DesktopManagerCtx) ReadClipboard() (string, error) { func (manager *DesktopManagerCtx) ClipboardGetBinary(mime string) ([]byte, error) {
cmd := exec.Command("xclip", "-selection", "clipboard", "-o") cmd := exec.Command("xclip", "-selection", "clipboard", "-o", "-t", mime)
var stdout, stderr bytes.Buffer var stdout, stderr bytes.Buffer
cmd.Stdout = &stdout cmd.Stdout = &stdout
@ -17,15 +17,15 @@ func (manager *DesktopManagerCtx) ReadClipboard() (string, error) {
err := cmd.Run() err := cmd.Run()
if err != nil { if err != nil {
msg := strings.TrimSpace(string(stderr.Bytes())) msg := strings.TrimSpace(string(stderr.Bytes()))
return "", fmt.Errorf("%s", msg) return nil, fmt.Errorf("%s", msg)
} }
return string(stdout.Bytes()), nil return stdout.Bytes(), nil
} }
func (manager *DesktopManagerCtx) WriteClipboard(data string) error { func (manager *DesktopManagerCtx) ClipboardSetBinary(mime string, data []byte) error {
cmd := exec.Command("xclip", "-selection", "clipboard", "-i") cmd := exec.Command("xclip", "-selection", "clipboard", "-i", "-t", mime)
cmd.Stdin = strings.NewReader(data) cmd.Stdin = bytes.NewReader(data)
var stderr bytes.Buffer var stderr bytes.Buffer
cmd.Stderr = &stderr cmd.Stderr = &stderr
@ -38,3 +38,51 @@ func (manager *DesktopManagerCtx) WriteClipboard(data string) error {
return nil return nil
} }
func (manager *DesktopManagerCtx) ClipboardGetTargets() ([]string, error) {
cmd := exec.Command("xclip", "-selection", "clipboard", "-o", "-t", "TARGETS")
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))
}

View File

@ -67,8 +67,13 @@ type DesktopManager interface {
OnEventError(listener func(error_code uint8, message string, request_code uint8, minor_code uint8)) OnEventError(listener func(error_code uint8, message string, request_code uint8, minor_code uint8))
// clipboard // clipboard
ReadClipboard() (string, error) ClipboardGetBinary(mime string) ([]byte, error)
WriteClipboard(data string) error ClipboardSetBinary(mime string, data []byte) error
ClipboardGetTargets() ([]string, error)
ClipboardGetPlainText() (string, error)
ClipboardSetPlainText(data string) error
ClipboardGetRichText() (string, error)
ClipboardSetRichText(data string) error
// drop // drop
DropFiles(x int, y int, files []string) bool DropFiles(x int, y int, files []string) bool

View File

@ -16,5 +16,5 @@ func (h *MessageHandlerCtx) clipboardSet(session types.Session, payload *message
return nil return nil
} }
return h.desktop.WriteClipboard(payload.Text) return h.desktop.ClipboardSetPlainText(payload.Text)
} }

View File

@ -117,7 +117,7 @@ func (ws *WebSocketManagerCtx) Start() {
return return
} }
text, err := ws.desktop.ReadClipboard() text, err := ws.desktop.ClipboardGetPlainText()
if err != nil { if err != nil {
ws.logger.Warn().Err(err).Msg("could not get clipboard content") ws.logger.Warn().Err(err).Msg("could not get clipboard content")
} }