2020-11-01 16:09:48 +01:00
|
|
|
package handler
|
2020-10-22 16:54:50 +02:00
|
|
|
|
|
|
|
import (
|
2021-09-01 23:10:18 +02:00
|
|
|
"errors"
|
|
|
|
|
2022-03-20 11:43:00 +01:00
|
|
|
"gitlab.com/demodesk/neko/server/pkg/types"
|
|
|
|
"gitlab.com/demodesk/neko/server/pkg/types/event"
|
|
|
|
"gitlab.com/demodesk/neko/server/pkg/types/message"
|
|
|
|
"gitlab.com/demodesk/neko/server/pkg/xorg"
|
2020-10-22 16:54:50 +02:00
|
|
|
)
|
|
|
|
|
2022-01-30 17:52:50 +01: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-29 18:23:58 +02:00
|
|
|
|
2022-01-30 17:52:50 +01:00
|
|
|
func (h *MessageHandlerCtx) controlRelease(session types.Session) error {
|
2022-03-26 23:20:38 +01:00
|
|
|
if !session.Profile().CanHost || session.PrivateModeEnabled() {
|
2022-01-30 17:52:50 +01:00
|
|
|
return ErrIsNotAllowedToHost
|
2020-12-06 18:50:41 +01:00
|
|
|
}
|
|
|
|
|
2020-10-31 23:03:37 +01:00
|
|
|
if !session.IsHost() {
|
2022-01-30 17:52:50 +01:00
|
|
|
return ErrIsNotTheHost
|
2020-10-22 16:54:50 +02:00
|
|
|
}
|
|
|
|
|
2020-12-02 10:46:00 +01:00
|
|
|
h.desktop.ResetKeys()
|
2020-10-22 16:54:50 +02:00
|
|
|
h.sessions.ClearHost()
|
2020-12-02 10:46:00 +01:00
|
|
|
|
2020-11-18 20:30:33 +01:00
|
|
|
return nil
|
2020-10-22 16:54:50 +02:00
|
|
|
}
|
|
|
|
|
2020-11-01 16:09:48 +01:00
|
|
|
func (h *MessageHandlerCtx) controlRequest(session types.Session) error {
|
2022-03-26 23:20:38 +01:00
|
|
|
if !session.Profile().CanHost || session.PrivateModeEnabled() {
|
2022-01-30 17:52:50 +01:00
|
|
|
return ErrIsNotAllowedToHost
|
2020-12-06 18:50:41 +01:00
|
|
|
}
|
|
|
|
|
2020-12-02 10:47:20 +01:00
|
|
|
if session.IsHost() {
|
2022-01-30 17:52:50 +01:00
|
|
|
return ErrIsAlreadyTheHost
|
2020-12-02 10:47:20 +01:00
|
|
|
}
|
|
|
|
|
2022-03-27 00:08:06 +01:00
|
|
|
if !h.sessions.Settings().ImplicitHosting {
|
2020-12-02 11:24:20 +01:00
|
|
|
// tell session if there is a host
|
|
|
|
if host := h.sessions.GetHost(); host != nil {
|
2021-09-01 21:58:39 +02:00
|
|
|
session.Send(
|
|
|
|
event.CONTROL_HOST,
|
2020-12-02 11:24:20 +01:00
|
|
|
message.ControlHost{
|
|
|
|
HasHost: true,
|
|
|
|
HostID: host.ID(),
|
|
|
|
})
|
2021-09-01 21:58:39 +02:00
|
|
|
|
2022-01-30 17:52:50 +01:00
|
|
|
return ErrIsAlreadyHosted
|
2020-12-02 11:24:20 +01:00
|
|
|
}
|
2020-10-22 16:54:50 +02:00
|
|
|
}
|
|
|
|
|
2020-11-30 18:24:38 +01:00
|
|
|
h.sessions.SetHost(session)
|
2022-01-30 17:52:50 +01:00
|
|
|
|
2020-11-18 20:30:33 +01:00
|
|
|
return nil
|
2020-10-22 16:54:50 +02:00
|
|
|
}
|
2022-01-30 01:25:04 +01:00
|
|
|
|
|
|
|
func (h *MessageHandlerCtx) controlKeyPress(session types.Session, payload *message.ControlKey) error {
|
2022-01-30 17:52:50 +01:00
|
|
|
if err := h.controlRequest(session); err != nil && !errors.Is(err, ErrIsAlreadyTheHost) {
|
|
|
|
return err
|
2022-01-30 01:25:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return h.desktop.KeyPress(payload.Keysym)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *MessageHandlerCtx) controlKeyDown(session types.Session, payload *message.ControlKey) error {
|
2022-01-30 17:52:50 +01:00
|
|
|
if err := h.controlRequest(session); err != nil && !errors.Is(err, ErrIsAlreadyTheHost) {
|
|
|
|
return err
|
2022-01-30 01:25:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return h.desktop.KeyDown(payload.Keysym)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *MessageHandlerCtx) controlKeyUp(session types.Session, payload *message.ControlKey) error {
|
2022-01-30 17:52:50 +01:00
|
|
|
if err := h.controlRequest(session); err != nil && !errors.Is(err, ErrIsAlreadyTheHost) {
|
|
|
|
return err
|
2022-01-30 01:25:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return h.desktop.KeyUp(payload.Keysym)
|
|
|
|
}
|
|
|
|
|
2022-01-30 17:53:22 +01: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 01:25:04 +01:00
|
|
|
func (h *MessageHandlerCtx) controlCopy(session types.Session) error {
|
2022-01-30 17:52:50 +01:00
|
|
|
if err := h.controlRequest(session); err != nil && !errors.Is(err, ErrIsAlreadyTheHost) {
|
|
|
|
return err
|
2022-01-30 01:25:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return h.desktop.KeyPress(xorg.XK_Control_L, xorg.XK_c)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *MessageHandlerCtx) controlPaste(session types.Session) error {
|
2022-01-30 17:52:50 +01:00
|
|
|
if err := h.controlRequest(session); err != nil && !errors.Is(err, ErrIsAlreadyTheHost) {
|
|
|
|
return err
|
2022-01-30 01:25:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return h.desktop.KeyPress(xorg.XK_Control_L, xorg.XK_v)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *MessageHandlerCtx) controlSelectAll(session types.Session) error {
|
2022-01-30 17:52:50 +01:00
|
|
|
if err := h.controlRequest(session); err != nil && !errors.Is(err, ErrIsAlreadyTheHost) {
|
|
|
|
return err
|
2022-01-30 01:25:04 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return h.desktop.KeyPress(xorg.XK_Control_L, xorg.XK_a)
|
|
|
|
}
|