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>
37 #include <X11/extensions/XKBcommon.h>
38 #include <X11/keysym.h>
44 static bool terminate;
46 static void sig_term(struct kmscon_signal *sig, int signum, void *data)
51 /* Pressing Ctrl-\ should toggle the capturing. */
52 static void sig_quit(struct kmscon_signal *sig, int signum, void *data)
54 struct kmscon_input *input = data;
56 if (kmscon_input_is_asleep(input)) {
57 kmscon_input_wake_up(input);
58 log_info("Woke Up\n");
60 kmscon_input_sleep(input);
61 log_info("Went to sleep\n");
65 static void print_modifiers(uint8_t mods)
67 if (mods & XKB_COMMON_SHIFT_MASK)
69 if (mods & XKB_COMMON_LOCK_MASK)
71 if (mods & XKB_COMMON_CONTROL_MASK)
73 if (mods & XKB_COMMON_MOD1_MASK)
75 if (mods & XKB_COMMON_MOD2_MASK)
77 if (mods & XKB_COMMON_MOD3_MASK)
79 if (mods & XKB_COMMON_MOD4_MASK)
81 if (mods & XKB_COMMON_MOD5_MASK)
86 static void input_arrived(struct kmscon_input *input,
87 struct kmscon_input_event *ev, void *data)
91 char utf8[MB_CUR_MAX + 1];
93 if (ev->unicode == KMSCON_INPUT_INVALID) {
94 xkb_keysym_to_string(ev->keysym, s, sizeof(s));
98 * Just a proof-of-concept hack. This works because glibc uses
99 * UTF-32 (= UCS-4) as the internal wchar_t encoding.
101 len = wctomb(utf8, (wchar_t)ev->unicode);
103 log_info("Bad unicode char\n");
109 printf("utf8 %s ", utf8);
111 print_modifiers(ev->modifiers);
114 int main(int argc, char **argv)
117 struct kmscon_eloop *loop;
118 struct kmscon_input *input;
119 struct kmscon_signal *sigint, *sigquit;
121 if (!setlocale(LC_ALL, "en_US.UTF-8")) {
122 log_err("Cannot set locale: %m\n");
127 ret = kmscon_eloop_new(&loop);
129 log_err("Cannot create eloop\n");
133 ret = kmscon_input_new(&input);
135 log_err("Cannot create input\n");
139 ret = kmscon_eloop_new_signal(loop, &sigint, SIGINT, sig_term, NULL);
141 log_err("Cannot add INT signal\n");
145 ret = kmscon_eloop_new_signal(loop, &sigquit, SIGQUIT, sig_quit, input);
147 log_err("Cannot add quit signal\n");
151 ret = kmscon_input_connect_eloop(input, loop, input_arrived, NULL);
153 log_err("Cannot connect input\n");
157 kmscon_input_wake_up(input);
159 system("stty -echo");
162 ret = kmscon_eloop_dispatch(loop, -1);
164 log_err("Dispatcher failed\n");
172 kmscon_eloop_rm_signal(sigquit);
174 kmscon_eloop_rm_signal(sigint);
176 kmscon_input_unref(input);
178 kmscon_eloop_unref(loop);