mirror of
https://github.com/m1k1o/neko.git
synced 2024-07-24 14:40:50 +12:00
xclip multiple clipboard content types.
This commit is contained in:
parent
4602f5f6cf
commit
2d11b4fa56
@ -7,11 +7,22 @@ import (
|
||||
)
|
||||
|
||||
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) {
|
||||
text, err := h.desktop.ReadClipboard()
|
||||
func (h *RoomHandler) clipboardGetTargets(w http.ResponseWriter, r *http.Request) {
|
||||
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 {
|
||||
utils.HttpInternalServerError(w, err)
|
||||
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{}
|
||||
if !utils.HttpJsonRequest(w, r, data) {
|
||||
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 {
|
||||
utils.HttpInternalServerError(w, err)
|
||||
return
|
||||
|
@ -38,8 +38,11 @@ func (h *RoomHandler) Route(r chi.Router) {
|
||||
})
|
||||
|
||||
r.With(auth.HostsOnly).Route("/clipboard", func(r chi.Router) {
|
||||
r.Get("/", h.clipboardRead)
|
||||
r.Post("/", h.clipboardWrite)
|
||||
r.Get("/targets", h.clipboardGetTargets)
|
||||
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) {
|
||||
|
@ -7,8 +7,8 @@ import (
|
||||
"strings"
|
||||
)
|
||||
|
||||
func (manager *DesktopManagerCtx) ReadClipboard() (string, error) {
|
||||
cmd := exec.Command("xclip", "-selection", "clipboard", "-o")
|
||||
func (manager *DesktopManagerCtx) ClipboardGetBinary(mime string) ([]byte, error) {
|
||||
cmd := exec.Command("xclip", "-selection", "clipboard", "-o", "-t", mime)
|
||||
|
||||
var stdout, stderr bytes.Buffer
|
||||
cmd.Stdout = &stdout
|
||||
@ -17,15 +17,15 @@ func (manager *DesktopManagerCtx) ReadClipboard() (string, error) {
|
||||
err := cmd.Run()
|
||||
if err != nil {
|
||||
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 {
|
||||
cmd := exec.Command("xclip", "-selection", "clipboard", "-i")
|
||||
cmd.Stdin = strings.NewReader(data)
|
||||
func (manager *DesktopManagerCtx) ClipboardSetBinary(mime string, data []byte) error {
|
||||
cmd := exec.Command("xclip", "-selection", "clipboard", "-i", "-t", mime)
|
||||
cmd.Stdin = bytes.NewReader(data)
|
||||
|
||||
var stderr bytes.Buffer
|
||||
cmd.Stderr = &stderr
|
||||
@ -38,3 +38,51 @@ func (manager *DesktopManagerCtx) WriteClipboard(data string) error {
|
||||
|
||||
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))
|
||||
}
|
||||
|
@ -67,8 +67,13 @@ type DesktopManager interface {
|
||||
OnEventError(listener func(error_code uint8, message string, request_code uint8, minor_code uint8))
|
||||
|
||||
// clipboard
|
||||
ReadClipboard() (string, error)
|
||||
WriteClipboard(data string) error
|
||||
ClipboardGetBinary(mime string) ([]byte, 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
|
||||
DropFiles(x int, y int, files []string) bool
|
||||
|
@ -16,5 +16,5 @@ func (h *MessageHandlerCtx) clipboardSet(session types.Session, payload *message
|
||||
return nil
|
||||
}
|
||||
|
||||
return h.desktop.WriteClipboard(payload.Text)
|
||||
return h.desktop.ClipboardSetPlainText(payload.Text)
|
||||
}
|
||||
|
@ -117,7 +117,7 @@ func (ws *WebSocketManagerCtx) Start() {
|
||||
return
|
||||
}
|
||||
|
||||
text, err := ws.desktop.ReadClipboard()
|
||||
text, err := ws.desktop.ClipboardGetPlainText()
|
||||
if err != nil {
|
||||
ws.logger.Warn().Err(err).Msg("could not get clipboard content")
|
||||
}
|
||||
|
Loading…
Reference in New Issue
Block a user