replace libclipboard with xclip.

This commit is contained in:
Miroslav Šedivý 2021-01-28 21:12:35 +01:00
parent ced0a89bbe
commit 11e74459ac
9 changed files with 33 additions and 96 deletions

View File

@ -11,17 +11,7 @@ RUN set -eux; \
apt-get update; \ apt-get update; \
apt-get install -y --no-install-recommends \ apt-get install -y --no-install-recommends \
libx11-dev libxrandr-dev libxtst-dev libgtk-3-dev \ libx11-dev libxrandr-dev libxtst-dev libgtk-3-dev \
libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev \ libgstreamer1.0-dev libgstreamer-plugins-base1.0-dev; \
git cmake make; \
#
# install libclipboard
cd /tmp; \
git clone https://github.com/jtanx/libclipboard; \
cd libclipboard; \
cmake .; \
make -j`nproc`; \
make install; \
rm -rf /tmp/libclipboard; \
# #
# clean up # clean up
apt-get clean -y; \ apt-get clean -y; \
@ -53,8 +43,8 @@ RUN set -eux; \
pulseaudio dbus-x11 xserver-xorg-video-dummy xserver-xorg-input-void \ pulseaudio dbus-x11 xserver-xorg-video-dummy xserver-xorg-input-void \
libcairo2 libxcb1 libxrandr2 libxv1 libopus0 libvpx5 \ libcairo2 libxcb1 libxrandr2 libxv1 libopus0 libvpx5 \
# #
# file chooser handler # file chooser handler, clipboard
xdotool \ xdotool xclip \
# #
# gst # gst
gstreamer1.0-plugins-base gstreamer1.0-plugins-good \ gstreamer1.0-plugins-base gstreamer1.0-plugins-good \

View File

@ -11,8 +11,11 @@ type ClipboardPayload struct {
} }
func (h *RoomHandler) clipboardRead(w http.ResponseWriter, r *http.Request) { func (h *RoomHandler) clipboardRead(w http.ResponseWriter, r *http.Request) {
// TODO: error check? text, err := h.desktop.ReadClipboard()
text := h.desktop.ReadClipboard() if err != nil {
utils.HttpInternalServerError(w, err)
return
}
utils.HttpSuccess(w, ClipboardPayload{ utils.HttpSuccess(w, ClipboardPayload{
Text: text, Text: text,
@ -25,7 +28,11 @@ func (h *RoomHandler) clipboardWrite(w http.ResponseWriter, r *http.Request) {
return return
} }
// TODO: error check? err := h.desktop.WriteClipboard(data.Text)
h.desktop.WriteClipboard(data.Text) if err != nil {
utils.HttpInternalServerError(w, err)
return
}
utils.HttpSuccess(w) utils.HttpSuccess(w)
} }

View File

@ -1,13 +1,17 @@
package desktop package desktop
import ( import (
"demodesk/neko/internal/desktop/clipboard" "os/exec"
"strings"
) )
func (manager *DesktopManagerCtx) ReadClipboard() string { func (manager *DesktopManagerCtx) ReadClipboard() (string, error) {
return clipboard.ReadClipboard() out, err := exec.Command("xclip", "-selection", "clipboard", "-o").Output()
return string(out), err
} }
func (manager *DesktopManagerCtx) WriteClipboard(data string) { func (manager *DesktopManagerCtx) WriteClipboard(data string) error {
clipboard.WriteClipboard(data) cmd := exec.Command("xclip", "-selection", "clipboard", "-i")
cmd.Stdin = strings.NewReader(data)
return cmd.Run()
} }

View File

@ -1,21 +0,0 @@
#include "clipboard.h"
static clipboard_c *CLIPBOARD = NULL;
clipboard_c *getClipboard(void) {
if (CLIPBOARD == NULL) {
CLIPBOARD = clipboard_new(NULL);
}
return CLIPBOARD;
}
void ClipboardSet(char *src) {
clipboard_c *cb = getClipboard();
clipboard_set_text_ex(cb, src, strlen(src), 0);
}
char *ClipboardGet() {
clipboard_c *cb = getClipboard();
return clipboard_text_ex(cb, NULL, 0);
}

View File

@ -1,35 +0,0 @@
package clipboard
/*
#cgo linux LDFLAGS: /usr/local/lib/libclipboard.a -lxcb
#include "clipboard.h"
*/
import "C"
import (
"sync"
"unsafe"
)
var mu = sync.Mutex{}
func ReadClipboard() string {
mu.Lock()
defer mu.Unlock()
clipboardUnsafe := C.ClipboardGet()
defer C.free(unsafe.Pointer(clipboardUnsafe))
return C.GoString(clipboardUnsafe)
}
func WriteClipboard(data string) {
mu.Lock()
defer mu.Unlock()
clipboardUnsafe := C.CString(data)
defer C.free(unsafe.Pointer(clipboardUnsafe))
C.ClipboardSet(clipboardUnsafe)
}

View File

@ -1,9 +0,0 @@
#pragma once
#include <libclipboard.h>
#include <string.h>
clipboard_c *getClipboard(void);
void ClipboardSet(char *src);
char *ClipboardGet();

View File

@ -67,8 +67,8 @@ 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 ReadClipboard() (string, error)
WriteClipboard(data string) WriteClipboard(data string) error
// drop // drop
DropFiles(x int, y int, files []string) bool DropFiles(x int, y int, files []string) bool

View File

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

View File

@ -117,13 +117,15 @@ func (ws *WebSocketManagerCtx) Start() {
return return
} }
text := ws.desktop.ReadClipboard() text, err := ws.desktop.ReadClipboard()
err := session.Send(message.ClipboardData{ if err != nil {
ws.logger.Warn().Err(err).Msg("could not get clipboard content")
}
if err := session.Send(message.ClipboardData{
Event: event.CLIPBOARD_UPDATED, Event: event.CLIPBOARD_UPDATED,
Text: text, Text: text,
}) }); err != nil {
if err != nil {
ws.logger.Warn().Err(err).Msg("could not sync clipboard") ws.logger.Warn().Err(err).Msg("could not sync clipboard")
} }
}) })