input: use libxkbcommon to convert input events
[platform/upstream/kmscon.git] / tests / test_input.c
1 /*
2  * test_input - Test the input system - hotplug and keypresses
3  *
4  * Copyright (c) 2011 Ran Benita <ran234@gmail.com>
5  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files
8  * (the "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included
15  * in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25
26 #include <errno.h>
27 #include <linux/input.h>
28 #include <locale.h>
29 #include <signal.h>
30 #include <stdbool.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <unistd.h>
35 #include <wchar.h>
36
37 #include "eloop.h"
38 #include "input.h"
39 #include "log.h"
40
41 static bool terminate;
42
43 static void sig_term(struct kmscon_signal *sig, int signum, void *data)
44 {
45         terminate = true;
46 }
47
48 /* Pressing Ctrl-\ should toggle the capturing. */
49 static void sig_quit(struct kmscon_signal *sig, int signum, void *data)
50 {
51         struct kmscon_input *input = data;
52
53         if (kmscon_input_is_asleep(input)) {
54                 kmscon_input_wake_up(input);
55                 log_info("Woke Up\n");
56         } else {
57                 kmscon_input_sleep(input);
58                 log_info("Went to sleep\n");
59         }
60 }
61
62 static void print_modifiers(uint8_t mods)
63 {
64         if (mods & XKB_COMMON_SHIFT_MASK)
65                 printf("SHIFT ");
66         if (mods & XKB_COMMON_LOCK_MASK)
67                 printf("LOCK ");
68         if (mods & XKB_COMMON_CONTROL_MASK)
69                 printf("CONTROL ");
70         if (mods & XKB_COMMON_MOD1_MASK)
71                 printf("MOD1 ");
72         if (mods & XKB_COMMON_MOD2_MASK)
73                 printf("MOD2 ");
74         if (mods & XKB_COMMON_MOD3_MASK)
75                 printf("MOD3 ");
76         if (mods & XKB_COMMON_MOD4_MASK)
77                 printf("MOD4 ");
78         if (mods & XKB_COMMON_MOD5_MASK)
79                 printf("MOD5 ");
80         printf("\n");
81 }
82
83 static void input_arrived(struct kmscon_input *input,
84                                 struct kmscon_input_event *ev, void *data)
85 {
86         int len;
87         char s[16];
88         char utf8[MB_CUR_MAX + 1];
89
90         if (ev->unicode == 0) {
91                 xkb_keysym_to_string(ev->keysym, s, sizeof(s));
92                 printf("sym %s ", s);
93         } else {
94                 /*
95                  * Just a proof-of-concept hack. This works because glibc uses
96                  * UTF-32 (= UCS-4) as the internal wchar_t encoding.
97                  */
98                 len = wctomb(utf8, (wchar_t)ev->unicode);
99                 if (len <= 0) {
100                         log_info("Bad unicode char\n");
101                         return;
102                 } else {
103                         utf8[len] = '\0';
104                 }
105
106                 printf("utf8 %s ", utf8);
107         }
108         print_modifiers(ev->modifiers);
109 }
110
111 int main(int argc, char **argv)
112 {
113         int ret;
114         struct kmscon_eloop *loop;
115         struct kmscon_input *input;
116         struct kmscon_signal *sigint, *sigquit;
117
118         if (!setlocale(LC_ALL, "en_US.UTF-8")) {
119                 log_err("Cannot set locale: %m\n");
120                 ret = -EFAULT;
121                 goto err_out;
122         }
123
124         ret = kmscon_eloop_new(&loop);
125         if (ret) {
126                 log_err("Cannot create eloop\n");
127                 goto err_out;
128         }
129
130         ret = kmscon_input_new(&input);
131         if (ret) {
132                 log_err("Cannot create input\n");
133                 goto err_loop;
134         }
135
136         ret = kmscon_eloop_new_signal(loop, &sigint, SIGINT, sig_term, NULL);
137         if (ret) {
138                 log_err("Cannot add INT signal\n");
139                 goto err_input;
140         }
141
142         ret = kmscon_eloop_new_signal(loop, &sigquit, SIGQUIT, sig_quit, input);
143         if (ret) {
144                 log_err("Cannot add quit signal\n");
145                 goto err_sigint;
146         }
147
148         ret = kmscon_input_connect_eloop(input, loop, input_arrived, NULL);
149         if (ret) {
150                 log_err("Cannot connect input\n");
151                 goto err_sigquit;
152         }
153
154         kmscon_input_wake_up(input);
155
156         system("stty -echo");
157
158         while (!terminate) {
159                 ret = kmscon_eloop_dispatch(loop, -1);
160                 if (ret) {
161                         log_err("Dispatcher failed\n");
162                         break;
163                 }
164         }
165
166         system("stty echo");
167
168 err_sigquit:
169         kmscon_eloop_rm_signal(sigquit);
170 err_sigint:
171         kmscon_eloop_rm_signal(sigint);
172 err_input:
173         kmscon_input_unref(input);
174 err_loop:
175         kmscon_eloop_unref(loop);
176 err_out:
177         return abs(ret);
178 }