1f08c3bdfSopenharmony_ci/* 2f08c3bdfSopenharmony_ci * Copyright (c) 2015 Cedric Hnyda <chnyda@suse.com> 3f08c3bdfSopenharmony_ci * 4f08c3bdfSopenharmony_ci * This program is free software; you can redistribute it and/or 5f08c3bdfSopenharmony_ci * modify it under the terms of the GNU General Public License as 6f08c3bdfSopenharmony_ci * published by the Free Software Foundation; either version 2 of 7f08c3bdfSopenharmony_ci * the License, or (at your option) any later version. 8f08c3bdfSopenharmony_ci * 9f08c3bdfSopenharmony_ci * This program is distributed in the hope that it would be useful, 10f08c3bdfSopenharmony_ci * but WITHOUT ANY WARRANTY; without even the implied warranty of 11f08c3bdfSopenharmony_ci * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 12f08c3bdfSopenharmony_ci * GNU General Public License for more details. 13f08c3bdfSopenharmony_ci * 14f08c3bdfSopenharmony_ci * You should have received a copy of the GNU General Public License 15f08c3bdfSopenharmony_ci * along with this program; if not, write the Free Software Foundation, 16f08c3bdfSopenharmony_ci * Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA 17f08c3bdfSopenharmony_ci */ 18f08c3bdfSopenharmony_ci 19f08c3bdfSopenharmony_ci /* 20f08c3bdfSopenharmony_ci * Create a virtual device (mouse), send events to /dev/uinput 21f08c3bdfSopenharmony_ci * and check that the events are well received in /dev/input/mice 22f08c3bdfSopenharmony_ci */ 23f08c3bdfSopenharmony_ci 24f08c3bdfSopenharmony_ci#include <linux/input.h> 25f08c3bdfSopenharmony_ci#include <linux/uinput.h> 26f08c3bdfSopenharmony_ci 27f08c3bdfSopenharmony_ci#include "test.h" 28f08c3bdfSopenharmony_ci#include "safe_macros.h" 29f08c3bdfSopenharmony_ci#include "lapi/fcntl.h" 30f08c3bdfSopenharmony_ci#include "input_helper.h" 31f08c3bdfSopenharmony_ci 32f08c3bdfSopenharmony_ci#define NB_TEST 10 33f08c3bdfSopenharmony_ci#define PS2_RIGHT_BTN 0x02 34f08c3bdfSopenharmony_ci 35f08c3bdfSopenharmony_cistatic void setup(void); 36f08c3bdfSopenharmony_cistatic void send_events(void); 37f08c3bdfSopenharmony_cistatic int check_events(void); 38f08c3bdfSopenharmony_cistatic void cleanup(void); 39f08c3bdfSopenharmony_ci 40f08c3bdfSopenharmony_cistatic int fd, fd2; 41f08c3bdfSopenharmony_ci 42f08c3bdfSopenharmony_cichar *TCID = "input03"; 43f08c3bdfSopenharmony_ci 44f08c3bdfSopenharmony_ciint main(int ac, char **av) 45f08c3bdfSopenharmony_ci{ 46f08c3bdfSopenharmony_ci int lc; 47f08c3bdfSopenharmony_ci int pid; 48f08c3bdfSopenharmony_ci 49f08c3bdfSopenharmony_ci tst_parse_opts(ac, av, NULL, NULL); 50f08c3bdfSopenharmony_ci 51f08c3bdfSopenharmony_ci setup(); 52f08c3bdfSopenharmony_ci 53f08c3bdfSopenharmony_ci for (lc = 0; TEST_LOOPING(lc); ++lc) { 54f08c3bdfSopenharmony_ci pid = tst_fork(); 55f08c3bdfSopenharmony_ci 56f08c3bdfSopenharmony_ci switch (pid) { 57f08c3bdfSopenharmony_ci case 0: 58f08c3bdfSopenharmony_ci send_events(); 59f08c3bdfSopenharmony_ci exit(0); 60f08c3bdfSopenharmony_ci case -1: 61f08c3bdfSopenharmony_ci tst_brkm(TBROK | TERRNO, cleanup, "fork() failed"); 62f08c3bdfSopenharmony_ci default: 63f08c3bdfSopenharmony_ci if (check_events()) 64f08c3bdfSopenharmony_ci tst_resm(TFAIL, "Wrong data received"); 65f08c3bdfSopenharmony_ci else 66f08c3bdfSopenharmony_ci tst_resm(TPASS, 67f08c3bdfSopenharmony_ci "Data received in /dev/input/mice"); 68f08c3bdfSopenharmony_ci break; 69f08c3bdfSopenharmony_ci } 70f08c3bdfSopenharmony_ci 71f08c3bdfSopenharmony_ci SAFE_WAITPID(NULL, pid, NULL, 0); 72f08c3bdfSopenharmony_ci } 73f08c3bdfSopenharmony_ci 74f08c3bdfSopenharmony_ci cleanup(); 75f08c3bdfSopenharmony_ci tst_exit(); 76f08c3bdfSopenharmony_ci} 77f08c3bdfSopenharmony_ci 78f08c3bdfSopenharmony_cistatic void setup(void) 79f08c3bdfSopenharmony_ci{ 80f08c3bdfSopenharmony_ci tst_require_root(); 81f08c3bdfSopenharmony_ci 82f08c3bdfSopenharmony_ci fd = open_uinput(); 83f08c3bdfSopenharmony_ci 84f08c3bdfSopenharmony_ci setup_mouse_events(fd); 85f08c3bdfSopenharmony_ci SAFE_IOCTL(NULL, fd, UI_SET_EVBIT, EV_KEY); 86f08c3bdfSopenharmony_ci SAFE_IOCTL(NULL, fd, UI_SET_KEYBIT, BTN_RIGHT); 87f08c3bdfSopenharmony_ci 88f08c3bdfSopenharmony_ci create_device(fd); 89f08c3bdfSopenharmony_ci 90f08c3bdfSopenharmony_ci fd2 = SAFE_OPEN(NULL, "/dev/input/mice", O_RDONLY); 91f08c3bdfSopenharmony_ci} 92f08c3bdfSopenharmony_ci 93f08c3bdfSopenharmony_cistatic void send_events(void) 94f08c3bdfSopenharmony_ci{ 95f08c3bdfSopenharmony_ci int nb; 96f08c3bdfSopenharmony_ci 97f08c3bdfSopenharmony_ci for (nb = 0; nb < NB_TEST; ++nb) { 98f08c3bdfSopenharmony_ci send_event(fd, EV_KEY, BTN_RIGHT, 1); 99f08c3bdfSopenharmony_ci send_event(fd, EV_SYN, 0, 0); 100f08c3bdfSopenharmony_ci usleep(1000); 101f08c3bdfSopenharmony_ci send_event(fd, EV_KEY, BTN_RIGHT, 0); 102f08c3bdfSopenharmony_ci send_event(fd, EV_SYN, 0, 0); 103f08c3bdfSopenharmony_ci usleep(1000); 104f08c3bdfSopenharmony_ci } 105f08c3bdfSopenharmony_ci} 106f08c3bdfSopenharmony_ci 107f08c3bdfSopenharmony_cistatic int check_events(void) 108f08c3bdfSopenharmony_ci{ 109f08c3bdfSopenharmony_ci int nb, rd, i, pressed = 0; 110f08c3bdfSopenharmony_ci char buf[30]; 111f08c3bdfSopenharmony_ci 112f08c3bdfSopenharmony_ci nb = 0; 113f08c3bdfSopenharmony_ci 114f08c3bdfSopenharmony_ci while (nb < NB_TEST) { 115f08c3bdfSopenharmony_ci rd = read(fd2, buf, sizeof(buf)); 116f08c3bdfSopenharmony_ci 117f08c3bdfSopenharmony_ci if (rd < 0) 118f08c3bdfSopenharmony_ci tst_brkm(TBROK | TERRNO, NULL, "read() failed"); 119f08c3bdfSopenharmony_ci 120f08c3bdfSopenharmony_ci if (rd % 3) { 121f08c3bdfSopenharmony_ci tst_resm(TINFO, "read() returned %i", rd); 122f08c3bdfSopenharmony_ci return 1; 123f08c3bdfSopenharmony_ci } 124f08c3bdfSopenharmony_ci 125f08c3bdfSopenharmony_ci for (i = 0; i < rd / 3; i++) { 126f08c3bdfSopenharmony_ci if (buf[3*i] & PS2_RIGHT_BTN) 127f08c3bdfSopenharmony_ci pressed = 1; 128f08c3bdfSopenharmony_ci 129f08c3bdfSopenharmony_ci if (pressed == 1 && !(buf[3*i] & PS2_RIGHT_BTN)) { 130f08c3bdfSopenharmony_ci pressed = 0; 131f08c3bdfSopenharmony_ci nb++; 132f08c3bdfSopenharmony_ci } 133f08c3bdfSopenharmony_ci } 134f08c3bdfSopenharmony_ci } 135f08c3bdfSopenharmony_ci 136f08c3bdfSopenharmony_ci return nb != NB_TEST; 137f08c3bdfSopenharmony_ci} 138f08c3bdfSopenharmony_ci 139f08c3bdfSopenharmony_cistatic void cleanup(void) 140f08c3bdfSopenharmony_ci{ 141f08c3bdfSopenharmony_ci if (fd2 > 0 && close(fd2)) 142f08c3bdfSopenharmony_ci tst_resm(TWARN, "close(fd2) failed"); 143f08c3bdfSopenharmony_ci 144f08c3bdfSopenharmony_ci destroy_device(fd); 145f08c3bdfSopenharmony_ci} 146