-wxJoystickThread::wxJoystickThread(wxHIDJoystick* hid, int joystick)
- : m_hid(hid),
- m_joystick(joystick),
- m_lastposition(wxDefaultPosition),
- m_buttons(0),
- m_catchwin(NULL),
- m_polling(0)
-{
- for (int i=0; i<15; i++)
- m_axe[i] = 0;
-}
-
-
-# define wxJSVERIFY(arg) if(!(arg)) {wxLogSysError(wxT(#arg)); return NULL;}
-# define wxJSASSERT(arg) wxJSVERIFY(arg)
-
-void* wxJoystickThread::Entry()
-{
- CFRunLoopSourceRef pRLSource = NULL;
-
- wxJSVERIFY( (*m_hid->GetQueue())->createAsyncEventSource(m_hid->GetQueue(), &pRLSource)
- == kIOReturnSuccess );
- wxJSASSERT(pRLSource != NULL);
-
- //attach runloop source to main run loop in thread
- CFRunLoopRef pRL = CFRunLoopGetCurrent();
- CFRunLoopAddSource(pRL, pRLSource, kCFRunLoopDefaultMode);
-
-// wxJSVERIFY( (*m_hid->GetQueue())->start(m_hid->GetQueue()) == kIOReturnSuccess );
-
- double dTime;
- IOHIDEventStruct hidevent;
- AbsoluteTime bogustime = {0,0};
- IOReturn ret;
-
- while(true)
- {
- if (TestDestroy())
- break;
-
- if (m_polling)
- dTime = 0.0001 * m_polling;
- else
- dTime = 0.0001 * 10; // check at least every 10 msec in blocking case
-
- CFRunLoopRunInMode(kCFRunLoopDefaultMode, dTime, m_polling);
-
- while ( (ret = (*m_hid->GetQueue())->getNextEvent(m_hid->GetQueue(),
- &hidevent, bogustime, 0)) != kIOReturnUnderrun )
- {
- if (TestDestroy())
- break;
-
- wxJSASSERT(ret == kIOReturnSuccess);
- wxJoystickEvent wxevent;
-
- int nIndex = 0;
- IOHIDElementCookie* pCookies = m_hid->GetCookies();
- while(nIndex < 50)
- {
- if(hidevent.elementCookie == pCookies[nIndex])
- break;
- }
- wxASSERT(nIndex != 50);
-
- if (nIndex < 40)
- {
- if (hidevent.value)
- {
- m_buttons |= (1 << nIndex);
- wxevent.SetEventType(wxEVT_JOY_BUTTON_DOWN);
- }
- else
- {
- m_buttons &= ~(1 << nIndex);
- wxevent.SetEventType(wxEVT_JOY_BUTTON_UP);
- }
-
- wxevent.SetButtonChange(nIndex);
- }
- else if (nIndex == wxJS_AXIS_X)
- {
- m_lastposition.x = hidevent.value;
- wxevent.SetEventType(wxEVT_JOY_MOVE);
- m_axe[nIndex - 39] = hidevent.value;
- }
- else if (nIndex == wxJS_AXIS_Y)
- {
- m_lastposition.y = hidevent.value;
- wxevent.SetEventType(wxEVT_JOY_MOVE);
- m_axe[nIndex - 39] = hidevent.value;
- }
- else if (nIndex == wxJS_AXIS_Z)
- {
- wxevent.SetEventType(wxEVT_JOY_ZMOVE);
- m_axe[nIndex - 39] = hidevent.value;
- }
- else
- wxevent.SetEventType(wxEVT_JOY_MOVE);
-
- Nanoseconds timestamp = AbsoluteToNanoseconds(hidevent.timestamp);
-
- wxULongLong llTime(timestamp.hi, timestamp.lo);
-
- llTime /= 1000000;
-
- wxevent.SetTimestamp(llTime.GetValue());
- wxevent.SetJoystick(m_joystick);
- wxevent.SetButtonState(m_buttons);
- wxevent.SetPosition(m_lastposition);
- wxevent.SetZPosition(m_axe[3]);
- wxevent.SetEventObject(m_catchwin);
-
- if (m_catchwin)
- m_catchwin->AddPendingEvent(wxevent);
- }
- }
- return NULL;
-}
-