2020-11-02 04:09:48 +13:00
|
|
|
package handler
|
2020-10-23 03:54:50 +13:00
|
|
|
|
|
|
|
import (
|
2020-10-29 07:15:48 +13:00
|
|
|
"demodesk/neko/internal/types"
|
|
|
|
"demodesk/neko/internal/types/event"
|
|
|
|
"demodesk/neko/internal/types/message"
|
2020-10-23 03:54:50 +13:00
|
|
|
)
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func (h *MessageHandlerCtx) controlRelease(session types.Session) error {
|
2020-10-23 03:54:50 +13:00
|
|
|
// check if session is host
|
2020-11-01 11:03:37 +13:00
|
|
|
if !session.IsHost() {
|
2020-11-01 10:43:47 +13:00
|
|
|
h.logger.Debug().Str("id", session.ID()).Msg("is not the host")
|
2020-10-23 03:54:50 +13:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// release host
|
2020-11-01 10:43:47 +13:00
|
|
|
h.logger.Debug().Str("id", session.ID()).Msgf("host called %s", event.CONTROL_RELEASE)
|
2020-10-23 03:54:50 +13:00
|
|
|
h.sessions.ClearHost()
|
|
|
|
|
|
|
|
// tell everyone
|
|
|
|
if err := h.sessions.Broadcast(
|
|
|
|
message.Control{
|
|
|
|
Event: event.CONTROL_RELEASE,
|
2020-11-01 10:43:47 +13:00
|
|
|
ID: session.ID(),
|
2020-10-23 03:54:50 +13:00
|
|
|
}, nil); err != nil {
|
|
|
|
h.logger.Warn().Err(err).Msgf("broadcasting event %s has failed", event.CONTROL_RELEASE)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func (h *MessageHandlerCtx) controlRequest(session types.Session) error {
|
2020-11-01 11:53:19 +13:00
|
|
|
host := h.sessions.GetHost()
|
|
|
|
|
|
|
|
if host == nil {
|
2020-10-23 03:54:50 +13:00
|
|
|
// set host
|
2020-11-01 11:53:19 +13:00
|
|
|
h.sessions.SetHost(session)
|
2020-10-23 03:54:50 +13:00
|
|
|
|
|
|
|
// let everyone know
|
|
|
|
if err := h.sessions.Broadcast(
|
|
|
|
message.Control{
|
|
|
|
Event: event.CONTROL_LOCKED,
|
2020-11-01 10:43:47 +13:00
|
|
|
ID: session.ID(),
|
2020-10-23 03:54:50 +13:00
|
|
|
}, nil); err != nil {
|
|
|
|
h.logger.Warn().Err(err).Msgf("broadcasting event %s has failed", event.CONTROL_LOCKED)
|
|
|
|
return err
|
|
|
|
}
|
2020-11-01 11:53:19 +13:00
|
|
|
} else {
|
2020-10-23 03:54:50 +13:00
|
|
|
// tell session there is a host
|
|
|
|
if err := session.Send(message.Control{
|
|
|
|
Event: event.CONTROL_REQUEST,
|
|
|
|
ID: host.ID(),
|
|
|
|
}); err != nil {
|
2020-11-01 10:43:47 +13:00
|
|
|
h.logger.Warn().Err(err).Str("id", session.ID()).Msgf("sending event %s has failed", event.CONTROL_REQUEST)
|
2020-10-23 03:54:50 +13:00
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
// tell host session wants to be host
|
|
|
|
if err := host.Send(message.Control{
|
|
|
|
Event: event.CONTROL_REQUESTING,
|
2020-11-01 10:43:47 +13:00
|
|
|
ID: session.ID(),
|
2020-10-23 03:54:50 +13:00
|
|
|
}); err != nil {
|
|
|
|
h.logger.Warn().Err(err).Str("id", host.ID()).Msgf("sending event %s has failed", event.CONTROL_REQUESTING)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func (h *MessageHandlerCtx) controlGive(session types.Session, payload *message.Control) error {
|
2020-10-23 03:54:50 +13:00
|
|
|
// check if session is host
|
2020-11-01 11:03:37 +13:00
|
|
|
if !session.IsHost() {
|
2020-11-01 10:43:47 +13:00
|
|
|
h.logger.Debug().Str("id", session.ID()).Msg("is not the host")
|
2020-10-23 03:54:50 +13:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-01 11:53:19 +13:00
|
|
|
target, ok := h.sessions.Get(payload.ID)
|
|
|
|
if !ok {
|
|
|
|
h.logger.Debug().Str("id", target.ID()).Msg("user does not exist")
|
2020-10-23 03:54:50 +13:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// set host
|
2020-11-01 11:53:19 +13:00
|
|
|
h.sessions.SetHost(target)
|
2020-10-23 03:54:50 +13:00
|
|
|
|
|
|
|
// let everyone know
|
|
|
|
if err := h.sessions.Broadcast(
|
|
|
|
message.ControlTarget{
|
|
|
|
Event: event.CONTROL_GIVE,
|
2020-11-01 10:43:47 +13:00
|
|
|
ID: session.ID(),
|
2020-11-01 11:53:19 +13:00
|
|
|
Target: target.ID(),
|
2020-10-23 03:54:50 +13:00
|
|
|
}, nil); err != nil {
|
|
|
|
h.logger.Warn().Err(err).Msgf("broadcasting event %s has failed", event.CONTROL_LOCKED)
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func (h *MessageHandlerCtx) controlClipboard(session types.Session, payload *message.Clipboard) error {
|
2020-10-23 03:54:50 +13:00
|
|
|
// check if session is host
|
2020-11-01 11:03:37 +13:00
|
|
|
if !session.IsHost() {
|
2020-11-01 10:43:47 +13:00
|
|
|
h.logger.Debug().Str("id", session.ID()).Msg("is not the host")
|
2020-10-23 03:54:50 +13:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
h.desktop.WriteClipboard(payload.Text)
|
2020-10-23 03:54:50 +13:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-11-02 04:09:48 +13:00
|
|
|
func (h *MessageHandlerCtx) controlKeyboard(session types.Session, payload *message.Keyboard) error {
|
2020-10-23 03:54:50 +13:00
|
|
|
// check if session is host
|
2020-11-01 11:03:37 +13:00
|
|
|
if !session.IsHost() {
|
2020-11-01 10:43:47 +13:00
|
|
|
h.logger.Debug().Str("id", session.ID()).Msg("is not the host")
|
2020-10-23 03:54:50 +13:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// change layout
|
|
|
|
if payload.Layout != nil {
|
2020-11-02 04:09:48 +13:00
|
|
|
h.desktop.SetKeyboardLayout(*payload.Layout)
|
2020-10-23 03:54:50 +13:00
|
|
|
}
|
|
|
|
|
|
|
|
// set num lock
|
|
|
|
var NumLock = 0
|
|
|
|
if payload.NumLock == nil {
|
|
|
|
NumLock = -1
|
|
|
|
} else if *payload.NumLock {
|
|
|
|
NumLock = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// set caps lock
|
|
|
|
var CapsLock = 0
|
|
|
|
if payload.CapsLock == nil {
|
|
|
|
CapsLock = -1
|
|
|
|
} else if *payload.CapsLock {
|
|
|
|
CapsLock = 1
|
|
|
|
}
|
|
|
|
|
|
|
|
// set scroll lock
|
|
|
|
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)
|
2020-10-23 03:54:50 +13:00
|
|
|
return nil
|
|
|
|
}
|