MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
hid_detect.c
Go to the documentation of this file.
1 /* $Header: /var/cvs/mbdyn/mbdyn/mbdyn-1.0/modules/module-hid/hid_detect.c,v 1.5 2017/01/12 14:52:24 masarati Exp $ */
2 /*
3  * MBDyn (C) is a multibody analysis code.
4  * http://www.mbdyn.org
5  *
6  * Copyright (C) 1996-2017
7  *
8  * Pierangelo Masarati <masarati@aero.polimi.it>
9  * Paolo Mantegazza <mantegazza@aero.polimi.it>
10  *
11  * Dipartimento di Ingegneria Aerospaziale - Politecnico di Milano
12  * via La Masa, 34 - 20156 Milano, Italy
13  * http://www.aero.polimi.it
14  *
15  * Changing this copyright notice is forbidden.
16  *
17  * This program is free software; you can redistribute it and/or modify
18  * it under the terms of the GNU General Public License as published by
19  * the Free Software Foundation (version 2 of the License).
20  *
21  *
22  * This program is distributed in the hope that it will be useful,
23  * but WITHOUT ANY WARRANTY; without even the implied warranty of
24  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
25  * GNU General Public License for more details.
26  *
27  * You should have received a copy of the GNU General Public License
28  * along with this program; if not, write to the Free Software
29  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
30  */
31 
32 #include "stdlib.h"
33 #include "unistd.h"
34 #include "stdio.h"
35 #include "errno.h"
36 #include "stdint.h"
37 #include "string.h"
38 
39 #define LO(x) ((x) & 0x0F)
40 #define HI(x) (((x) & 0xF0) >> 8)
41 #define HEX(x) ("0123456789abcdef"[(x)])
42 
43 static const char *
44 type2str(uint8_t type)
45 {
46  switch (type) {
47  case 1:
48  return "button";
49  case 2:
50  return "linctl";
51  }
52  return "unknwn";
53 }
54 
55 int
56 main(int argc, char *argv[])
57 {
58  FILE *fd;
59 
60  const char *device = "/dev/input/js0";
61 
62  if (argc > 1) {
63  device = argv[1];
64  }
65 
66  fd = fopen(device, "r");
67  if (fd == NULL) {
68  int save_errno = errno;
69  fprintf(stderr, "fopen(\"%s\")=%d %s\n", device, save_errno, strerror(save_errno));
70  exit(EXIT_FAILURE);
71  }
72 
73  int nbt = 0, nlc = 0;
74  uint8_t maxbt = 0, maxlc = 0;
75  int preamble = 1;
76 
77  for (;;) {
78  char buf[8];
79  uint32_t cnt;
80  int16_t value;
81  uint8_t type, idx;
82 
83  size_t rc;
84 
85  rc = fread((void *)&buf[0], 1, sizeof(buf), fd);
86  if (rc < 1) {
87  fprintf(stderr, "fread returned no data\n");
88  break;
89  }
90 
91  cnt = *((uint32_t *)&buf[0]);
92  value = *((int16_t *)&buf[4]);
93  type = *((uint8_t *)&buf[6]);
94  idx = *((uint8_t *)&buf[7]);
95 
96  if (preamble) {
97  if (!(type & 0x80U)) {
98  printf("buttons=%d (max=%u) linear controls=%d (max=%u)\n\n", nbt, maxbt, nlc, maxlc);
99  preamble = 0;
100 
101  } else {
102  switch (type & 0x7FU) {
103  case 1:
104  nbt++;
105  if (idx > maxbt) {
106  maxbt = idx;
107  }
108  break;
109 
110  case 2:
111  nlc++;
112  if (idx > maxlc) {
113  maxlc = idx;
114  }
115  break;
116  }
117 
118  printf("0x%8x %s[%2u]=%6d\n", cnt, type2str(type & 0x7FU), idx, value);
119 
120  continue;
121  }
122  }
123 
124  printf("0x%8x %s[%2u]=%6d\n", cnt, type2str(type), idx, value);
125  }
126 
127  return 0;
128 }
int main(int argc, char *argv[])
Definition: hid_detect.c:56
static doublereal buf[BUFSIZE]
Definition: discctrl.cc:333
static const char * type2str(uint8_t type)
Definition: hid_detect.c:44