From 2680a1f702c5aec07f7ccbabf2881bca04167922 Mon Sep 17 00:00:00 2001 From: Craig Date: Mon, 3 Feb 2020 14:49:27 +0000 Subject: [PATCH] muxed interactions with xserver, fix for #28? --- server/internal/clip/clip.c | 23 -------------- server/internal/clip/clip.go | 21 ------------ server/internal/clip/clip.h | 5 --- server/internal/hid/hid.c | 18 +++++++++++ server/internal/hid/hid.go | 44 +++++++++++++++++++++++++- server/internal/hid/hid.h | 13 +++----- server/internal/websocket/control.go | 4 +-- server/internal/websocket/websocket.go | 6 ++-- 8 files changed, 71 insertions(+), 63 deletions(-) delete mode 100644 server/internal/clip/clip.c delete mode 100644 server/internal/clip/clip.go delete mode 100644 server/internal/clip/clip.h diff --git a/server/internal/clip/clip.c b/server/internal/clip/clip.c deleted file mode 100644 index a2db5af6..00000000 --- a/server/internal/clip/clip.c +++ /dev/null @@ -1,23 +0,0 @@ -#include "clip.h" - -#include -#include - -clipboard_c *CLIPBOARD = NULL; - -clipboard_c *getClipboard(void) { - if (CLIPBOARD == NULL) { - CLIPBOARD = clipboard_new(NULL); - } - return CLIPBOARD; -} - -void set_clipboard(char *src) { - clipboard_c *cb = getClipboard(); - clipboard_set_text_ex(cb, src, strlen(src), 0); -} - -char * get_clipboard() { - clipboard_c *cb = getClipboard(); - return clipboard_text_ex(cb, NULL, 0); -} diff --git a/server/internal/clip/clip.go b/server/internal/clip/clip.go deleted file mode 100644 index 5e25a51f..00000000 --- a/server/internal/clip/clip.go +++ /dev/null @@ -1,21 +0,0 @@ -// NOTE: I have no fucking clue what I'm doing with this, -// it works, but I am positive I'm doing this very wrong... -// should I be freeing these strings? does go cg them? -// pretty sure this *isn't* thread safe either.... /shrug - -package clip - -/* -#cgo linux LDFLAGS: -lclipboard - -#include "clip.h" -*/ -import "C" - -func Read() string { - return C.GoString(C.get_clipboard()) -} - -func Write(data string) { - C.set_clipboard(C.CString(data)) -} diff --git a/server/internal/clip/clip.h b/server/internal/clip/clip.h deleted file mode 100644 index c9936350..00000000 --- a/server/internal/clip/clip.h +++ /dev/null @@ -1,5 +0,0 @@ -#include - -clipboard_c *getClipboard(void); -void set_clipboard(char *src); -char * get_clipboard(); diff --git a/server/internal/hid/hid.c b/server/internal/hid/hid.c index 0c2aa9a6..934b1132 100644 --- a/server/internal/hid/hid.c +++ b/server/internal/hid/hid.c @@ -1,5 +1,6 @@ #include "hid.h" +static clipboard_c *CLIPBOARD = NULL; static Display *DISPLAY = NULL; static char *NAME = ":0.0"; static int REGISTERED = 0; @@ -32,6 +33,13 @@ Display *getXDisplay(void) { return DISPLAY; } +clipboard_c *getClipboard(void) { + if (CLIPBOARD == NULL) { + CLIPBOARD = clipboard_new(NULL); + } + return CLIPBOARD; +} + void closeXDisplay(void) { if (DISPLAY != NULL) { XCloseDisplay(DISPLAY); @@ -92,3 +100,13 @@ void XKey(unsigned long key, int down) { XTestFakeKeyEvent(display, code, down, CurrentTime); 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); +} diff --git a/server/internal/hid/hid.go b/server/internal/hid/hid.go index 0c9950cd..46a30cbb 100644 --- a/server/internal/hid/hid.go +++ b/server/internal/hid/hid.go @@ -1,8 +1,13 @@ +// NOTE: I have no fucking clue what I'm doing with this, +// it works, but I am positive I'm doing this very wrong... +// should I be freeing these strings? does go cg them? +// pretty sure this *isn't* thread safe either.... /shrug + package hid /* #cgo linux CFLAGS: -I/usr/src -#cgo linux LDFLAGS: -L/usr/src -lX11 -lXtst +#cgo linux LDFLAGS: -L/usr/src -lX11 -lXtst -lclipboard #include "hid.h" */ @@ -10,6 +15,7 @@ import "C" import ( "fmt" + "sync" "time" "n.eko.moe/neko/internal/hid/keycode" @@ -18,6 +24,7 @@ import ( var debounce = make(map[int]time.Time) var buttons = make(map[int]keycode.Button) var keys = make(map[int]keycode.Key) +var mu = sync.Mutex{} func init() { keys[keycode.BACKSPACE.Code] = keycode.BACKSPACE @@ -130,18 +137,30 @@ func init() { } func Display(display string) { + mu.Lock() + defer mu.Unlock() + C.setXDisplay(C.CString(display)) } func Move(x, y int) { + mu.Lock() + defer mu.Unlock() + C.XMove(C.int(x), C.int(y)) } func Scroll(x, y int) { + mu.Lock() + defer mu.Unlock() + C.XScroll(C.int(x), C.int(y)) } func ButtonDown(code int) (*keycode.Button, error) { + mu.Lock() + defer mu.Unlock() + button, ok := buttons[code] if !ok { return nil, fmt.Errorf("invalid button %v", code) @@ -158,6 +177,9 @@ func ButtonDown(code int) (*keycode.Button, error) { } func KeyDown(code int) (*keycode.Key, error) { + mu.Lock() + defer mu.Unlock() + key, ok := keys[code] if !ok { return nil, fmt.Errorf("invalid key %v", code) @@ -174,6 +196,9 @@ func KeyDown(code int) (*keycode.Key, error) { } func ButtonUp(code int) (*keycode.Button, error) { + mu.Lock() + defer mu.Unlock() + button, ok := buttons[code] if !ok { return nil, fmt.Errorf("invalid button %v", code) @@ -190,6 +215,9 @@ func ButtonUp(code int) (*keycode.Button, error) { } func KeyUp(code int) (*keycode.Key, error) { + mu.Lock() + defer mu.Unlock() + key, ok := keys[code] if !ok { return nil, fmt.Errorf("invalid key %v", code) @@ -205,6 +233,20 @@ func KeyUp(code int) (*keycode.Key, error) { return &key, nil } +func ReadClipboard() string { + mu.Lock() + defer mu.Unlock() + + return C.GoString(C.XClipboardGet()) +} + +func WriteClipboard(data string) { + mu.Lock() + defer mu.Unlock() + + C.XClipboardSet(C.CString(data)) +} + func Reset() { for key := range debounce { if key < 8 { diff --git a/server/internal/hid/hid.h b/server/internal/hid/hid.h index 55b78dfe..18a0f7ed 100644 --- a/server/internal/hid/hid.h +++ b/server/internal/hid/hid.h @@ -5,6 +5,7 @@ #include #include + #include #include #include #include /* For fputs() */ @@ -16,20 +17,16 @@ * * Note that this is almost certainly not thread safe. */ Display *getXDisplay(void); + clipboard_c *getClipboard(void); + void XClipboardSet(char *src); + char *XClipboardGet(); 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 closeXDisplay(void); - #ifdef __cplusplus - extern "C" - { - #endif - void setXDisplay(char *input); - #ifdef __cplusplus - } - #endif + void setXDisplay(char *input); #endif diff --git a/server/internal/websocket/control.go b/server/internal/websocket/control.go index 2a7f91c3..0b567eaf 100644 --- a/server/internal/websocket/control.go +++ b/server/internal/websocket/control.go @@ -1,7 +1,7 @@ package websocket import ( - "n.eko.moe/neko/internal/clip" + "n.eko.moe/neko/internal/hid" "n.eko.moe/neko/internal/types" "n.eko.moe/neko/internal/types/event" "n.eko.moe/neko/internal/types/message" @@ -113,6 +113,6 @@ func (h *MessageHandler) controlClipboard(id string, session types.Session, payl return nil } - clip.Write(payload.Text) + hid.WriteClipboard(payload.Text) return nil } diff --git a/server/internal/websocket/websocket.go b/server/internal/websocket/websocket.go index 5ba84ac6..b22bc9c4 100644 --- a/server/internal/websocket/websocket.go +++ b/server/internal/websocket/websocket.go @@ -9,7 +9,7 @@ import ( "github.com/rs/zerolog" "github.com/rs/zerolog/log" - "n.eko.moe/neko/internal/clip" + "n.eko.moe/neko/internal/hid" "n.eko.moe/neko/internal/types" "n.eko.moe/neko/internal/types/config" "n.eko.moe/neko/internal/types/event" @@ -81,7 +81,7 @@ func (ws *WebSocketHandler) Start() error { ws.logger.Info().Msg("shutdown") }() - current := clip.Read() + current := hid.ReadClipboard() for { select { @@ -89,7 +89,7 @@ func (ws *WebSocketHandler) Start() error { return default: if ws.sessions.HasHost() { - text := clip.Read() + text := hid.ReadClipboard() if text != current { session, ok := ws.sessions.GetHost() if ok {