RDK Documentation (Open Sourced RDK Components)
test_uinput.c
1 /*
2  * If not stated otherwise in this file or this component's Licenses.txt file the
3  * following copyright and licenses apply:
4  *
5  * Copyright 2016 RDK Management
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18 */
19 #include <linux/input.h>
20 #include <fcntl.h>
21 #include <stdio.h>
22 #include <string.h>
23 #include <unistd.h>
24 #include <errno.h>
25 
26 int main(int argc, char *argv[])
27 {
28  /* open device */
29  if (argc < 2) {
30  printf("test_uinput: <device file path>\r\n");
31  return 0;
32  }
33 
34  const char *devFile = argv[1];
35 
36  int fd = open(devFile, O_RDONLY|O_SYNC);
37  if (fd >= 0) {
38  struct input_event ev;
39  const int count = sizeof(ev);
40  while(1) {
41  bzero(&ev, count);
42  int ret = read(fd, &ev, count);
43  if (ret == count) {
44  printf("Getting input [%ld.%ld] - %d %d %d\r\n",
45  ev.time.tv_sec, ev.time.tv_usec,
46  ev.type,
47  ev.code,
48  ev.value);
49  if (ev.type == EV_KEY) {
50  if (ev.value >= 0 && ev.value <=2) {
51  printf("[%ld].[%ld] : Key [%d] [%s]\r\n",
52  ev.time.tv_sec, ev.time.tv_usec,
53  ev.code,
54  (ev.value == 1) ? "+++++PRESSED" : ((ev.value == 0) ? "=====Release" : "......."));
55  }
56  }
57  else if (ev.type == EV_SYN) {
58  printf("[%ld].[%ld] : SYN [%s]\r\n",
59  ev.time.tv_sec, ev.time.tv_usec,
60  (ev.value == SYN_REPORT) ? "SYN_REPORT" : "SYN_OTHER");
61  }
62  }
63  else {
64  printf("read() input event returned %d failure [%s]\r\n", ret, strerror(errno));
65  break;
66  }
67  }
68  }
69  else {
70  printf("open file %s failed [%s]\r\n", devFile, strerror(errno));
71  }
72 
73  return 0;
74 }