From 4a7800c93fdab183a3bacd7c21a7001c02a36dec Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Mon, 15 Jun 2020 23:14:23 +0200 Subject: [PATCH] change kbd layout using setxkbmap --- server/internal/remote/manager.go | 4 ++++ server/internal/types/remote.go | 1 + server/internal/websocket/control.go | 5 +++-- server/internal/xorg/xorg.c | 7 +++++++ server/internal/xorg/xorg.go | 10 ++++++++++ server/internal/xorg/xorg.h | 2 ++ 6 files changed, 27 insertions(+), 2 deletions(-) diff --git a/server/internal/remote/manager.go b/server/internal/remote/manager.go index 9a4a4e27..fe4413fb 100644 --- a/server/internal/remote/manager.go +++ b/server/internal/remote/manager.go @@ -218,3 +218,7 @@ func (manager *RemoteManager) ScreenConfigurations() map[int]types.ScreenConfigu func (manager *RemoteManager) GetScreenSize() *types.ScreenSize { return xorg.GetScreenSize() } + +func (manager *RemoteManager) SetKeyboard(layout string) { + xorg.SetKeyboard(layout) +} \ No newline at end of file diff --git a/server/internal/types/remote.go b/server/internal/types/remote.go index 3bdf7fd1..4b6453be 100644 --- a/server/internal/types/remote.go +++ b/server/internal/types/remote.go @@ -22,4 +22,5 @@ type RemoteManager interface { ReadClipboard() string WriteClipboard(data string) ResetKeys() + SetKeyboard(layout string) } diff --git a/server/internal/websocket/control.go b/server/internal/websocket/control.go index e7dabe59..d3b419fc 100644 --- a/server/internal/websocket/control.go +++ b/server/internal/websocket/control.go @@ -34,8 +34,9 @@ func (h *MessageHandler) controlRelease(id string, session types.Session) error func (h *MessageHandler) controlRequest(id string, session types.Session, payload *message.ControlRequest) error { // check for host if !h.sessions.HasHost() { - // TODO: Keyboard Layout change. - h.logger.Warn().Msgf("should set keyboard to %s", payload.KeyboardLayout) + // keyboard layout change + h.logger.Debug().Msgf("change keyboard to %s", payload.KeyboardLayout) + h.remote.SetKeyboard(payload.KeyboardLayout) // set host h.sessions.SetHost(id) diff --git a/server/internal/xorg/xorg.c b/server/internal/xorg/xorg.c index d1823864..2f129af1 100644 --- a/server/internal/xorg/xorg.c +++ b/server/internal/xorg/xorg.c @@ -165,3 +165,10 @@ short XGetScreenRate() { XRRScreenConfiguration *conf = XRRGetScreenInfo(display, RootWindow(display, 0)); return XRRConfigCurrentRate(conf); } + +void SetKeyboard(char *layout) { + // TOOD: refactor, use native API. + char cmd[12] = "setxkbmap "; + strcat(cmd, layout); + system(cmd); +} diff --git a/server/internal/xorg/xorg.go b/server/internal/xorg/xorg.go index 852a1551..8ff53f1f 100644 --- a/server/internal/xorg/xorg.go +++ b/server/internal/xorg/xorg.go @@ -210,6 +210,16 @@ func GetScreenSize() *types.ScreenSize { return nil } +func SetKeyboard(layout string) { + mu.Lock() + defer mu.Unlock() + + layoutUnsafe := C.CString(layout) + defer C.free(unsafe.Pointer(layoutUnsafe)) + + C.SetKeyboard(layoutUnsafe) +} + //export goCreateScreenSize func goCreateScreenSize(index C.int, width C.int, height C.int, mwidth C.int, mheight C.int) { ScreenConfigurations[int(index)] = types.ScreenConfiguration{ diff --git a/server/internal/xorg/xorg.h b/server/internal/xorg/xorg.h index e29b95d8..24384c48 100644 --- a/server/internal/xorg/xorg.h +++ b/server/internal/xorg/xorg.h @@ -38,5 +38,7 @@ void XDisplayClose(void); void XDisplaySet(char *input); + + void SetKeyboard(char *layout); #endif