summaryrefslogtreecommitdiff
path: root/intcp-mapper.nix
blob: 95f0a6acc6b610f69b6036e40aceab6ae404dc30 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
{ lib, stdenv, writeText, mappings ? [] }:

stdenv.mkDerivation rec {
    name = "intcp-mapper";

    src = writeText "intcp-mapper.c" ''
#include <stdio.h>
#include <stdlib.h>
#include <linux/input.h>

int main(void) {
    setbuf(stdin, NULL), setbuf(stdout, NULL);

    struct input_event event;
    while (fread(&event, sizeof(event), 1, stdin) == 1) {
        /* Example
        if (event.type == EV_KEY && event.code == KEY_X) {
            event.code = KEY_Y;
        }
        */

        if (event.type == EV_KEY) {
            switch (event.code) {
            ${ lib.concatStrings (map (m: ''
                case ${m.from}:
                    event.code = ${m.to};
                    break;
            '') mappings) }
            }
        }

        fwrite(&event, sizeof(event), 1, stdout);
    };
}
    '';

    dontUnpack = true;

    buildPhase = ''
        cat $src
        gcc $src -o mapper
    '';

    installPhase = ''
        mkdir -p $out/bin
        install -m 555 mapper $out/bin/mapper
    '';
}