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