control actions with requests.

This commit is contained in:
Miroslav Šedivý 2022-01-30 17:52:50 +01:00
parent f6eb8d0315
commit e8c7ab421a

View File

@ -9,17 +9,20 @@ import (
"demodesk/neko/internal/types/message" "demodesk/neko/internal/types/message"
) )
func (h *MessageHandlerCtx) controlRelease(session types.Session) error { var (
logger := h.logger.With().Str("session_id", session.ID()).Logger() 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")
)
func (h *MessageHandlerCtx) controlRelease(session types.Session) error {
if !session.Profile().CanHost { if !session.Profile().CanHost {
logger.Debug().Msg("is not allowed to host") return ErrIsNotAllowedToHost
return nil
} }
if !session.IsHost() { if !session.IsHost() {
logger.Debug().Msg("is not the host") return ErrIsNotTheHost
return nil
} }
h.desktop.ResetKeys() h.desktop.ResetKeys()
@ -30,11 +33,11 @@ func (h *MessageHandlerCtx) controlRelease(session types.Session) error {
func (h *MessageHandlerCtx) controlRequest(session types.Session) error { func (h *MessageHandlerCtx) controlRequest(session types.Session) error {
if !session.Profile().CanHost { if !session.Profile().CanHost {
return errors.New("is not allowed to host") return ErrIsNotAllowedToHost
} }
if session.IsHost() { if session.IsHost() {
return errors.New("is already the host") return ErrIsAlreadyTheHost
} }
if !h.sessions.ImplicitHosting() { if !h.sessions.ImplicitHosting() {
@ -47,75 +50,58 @@ func (h *MessageHandlerCtx) controlRequest(session types.Session) error {
HostID: host.ID(), HostID: host.ID(),
}) })
return nil return ErrIsAlreadyHosted
} }
} }
h.sessions.SetHost(session) h.sessions.SetHost(session)
return nil return nil
} }
func (h *MessageHandlerCtx) controlKeyPress(session types.Session, payload *message.ControlKey) error { func (h *MessageHandlerCtx) controlKeyPress(session types.Session, payload *message.ControlKey) error {
logger := h.logger.With().Str("session_id", session.ID()).Logger() if err := h.controlRequest(session); err != nil && !errors.Is(err, ErrIsAlreadyTheHost) {
return err
if !session.IsHost() {
logger.Debug().Msg("is not the host")
return nil
} }
return h.desktop.KeyPress(payload.Keysym) return h.desktop.KeyPress(payload.Keysym)
} }
func (h *MessageHandlerCtx) controlKeyDown(session types.Session, payload *message.ControlKey) error { func (h *MessageHandlerCtx) controlKeyDown(session types.Session, payload *message.ControlKey) error {
logger := h.logger.With().Str("session_id", session.ID()).Logger() if err := h.controlRequest(session); err != nil && !errors.Is(err, ErrIsAlreadyTheHost) {
return err
if !session.IsHost() {
logger.Debug().Msg("is not the host")
return nil
} }
return h.desktop.KeyDown(payload.Keysym) return h.desktop.KeyDown(payload.Keysym)
} }
func (h *MessageHandlerCtx) controlKeyUp(session types.Session, payload *message.ControlKey) error { func (h *MessageHandlerCtx) controlKeyUp(session types.Session, payload *message.ControlKey) error {
logger := h.logger.With().Str("session_id", session.ID()).Logger() if err := h.controlRequest(session); err != nil && !errors.Is(err, ErrIsAlreadyTheHost) {
return err
if !session.IsHost() {
logger.Debug().Msg("is not the host")
return nil
} }
return h.desktop.KeyUp(payload.Keysym) return h.desktop.KeyUp(payload.Keysym)
} }
func (h *MessageHandlerCtx) controlCopy(session types.Session) error { func (h *MessageHandlerCtx) controlCopy(session types.Session) error {
logger := h.logger.With().Str("session_id", session.ID()).Logger() if err := h.controlRequest(session); err != nil && !errors.Is(err, ErrIsAlreadyTheHost) {
return err
if !session.IsHost() {
logger.Debug().Msg("is not the host")
return nil
} }
return h.desktop.KeyPress(xorg.XK_Control_L, xorg.XK_c) return h.desktop.KeyPress(xorg.XK_Control_L, xorg.XK_c)
} }
func (h *MessageHandlerCtx) controlPaste(session types.Session) error { func (h *MessageHandlerCtx) controlPaste(session types.Session) error {
logger := h.logger.With().Str("session_id", session.ID()).Logger() if err := h.controlRequest(session); err != nil && !errors.Is(err, ErrIsAlreadyTheHost) {
return err
if !session.IsHost() {
logger.Debug().Msg("is not the host")
return nil
} }
return h.desktop.KeyPress(xorg.XK_Control_L, xorg.XK_v) return h.desktop.KeyPress(xorg.XK_Control_L, xorg.XK_v)
} }
func (h *MessageHandlerCtx) controlSelectAll(session types.Session) error { func (h *MessageHandlerCtx) controlSelectAll(session types.Session) error {
logger := h.logger.With().Str("session_id", session.ID()).Logger() if err := h.controlRequest(session); err != nil && !errors.Is(err, ErrIsAlreadyTheHost) {
return err
if !session.IsHost() {
logger.Debug().Msg("is not the host")
return nil
} }
return h.desktop.KeyPress(xorg.XK_Control_L, xorg.XK_a) return h.desktop.KeyPress(xorg.XK_Control_L, xorg.XK_a)