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>
36 #include <X11/keysym.h>
43 static bool terminate;
45 static void sig_term(struct ev_signal *sig, int signum, void *data)
50 /* Pressing Ctrl-\ should toggle the capturing. */
51 static void sig_quit(struct ev_signal *sig, int signum, void *data)
53 struct kmscon_input *input = data;
55 if (kmscon_input_is_asleep(input)) {
56 kmscon_input_wake_up(input);
57 log_info("Woke Up\n");
59 kmscon_input_sleep(input);
60 log_info("Went to sleep\n");
64 static void print_modifiers(unsigned int mods)
66 if (mods & KMSCON_SHIFT_MASK)
68 if (mods & KMSCON_LOCK_MASK)
70 if (mods & KMSCON_CONTROL_MASK)
72 if (mods & KMSCON_MOD1_MASK)
74 if (mods & KMSCON_MOD2_MASK)
76 if (mods & KMSCON_MOD3_MASK)
78 if (mods & KMSCON_MOD4_MASK)
80 if (mods & KMSCON_MOD5_MASK)
85 static void input_arrived(struct kmscon_input *input,
86 struct kmscon_input_event *ev, void *data)
90 if (ev->unicode == KMSCON_INPUT_INVALID) {
91 kmscon_kbd_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 printf("unicode %lc ", ev->unicode);
100 print_modifiers(ev->mods);
103 int main(int argc, char **argv)
106 struct ev_eloop *loop;
107 struct kmscon_input *input;
108 struct ev_signal *sigint, *sigquit;
110 if (!setlocale(LC_ALL, "")) {
111 log_err("Cannot set locale: %m\n");
116 ret = ev_eloop_new(&loop);
118 log_err("Cannot create eloop\n");
122 ret = kmscon_input_new(&input);
124 log_err("Cannot create input\n");
128 ret = ev_eloop_new_signal(loop, &sigint, SIGINT, sig_term, NULL);
130 log_err("Cannot add INT signal\n");
134 ret = ev_eloop_new_signal(loop, &sigquit, SIGQUIT, sig_quit, input);
136 log_err("Cannot add quit signal\n");
140 ret = kmscon_input_connect_eloop(input, loop, input_arrived, NULL);
142 log_err("Cannot connect input\n");
146 kmscon_input_wake_up(input);
148 system("stty -echo");
151 ret = ev_eloop_dispatch(loop, -1);
153 log_err("Dispatcher failed\n");
161 ev_eloop_rm_signal(sigquit);
163 ev_eloop_rm_signal(sigint);
165 kmscon_input_unref(input);
167 ev_eloop_unref(loop);