[download]

local/bin/xkbstate

   1 //usr/bin/cc -xc $0 -o$0~ -lX11 && exec $0~ "$@"
   2 
   3 #include <stdio.h>
   4 #include <unistd.h>
   5 #include <X11/XKBlib.h>
   6 
   7 int main(int argc, char *argv[]){
   8     Display *dpy;
   9     int last = -1;
  10     XkbStateRec state;
  11 
  12     if (!(dpy = XOpenDisplay(NULL)))
  13         return 1;
  14 
  15     while (True){
  16         usleep(100000);
  17         XkbGetState(dpy, XkbUseCoreKbd, &state);
  18 
  19         if (state.group == last)
  20             continue;
  21 
  22         last = state.group;
  23 
  24         if (argc > last + 1)
  25             printf("%s\n", argv[last + 1]);
  26         else
  27             printf("%d\n", last);
  28 
  29         fflush(stdout);
  30     }
  31 
  32     XCloseDisplay(dpy);
  33 }
  34 
  35 // vim: ft=c