From 5afd9290c00f7e7783619d5675c5ef67fa22cffe Mon Sep 17 00:00:00 2001 From: Nakidai Date: Mon, 19 Feb 2024 23:11:14 +0300 Subject: [PATCH] Add files --- LICENSE | 9 +++++++++ Makefile | 14 ++++++++++++++ README.md | 4 ++++ getlayout.c | 35 +++++++++++++++++++++++++++++++++++ 4 files changed, 62 insertions(+) create mode 100644 LICENSE create mode 100644 Makefile create mode 100644 README.md create mode 100644 getlayout.c diff --git a/LICENSE b/LICENSE new file mode 100644 index 0000000..7977ab2 --- /dev/null +++ b/LICENSE @@ -0,0 +1,9 @@ +Copyright 2024 Nakidai + +Redistribution and use in source and binary forms, with or without modification, are permitted provided that the following conditions are met: + +1. Redistributions of source code must retain the above copyright notice, this list of conditions and the following disclaimer. + +2. Redistributions in binary form must reproduce the above copyright notice, this list of conditions and the following disclaimer in the documentation and/or other materials provided with the distribution. + +THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS “AS IS” AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. diff --git a/Makefile b/Makefile new file mode 100644 index 0000000..b36fea4 --- /dev/null +++ b/Makefile @@ -0,0 +1,14 @@ +PREFIX ?= /usr + +all: getlayout + +install: getlayout + install -m 555 getlayout ${PREFIX}/bin/getlayout + +clean: + rm -f getlayout + +getlayout: + cc -lX11 -lxkbfile getlayout.c -o getlayout + +.PHONY: all install clean diff --git a/README.md b/README.md new file mode 100644 index 0000000..b37c7d5 --- /dev/null +++ b/README.md @@ -0,0 +1,4 @@ +getlayout +-- +Some small utility to get current layout. Depends on X11. +Edited version of [this script](https://gist.github.com/fikovnik/ef428e82a26774280c4fdf8f96ce8eeb) diff --git a/getlayout.c b/getlayout.c new file mode 100644 index 0000000..5620a15 --- /dev/null +++ b/getlayout.c @@ -0,0 +1,35 @@ +/* cc -lX11 -lxkbfile getlayout.c */ + +#include +#include + +#include +#include + +int main(void) +{ + Display *dpy = XOpenDisplay(NULL); + + if (dpy == NULL) + { + fputs("Cannot open display\n", stderr); + return 1; + } + + XkbStateRec state; + XkbGetState(dpy, XkbUseCoreKbd, &state); + + XkbRF_VarDefsRec vd; + XkbRF_GetNamesProp(dpy, NULL, &vd); + + char *tok = strtok(vd.layout, ","); + + for (int i = 0; i < state.group; i++) + { + tok = strtok(NULL, ","); + if (tok == NULL) + return 1; + } + + fputs(tok, stdout); +}