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