Skip to content

Commit

Permalink
desktop: +xkbledq
Browse files Browse the repository at this point in the history
  • Loading branch information
mk-fg committed Sep 22, 2024
1 parent e492dd3 commit 2b8ccb5
Show file tree
Hide file tree
Showing 2 changed files with 59 additions and 0 deletions.
14 changes: 14 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -184,6 +184,7 @@ Contents - links to doc section for each script here:
- [exclip](#hdr-exclip)
- [xdpms](#hdr-xdpms)
- [xiwait](#hdr-xiwait)
- [xkbledq](#hdr-xkbledq)
- [rss-get](#hdr-rss-get)
- [qr](#hdr-qr)
- [gtk-color-calc](#hdr-gtk-color-calc)
Expand Down Expand Up @@ -3757,6 +3758,19 @@ Build with: `gcc -O2 -lX11 -lXi -Wall xiwait.c -o xiwait && strip xiwait`

Also same as xdpms - should probably only work on Xorg systems, not wayland.

<a name=hdr-xkbledq></a>
##### [xkbledq](desktop/xkbledq.c)

Very simple C binary to query X11 keyboard (XKB) LED state(s),
either printing them or returning state via exit code=43 if LED is enabled.

Build with: `gcc -O2 -lX11 -Wall xkbledq.c -o xkbledq && strip xkbledq`\
Usage: `xkbledq` (print enabled LEDs), `xkbledq scroll` (return 43 if scroll lock enabled).

Intended for checking whether specific mode should be enabled depending on
user-visible keyboard LED state (if/when it's used as a simple indicator),
e.g. swap xmodmap layout depending on Scroll Lock mode.

<a name=hdr-rss-get></a>
##### [rss-get](desktop/rss-get)

Expand Down
45 changes: 45 additions & 0 deletions desktop/xkbledq.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Small standalone C binary to query/print keyboard LED state(s)
// Build with: gcc -O2 -lX11 -Wall xkbledq.c -o xkbledq && strip xkbledq
// Usage info: ./xkbledq -h

#include <stdio.h>
#include <string.h>

#include <X11/Xlib.h>
#include <X11/XKBlib.h>

#define err_unless(chk) if (!(chk)) { err = "'" #chk "' failed"; goto cleanup; }

int main(int argc, char *argv[]) {
if (argc > 2) {
printf("Usage: %s [-h/--help] [led]\n", argv[0]);
printf("Show/query named keyboard LED state(s).\n");
printf( "When querying, returns 1 for e.g."
" \"%s scroll\" if scroll lock LED is lit, 0 otherwise.\n", argv[0] );
return 1; }
char *query = "";
if (argc == 2) query = argv[1];

char *err = NULL;
Display *dpy = NULL;
int xkb_opcode, xkb_event, xkb_error;
int xkb_lmaj = XkbMajorVersion, xkb_lmin = XkbMinorVersion;
err_unless(dpy = XOpenDisplay(NULL));
err_unless(XkbLibraryVersion(&xkb_lmaj, &xkb_lmin));
err_unless(XkbQueryExtension(dpy, &xkb_opcode, &xkb_event, &xkb_error, &xkb_lmaj, &xkb_lmin));

char atoms[][12] = {"Caps Lock", "Num Lock", "Scroll Lock"};
char leds[][7] = {"caps", "num", "scroll"};

Bool st;
for (size_t n = 0; n <= 2; n++) {
XkbGetNamedIndicator(dpy, XInternAtom(dpy, atoms[n], False), NULL, &st, NULL, NULL);
if (!st) continue;
if (strlen(query)) { if (!strcmp(query, leds[n])) return 43; }
else puts(leds[n]); }

cleanup:
if (dpy) XCloseDisplay(dpy);
if (err) { fprintf(stderr, "ERROR: %s\n", err); return 1; }
return 0;
}

0 comments on commit 2b8ccb5

Please sign in to comment.