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>
34 #include <sys/signalfd.h>
37 #include <X11/keysym.h>
43 #include "test_include.h"
45 /* Pressing Ctrl-\ should toggle the capturing. */
46 static void sig_quit(struct ev_eloop *p, struct signalfd_siginfo *info,
49 struct kmscon_input *input = data;
51 if (kmscon_input_is_asleep(input)) {
52 kmscon_input_wake_up(input);
53 log_info("Woke Up\n");
55 kmscon_input_sleep(input);
56 log_info("Went to sleep\n");
60 static void print_modifiers(unsigned int mods)
62 if (mods & KMSCON_SHIFT_MASK)
64 if (mods & KMSCON_LOCK_MASK)
66 if (mods & KMSCON_CONTROL_MASK)
68 if (mods & KMSCON_MOD1_MASK)
70 if (mods & KMSCON_MOD2_MASK)
72 if (mods & KMSCON_MOD3_MASK)
74 if (mods & KMSCON_MOD4_MASK)
76 if (mods & KMSCON_MOD5_MASK)
81 static void input_arrived(struct kmscon_input *input,
82 struct kmscon_input_event *ev, void *data)
86 if (ev->unicode == KMSCON_INPUT_INVALID) {
87 kmscon_kbd_keysym_to_string(ev->keysym, s, sizeof(s));
91 * Just a proof-of-concept hack. This works because glibc uses
92 * UTF-32 (= UCS-4) as the internal wchar_t encoding.
94 printf("unicode %lc ", ev->unicode);
96 print_modifiers(ev->mods);
99 int main(int argc, char **argv)
102 struct ev_eloop *eloop;
103 struct kmscon_input *input;
105 ret = test_prepare(argc, argv, &eloop);
109 if (!setlocale(LC_ALL, "")) {
110 log_err("Cannot set locale: %m");
115 ret = kmscon_input_new(&input);
119 ret = ev_eloop_register_signal_cb(eloop, SIGQUIT, sig_quit, input);
123 ret = kmscon_input_connect_eloop(input, eloop);
127 ret = kmscon_input_register_cb(input, input_arrived, NULL);
131 kmscon_input_wake_up(input);
133 system("stty -echo");
134 ev_eloop_run(eloop, -1);
137 kmscon_input_unregister_cb(input, input_arrived, NULL);
139 ev_eloop_unregister_signal_cb(eloop, SIGQUIT, sig_quit, input);
141 kmscon_input_unref(input);