<commit_before>#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <linux/input.h>
#include <unistd.h>

// Event `value`; see: https://www.kernel.org/doc/html/latest/input/event-codes.html

#define EV_DOWN 1
#define EV_UP 0
#define EV_REPEAT 2

// When pushing keys in the Colemak layout, these are the hardware keys seen by
// the libevdev.

#define COLEMAK_A KEY_A
#define COLEMAK_C KEY_C
#define COLEMAK_F KEY_E
#define COLEMAK_G KEY_T
#define COLEMAK_I KEY_L
#define COLEMAK_L KEY_U
#define COLEMAK_N KEY_J
#define COLEMAK_R KEY_S
#define COLEMAK_T KEY_F
#define COLEMAK_W KEY_W
#define COLEMAK_Z KEY_Z

typedef enum {
    UP = 0,
    DOWN = 1,
} key_state;

/**
 * Reflects UP/DOWN state of distinct modifier keys.
 */
typedef struct modifier_state {
    key_state l_alt : 1;
    key_state l_ctrl : 1;
    key_state l_meta : 1;
    key_state l_shift : 1;
    key_state r_alt : 1;
    key_state r_ctrl : 1;
    key_state r_meta : 1;
    key_state r_shift : 1;
} modifier_state;

typedef enum {
    NEITHER = 0,
    LEFT = 1,
    RIGHT = 2,
    EITHER = 3,
    BOTH = 4,
    // TODO: maybe, OPTIONAL = 5
} modifier_req;

/**
 * Represents hardware state of modifier keys in upstream physical device.
 */
modifier_state hw_modifier_state;

/**
 * Represents software state of modifier keys in downstream virtual device.
 */
modifier_state virt_modifier_state;

/**
 * Used to track active remappings during the interval between EV_DOWN and
 * EV_UP.
 *
 * For example, if "x" is remapped to "y", upon seeing an EV_DOWN event for "x"
 * we set `active_remappings[KEY_X] = KEY_Y`. In this way, when we see the EV_UP
 * event for "x", we know to synthesize an EV_UP event for "y".
 */
__u16 active_remappings[KEY_MAX] = {0};

/**
 * Specifies required modifier combination in order to match or generate a
 * keyboard event.
 */
typedef struct modifier_spec {
    modifier_req alt: 3;
    modifier_req ctrl: 3;
    modifier_req meta: 3;
    modifier_req shift: 3;
} modifier_spec;

typedef struct key_combination {
    __u16 code;
    modifier_spec modifiers;
} key_combination;

typedef struct mapping {
    key_combination from;
    key_combination to;
} mapping;

const mapping mappings[] = {
    {
        .from = {.code = COLEMAK_A, .modifiers = {.alt = EITHER}},
        .to = {.code = COLEMAK_A, .modifiers = {.ctrl = LEFT}},
    },
    {
        .from = {.code = COLEMAK_C, .modifiers = {.alt = EITHER}},
        .to = {.code = COLEMAK_C, .modifiers = {.ctrl = LEFT}},
    },
    {
        .from = {.code = COLEMAK_F, .modifiers = {.alt = EITHER}},
        .to = {.code = COLEMAK_F, .modifiers = {.ctrl = LEFT}},
    },
    {
        .from = {.code = COLEMAK_G, .modifiers = {.alt = EITHER}},
        .to = {.code = COLEMAK_G, .modifiers = {.ctrl = LEFT}},
    },
    {
        .from = {.code = COLEMAK_G, .modifiers = {.alt = EITHER, .shift = EITHER}},
        .to = {.code = COLEMAK_G, .modifiers = {.ctrl = LEFT, .shift = LEFT}},
    },
    {
        .from = {.code = COLEMAK_L, .modifiers = {.alt = EITHER}},
        .to = {.code = COLEMAK_L, .modifiers = {.ctrl = LEFT}},
    },
    {
        .from = {.code = COLEMAK_N, .modifiers = {.alt = EITHER}},
        .to = {.code = COLEMAK_N, .modifiers = {.ctrl = LEFT}},
    },
    {
        .from = {.code = COLEMAK_N, .modifiers = {.alt = EITHER, .shift = EITHER}},
        .to = {.code = COLEMAK_N, .modifiers = {.ctrl = LEFT, .shift = LEFT}},
    },
    {
        .from = {.code = COLEMAK_R, .modifiers = {.alt = EITHER}},
        .to = {.code = COLEMAK_R, .modifiers = {.ctrl = LEFT}},
    },
    {
        .from = {.code = COLEMAK_T, .modifiers = {.alt = EITHER}},
        .to = {.code = COLEMAK_T, .modifiers = {.ctrl = LEFT}},
    },
    {
        .from = {.code = COLEMAK_T, .modifiers = {.alt = EITHER, .shift = EITHER}},
        .to = {.code = COLEMAK_T, .modifiers = {.ctrl = LEFT, .shift = LEFT}},
    },
    {
        .from = {.code = COLEMAK_W, .modifiers = {.alt = EITHER}},
        .to = {.code = COLEMAK_W, .modifiers = {.ctrl = LEFT}},
    },
    {
        .from = {.code = COLEMAK_Z, .modifiers = {.alt = EITHER}},
        .to = {.code = COLEMAK_Z, .modifiers = {.ctrl = LEFT}},
    },
    {
        .from = {.code = COLEMAK_Z, .modifiers = {.alt = EITHER, .shift = EITHER}},<commit_msg>feat(interception): add Alt-X to list of remapped keys<commit_after>#include <assert.h>
#include <stdbool.h>
#include <stdio.h>
#include <stdlib.h>
#include <linux/input.h>
#include <unistd.h>

// Event `value`; see: https://www.kernel.org/doc/html/latest/input/event-codes.html

#define EV_DOWN 1
#define EV_UP 0
#define EV_REPEAT 2

// When pushing keys in the Colemak layout, these are the hardware keys seen by
// the libevdev.

#define COLEMAK_A KEY_A
#define COLEMAK_C KEY_C
#define COLEMAK_F KEY_E
#define COLEMAK_G KEY_T
#define COLEMAK_I KEY_L
#define COLEMAK_L KEY_U
#define COLEMAK_N KEY_J
#define COLEMAK_R KEY_S
#define COLEMAK_T KEY_F
#define COLEMAK_W KEY_W
#define COLEMAK_X KEY_X
#define COLEMAK_Z KEY_Z

typedef enum {
    UP = 0,
    DOWN = 1,
} key_state;

/**
 * Reflects UP/DOWN state of distinct modifier keys.
 */
typedef struct modifier_state {
    key_state l_alt : 1;
    key_state l_ctrl : 1;
    key_state l_meta : 1;
    key_state l_shift : 1;
    key_state r_alt : 1;
    key_state r_ctrl : 1;
    key_state r_meta : 1;
    key_state r_shift : 1;
} modifier_state;

typedef enum {
    NEITHER = 0,
    LEFT = 1,
    RIGHT = 2,
    EITHER = 3,
    BOTH = 4,
    // TODO: maybe, OPTIONAL = 5
} modifier_req;

/**
 * Represents hardware state of modifier keys in upstream physical device.
 */
modifier_state hw_modifier_state;

/**
 * Represents software state of modifier keys in downstream virtual device.
 */
modifier_state virt_modifier_state;

/**
 * Used to track active remappings during the interval between EV_DOWN and
 * EV_UP.
 *
 * For example, if "x" is remapped to "y", upon seeing an EV_DOWN event for "x"
 * we set `active_remappings[KEY_X] = KEY_Y`. In this way, when we see the EV_UP
 * event for "x", we know to synthesize an EV_UP event for "y".
 */
__u16 active_remappings[KEY_MAX] = {0};

/**
 * Specifies required modifier combination in order to match or generate a
 * keyboard event.
 */
typedef struct modifier_spec {
    modifier_req alt: 3;
    modifier_req ctrl: 3;
    modifier_req meta: 3;
    modifier_req shift: 3;
} modifier_spec;

typedef struct key_combination {
    __u16 code;
    modifier_spec modifiers;
} key_combination;

typedef struct mapping {
    key_combination from;
    key_combination to;
} mapping;

const mapping mappings[] = {
    {
        .from = {.code = COLEMAK_A, .modifiers = {.alt = EITHER}},
        .to = {.code = COLEMAK_A, .modifiers = {.ctrl = LEFT}},
    },
    {
        .from = {.code = COLEMAK_C, .modifiers = {.alt = EITHER}},
        .to = {.code = COLEMAK_C, .modifiers = {.ctrl = LEFT}},
    },
    {
        .from = {.code = COLEMAK_F, .modifiers = {.alt = EITHER}},
        .to = {.code = COLEMAK_F, .modifiers = {.ctrl = LEFT}},
    },
    {
        .from = {.code = COLEMAK_G, .modifiers = {.alt = EITHER}},
        .to = {.code = COLEMAK_G, .modifiers = {.ctrl = LEFT}},
    },
    {
        .from = {.code = COLEMAK_G, .modifiers = {.alt = EITHER, .shift = EITHER}},
        .to = {.code = COLEMAK_G, .modifiers = {.ctrl = LEFT, .shift = LEFT}},
    },
    {
        .from = {.code = COLEMAK_L, .modifiers = {.alt = EITHER}},
        .to = {.code = COLEMAK_L, .modifiers = {.ctrl = LEFT}},
    },
    {
        .from = {.code = COLEMAK_N, .modifiers = {.alt = EITHER}},
        .to = {.code = COLEMAK_N, .modifiers = {.ctrl = LEFT}},
    },
    {
        .from = {.code = COLEMAK_N, .modifiers = {.alt = EITHER, .shift = EITHER}},
        .to = {.code = COLEMAK_N, .modifiers = {.ctrl = LEFT, .shift = LEFT}},
    },
    {
        .from = {.code = COLEMAK_R, .modifiers = {.alt = EITHER}},
        .to = {.code = COLEMAK_R, .modifiers = {.ctrl = LEFT}},
    },
    {
        .from = {.code = COLEMAK_T, .modifiers = {.alt = EITHER}},
        .to = {.code = COLEMAK_T, .modifiers = {.ctrl = LEFT}},
    },
    {
        .from = {.code = COLEMAK_T, .modifiers = {.alt = EITHER, .shift = EITHER}},
        .to = {.code = COLEMAK_T, .modifiers = {.ctrl = LEFT, .shift = LEFT}},
    },
    {
        .from = {.code = COLEMAK_W, .modifiers = {.alt = EITHER}},
        .to = {.code = COLEMAK_W, .modifiers = {.ctrl = LEFT}},
    },
    {
        .from = {.code = COLEMAK_X, .modifiers = {.alt = EITHER}},
        .to = {.code = COLEMAK_X, .modifiers = {.ctrl = LEFT}},
    },
    {
        .from = {.code = COLEMAK_Z, .modifiers = {.alt = EITHER}},
        .to = {.code = COLEMAK_Z, .modifiers = {.ctrl = LEFT}},
    },
    {
        .from = {.code = COLEMAK_Z, .modifiers = {.alt = EITHER, .shift = EITHER}},
{'fraction_non_alphanumeric': 0.1505278692246566, 'fraction_numerical': 0.006357134748552617, 'mean_word_length': 2.4521943573667713, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 2, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7544578', 'n_tokens_mistral': 3552, 'n_tokens_neox': 3411, 'n_words': 891}