6 #pragma comment(lib,"winmm.lib")
21 const int STATUS_BROKEN = -1;
23 const int CENTERED_VALUE = -1;
25 #define MAX_TIME_INTERVAL (2000000) // max time to try and reacquire
27 vrpn_Joywin32::vrpn_Joywin32 (
const char * name,
vrpn_Connection * c, vrpn_uint8 joyNumber, vrpn_float64 readRate, vrpn_uint8 mode, vrpn_int32 deadzone) :
31 _joyNumber(joyNumber),
36 last_error_report.tv_sec = 0;
37 last_error_report.tv_usec = 0;
39 if (deadzone >100 || deadzone<0) {
40 fprintf(stderr,
"invalid deadzone, (should be a percentage between 0 and 100).\n");
41 _status = STATUS_BROKEN;
46 fprintf(stderr,
"invalid mode, should be 0 (raw), 1 (normalized to 0,1) or 2 (normalized to -1,1).\n");
47 _status = STATUS_BROKEN;
52 fprintf(stderr,
"invalid read rate, should be positive.\n");
53 _status = STATUS_BROKEN;
57 if (_numchannels < 12) {
58 fprintf(stderr,
"vrpn_JoyWin32::vrpn_JoyWin32(): Not enough analog channels!\n");
59 _status = STATUS_BROKEN;
63 _deadzone = deadzone / 100.0;
69 void vrpn_Joywin32::init_joystick(
void)
78 fprintf(stderr,
"There are no joystick devices installed.\n");
79 _status = STATUS_BROKEN;
83 if(_joyNumber > joyGetNumDevs()) {
84 fprintf(stderr,
"There are not %d joysticks devices installed, unable to get joystick id %d.\n", _joyNumber, _joyNumber);
85 _status = STATUS_BROKEN;
92 dwResult = joyGetPos (_joyNumber-1, &tempJoyInfo);
104 case JOYERR_UNPLUGGED:
105 fprintf(stderr,
"Please plug in the joystick first.\n");
106 _status = STATUS_BROKEN;
108 case MMSYSERR_NODRIVER:
109 fprintf(stderr,
"No valid joystick driver.\n");
110 _status = STATUS_BROKEN;
113 fprintf(stderr,
"Bad joystick parameters.\n");
114 _status = STATUS_BROKEN;
117 fprintf(stderr,
"Couldn't capture joystick input, try restarting.\n");
118 _status = STATUS_BROKEN;
123 if (joyGetDevCaps(_joyNumber-1, &_jc,
sizeof(_jc)) != JOYERR_NOERROR){
124 fprintf(stderr,
"Unable to get joystick capabilities.\n");
125 _status = STATUS_BROKEN;
140 fprintf(stderr,
"Joystick (%s) has %d axes and %d buttons.\n",
141 _jc.szPname, _jc.wNumAxes+(JOYCAPS_HASPOV?1:0), _jc.wNumButtons);
148 void vrpn_Joywin32::clear_values(
void)
152 for (i = 0; i < _numbuttons; i++) {
155 for (i = 0; i < _numchannels; i++) {
165 int vrpn_Joywin32::get_report(
void)
168 struct timeval reporttime;
174 printf(
" now: %ld:%ld, last %ld:%ld\n", reporttime.tv_sec, reporttime.tv_usec,
175 _timestamp.tv_sec, static_cast<long>(_timestamp.tv_usec));
176 printf(
" win32 joystick: Getting report\n");
181 jie.dwFlags = JOY_RETURNALL;
182 jie.dwSize =
sizeof(jie);
184 if (joyGetPosEx(_joyNumber-1, &jie) != JOYERR_NOERROR ){
185 fprintf(stderr,
"Unable to get joystick information.\n");
188 _status = STATUS_BROKEN;
195 vrpn_float64 normalizer;
198 channel[0] = jie.dwXpos;
199 channel[1] = jie.dwYpos;
201 if (_jc.wCaps & JOYCAPS_HASZ) {
202 channel[2] = jie.dwZpos;
206 if (_jc.wCaps & JOYCAPS_HASR) {
207 channel[3] = jie.dwRpos;
210 if (_jc.wCaps & JOYCAPS_HASU) {
211 channel[4] = jie.dwUpos;
214 if (_jc.wCaps & JOYCAPS_HASV) {
215 channel[5] = jie.dwVpos;
222 if (_jc.wCaps & JOYCAPS_HASPOV) {
223 channel[8] = jie.dwPOV;
224 if (channel[8] == 65535 || channel[8] == -1) {
225 channel[8] = CENTERED_VALUE;
227 if (channel[8] != CENTERED_VALUE) {
242 normalizer = (_jc.wXmax - _jc.wXmin);
243 if (normalizer != 0) channel[0] = (2*channel[0] - _jc.wXmax - _jc.wXmin) / (_jc.wXmax - _jc.wXmin);
244 normalizer = (_jc.wYmax - _jc.wYmin);
245 if (normalizer != 0) channel[1] = (2*channel[1] - _jc.wYmax - _jc.wYmin) / (_jc.wYmax - _jc.wYmin);
246 if (_jc.wCaps & JOYCAPS_HASZ) {
247 normalizer = (_jc.wZmax - _jc.wZmin);
248 if (normalizer != 0) channel[2] = (2*channel[2] - _jc.wZmax - _jc.wZmin) / (_jc.wZmax - _jc.wZmin);
250 if (_jc.wCaps & JOYCAPS_HASR) {
251 normalizer = (_jc.wRmax - _jc.wRmin);
252 if (normalizer != 0) channel[3] = (2*channel[3] - _jc.wRmax - _jc.wRmin) / (_jc.wRmax - _jc.wRmin);
254 if (_jc.wCaps & JOYCAPS_HASU) {
255 normalizer = (_jc.wUmax - _jc.wUmin);
256 if (normalizer != 0) channel[4] = (2*channel[4] - _jc.wUmax - _jc.wUmin) / (_jc.wUmax - _jc.wUmin);
258 if (_jc.wCaps & JOYCAPS_HASV) {
259 normalizer = (_jc.wVmax - _jc.wVmin);
260 if (normalizer != 0) channel[5] = (2*channel[5] - _jc.wVmax - _jc.wVmin) / (_jc.wVmax - _jc.wVmin);
264 else if (_mode == 1){
265 normalizer = _jc.wXmax - _jc.wXmin;
266 if (normalizer != 0) channel[0] = (channel[0] - _jc.wXmin) / normalizer;
267 normalizer = _jc.wYmax - _jc.wYmin;
268 if (normalizer != 0) channel[1] = (channel[1] - _jc.wYmin) / normalizer;
269 if (_jc.wCaps & JOYCAPS_HASZ) {
270 normalizer = _jc.wZmax - _jc.wZmin;
271 if (normalizer != 0) channel[2] = (channel[2] - _jc.wZmin) / normalizer;
273 if (_jc.wCaps & JOYCAPS_HASR) {
274 normalizer = _jc.wRmax - _jc.wRmin;
275 if (normalizer != 0) channel[3] = (channel[3] - _jc.wRmin) / normalizer;
277 if (_jc.wCaps & JOYCAPS_HASU) {
278 normalizer = _jc.wUmax - _jc.wUmin;
279 if (normalizer != 0) channel[4] = (channel[4] - _jc.wUmin) / normalizer;
281 if (_jc.wCaps & JOYCAPS_HASV) {
282 normalizer = _jc.wVmax - _jc.wVmin;
283 if (normalizer != 0) channel[5] = (channel[5] - _jc.wVmin) / normalizer;
290 for (vrpn_uint32 j=0;j<8;j++) {
292 if (channel[j] > 1.0) channel[j] = 1.0;
293 else if (channel[j] < 0.0) channel[j] = 0.0;
296 if (channel[j] <= _deadzone) {
300 channel[j] = (channel[j] - _deadzone)/(1-_deadzone);
304 else if(_mode == 2) {
306 for (vrpn_uint32 j=0;j<8;j++) {
308 if (channel[j] > 1.0) channel[j] = 1.0;
309 else if (channel[j] < -1.0) channel[j] = -1.0;
312 if (channel[j] > -_deadzone && channel[j] < _deadzone) {
317 channel[j] = (channel[j] - _deadzone)/(1-_deadzone);
320 channel[j] = (channel[j] + _deadzone)/(1-_deadzone);
327 for (vrpn_uint32 i=0;i<(
std::min)(_jc.wMaxButtons, _numbuttons);i++) {
329 buttons[i] = (char) ((jie.dwButtons&(1<<(i)))>>(i));
333 _timestamp = reporttime;
338 void vrpn_Joywin32::report_changes(vrpn_uint32 class_of_service)
347 void vrpn_Joywin32::report(vrpn_uint32 class_of_service)
360 void vrpn_Joywin32::mainloop()
371 send_text_message(
"Cannot talk to joystick, trying resetting it", now,
vrpn_TEXT_ERROR);
372 last_error_report = now;
384 fprintf(stderr,
"vrpn_Joywin32: Unknown mode (internal error)\n");