summaryrefslogtreecommitdiffstats
path: root/utils/readmap.c
blob: 53bf6ad26f912676eb7b41ab18f86e7f9381181b (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
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
#include <errno.h>
#include <fcntl.h>
#include <sys/ioctl.h>
#include <linux/keyboard.h>
#include <linux/kd.h>
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <unistd.h>

#include "../isys/lang.h"

int main(int argc, char ** argv) {
    int console;
    int kmap, key;
    struct kbentry entry;
    int keymaps[MAX_NR_KEYMAPS];
    int count = 0;
    int out;
    short keymap[NR_KEYS];
    int magic = KMAP_MAGIC;
    int verbose = 0;

    if (argc != 2) {
	printf("bad usage\n");
	exit(1);
    }

    if (getenv("DEBUG") != NULL)
        verbose = 1;
 
    memset(keymaps, 0, sizeof(keymaps));

    console = open("/dev/console", O_RDWR);
    if (console < 0) {
	perror("open console");
	exit(1);
    }

    for (kmap = 0; kmap < MAX_NR_KEYMAPS; kmap++) {
	for (key = 0; key < NR_KEYS; key++) {
	    entry.kb_index = key;
	    entry.kb_table = kmap;
	    if (ioctl(console, KDGKBENT, &entry)) {
		perror("ioctl failed");
		exit(1);
	    } else if (KTYP(entry.kb_value) != KT_SPEC) {
		keymaps[kmap] = 1;
		count++;
		break;
	    }
	}
    }

    if (verbose) fprintf(stderr, "found %d valid keymaps\n", count);

    if (verbose) fprintf(stderr, "creating keymap file %s\n", argv[1]);
    if ((out = open(argv[1], O_WRONLY | O_CREAT | O_TRUNC, 0666)) < 1) {
	perror("open keymap");
	exit(1);
    }

    if (write(out, &magic, sizeof(magic)) != sizeof(magic)) {
	perror("write magic");
	exit(1);
    }

    if (write(out, keymaps, sizeof(keymaps)) != sizeof(keymaps)) {
	perror("write header");
	exit(1);
    }
        
    for (kmap = 0; kmap < MAX_NR_KEYMAPS; kmap++) {
	if (!keymaps[kmap]) continue;
	for (key = 0; key < NR_KEYS; key++) {
	    entry.kb_index = key;
	    entry.kb_table = kmap;
	    if (ioctl(console, KDGKBENT, &entry)) {
		perror("ioctl failed");
		exit(1);
	    } else {
		keymap[key] = entry.kb_value;
	    }
	}

	if (write(out, keymap, sizeof(keymap)) != sizeof(keymap)) {
	    perror("write keymap");
	    exit(1);
	}
    }

    close(out);

    return 0;
}