GUI-FreeRDP/freerdp/client/X11/cli/keylang.c

54 lines
942 B
C
Raw Permalink Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

/*
* keylang.c
*
* Created on: 17 февр. 2023 г.
* Author: alexander
*/
#include "keylang.h"
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <X11/XKBlib.h>
#include <X11/extensions/XKBrules.h>
/*
* 0 - unknown
* 1 - us
* 2 - ru
*/
int getKeyboardLanguage()
{
Display *dpy = XOpenDisplay(NULL);
XkbStateRec state;
XkbGetState(dpy, XkbUseCoreKbd, &state);
XkbRF_VarDefsRec vd;
XkbRF_GetNamesProp(dpy, NULL, &vd);
int length = strlen(vd.layout) + 1;
char *current = (char *)malloc(sizeof(char) * length);
strncpy(current, vd.layout, length);
current[length] = '\0';
XCloseDisplay(dpy);
char *lang = strtok(current, ",");
for (int i = 0; i < state.group; i++)
lang = strtok(NULL, ",");
int langcode = 0;
if (!strcmp(lang, "us")) langcode = 1;
if (!strcmp(lang, "ru")) langcode = 2;
free(current);
return langcode;
}