1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: wxJoystick class
8 // Copyright: (c) Ryan Norton
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 //===========================================================================
14 //===========================================================================
16 //---------------------------------------------------------------------------
17 // Pre-compiled header stuff
18 //---------------------------------------------------------------------------
20 // For compilers that support precompilation, includes "wx.h".
21 #include "wx/wxprec.h"
23 //---------------------------------------------------------------------------
25 //---------------------------------------------------------------------------
27 //we only support HID on OSX (DARWIN), since it requires DARWIN...
28 #if wxUSE_JOYSTICK && defined(__DARWIN__)
30 //---------------------------------------------------------------------------
32 //---------------------------------------------------------------------------
33 #include "wx/event.h" //joystick wxEvents
34 #include "wx/log.h" //logging...
35 #include "wx/joystick.h" //...
36 #include "wx/thread.h" //wxThread for polling thread/ wxCriticalSection
37 #include "wx/window.h" //for wxWindow to "capture" joystick
40 #include "wx/mac/corefoundation/hid.h" //private mac hid stuff
43 #include <CoreServices/CoreServices.h>
44 #include <mach/mach.h>
45 #include <mach/mach_time.h>
48 //---------------------------------------------------------------------------
49 // Definitions/Enumerations
50 //---------------------------------------------------------------------------
52 #define wxJS_MAX_AXES 10 /*max number of axes*/
53 #define wxJS_MAX_BUTTONS 40 /*max number of buttons*/
57 //These are positions within the cookie array
58 //in wxHIDJoystick that the cookies that store the axis' are
67 //---------------------------------------------------------------------------
69 //---------------------------------------------------------------------------
70 class wxHIDJoystick
: public wxHIDDevice
76 bool Create(int nWhich
);
77 virtual void BuildCookies(wxCFArray
& Array
);
78 void MakeCookies(wxCFArray
& Array
);
79 IOHIDElementCookie
* GetCookies();
80 IOHIDQueueInterface
** GetQueue();
82 int m_nXMax
, m_nYMax
, m_nZMax
, m_nRudderMax
, m_nUMax
, m_nVMax
,
83 m_nXMin
, m_nYMin
, m_nZMin
, m_nRudderMin
, m_nUMin
, m_nVMin
;
85 friend class wxJoystick
;
88 //---------------------------------------------------------------------------
90 //---------------------------------------------------------------------------
91 class wxJoystickThread
: public wxThread
94 wxJoystickThread(wxHIDJoystick
* hid
, int joystick
);
96 static void HIDCallback(void* target
, IOReturn res
, void* context
, void* sender
);
101 wxPoint m_lastposition
;
102 int m_axe
[wxJS_MAX_AXES
];
104 wxWindow
* m_catchwin
;
107 friend class wxJoystick
;
110 //===========================================================================
112 //===========================================================================
114 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
115 // wxGetIntFromCFDictionary
117 // Helper function that gets a integer from a dictionary key
118 //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
119 void wxGetIntFromCFDictionary(CFTypeRef cfDict
, CFStringRef key
, int* pOut
)
122 (CFNumberRef
) CFDictionaryGetValue((CFDictionaryRef
) cfDict
,
124 kCFNumberIntType
, pOut
);
127 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
131 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
133 IMPLEMENT_DYNAMIC_CLASS(wxJoystick
, wxObject
)
135 //---------------------------------------------------------------------------
136 // wxJoystick Constructor
138 // 1) Initializes member variables
139 // 2) Attempts to create the native HID joystick implementation - if none
140 // could be found (no joysticks, etc.) then it sets it to NULL
141 //---------------------------------------------------------------------------
142 wxJoystick::wxJoystick(int joystick
)
143 : m_joystick(joystick
),
146 m_hid
= new wxHIDJoystick();
148 if (m_hid
->Create(m_joystick
))
150 m_thread
= new wxJoystickThread(m_hid
, m_joystick
);
161 //---------------------------------------------------------------------------
162 // wxJoystick Destructor
164 // Releases the capture of the thread, deletes it, and deletes
165 // the native implementation.
166 //---------------------------------------------------------------------------
167 wxJoystick::~wxJoystick()
171 m_thread
->Delete(); // It's detached so it will delete itself
177 //---------------------------------------------------------------------------
178 // wxJoystick::Get[XXX]Position
180 // Returns the value of an axis that was polled from the thread. In the
181 // case of GetPosition returns the X and Y values in a wxPoint
182 //---------------------------------------------------------------------------
183 wxPoint
wxJoystick::GetPosition() const
185 wxPoint
pos(wxDefaultPosition
);
186 if (m_thread
) pos
= m_thread
->m_lastposition
;
189 int wxJoystick::GetZPosition() const
192 return m_thread
->m_axe
[wxJS_AXIS_Z
];
195 int wxJoystick::GetRudderPosition() const
198 return m_thread
->m_axe
[wxJS_AXIS_RUDDER
];
201 int wxJoystick::GetUPosition() const
204 return m_thread
->m_axe
[wxJS_AXIS_U
];
207 int wxJoystick::GetVPosition() const
210 return m_thread
->m_axe
[wxJS_AXIS_V
];
214 //---------------------------------------------------------------------------
215 // wxJoystick::GetButtonState
217 // Returns the state of the buttons in a bitmask as dictated by the
218 // wx manual (the real work takes place in the thread, as always)
219 //---------------------------------------------------------------------------
220 int wxJoystick::GetButtonState() const
223 return m_thread
->m_buttons
;
227 //---------------------------------------------------------------------------
230 // Returns whether the joystick initialized successfully - in this case
231 // if the native implementation doesn't exist (in constructor)
232 //---------------------------------------------------------------------------
233 bool wxJoystick::IsOk() const
235 return m_hid
!= NULL
;
238 //---------------------------------------------------------------------------
239 // wxJoystick::Get[XXX](Id/Name)
241 // Simple accessors to the native HID implementation
242 //---------------------------------------------------------------------------
243 int wxJoystick::GetManufacturerId() const
244 { return m_hid
->m_nManufacturerId
; }
245 int wxJoystick::GetProductId() const
246 { return m_hid
->m_nProductId
; }
247 wxString
wxJoystick::GetProductName() const
248 { return m_hid
->m_szProductName
; }
250 //---------------------------------------------------------------------------
251 // wxJoystick::GetNumberButtons
252 // wxJoystick::GetNumberAxes
254 // Queries the joystick for an active number of buttons/axes.
256 // In the native HID implementation, the cookies:
257 // 0-40 are the buttons of the joystick
258 // 40-50 are the axes of the joystick
260 // These just query the native HID implementation as above.
261 //---------------------------------------------------------------------------
262 int wxJoystick::GetNumberButtons() const
266 for(int nIndex
= 0; nIndex
< 40; ++nIndex
)
268 if(m_hid
->HasElement(nIndex
))
274 int wxJoystick::GetNumberAxes() const
278 for(int nIndex
= 40; nIndex
< 50; ++nIndex
)
280 if(m_hid
->HasElement(nIndex
))
287 //---------------------------------------------------------------------------
288 // wxJoystick::GetNumberJoysticks
290 // Gets the number of joysticks on the system. In HID that
291 // is all devices with the kHIDUsage_GD_Joystick or kHIDUsage_GD_GamePad
293 //---------------------------------------------------------------------------
294 int wxJoystick::GetNumberJoysticks()
297 wxHIDDevice::GetCount(kHIDPage_GenericDesktop
, kHIDUsage_GD_Joystick
) +
298 wxHIDDevice::GetCount(kHIDPage_GenericDesktop
, kHIDUsage_GD_GamePad
);
301 //---------------------------------------------------------------------------
302 // wxJoystick::SetCapture
304 // Stops sending events from the thread to the window set in
305 // SetCapture and stops polling the joystick
306 //---------------------------------------------------------------------------
307 bool wxJoystick::SetCapture(wxWindow
* win
, int pollingFreq
)
311 m_thread
->m_catchwin
= win
;
312 m_thread
->m_polling
= pollingFreq
;
318 //---------------------------------------------------------------------------
319 // wxJoystick::ReleaseCapture
321 // Stops sending events from the thread to the window set in
322 // SetCapture and stops polling the joystick
323 //---------------------------------------------------------------------------
324 bool wxJoystick::ReleaseCapture()
328 m_thread
->m_catchwin
= NULL
;
329 m_thread
->m_polling
= 0;
335 //---------------------------------------------------------------------------
336 // wxJoystick::Get[XXX]
338 // Gets the minimum and maximum values for each axis, returning 0 if the
339 // axis doesn't exist.
340 //---------------------------------------------------------------------------
341 int wxJoystick::GetXMin() const
342 { return m_hid
->m_nXMin
; }
343 int wxJoystick::GetYMin() const
344 { return m_hid
->m_nYMin
; }
345 int wxJoystick::GetZMin() const
346 { return m_hid
->m_nZMin
; }
347 int wxJoystick::GetRudderMin() const
348 { return m_hid
->m_nRudderMin
; }
349 int wxJoystick::GetUMin() const
350 { return m_hid
->m_nUMin
; }
351 int wxJoystick::GetVMin() const
352 { return m_hid
->m_nVMin
; }
354 int wxJoystick::GetXMax() const
355 { return m_hid
->m_nXMax
; }
356 int wxJoystick::GetYMax() const
357 { return m_hid
->m_nYMax
; }
358 int wxJoystick::GetZMax() const
359 { return m_hid
->m_nZMax
; }
360 int wxJoystick::GetRudderMax() const
361 { return m_hid
->m_nRudderMax
; }
362 int wxJoystick::GetUMax() const
363 { return m_hid
->m_nUMax
; }
364 int wxJoystick::GetVMax() const
365 { return m_hid
->m_nVMax
; }
367 //---------------------------------------------------------------------------
368 // wxJoystick::Get[XXX]
370 // Min/Max values for buttons, axes, etc.. Polling in this case is just
371 // what the linux port has.
372 //---------------------------------------------------------------------------
373 int wxJoystick::GetMaxButtons() const
374 { return wxJS_MAX_BUTTONS
; }
375 int wxJoystick::GetMaxAxes() const
376 { return wxJS_MAX_AXES
; }
377 int wxJoystick::GetPollingMin() const
379 int wxJoystick::GetPollingMax() const
382 //---------------------------------------------------------------------------
383 // wxJoystick::Has[XXX]
385 // Just queries the native hid implementation if the cookie was found
386 // when enumerating the cookies of the joystick device
387 //---------------------------------------------------------------------------
388 bool wxJoystick::HasZ() const
389 { return m_hid
->HasElement(wxJS_AXIS_Z
); }
390 bool wxJoystick::HasRudder() const
391 { return m_hid
->HasElement(wxJS_AXIS_RUDDER
); }
392 bool wxJoystick::HasU() const
393 { return m_hid
->HasElement(wxJS_AXIS_U
); }
394 bool wxJoystick::HasV() const
395 { return m_hid
->HasElement(wxJS_AXIS_V
); }
397 //---------------------------------------------------------------------------
399 //---------------------------------------------------------------------------
400 int wxJoystick::GetPOVPosition() const
402 int wxJoystick::GetPOVCTSPosition() const
404 int wxJoystick::GetMovementThreshold() const
406 void wxJoystick::SetMovementThreshold(int threshold
)
408 bool wxJoystick::HasPOV() const
410 bool wxJoystick::HasPOV4Dir() const
412 bool wxJoystick::HasPOVCTS() const
415 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
419 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
421 //---------------------------------------------------------------------------
422 // wxHIDJoystick ctor
424 // Initializes the min/max members
425 //---------------------------------------------------------------------------
426 wxHIDJoystick::wxHIDJoystick() :
427 m_nXMax(0), m_nYMax(0), m_nZMax(0), m_nRudderMax(0), m_nUMax(0), m_nVMax(0),
428 m_nXMin(0), m_nYMin(0), m_nZMin(0), m_nRudderMin(0), m_nUMin(0), m_nVMin(0)
432 //---------------------------------------------------------------------------
433 // wxHIDJoystick dtor
436 //---------------------------------------------------------------------------
437 wxHIDJoystick::~wxHIDJoystick()
441 //---------------------------------------------------------------------------
442 // wxHIDJoystick::Create
444 // Creates the native HID device (joysticks are of either
445 // kHIDUsage_GD_Joystick or kHIDUsage_GD_GamePad)
446 //---------------------------------------------------------------------------
447 bool wxHIDJoystick::Create(int nWhich
)
449 int nJoysticks
= GetCount(kHIDPage_GenericDesktop
, kHIDUsage_GD_Joystick
);
451 if (nWhich
<= nJoysticks
)
452 return wxHIDDevice::Create(kHIDPage_GenericDesktop
, kHIDUsage_GD_Joystick
);
454 nWhich
-= nJoysticks
;
456 int nGamePads
= GetCount(kHIDPage_GenericDesktop
, kHIDUsage_GD_GamePad
);
458 if (nWhich
<= nGamePads
)
459 return wxHIDDevice::Create(kHIDPage_GenericDesktop
, kHIDUsage_GD_GamePad
);
464 //---------------------------------------------------------------------------
465 // wxHIDJoystick::BuildCookies
466 // wxHIDJoystick::MakeCookies
468 // Sets up the cookies for the HID device (called from Create) - as
469 // mentioned 0-40 are the buttons and 40-50 are the axes.
471 // MakeCookies is just a recursive function for each array within
473 //---------------------------------------------------------------------------
474 void wxHIDJoystick::BuildCookies(wxCFArray
& Array
)
476 Array
= CFDictionaryGetValue((CFDictionaryRef
)Array
[0], CFSTR(kIOHIDElementKey
));
477 InitCookies(50, true);
479 memset(m_pCookies
, 0, sizeof(*m_pCookies
) * 50);
482 // I wasted two hours of my life on this line :(
483 // accidently removed it during some source cleaning...
487 //paranoid debugging stuff
489 for(int i
= 0; i
< 50; ++i
)
490 wxPrintf(wxT("\nVAL #%i:[%i]"), i
, m_pCookies
[i
]);
494 void wxHIDJoystick::MakeCookies(wxCFArray
& Array
)
496 int i
, nUsage
, nPage
;
498 for (i
= 0; i
< Array
.Count(); ++i
)
500 const void* ref
= CFDictionaryGetValue((CFDictionaryRef
)Array
[i
], CFSTR(kIOHIDElementKey
));
504 wxCFArray
newarray(ref
);
505 MakeCookies(newarray
);
510 (CFNumberRef
) CFDictionaryGetValue((CFDictionaryRef
) Array
[i
], CFSTR(kIOHIDElementUsageKey
)),
511 kCFNumberIntType
, &nUsage
);
514 (CFNumberRef
) CFDictionaryGetValue((CFDictionaryRef
) Array
[i
], CFSTR(kIOHIDElementUsagePageKey
)),
515 kCFNumberIntType
, &nPage
);
518 wxLogSysError(wxT("[%i][%i]"), nUsage
, nPage
);
520 if (nPage
== kHIDPage_Button
&& nUsage
<= 40)
521 AddCookieInQueue(Array
[i
], nUsage
-1 );
522 else if (nPage
== kHIDPage_GenericDesktop
)
528 AddCookieInQueue(Array
[i
], wxJS_AXIS_X
);
529 wxGetIntFromCFDictionary(Array
[i
], CFSTR(kIOHIDElementMaxKey
),
531 wxGetIntFromCFDictionary(Array
[i
], CFSTR(kIOHIDElementMinKey
),
535 AddCookieInQueue(Array
[i
], wxJS_AXIS_Y
);
536 wxGetIntFromCFDictionary(Array
[i
], CFSTR(kIOHIDElementMaxKey
),
538 wxGetIntFromCFDictionary(Array
[i
], CFSTR(kIOHIDElementMinKey
),
542 AddCookieInQueue(Array
[i
], wxJS_AXIS_Z
);
543 wxGetIntFromCFDictionary(Array
[i
], CFSTR(kIOHIDElementMaxKey
),
545 wxGetIntFromCFDictionary(Array
[i
], CFSTR(kIOHIDElementMinKey
),
552 else if (nPage
== kHIDPage_Simulation
&& nUsage
== kHIDUsage_Sim_Rudder
)
555 AddCookieInQueue(Array
[i
], wxJS_AXIS_RUDDER
);
556 wxGetIntFromCFDictionary(Array
[i
], CFSTR(kIOHIDElementMaxKey
),
558 wxGetIntFromCFDictionary(Array
[i
], CFSTR(kIOHIDElementMinKey
),
565 //---------------------------------------------------------------------------
566 // wxHIDJoystick::Get[XXX]
568 // Simple accessors so that the HID callback and the thread procedure
569 // can access members from wxHIDDevice (our parent here).
570 //---------------------------------------------------------------------------
571 IOHIDElementCookie
* wxHIDJoystick::GetCookies()
572 { return m_pCookies
; }
573 IOHIDQueueInterface
** wxHIDJoystick::GetQueue()
574 { return m_ppQueue
; }
576 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
580 //+++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++
582 //---------------------------------------------------------------------------
583 // wxJoystickThread Constructor
585 // Just initializes members
586 //---------------------------------------------------------------------------
587 wxJoystickThread::wxJoystickThread(wxHIDJoystick
* hid
, int joystick
)
589 m_joystick(joystick
),
590 m_lastposition(127,127),
595 memset(m_axe
, 0, sizeof(int) * wxJS_MAX_AXES
);
598 //---------------------------------------------------------------------------
599 // wxJoystickThread::Entry
603 // Runs a CFRunLoop for polling. Basically, it sets the HID queue to
604 // call wxJoystickThread::HIDCallback in the context of this thread
605 // when something changes on the device. It polls as long as the user
606 // wants, or a certain amount if the user wants to "block". Note that
607 // we don't actually block here since this is in a secondary thread.
608 //---------------------------------------------------------------------------
609 void* wxJoystickThread::Entry()
611 CFRunLoopSourceRef pRLSource
= NULL
;
613 if ((*m_hid
->GetQueue())->createAsyncEventSource(
614 m_hid
->GetQueue(), &pRLSource
) != kIOReturnSuccess
)
616 wxLogSysError(wxT("Couldn't create async event source"));
620 wxASSERT(pRLSource
!= NULL
);
622 //attach runloop source to main run loop in thread
623 CFRunLoopRef pRL
= CFRunLoopGetCurrent();
624 CFRunLoopAddSource(pRL
, pRLSource
, kCFRunLoopDefaultMode
);
625 wxASSERT( CFRunLoopContainsSource(pRL
, pRLSource
, kCFRunLoopDefaultMode
) );
628 if( (*m_hid
->GetQueue())->setEventCallout(m_hid
->GetQueue(),
629 wxJoystickThread::HIDCallback
, this, this) != kIOReturnSuccess
)
631 wxLogSysError(wxT("Could not set event callout for queue"));
635 if( (*m_hid
->GetQueue())->start(m_hid
->GetQueue()) != kIOReturnSuccess
)
637 wxLogSysError(wxT("Could not start queue"));
649 dTime
= 0.0001 * m_polling
;
651 dTime
= 0.0001 * 10; // check at least every 10 msec in "blocking" case
653 //true just "handles and returns" - false forces it to stay the time
656 CFRunLoopRunInMode(kCFRunLoopDefaultMode
, dTime
, true);
659 HIDCallback(this, ret
, this, this);
664 wxASSERT( CFRunLoopContainsSource(pRL
, pRLSource
, kCFRunLoopDefaultMode
) );
666 CFRunLoopRemoveSource(pRL
, pRLSource
, kCFRunLoopDefaultMode
);
667 CFRelease(pRLSource
);
672 //---------------------------------------------------------------------------
673 // wxJoystickThread::HIDCallback (static)
675 // Callback for the native HID device when it recieves input.
677 // This is where the REAL dirty work gets done.
679 // 1) Loops through each event the queue has recieved
680 // 2) First, checks if the thread that is running the loop for
681 // the polling has ended - if so it breaks out
682 // 3) Next, it checks if there was an error getting this event from
683 // the HID queue, if there was, it logs an error and returns
684 // 4) Now it does the real dirty work by getting the button states
685 // from cookies 0-40 and axes positions/states from cookies 40-50
686 // in the native HID device by quering cookie values.
687 // 5) Sends the event to the polling window (if any)
688 // 6) Gets the next event and goes back to (1)
689 //---------------------------------------------------------------------------
690 /*static*/ void wxJoystickThread::HIDCallback(void* target
, IOReturn res
,
691 void* context
, void* sender
)
693 IOHIDEventStruct hidevent
;
694 AbsoluteTime bogustime
= {0,0};
696 wxJoystickThread
* pThis
= (wxJoystickThread
*) context
;
697 wxHIDJoystick
* m_hid
= pThis
->m_hid
;
699 //Get the "first" event from the queue
700 //bogustime tells it we don't care at what time to start
701 //where it gets the next from
702 ret
= (*m_hid
->GetQueue())->getNextEvent(m_hid
->GetQueue(),
703 &hidevent
, bogustime
, 0);
705 while (ret
!= kIOReturnUnderrun
)
707 if (pThis
->TestDestroy())
710 if(ret
!= kIOReturnSuccess
)
712 wxLogSysError(wxString::Format(wxT("wxJoystick Error:[%i]"), ret
));
716 wxJoystickEvent wxevent
;
718 //Find the cookie that changed
720 IOHIDElementCookie
* pCookies
= m_hid
->GetCookies();
723 if(hidevent
.elementCookie
== pCookies
[nIndex
])
733 wxLogSysError(wxString::Format(wxT("wxJoystick Out Of Bounds Error")));
738 //is the cookie a button?
743 pThis
->m_buttons
|= (1 << nIndex
);
744 wxevent
.SetEventType(wxEVT_JOY_BUTTON_DOWN
);
748 pThis
->m_buttons
&= ~(1 << nIndex
);
749 wxevent
.SetEventType(wxEVT_JOY_BUTTON_UP
);
752 wxevent
.SetButtonChange(nIndex
+1);
754 else if (nIndex
== wxJS_AXIS_X
)
756 pThis
->m_lastposition
.x
= hidevent
.value
;
757 wxevent
.SetEventType(wxEVT_JOY_MOVE
);
758 pThis
->m_axe
[0] = hidevent
.value
;
760 else if (nIndex
== wxJS_AXIS_Y
)
762 pThis
->m_lastposition
.y
= hidevent
.value
;
763 wxevent
.SetEventType(wxEVT_JOY_MOVE
);
764 pThis
->m_axe
[1] = hidevent
.value
;
766 else if (nIndex
== wxJS_AXIS_Z
)
768 wxevent
.SetEventType(wxEVT_JOY_ZMOVE
);
769 pThis
->m_axe
[2] = hidevent
.value
;
772 wxevent
.SetEventType(wxEVT_JOY_MOVE
);
774 Nanoseconds timestamp
= AbsoluteToNanoseconds(hidevent
.timestamp
);
776 wxULongLong
llTime(timestamp
.hi
, timestamp
.lo
);
780 wxevent
.SetTimestamp(llTime
.GetValue());
781 wxevent
.SetJoystick(pThis
->m_joystick
);
782 wxevent
.SetButtonState(pThis
->m_buttons
);
783 wxevent
.SetPosition(pThis
->m_lastposition
);
784 wxevent
.SetZPosition(pThis
->m_axe
[2]);
785 wxevent
.SetEventObject(pThis
->m_catchwin
);
787 if (pThis
->m_catchwin
)
788 pThis
->m_catchwin
->AddPendingEvent(wxevent
);
790 ret
= (*m_hid
->GetQueue())->getNextEvent(m_hid
->GetQueue(),
791 &hidevent
, bogustime
, 0);
795 #endif // wxUSE_JOYSTICK && defined(__DARWIN__)