gmsh-TingyuanDoc  0.1
An Open-Source Timing-driven Analytical Mixed-size FPGA Placer
GamePad.cpp
Go to the documentation of this file.
1 // Gmsh - Copyright (C) 1997-2022 C. Geuzaine, J.-F. Remacle
2 //
3 // See the LICENSE.txt file in the Gmsh root directory for license information.
4 // Please report all issues on https://gitlab.onelab.info/gmsh/gmsh/issues.
5 //
6 // Contributed by Gilles Marckmann <gilles.marckmann@ec-nantes.fr>
7 // default map is "THRUSTMASTER FireStorm Dual Power"-like
8 //
9 // ###### ######
10 // # but5 # # but7 #
11 // .########.. ..#######.
12 // ............ .......... .
13 // ...#######... ...########..
14 // ..# but4 #... ...# but6 #..
15 // ...########....... ........#########..
16 // .................... .....................
17 // .......#................... ..........................
18 // ......axis5..........................................####.......
19 // ........ #...........................................# b3 #.......
20 // ..........#...........................................# #.......
21 // ...###< axis4 >###...............................####..####..####...
22 // ...........#.....................................# b1 #......# b2 #...
23 // ............#.....................................# #......# #....
24 // ............#......................................####........####.....
25 // .............#.............................................####...........
26 // ..........................................................# b0 #..........
27 // ...........................................................# #...........
28 // .......................#######................#######.......####............
29 // ......................# axis1 ##............## axis3 ##......................
30 // .....................## | ##..........## | ##.....................
31 // ......................#<--axis0-->#..........#<--axis2--> #.....................
32 // ......................## | ##. .####. .## | ##......................
33 // .......................## | ##.. # b9 #...## | ##.......................
34 // ..........................#######......####......#######.........................
35 // ..................................................................................
36 // ..................... .......... ......... .....................
37 // .................... ....................
38 // .................. ..................
39 // .................. ..................
40 // .................. .................
41 // ................. ................
42 // ................ ...............
43 // .............. ..............
44 // ............. ............
45 // ............ ............
46 // .......... ..........
47 // ....... ........
48 // .... ...
49 //
50 
51 #include <math.h>
52 #include <cstring>
53 #include <string>
54 #include <iostream>
55 #include <stdio.h>
56 #include <algorithm>
57 #include "GamePad.h"
58 
59 #if !defined(WIN32)
60 #include <unistd.h>
61 #endif
62 
63 GamePad::GamePad() : active(false), frequency(.01)
64 {
65 #if defined(WIN32)
66  gamepad_fd = 0;
67  for (int i = JOYSTICKID1 ; i < JOYSTICKID2 ; i++) {
68  if(JOYERR_NOERROR == joyGetDevCaps(i, &caps, sizeof(JOYCAPS)) ) {
69  /*
70  std::cout << "Joystick/Gamepad detected: "<< name ;
71  std::cout << " ( " << (int)caps.wNumAxes <<" axes /" ;
72  std::cout << (int)caps.wNumButtons <<" buttons)" << std::endl;
73  std::cout << " Driver name ....... " << caps.szPname << std::endl;
74  std::cout << " Manufacturer ID ... " << caps.wMid << std::endl;
75  std::cout << " Product ID ........ " << caps.wPid << std::endl;
76  std::cout << " RegKey ............ " << caps.szRegKey << std::endl;
77  std::cout << " OEMVxD ............ " << caps.szOEMVxD << std::endl;
78  std::cout << " wXmin ............. " << caps.wXmin << std::endl;
79  std::cout << " wXmax ............. " << caps.wXmax << std::endl;
80  std::cout << " wYmin ............. " << caps.wYmin << std::endl;
81  std::cout << " wYmax ............. " << caps.wYmax << std::endl;
82  std::cout << " wZmin ............. " << caps.wZmin << std::endl;
83  std::cout << " wZmax ............. " << caps.wZmax << std::endl;
84  std::cout << " wPeriodMin ........ " << caps.wPeriodMin << std::endl;
85  std::cout << " wPeriodMax ........ " << caps.wPeriodMax << std::endl;
86  std::cout << " wRmin ............. " << caps.wRmin << std::endl;
87  std::cout << " wRmax ............. " << caps.wRmax << std::endl;
88  std::cout << " wUmin ............. " << caps.wUmin << std::endl;
89  std::cout << " wUmax ............. " << caps.wUmax << std::endl;
90  std::cout << " wVmin ............. " << caps.wVmin << std::endl;
91  std::cout << " wVmax ............. " << caps.wVmax << std::endl;
92  */
93  if((1./frequency < (double)caps.wPeriodMin) ||
94  (1./frequency > (double)caps.wPeriodMax)){
95  frequency=.1/(double)caps.wPeriodMin;
96  }
97  gamepad_fd = i;
98  axes = std::min(GP_AXES,(int)caps.wNumAxes + 2);
99  buttons = std::min(GP_BUTTONS,(int)caps.wNumButtons);
100  for (int i = 0; i < buttons; i++) button[i] = false;
101  for (int i = 0; i < axes; i++) axe[i] = 0.;
102  active = true;
103  }
104  }
105  for(int i = 0; i < buttons; i++) button[i] = 0;
106  for(int i = 0; i < axes; i++) axe[i] = 0;
107  joyGetPosEx(gamepad_fd, &infoex);
108  infoex.dwFlags = JOY_RETURNALL;
109 #elif defined(HAVE_LINUX_JOYSTICK)
110  gamepad_fd = open(GAMEPAD_DEV, O_RDONLY | O_NONBLOCK);
111  if (gamepad_fd > 0) {
112  ioctl(gamepad_fd, JSIOCGNAME(256), name);
113  ioctl(gamepad_fd, JSIOCGVERSION, &version);
114  ioctl(gamepad_fd, JSIOCGAXES, &axes);
115  ioctl(gamepad_fd, JSIOCGBUTTONS, &buttons);
116  std::cout << "Joystick/Gamepad detected: " << name ;
117  std::cout << " (version " << version << " / " ;
118  std::cout << (int)axes <<" axes /" ;
119  std::cout << (int)buttons <<" buttons)" << std::endl;
120  active = true;
121  }
122 #endif
123  if(active){
124  for (int i = 0; i < std::min(9, (int)buttons); i++) {
125  button[i] = false;toggle_status[i] = false;
126  }
127  for (int i = 0; i < std::min(7, (int)axes); i++) axe[i] = 0.;
128  for (int i = 0; i < std::min(9, (int)buttons); i++) button_map[i] = i;
129  for (int i = 0; i < std::min(7, (int)axes); i++) axe_map[i] = i;
130  axe_map[6]=1;
131  // another recognized map "Thrustmaster Run'N' Drive Wireless PS3"
132  // warning :: on Windows we dont have the human-friendly Model Name of the Gamepad
133  if(strcmp(name, "Thrustmaster Run'N' Drive Wireless PS3") == 0){
134  button_map[0] = 1;
135  button_map[1] = 0;
136  button_map[5] = 6;
137  button_map[6] = 5;
138  }
139  }
140 }
141 
143 {
144  active = false;
145 #if defined(HAVE_LINUX_JOYSTICK)
146  if (gamepad_fd) close(gamepad_fd);
147  gamepad_fd = 0;
148 #endif
149 }
150 
151 bool GamePad::toggle(const int _nbut)
152 {
153  bool res;
154  if(toggle_status[_nbut]){ res = true; toggle_status[_nbut] = false; }
155  else{ res = false; }
156  return res;
157 }
158 
160 {
161 #if defined(WIN32)
162  infoex.dwFlags = JOY_RETURNALL;
163  joyGetPosEx(gamepad_fd, &infoex) ;
164  axe[0] = (double)((1. * infoex.dwXpos - 32767.) / 32767.);
165  axe[1] = (double)((1. * infoex.dwYpos - 32767.) / 32767.);
166  axe[2] = (double)((1. * infoex.dwRpos - 32767.) / 32767.);
167  axe[3] = (double)((1. * infoex.dwZpos - 32767.) / 32767.);
168  if(infoex.dwPOV < 38000.) {
169  double alpha = 3.14159 / 18000. * infoex.dwPOV;
170  axe[4] = 1.001 * sin(alpha);
171  axe[5] = -1.001 * cos(alpha);
172  }
173  else{
174  axe[4] = axe[5] = 0.;
175  }
176  for (int i = 0; i < 6; i++) if(fabs(axe[i]) < .01) axe[i] = 0.;
177  bool event = false;
178  int event_num = 0;
179  bool event_value = false;
180  for (int i = 0; i < buttons; i++){
181  int bin = pow(2.0, i);
182  if(button[i] != (bool)(infoex.dwButtons & bin)) {
183  event = true;
184  event_num = i;
185  event_value = (bool)(infoex.dwButtons & bin);
186  }
187  }
188  if(event){
189  if(button[event_num] == 0 && event_value){ toggle_status[event_num] = true; }
190  button[event_num] = event_value;
191  }
192  /*
193  std::cout<< "--------------------" << std::endl;
194  std::cout<< infoex.dwXpos << std::endl;
195  std::cout<< infoex.dwYpos << std::endl;
196  std::cout<< infoex.dwZpos << std::endl;
197  std::cout<< infoex.dwRpos << std::endl;
198  std::cout<< infoex.dwUpos << std::endl;
199  std::cout<< infoex.dwVpos << std::endl;
200  std::cout<< infoex.dwButtons << std::endl;
201  std::cout<< infoex.dwButtonNumber << std::endl;
202  std::cout<< infoex.dwPOV << std::endl;
203  */
204 #elif defined(HAVE_LINUX_JOYSTICK)
205  int result = read(gamepad_fd, &event, sizeof(event));
206  if (result > 0){
207  switch (event.type){
208  case JS_EVENT_INIT:
209  break;
210  case JS_EVENT_INIT | JS_EVENT_AXIS:
211  break;
212  case JS_EVENT_INIT | JS_EVENT_BUTTON:
213  break;
214  case JS_EVENT_AXIS:
215  axe[(int)event.number] = (double)event.value / 32767.;
216  break;
217  case JS_EVENT_BUTTON:
218  if(button[(int)event.number] == 0. && (bool)event.value) {
219  toggle_status[(int)event.number] = true;
220  }
221  button[(int)event.number] = (bool)event.value ;
222  break;
223  default:
224  break;
225  }
226  }
227 #endif
228  return 1;
229 }
230 
232 {
233  for (int i = 0; i < 6; i++) std::cout << "_________";
234  std::cout << std::endl; std::cout << " axis ";
235  for (int i = 0; i < 6; i++) std::cout << " | "<<i;
236  std::cout << std::endl; std::cout << " ";
237  for (int i = 0; i < 6; i++) std::cout << " | "<< axe[i] ;
238  std::cout << std::endl;
239  for (int i = 0; i < 10; i++) std::cout << "_____";
240  std::cout << std::endl; std::cout << " b.";
241  for (int i = 0; i < 10; i++) std::cout << " | " << i;
242  std::cout << std::endl; std::cout << " ";
243  for (int i = 0; i < 10; i++) std::cout << " | " << button[i];
244  std::cout << std::endl;
245  for (int i = 0; i < 10; i++) std::cout << "_____";
246  std::cout << std::endl;
247 }
GamePad::active
bool active
Definition: GamePad.h:29
GamePad::button_map
int button_map[10]
Definition: GamePad.h:40
GP_AXES
#define GP_AXES
Definition: GamePad.h:14
GamePad::read_event
int read_event()
Definition: GamePad.cpp:159
GamePad.h
GamePad::button
bool button[GP_BUTTONS]
Definition: GamePad.h:36
GamePad::axe_map
int axe_map[8]
Definition: GamePad.h:41
GamePad::affiche
void affiche()
Definition: GamePad.cpp:231
GamePad::axes
int axes
Definition: GamePad.h:59
GamePad::~GamePad
~GamePad()
Definition: GamePad.cpp:142
GP_BUTTONS
#define GP_BUTTONS
Definition: GamePad.h:13
GamePad::axe
double axe[GP_AXES]
Definition: GamePad.h:35
GamePad::frequency
double frequency
Definition: GamePad.h:32
GamePad::toggle_status
bool toggle_status[GP_BUTTONS]
Definition: GamePad.h:30
GamePad::GamePad
GamePad()
Definition: GamePad.cpp:63
GamePad::name
char name[256]
Definition: GamePad.h:44
GamePad::buttons
int buttons
Definition: GamePad.h:60
GamePad::toggle
bool toggle(const int _nbut)
Definition: GamePad.cpp:151