<commit_before>#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <X11/Xlib.h>
#include <X11/XKBlib.h>
#include <X11/extensions/XKBrules.h>

#define DELIMETER ","
#define LAYOUT_FORMAT "%s\r\n"

static void print_usage()
{
    fprintf(stdout, "usage: cxl [options]\n");
    fprintf(stdout, "Options:\n");
    fprintf(stdout, "  -d\tPrint current layout and exit (dump).\n");
    fprintf(stdout, "  -h\tThis message.\n");
}

int main(int argc, char *argv[])
{
    int rc = EXIT_FAILURE;
    XkbEvent event;
    XkbRF_VarDefsRec vd;
    XkbStateRec state;
    char *groups[XkbNumKbdGroups];
    char **tmp = groups;
    char *display_name = NULL;
    Display *display = NULL;
    int opt = 0;
    int done = 0;
    while ((opt = getopt(argc, argv, "dh")) != -1) {
        switch (opt) {
        case 'd':
            done = 1;
            break;
        case 'h':
            rc = EXIT_SUCCESS;
            /* fall through */
        default:
            print_usage();
            goto out;
        }
    }
    if (!(display = XOpenDisplay(display_name))) {
        fprintf(stderr, "failed to open display\n");
        goto out;
    }
    if (XkbRF_GetNamesProp(display, NULL, &vd) != True) {
        fprintf(stderr, "failed to get layout names\n");
        goto out_close_display;
    }
    while ((*(tmp++) = strsep(&vd.layout, DELIMETER)));
    if (XkbGetState(display, XkbUseCoreKbd, &state) == Success)
        fprintf(stdout, LAYOUT_FORMAT, groups[state.locked_group]);
    else
        fprintf(stderr, "failed to get current keyboard state\n");
    if (!done && XkbSelectEventDetails(display,
                                       XkbUseCoreKbd,
                                       XkbStateNotify,
                                       XkbGroupLockMask,
                                       XkbGroupLockMask) != True) {
        fprintf(stderr, "failed to select layout change event\n");
        goto out_close_display;
    }
    while (!done)
        if (XNextEvent(display, &event.core) == Success)
            fprintf(stdout, LAYOUT_FORMAT, groups[event.state.locked_group]);
        else
            fprintf(stderr, "failed to get next event\n");

    XFree(vd.model);
    XFree(vd.layout);
    XFree(vd.variant);
    XFree(vd.options);
out_close_display:
    rc = XCloseDisplay(display);
out:
    return rc;
}
<commit_msg>Layout monitor code extracted<commit_after>#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <X11/Xlib.h>
#include <X11/XKBlib.h>
#include <X11/extensions/XKBrules.h>

#define DELIMETER ","

typedef void (printer_t)(int grp_num, void *ctxt);

static void literal_prt(int grp_num, void *ctxt)
{
    fprintf(stdout, "%s\n", ((char**)ctxt)[grp_num]);
}

static void print_usage()
{
    fprintf(stdout, "usage: cxl [options]\n");
    fprintf(stdout, "Options:\n");
    fprintf(stdout, "  -d\tPrint current layout and exit (dump).\n");
    fprintf(stdout, "  -h\tThis message.\n");
}

static void monitor_events(Display *dpy, printer_t *prt, void *prt_ctxt)
{
    XkbEvent event;
    if (XkbSelectEventDetails(dpy,
                              XkbUseCoreKbd,
                              XkbStateNotify,
                              XkbGroupLockMask,
                              XkbGroupLockMask) == True) {
        while (1)
            if (XNextEvent(dpy, &event.core) == Success)
                prt(event.state.locked_group, prt_ctxt);
            else
                fprintf(stderr, "failed to get next event\n");
    } else {
        fprintf(stderr, "failed to select layout change event\n");
    }
}

int main(int argc, char *argv[])
{
    int rc = EXIT_FAILURE;
    XkbRF_VarDefsRec vd;
    XkbStateRec state;
    char *groups[XkbNumKbdGroups];
    char **tmp = groups;
    char *display_name = NULL;
    Display *display = NULL;
    int opt = 0;
    int dump = 0;
    printer_t *printer = literal_prt;
    while ((opt = getopt(argc, argv, "dh")) != -1) {
        switch (opt) {
        case 'd':
            dump = 1;
            break;
        case 'h':
            rc = EXIT_SUCCESS;
            /* fall through */
        default:
            print_usage();
            goto out;
        }
    }
    if (!(display = XOpenDisplay(display_name))) {
        fprintf(stderr, "failed to open display\n");
        goto out;
    }
    if (XkbRF_GetNamesProp(display, NULL, &vd) != True) {
        fprintf(stderr, "failed to get layout names\n");
        goto out_close_display;
    }
    while ((*(tmp++) = strsep(&vd.layout, DELIMETER)));
    if (XkbGetState(display, XkbUseCoreKbd, &state) == Success)
        printer(state.locked_group, groups);
    else
        fprintf(stderr, "failed to get current keyboard state\n");
    if (!dump) monitor_events(display, printer, groups);

    XFree(vd.model);
    XFree(vd.layout);
    XFree(vd.variant);
    XFree(vd.options);
out_close_display:
    rc = XCloseDisplay(display);
out:
    return rc;
}

{'fraction_non_alphanumeric': 0.13174989849776694, 'fraction_numerical': 0.004263093788063338, 'mean_word_length': 2.191062176165803, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 4, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '6127345', 'n_tokens_mistral': 1617, 'n_tokens_neox': 1619, 'n_words': 379}