Skip to content

Instantly share code, notes, and snippets.

@kreijack
Created January 10, 2021 17:07
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kreijack/a7d7d42bb8779eb29dd1f331f500a423 to your computer and use it in GitHub Desktop.
Save kreijack/a7d7d42bb8779eb29dd1f331f500a423 to your computer and use it in GitHub Desktop.
Query the service org.freedesktop.locale1 about the current X11 keyboard configuration
/*
* Author: Goffredo Baroncelli <kreijack@inwind.it>
* Date: 2021/01/10
* File name: query-locale.c
* Derived:
* http://0pointer.net/blog/the-new-sd-bus-api-of-systemd.html
* man sd_bus_get_property_string
* man org.freedesktop.locale1
* Description:
* Query the service org.freedesktop.locale1 about the current X11
* keyboard configuration
* Compile:
* $ gcc query-locale.c -o query-locale \
* `pkg-config --cflags --libs libsystemd`
* Run:
* $ ./query-locale
* X11Layout: it
* X11Model: pc105
* X11Options: terminate:ctrl_alt_bksp
* X11Variant:
* $
*/
#include <stdio.h>
#include <stdlib.h>
#include <systemd/sd-bus.h>
static int get_prop(sd_bus *bus, char *property_name, sd_bus_error *error, char **res) {
int r;
r = sd_bus_get_property_string(bus,
"org.freedesktop.locale1", /* service to contact */
"/org/freedesktop/locale1", /* object path */
"org.freedesktop.locale1", /* interface name */
property_name,
error,
res);
return r;
}
int main(int argc, char *argv[]) {
sd_bus_message *m = NULL;
sd_bus *bus = NULL;
const char *path;
int i, r;
char *props[] = {
"X11Layout",
"X11Model",
"X11Options",
"X11Variant",
NULL
};
/* Connect to the system bus */
r = sd_bus_open_system(&bus);
if (r < 0) {
fprintf(stderr, "Failed to connect to system bus: %s\n", strerror(-r));
goto finish;
}
for (i = 0 ; props[i] ; i++) {
sd_bus_error error = SD_BUS_ERROR_NULL;
char *ret;
r = get_prop(bus, props[i], &error, &ret);
if (r < 0) {
fprintf(stderr, "Failed to issue method call: %s\n", error.message);
sd_bus_error_free(&error);
goto finish;
}
sd_bus_error_free(&error);
printf("%s:\t%s\n", props[i], ret);
free(ret);
}
finish:
sd_bus_unref(bus);
return r < 0 ? EXIT_FAILURE : EXIT_SUCCESS;
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment