xkblayout

prints current xkb layout
git clone git://git.2f30.org/xkblayout.git
Log | Files | Refs | README | LICENSE

commit 95f9584e724060cccb9fe3ee5846c766833fbd46
Author: lostd <lostd@2f30.org>
Date:   Sat Oct 17 22:15:25 +0300

First version

Diffstat:
LICENSE | 23+++++++++++++++++++++++
Makefile | 19+++++++++++++++++++
README | 4++++
xkblayout.c | 65+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
4 files changed, 111 insertions(+), 0 deletions(-)
diff --git a/LICENSE b/LICENSE @@ -0,0 +1,23 @@ +Copyright (c) 2015 Lazaros Koromilas <lostd@2f30.org> +All rights reserved. + +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 AUTHOR ``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 AUTHOR 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 @@ -0,0 +1,19 @@ +PREFIX = /usr/local +CFLAGS = -I/usr/X11R6/include +LDFLAGS = -L/usr/X11R6/lib +LDLIBS = -lX11 -lxkbfile +BIN = xkblayout + +all: $(BIN) + +clean: + rm -f $(BIN) + +install: all + mkdir -p $(DESTDIR)$(PREFIX)/bin + cp -f $(BIN) $(DESTDIR)$(PREFIX)/bin + +uninstall: + rm -f $(DESTDIR)$(PREFIX)/bin/$(BIN) + +.PHONY: all clean install uninstall diff --git a/README b/README @@ -0,0 +1,4 @@ +Small program that prints the current XKB layout. It is useful +for constructing the status string for dwm. For example: + + xsetroot -name "$(xkblayout) -- $(date)" diff --git a/xkblayout.c b/xkblayout.c @@ -0,0 +1,65 @@ +#include <err.h> +#include <stdio.h> +#include <stdlib.h> +#include <string.h> + +#include <X11/XKBlib.h> +#include <X11/extensions/XKBrules.h> + +#ifdef DEBUG +#define DPRINTF_U(t) printf(#t"=%u\n", t) +#define DPRINTF_S(t) printf(#t"=%s\n", t) +#else +#define DPRINTF_U(t) +#define DPRINTF_S(t) +#endif + +int +main(void) +{ + Display *dpy; + XkbDescPtr desc; + XkbStateRec state; + XkbRF_VarDefsRec vd; + char *group, *symbols; + char *tmp, *str, *orig, *tok; + unsigned int i; + + dpy = XOpenDisplay(NULL); + if (dpy == NULL) + errx(1, "Cannot open display"); + + /* Get layout group index in [0..3] */ + XkbGetState(dpy, XkbUseCoreKbd, &state); + DPRINTF_U(state.group); + + /* Get layout names */ + desc = XkbGetKeyboard(dpy, XkbAllComponentsMask, XkbUseCoreKbd); + group = XGetAtomName(dpy, desc->names->groups[state.group]); + DPRINTF_S(group); + symbols = XGetAtomName(dpy, desc->names->symbols); + DPRINTF_S(symbols); + + /* Get clean layouts string */ + XkbRF_GetNamesProp(dpy, &tmp, &vd); + DPRINTF_S(tmp); + DPRINTF_S(vd.layout); + + str = strdup(vd.layout); + orig = str; + + /* Tokenize until we reach the current layout */ + for (i = 0; i <= state.group; i++) { + tok = strsep(&str, ","); + if (tok == NULL) + return 1; + DPRINTF_S(tok); + } + + /* Print layout name */ + printf("%s\n", tok); + + free(orig); + + return 0; +}