2 * test_input - Test the input system - hotplug and keypresses
4 * Copyright (c) 2011 Ran Benita <ran234@gmail.com>
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:
14 * The above copyright notice and this permission notice shall be included
15 * in all copies or substantial portions of the Software.
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.
27 #include <linux/input.h>
41 static bool terminate;
43 static void sig_term(struct kmscon_signal *sig, int signum, void *data)
48 /* Pressing Ctrl-\ should toggle the capturing. */
49 static void sig_quit(struct kmscon_signal *sig, int signum, void *data)
51 struct kmscon_input *input = data;
53 if (kmscon_input_is_asleep(input)) {
54 kmscon_input_wake_up(input);
55 log_info("Woke Up\n");
57 kmscon_input_sleep(input);
58 log_info("Went to sleep\n");
62 static void print_modifiers(uint8_t mods)
64 if (mods & XKB_COMMON_SHIFT_MASK)
66 if (mods & XKB_COMMON_LOCK_MASK)
68 if (mods & XKB_COMMON_CONTROL_MASK)
70 if (mods & XKB_COMMON_MOD1_MASK)
72 if (mods & XKB_COMMON_MOD2_MASK)
74 if (mods & XKB_COMMON_MOD3_MASK)
76 if (mods & XKB_COMMON_MOD4_MASK)
78 if (mods & XKB_COMMON_MOD5_MASK)
83 static void input_arrived(struct kmscon_input *input,
84 struct kmscon_input_event *ev, void *data)
88 char utf8[MB_CUR_MAX + 1];
90 if (ev->unicode == 0) {
91 xkb_keysym_to_string(ev->keysym, s, sizeof(s));
95 * Just a proof-of-concept hack. This works because glibc uses
96 * UTF-32 (= UCS-4) as the internal wchar_t encoding.
98 len = wctomb(utf8, (wchar_t)ev->unicode);
100 log_info("Bad unicode char\n");
106 printf("utf8 %s ", utf8);
108 print_modifiers(ev->modifiers);
111 int main(int argc, char **argv)
114 struct kmscon_eloop *loop;
115 struct kmscon_input *input;
116 struct kmscon_signal *sigint, *sigquit;
118 if (!setlocale(LC_ALL, "en_US.UTF-8")) {
119 log_err("Cannot set locale: %m\n");
124 ret = kmscon_eloop_new(&loop);
126 log_err("Cannot create eloop\n");
130 ret = kmscon_input_new(&input);
132 log_err("Cannot create input\n");
136 ret = kmscon_eloop_new_signal(loop, &sigint, SIGINT, sig_term, NULL);
138 log_err("Cannot add INT signal\n");
142 ret = kmscon_eloop_new_signal(loop, &sigquit, SIGQUIT, sig_quit, input);
144 log_err("Cannot add quit signal\n");
148 ret = kmscon_input_connect_eloop(input, loop, input_arrived, NULL);
150 log_err("Cannot connect input\n");
154 kmscon_input_wake_up(input);
156 system("stty -echo");
159 ret = kmscon_eloop_dispatch(loop, -1);
161 log_err("Dispatcher failed\n");
169 kmscon_eloop_rm_signal(sigquit);
171 kmscon_eloop_rm_signal(sigint);
173 kmscon_input_unref(input);
175 kmscon_eloop_unref(loop);