]>
Commit | Line | Data |
---|---|---|
ec8bd392 RN |
1 | ///////////////////////////////////////////////////////////////////////////// |
2 | // Name: hid.cpp | |
3 | // Purpose: DARWIN HID layer for WX Implementation | |
4 | // Author: Ryan Norton | |
5 | // Modified by: | |
6 | // Created: 11/11/2003 | |
7 | // RCS-ID: $Id$ | |
8 | // Copyright: (c) Ryan Norton | |
9 | // Licence: wxWindows licence | |
10 | ///////////////////////////////////////////////////////////////////////////// | |
11 | ||
1553abf4 RN |
12 | // =========================================================================== |
13 | // declarations | |
14 | // =========================================================================== | |
15 | ||
16 | // --------------------------------------------------------------------------- | |
17 | // headers | |
18 | // --------------------------------------------------------------------------- | |
19 | ||
3d1a4878 | 20 | #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA) |
1553abf4 RN |
21 | #pragma implementation "hid.h" |
22 | #endif | |
23 | ||
24 | // For compilers that support precompilation, includes "wx.h". | |
25 | #include "wx/wxprec.h" | |
26 | ||
27 | #ifdef __BORLANDC__ | |
28 | #pragma hdrstop | |
29 | #endif | |
30 | ||
ec8bd392 RN |
31 | //DARWIN _ONLY_ |
32 | #ifdef __DARWIN__ | |
33 | ||
4cb1d3da | 34 | #include "wx/mac/corefoundation/hid.h" |
ec8bd392 RN |
35 | #include "wx/string.h" |
36 | #include "wx/log.h" | |
65442ab6 | 37 | #include "wx/mac/corefoundation/cfstring.h" |
7f71c4c8 | 38 | |
1553abf4 RN |
39 | |
40 | // --------------------------------------------------------------------------- | |
41 | // assertion macros | |
42 | // --------------------------------------------------------------------------- | |
43 | ||
7f71c4c8 RN |
44 | #define wxFORCECHECK_MSG(arg, msg) \ |
45 | {\ | |
46 | if (arg) \ | |
47 | {\ | |
48 | wxLogSysError(wxString::Format(wxT("Message:%s\nHID: %s failed!"), wxT(msg), wxT(#arg)));\ | |
49 | return false;\ | |
50 | }\ | |
51 | } | |
52 | #define wxIOCHECK(arg, msg) wxFORCECHECK_MSG(arg != kIOReturnSuccess, msg) | |
53 | #define wxKERNCHECK(arg, msg) wxFORCECHECK_MSG(arg != KERN_SUCCESS, msg) | |
54 | #define wxSCHECK(arg, msg) wxFORCECHECK_MSG(arg != S_OK, msg) | |
55 | ||
ec8bd392 RN |
56 | #ifdef __WXDEBUG___ |
57 | # define wxVERIFY(arg) wxASSERT(arg) | |
58 | #else | |
59 | # define wxVERIFY(arg) arg | |
60 | #endif | |
61 | ||
62 | /* | |
7f71c4c8 RN |
63 | void CFShowTypeIDDescription(CFTypeRef pData) |
64 | { | |
65 | if(!pData) | |
66 | { | |
ec8bd392 | 67 | wxASSERT(false); |
7f71c4c8 RN |
68 | return; |
69 | } | |
70 | ||
71 | wxMessageBox( | |
72 | CFStringGetCStringPtr( | |
73 | CFCopyTypeIDDescription(CFGetTypeID(pData)),CFStringGetSystemEncoding() | |
74 | ) | |
75 | ); | |
76 | } | |
ec8bd392 | 77 | */ |
7f71c4c8 RN |
78 | |
79 | // ============================================================================ | |
80 | // implementation | |
81 | // ============================================================================ | |
82 | ||
1553abf4 RN |
83 | // --------------------------------------------------------------------------- |
84 | // wxHIDDevice | |
85 | // --------------------------------------------------------------------------- | |
7f71c4c8 | 86 | |
4cb1d3da | 87 | bool wxHIDDevice::Create (int nClass, int nType, int nDev) |
7f71c4c8 RN |
88 | { |
89 | //Create the mach port | |
90 | wxIOCHECK(IOMasterPort(bootstrap_port, &m_pPort), "Could not create mach port"); | |
91 | ||
92 | //Dictionary that will hold first | |
93 | //the matching dictionary for determining which kind of devices we want, | |
94 | //then later some registry properties from an iterator (see below) | |
95 | CFMutableDictionaryRef pDictionary; | |
96 | ||
97 | //Create a dictionary | |
98 | //The call to IOServiceMatching filters down the | |
99 | //the services we want to hid services (and also eats the | |
100 | //dictionary up for us (consumes one reference)) | |
ec8bd392 | 101 | wxVERIFY((pDictionary = IOServiceMatching(kIOHIDDeviceKey)) != NULL ); |
7f71c4c8 RN |
102 | |
103 | //Here we'll filter down the services to what we want | |
104 | if (nType != -1) | |
105 | { | |
106 | CFNumberRef pType = CFNumberCreate(kCFAllocatorDefault, | |
107 | kCFNumberIntType, &nType); | |
108 | CFDictionarySetValue(pDictionary, CFSTR(kIOHIDPrimaryUsageKey), pType); | |
109 | CFRelease(pType); | |
110 | } | |
111 | if (nClass != -1) | |
112 | { | |
113 | CFNumberRef pClass = CFNumberCreate(kCFAllocatorDefault, | |
114 | kCFNumberIntType, &nClass); | |
115 | CFDictionarySetValue(pDictionary, CFSTR(kIOHIDPrimaryUsagePageKey), pClass); | |
116 | CFRelease(pClass); | |
117 | } | |
118 | ||
119 | //Now get the maching services | |
120 | io_iterator_t pIterator; | |
121 | wxIOCHECK(IOServiceGetMatchingServices(m_pPort, pDictionary, &pIterator), "No Matching HID Services"); | |
65442ab6 | 122 | wxASSERT_MSG(pIterator != 0, wxT("No devices found!")); |
7f71c4c8 RN |
123 | |
124 | //Now we iterate through them | |
125 | io_object_t pObject; | |
b409fa19 | 126 | while ( (pObject = IOIteratorNext(pIterator)) != 0) |
7f71c4c8 | 127 | { |
4cb1d3da RN |
128 | if(--nDev != 0) |
129 | continue; | |
130 | ||
ec8bd392 | 131 | wxVERIFY(IORegistryEntryCreateCFProperties(pObject, &pDictionary, |
7f71c4c8 RN |
132 | kCFAllocatorDefault, kNilOptions) == KERN_SUCCESS); |
133 | ||
134 | //Just for sanity :) | |
342689c8 | 135 | wxASSERT(CFGetTypeID(CFDictionaryGetValue(pDictionary, CFSTR(kIOHIDProductKey))) == CFStringGetTypeID()); |
7f71c4c8 | 136 | |
65442ab6 RN |
137 | /* |
138 | kIOHIDTransportKey; | |
139 | kIOHIDVendorIDKey; | |
140 | kIOHIDProductIDKey; | |
141 | kIOHIDVersionNumberKey; | |
142 | kIOHIDManufacturerKey; | |
143 | kIOHIDSerialNumberKey; | |
144 | if !kIOHIDLocationIDKey | |
145 | kUSBDevicePropertyLocationID | |
146 | kIOHIDPrimaryUsageKey | |
147 | kIOHIDPrimaryUsagePageKey | |
148 | idProduct | |
149 | idVendor | |
150 | USB Product Name | |
151 | */ | |
7f71c4c8 | 152 | //Get [product] name |
65442ab6 RN |
153 | m_szProductName = wxMacCFStringHolder( (CFStringRef) CFDictionaryGetValue(pDictionary, CFSTR(kIOHIDProductKey)), false ).AsString(); |
154 | ||
2ff29ddf RN |
155 | CFNumberRef nref = (CFNumberRef) CFDictionaryGetValue(pDictionary, CFSTR(kIOHIDProductIDKey)); |
156 | ||
157 | if (nref) | |
158 | CFNumberGetValue( | |
159 | nref, | |
65442ab6 RN |
160 | kCFNumberIntType, |
161 | &m_nProductId | |
162 | ); | |
163 | ||
2ff29ddf RN |
164 | nref = (CFNumberRef) CFDictionaryGetValue(pDictionary, CFSTR(kIOHIDVendorIDKey)); |
165 | if (nref) | |
65442ab6 | 166 | CFNumberGetValue( |
2ff29ddf | 167 | nref, |
65442ab6 RN |
168 | kCFNumberIntType, |
169 | &m_nManufacturerId | |
170 | ); | |
7f71c4c8 | 171 | |
7f71c4c8 RN |
172 | //Create the interface (good grief - long function names!) |
173 | SInt32 nScore; | |
174 | IOCFPlugInInterface** ppPlugin; | |
175 | wxIOCHECK(IOCreatePlugInInterfaceForService(pObject, kIOHIDDeviceUserClientTypeID, | |
176 | kIOCFPlugInInterfaceID, &ppPlugin, &nScore), ""); | |
177 | ||
178 | //Now, the final thing we can check before we fall back to asserts | |
179 | //(because the dtor only checks if the device is ok, so if anything | |
180 | //fails from now on the dtor will delete the device anyway, so we can't break from this). | |
181 | ||
182 | //Get the HID interface from the plugin to the mach port | |
183 | wxSCHECK((*ppPlugin)->QueryInterface(ppPlugin, | |
184 | CFUUIDGetUUIDBytes(kIOHIDDeviceInterfaceID), (void**) &m_ppDevice), ""); | |
185 | ||
186 | //release the plugin | |
187 | (*ppPlugin)->Release(ppPlugin); | |
188 | ||
189 | //open the HID interface... | |
ec8bd392 | 190 | wxVERIFY((*m_ppDevice)->open(m_ppDevice, 0) == S_OK); |
7f71c4c8 | 191 | |
4cb1d3da RN |
192 | // |
193 | //Now the hard part - in order to scan things we need "cookies" - | |
194 | // | |
195 | wxCFArray CookieArray = CFDictionaryGetValue(pDictionary, CFSTR(kIOHIDElementKey)); | |
196 | BuildCookies(CookieArray); | |
4cb1d3da | 197 | |
7f71c4c8 RN |
198 | //cleanup |
199 | CFRelease(pDictionary); | |
200 | IOObjectRelease(pObject); | |
201 | break; | |
202 | } | |
203 | //iterator cleanup | |
204 | IOObjectRelease(pIterator); | |
205 | ||
206 | return true; | |
207 | }//end Create() | |
208 | ||
4cb1d3da RN |
209 | int wxHIDDevice::GetCount (int nClass, int nType) |
210 | { | |
211 | mach_port_t m_pPort; | |
212 | ||
213 | //Create the mach port | |
214 | wxIOCHECK(IOMasterPort(bootstrap_port, &m_pPort), "Could not create mach port"); | |
215 | ||
216 | //Dictionary that will hold first | |
217 | //the matching dictionary for determining which kind of devices we want, | |
218 | //then later some registry properties from an iterator (see below) | |
219 | CFMutableDictionaryRef pDictionary; | |
220 | ||
221 | //Create a dictionary | |
222 | //The call to IOServiceMatching filters down the | |
223 | //the services we want to hid services (and also eats the | |
224 | //dictionary up for us (consumes one reference)) | |
225 | wxVERIFY((pDictionary = IOServiceMatching(kIOHIDDeviceKey)) != NULL ); | |
226 | ||
227 | //Here we'll filter down the services to what we want | |
228 | if (nType != -1) | |
229 | { | |
230 | CFNumberRef pType = CFNumberCreate(kCFAllocatorDefault, | |
231 | kCFNumberIntType, &nType); | |
232 | CFDictionarySetValue(pDictionary, CFSTR(kIOHIDPrimaryUsageKey), pType); | |
233 | CFRelease(pType); | |
234 | } | |
235 | if (nClass != -1) | |
236 | { | |
237 | CFNumberRef pClass = CFNumberCreate(kCFAllocatorDefault, | |
238 | kCFNumberIntType, &nClass); | |
239 | CFDictionarySetValue(pDictionary, CFSTR(kIOHIDPrimaryUsagePageKey), pClass); | |
240 | CFRelease(pClass); | |
241 | } | |
242 | ||
243 | //Now get the maching services | |
244 | io_iterator_t pIterator; | |
245 | wxIOCHECK(IOServiceGetMatchingServices(m_pPort, pDictionary, &pIterator), "No Matching HID Services"); | |
65442ab6 RN |
246 | |
247 | if(pIterator == NULL) | |
248 | return 0; | |
4cb1d3da RN |
249 | |
250 | //Now we iterate through them | |
251 | io_object_t pObject; | |
252 | ||
253 | int nCount = 0; | |
254 | ||
255 | while ( (pObject = IOIteratorNext(pIterator)) != 0) | |
256 | ++nCount; | |
257 | ||
258 | //iterator cleanup | |
259 | IOObjectRelease(pIterator); | |
260 | ||
261 | return nCount; | |
262 | }//end Create() | |
263 | ||
264 | void wxHIDDevice::AddCookie(CFTypeRef Data, int i) | |
7f71c4c8 RN |
265 | { |
266 | CFNumberGetValue( | |
267 | (CFNumberRef) CFDictionaryGetValue ( (CFDictionaryRef) Data | |
268 | , CFSTR(kIOHIDElementCookieKey) | |
269 | ), | |
270 | kCFNumberIntType, | |
271 | &m_pCookies[i] | |
272 | ); | |
273 | } | |
274 | ||
4cb1d3da | 275 | void wxHIDDevice::AddCookieInQueue(CFTypeRef Data, int i) |
7f71c4c8 RN |
276 | { |
277 | AddCookie(Data, i); | |
ec8bd392 | 278 | wxVERIFY((*m_ppQueue)->addElement(m_ppQueue, m_pCookies[i], 0) == S_OK);//3rd Param flags (none yet) |
7f71c4c8 RN |
279 | } |
280 | ||
4cb1d3da | 281 | void wxHIDDevice::InitCookies(size_t dwSize, bool bQueue) |
7f71c4c8 RN |
282 | { |
283 | m_pCookies = new IOHIDElementCookie[dwSize]; | |
284 | if (bQueue) | |
285 | { | |
4cb1d3da | 286 | wxASSERT( m_ppQueue == NULL); |
ec8bd392 RN |
287 | wxVERIFY( (m_ppQueue = (*m_ppDevice)->allocQueue(m_ppDevice)) != NULL); |
288 | wxVERIFY( (*m_ppQueue)->create(m_ppQueue, 0, 512) == S_OK); //Param 2, flags, none yet | |
7f71c4c8 RN |
289 | } |
290 | } | |
291 | ||
4cb1d3da | 292 | bool wxHIDDevice::IsActive(int nIndex) |
7f71c4c8 RN |
293 | { |
294 | wxASSERT(m_pCookies[nIndex] != NULL); | |
295 | IOHIDEventStruct Event; | |
296 | (*m_ppDevice)->getElementValue(m_ppDevice, m_pCookies[nIndex], &Event); | |
297 | return !!Event.value; | |
298 | } | |
299 | ||
4cb1d3da RN |
300 | bool wxHIDDevice::HasElement(int nIndex) |
301 | { | |
302 | return m_pCookies[nIndex] != NULL; | |
303 | } | |
7f71c4c8 RN |
304 | |
305 | wxHIDDevice::~wxHIDDevice() | |
306 | { | |
307 | if (m_ppDevice != NULL) | |
308 | { | |
4cb1d3da RN |
309 | if (m_ppQueue != NULL) |
310 | { | |
311 | (*m_ppQueue)->stop(m_ppQueue); | |
312 | (*m_ppQueue)->dispose(m_ppQueue); | |
313 | (*m_ppQueue)->Release(m_ppQueue); | |
314 | } | |
7f71c4c8 RN |
315 | (*m_ppDevice)->close(m_ppDevice); |
316 | (*m_ppDevice)->Release(m_ppDevice); | |
317 | mach_port_deallocate(mach_task_self(), m_pPort); | |
318 | } | |
319 | ||
320 | if (m_pCookies != NULL) | |
321 | { | |
322 | delete [] m_pCookies; | |
7f71c4c8 RN |
323 | } |
324 | } | |
7f71c4c8 | 325 | |
1553abf4 RN |
326 | // --------------------------------------------------------------------------- |
327 | // wxHIDKeyboard | |
328 | // --------------------------------------------------------------------------- | |
329 | ||
7f71c4c8 RN |
330 | enum |
331 | { | |
332 | WXK_RSHIFT = 400, | |
333 | WXK_RALT, | |
334 | WXK_RCONTROL, | |
335 | WXK_RMENU | |
336 | ||
337 | }; | |
338 | ||
339 | bool wxHIDKeyboard::Create() | |
340 | { | |
341 | return wxHIDDevice::Create(kHIDPage_GenericDesktop, kHIDUsage_GD_Keyboard); | |
342 | } | |
343 | ||
344 | void wxHIDKeyboard::BuildCookies(wxCFArray& Array) | |
345 | { | |
346 | Array = CFDictionaryGetValue((CFDictionaryRef)Array[0], CFSTR(kIOHIDElementKey)); | |
347 | InitCookies(500); | |
348 | int i, | |
349 | nUsage; | |
350 | for (i = 0; i < Array.Count(); ++i) | |
351 | { | |
352 | CFNumberGetValue( | |
353 | (CFNumberRef) CFDictionaryGetValue((CFDictionaryRef) Array[i], CFSTR(kIOHIDElementUsageKey)), | |
354 | kCFNumberLongType, &nUsage); | |
355 | ||
356 | if (nUsage >= kHIDUsage_KeyboardA && nUsage <= kHIDUsage_KeyboardZ) | |
357 | AddCookie(Array[i], 'A' + (nUsage - kHIDUsage_KeyboardA) ); | |
358 | else if (nUsage >= kHIDUsage_Keyboard1 && nUsage <= kHIDUsage_Keyboard9) | |
359 | AddCookie(Array[i], '1' + (nUsage - kHIDUsage_Keyboard1) ); | |
360 | else if (nUsage >= kHIDUsage_KeyboardF1 && nUsage <= kHIDUsage_KeyboardF12) | |
361 | AddCookie(Array[i], WXK_F1 + (nUsage - kHIDUsage_KeyboardF1) ); | |
362 | else if (nUsage >= kHIDUsage_KeyboardF13 && nUsage <= kHIDUsage_KeyboardF24) | |
363 | AddCookie(Array[i], WXK_F13 + (nUsage - kHIDUsage_KeyboardF13) ); | |
364 | else if (nUsage >= kHIDUsage_Keypad1 && nUsage <= kHIDUsage_Keypad9) | |
365 | AddCookie(Array[i], WXK_NUMPAD1 + (nUsage - kHIDUsage_Keypad1) ); | |
366 | else switch (nUsage) | |
367 | { | |
368 | //0's (wx & ascii go 0-9, but HID goes 1-0) | |
369 | case kHIDUsage_Keyboard0: | |
370 | AddCookie(Array[i],'0'); | |
371 | break; | |
372 | case kHIDUsage_Keypad0: | |
373 | AddCookie(Array[i],WXK_NUMPAD0); | |
374 | break; | |
375 | ||
376 | //Basic | |
377 | case kHIDUsage_KeyboardReturnOrEnter: | |
378 | AddCookie(Array[i], WXK_RETURN); | |
379 | break; | |
380 | case kHIDUsage_KeyboardEscape: | |
381 | AddCookie(Array[i], WXK_ESCAPE); | |
382 | break; | |
383 | case kHIDUsage_KeyboardDeleteOrBackspace: | |
384 | AddCookie(Array[i], WXK_BACK); | |
385 | break; | |
386 | case kHIDUsage_KeyboardTab: | |
387 | AddCookie(Array[i], WXK_TAB); | |
388 | break; | |
389 | case kHIDUsage_KeyboardSpacebar: | |
390 | AddCookie(Array[i], WXK_SPACE); | |
391 | break; | |
392 | case kHIDUsage_KeyboardPageUp: | |
393 | AddCookie(Array[i], WXK_PRIOR); | |
394 | break; | |
395 | case kHIDUsage_KeyboardEnd: | |
396 | AddCookie(Array[i], WXK_END); | |
397 | break; | |
398 | case kHIDUsage_KeyboardPageDown: | |
399 | AddCookie(Array[i], WXK_NEXT); | |
400 | break; | |
401 | case kHIDUsage_KeyboardRightArrow: | |
402 | AddCookie(Array[i], WXK_RIGHT); | |
403 | break; | |
404 | case kHIDUsage_KeyboardLeftArrow: | |
405 | AddCookie(Array[i], WXK_LEFT); | |
406 | break; | |
407 | case kHIDUsage_KeyboardDownArrow: | |
408 | AddCookie(Array[i], WXK_DOWN); | |
409 | break; | |
410 | case kHIDUsage_KeyboardUpArrow: | |
411 | AddCookie(Array[i], WXK_UP); | |
412 | break; | |
413 | ||
414 | //LEDS | |
415 | case kHIDUsage_KeyboardCapsLock: | |
416 | AddCookie(Array[i],WXK_CAPITAL); | |
417 | break; | |
418 | case kHIDUsage_KeypadNumLock: | |
419 | AddCookie(Array[i],WXK_NUMLOCK); | |
420 | break; | |
421 | case kHIDUsage_KeyboardScrollLock: | |
422 | AddCookie(Array[i],WXK_SCROLL); | |
423 | break; | |
424 | ||
425 | //Menu keys, Shift, other specials | |
426 | case kHIDUsage_KeyboardLeftControl: | |
427 | AddCookie(Array[i],WXK_CONTROL); | |
428 | break; | |
429 | case kHIDUsage_KeyboardLeftShift: | |
430 | AddCookie(Array[i],WXK_SHIFT); | |
431 | break; | |
432 | case kHIDUsage_KeyboardLeftAlt: | |
433 | AddCookie(Array[i],WXK_ALT); | |
434 | break; | |
435 | case kHIDUsage_KeyboardLeftGUI: | |
436 | AddCookie(Array[i],WXK_MENU); | |
437 | break; | |
438 | case kHIDUsage_KeyboardRightControl: | |
439 | AddCookie(Array[i],WXK_RCONTROL); | |
440 | break; | |
441 | case kHIDUsage_KeyboardRightShift: | |
442 | AddCookie(Array[i],WXK_RSHIFT); | |
443 | break; | |
444 | case kHIDUsage_KeyboardRightAlt: | |
445 | AddCookie(Array[i],WXK_RALT); | |
446 | break; | |
447 | case kHIDUsage_KeyboardRightGUI: | |
448 | AddCookie(Array[i],WXK_RMENU); | |
449 | break; | |
450 | ||
451 | //Default | |
452 | default: | |
453 | //not in wx keycodes - do nothing.... | |
454 | break; | |
455 | } | |
456 | } | |
457 | }//end buildcookies | |
ec8bd392 | 458 | |
4cb1d3da RN |
459 | // |
460 | // wxGetKeyState | |
461 | // | |
462 | ||
463 | #include "wx/utils.h" | |
464 | #include "wx/module.h" | |
465 | ||
466 | class wxHIDModule : public wxModule | |
467 | { | |
468 | DECLARE_DYNAMIC_CLASS(wxHIDModule) | |
469 | ||
470 | public: | |
471 | static wxHIDKeyboard* sm_keyboard; | |
472 | ||
473 | virtual bool OnInit() | |
474 | { | |
475 | sm_keyboard = NULL; | |
476 | return true; | |
477 | } | |
478 | virtual void OnExit() | |
479 | { | |
480 | if (sm_keyboard) | |
481 | delete sm_keyboard; | |
482 | } | |
483 | }; | |
484 | ||
485 | IMPLEMENT_DYNAMIC_CLASS(wxHIDModule, wxModule) | |
486 | ||
487 | wxHIDKeyboard* wxHIDModule::sm_keyboard; | |
488 | ||
489 | bool wxGetKeyState (wxKeyCode key) | |
490 | { | |
491 | if (!wxHIDModule::sm_keyboard) | |
492 | { | |
493 | wxHIDModule::sm_keyboard = new wxHIDKeyboard(); | |
494 | bool bOK = wxHIDModule::sm_keyboard->Create(); | |
495 | wxASSERT(bOK); | |
496 | if(!bOK) | |
497 | { | |
498 | delete wxHIDModule::sm_keyboard; | |
499 | wxHIDModule::sm_keyboard = NULL; | |
500 | return false; | |
501 | } | |
502 | } | |
503 | ||
504 | return wxHIDModule::sm_keyboard->IsActive(key); | |
505 | } | |
506 | ||
ec8bd392 | 507 | #endif //__DARWIN__ |