From 83be51ea0fc0f238666ed4fcfaafd2737d1e236b Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Mon, 30 Nov 2020 17:45:53 +0100 Subject: [PATCH] update keyboard WS. --- internal/types/event/events.go | 5 ++- internal/types/message/messages.go | 17 ++------- internal/websocket/handler/control.go | 42 ---------------------- internal/websocket/handler/handler.go | 17 ++++++--- internal/websocket/handler/keyboard.go | 48 ++++++++++++++++++++++++++ 5 files changed, 65 insertions(+), 64 deletions(-) create mode 100644 internal/websocket/handler/keyboard.go diff --git a/internal/types/event/events.go b/internal/types/event/events.go index 0ba2b473..cfb5510f 100644 --- a/internal/types/event/events.go +++ b/internal/types/event/events.go @@ -22,7 +22,6 @@ const ( CONTROL_LOCKED = "control/locked" // TODO: Remove. CONTROL_REQUESTING = "control/requesting" // TODO: Remove. CONTROL_GIVE = "control/give" // TODO: Remove. - CONTROL_KEYBOARD = "control/keyboard" // TODO: Remove. CONTROL_HOST = "control/host" // TODO: New. CONTROL_RELEASE = "control/release" CONTROL_REQUEST = "control/request" @@ -46,8 +45,8 @@ const ( ) const ( - KEYBOARD_MODIFIERS = "keyboard/modifiers" // TODO: New. - KEYBOARD_LAYOUT = "keyboard/layout" // TODO: New. + KEYBOARD_MODIFIERS = "keyboard/modifiers" + KEYBOARD_LAYOUT = "keyboard/layout" ) const ( diff --git a/internal/types/message/messages.go b/internal/types/message/messages.go index 4158b336..55f82828 100644 --- a/internal/types/message/messages.go +++ b/internal/types/message/messages.go @@ -99,15 +99,13 @@ type ClipboardData struct { } // Keyboard -// TODO: New. type KeyboardModifiers struct { Event string `json:"event,omitempty"` - CapsLock bool `json:"caps_lock"` - NumLock bool `json:"num_lock"` - ScrollLock bool `json:"scroll_lock"` + CapsLock *bool `json:"caps_lock"` + NumLock *bool `json:"num_lock"` + ScrollLock *bool `json:"scroll_lock"` } -// TODO: New. type KeyboardLayout struct { Event string `json:"event,omitempty"` Layout string `json:"layout"` @@ -152,15 +150,6 @@ type MemberDisconnected struct { ID string `json:"id"` } -// TODO: Remove. -type Keyboard struct { - Event string `json:"event"` - Layout *string `json:"layout,omitempty"` - CapsLock *bool `json:"capsLock,omitempty"` - NumLock *bool `json:"numLock,omitempty"` - ScrollLock *bool `json:"scrollLock,omitempty"` -} - // TODO: Remove. type Control struct { Event string `json:"event"` diff --git a/internal/websocket/handler/control.go b/internal/websocket/handler/control.go index 1051dae5..c609c48b 100644 --- a/internal/websocket/handler/control.go +++ b/internal/websocket/handler/control.go @@ -81,45 +81,3 @@ func (h *MessageHandlerCtx) controlGive(session types.Session, payload *message. return nil } - -func (h *MessageHandlerCtx) controlKeyboard(session types.Session, payload *message.Keyboard) error { - if !session.IsHost() { - h.logger.Debug().Str("id", session.ID()).Msg("is not the host") - return nil - } - - // change layout - if payload.Layout != nil { - 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") - - h.desktop.SetKeyboardModifiers(NumLock, CapsLock, ScrollLock) - return nil -} diff --git a/internal/websocket/handler/handler.go b/internal/websocket/handler/handler.go index b3068461..a78f19ee 100644 --- a/internal/websocket/handler/handler.go +++ b/internal/websocket/handler/handler.go @@ -63,11 +63,6 @@ func (h *MessageHandlerCtx) Message(session types.Session, raw []byte) error { err = utils.Unmarshal(payload, raw, func() error { return h.controlGive(session, payload) }) - case event.CONTROL_KEYBOARD: - payload := &message.Keyboard{} - err = utils.Unmarshal(payload, raw, func() error { - return h.controlKeyboard(session, payload) - }) // Clipboard Events case event.CLIPBOARD_SET: @@ -76,6 +71,18 @@ func (h *MessageHandlerCtx) Message(session types.Session, raw []byte) error { return h.clipboardSet(session, payload) }) + // Keyboard Events + case event.KEYBOARD_MODIFIERS: + payload := &message.KeyboardLayout{} + err = utils.Unmarshal(payload, raw, func() error { + return h.keyboardLayout(session, payload) + }) + case event.KEYBOARD_LAYOUT: + payload := &message.KeyboardModifiers{} + err = utils.Unmarshal(payload, raw, func() error { + return h.keyboardModifiers(session, payload) + }) + // Screen Events case event.SCREEN_RESOLUTION: err = h.screenSize(session) diff --git a/internal/websocket/handler/keyboard.go b/internal/websocket/handler/keyboard.go new file mode 100644 index 00000000..4776eef6 --- /dev/null +++ b/internal/websocket/handler/keyboard.go @@ -0,0 +1,48 @@ +package handler + +import ( + "demodesk/neko/internal/types" + "demodesk/neko/internal/types/message" +) + + +func (h *MessageHandlerCtx) keyboardLayout(session types.Session, payload *message.KeyboardLayout) error { + if !session.IsHost() { + h.logger.Debug().Str("id", session.ID()).Msg("is not the host") + return nil + } + + h.desktop.SetKeyboardLayout(payload.Layout) + return nil +} + +func (h *MessageHandlerCtx) keyboardModifiers(session types.Session, payload *message.KeyboardModifiers) error { + if !session.IsHost() { + h.logger.Debug().Str("id", session.ID()).Msg("is not the host") + return nil + } + + 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.desktop.SetKeyboardModifiers(NumLock, CapsLock, ScrollLock) + return nil +}