extract clipboard from xorg.

This commit is contained in:
Miroslav Šedivý 2022-09-12 21:33:08 +02:00
parent 2be071d215
commit e3e3cf9d22
7 changed files with 70 additions and 47 deletions

View File

@ -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);
}

View File

@ -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)
}

View File

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

View File

@ -6,9 +6,10 @@ import (
"time" "time"
"m1k1o/neko/internal/gst" "m1k1o/neko/internal/gst"
"m1k1o/neko/internal/remote/clipboard"
"m1k1o/neko/internal/remote/xorg"
"m1k1o/neko/internal/types" "m1k1o/neko/internal/types"
"m1k1o/neko/internal/types/config" "m1k1o/neko/internal/types/config"
"m1k1o/neko/internal/xorg"
"github.com/kataras/go-events" "github.com/kataras/go-events"
"github.com/rs/zerolog" "github.com/rs/zerolog"
@ -233,11 +234,11 @@ func (manager *RemoteManager) KeyUp(code uint64) error {
} }
func (manager *RemoteManager) ReadClipboard() string { func (manager *RemoteManager) ReadClipboard() string {
return xorg.ReadClipboard() return clipboard.Read()
} }
func (manager *RemoteManager) WriteClipboard(data string) { func (manager *RemoteManager) WriteClipboard(data string) {
xorg.WriteClipboard(data) clipboard.Write(data)
} }
func (manager *RemoteManager) ResetKeys() { func (manager *RemoteManager) ResetKeys() {

View File

@ -1,6 +1,5 @@
#include "xorg.h" #include "xorg.h"
static clipboard_c *CLIPBOARD = NULL;
static Display *DISPLAY = NULL; static Display *DISPLAY = NULL;
static char *NAME = ":0.0"; static char *NAME = ":0.0";
static int REGISTERED = 0; static int REGISTERED = 0;
@ -75,13 +74,6 @@ Display *getXDisplay(void) {
return DISPLAY; return DISPLAY;
} }
clipboard_c *getClipboard(void) {
if (CLIPBOARD == NULL) {
CLIPBOARD = clipboard_new(NULL);
}
return CLIPBOARD;
}
void XDisplayClose(void) { void XDisplayClose(void) {
if (DISPLAY != NULL) { if (DISPLAY != NULL) {
XCloseDisplay(DISPLAY); XCloseDisplay(DISPLAY);
@ -208,16 +200,6 @@ void XKey(KeySym key, int down) {
XSync(display, 0); 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() { void XGetScreenConfigurations() {
Display *display = getXDisplay(); Display *display = getXDisplay();
Window root = RootWindow(display, 0); Window root = RootWindow(display, 0);

View File

@ -2,7 +2,7 @@ package xorg
/* /*
#cgo CFLAGS: -I/usr/local/include/ #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" #include "xorg.h"
*/ */
@ -107,26 +107,6 @@ func KeyUp(code uint64) error {
return nil 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() { func ResetKeys() {
for code := range debounce_button { for code := range debounce_button {
_ = ButtonUp(code) _ = ButtonUp(code)

View File

@ -3,7 +3,6 @@
#include <X11/XKBlib.h> #include <X11/XKBlib.h>
#include <X11/extensions/Xrandr.h> #include <X11/extensions/Xrandr.h>
#include <X11/extensions/XTest.h> #include <X11/extensions/XTest.h>
#include <libclipboard.h>
#include <stdlib.h> /* For free() */ #include <stdlib.h> /* For free() */
#include <stdio.h> /* For fputs() */ #include <stdio.h> /* For fputs() */
#include <string.h> /* For strdup() */ #include <string.h> /* For strdup() */
@ -23,16 +22,12 @@ typedef struct xkeys_t {
* *
* Note that this is almost certainly not thread safe. */ * Note that this is almost certainly not thread safe. */
Display *getXDisplay(void); Display *getXDisplay(void);
clipboard_c *getClipboard(void);
void XMove(int x, int y); void XMove(int x, int y);
void XScroll(int x, int y); void XScroll(int x, int y);
void XButton(unsigned int button, int down); void XButton(unsigned int button, int down);
void XKey(unsigned long key, int down); void XKey(unsigned long key, int down);
void XClipboardSet(char *src);
char *XClipboardGet();
void XGetScreenConfigurations(); void XGetScreenConfigurations();
void XSetScreenConfiguration(int index, short rate); void XSetScreenConfiguration(int index, short rate);
int XGetScreenSize(); int XGetScreenSize();