xref: /aosp_15_r20/external/ltp/testcases/kernel/input/input05.c (revision 49cdfc7efb34551c7342be41a7384b9c40d7cab7)
1*49cdfc7eSAndroid Build Coastguard Worker /*
2*49cdfc7eSAndroid Build Coastguard Worker  * Copyright (c) 2015 Cedric Hnyda <[email protected]>
3*49cdfc7eSAndroid Build Coastguard Worker  *
4*49cdfc7eSAndroid Build Coastguard Worker  * This program is free software; you can redistribute it and/or
5*49cdfc7eSAndroid Build Coastguard Worker  * modify it under the terms of the GNU General Public License as
6*49cdfc7eSAndroid Build Coastguard Worker  * published by the Free Software Foundation; either version 2 of
7*49cdfc7eSAndroid Build Coastguard Worker  * the License, or (at your option) any later version.
8*49cdfc7eSAndroid Build Coastguard Worker  *
9*49cdfc7eSAndroid Build Coastguard Worker  * This program is distributed in the hope that it would be useful,
10*49cdfc7eSAndroid Build Coastguard Worker  * but WITHOUT ANY WARRANTY; without even the implied warranty of
11*49cdfc7eSAndroid Build Coastguard Worker  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12*49cdfc7eSAndroid Build Coastguard Worker  * GNU General Public License for more details.
13*49cdfc7eSAndroid Build Coastguard Worker  *
14*49cdfc7eSAndroid Build Coastguard Worker  * You should have received a copy of the GNU General Public License
15*49cdfc7eSAndroid Build Coastguard Worker  * along with this program; if not, write the Free Software Foundation,
16*49cdfc7eSAndroid Build Coastguard Worker  * Inc.,  51 Franklin St, Fifth Floor, Boston, MA  02110-1301  USA
17*49cdfc7eSAndroid Build Coastguard Worker  */
18*49cdfc7eSAndroid Build Coastguard Worker 
19*49cdfc7eSAndroid Build Coastguard Worker  /*
20*49cdfc7eSAndroid Build Coastguard Worker   *  Create a virtual device (mouse), send events to /dev/uinput
21*49cdfc7eSAndroid Build Coastguard Worker   *  and Check that events not advertised in the input device bits
22*49cdfc7eSAndroid Build Coastguard Worker   *  are filtered.
23*49cdfc7eSAndroid Build Coastguard Worker   */
24*49cdfc7eSAndroid Build Coastguard Worker 
25*49cdfc7eSAndroid Build Coastguard Worker #include <linux/input.h>
26*49cdfc7eSAndroid Build Coastguard Worker #include <linux/uinput.h>
27*49cdfc7eSAndroid Build Coastguard Worker 
28*49cdfc7eSAndroid Build Coastguard Worker #include "test.h"
29*49cdfc7eSAndroid Build Coastguard Worker #include "safe_macros.h"
30*49cdfc7eSAndroid Build Coastguard Worker #include "lapi/fcntl.h"
31*49cdfc7eSAndroid Build Coastguard Worker #include "input_helper.h"
32*49cdfc7eSAndroid Build Coastguard Worker 
33*49cdfc7eSAndroid Build Coastguard Worker #define X_VALUE 10
34*49cdfc7eSAndroid Build Coastguard Worker #define Y_VALUE 10
35*49cdfc7eSAndroid Build Coastguard Worker 
36*49cdfc7eSAndroid Build Coastguard Worker #define NB_TEST 20
37*49cdfc7eSAndroid Build Coastguard Worker 
38*49cdfc7eSAndroid Build Coastguard Worker static void setup(void);
39*49cdfc7eSAndroid Build Coastguard Worker static void send_events(void);
40*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void);
41*49cdfc7eSAndroid Build Coastguard Worker 
42*49cdfc7eSAndroid Build Coastguard Worker static int fd;
43*49cdfc7eSAndroid Build Coastguard Worker static int fd2;
44*49cdfc7eSAndroid Build Coastguard Worker 
45*49cdfc7eSAndroid Build Coastguard Worker char *TCID = "input05";
46*49cdfc7eSAndroid Build Coastguard Worker 
main(int ac,char ** av)47*49cdfc7eSAndroid Build Coastguard Worker int main(int ac, char **av)
48*49cdfc7eSAndroid Build Coastguard Worker {
49*49cdfc7eSAndroid Build Coastguard Worker 	int lc;
50*49cdfc7eSAndroid Build Coastguard Worker 	int pid;
51*49cdfc7eSAndroid Build Coastguard Worker 
52*49cdfc7eSAndroid Build Coastguard Worker 	tst_parse_opts(ac, av, NULL, NULL);
53*49cdfc7eSAndroid Build Coastguard Worker 
54*49cdfc7eSAndroid Build Coastguard Worker 	setup();
55*49cdfc7eSAndroid Build Coastguard Worker 
56*49cdfc7eSAndroid Build Coastguard Worker 	for (lc = 0; TEST_LOOPING(lc); ++lc) {
57*49cdfc7eSAndroid Build Coastguard Worker 		pid = tst_fork();
58*49cdfc7eSAndroid Build Coastguard Worker 
59*49cdfc7eSAndroid Build Coastguard Worker 		switch (pid) {
60*49cdfc7eSAndroid Build Coastguard Worker 		case 0:
61*49cdfc7eSAndroid Build Coastguard Worker 			send_events();
62*49cdfc7eSAndroid Build Coastguard Worker 			exit(0);
63*49cdfc7eSAndroid Build Coastguard Worker 		case -1:
64*49cdfc7eSAndroid Build Coastguard Worker 			tst_brkm(TBROK | TERRNO, cleanup, "fork() failed");
65*49cdfc7eSAndroid Build Coastguard Worker 		default:
66*49cdfc7eSAndroid Build Coastguard Worker 			if (no_events_queued(fd2, 1))
67*49cdfc7eSAndroid Build Coastguard Worker 				tst_resm(TPASS, "No data received in eventX");
68*49cdfc7eSAndroid Build Coastguard Worker 			else
69*49cdfc7eSAndroid Build Coastguard Worker 				tst_resm(TFAIL, "Data received in eventX");
70*49cdfc7eSAndroid Build Coastguard Worker 		break;
71*49cdfc7eSAndroid Build Coastguard Worker 		}
72*49cdfc7eSAndroid Build Coastguard Worker 
73*49cdfc7eSAndroid Build Coastguard Worker 		SAFE_WAITPID(NULL, pid, NULL, 0);
74*49cdfc7eSAndroid Build Coastguard Worker 	}
75*49cdfc7eSAndroid Build Coastguard Worker 
76*49cdfc7eSAndroid Build Coastguard Worker 	cleanup();
77*49cdfc7eSAndroid Build Coastguard Worker 	tst_exit();
78*49cdfc7eSAndroid Build Coastguard Worker }
79*49cdfc7eSAndroid Build Coastguard Worker 
setup(void)80*49cdfc7eSAndroid Build Coastguard Worker static void setup(void)
81*49cdfc7eSAndroid Build Coastguard Worker {
82*49cdfc7eSAndroid Build Coastguard Worker 	tst_require_root();
83*49cdfc7eSAndroid Build Coastguard Worker 
84*49cdfc7eSAndroid Build Coastguard Worker 	fd = open_uinput();
85*49cdfc7eSAndroid Build Coastguard Worker 
86*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_IOCTL(NULL, fd, UI_SET_EVBIT, EV_KEY);
87*49cdfc7eSAndroid Build Coastguard Worker 	SAFE_IOCTL(NULL, fd, UI_SET_KEYBIT, BTN_LEFT);
88*49cdfc7eSAndroid Build Coastguard Worker 
89*49cdfc7eSAndroid Build Coastguard Worker 	create_device(fd);
90*49cdfc7eSAndroid Build Coastguard Worker 
91*49cdfc7eSAndroid Build Coastguard Worker 	fd2 = open_device();
92*49cdfc7eSAndroid Build Coastguard Worker }
93*49cdfc7eSAndroid Build Coastguard Worker 
send_events(void)94*49cdfc7eSAndroid Build Coastguard Worker static void send_events(void)
95*49cdfc7eSAndroid Build Coastguard Worker {
96*49cdfc7eSAndroid Build Coastguard Worker 	int nb;
97*49cdfc7eSAndroid Build Coastguard Worker 
98*49cdfc7eSAndroid Build Coastguard Worker 	for (nb = 0; nb < NB_TEST; ++nb) {
99*49cdfc7eSAndroid Build Coastguard Worker 		send_rel_move(fd, X_VALUE, Y_VALUE);
100*49cdfc7eSAndroid Build Coastguard Worker 		usleep(1000);
101*49cdfc7eSAndroid Build Coastguard Worker 	}
102*49cdfc7eSAndroid Build Coastguard Worker }
103*49cdfc7eSAndroid Build Coastguard Worker 
cleanup(void)104*49cdfc7eSAndroid Build Coastguard Worker static void cleanup(void)
105*49cdfc7eSAndroid Build Coastguard Worker {
106*49cdfc7eSAndroid Build Coastguard Worker 	destroy_device(fd);
107*49cdfc7eSAndroid Build Coastguard Worker }
108