File: export-keysyms.c

package info (click to toggle)
libxkbcommon 1.13.1-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 8,344 kB
  • sloc: ansic: 57,807; xml: 8,905; python: 7,451; yacc: 913; sh: 253; makefile: 23
file content (90 lines) | stat: -rw-r--r-- 2,567 bytes parent folder | download
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
/*
 * Copyright © 2024 Pierre Le Marre
 * SPDX-License-Identifier: MIT
 */

#include "config.h"

#include <locale.h>
#include <stdio.h>
#include <stdbool.h>
#include <stdlib.h>
#include <string.h>
#if HAVE_ICU
#include <unicode/uchar.h>
#endif

#include "xkbcommon/xkbcommon.h"
#include "src/keysym.h"

static void
print_char_name(uint32_t cp)
{
#if HAVE_ICU
    char char_name[256];
    UErrorCode pErrorCode = 0;
    int32_t count = u_charName((UChar32)cp, U_CHAR_NAME_ALIAS, char_name,
                                sizeof(char_name), &pErrorCode);
    if (count <= 0) {
        count = u_charName((UChar32)cp, U_EXTENDED_CHAR_NAME,
                            char_name, sizeof(char_name), &pErrorCode);
    }
    if (count > 0) {
        printf(" %s", char_name);
    }
#endif
}

int
main(int argc, char **argv)
{
    bool explicit = !(argc > 1 && strcmp("all", argv[1]) == 0);
    int idx = 1 + !explicit;
    bool char_names = argc > idx && strcmp("names", argv[idx]) == 0;
#if !HAVE_ICU
    if (char_names)
        fprintf(stderr, "ERROR: names argument requires ICU.\n");
#endif

    setlocale(LC_ALL, "");

    struct xkb_keysym_iterator *iter = xkb_keysym_iterator_new(explicit);
    while (xkb_keysym_iterator_next(iter)) {
        xkb_keysym_t ks = xkb_keysym_iterator_get_keysym(iter);
        printf("0x%04x:\n", ks);
        char name[XKB_KEYSYM_NAME_MAX_SIZE];
        xkb_keysym_iterator_get_name(iter, name, sizeof(name));
        printf("  name: %s\n", name);
        // char utf8[7];
        // int count = xkb_keysym_to_utf8(ks, utf8, sizeof(utf8));
        uint32_t cp = xkb_keysym_to_utf32(ks);
        if (cp) {
            printf("  code point: 0x%04X", cp);
            if (char_names) {
                printf(" #");
                print_char_name(cp);
            }
            printf("\n");
        }
        xkb_keysym_t ks2;
        if ((ks2 = xkb_keysym_to_lower(ks)) != ks) {
            xkb_keysym_get_name(ks2, name, sizeof(name));
            printf("  lower: 0x%04x # %s", ks2, name);
            if (char_names) {
                cp = xkb_keysym_to_utf32(ks2);
                print_char_name(cp);
            }
            printf("\n");
        }
        if ((ks2 = xkb_keysym_to_upper(ks)) != ks) {
            xkb_keysym_get_name(ks2, name, sizeof(name));
            printf("  upper: 0x%04x # %s", ks2, name);
            if (char_names) {
                cp = xkb_keysym_to_utf32(ks2);
                print_char_name(cp);
            }
            printf("\n");
        }
    };
    xkb_keysym_iterator_unref(iter);
}