test_input: fix compilation for kbd_keysym_to_string()
[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 #include <errno.h>
28 #include <linux/input.h>
29 #include <locale.h>
30 #include <signal.h>
31 #include <stdbool.h>
32 #include <stdio.h>
33 #include <stdlib.h>
34 #include <string.h>
35 #include <sys/signalfd.h>
36 #include <unistd.h>
37 #include <X11/keysym.h>
38 #include "eloop.h"
39 #include "log.h"
40 #include "uterm.h"
41 #include "test_include.h"
42
43 static struct ev_eloop *eloop;
44 static struct uterm_input *input;
45
46 /* Pressing Ctrl-\ should toggle the capturing. */
47 static void sig_quit(struct ev_eloop *p,
48                         struct signalfd_siginfo *info,
49                         void *data)
50 {
51         if (!input)
52                 return;
53
54         if (uterm_input_is_awake(input)) {
55                 uterm_input_sleep(input);
56                 log_info("Went to sleep\n");
57         } else {
58                 uterm_input_wake_up(input);
59                 log_info("Woke Up\n");
60         }
61 }
62
63 static void print_modifiers(unsigned int mods)
64 {
65         if (mods & UTERM_SHIFT_MASK)
66                 printf("SHIFT ");
67         if (mods & UTERM_LOCK_MASK)
68                 printf("LOCK ");
69         if (mods & UTERM_CONTROL_MASK)
70                 printf("CONTROL ");
71         if (mods & UTERM_MOD1_MASK)
72                 printf("MOD1 ");
73         if (mods & UTERM_MOD2_MASK)
74                 printf("MOD2 ");
75         if (mods & UTERM_MOD3_MASK)
76                 printf("MOD3 ");
77         if (mods & UTERM_MOD4_MASK)
78                 printf("MOD4 ");
79         if (mods & UTERM_MOD5_MASK)
80                 printf("MOD5 ");
81         printf("\n");
82 }
83
84 static void input_arrived(struct uterm_input *input,
85                                 struct uterm_input_event *ev,
86                                 void *data)
87 {
88         char s[32];
89
90         uterm_input_keysym_to_string(input, ev->keysym, s, sizeof(s));
91         printf("sym %s ", s);
92         if (ev->unicode != UTERM_INPUT_INVALID) {
93                 /*
94                  * Just a proof-of-concept hack. This works because glibc uses
95                  * UTF-32 (= UCS-4) as the internal wchar_t encoding.
96                  */
97                 printf("unicode %lc ", ev->unicode);
98         }
99         print_modifiers(ev->mods);
100 }
101
102 static void monitor_event(struct uterm_monitor *mon,
103                                 struct uterm_monitor_event *ev,
104                                 void *data)
105 {
106         int ret;
107
108         if (ev->type == UTERM_MONITOR_NEW_SEAT) {
109                 if (strcmp(ev->seat_name, "seat0"))
110                         return;
111
112                 ret = uterm_input_new(&input, eloop);
113                 if (ret)
114                         return;
115                 ret = uterm_input_register_cb(input, input_arrived, NULL);
116                 if (ret)
117                         return;
118                 uterm_input_wake_up(input);
119         } else if (ev->type == UTERM_MONITOR_FREE_SEAT) {
120                 uterm_input_unregister_cb(input, input_arrived, NULL);
121                 uterm_input_unref(input);
122         } else if (ev->type == UTERM_MONITOR_NEW_DEV) {
123                 if (ev->dev_type == UTERM_MONITOR_INPUT)
124                         uterm_input_add_dev(input, ev->dev_node);
125         } else if (ev->type == UTERM_MONITOR_FREE_DEV) {
126                 if (ev->dev_type == UTERM_MONITOR_INPUT)
127                         uterm_input_remove_dev(input, ev->dev_node);
128         }
129 }
130
131 int main(int argc, char **argv)
132 {
133         int ret;
134         struct uterm_monitor *mon;
135
136         ret = test_prepare(argc, argv, &eloop);
137         if (ret)
138                 goto err_fail;
139
140         if (!setlocale(LC_ALL, "")) {
141                 log_err("Cannot set locale: %m");
142                 ret = -EFAULT;
143                 goto err_exit;
144         }
145
146         ret = uterm_monitor_new(&mon, eloop, monitor_event, NULL);
147         if (ret)
148                 goto err_exit;
149
150         ret = ev_eloop_register_signal_cb(eloop, SIGQUIT, sig_quit, NULL);
151         if (ret)
152                 goto err_mon;
153
154         system("stty -echo");
155         uterm_monitor_scan(mon);
156         ev_eloop_run(eloop, -1);
157         system("stty echo");
158
159         ev_eloop_unregister_signal_cb(eloop, SIGQUIT, sig_quit, NULL);
160 err_mon:
161         uterm_monitor_unref(mon);
162 err_exit:
163         test_exit(eloop);
164 err_fail:
165         test_fail(ret);
166         return abs(ret);
167 }