54 lines
942 B
C
54 lines
942 B
C
/*
|
||
* 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;
|
||
}
|