xevent to update clipboard.

This commit is contained in:
Miroslav Šedivý 2021-01-11 15:30:53 +01:00
parent 910f0af995
commit a2ca3727fe
6 changed files with 46 additions and 42 deletions

View File

@ -8,6 +8,10 @@ func (manager *DesktopManagerCtx) OnCursorChanged(listener func(serial uint64))
xevent.OnCursorChanged(listener)
}
func (manager *DesktopManagerCtx) OnClipboardUpdated(listener func()) {
xevent.OnClipboardUpdated(listener)
}
func (manager *DesktopManagerCtx) OnEventError(listener func(error_code uint8, message string, request_code uint8, minor_code uint8)) {
xevent.OnEventError(listener)
}

View File

@ -23,7 +23,11 @@ void XEventLoop(char *name) {
return;
}
Atom XA_CLIPBOARD;
XA_CLIPBOARD = XInternAtom(display, "CLIPBOARD", 0);
XFixesSelectSelectionInput(display, root, XA_CLIPBOARD, XFixesSetSelectionOwnerNotifyMask);
XFixesSelectCursorInput(display, root, XFixesDisplayCursorNotifyMask);
XSync(display, 0);
XSetErrorHandler(XEventError);
@ -39,6 +43,15 @@ void XEventLoop(char *name) {
continue;
}
}
// XFixesSelectionNotifyEvent
if (event.type == xfixes_event_base + XFixesSelectionNotify) {
XFixesSelectionNotifyEvent notifyEvent = *((XFixesSelectionNotifyEvent *) &event);
if (notifyEvent.subtype == XFixesSetSelectionOwnerNotify && notifyEvent.selection == XA_CLIPBOARD) {
goXEventClipboardUpdated();
continue;
}
}
}
XCloseDisplay(display);

View File

@ -33,6 +33,12 @@ func OnCursorChanged(listener func(serial uint64)) {
})
}
func OnClipboardUpdated(listener func()) {
emmiter.On("clipboard-updated", func(payload ...interface{}) {
listener()
})
}
func OnEventError(listener func(error_code uint8, message string, request_code uint8, minor_code uint8)) {
emmiter.On("event-error", func(payload ...interface{}) {
listener(payload[0].(uint8), payload[1].(string), payload[2].(uint8), payload[3].(uint8))
@ -44,6 +50,11 @@ func goXEventCursorChanged(event C.XFixesCursorNotifyEvent) {
emmiter.Emit("cursor-changed", uint64(event.cursor_serial))
}
//export goXEventClipboardUpdated
func goXEventClipboardUpdated() {
emmiter.Emit("clipboard-updated")
}
//export goXEventError
func goXEventError(event *C.XErrorEvent, message *C.char) {
emmiter.Emit("event-error", uint8(event.error_code), C.GoString(message), uint8(event.request_code), uint8(event.minor_code))

View File

@ -1,5 +1,6 @@
#pragma once
#include <X11/Xatom.h>
#include <X11/Xlib.h>
#include <X11/extensions/Xfixes.h>
#include <stdint.h>
@ -7,6 +8,7 @@
#include <string.h>
extern void goXEventCursorChanged(XFixesCursorNotifyEvent event);
extern void goXEventClipboardUpdated();
extern void goXEventError(XErrorEvent *event, char *message);
extern int goXEventActive();

View File

@ -44,6 +44,7 @@ type DesktopManager interface {
// xevent
OnCursorChanged(listener func(serial uint64))
OnClipboardUpdated(listener func())
OnEventError(listener func(error_code uint8, message string, request_code uint8, minor_code uint8))
// clipboard

View File

@ -47,7 +47,6 @@ type WebSocketManagerCtx struct {
desktop types.DesktopManager
handler *handler.MessageHandlerCtx
handlers []types.HandlerFunction
shutdown chan bool
}
func (ws *WebSocketManagerCtx) Start() {
@ -118,51 +117,25 @@ func (ws *WebSocketManagerCtx) Start() {
}, nil)
})
go func() {
ws.logger.Info().Msg("clipboard loop started")
defer func() {
ws.logger.Info().Msg("clipboard loop stopped")
}()
current := ws.desktop.ReadClipboard()
for {
select {
case <-ws.shutdown:
return
default:
session := ws.sessions.GetHost()
if session == nil {
break
}
text := ws.desktop.ReadClipboard()
if text == current {
break
}
current = text
if !session.CanAccessClipboard() {
break
}
if err := session.Send(
message.ClipboardData{
Event: event.CLIPBOARD_UPDATED,
Text: text,
}); err != nil {
ws.logger.Warn().Err(err).Msg("could not sync clipboard")
}
}
time.Sleep(100 * time.Millisecond)
ws.desktop.OnClipboardUpdated(func() {
session := ws.sessions.GetHost()
if session == nil || !session.CanAccessClipboard() {
return
}
}()
text := ws.desktop.ReadClipboard()
err := session.Send(message.ClipboardData{
Event: event.CLIPBOARD_UPDATED,
Text: text,
})
if err != nil {
ws.logger.Warn().Err(err).Msg("could not sync clipboard")
}
})
}
func (ws *WebSocketManagerCtx) Shutdown() error {
ws.shutdown <- true
return nil
}