From 7f18c5842d163d4a49ea7c1cc16080cd9668cfea Mon Sep 17 00:00:00 2001 From: =?UTF-8?q?Miroslav=20=C5=A0ediv=C3=BD?= Date: Tue, 12 Jan 2021 23:48:15 +0100 Subject: [PATCH] changing keyboard layout as go exec. --- internal/api/room/keyboard.go | 6 +++++- internal/desktop/xorg.go | 9 +++++++-- internal/desktop/xorg/xorg.c | 7 ------- internal/desktop/xorg/xorg.go | 15 --------------- internal/desktop/xorg/xorg.h | 1 - internal/types/desktop.go | 2 +- internal/websocket/handler/keyboard.go | 3 +-- 7 files changed, 14 insertions(+), 29 deletions(-) diff --git a/internal/api/room/keyboard.go b/internal/api/room/keyboard.go index 00e26c45..73d0a696 100644 --- a/internal/api/room/keyboard.go +++ b/internal/api/room/keyboard.go @@ -22,7 +22,11 @@ func (h *RoomHandler) keyboardLayoutSet(w http.ResponseWriter, r *http.Request) return } - h.desktop.SetKeyboardLayout(data.Layout) + err := h.desktop.SetKeyboardLayout(data.Layout) + if err != nil{ + utils.HttpInternalServerError(w, "Unable to change keyboard layout.") + return + } utils.HttpSuccess(w) } diff --git a/internal/desktop/xorg.go b/internal/desktop/xorg.go index 0172b69f..fed7cee8 100644 --- a/internal/desktop/xorg.go +++ b/internal/desktop/xorg.go @@ -1,6 +1,8 @@ package desktop import ( + "os/exec" + "demodesk/neko/internal/types" "demodesk/neko/internal/desktop/xorg" ) @@ -53,8 +55,11 @@ func (manager *DesktopManagerCtx) ChangeScreenSize(width int, height int, rate i return xorg.ChangeScreenSize(width, height, rate) } -func (manager *DesktopManagerCtx) SetKeyboardLayout(layout string) { - xorg.SetKeyboardLayout(layout) +func (manager *DesktopManagerCtx) SetKeyboardLayout(layout string) error { + // TOOD: Use native API. + cmd := exec.Command("setxkbmap", layout) + _, err := cmd.Output() + return err } func (manager *DesktopManagerCtx) SetKeyboardModifiers(mod types.KeyboardModifiers) { diff --git a/internal/desktop/xorg/xorg.c b/internal/desktop/xorg/xorg.c index afbd4135..ef8b8001 100644 --- a/internal/desktop/xorg/xorg.c +++ b/internal/desktop/xorg/xorg.c @@ -119,13 +119,6 @@ short XGetScreenRate() { return XRRConfigCurrentRate(conf); } -void SetKeyboardLayout(char *layout) { - // TOOD: refactor, use native API. - char cmd[13] = "setxkbmap "; - strncat(cmd, layout, 2); - system(cmd); -} - void XSetKeyboardModifier(int mod, int on) { Display *display = getXDisplay(); XkbLockModifiers(display, XkbUseCoreKbd, mod, on ? mod : 0); diff --git a/internal/desktop/xorg/xorg.go b/internal/desktop/xorg/xorg.go index 686e1afa..6649d93e 100644 --- a/internal/desktop/xorg/xorg.go +++ b/internal/desktop/xorg/xorg.go @@ -12,7 +12,6 @@ import ( "sync" "time" "unsafe" - "regexp" "demodesk/neko/internal/types" ) @@ -204,20 +203,6 @@ func GetScreenSize() *types.ScreenSize { return nil } -func SetKeyboardLayout(layout string) { - mu.Lock() - defer mu.Unlock() - - if !regexp.MustCompile(`^[a-zA-Z]+$`).MatchString(layout) { - return - } - - layoutUnsafe := C.CString(layout) - defer C.free(unsafe.Pointer(layoutUnsafe)) - - C.SetKeyboardLayout(layoutUnsafe) -} - func SetKeyboardModifier(mod KbdModifiers, active bool) { mu.Lock() defer mu.Unlock() diff --git a/internal/desktop/xorg/xorg.h b/internal/desktop/xorg/xorg.h index 0b1cf5c5..ef9c04f9 100644 --- a/internal/desktop/xorg/xorg.h +++ b/internal/desktop/xorg/xorg.h @@ -25,7 +25,6 @@ void XSetScreenConfiguration(int index, short rate); int XGetScreenSize(); short XGetScreenRate(); -void SetKeyboardLayout(char *layout); void XSetKeyboardModifier(int mod, int on); char XGetKeyboardModifiers(); XFixesCursorImage *XGetCursorImage(void); diff --git a/internal/types/desktop.go b/internal/types/desktop.go index f79b46a3..20e76271 100644 --- a/internal/types/desktop.go +++ b/internal/types/desktop.go @@ -43,7 +43,7 @@ type DesktopManager interface { ResetKeys() ScreenConfigurations() map[int]ScreenConfiguration GetScreenSize() *ScreenSize - SetKeyboardLayout(layout string) + SetKeyboardLayout(layout string) error SetKeyboardModifiers(mod KeyboardModifiers) GetKeyboardModifiers() KeyboardModifiers GetCursorImage() *CursorImage diff --git a/internal/websocket/handler/keyboard.go b/internal/websocket/handler/keyboard.go index 2ff5da88..1314cdf7 100644 --- a/internal/websocket/handler/keyboard.go +++ b/internal/websocket/handler/keyboard.go @@ -12,8 +12,7 @@ func (h *MessageHandlerCtx) keyboardLayout(session types.Session, payload *messa return nil } - h.desktop.SetKeyboardLayout(payload.Layout) - return nil + return h.desktop.SetKeyboardLayout(payload.Layout) } func (h *MessageHandlerCtx) keyboardModifiers(session types.Session, payload *message.KeyboardModifiers) error {