From 81531d74c14e795e1340facb79917c0a515a679b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Thu, 21 Jul 2022 18:28:32 +0200 Subject: [PATCH] websocket move cursor & scroll. --- internal/websocket/handler/control.go | 18 ++++++++++++++++++ internal/websocket/handler/handler.go | 10 ++++++++++ pkg/types/event/events.go | 4 ++-- pkg/types/message/messages.go | 13 +++---------- 4 files changed, 33 insertions(+), 12 deletions(-) diff --git a/internal/websocket/handler/control.go b/internal/websocket/handler/control.go index 0b3ff7a1..2533dab2 100644 --- a/internal/websocket/handler/control.go +++ b/internal/websocket/handler/control.go @@ -59,6 +59,24 @@ func (h *MessageHandlerCtx) controlRequest(session types.Session) error { return nil } +func (h *MessageHandlerCtx) controlMove(session types.Session, payload *message.ControlPos) error { + if err := h.controlRequest(session); err != nil && !errors.Is(err, ErrIsAlreadyTheHost) { + return err + } + + h.desktop.Move(payload.X, payload.Y) + return nil +} + +func (h *MessageHandlerCtx) controlScroll(session types.Session, payload *message.ControlPos) error { + if err := h.controlRequest(session); err != nil && !errors.Is(err, ErrIsAlreadyTheHost) { + return err + } + + h.desktop.Scroll(payload.X, payload.Y) + return nil +} + 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 04b3f9dd..d8bb781e 100644 --- a/internal/websocket/handler/handler.go +++ b/internal/websocket/handler/handler.go @@ -77,6 +77,16 @@ func (h *MessageHandlerCtx) Message(session types.Session, data types.WebSocketM err = h.controlRelease(session) case event.CONTROL_REQUEST: err = h.controlRequest(session) + case event.CONTROL_MOVE: + payload := &message.ControlPos{} + err = utils.Unmarshal(payload, data.Payload, func() error { + return h.controlMove(session, payload) + }) + case event.CONTROL_SCROLL: + payload := &message.ControlPos{} + err = utils.Unmarshal(payload, data.Payload, func() error { + return h.controlScroll(session, payload) + }) case event.CONTROL_KEYPRESS: payload := &message.ControlKey{} err = utils.Unmarshal(payload, data.Payload, func() error { diff --git a/pkg/types/event/events.go b/pkg/types/event/events.go index 60973e40..a72e73f5 100644 --- a/pkg/types/event/events.go +++ b/pkg/types/event/events.go @@ -32,8 +32,8 @@ const ( CONTROL_RELEASE = "control/release" CONTROL_REQUEST = "control/request" // mouse - CONTROL_MOVE = "control/move" // TODO: New. (fallback) - CONTROL_SCROLL = "control/scroll" // TODO: New. (fallback) + CONTROL_MOVE = "control/move" + CONTROL_SCROLL = "control/scroll" // keyboard CONTROL_KEYPRESS = "control/keypress" CONTROL_KEYDOWN = "control/keydown" diff --git a/pkg/types/message/messages.go b/pkg/types/message/messages.go index cb8470a5..9b61c048 100644 --- a/pkg/types/message/messages.go +++ b/pkg/types/message/messages.go @@ -101,16 +101,9 @@ type ControlHost struct { HostID string `json:"host_id,omitempty"` } -// TODO: New. -type ControlMove struct { - X uint16 `json:"x"` - Y uint16 `json:"y"` -} - -// TODO: New. -type ControlScroll struct { - X int16 `json:"x"` - Y int16 `json:"y"` +type ControlPos struct { + X int `json:"x"` + Y int `json:"y"` } type ControlKey struct {