From e3e3cf9d22cdebf46fd093ccfb629e2bf8cbb711 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Mon, 12 Sep 2022 21:33:08 +0200 Subject: [PATCH] extract clipboard from xorg. --- server/internal/remote/clipboard/clipboard.c | 21 +++++++++++ server/internal/remote/clipboard/clipboard.go | 35 +++++++++++++++++++ server/internal/remote/clipboard/clipboard.h | 9 +++++ server/internal/remote/manager.go | 7 ++-- server/internal/{ => remote}/xorg/xorg.c | 18 ---------- server/internal/{ => remote}/xorg/xorg.go | 22 +----------- server/internal/{ => remote}/xorg/xorg.h | 5 --- 7 files changed, 70 insertions(+), 47 deletions(-) create mode 100644 server/internal/remote/clipboard/clipboard.c create mode 100644 server/internal/remote/clipboard/clipboard.go create mode 100644 server/internal/remote/clipboard/clipboard.h rename server/internal/{ => remote}/xorg/xorg.c (94%) rename server/internal/{ => remote}/xorg/xorg.go (89%) rename server/internal/{ => remote}/xorg/xorg.h (91%) diff --git a/server/internal/remote/clipboard/clipboard.c b/server/internal/remote/clipboard/clipboard.c new file mode 100644 index 0000000..c500b33 --- /dev/null +++ b/server/internal/remote/clipboard/clipboard.c @@ -0,0 +1,21 @@ +#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); +} diff --git a/server/internal/remote/clipboard/clipboard.go b/server/internal/remote/clipboard/clipboard.go new file mode 100644 index 0000000..a694dfa --- /dev/null +++ b/server/internal/remote/clipboard/clipboard.go @@ -0,0 +1,35 @@ +package clipboard + +/* +#cgo linux LDFLAGS: /usr/local/lib/libclipboard.a -lxcb + +#include "clipboard.h" +*/ +import "C" + +import ( + "sync" + "unsafe" +) + +var mu = sync.Mutex{} + +func Read() string { + mu.Lock() + defer mu.Unlock() + + clipboardUnsafe := C.ClipboardGet() + defer C.free(unsafe.Pointer(clipboardUnsafe)) + + return C.GoString(clipboardUnsafe) +} + +func Write(data string) { + mu.Lock() + defer mu.Unlock() + + clipboardUnsafe := C.CString(data) + defer C.free(unsafe.Pointer(clipboardUnsafe)) + + C.ClipboardSet(clipboardUnsafe) +} diff --git a/server/internal/remote/clipboard/clipboard.h b/server/internal/remote/clipboard/clipboard.h new file mode 100644 index 0000000..5f5cf36 --- /dev/null +++ b/server/internal/remote/clipboard/clipboard.h @@ -0,0 +1,9 @@ +#pragma once + +#include +#include + +clipboard_c *getClipboard(void); + +void ClipboardSet(char *src); +char *ClipboardGet(); diff --git a/server/internal/remote/manager.go b/server/internal/remote/manager.go index fa73387..f37b285 100644 --- a/server/internal/remote/manager.go +++ b/server/internal/remote/manager.go @@ -6,9 +6,10 @@ import ( "time" "m1k1o/neko/internal/gst" + "m1k1o/neko/internal/remote/clipboard" + "m1k1o/neko/internal/remote/xorg" "m1k1o/neko/internal/types" "m1k1o/neko/internal/types/config" - "m1k1o/neko/internal/xorg" "github.com/kataras/go-events" "github.com/rs/zerolog" @@ -233,11 +234,11 @@ func (manager *RemoteManager) KeyUp(code uint64) error { } func (manager *RemoteManager) ReadClipboard() string { - return xorg.ReadClipboard() + return clipboard.Read() } func (manager *RemoteManager) WriteClipboard(data string) { - xorg.WriteClipboard(data) + clipboard.Write(data) } func (manager *RemoteManager) ResetKeys() { diff --git a/server/internal/xorg/xorg.c b/server/internal/remote/xorg/xorg.c similarity index 94% rename from server/internal/xorg/xorg.c rename to server/internal/remote/xorg/xorg.c index 8defa28..51dfc80 100644 --- a/server/internal/xorg/xorg.c +++ b/server/internal/remote/xorg/xorg.c @@ -1,6 +1,5 @@ #include "xorg.h" -static clipboard_c *CLIPBOARD = NULL; static Display *DISPLAY = NULL; static char *NAME = ":0.0"; static int REGISTERED = 0; @@ -75,13 +74,6 @@ Display *getXDisplay(void) { return DISPLAY; } -clipboard_c *getClipboard(void) { - if (CLIPBOARD == NULL) { - CLIPBOARD = clipboard_new(NULL); - } - return CLIPBOARD; -} - void XDisplayClose(void) { if (DISPLAY != NULL) { XCloseDisplay(DISPLAY); @@ -208,16 +200,6 @@ void XKey(KeySym key, int down) { XSync(display, 0); } -void XClipboardSet(char *src) { - clipboard_c *cb = getClipboard(); - clipboard_set_text_ex(cb, src, strlen(src), 0); -} - -char *XClipboardGet() { - clipboard_c *cb = getClipboard(); - return clipboard_text_ex(cb, NULL, 0); -} - void XGetScreenConfigurations() { Display *display = getXDisplay(); Window root = RootWindow(display, 0); diff --git a/server/internal/xorg/xorg.go b/server/internal/remote/xorg/xorg.go similarity index 89% rename from server/internal/xorg/xorg.go rename to server/internal/remote/xorg/xorg.go index 9da7d40..6c7bd88 100644 --- a/server/internal/xorg/xorg.go +++ b/server/internal/remote/xorg/xorg.go @@ -2,7 +2,7 @@ package xorg /* #cgo CFLAGS: -I/usr/local/include/ -#cgo LDFLAGS: /usr/local/lib/libclipboard.a -L/usr/local/lib -lX11 -lXtst -lXrandr -lxcb +#cgo LDFLAGS: -lX11 -lXtst -lXrandr -lxcb #include "xorg.h" */ @@ -107,26 +107,6 @@ func KeyUp(code uint64) error { return nil } -func ReadClipboard() string { - mu.Lock() - defer mu.Unlock() - - clipboardUnsafe := C.XClipboardGet() - 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.XClipboardSet(clipboardUnsafe) -} - func ResetKeys() { for code := range debounce_button { _ = ButtonUp(code) diff --git a/server/internal/xorg/xorg.h b/server/internal/remote/xorg/xorg.h similarity index 91% rename from server/internal/xorg/xorg.h rename to server/internal/remote/xorg/xorg.h index b4078ad..4a284e6 100644 --- a/server/internal/xorg/xorg.h +++ b/server/internal/remote/xorg/xorg.h @@ -3,7 +3,6 @@ #include #include #include -#include #include /* For free() */ #include /* For fputs() */ #include /* For strdup() */ @@ -23,16 +22,12 @@ typedef struct xkeys_t { * * Note that this is almost certainly not thread safe. */ Display *getXDisplay(void); -clipboard_c *getClipboard(void); void XMove(int x, int y); void XScroll(int x, int y); void XButton(unsigned int button, int down); void XKey(unsigned long key, int down); -void XClipboardSet(char *src); -char *XClipboardGet(); - void XGetScreenConfigurations(); void XSetScreenConfiguration(int index, short rate); int XGetScreenSize();