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.
27 static void print_help();
30 #include <linux/input.h>
37 #include <sys/signalfd.h>
39 #include <X11/keysym.h>
40 #include <xkbcommon/xkbcommon.h>
44 #include "test_include.h"
46 static struct ev_eloop *eloop;
47 static struct uterm_input *input;
56 /* Pressing Ctrl-\ should toggle the capturing. */
57 static void sig_quit(struct ev_eloop *p,
58 struct signalfd_siginfo *info,
64 if (uterm_input_is_awake(input)) {
65 uterm_input_sleep(input);
66 log_info("Went to sleep\n");
68 uterm_input_wake_up(input);
69 log_info("Woke Up\n");
73 static void print_modifiers(unsigned int mods)
75 if (mods & UTERM_SHIFT_MASK)
77 if (mods & UTERM_LOCK_MASK)
79 if (mods & UTERM_CONTROL_MASK)
81 if (mods & UTERM_ALT_MASK)
83 if (mods & UTERM_LOGO_MASK)
88 static void input_arrived(struct uterm_input *input,
89 struct uterm_input_event *ev,
94 xkb_keysym_get_name(ev->keysyms[0], s, sizeof(s));
96 if (ev->codepoints[0] != UTERM_INPUT_INVALID) {
98 * Just a proof-of-concept hack. This works because glibc uses
99 * UTF-32 (= UCS-4) as the internal wchar_t encoding.
101 printf("unicode %lc ", ev->codepoints[0]);
103 print_modifiers(ev->mods);
106 static void monitor_event(struct uterm_monitor *mon,
107 struct uterm_monitor_event *ev,
112 if (ev->type == UTERM_MONITOR_NEW_SEAT) {
113 if (strcmp(ev->seat_name, "seat0"))
116 ret = uterm_input_new(&input, eloop,
117 input_conf.xkb_model,
118 input_conf.xkb_layout,
119 input_conf.xkb_variant,
120 input_conf.xkb_options,
124 ret = uterm_input_register_cb(input, input_arrived, NULL);
127 uterm_input_wake_up(input);
128 } else if (ev->type == UTERM_MONITOR_FREE_SEAT) {
129 uterm_input_unregister_cb(input, input_arrived, NULL);
130 uterm_input_unref(input);
131 } else if (ev->type == UTERM_MONITOR_NEW_DEV) {
132 if (ev->dev_type == UTERM_MONITOR_INPUT)
133 uterm_input_add_dev(input, ev->dev_node);
134 } else if (ev->type == UTERM_MONITOR_FREE_DEV) {
135 if (ev->dev_type == UTERM_MONITOR_INPUT)
136 uterm_input_remove_dev(input, ev->dev_node);
140 static void print_help()
143 * Usage/Help information
144 * This should be scaled to a maximum of 80 characters per line:
147 * | 10 | 20 | 30 | 40 | 50 | 60 | 70 | 80 |
148 * "12345678901234567890123456789012345678901234567890123456789012345678901234567890\n"
149 * 80 char line starting with tab:
150 * |10| 20 | 30 | 40 | 50 | 60 | 70 | 80 |
151 * "\t901234567890123456789012345678901234567890123456789012345678901234567890\n"
156 "\t%1$s -h [options]\n"
158 "You can prefix boolean options with \"no-\" to negate it. If an argument is\n"
159 "given multiple times, only the last argument matters if not otherwise stated.\n"
164 "Input Device Options:\n"
165 "\t --xkb-model <model> [-] Set XkbModel for input devices\n"
166 "\t --xkb-layout <layout> [-] Set XkbLayout for input devices\n"
167 "\t --xkb-variant <variant> [-] Set XkbVariant for input devices\n"
168 "\t --xkb-options <options> [-] Set XkbOptions for input devices\n",
172 * | 10 | 20 | 30 | 40 | 50 | 60 | 70 | 80 |
173 * "12345678901234567890123456789012345678901234567890123456789012345678901234567890\n"
174 * 80 char line starting with tab:
175 * |10| 20 | 30 | 40 | 50 | 60 | 70 | 80 |
176 * "\t901234567890123456789012345678901234567890123456789012345678901234567890\n"
180 struct conf_option options[] = {
182 CONF_OPTION_STRING(0, "xkb-model", &input_conf.xkb_model, ""),
183 CONF_OPTION_STRING(0, "xkb-layout", &input_conf.xkb_layout, ""),
184 CONF_OPTION_STRING(0, "xkb-variant", &input_conf.xkb_variant, ""),
185 CONF_OPTION_STRING(0, "xkb-options", &input_conf.xkb_options, ""),
188 int main(int argc, char **argv)
191 struct uterm_monitor *mon;
194 onum = sizeof(options) / sizeof(*options);
195 ret = test_prepare(options, onum, argc, argv, &eloop);
199 if (!setlocale(LC_ALL, "")) {
200 log_err("Cannot set locale: %m");
205 ret = uterm_monitor_new(&mon, eloop, monitor_event, NULL);
209 ret = ev_eloop_register_signal_cb(eloop, SIGQUIT, sig_quit, NULL);
213 system("stty -echo");
214 uterm_monitor_scan(mon);
215 ev_eloop_run(eloop, -1);
218 ev_eloop_unregister_signal_cb(eloop, SIGQUIT, sig_quit, NULL);
220 uterm_monitor_unref(mon);
222 test_exit(options, onum, eloop);
224 if (ret != -ECANCELED)