2 * test_input - Test the input system - hotplug and keypresses
4 * Copyright (c) 2011 Ran Benita <ran234@gmail.com>
5 * Copyright (c) 2012 David Herrmann <dh.herrmann@googlemail.com>
7 * Permission is hereby granted, free of charge, to any person obtaining
8 * a copy of this software and associated documentation files
9 * (the "Software"), to deal in the Software without restriction, including
10 * without limitation the rights to use, copy, modify, merge, publish,
11 * distribute, sublicense, and/or sell copies of the Software, and to
12 * permit persons to whom the Software is furnished to do so, subject to
13 * the following conditions:
15 * The above copyright notice and this permission notice shall be included
16 * in all copies or substantial portions of the Software.
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
21 * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
22 * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
28 #include <linux/input.h>
35 #include <sys/signalfd.h>
37 #include <X11/keysym.h>
41 #include "test_include.h"
43 static struct ev_eloop *eloop;
44 static struct uterm_input *input;
46 /* Pressing Ctrl-\ should toggle the capturing. */
47 static void sig_quit(struct ev_eloop *p,
48 struct signalfd_siginfo *info,
54 if (uterm_input_is_awake(input)) {
55 uterm_input_sleep(input);
56 log_info("Went to sleep\n");
58 uterm_input_wake_up(input);
59 log_info("Woke Up\n");
63 static void print_modifiers(unsigned int mods)
65 if (mods & UTERM_SHIFT_MASK)
67 if (mods & UTERM_LOCK_MASK)
69 if (mods & UTERM_CONTROL_MASK)
71 if (mods & UTERM_MOD1_MASK)
73 if (mods & UTERM_MOD2_MASK)
75 if (mods & UTERM_MOD3_MASK)
77 if (mods & UTERM_MOD4_MASK)
79 if (mods & UTERM_MOD5_MASK)
84 static void input_arrived(struct uterm_input *input,
85 struct uterm_input_event *ev,
90 uterm_input_keysym_to_string(input, ev->keysym, s, sizeof(s));
92 if (ev->unicode != UTERM_INPUT_INVALID) {
94 * Just a proof-of-concept hack. This works because glibc uses
95 * UTF-32 (= UCS-4) as the internal wchar_t encoding.
97 printf("unicode %lc ", ev->unicode);
99 print_modifiers(ev->mods);
102 static void monitor_event(struct uterm_monitor *mon,
103 struct uterm_monitor_event *ev,
108 if (ev->type == UTERM_MONITOR_NEW_SEAT) {
109 if (strcmp(ev->seat_name, "seat0"))
112 ret = uterm_input_new(&input, eloop);
115 ret = uterm_input_register_cb(input, input_arrived, NULL);
118 uterm_input_wake_up(input);
119 } else if (ev->type == UTERM_MONITOR_FREE_SEAT) {
120 uterm_input_unregister_cb(input, input_arrived, NULL);
121 uterm_input_unref(input);
122 } else if (ev->type == UTERM_MONITOR_NEW_DEV) {
123 if (ev->dev_type == UTERM_MONITOR_INPUT)
124 uterm_input_add_dev(input, ev->dev_node);
125 } else if (ev->type == UTERM_MONITOR_FREE_DEV) {
126 if (ev->dev_type == UTERM_MONITOR_INPUT)
127 uterm_input_remove_dev(input, ev->dev_node);
131 int main(int argc, char **argv)
134 struct uterm_monitor *mon;
136 ret = test_prepare(argc, argv, &eloop);
140 if (!setlocale(LC_ALL, "")) {
141 log_err("Cannot set locale: %m");
146 ret = uterm_monitor_new(&mon, eloop, monitor_event, NULL);
150 ret = ev_eloop_register_signal_cb(eloop, SIGQUIT, sig_quit, NULL);
154 system("stty -echo");
155 uterm_monitor_scan(mon);
156 ev_eloop_run(eloop, -1);
159 ev_eloop_unregister_signal_cb(eloop, SIGQUIT, sig_quit, NULL);
161 uterm_monitor_unref(mon);