tests: add test for the input subsystem
[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 <signal.h>
29 #include <stdbool.h>
30 #include <stdio.h>
31 #include <stdlib.h>
32 #include <string.h>
33 #include <unistd.h>
34
35 #include "eloop.h"
36 #include "input.h"
37 #include "log.h"
38
39 static bool terminate;
40
41 static void sig_term(struct kmscon_signal *sig, int signum, void *data)
42 {
43         terminate = true;
44 }
45
46 /* Pressing Ctrl-\ should toggle the capturing. */
47 static void sig_quit(struct kmscon_signal *sig, int signum, 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 input_arrived(struct kmscon_input *input, uint16_t type,
61                                 uint16_t code, int32_t value, void *data)
62 {
63         printf("got (type %x) (code %x) (value %x)\n", type, code, value);
64 }
65
66 int main(int argc, char **argv)
67 {
68         int ret;
69         struct kmscon_eloop *loop;
70         struct kmscon_input *input;
71         struct kmscon_signal *sigint, *sigquit;
72
73         ret = kmscon_eloop_new(&loop);
74         if (ret) {
75                 log_err("Cannot create eloop\n");
76                 goto err_out;
77         }
78
79         ret = kmscon_input_new(&input);
80         if (ret) {
81                 log_err("Cannot create input\n");
82                 goto err_loop;
83         }
84
85         ret = kmscon_eloop_new_signal(loop, &sigint, SIGINT, sig_term, NULL);
86         if (ret) {
87                 log_err("Cannot add INT signal\n");
88                 goto err_input;
89         }
90
91         ret = kmscon_eloop_new_signal(loop, &sigquit, SIGQUIT, sig_quit, input);
92         if (ret) {
93                 log_err("Cannot add quit signal\n");
94                 goto err_sigint;
95         }
96
97         ret = kmscon_input_connect_eloop(input, loop, input_arrived, NULL);
98         if (ret) {
99                 log_err("Cannot connect input\n");
100                 goto err_sigquit;
101         }
102
103         kmscon_input_wake_up(input);
104
105         system("stty -echo");
106
107         while (!terminate) {
108                 ret = kmscon_eloop_dispatch(loop, -1);
109                 if (ret) {
110                         log_err("Dispatcher failed\n");
111                         break;
112                 }
113         }
114
115         system("stty echo");
116
117 err_sigquit:
118         kmscon_eloop_rm_signal(sigquit);
119 err_sigint:
120         kmscon_eloop_rm_signal(sigint);
121 err_input:
122         kmscon_input_unref(input);
123 err_loop:
124         kmscon_eloop_unref(loop);
125 err_out:
126         return abs(ret);
127 }