Keyboard layouts information retrieval

Peter Hutterer peter.hutterer at who-t.net
Mon Sep 23 00:08:40 UTC 2019


On Sun, Sep 22, 2019 at 02:22:08PM +0200, Glus Xof wrote:
> Hi Ken,
> 
> First of all, I must thank you for your courage to answer a question that
> probably went incomplete and sounds a little weird.
> 
> The idea is to call X11 server directly (maybe using Display). Parsing disk
> files (/usr/share/X11/xkb/) could not be good enough, here.
> 
> The second focus is to limit the data according to the working keyboard
> geometry.
> 
> Is there a function to call X11 server for...
> 
> a) the current/active/detected/in use keyboard geometry name?
> b) the names and/or ids of all defined keyboard layouts for an specific
> geometry ?
> c) the list of keycodes/levels/keysyms for an specific layout (and
> geometry) ?
> 
> I'm working in C/C++.

A few things: the server (and XKB itself) doesn't have a concept of "layout"
as you'd think. It has a concept of keymaps which is basically the set of
symbols your keyboard currently generates. Where you have multiple layouts,
you have multiple "groups" in XKB, but again, the server doesn't care about
layouts per se.

Layouts are a shortcut to define which symbols to load, and the conversion
from layout (as specified by the user) to keymap (as used by the server) is
defined by the /usr/share/X11/xkb/rules/evdev file.

Which basically means: what you want to do isn't possible, the layout to
keymap conversion is partially lossy. You can get the keycodes/levels/etc
from the keymap, but you can't get back to the layout from that.

There's an xkb property that contains the layout, but note that it only
contains the setting of the last device. If you have e.g. keyboards with
different layouts, it won't be correct:
_XKB_RULES_NAMES = "evdev", "pc105", "us", "", ""
And if a client can change the keymap without ever updating the property.

So yeah, what you want to do won't work. Maybe try to explain what the end
result is going to be.

> a) the current/active/detected/in use keyboard geometry name?

that property is your best bet, but it could be wrong

> b) the names and/or ids of all defined keyboard layouts for an specific
> geometry ?

geometries use wildcards, so a model of 'banana' will use the 'pc104'
geometry. so that data simply doesn't exist in the form you think it does.

> c) the list of keycodes/levels/keysyms for an specific layout (and
> geometry) ?

That bit is in the xkb map and you can run through it and parse it. I
recommend libxkbcommon but then again, it doesn't to geometry (because
geometry is largely obsolete). But start with XkbGetMap and work your way
from there.

Cheers,
   Peter


> Missatge de Ken Moffat <zarniwhoop at ntlworld.com> del dia dv., 20 de set.
> 2019 a les 21:16:
> 
> > On Fri, Sep 20, 2019 at 11:15:41AM +0200, Glus Xof wrote:
> > > Hi guys,
> > >
> > > I need to retrieve all keyboard keycodes, levels, keysyms and keyboard
> > > layout names for all keyboard layouts defined in my X11 system and
> > > according the current keyboard geometry.
> > >
> > > Which would be the suitable library for ?
> > >
> > > Is there any tutorial, piece of code... ?
> > >
> > > Glus
> >
> > Hi Glus,
> >
> > that sounds an uncommon requirement.  I have no idea what you want
> > to do with that data, but I'll be very surprised if there are any
> > libraries or tutorials for doing this.  OTOH, it would not be the
> > first time I've been surprised :)
> >
> > Anyway, if X is in /usr then xkeyboard-config installs its files in
> > /usr/share/X11/xkb/.  For example, on a British keyboard the various
> > options are in /usr/share/X11/xkb/symbols/gb with labels like
> >  xkb_symbols "dvorak"
> >
> > But these names are only unique within that 'language' file, and
> > they usually include various other items (e.g. "us(dvorak-alt-intl)"
> > for that example.
> >
> > Depending on exactly why you want to do this, and for how wide a
> > range of keyboards, it looks like a job for your favourite scripting
> > language.
> >
> > Similarly, geometry definitions in xkb/geometry and other files in
> > compat/ keycodes/ rules/ and types/.
> >
> > Potentially, your scope is vast (e.g. I could use non-gb non-qwerty
> > layouts on my standard British keyboard, although the only sane
> > reason would be if I knew one of those layouts by heart).  In
> > practice, those of us who use X (possibly without gnome or kde) can
> > make our own local changes to get modified mappings for things which
> > are otherwise only in Compose settings.
> >
> > ĸen
> > --
> > thread 'main' panicked at 'giraffe',
> > /tmp/rustc-1.32.0-src/src/test/run-fail/while-panic.rs:17:13
> > _______________________________________________
> > xorg at lists.x.org: X.Org support
> > Archives: http://lists.freedesktop.org/archives/xorg
> > Info: https://lists.x.org/mailman/listinfo/xorg
> > Your subscription address: %(user_address)s

> _______________________________________________
> xorg at lists.x.org: X.Org support
> Archives: http://lists.freedesktop.org/archives/xorg
> Info: https://lists.x.org/mailman/listinfo/xorg
> Your subscription address: %(user_address)s



More information about the xorg mailing list