eloop: move prefix to "ev_" instead of "kmscon_"
[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 <unistd.h>
35
36 #include <X11/keysym.h>
37
38 #include "eloop.h"
39 #include "input.h"
40 #include "kbd.h"
41 #include "log.h"
42
43 static bool terminate;
44
45 static void sig_term(struct ev_signal *sig, int signum, void *data)
46 {
47         terminate = true;
48 }
49
50 /* Pressing Ctrl-\ should toggle the capturing. */
51 static void sig_quit(struct ev_signal *sig, int signum, void *data)
52 {
53         struct kmscon_input *input = data;
54
55         if (kmscon_input_is_asleep(input)) {
56                 kmscon_input_wake_up(input);
57                 log_info("Woke Up\n");
58         } else {
59                 kmscon_input_sleep(input);
60                 log_info("Went to sleep\n");
61         }
62 }
63
64 static void print_modifiers(unsigned int mods)
65 {
66         if (mods & KMSCON_SHIFT_MASK)
67                 printf("SHIFT ");
68         if (mods & KMSCON_LOCK_MASK)
69                 printf("LOCK ");
70         if (mods & KMSCON_CONTROL_MASK)
71                 printf("CONTROL ");
72         if (mods & KMSCON_MOD1_MASK)
73                 printf("MOD1 ");
74         if (mods & KMSCON_MOD2_MASK)
75                 printf("MOD2 ");
76         if (mods & KMSCON_MOD3_MASK)
77                 printf("MOD3 ");
78         if (mods & KMSCON_MOD4_MASK)
79                 printf("MOD4 ");
80         if (mods & KMSCON_MOD5_MASK)
81                 printf("MOD5 ");
82         printf("\n");
83 }
84
85 static void input_arrived(struct kmscon_input *input,
86                                 struct kmscon_input_event *ev, void *data)
87 {
88         char s[16];
89
90         if (ev->unicode == KMSCON_INPUT_INVALID) {
91                 kmscon_kbd_keysym_to_string(ev->keysym, s, sizeof(s));
92                 printf("sym %s ", s);
93         } else {
94                 /*
95                  * Just a proof-of-concept hack. This works because glibc uses
96                  * UTF-32 (= UCS-4) as the internal wchar_t encoding.
97                  */
98                 printf("unicode %lc ", ev->unicode);
99         }
100         print_modifiers(ev->mods);
101 }
102
103 int main(int argc, char **argv)
104 {
105         int ret;
106         struct ev_eloop *loop;
107         struct kmscon_input *input;
108         struct ev_signal *sigint, *sigquit;
109
110         if (!setlocale(LC_ALL, "")) {
111                 log_err("Cannot set locale: %m\n");
112                 ret = -EFAULT;
113                 goto err_out;
114         }
115
116         ret = ev_eloop_new(&loop);
117         if (ret) {
118                 log_err("Cannot create eloop\n");
119                 goto err_out;
120         }
121
122         ret = kmscon_input_new(&input);
123         if (ret) {
124                 log_err("Cannot create input\n");
125                 goto err_loop;
126         }
127
128         ret = ev_eloop_new_signal(loop, &sigint, SIGINT, sig_term, NULL);
129         if (ret) {
130                 log_err("Cannot add INT signal\n");
131                 goto err_input;
132         }
133
134         ret = ev_eloop_new_signal(loop, &sigquit, SIGQUIT, sig_quit, input);
135         if (ret) {
136                 log_err("Cannot add quit signal\n");
137                 goto err_sigint;
138         }
139
140         ret = kmscon_input_connect_eloop(input, loop, input_arrived, NULL);
141         if (ret) {
142                 log_err("Cannot connect input\n");
143                 goto err_sigquit;
144         }
145
146         kmscon_input_wake_up(input);
147
148         system("stty -echo");
149
150         while (!terminate) {
151                 ret = ev_eloop_dispatch(loop, -1);
152                 if (ret) {
153                         log_err("Dispatcher failed\n");
154                         break;
155                 }
156         }
157
158         system("stty echo");
159
160 err_sigquit:
161         ev_eloop_rm_signal(sigquit);
162 err_sigint:
163         ev_eloop_rm_signal(sigint);
164 err_input:
165         kmscon_input_unref(input);
166 err_loop:
167         ev_eloop_unref(loop);
168 err_out:
169         return abs(ret);
170 }