muxed interactions with xserver, fix for #28?

This commit is contained in:
Craig 2020-02-03 14:49:27 +00:00
parent 3fc7737442
commit 2680a1f702
8 changed files with 71 additions and 63 deletions

View File

@ -1,23 +0,0 @@
#include "clip.h"
#include <libclipboard.h>
#include <string.h>
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);
}

View File

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

View File

@ -1,5 +0,0 @@
#include <libclipboard.h>
clipboard_c *getClipboard(void);
void set_clipboard(char *src);
char * get_clipboard();

View File

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

View File

@ -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 {

View File

@ -5,6 +5,7 @@
#include <X11/Xlib.h>
#include <X11/extensions/XTest.h>
#include <libclipboard.h>
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h> /* 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
#endif

View File

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

View File

@ -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 {