Move utils.{c,h} to be used by the entire project
[platform/upstream/libxkbcommon.git] / test / xkey.c
1 #include "xkbmisc.h"
2 #include "xkbcommon/xkbcommon.h"
3 #include <stdlib.h>
4 #include <stdio.h>
5 #include <string.h>
6 #include <X11/X.h>
7
8 static void print_keysym(const char *s)
9 {
10     xkb_keysym_t ks = xkb_string_to_keysym(s);
11     if (ks == NoSymbol)
12         printf("NoSymbol\n");
13     else
14         printf("0x%lX\n", ks);
15 }
16
17 static void print_string(xkb_keysym_t ks)
18 {
19     char s[16];
20
21     xkb_keysym_to_string(ks, s, sizeof s);
22     printf("%s\n", s);
23 }
24
25 int main(int argc, char *argv[])
26 {
27     int mode;
28     xkb_keysym_t sym;
29
30     if (argc < 3) {
31         fprintf(stderr, "error: not enough arguments\n");
32         exit(EXIT_FAILURE);
33     }
34
35     if (strcmp(argv[1], "-k") == 0) {
36         mode = 0;
37         sym = strtoul(argv[2], NULL, 16);
38     }
39     else if (strcmp(argv[1], "-s") == 0)
40         mode = 1;
41     else {
42         fprintf(stderr, "error: unrecognized argument \"%s\"\n", argv[1]);
43         exit(EXIT_FAILURE);
44     }
45
46     if (mode == 0)
47         print_string(sym);
48     else
49         print_keysym(argv[2]);
50
51     return 0;
52 }