MBDyn-1.7.3
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Groups
hid_detect.c File Reference
#include "stdlib.h"
#include "unistd.h"
#include "stdio.h"
#include "errno.h"
#include "stdint.h"
#include "string.h"
Include dependency graph for hid_detect.c:

Go to the source code of this file.

Macros

#define LO(x)   ((x) & 0x0F)
 
#define HI(x)   (((x) & 0xF0) >> 8)
 
#define HEX(x)   ("0123456789abcdef"[(x)])
 

Functions

static const char * type2str (uint8_t type)
 
int main (int argc, char *argv[])
 

Macro Definition Documentation

#define HEX (   x)    ("0123456789abcdef"[(x)])

Definition at line 41 of file hid_detect.c.

#define HI (   x)    (((x) & 0xF0) >> 8)

Definition at line 40 of file hid_detect.c.

#define LO (   x)    ((x) & 0x0F)

Definition at line 39 of file hid_detect.c.

Function Documentation

int main ( int  argc,
char *  argv[] 
)

Definition at line 56 of file hid_detect.c.

References buf, and type2str().

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 }
static doublereal buf[BUFSIZE]
Definition: discctrl.cc:333
static const char * type2str(uint8_t type)
Definition: hid_detect.c:44

Here is the call graph for this function:

static const char* type2str ( uint8_t  type)
static

Definition at line 44 of file hid_detect.c.

Referenced by main().

45 {
46  switch (type) {
47  case 1:
48  return "button";
49  case 2:
50  return "linctl";
51  }
52  return "unknwn";
53 }