From 910f0af995a752e9596ef8a9fd537af1e54a73d3 Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Sun, 10 Jan 2021 15:58:17 +0100 Subject: [PATCH] xevent cursor change. --- internal/desktop/manager.go | 3 +++ internal/desktop/xevent.go | 13 +++++++++++++ internal/types/desktop.go | 4 ++++ internal/websocket/manager.go | 35 +++++++++++++++++++---------------- 4 files changed, 39 insertions(+), 16 deletions(-) create mode 100644 internal/desktop/xevent.go diff --git a/internal/desktop/manager.go b/internal/desktop/manager.go index 62e5e3df..96ddac60 100644 --- a/internal/desktop/manager.go +++ b/internal/desktop/manager.go @@ -11,6 +11,7 @@ import ( "demodesk/neko/internal/config" "demodesk/neko/internal/desktop/xorg" + "demodesk/neko/internal/desktop/xevent" ) var mu = sync.Mutex{} @@ -50,6 +51,8 @@ func (manager *DesktopManagerCtx) Start() { manager.logger.Warn().Err(err).Msg("unable to set initial screen size") } + go xevent.EventLoop(manager.display) + go func() { defer func() { xorg.DisplayClose() diff --git a/internal/desktop/xevent.go b/internal/desktop/xevent.go new file mode 100644 index 00000000..1bea6213 --- /dev/null +++ b/internal/desktop/xevent.go @@ -0,0 +1,13 @@ +package desktop + +import ( + "demodesk/neko/internal/desktop/xevent" +) + +func (manager *DesktopManagerCtx) OnCursorChanged(listener func(serial uint64)) { + xevent.OnCursorChanged(listener) +} + +func (manager *DesktopManagerCtx) OnEventError(listener func(error_code uint8, message string, request_code uint8, minor_code uint8)) { + xevent.OnEventError(listener) +} diff --git a/internal/types/desktop.go b/internal/types/desktop.go index 7acee29e..bb694be4 100644 --- a/internal/types/desktop.go +++ b/internal/types/desktop.go @@ -42,6 +42,10 @@ type DesktopManager interface { SetKeyboardModifiers(NumLock int, CapsLock int, ScrollLock int) GetCursorImage() *CursorImage + // xevent + OnCursorChanged(listener func(serial uint64)) + OnEventError(listener func(error_code uint8, message string, request_code uint8, minor_code uint8)) + // clipboard ReadClipboard() string WriteClipboard(data string) diff --git a/internal/websocket/manager.go b/internal/websocket/manager.go index f9c2d0e6..6892a3c9 100644 --- a/internal/websocket/manager.go +++ b/internal/websocket/manager.go @@ -99,6 +99,25 @@ func (ws *WebSocketManagerCtx) Start() { } }) + // TOOD: Throttle events. + ws.desktop.OnCursorChanged(func(serial uint64) { + cur := ws.desktop.GetCursorImage() + uri, err := utils.GetCursorImageURI(cur) + if err != nil { + ws.logger.Warn().Err(err).Msg("could create cursor image") + return + } + + ws.sessions.Broadcast(message.CursorImage{ + Event: event.CURSOR_IMAGE, + Uri: uri, + Width: cur.Width, + Height: cur.Height, + X: cur.Xhot, + Y: cur.Yhot, + }, nil) + }) + go func() { ws.logger.Info().Msg("clipboard loop started") @@ -107,28 +126,12 @@ func (ws *WebSocketManagerCtx) Start() { }() current := ws.desktop.ReadClipboard() - cursor := uint64(0) for { select { case <-ws.shutdown: return default: - cur := ws.desktop.GetCursorImage() - if cursor != cur.Serial || cur.Serial == 0 { - cursor = cur.Serial - - uri, _ := utils.GetCursorImageURI(cur) - ws.sessions.Broadcast(message.CursorImage{ - Event: event.CURSOR_IMAGE, - Uri: uri, - Width: cur.Width, - Height: cur.Height, - X: cur.Xhot, - Y: cur.Yhot, - }, nil) - } - session := ws.sessions.GetHost() if session == nil { break