neko/server/internal/websocket/handler/control.go

229 lines
6.6 KiB
Go
Raw Normal View History

2020-11-02 04:09:48 +13:00
package handler
import (
2021-09-02 09:10:18 +12:00
"errors"
"github.com/demodesk/neko/pkg/types"
"github.com/demodesk/neko/pkg/types/event"
"github.com/demodesk/neko/pkg/types/message"
"github.com/demodesk/neko/pkg/xorg"
)
2022-01-31 05:52:50 +13:00
var (
ErrIsNotAllowedToHost = errors.New("is not allowed to host")
ErrIsNotTheHost = errors.New("is not the host")
ErrIsAlreadyTheHost = errors.New("is already the host")
ErrIsAlreadyHosted = errors.New("is already hosted")
)
2021-08-30 04:23:58 +12:00
2022-01-31 05:52:50 +13:00
func (h *MessageHandlerCtx) controlRelease(session types.Session) error {
2022-03-27 11:20:38 +13:00
if !session.Profile().CanHost || session.PrivateModeEnabled() {
2022-01-31 05:52:50 +13:00
return ErrIsNotAllowedToHost
2020-12-07 06:50:41 +13:00
}
2020-11-01 11:03:37 +13:00
if !session.IsHost() {
2022-01-31 05:52:50 +13:00
return ErrIsNotTheHost
}
2020-12-02 22:46:00 +13:00
h.desktop.ResetKeys()
2024-05-07 09:47:13 +12:00
session.ClearHost()
2020-12-02 22:46:00 +13:00
2020-11-19 08:30:33 +13:00
return nil
}
2020-11-02 04:09:48 +13:00
func (h *MessageHandlerCtx) controlRequest(session types.Session) error {
2022-03-27 11:20:38 +13:00
if !session.Profile().CanHost || session.PrivateModeEnabled() {
2022-01-31 05:52:50 +13:00
return ErrIsNotAllowedToHost
2020-12-07 06:50:41 +13:00
}
2020-12-02 22:47:20 +13:00
if session.IsHost() {
2022-01-31 05:52:50 +13:00
return ErrIsAlreadyTheHost
2020-12-02 22:47:20 +13:00
}
2023-05-15 08:41:32 +12:00
if h.sessions.Settings().LockedControls && !session.Profile().IsAdmin {
return ErrIsNotAllowedToHost
}
2024-05-10 22:41:51 +12:00
// if implicit hosting is enabled, set session as host without asking
if h.sessions.Settings().ImplicitHosting {
session.SetAsHost()
return nil
}
2024-05-10 22:41:51 +12:00
// if there is no host, set session as host
host, hasHost := h.sessions.GetHost()
if !hasHost {
session.SetAsHost()
return nil
}
2022-01-31 05:52:50 +13:00
2024-05-10 22:41:51 +12:00
// TODO: Some throttling mechanism to prevent spamming.
// let host know that someone wants to take control
host.Send(
event.CONTROL_REQUEST,
message.SessionID{
ID: session.ID(),
})
return ErrIsAlreadyHosted
}
2022-01-30 13:25:04 +13:00
2022-07-22 04:28:32 +12:00
func (h *MessageHandlerCtx) controlMove(session types.Session, payload *message.ControlPos) error {
if err := h.controlRequest(session); err != nil && !errors.Is(err, ErrIsAlreadyTheHost) {
return err
}
2022-07-22 04:28:51 +12:00
// handle active cursor movement
2022-07-22 04:28:32 +12:00
h.desktop.Move(payload.X, payload.Y)
2022-07-22 04:28:51 +12:00
h.webrtc.SetCursorPosition(payload.X, payload.Y)
2022-07-22 04:28:32 +12:00
return nil
}
func (h *MessageHandlerCtx) controlScroll(session types.Session, payload *message.ControlScroll) error {
2022-07-22 04:28:32 +12:00
if err := h.controlRequest(session); err != nil && !errors.Is(err, ErrIsAlreadyTheHost) {
return err
}
// TOOD: remove this once the client is fixed
if payload.DeltaX == 0 && payload.DeltaY == 0 {
payload.DeltaX = payload.X
payload.DeltaY = payload.Y
}
h.desktop.Scroll(payload.DeltaX, payload.DeltaY, payload.ControlKey)
2022-07-22 04:28:32 +12:00
return nil
}
func (h *MessageHandlerCtx) controlButtonPress(session types.Session, payload *message.ControlButton) error {
if payload.ControlPos != nil {
if err := h.controlMove(session, payload.ControlPos); err != nil {
return err
}
} else 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 payload.ControlPos != nil {
if err := h.controlMove(session, payload.ControlPos); err != nil {
return err
}
} else 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 payload.ControlPos != nil {
if err := h.controlMove(session, payload.ControlPos); err != nil {
return err
}
} else if err := h.controlRequest(session); err != nil && !errors.Is(err, ErrIsAlreadyTheHost) {
return err
}
return h.desktop.ButtonUp(payload.Code)
}
2022-01-30 13:25:04 +13:00
func (h *MessageHandlerCtx) controlKeyPress(session types.Session, payload *message.ControlKey) error {
if payload.ControlPos != nil {
if err := h.controlMove(session, payload.ControlPos); err != nil {
return err
}
} else if err := h.controlRequest(session); err != nil && !errors.Is(err, ErrIsAlreadyTheHost) {
2022-01-31 05:52:50 +13:00
return err
2022-01-30 13:25:04 +13:00
}
return h.desktop.KeyPress(payload.Keysym)
}
func (h *MessageHandlerCtx) controlKeyDown(session types.Session, payload *message.ControlKey) error {
if payload.ControlPos != nil {
if err := h.controlMove(session, payload.ControlPos); err != nil {
return err
}
} else if err := h.controlRequest(session); err != nil && !errors.Is(err, ErrIsAlreadyTheHost) {
2022-01-31 05:52:50 +13:00
return err
2022-01-30 13:25:04 +13:00
}
return h.desktop.KeyDown(payload.Keysym)
}
func (h *MessageHandlerCtx) controlKeyUp(session types.Session, payload *message.ControlKey) error {
if payload.ControlPos != nil {
if err := h.controlMove(session, payload.ControlPos); err != nil {
return err
}
} else if err := h.controlRequest(session); err != nil && !errors.Is(err, ErrIsAlreadyTheHost) {
2022-01-31 05:52:50 +13:00
return err
2022-01-30 13:25:04 +13:00
}
return h.desktop.KeyUp(payload.Keysym)
}
func (h *MessageHandlerCtx) controlTouchBegin(session types.Session, payload *message.ControlTouch) error {
if err := h.controlRequest(session); err != nil && !errors.Is(err, ErrIsAlreadyTheHost) {
return err
}
return h.desktop.TouchBegin(payload.TouchId, payload.X, payload.Y, payload.Pressure)
}
func (h *MessageHandlerCtx) controlTouchUpdate(session types.Session, payload *message.ControlTouch) error {
if err := h.controlRequest(session); err != nil && !errors.Is(err, ErrIsAlreadyTheHost) {
return err
}
return h.desktop.TouchUpdate(payload.TouchId, payload.X, payload.Y, payload.Pressure)
}
func (h *MessageHandlerCtx) controlTouchEnd(session types.Session, payload *message.ControlTouch) error {
if err := h.controlRequest(session); err != nil && !errors.Is(err, ErrIsAlreadyTheHost) {
return err
}
return h.desktop.TouchEnd(payload.TouchId, payload.X, payload.Y, payload.Pressure)
}
2022-01-31 05:53:22 +13:00
func (h *MessageHandlerCtx) controlCut(session types.Session) error {
if err := h.controlRequest(session); err != nil && !errors.Is(err, ErrIsAlreadyTheHost) {
return err
}
return h.desktop.KeyPress(xorg.XK_Control_L, xorg.XK_x)
}
2022-01-30 13:25:04 +13:00
func (h *MessageHandlerCtx) controlCopy(session types.Session) error {
2022-01-31 05:52:50 +13:00
if err := h.controlRequest(session); err != nil && !errors.Is(err, ErrIsAlreadyTheHost) {
return err
2022-01-30 13:25:04 +13:00
}
return h.desktop.KeyPress(xorg.XK_Control_L, xorg.XK_c)
}
2022-05-04 07:57:31 +12:00
func (h *MessageHandlerCtx) controlPaste(session types.Session, payload *message.ClipboardData) error {
2022-01-31 05:52:50 +13:00
if err := h.controlRequest(session); err != nil && !errors.Is(err, ErrIsAlreadyTheHost) {
return err
2022-01-30 13:25:04 +13:00
}
2022-05-04 07:57:31 +12:00
// if there have been set clipboard data, set them first
if payload != nil && payload.Text != "" {
if err := h.clipboardSet(session, payload); err != nil {
return err
}
}
2022-01-30 13:25:04 +13:00
return h.desktop.KeyPress(xorg.XK_Control_L, xorg.XK_v)
}
func (h *MessageHandlerCtx) controlSelectAll(session types.Session) error {
2022-01-31 05:52:50 +13:00
if err := h.controlRequest(session); err != nil && !errors.Is(err, ErrIsAlreadyTheHost) {
return err
2022-01-30 13:25:04 +13:00
}
return h.desktop.KeyPress(xorg.XK_Control_L, xorg.XK_a)
}