From 7d5ec0190a520b226883affefce513f6e5ad252a Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Sat, 23 Jul 2022 00:22:26 +0200 Subject: [PATCH] add button events to control WebSocket. --- internal/desktop/xorg.go | 7 +++++++ internal/websocket/handler/control.go | 24 ++++++++++++++++++++++++ internal/websocket/handler/handler.go | 16 ++++++++++++++++ pkg/types/desktop.go | 1 + pkg/types/event/events.go | 7 +++++-- pkg/types/message/messages.go | 4 ++++ 6 files changed, 57 insertions(+), 2 deletions(-) diff --git a/internal/desktop/xorg.go b/internal/desktop/xorg.go index 8912d13b..a990b144 100644 --- a/internal/desktop/xorg.go +++ b/internal/desktop/xorg.go @@ -38,6 +38,13 @@ func (manager *DesktopManagerCtx) KeyUp(code uint32) error { return xorg.KeyUp(code) } +func (manager *DesktopManagerCtx) ButtonPress(code uint32) error { + xorg.ResetKeys() + defer xorg.ResetKeys() + + return xorg.ButtonDown(code) +} + func (manager *DesktopManagerCtx) KeyPress(codes ...uint32) error { xorg.ResetKeys() defer xorg.ResetKeys() diff --git a/internal/websocket/handler/control.go b/internal/websocket/handler/control.go index 9c3f8dfc..729d962b 100644 --- a/internal/websocket/handler/control.go +++ b/internal/websocket/handler/control.go @@ -79,6 +79,30 @@ func (h *MessageHandlerCtx) controlScroll(session types.Session, payload *messag return nil } +func (h *MessageHandlerCtx) controlButtonPress(session types.Session, payload *message.ControlButton) error { + if err := h.controlRequest(session); err != nil && !errors.Is(err, ErrIsAlreadyTheHost) { + return err + } + + return h.desktop.ButtonPress(payload.Code) +} + +func (h *MessageHandlerCtx) controlButtonDown(session types.Session, payload *message.ControlButton) error { + if err := h.controlRequest(session); err != nil && !errors.Is(err, ErrIsAlreadyTheHost) { + return err + } + + return h.desktop.ButtonDown(payload.Code) +} + +func (h *MessageHandlerCtx) controlButtonUp(session types.Session, payload *message.ControlButton) error { + if err := h.controlRequest(session); err != nil && !errors.Is(err, ErrIsAlreadyTheHost) { + return err + } + + return h.desktop.ButtonUp(payload.Code) +} + func (h *MessageHandlerCtx) controlKeyPress(session types.Session, payload *message.ControlKey) error { if err := h.controlRequest(session); err != nil && !errors.Is(err, ErrIsAlreadyTheHost) { return err diff --git a/internal/websocket/handler/handler.go b/internal/websocket/handler/handler.go index d8bb781e..c128c064 100644 --- a/internal/websocket/handler/handler.go +++ b/internal/websocket/handler/handler.go @@ -87,6 +87,21 @@ func (h *MessageHandlerCtx) Message(session types.Session, data types.WebSocketM err = utils.Unmarshal(payload, data.Payload, func() error { return h.controlScroll(session, payload) }) + case event.CONTROL_BUTTONPRESS: + payload := &message.ControlButton{} + err = utils.Unmarshal(payload, data.Payload, func() error { + return h.controlButtonPress(session, payload) + }) + case event.CONTROL_BUTTONDOWN: + payload := &message.ControlButton{} + err = utils.Unmarshal(payload, data.Payload, func() error { + return h.controlButtonDown(session, payload) + }) + case event.CONTROL_BUTTONUP: + payload := &message.ControlButton{} + err = utils.Unmarshal(payload, data.Payload, func() error { + return h.controlButtonUp(session, payload) + }) case event.CONTROL_KEYPRESS: payload := &message.ControlKey{} err = utils.Unmarshal(payload, data.Payload, func() error { @@ -102,6 +117,7 @@ func (h *MessageHandlerCtx) Message(session types.Session, data types.WebSocketM err = utils.Unmarshal(payload, data.Payload, func() error { return h.controlKeyUp(session, payload) }) + // actions case event.CONTROL_CUT: err = h.controlCut(session) case event.CONTROL_COPY: diff --git a/pkg/types/desktop.go b/pkg/types/desktop.go index f55c877a..ed06c0bf 100644 --- a/pkg/types/desktop.go +++ b/pkg/types/desktop.go @@ -54,6 +54,7 @@ type DesktopManager interface { KeyDown(code uint32) error ButtonUp(code uint32) error KeyUp(code uint32) error + ButtonPress(code uint32) error KeyPress(codes ...uint32) error ResetKeys() ScreenConfigurations() map[int]ScreenConfiguration diff --git a/pkg/types/event/events.go b/pkg/types/event/events.go index a72e73f5..3e773a9c 100644 --- a/pkg/types/event/events.go +++ b/pkg/types/event/events.go @@ -32,8 +32,11 @@ const ( CONTROL_RELEASE = "control/release" CONTROL_REQUEST = "control/request" // mouse - CONTROL_MOVE = "control/move" - CONTROL_SCROLL = "control/scroll" + CONTROL_MOVE = "control/move" + CONTROL_SCROLL = "control/scroll" + CONTROL_BUTTONPRESS = "control/buttonpress" + CONTROL_BUTTONDOWN = "control/buttondown" + CONTROL_BUTTONUP = "control/buttonup" // keyboard CONTROL_KEYPRESS = "control/keypress" CONTROL_KEYDOWN = "control/keydown" diff --git a/pkg/types/message/messages.go b/pkg/types/message/messages.go index 9b61c048..0319be32 100644 --- a/pkg/types/message/messages.go +++ b/pkg/types/message/messages.go @@ -106,6 +106,10 @@ type ControlPos struct { Y int `json:"y"` } +type ControlButton struct { + Code uint32 `json:"code"` +} + type ControlKey struct { Keysym uint32 `json:"keysym"` }