test_input: use new test_include.h infrastructure
[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  *
6  * Permission is hereby granted, free of charge, to any person obtaining
7  * a copy of this software and associated documentation files
8  * (the "Software"), to deal in the Software without restriction, including
9  * without limitation the rights to use, copy, modify, merge, publish,
10  * distribute, sublicense, and/or sell copies of the Software, and to
11  * permit persons to whom the Software is furnished to do so, subject to
12  * the following conditions:
13  *
14  * The above copyright notice and this permission notice shall be included
15  * in all copies or substantial portions of the Software.
16  *
17  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
18  * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
19  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
20  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
21  * CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
22  * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
23  * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
24  */
25
26 #include <errno.h>
27 #include <linux/input.h>
28 #include <locale.h>
29 #include <signal.h>
30 #include <stdbool.h>
31 #include <stdio.h>
32 #include <stdlib.h>
33 #include <string.h>
34 #include <sys/signalfd.h>
35 #include <unistd.h>
36
37 #include <X11/keysym.h>
38
39 #include "eloop.h"
40 #include "input.h"
41 #include "kbd.h"
42 #include "log.h"
43 #include "test_include.h"
44
45 /* Pressing Ctrl-\ should toggle the capturing. */
46 static void sig_quit(struct ev_eloop *p, struct signalfd_siginfo *info,
47                         void *data)
48 {
49         struct kmscon_input *input = data;
50
51         if (kmscon_input_is_asleep(input)) {
52                 kmscon_input_wake_up(input);
53                 log_info("Woke Up\n");
54         } else {
55                 kmscon_input_sleep(input);
56                 log_info("Went to sleep\n");
57         }
58 }
59
60 static void print_modifiers(unsigned int mods)
61 {
62         if (mods & KMSCON_SHIFT_MASK)
63                 printf("SHIFT ");
64         if (mods & KMSCON_LOCK_MASK)
65                 printf("LOCK ");
66         if (mods & KMSCON_CONTROL_MASK)
67                 printf("CONTROL ");
68         if (mods & KMSCON_MOD1_MASK)
69                 printf("MOD1 ");
70         if (mods & KMSCON_MOD2_MASK)
71                 printf("MOD2 ");
72         if (mods & KMSCON_MOD3_MASK)
73                 printf("MOD3 ");
74         if (mods & KMSCON_MOD4_MASK)
75                 printf("MOD4 ");
76         if (mods & KMSCON_MOD5_MASK)
77                 printf("MOD5 ");
78         printf("\n");
79 }
80
81 static void input_arrived(struct kmscon_input *input,
82                                 struct kmscon_input_event *ev, void *data)
83 {
84         char s[16];
85
86         if (ev->unicode == KMSCON_INPUT_INVALID) {
87                 kmscon_kbd_keysym_to_string(ev->keysym, s, sizeof(s));
88                 printf("sym %s ", s);
89         } else {
90                 /*
91                  * Just a proof-of-concept hack. This works because glibc uses
92                  * UTF-32 (= UCS-4) as the internal wchar_t encoding.
93                  */
94                 printf("unicode %lc ", ev->unicode);
95         }
96         print_modifiers(ev->mods);
97 }
98
99 int main(int argc, char **argv)
100 {
101         int ret;
102         struct ev_eloop *eloop;
103         struct kmscon_input *input;
104
105         ret = test_prepare(argc, argv, &eloop);
106         if (ret)
107                 goto err_fail;
108
109         if (!setlocale(LC_ALL, "")) {
110                 log_err("Cannot set locale: %m");
111                 ret = -EFAULT;
112                 goto err_exit;
113         }
114
115         ret = kmscon_input_new(&input);
116         if (ret)
117                 goto err_exit;
118
119         ret = ev_eloop_register_signal_cb(eloop, SIGQUIT, sig_quit, input);
120         if (ret)
121                 goto err_input;
122
123         ret = kmscon_input_connect_eloop(input, eloop);
124         if (ret)
125                 goto err_sigquit;
126
127         ret = kmscon_input_register_cb(input, input_arrived, NULL);
128         if (ret)
129                 goto err_sigquit;
130
131         kmscon_input_wake_up(input);
132
133         system("stty -echo");
134         ev_eloop_run(eloop, -1);
135         system("stty echo");
136
137         kmscon_input_unregister_cb(input, input_arrived, NULL);
138 err_sigquit:
139         ev_eloop_unregister_signal_cb(eloop, SIGQUIT, sig_quit, input);
140 err_input:
141         kmscon_input_unref(input);
142 err_exit:
143         test_exit(eloop);
144 err_fail:
145         test_fail(ret);
146         return abs(ret);
147 }