KeyboardLayout to KeyboardMap.

This commit is contained in:
Miroslav Šedivý 2021-01-15 16:53:03 +01:00
parent 8c47cbae68
commit 0e09609c67
8 changed files with 76 additions and 22 deletions

View File

@ -43,7 +43,8 @@ func (h *RoomHandler) Route(r chi.Router) {
}) })
r.Route("/keyboard", func(r chi.Router) { r.Route("/keyboard", func(r chi.Router) {
r.With(auth.HostsOnly).Post("/layout", h.keyboardLayoutSet) r.Get("/map", h.keyboardMapGet)
r.With(auth.HostsOnly).Post("/map", h.keyboardMapSet)
r.Get("/modifiers", h.keyboardModifiersGet) r.Get("/modifiers", h.keyboardModifiersGet)
r.With(auth.HostsOnly).Post("/modifiers", h.keyboardModifiersSet) r.With(auth.HostsOnly).Post("/modifiers", h.keyboardModifiersSet)

View File

@ -7,7 +7,7 @@ import (
"demodesk/neko/internal/types" "demodesk/neko/internal/types"
) )
type KeyboardLayoutData struct { type KeyboardMapData struct {
Layout string `json:"layout"` Layout string `json:"layout"`
Variant string `json:"variant"` Variant string `json:"variant"`
} }
@ -17,21 +17,39 @@ type KeyboardModifiersData struct {
CapsLock *bool `json:"capslock"` CapsLock *bool `json:"capslock"`
} }
func (h *RoomHandler) keyboardLayoutSet(w http.ResponseWriter, r *http.Request) { func (h *RoomHandler) keyboardMapSet(w http.ResponseWriter, r *http.Request) {
data := &KeyboardLayoutData{} data := &KeyboardMapData{}
if !utils.HttpJsonRequest(w, r, data) { if !utils.HttpJsonRequest(w, r, data) {
return return
} }
err := h.desktop.SetKeyboardLayout(data.Layout, data.Variant) err := h.desktop.SetKeyboardMap(types.KeyboardMap{
Layout: data.Layout,
Variant: data.Variant,
})
if err != nil{ if err != nil{
utils.HttpInternalServerError(w, "Unable to change keyboard layout.") utils.HttpInternalServerError(w, "Unable to change keyboard map.")
return return
} }
utils.HttpSuccess(w) utils.HttpSuccess(w)
} }
func (h *RoomHandler) keyboardMapGet(w http.ResponseWriter, r *http.Request) {
data, err := h.desktop.GetKeyboardMap()
if err != nil{
utils.HttpInternalServerError(w, "Unable to get keyboard map.")
return
}
utils.HttpSuccess(w, KeyboardMapData{
Layout: data.Layout,
Variant: data.Variant,
})
}
func (h *RoomHandler) keyboardModifiersSet(w http.ResponseWriter, r *http.Request) { func (h *RoomHandler) keyboardModifiersSet(w http.ResponseWriter, r *http.Request) {
data := &KeyboardModifiersData{} data := &KeyboardModifiersData{}
if !utils.HttpJsonRequest(w, r, data) { if !utils.HttpJsonRequest(w, r, data) {

View File

@ -1,6 +1,7 @@
package desktop package desktop
import ( import (
"regexp"
"os/exec" "os/exec"
"demodesk/neko/internal/types" "demodesk/neko/internal/types"
@ -55,13 +56,38 @@ func (manager *DesktopManagerCtx) ChangeScreenSize(width int, height int, rate i
return xorg.ChangeScreenSize(width, height, rate) return xorg.ChangeScreenSize(width, height, rate)
} }
func (manager *DesktopManagerCtx) SetKeyboardLayout(layout string, variant string) error { func (manager *DesktopManagerCtx) SetKeyboardMap(kbd types.KeyboardMap) error {
// TOOD: Use native API. // TOOD: Use native API.
cmd := exec.Command("setxkbmap", "-layout", layout, "-variant", variant) cmd := exec.Command("setxkbmap", "-layout", kbd.Layout, "-variant", kbd.Variant)
_, err := cmd.Output() _, err := cmd.Output()
return err return err
} }
func (manager *DesktopManagerCtx) GetKeyboardMap() (*types.KeyboardMap, error) {
// TOOD: Use native API.
cmd := exec.Command("setxkbmap", "-query")
res, err := cmd.Output()
if err != nil {
return nil, err
}
kbd := types.KeyboardMap{}
re := regexp.MustCompile(`layout:\s+(.*)\n`)
arr := re.FindStringSubmatch(string(res))
if len(arr) > 1 {
kbd.Layout = arr[1]
}
re = regexp.MustCompile(`variant:\s+(.*)\n`)
arr = re.FindStringSubmatch(string(res))
if len(arr) > 1 {
kbd.Variant = arr[1]
}
return &kbd, nil
}
func (manager *DesktopManagerCtx) SetKeyboardModifiers(mod types.KeyboardModifiers) { func (manager *DesktopManagerCtx) SetKeyboardModifiers(mod types.KeyboardModifiers) {
if mod.NumLock != nil { if mod.NumLock != nil {
xorg.SetKeyboardModifier(xorg.KBD_NUM_LOCK, *mod.NumLock) xorg.SetKeyboardModifier(xorg.KBD_NUM_LOCK, *mod.NumLock)

View File

@ -26,6 +26,11 @@ type KeyboardModifiers struct {
CapsLock *bool CapsLock *bool
} }
type KeyboardMap struct {
Layout string
Variant string
}
type DesktopManager interface { type DesktopManager interface {
Start() Start()
Shutdown() error Shutdown() error
@ -43,7 +48,8 @@ type DesktopManager interface {
ResetKeys() ResetKeys()
ScreenConfigurations() map[int]ScreenConfiguration ScreenConfigurations() map[int]ScreenConfiguration
GetScreenSize() *ScreenSize GetScreenSize() *ScreenSize
SetKeyboardLayout(layout string, variant string) error SetKeyboardMap(KeyboardMap) error
GetKeyboardMap() (*KeyboardMap, error)
SetKeyboardModifiers(mod KeyboardModifiers) SetKeyboardModifiers(mod KeyboardModifiers)
GetKeyboardModifiers() KeyboardModifiers GetKeyboardModifiers() KeyboardModifiers
GetCursorImage() *CursorImage GetCursorImage() *CursorImage

View File

@ -41,7 +41,7 @@ const (
const ( const (
KEYBOARD_MODIFIERS = "keyboard/modifiers" KEYBOARD_MODIFIERS = "keyboard/modifiers"
KEYBOARD_LAYOUT = "keyboard/layout" KEYBOARD_MAP = "keyboard/map"
) )
const ( const (

View File

@ -132,6 +132,12 @@ type ClipboardData struct {
// Keyboard // Keyboard
///////////////////////////// /////////////////////////////
type KeyboardMap struct {
Event string `json:"event,omitempty"`
Layout string `json:"layout"`
Variant string `json:"variant"`
}
type KeyboardModifiers struct { type KeyboardModifiers struct {
Event string `json:"event,omitempty"` Event string `json:"event,omitempty"`
CapsLock *bool `json:"caps_lock"` CapsLock *bool `json:"caps_lock"`
@ -139,12 +145,6 @@ type KeyboardModifiers struct {
ScrollLock *bool `json:"scroll_lock"` ScrollLock *bool `json:"scroll_lock"`
} }
type KeyboardLayout struct {
Event string `json:"event,omitempty"`
Layout string `json:"layout"`
Variant string `json:"variant"`
}
///////////////////////////// /////////////////////////////
// Cursor // Cursor
///////////////////////////// /////////////////////////////

View File

@ -76,12 +76,12 @@ func (h *MessageHandlerCtx) Message(session types.Session, raw []byte) bool {
}) })
// Keyboard Events // Keyboard Events
case event.KEYBOARD_MODIFIERS: case event.KEYBOARD_MAP:
payload := &message.KeyboardLayout{} payload := &message.KeyboardMap{}
err = utils.Unmarshal(payload, raw, func() error { err = utils.Unmarshal(payload, raw, func() error {
return h.keyboardLayout(session, payload) return h.keyboardMap(session, payload)
}) })
case event.KEYBOARD_LAYOUT: case event.KEYBOARD_MODIFIERS:
payload := &message.KeyboardModifiers{} payload := &message.KeyboardModifiers{}
err = utils.Unmarshal(payload, raw, func() error { err = utils.Unmarshal(payload, raw, func() error {
return h.keyboardModifiers(session, payload) return h.keyboardModifiers(session, payload)

View File

@ -6,13 +6,16 @@ import (
) )
func (h *MessageHandlerCtx) keyboardLayout(session types.Session, payload *message.KeyboardLayout) error { func (h *MessageHandlerCtx) keyboardMap(session types.Session, payload *message.KeyboardMap) error {
if !session.IsHost() { if !session.IsHost() {
h.logger.Debug().Str("id", session.ID()).Msg("is not the host") h.logger.Debug().Str("id", session.ID()).Msg("is not the host")
return nil return nil
} }
return h.desktop.SetKeyboardLayout(payload.Layout, payload.Variant) return h.desktop.SetKeyboardMap(types.KeyboardMap{
Layout: payload.Layout,
Variant: payload.Variant,
})
} }
func (h *MessageHandlerCtx) keyboardModifiers(session types.Session, payload *message.KeyboardModifiers) error { func (h *MessageHandlerCtx) keyboardModifiers(session types.Session, payload *message.KeyboardModifiers) error {