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 extern void kbd_keysym_to_string(uint32_t keysym, char *str, size_t size);
45 static struct ev_eloop *eloop;
46 static struct uterm_input *input;
48 /* Pressing Ctrl-\ should toggle the capturing. */
49 static void sig_quit(struct ev_eloop *p,
50 struct signalfd_siginfo *info,
56 if (uterm_input_is_awake(input)) {
57 uterm_input_sleep(input);
58 log_info("Went to sleep\n");
60 uterm_input_wake_up(input);
61 log_info("Woke Up\n");
65 static void print_modifiers(unsigned int mods)
67 if (mods & UTERM_SHIFT_MASK)
69 if (mods & UTERM_LOCK_MASK)
71 if (mods & UTERM_CONTROL_MASK)
73 if (mods & UTERM_MOD1_MASK)
75 if (mods & UTERM_MOD2_MASK)
77 if (mods & UTERM_MOD3_MASK)
79 if (mods & UTERM_MOD4_MASK)
81 if (mods & UTERM_MOD5_MASK)
86 static void input_arrived(struct uterm_input *input,
87 struct uterm_input_event *ev,
92 kbd_keysym_to_string(ev->keysym, s, sizeof(s));
94 if (ev->unicode != UTERM_INPUT_INVALID) {
96 * Just a proof-of-concept hack. This works because glibc uses
97 * UTF-32 (= UCS-4) as the internal wchar_t encoding.
99 printf("unicode %lc ", ev->unicode);
101 print_modifiers(ev->mods);
104 static void monitor_event(struct uterm_monitor *mon,
105 struct uterm_monitor_event *ev,
110 if (ev->type == UTERM_MONITOR_NEW_SEAT) {
111 if (strcmp(ev->seat_name, "seat0"))
114 ret = uterm_input_new(&input, eloop);
117 ret = uterm_input_register_cb(input, input_arrived, NULL);
120 uterm_input_wake_up(input);
121 } else if (ev->type == UTERM_MONITOR_FREE_SEAT) {
122 uterm_input_unregister_cb(input, input_arrived, NULL);
123 uterm_input_unref(input);
124 } else if (ev->type == UTERM_MONITOR_NEW_DEV) {
125 if (ev->dev_type == UTERM_MONITOR_INPUT)
126 uterm_input_add_dev(input, ev->dev_node);
127 } else if (ev->type == UTERM_MONITOR_FREE_DEV) {
128 if (ev->dev_type == UTERM_MONITOR_INPUT)
129 uterm_input_remove_dev(input, ev->dev_node);
133 int main(int argc, char **argv)
136 struct uterm_monitor *mon;
138 ret = test_prepare(argc, argv, &eloop);
142 if (!setlocale(LC_ALL, "")) {
143 log_err("Cannot set locale: %m");
148 ret = uterm_monitor_new(&mon, eloop, monitor_event, NULL);
152 ret = ev_eloop_register_signal_cb(eloop, SIGQUIT, sig_quit, NULL);
156 system("stty -echo");
157 uterm_monitor_scan(mon);
158 ev_eloop_run(eloop, -1);
161 ev_eloop_unregister_signal_cb(eloop, SIGQUIT, sig_quit, NULL);
163 uterm_monitor_unref(mon);