neko/internal/websocket/handler/control.go

136 lines
2.8 KiB
Go
Raw Normal View History

2020-11-02 04:09:48 +13:00
package handler
import (
2020-10-29 07:15:48 +13:00
"demodesk/neko/internal/types"
"demodesk/neko/internal/types/event"
"demodesk/neko/internal/types/message"
)
2020-11-02 04:09:48 +13:00
func (h *MessageHandlerCtx) controlRelease(session types.Session) error {
2020-11-01 11:03:37 +13:00
if !session.IsHost() {
h.logger.Debug().Str("id", session.ID()).Msg("is not the host")
return nil
}
h.logger.Debug().Str("id", session.ID()).Msgf("host called %s", event.CONTROL_RELEASE)
h.sessions.ClearHost()
2020-11-19 08:30:33 +13:00
h.sessions.Broadcast(
message.Control{
Event: event.CONTROL_RELEASE,
ID: session.ID(),
2020-11-17 06:12:25 +13:00
}, nil)
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 {
host := h.sessions.GetHost()
if host == nil {
// set host
h.sessions.SetHost(session)
// let everyone know
2020-11-19 08:30:33 +13:00
h.sessions.Broadcast(
message.Control{
Event: event.CONTROL_LOCKED,
ID: session.ID(),
2020-11-17 06:12:25 +13:00
}, nil)
2020-11-19 08:30:33 +13:00
} else {
// tell session there is a host
if err := session.Send(
message.Control{
Event: event.CONTROL_REQUEST,
ID: host.ID(),
}); err != nil {
return err
}
2020-11-19 08:30:33 +13:00
// tell host session wants to be host
return host.Send(
message.Control{
Event: event.CONTROL_REQUESTING,
ID: session.ID(),
})
}
2020-11-19 08:30:33 +13:00
return nil
}
2020-11-02 04:09:48 +13:00
func (h *MessageHandlerCtx) controlGive(session types.Session, payload *message.Control) error {
2020-11-01 11:03:37 +13:00
if !session.IsHost() {
h.logger.Debug().Str("id", session.ID()).Msg("is not the host")
return nil
}
target, ok := h.sessions.Get(payload.ID)
if !ok {
2020-11-02 06:12:16 +13:00
h.logger.Debug().Str("id", payload.ID).Msg("can't find target session")
return nil
}
h.sessions.SetHost(target)
2020-11-19 08:30:33 +13:00
h.sessions.Broadcast(
message.ControlTarget{
Event: event.CONTROL_GIVE,
ID: session.ID(),
Target: target.ID(),
2020-11-17 06:12:25 +13:00
}, nil)
2020-11-19 08:30:33 +13:00
return nil
}
2020-11-02 04:09:48 +13:00
func (h *MessageHandlerCtx) controlClipboard(session types.Session, payload *message.Clipboard) error {
2020-11-01 11:03:37 +13:00
if !session.IsHost() {
h.logger.Debug().Str("id", session.ID()).Msg("is not the host")
return nil
}
2020-11-02 04:09:48 +13:00
h.desktop.WriteClipboard(payload.Text)
return nil
}
2020-11-02 04:09:48 +13:00
func (h *MessageHandlerCtx) controlKeyboard(session types.Session, payload *message.Keyboard) error {
2020-11-01 11:03:37 +13:00
if !session.IsHost() {
h.logger.Debug().Str("id", session.ID()).Msg("is not the host")
return nil
}
// change layout
if payload.Layout != nil {
2020-11-02 04:09:48 +13:00
h.desktop.SetKeyboardLayout(*payload.Layout)
}
var NumLock = 0
if payload.NumLock == nil {
NumLock = -1
} else if *payload.NumLock {
NumLock = 1
}
var CapsLock = 0
if payload.CapsLock == nil {
CapsLock = -1
} else if *payload.CapsLock {
CapsLock = 1
}
var ScrollLock = 0
if payload.ScrollLock == nil {
ScrollLock = -1
} else if *payload.ScrollLock {
ScrollLock = 1
}
h.logger.Debug().
Int("NumLock", NumLock).
Int("CapsLock", CapsLock).
Int("ScrollLock", ScrollLock).
Msg("setting keyboard modifiers")
2020-11-02 04:09:48 +13:00
h.desktop.SetKeyboardModifiers(NumLock, CapsLock, ScrollLock)
return nil
}