test_input: fix compilation with changed input API
[platform/upstream/kmscon.git] / tests / test_input.c
1 /*
2  * test_input - Test the input system - hotplug and keypresses
3  *
4  * Copyright (c) 2011 Ran Benita <ran234@gmail.com>
5  * Copyright (c) 2012 David Herrmann <dh.herrmann@googlemail.com>
6  *
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:
14  *
15  * The above copyright notice and this permission notice shall be included
16  * in all copies or substantial portions of the Software.
17  *
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.
25  */
26
27 static void print_help();
28
29 #include <errno.h>
30 #include <linux/input.h>
31 #include <locale.h>
32 #include <signal.h>
33 #include <stdbool.h>
34 #include <stdio.h>
35 #include <stdlib.h>
36 #include <string.h>
37 #include <sys/signalfd.h>
38 #include <unistd.h>
39 #include <X11/keysym.h>
40 #include "eloop.h"
41 #include "log.h"
42 #include "uterm.h"
43 #include "test_include.h"
44
45 static struct ev_eloop *eloop;
46 static struct uterm_input *input;
47
48 struct {
49         char *xkb_layout;
50         char *xkb_variant;
51         char *xkb_options;
52 } input_conf;
53
54 /* Pressing Ctrl-\ should toggle the capturing. */
55 static void sig_quit(struct ev_eloop *p,
56                         struct signalfd_siginfo *info,
57                         void *data)
58 {
59         if (!input)
60                 return;
61
62         if (uterm_input_is_awake(input)) {
63                 uterm_input_sleep(input);
64                 log_info("Went to sleep\n");
65         } else {
66                 uterm_input_wake_up(input);
67                 log_info("Woke Up\n");
68         }
69 }
70
71 static void print_modifiers(unsigned int mods)
72 {
73         if (mods & UTERM_SHIFT_MASK)
74                 printf("SHIFT ");
75         if (mods & UTERM_LOCK_MASK)
76                 printf("LOCK ");
77         if (mods & UTERM_CONTROL_MASK)
78                 printf("CONTROL ");
79         if (mods & UTERM_ALT_MASK)
80                 printf("ALT ");
81         if (mods & UTERM_LOGO_MASK)
82                 printf("LOGO ");
83         printf("\n");
84 }
85
86 static void input_arrived(struct uterm_input *input,
87                                 struct uterm_input_event *ev,
88                                 void *data)
89 {
90         char s[32];
91
92         uterm_input_keysym_to_string(input, ev->keysym, s, sizeof(s));
93         printf("sym %s ", s);
94         if (ev->unicode != UTERM_INPUT_INVALID) {
95                 /*
96                  * Just a proof-of-concept hack. This works because glibc uses
97                  * UTF-32 (= UCS-4) as the internal wchar_t encoding.
98                  */
99                 printf("unicode %lc ", ev->unicode);
100         }
101         print_modifiers(ev->mods);
102 }
103
104 static void monitor_event(struct uterm_monitor *mon,
105                                 struct uterm_monitor_event *ev,
106                                 void *data)
107 {
108         int ret;
109
110         if (ev->type == UTERM_MONITOR_NEW_SEAT) {
111                 if (strcmp(ev->seat_name, "seat0"))
112                         return;
113
114                 ret = uterm_input_new(&input, eloop,
115                                       input_conf.xkb_layout,
116                                       input_conf.xkb_variant,
117                                       input_conf.xkb_options);
118                 if (ret)
119                         return;
120                 ret = uterm_input_register_cb(input, input_arrived, NULL);
121                 if (ret)
122                         return;
123                 uterm_input_wake_up(input);
124         } else if (ev->type == UTERM_MONITOR_FREE_SEAT) {
125                 uterm_input_unregister_cb(input, input_arrived, NULL);
126                 uterm_input_unref(input);
127         } else if (ev->type == UTERM_MONITOR_NEW_DEV) {
128                 if (ev->dev_type == UTERM_MONITOR_INPUT)
129                         uterm_input_add_dev(input, ev->dev_node);
130         } else if (ev->type == UTERM_MONITOR_FREE_DEV) {
131                 if (ev->dev_type == UTERM_MONITOR_INPUT)
132                         uterm_input_remove_dev(input, ev->dev_node);
133         }
134 }
135
136 static void print_help()
137 {
138         /*
139          * Usage/Help information
140          * This should be scaled to a maximum of 80 characters per line:
141          *
142          * 80 char line:
143          *       |   10   |    20   |    30   |    40   |    50   |    60   |    70   |    80   |
144          *      "12345678901234567890123456789012345678901234567890123456789012345678901234567890\n"
145          * 80 char line starting with tab:
146          *       |10|    20   |    30   |    40   |    50   |    60   |    70   |    80   |
147          *      "\t901234567890123456789012345678901234567890123456789012345678901234567890\n"
148          */
149         fprintf(stderr,
150                 "Usage:\n"
151                 "\t%1$s [options]\n"
152                 "\t%1$s -h [options]\n"
153                 "\n"
154                 "You can prefix boolean options with \"no-\" to negate it. If an argument is\n"
155                 "given multiple times, only the last argument matters if not otherwise stated.\n"
156                 "\n"
157                 "General Options:\n"
158                 TEST_HELP
159                 "\n"
160                 "Input Device Options:\n"
161                 "\t    --xkb-layout <layout>   [us]    Set XkbLayout for input devices\n"
162                 "\t    --xkb-variant <variant> [-]     Set XkbVariant for input devices\n"
163                 "\t    --xkb-options <options> [-]     Set XkbOptions for input devices\n",
164                 "test_input");
165         /*
166          * 80 char line:
167          *       |   10   |    20   |    30   |    40   |    50   |    60   |    70   |    80   |
168          *      "12345678901234567890123456789012345678901234567890123456789012345678901234567890\n"
169          * 80 char line starting with tab:
170          *       |10|    20   |    30   |    40   |    50   |    60   |    70   |    80   |
171          *      "\t901234567890123456789012345678901234567890123456789012345678901234567890\n"
172          */
173 }
174
175 struct conf_option options[] = {
176         TEST_OPTIONS,
177         CONF_OPTION_STRING(0, "xkb-layout", NULL, &input_conf.xkb_layout, "us"),
178         CONF_OPTION_STRING(0, "xkb-variant", NULL, &input_conf.xkb_variant, ""),
179         CONF_OPTION_STRING(0, "xkb-options", NULL, &input_conf.xkb_options, ""),
180 };
181
182 int main(int argc, char **argv)
183 {
184         int ret;
185         struct uterm_monitor *mon;
186         size_t onum;
187
188         onum = sizeof(options) / sizeof(*options);
189         ret = test_prepare(options, onum, argc, argv, &eloop);
190         if (ret)
191                 goto err_fail;
192
193         if (!setlocale(LC_ALL, "")) {
194                 log_err("Cannot set locale: %m");
195                 ret = -EFAULT;
196                 goto err_exit;
197         }
198
199         ret = uterm_monitor_new(&mon, eloop, monitor_event, NULL);
200         if (ret)
201                 goto err_exit;
202
203         ret = ev_eloop_register_signal_cb(eloop, SIGQUIT, sig_quit, NULL);
204         if (ret)
205                 goto err_mon;
206
207         system("stty -echo");
208         uterm_monitor_scan(mon);
209         ev_eloop_run(eloop, -1);
210         system("stty echo");
211
212         ev_eloop_unregister_signal_cb(eloop, SIGQUIT, sig_quit, NULL);
213 err_mon:
214         uterm_monitor_unref(mon);
215 err_exit:
216         test_exit(options, onum, eloop);
217 err_fail:
218         if (ret != -ECANCELED)
219                 test_fail(ret);
220         return abs(ret);
221 }