Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

4431 lines
133 KiB

  1. /*****************************************************************************
  2. *
  3. * DIHid.c
  4. *
  5. * Copyright (c) 1996 Microsoft Corporation. All Rights Reserved.
  6. *
  7. * Abstract:
  8. *
  9. * The HID device callback.
  10. *
  11. * Contents:
  12. *
  13. * CHid_New
  14. *
  15. *****************************************************************************/
  16. #include "dinputpr.h"
  17. /*****************************************************************************
  18. *
  19. * The sqiffle for this file.
  20. *
  21. *****************************************************************************/
  22. #define sqfl sqflHidDev
  23. /*****************************************************************************
  24. *
  25. * Declare the interfaces we will be providing.
  26. *
  27. *****************************************************************************/
  28. Primary_Interface(CHid, IDirectInputDeviceCallback);
  29. Interface_Template_Begin(CHid)
  30. Primary_Interface_Template(CHid, IDirectInputDeviceCallback)
  31. Interface_Template_End(CHid)
  32. /*****************************************************************************
  33. *
  34. * Forward declarations
  35. *
  36. * These are out of laziness, not out of necessity.
  37. *
  38. *****************************************************************************/
  39. LRESULT CALLBACK
  40. CHid_SubclassProc(HWND hwnd, UINT wm, WPARAM wp, LPARAM lp,
  41. UINT_PTR uid, ULONG_PTR dwRef);
  42. STDMETHODIMP_(DWORD) CHid_GetUsage(PDICB pdcb, int iobj);
  43. /*****************************************************************************
  44. *
  45. * Hid devices are totally arbitrary, so there is nothing static we
  46. * can cook up to describe them. We generate all the information on
  47. * the fly.
  48. *
  49. *****************************************************************************/
  50. /*****************************************************************************
  51. *
  52. * Auxiliary helper definitions for CHid.
  53. *
  54. *****************************************************************************/
  55. #define ThisClass CHid
  56. #define ThisInterface IDirectInputDeviceCallback
  57. #define riidExpected &IID_IDirectInputDeviceCallback
  58. /*****************************************************************************
  59. *
  60. * CHid::QueryInterface (from IUnknown)
  61. * CHid::AddRef (from IUnknown)
  62. * CHid::Release (from IUnknown)
  63. *
  64. *****************************************************************************/
  65. /*****************************************************************************
  66. *
  67. * @doc INTERNAL
  68. *
  69. * @method HRESULT | CHid | QueryInterface |
  70. *
  71. * Gives a client access to other interfaces on an object.
  72. *
  73. * @cwrap LPDIRECTINPUT | lpDirectInput
  74. *
  75. * @parm IN REFIID | riid |
  76. *
  77. * The requested interface's IID.
  78. *
  79. * @parm OUT LPVOID * | ppvObj |
  80. *
  81. * Receives a pointer to the obtained interface.
  82. *
  83. * @returns
  84. *
  85. * Returns a COM error code.
  86. *
  87. * @xref OLE documentation for <mf IUnknown::QueryInterface>.
  88. *
  89. *****************************************************************************
  90. *
  91. * @doc INTERNAL
  92. *
  93. * @method HRESULT | CHid | AddRef |
  94. *
  95. * Increments the reference count for the interface.
  96. *
  97. * @cwrap LPDIRECTINPUT | lpDirectInput
  98. *
  99. * @returns
  100. *
  101. * Returns the object reference count.
  102. *
  103. * @xref OLE documentation for <mf IUnknown::AddRef>.
  104. *
  105. *****************************************************************************
  106. *
  107. * @doc INTERNAL
  108. *
  109. * @method HRESULT | CHid | Release |
  110. *
  111. * Decrements the reference count for the interface.
  112. * If the reference count on the object falls to zero,
  113. * the object is freed from memory.
  114. *
  115. * @cwrap LPDIRECTINPUT | lpDirectInput
  116. *
  117. * @returns
  118. *
  119. * Returns the object reference count.
  120. *
  121. * @xref OLE documentation for <mf IUnknown::Release>.
  122. *
  123. *****************************************************************************
  124. *
  125. * @doc INTERNAL
  126. *
  127. * @method HRESULT | CHid | QIHelper |
  128. *
  129. * We don't have any dynamic interfaces and simply forward
  130. * to <f Common_QIHelper>.
  131. *
  132. *
  133. * @parm IN REFIID | riid |
  134. *
  135. * The requested interface's IID.
  136. *
  137. * @parm OUT LPVOID * | ppvObj |
  138. *
  139. * Receives a pointer to the obtained interface.
  140. *
  141. *****************************************************************************/
  142. #ifdef DEBUG
  143. Default_QueryInterface(CHid)
  144. Default_AddRef(CHid)
  145. Default_Release(CHid)
  146. #else
  147. #define CHid_QueryInterface Common_QueryInterface
  148. #define CHid_AddRef Common_AddRef
  149. #define CHid_Release Common_Release
  150. #endif
  151. #define CHid_QIHelper Common_QIHelper
  152. /*****************************************************************************
  153. *
  154. * @doc INTERNAL
  155. *
  156. * @method void | CHid | RemoveSubclass |
  157. *
  158. * Remove our subclass hook on the window.
  159. *
  160. *****************************************************************************/
  161. void INTERNAL
  162. CHid_RemoveSubclass(PCHID this)
  163. {
  164. /*
  165. * !! All the comments in CJoy_RemoveSubclass apply here !!
  166. */
  167. if(this->hwnd)
  168. {
  169. HWND hwnd = this->hwnd;
  170. this->hwnd = 0;
  171. if(!RemoveWindowSubclass(hwnd, CHid_SubclassProc, 0))
  172. {
  173. /*
  174. * The RemoveWindowSubclass can fail if the window
  175. * was destroyed behind our back.
  176. */
  177. // AssertF(!IsWindow(hwnd));
  178. }
  179. Sleep(0); /* Let the worker thread drain */
  180. Common_Unhold(this);
  181. }
  182. }
  183. /*****************************************************************************
  184. *
  185. * @doc INTERNAL
  186. *
  187. * @method HRESULT | CHid | Unacquire |
  188. *
  189. * Tell the device driver to stop data acquisition.
  190. *
  191. * It is the caller's responsibility to call this only
  192. * when the device has been acquired.
  193. *
  194. * Warning! We require that the device critical section be
  195. * held so we don't race against our worker thread.
  196. *
  197. * @returns
  198. *
  199. * Returns a COM error code. The following error codes are
  200. * intended to be illustrative and not necessarily comprehensive.
  201. *
  202. * <c DI_OK> = <c S_OK>: The operation completed successfully.
  203. *
  204. * <c S_FALSE>: The operation was begun and should be completed
  205. * by the caller by communicating with the <t VXDINSTANCE>.
  206. *
  207. *****************************************************************************/
  208. STDMETHODIMP
  209. CHid_Unacquire(PDICB pdcb)
  210. {
  211. HRESULT hres;
  212. PCHID this;
  213. EnterProcI(IDirectInputDeviceCallback::HID::Unacquire,
  214. (_ "p", pdcb));
  215. /*
  216. * This is an internal interface, so we can skimp on validation.
  217. */
  218. this = _thisPvNm(pdcb, dcb);
  219. AssertF(this->pvi);
  220. AssertF(this->pvi->pdd);
  221. AssertF(CDIDev_InCrit(this->pvi->pdd));
  222. hres = S_FALSE; /* Please finish for me */
  223. ExitOleProcR();
  224. return hres;
  225. }
  226. /*****************************************************************************
  227. *
  228. * @doc INTERNAL
  229. *
  230. * @func void | CHid_Finalize |
  231. *
  232. * Releases the resources of the device after all references
  233. * (both strong and weak) are gone.
  234. *
  235. * @parm PV | pvObj |
  236. *
  237. * Object being released. Note that it may not have been
  238. * completely initialized, so everything should be done
  239. * carefully.
  240. *
  241. *****************************************************************************/
  242. void INTERNAL
  243. CHid_Finalize(PV pvObj)
  244. {
  245. UINT iType;
  246. PCHID this = pvObj;
  247. if(this->hkInstType)
  248. {
  249. RegCloseKey(this->hkInstType);
  250. }
  251. if(this->hkType)
  252. {
  253. RegCloseKey(this->hkType);
  254. }
  255. if( this->hkProp)
  256. {
  257. RegCloseKey(this->hkProp);
  258. }
  259. AssertF(this->hdev == INVALID_HANDLE_VALUE);
  260. AssertF(this->hdevEm == INVALID_HANDLE_VALUE);
  261. if(this->ppd)
  262. {
  263. HidD_FreePreparsedData(this->ppd);
  264. }
  265. /*
  266. *
  267. * Free group 2 memory:
  268. *
  269. * hriIn.pvReport Input data
  270. * hriIn.rgdata
  271. *
  272. * hriOut.pvReport Output data
  273. * hriOut.rgdata
  274. *
  275. * hriFea.pvReport Feature data (both in and out)
  276. * hriFea.rgdata
  277. *
  278. * pvPhys Used by ED
  279. * pvStage
  280. */
  281. FreePpv(&this->pvGroup2);
  282. /*
  283. * Freeing df.rgodf also frees rgpvCaps, rgvcaps, rgbcaps, rgcoll.
  284. */
  285. FreePpv(&this->df.rgodf);
  286. FreePpv(&this->rgbaxissemflags);
  287. FreePpv(&this->rgiobj);
  288. FreePpv(&this->ptszPath);
  289. FreePpv(&this->ptszId);
  290. FreePpv(&this->rgpbButtonMasks);
  291. for(iType = 0x0; iType < HidP_Max; iType++)
  292. {
  293. FreePpv(&this->pEnableReportId[iType]);
  294. }
  295. }
  296. /*****************************************************************************
  297. *
  298. * @doc INTERNAL
  299. *
  300. * @method HRESULT | CHid | AppFinalize |
  301. *
  302. * The client <t VXDINSTANCE> contains a weak pointer back
  303. * to us so that that it can party on the data format we
  304. * collected.
  305. *
  306. * @parm PV | pvObj |
  307. *
  308. * Object being released from the application's perspective.
  309. *
  310. *****************************************************************************/
  311. void INTERNAL
  312. CHid_AppFinalize(PV pvObj)
  313. {
  314. PCHID this = pvObj;
  315. if(this->pvi)
  316. {
  317. HRESULT hres;
  318. CHid_RemoveSubclass(this);
  319. /*
  320. * Prefix warns that "this" may have been freed (mb:34570) however
  321. * in AppFinalize we should always have our internal reference to
  322. * keep it around. As long as refcounting is not broken, we are OK
  323. * and any refcount bug has to be fixed so don't hide it here.
  324. */
  325. hres = Hel_DestroyInstance(this->pvi);
  326. AssertF(SUCCEEDED(hres));
  327. }
  328. }
  329. /*****************************************************************************
  330. *
  331. * @doc INTERNAL
  332. *
  333. * @func LRESULT | CHid_SubclassProc |
  334. *
  335. * Window subclass procedure which watches for
  336. * joystick configuration change notifications.
  337. *
  338. * Even if we are not a joystick, we still listen to
  339. * this, in case somebody recalibrated a remote control
  340. * or some other wacky thing like that.
  341. *
  342. * However, if our device has no calibratable controls,
  343. * then there's no point in watching for recalibration
  344. * notifications.
  345. *
  346. * @parm HWND | hwnd |
  347. *
  348. * The victim window.
  349. *
  350. * @parm UINT | wm |
  351. *
  352. * Window message.
  353. *
  354. * @parm WPARAM | wp |
  355. *
  356. * Message-specific data.
  357. *
  358. * @parm LPARAM | lp |
  359. *
  360. * Message-specific data.
  361. *
  362. * @parm UINT | uid |
  363. *
  364. * Callback identification number, always zero.
  365. *
  366. * @parm DWORD | dwRef |
  367. *
  368. * Reference data, a pointer to our joystick device callback.
  369. *
  370. *****************************************************************************/
  371. LRESULT CALLBACK
  372. CHid_SubclassProc(HWND hwnd, UINT wm, WPARAM wp, LPARAM lp,
  373. UINT_PTR uid, ULONG_PTR dwRef)
  374. {
  375. #ifdef XDEBUG
  376. static CHAR s_szProc[] = "";
  377. #endif
  378. PCHID this = (PCHID)dwRef;
  379. AssertF(uid == 0);
  380. /*
  381. * Wacky subtlety going on here to avoid race conditions.
  382. * See the mondo comment block in CJoy_RemoveSubclass [sic]
  383. * for details.
  384. *
  385. * We can get faked out if the memory associated with the
  386. * CHid is still physically allocated, the vtbl is magically
  387. * still there and the hwnd field somehow matches our hwnd.
  388. */
  389. if(SUCCEEDED(hresPv(this)) && this->hwnd == hwnd)
  390. {
  391. switch(wm)
  392. {
  393. case WM_POWERBROADCAST :
  394. // 7/18/2000(a-JiTay): IA64: Use %p format specifier for 32/64-bit pointers.
  395. SquirtSqflPtszV( sqfl | sqflError,
  396. TEXT("WM_POWERBROADCAST(0x%x) for 0x%p"), wp, this);
  397. if(wp == PBT_APMSUSPEND )
  398. {
  399. CEm_ForceDeviceUnacquire(pemFromPvi(this->pvi)->ped, 0x0 );
  400. }
  401. else if(wp == PBT_APMRESUMESUSPEND )
  402. {
  403. CEm_ForceDeviceUnacquire(pemFromPvi(this->pvi)->ped, 0x0 );
  404. DIBus_BuildList(TRUE);
  405. }
  406. break;
  407. default:
  408. if( wm == g_wmJoyChanged )
  409. {
  410. /*
  411. * Once we receive this notification message, we need to rebuild
  412. * our list, because sometimes the user has just changed the device's ID.
  413. * See manbug: 35445
  414. */
  415. DIHid_BuildHidList(TRUE);
  416. Common_Hold(this);
  417. CHid_LoadCalibrations(this);
  418. Common_Unhold(this);
  419. }
  420. // 7/18/2000(a-JiTay): IA64: Use %p format specifier for 32/64-bit pointers.
  421. SquirtSqflPtszV( sqfl | sqflVerbose,
  422. TEXT("wp(0x%x) wm(0x%x) for 0x%p"), wm, wp, this);
  423. break;
  424. }
  425. }
  426. return DefSubclassProc(hwnd, wm, wp, lp);
  427. }
  428. /*****************************************************************************
  429. *
  430. * @doc INTERNAL
  431. *
  432. * @method void | CHid | GetPhysicalState |
  433. *
  434. * Read the physical device state into <p pmstOut>.
  435. *
  436. * Note that it doesn't matter if this is not atomic.
  437. * If a device report arrives while we are reading it,
  438. * we will get a mix of old and new data. No big deal.
  439. *
  440. * @parm PCHID | this |
  441. *
  442. * The object in question.
  443. *
  444. * @parm PV | pvOut |
  445. *
  446. * Where to put the device state.
  447. *
  448. * @returns
  449. * None.
  450. *
  451. *****************************************************************************/
  452. void INLINE
  453. CHid_GetPhysicalState(PCHID this, PV pvOut)
  454. {
  455. AssertF(this->pvPhys);
  456. AssertF(this->cbPhys);
  457. CopyMemory(pvOut, this->pvPhys, this->cbPhys);
  458. }
  459. /*****************************************************************************
  460. *
  461. * @doc INTERNAL
  462. *
  463. * @method HRESULT | CHid | Acquire |
  464. *
  465. * Tell the device driver to begin data acquisition.
  466. * We create a handle to the device so we can talk to it again.
  467. * We must create each time so we can survive in the
  468. * "unplug/replug" case. When a device is unplugged,
  469. * its <t HANDLE> becomes permanently invalid and must be
  470. * re-opened for it to work again.
  471. *
  472. * Warning! We require that the device critical section be
  473. * held so we don't race against our worker thread.
  474. *
  475. * @returns
  476. *
  477. * Returns a COM error code. The following error codes are
  478. * intended to be illustrative and not necessarily comprehensive.
  479. *
  480. * <c DI_OK> = <c S_OK>: The operation completed successfully.
  481. *
  482. * <c S_FALSE>: The operation was begun and should be completed
  483. * by the caller by communicating with the <t VXDINSTANCE>.
  484. *
  485. *****************************************************************************/
  486. STDMETHODIMP
  487. CHid_Acquire(PDICB pdcb)
  488. {
  489. HRESULT hres;
  490. HANDLE h;
  491. PCHID this;
  492. EnterProcI(IDirectInputDeviceCallback::HID::Acquire,
  493. (_ "p", pdcb));
  494. /*
  495. * This is an internal interface, so we can skimp on validation.
  496. */
  497. this = _thisPvNm(pdcb, dcb);
  498. AssertF(this->pvi);
  499. AssertF(this->pvi->pdd);
  500. AssertF(CDIDev_InCrit(this->pvi->pdd));
  501. AssertF(this->hdev == INVALID_HANDLE_VALUE);
  502. /*
  503. * We must check connectivity by opening the device, because NT
  504. * leaves the device in the info list even though it has
  505. * been unplugged.
  506. */
  507. h = CHid_OpenDevicePath(this, FILE_FLAG_OVERLAPPED);
  508. if(h != INVALID_HANDLE_VALUE)
  509. {
  510. NTSTATUS stat;
  511. DWORD dwFlags2 = 0;
  512. /*
  513. * Obtain Flags2 to find out if input report is disabled for this device,
  514. * if we haven't done so.
  515. */
  516. if (!this->fFlags2Checked)
  517. {
  518. JoyReg_GetValue( this->hkProp, REGSTR_VAL_FLAGS2, REG_BINARY,
  519. &dwFlags2, cbX(dwFlags2) );
  520. this->fFlags2Checked = TRUE;
  521. this->fEnableInputReport = ( (dwFlags2 & JOYTYPE_ENABLEINPUTREPORT) != 0 );
  522. }
  523. if ( this->fEnableInputReport )
  524. {
  525. BYTE id;
  526. for (id = 0; id < this->wMaxReportId[HidP_Input]; ++id)
  527. if ( this->pEnableReportId[HidP_Input][id] )
  528. {
  529. BOOL bRet;
  530. *(BYTE*)this->hriIn.pvReport = id;
  531. bRet = HidD_GetInputReport(h, this->hriIn.pvReport, this->hriIn.cbReport);
  532. if (bRet)
  533. {
  534. stat = CHid_ParseData(this, HidP_Input, &this->hriIn);
  535. if (SUCCEEDED(stat))
  536. {
  537. this->pvi->fl |= VIFL_INITIALIZE; /* Set the flag so the event can be buffered.
  538. since VIFL_ACQUIRED isn't set yet. */
  539. CEm_AddState(&this->ed, this->pvStage, GetTickCount());
  540. this->pvi->fl &= ~VIFL_INITIALIZE; /* Clear the flag when done. */
  541. }
  542. } else
  543. {
  544. DWORD dwError = GetLastError();
  545. // ERROR_SEM_TIMEOUT means the device has timed out.
  546. if (dwError == ERROR_SEM_TIMEOUT)
  547. {
  548. /*
  549. * Timed out. The device does not support input report. We need to record
  550. * the fact in registry so that GetInputReport() does not ever get called
  551. * again for this device, since each failed call takes five seconds to
  552. * complete.
  553. */
  554. this->fEnableInputReport = TRUE;
  555. dwFlags2 &= ~JOYTYPE_ENABLEINPUTREPORT;
  556. hres = JoyReg_SetValue( this->hkProp, REGSTR_VAL_FLAGS2,
  557. REG_BINARY, (PV)&dwFlags2, cbX( dwFlags2 ) );
  558. break;
  559. }
  560. RPF("CHid_InitParse: Unable to read HID input report LastError(0x%X)", dwError );
  561. }
  562. }
  563. }
  564. CloseHandle(h);
  565. /* Please finish for me */
  566. hres = S_FALSE;
  567. } else
  568. {
  569. hres = DIERR_UNPLUGGED;
  570. }
  571. ExitOleProcR();
  572. return hres;
  573. }
  574. /*****************************************************************************
  575. *
  576. * @doc INTERNAL
  577. *
  578. * @method HRESULT | CHid | GetInstance |
  579. *
  580. * Obtains the DirectInput instance handle.
  581. *
  582. * @parm OUT PPV | ppvi |
  583. *
  584. * Receives the instance handle.
  585. *
  586. *****************************************************************************/
  587. STDMETHODIMP
  588. CHid_GetInstance(PDICB pdcb, PPV ppvi)
  589. {
  590. HRESULT hres;
  591. PCHID this;
  592. EnterProcI(IDirectInputDeviceCallback::Hid::GetInstance, (_ "p", pdcb));
  593. /*
  594. * This is an internal interface, so we can skimp on validation.
  595. */
  596. this = _thisPvNm(pdcb, dcb);
  597. AssertF(this->pvi);
  598. *ppvi = (PV)this->pvi;
  599. hres = S_OK;
  600. ExitOleProcPpvR(ppvi);
  601. return hres;
  602. }
  603. /*****************************************************************************
  604. *
  605. * @doc INTERNAL
  606. *
  607. * @method HRESULT | CHid | GetDataFormat |
  608. *
  609. * Obtains the device's preferred data format.
  610. *
  611. * @parm OUT LPDIDEVICEFORMAT * | ppdf |
  612. *
  613. * <t LPDIDEVICEFORMAT> to receive pointer to device format.
  614. *
  615. * @returns
  616. *
  617. * Returns a COM error code. The following error codes are
  618. * intended to be illustrative and not necessarily comprehensive.
  619. *
  620. * <c DI_OK> = <c S_OK>: The operation completed successfully.
  621. *
  622. * <c DIERR_INVALIDPARAM> = <c E_INVALIDARG>: The
  623. * <p lpmdr> parameter is not a valid pointer.
  624. *
  625. *****************************************************************************/
  626. STDMETHODIMP
  627. CHid_GetDataFormat(PDICB pdcb, LPDIDATAFORMAT *ppdf)
  628. {
  629. HRESULT hres;
  630. PCHID this;
  631. EnterProcI(IDirectInputDeviceCallback::Hid::GetDataFormat,
  632. (_ "p", pdcb));
  633. /*
  634. * This is an internal interface, so we can skimp on validation.
  635. */
  636. this = _thisPvNm(pdcb, dcb);
  637. *ppdf = &this->df;
  638. hres = S_OK;
  639. ExitOleProcPpvR(ppdf);
  640. return hres;
  641. }
  642. /*****************************************************************************
  643. *
  644. * @doc INTERNAL
  645. *
  646. * @func HRESULT | DIHid_GetParentRegistryProperty |
  647. *
  648. * @parm LPTSTR | ptszId |
  649. *
  650. * Device Instance ID.
  651. *
  652. * @parm DWORD | dwProperty |
  653. *
  654. * The property being queried.
  655. *
  656. * @parm LPDIPROPHEADER | diph |
  657. *
  658. * Property data to be set.
  659. *
  660. * @parm BOOL | diph |
  661. *
  662. * Get from parent or grand parent.
  663. *
  664. *****************************************************************************/
  665. HRESULT INTERNAL
  666. DIHid_GetParentRegistryProperty(LPTSTR ptszId, DWORD dwProperty, LPDIPROPHEADER pdiph, BOOL bGrandParent)
  667. {
  668. HDEVINFO hdev;
  669. LPDIPROPSTRING pstr = (PV)pdiph;
  670. TCHAR tsz[MAX_PATH];
  671. HRESULT hres = E_FAIL;
  672. ZeroX(tsz);
  673. hdev = SetupDiCreateDeviceInfoList(NULL, NULL);
  674. if(hdev != INVALID_HANDLE_VALUE)
  675. {
  676. SP_DEVINFO_DATA dinf;
  677. /*
  678. * For the instance name, use the friendly name if possible.
  679. * Else, use the device description.
  680. */
  681. dinf.cbSize = cbX(SP_DEVINFO_DATA);
  682. if(SetupDiOpenDeviceInfo(hdev, ptszId, NULL, 0, &dinf))
  683. {
  684. DEVINST DevInst;
  685. CONFIGRET cr;
  686. if( (cr = CM_Get_Parent(&DevInst, dinf.DevInst, 0x0)) == CR_SUCCESS )
  687. {
  688. ULONG ulLength;
  689. CAssertF( SPDRP_DEVICEDESC +1 == CM_DRP_DEVICEDESC );
  690. CAssertF( SPDRP_FRIENDLYNAME +1 == CM_DRP_FRIENDLYNAME );
  691. if(bGrandParent)
  692. {
  693. cr = CM_Get_Parent(&DevInst, DevInst, 0x0);
  694. if( cr != CR_SUCCESS )
  695. {
  696. // No GrandParent ??
  697. }
  698. }
  699. ulLength = MAX_PATH * cbX(TCHAR);
  700. if( cr == CR_SUCCESS &&
  701. ( cr = CM_Get_DevNode_Registry_Property(
  702. DevInst,
  703. dwProperty+1,
  704. NULL,
  705. tsz,
  706. &ulLength,
  707. 0x0 ) ) == CR_SUCCESS )
  708. {
  709. // Success
  710. hres = S_OK;
  711. #ifdef UNICODE
  712. lstrcpyW(pstr->wsz, tsz);
  713. #else
  714. TToU(pstr->wsz, MAX_PATH, tsz);
  715. #endif
  716. } else
  717. {
  718. SquirtSqflPtszV(sqfl | sqflVerbose,
  719. TEXT("CM_Get_DevNode_Registry_Property FAILED") );
  720. }
  721. } else
  722. {
  723. SquirtSqflPtszV(sqfl | sqflVerbose,
  724. TEXT("CM_Get_Parent FAILED") );
  725. }
  726. }
  727. SetupDiDestroyDeviceInfoList(hdev);
  728. } else
  729. {
  730. SquirtSqflPtszV(sqfl | sqflError,
  731. TEXT("SetupDiCreateDeviceInfoList FAILED, le = %d"), GetLastError() );
  732. }
  733. return hres;
  734. }
  735. /*****************************************************************************
  736. *
  737. * @doc INTERNAL
  738. *
  739. * @func HRESULT | DIHid_GetRegistryProperty |
  740. *
  741. * @parm LPTSTR | ptszId |
  742. *
  743. * Device Instance ID.
  744. *
  745. * @parm DWORD | dwProperty |
  746. *
  747. * The property being queried.
  748. *
  749. * @parm LPDIPROPHEADER | diph |
  750. *
  751. * Property data to be set.
  752. *
  753. *****************************************************************************/
  754. HRESULT EXTERNAL
  755. DIHid_GetRegistryProperty(LPTSTR ptszId, DWORD dwProperty, LPDIPROPHEADER pdiph)
  756. {
  757. HDEVINFO hdev;
  758. LPDIPROPSTRING pstr = (PV)pdiph;
  759. TCHAR tsz[MAX_PATH];
  760. HRESULT hres = E_FAIL;
  761. ZeroX(tsz);
  762. hdev = SetupDiCreateDeviceInfoList(NULL, NULL);
  763. if(hdev != INVALID_HANDLE_VALUE)
  764. {
  765. SP_DEVINFO_DATA dinf;
  766. /*
  767. * For the instance name, use the friendly name if possible.
  768. * Else, use the device description.
  769. */
  770. dinf.cbSize = cbX(SP_DEVINFO_DATA);
  771. if(SetupDiOpenDeviceInfo(hdev, ptszId, NULL, 0, &dinf))
  772. {
  773. if(SetupDiGetDeviceRegistryProperty(hdev, &dinf, dwProperty, NULL,
  774. (LPBYTE)tsz, MAX_PATH, NULL) )
  775. {
  776. hres = S_OK;
  777. #ifdef UNICODE
  778. lstrcpyW(pstr->wsz, tsz);
  779. #else
  780. TToU(pstr->wsz, MAX_PATH, tsz);
  781. #endif
  782. } else
  783. {
  784. SquirtSqflPtszV(sqfl | sqflVerbose,
  785. TEXT("SetupDiOpenDeviceInfo FAILED, le = %d"), GetLastError() );
  786. }
  787. } else
  788. {
  789. SquirtSqflPtszV(sqfl | sqflError,
  790. TEXT("SetupDiOpenDeviceInfo FAILED, le = %d"), GetLastError() );
  791. }
  792. SetupDiDestroyDeviceInfoList(hdev);
  793. } else
  794. {
  795. SquirtSqflPtszV(sqfl | sqflError,
  796. TEXT("SetupDiCreateDeviceInfoList FAILED, le = %d"), GetLastError() );
  797. }
  798. return hres;
  799. }
  800. /*****************************************************************************
  801. *
  802. * @doc INTERNAL
  803. *
  804. * @method HRESULT | CHid | GetGuidAndPath |
  805. *
  806. * Get a Hid device's class GUID (namely, the HID guid)
  807. * and device interface (path).
  808. *
  809. * @parm PCHID | this |
  810. *
  811. * The Hid object.
  812. *
  813. * @parm LPDIPROPHEADER | pdiph |
  814. *
  815. * Structure to receive property value.
  816. *
  817. *****************************************************************************/
  818. HRESULT INTERNAL
  819. CHid_GetGuidAndPath(PCHID this, LPDIPROPHEADER pdiph)
  820. {
  821. HRESULT hres;
  822. LPDIPROPGUIDANDPATH pgp = (PV)pdiph;
  823. pgp->guidClass = GUID_HIDClass;
  824. TToU(pgp->wszPath, cA(pgp->wszPath), this->ptszPath);
  825. hres = S_OK;
  826. return hres;
  827. }
  828. /*****************************************************************************
  829. *
  830. * @doc INTERNAL
  831. *
  832. * @method BOOLEAN | CHid | CHidInsertScanCodes |
  833. *
  834. *
  835. * @parm
  836. *
  837. * ISSUE-2001/03/29-timgill function needs documenting
  838. *
  839. *
  840. *****************************************************************************/
  841. BOOLEAN CHidInsertScanCodes
  842. (
  843. IN PVOID Context, // Some caller supplied context.
  844. IN PCHAR NewScanCodes, // A list of i8042 scan codes.
  845. IN ULONG Length // the length of the scan codes.
  846. )
  847. {
  848. int Idx;
  849. /*
  850. * This is not inner loop code so don't rush it
  851. */
  852. AssertF( Length <= cbX( DWORD ) );
  853. for( Idx = 0; Idx < cbX( DWORD ); Idx++ )
  854. {
  855. if( Length-- )
  856. {
  857. *(((PCHAR)Context)++) = *(NewScanCodes++);
  858. }
  859. else
  860. {
  861. *(((PCHAR)Context)++) = '\0';
  862. }
  863. }
  864. return TRUE;
  865. }
  866. /*****************************************************************************
  867. *
  868. * @doc INTERNAL
  869. *
  870. * @func BOOL | fHasSpecificHardwareMatch |
  871. *
  872. * Find out from SetupAPI whether the device was matched with a
  873. * specific hardware ID match or generic match.
  874. * A specific match should have caused a device description to be
  875. * installed which is likely to be at least as good as what HID could
  876. * get from a product string in firmware. (a. because it's easier to
  877. * update an INF after release than firmware; b. because HID can only
  878. * get us an English string.) Generic matches on the other hand are,
  879. * by definition, all the same so cannot be used to tell two devices
  880. * apart.
  881. *
  882. * @parm LPTSTR | ptszId |
  883. *
  884. * Device Instance ID.
  885. *
  886. * @returns
  887. * <c TRUE> if the device was installed using a specific match.
  888. * <c FALSE> if it was not or if installation info was unobtainable.
  889. *
  890. * @comm
  891. * This is used on Win2k for game controllers and Win9x for mice and
  892. * keyboards. Win2k we can't read HID mice and keyboards and on
  893. * Win9x VJoyD should always create device names before DInput.dll.
  894. * On Win9x this is less of a big deal for game controllers because
  895. * IHVs are accoustomed to adding their display name to
  896. * MediaProperties.
  897. *
  898. *****************************************************************************/
  899. BOOL fHasSpecificHardwareMatch( LPTSTR ptszId )
  900. {
  901. HDEVINFO hInfo;
  902. BOOL fRc = FALSE;
  903. EnterProcI(fHasSpecificHardwareMatch,(_ "s", ptszId));
  904. hInfo = SetupDiCreateDeviceInfoList(NULL, NULL);
  905. if( hInfo != INVALID_HANDLE_VALUE )
  906. {
  907. SP_DEVINFO_DATA dinf;
  908. dinf.cbSize = cbX(SP_DEVINFO_DATA);
  909. if( SetupDiOpenDeviceInfo(hInfo, ptszId, NULL, 0, &dinf) )
  910. {
  911. CONFIGRET cr;
  912. DEVINST DevInst;
  913. cr = CM_Get_Parent( &DevInst, dinf.DevInst, 0x0 );
  914. if( cr == CR_SUCCESS )
  915. {
  916. TCHAR tszDevInst[MAX_PATH];
  917. cr = CM_Get_Device_ID( DevInst, (DEVINSTID)tszDevInst, MAX_PATH, 0 );
  918. if( cr == CR_SUCCESS )
  919. {
  920. if( SetupDiOpenDeviceInfo(hInfo, tszDevInst, NULL, 0, &dinf) )
  921. {
  922. HKEY hkDrv;
  923. hkDrv = SetupDiOpenDevRegKey( hInfo, &dinf, DICS_FLAG_GLOBAL, 0,
  924. DIREG_DRV, MAXIMUM_ALLOWED );
  925. if( hkDrv != INVALID_HANDLE_VALUE )
  926. {
  927. PTCHAR tszHardwareID = NULL;
  928. PTCHAR tszMatchingID = NULL;
  929. ULONG ulLength = 0;
  930. cr = CM_Get_DevNode_Registry_Property(DevInst,
  931. CM_DRP_HARDWAREID,
  932. NULL,
  933. NULL,
  934. &ulLength,
  935. 0x0 );
  936. /*
  937. * Win2k returns CR_BUFFER_SMALL but
  938. * Win9x returns CR_SUCCESS so allow both.
  939. */
  940. if( ( ( cr == CR_BUFFER_SMALL ) || ( cr == CR_SUCCESS ) )
  941. && ulLength )
  942. {
  943. #ifndef WINNT
  944. /*
  945. * Need to allocate extra for terminator on Win9x
  946. */
  947. ulLength++;
  948. #endif
  949. if( SUCCEEDED( AllocCbPpv( ulLength + ( MAX_PATH * cbX(tszMatchingID[0]) ), &tszMatchingID ) ) )
  950. {
  951. cr = CM_Get_DevNode_Registry_Property(DevInst,
  952. CM_DRP_HARDWAREID,
  953. NULL,
  954. (PBYTE)&tszMatchingID[MAX_PATH],
  955. &ulLength,
  956. 0x0 );
  957. if( cr == CR_SUCCESS )
  958. {
  959. tszHardwareID = &tszMatchingID[MAX_PATH];
  960. }
  961. else
  962. {
  963. SquirtSqflPtszV(sqfl | sqflError,
  964. TEXT("CR error %d getting HW ID"), cr );
  965. }
  966. }
  967. else
  968. {
  969. SquirtSqflPtszV(sqfl | sqflError,
  970. TEXT("No memory requesting %d bytes for HW ID"), ulLength );
  971. }
  972. }
  973. else
  974. {
  975. SquirtSqflPtszV(sqfl | sqflError,
  976. TEXT("Unexpected CR error %d getting HW ID size"), cr );
  977. }
  978. if( tszHardwareID )
  979. {
  980. ulLength = MAX_PATH * cbX(tszMatchingID[0]);
  981. cr = RegQueryValueEx( hkDrv, REGSTR_VAL_MATCHINGDEVID, 0, 0, (PBYTE)tszMatchingID, &ulLength );
  982. if( CR_SUCCESS == cr )
  983. {
  984. while( ulLength = lstrlen( tszHardwareID ) )
  985. {
  986. if( !lstrcmpi( tszHardwareID, tszMatchingID ) )
  987. {
  988. fRc = TRUE;
  989. break;
  990. }
  991. tszHardwareID += ulLength + 1;
  992. }
  993. }
  994. else
  995. {
  996. SquirtSqflPtszV(sqfl | sqflError,
  997. TEXT("No matching ID!, cr = %d"), cr );
  998. }
  999. }
  1000. if( tszMatchingID )
  1001. {
  1002. FreePv( tszMatchingID );
  1003. }
  1004. RegCloseKey( hkDrv );
  1005. }
  1006. else
  1007. {
  1008. SquirtSqflPtszV(sqfl | sqflError,
  1009. TEXT("SetupDiOpenDevRegKey failed, le = %d"), GetLastError() );
  1010. }
  1011. }
  1012. else
  1013. {
  1014. SquirtSqflPtszV(sqfl | sqflError,
  1015. TEXT("SetupDiOpenDeviceInfo failed for %S (parent), le = %d"),
  1016. tszDevInst, GetLastError() );
  1017. }
  1018. }
  1019. else
  1020. {
  1021. SquirtSqflPtszV(sqfl | sqflError,
  1022. TEXT("CM_Get_Device_ID FAILED %d"), cr );
  1023. }
  1024. }
  1025. else
  1026. {
  1027. SquirtSqflPtszV(sqfl | sqflError,
  1028. TEXT("CM_Get_Parent FAILED %d"), cr );
  1029. }
  1030. }
  1031. else
  1032. {
  1033. SquirtSqflPtszV(sqfl | sqflError,
  1034. TEXT("SetupDiOpenDeviceInfo failed for %S (child), le = %d"),
  1035. ptszId, GetLastError() );
  1036. }
  1037. SetupDiDestroyDeviceInfoList(hInfo);
  1038. }
  1039. else
  1040. {
  1041. SquirtSqflPtszV(sqfl | sqflError,
  1042. TEXT("SetupDiCreateDeviceInfoList failed, le = %d"), GetLastError() );
  1043. }
  1044. ExitProc();
  1045. return fRc;
  1046. }
  1047. /*****************************************************************************
  1048. *
  1049. * @doc INTERNAL
  1050. *
  1051. * @func BOOL | fGetProductStringFromDevice |
  1052. *
  1053. * Try getting the product name from HID.
  1054. * If the device has one of these, this is what is displayed
  1055. * when the device is initially recognized. Unfortunately
  1056. * this name does not land up in the friendly name registry
  1057. * entry so in case this gets fixed we go directly to HID.
  1058. *
  1059. * @parm PCHID | this |
  1060. *
  1061. * The Hid object.
  1062. *
  1063. * @parm PWCHAR | wszBuffer |
  1064. *
  1065. * Where to put the product string if found.
  1066. *
  1067. * @parm ULONG | ulBufferLen |
  1068. *
  1069. * How big the string buffer is in bytes
  1070. *
  1071. * @returns
  1072. * <c TRUE> if a string has been placed in the buffer
  1073. * <c FALSE> if no string was retrieved
  1074. *
  1075. *****************************************************************************/
  1076. BOOL fGetProductStringFromDevice
  1077. (
  1078. PCHID this,
  1079. PWCHAR wszBuffer,
  1080. ULONG ulBufferLen
  1081. )
  1082. {
  1083. BOOL fRc;
  1084. /*
  1085. * If we already have a handle open (device is acquired), use
  1086. * it, otherwise open one just for now.
  1087. */
  1088. if( this->hdev != INVALID_HANDLE_VALUE )
  1089. {
  1090. fRc = HidD_GetProductString( this->hdev, wszBuffer, ulBufferLen );
  1091. }
  1092. else
  1093. {
  1094. HANDLE hdev;
  1095. hdev = CHid_OpenDevicePath(this, FILE_FLAG_OVERLAPPED);
  1096. if(hdev != INVALID_HANDLE_VALUE)
  1097. {
  1098. wszBuffer[0] = 0;
  1099. fRc = HidD_GetProductString( hdev, wszBuffer, ulBufferLen );
  1100. fRc = (fRc)?(wszBuffer[0] != 0):FALSE;
  1101. CloseHandle(hdev);
  1102. }
  1103. else
  1104. {
  1105. fRc = FALSE;
  1106. }
  1107. }
  1108. return fRc;
  1109. }
  1110. /*****************************************************************************
  1111. *
  1112. * @doc INTERNAL
  1113. *
  1114. * @method HRESULT | CHid | GetProperty |
  1115. *
  1116. * Get a Hid device property.
  1117. *
  1118. * @parm PCHID | this |
  1119. *
  1120. * The Hid object.
  1121. *
  1122. * @parm IN LPCDIPROPINFO | ppropi |
  1123. *
  1124. * Information describing the property being retrieved.
  1125. *
  1126. * @parm LPDIPROPHEADER | pdiph |
  1127. *
  1128. * Structure to receive property value.
  1129. *
  1130. * @returns
  1131. *
  1132. * <c E_NOTIMPL> nothing happened. The caller will do
  1133. * the default thing in response to <c E_NOTIMPL>.
  1134. *
  1135. *****************************************************************************/
  1136. #ifdef WINNT
  1137. TCHAR g_wszDefaultHIDName[80];
  1138. UINT g_uLenDefaultHIDSize;
  1139. #endif
  1140. STDMETHODIMP
  1141. CHid_GetProperty(PDICB pdcb, LPCDIPROPINFO ppropi, LPDIPROPHEADER pdiph)
  1142. {
  1143. HRESULT hres;
  1144. PCHID this;
  1145. EnterProcI(IDirectInputDeviceCallback::Hid::GetProperty,
  1146. (_ "pxxp", pdcb, ppropi->pguid, ppropi->iobj, pdiph));
  1147. /*
  1148. * This is an internal interface, so we can skimp on validation.
  1149. */
  1150. this = _thisPvNm(pdcb, dcb);
  1151. if(ppropi->iobj < this->df.dwNumObjs)
  1152. { /* Object property */
  1153. AssertF(ppropi->dwDevType == this->df.rgodf[ppropi->iobj].dwType);
  1154. switch((DWORD)(UINT_PTR)(ppropi->pguid))
  1155. {
  1156. case (DWORD)(UINT_PTR)(DIPROP_ENABLEREPORTID):
  1157. {
  1158. LPDIPROPDWORD ppropdw = CONTAINING_RECORD(pdiph, DIPROPDWORD, diph);
  1159. PHIDGROUPCAPS pcaps = this->rghoc[ppropi->iobj].pcaps;
  1160. AssertF(fLimpFF(pcaps,
  1161. pcaps->dwSignature == HIDGROUPCAPS_SIGNATURE));
  1162. ppropdw->dwData = 0x0;
  1163. AssertF(pcaps->wReportId < this->wMaxReportId[pcaps->type]);
  1164. AssertF(this->pEnableReportId[pcaps->type]);
  1165. (UCHAR)ppropdw->dwData = *(this->pEnableReportId[pcaps->type] + pcaps->wReportId);
  1166. hres = S_OK;
  1167. }
  1168. break;
  1169. case (DWORD)(UINT_PTR)(DIPROP_PHYSICALRANGE):
  1170. {
  1171. LPDIPROPRANGE pdiprg = CONTAINING_RECORD(pdiph, DIPROPRANGE, diph);
  1172. PHIDGROUPCAPS pcaps = this->rghoc[ppropi->iobj].pcaps;
  1173. pdiprg->lMin = pcaps->Physical.Min;
  1174. pdiprg->lMax = pcaps->Physical.Max;
  1175. hres = S_OK;
  1176. break;
  1177. }
  1178. break;
  1179. case (DWORD)(UINT_PTR)(DIPROP_LOGICALRANGE):
  1180. {
  1181. LPDIPROPRANGE pdiprg = CONTAINING_RECORD(pdiph, DIPROPRANGE, diph);
  1182. PHIDGROUPCAPS pcaps = this->rghoc[ppropi->iobj].pcaps;
  1183. pdiprg->lMin = pcaps->Logical.Min;
  1184. pdiprg->lMax = pcaps->Logical.Max;
  1185. hres = S_OK;
  1186. }
  1187. break;
  1188. case (DWORD)(UINT_PTR)(DIPROP_KEYNAME):
  1189. {
  1190. LPDIPROPSTRING pdipstr = (PV)pdiph;
  1191. UINT uiInstance = ppropi->iobj;
  1192. PHIDGROUPCAPS pcaps;
  1193. AssertF(uiInstance == CHid_ObjFromType(this, ppropi->dwDevType));
  1194. pcaps = this->rghoc[uiInstance].pcaps;
  1195. /*
  1196. * pcaps might be NULL if HID messed up and left gaps
  1197. * in the index lists.
  1198. */
  1199. if(pcaps)
  1200. {
  1201. UINT duiInstance;
  1202. AssertF(pcaps->dwSignature == HIDGROUPCAPS_SIGNATURE);
  1203. if(ppropi->dwDevType & DIDFT_COLLECTION)
  1204. {
  1205. duiInstance = 0;
  1206. } else
  1207. {
  1208. /*
  1209. * Now convert the uiInstance to a duiInstance,
  1210. * giving the index of this object into the group.
  1211. */
  1212. AssertF(HidP_IsValidReportType(pcaps->type));
  1213. duiInstance = uiInstance - (this->rgdwBase[pcaps->type] +
  1214. pcaps->DataIndexMin);
  1215. }
  1216. if(GetHIDString(pcaps->UsageMin + duiInstance,
  1217. pcaps->UsagePage,
  1218. pdipstr->wsz, cA(pdipstr->wsz)))
  1219. {
  1220. hres = S_OK;
  1221. }
  1222. else
  1223. {
  1224. hres = DIERR_OBJECTNOTFOUND;
  1225. }
  1226. } else
  1227. {
  1228. hres = DIERR_NOTFOUND;
  1229. }
  1230. }
  1231. break;
  1232. case (DWORD)(UINT_PTR)(DIPROP_SCANCODE):
  1233. {
  1234. LPDIPROPDWORD pdipdw = (PV)pdiph;
  1235. UINT uiInstance = ppropi->iobj;
  1236. PHIDGROUPCAPS pcaps;
  1237. AssertF(uiInstance == CHid_ObjFromType(this, ppropi->dwDevType));
  1238. pcaps = this->rghoc[uiInstance].pcaps;
  1239. /*
  1240. * pcaps might be NULL if HID messed up and left gaps
  1241. * in the index lists.
  1242. */
  1243. if(pcaps )
  1244. {
  1245. if ( pcaps->UsagePage == HID_USAGE_PAGE_KEYBOARD )
  1246. //ISSUE-2001/03/29-timgill unable to access keyboard consumer keys
  1247. //can't do this || pcaps->UsagePage == HID_USAGE_PAGE_CONSUMER )
  1248. {
  1249. UINT duiInstance;
  1250. HIDP_KEYBOARD_MODIFIER_STATE modifiers;
  1251. USAGE us;
  1252. AssertF(pcaps->dwSignature == HIDGROUPCAPS_SIGNATURE);
  1253. if(ppropi->dwDevType & DIDFT_COLLECTION)
  1254. {
  1255. duiInstance = 0;
  1256. } else
  1257. {
  1258. /*
  1259. * Now convert the uiInstance to a duiInstance,
  1260. * giving the index of this object into the group.
  1261. */
  1262. AssertF(HidP_IsValidReportType(pcaps->type));
  1263. duiInstance = uiInstance - (this->rgdwBase[pcaps->type] +
  1264. pcaps->DataIndexMin);
  1265. }
  1266. us = pcaps->UsageMin + duiInstance;
  1267. CAssertF( cbX( modifiers ) == cbX( modifiers.ul ) );
  1268. modifiers.ul = 0; // Use no modifiers for translation
  1269. if( SUCCEEDED(HidP_TranslateUsagesToI8042ScanCodes( &us, 1, HidP_Keyboard_Make, &modifiers,
  1270. CHidInsertScanCodes,
  1271. &pdipdw->dwData
  1272. ) ) )
  1273. {
  1274. hres = S_OK;
  1275. } else
  1276. {
  1277. hres = E_FAIL;
  1278. }
  1279. } else
  1280. {
  1281. hres = E_NOTIMPL;
  1282. }
  1283. } else
  1284. {
  1285. hres = DIERR_NOTFOUND;
  1286. }
  1287. }
  1288. break;
  1289. default:
  1290. if(ppropi->dwDevType & DIDFT_POV)
  1291. {
  1292. PHIDGROUPCAPS pcaps = this->rghoc[ppropi->iobj].pcaps;
  1293. AssertF(fLimpFF(pcaps,
  1294. pcaps->dwSignature == HIDGROUPCAPS_SIGNATURE));
  1295. #ifdef WINNT
  1296. if( pcaps && pcaps->IsPolledPOV && ppropi->pguid == DIPROP_CALIBRATIONMODE ) {
  1297. PJOYRANGECONVERT pjrc = this->rghoc[ppropi->iobj].pjrc;
  1298. if(pjrc)
  1299. {
  1300. hres = CCal_GetProperty(pjrc, ppropi->pguid, pdiph);
  1301. } else
  1302. {
  1303. hres = E_NOTIMPL;
  1304. }
  1305. } else
  1306. #endif
  1307. {
  1308. if(pcaps && ppropi->pguid == DIPROP_GRANULARITY)
  1309. {
  1310. LPDIPROPDWORD pdipdw = (PV)pdiph;
  1311. pdipdw->dwData = pcaps->usGranularity;
  1312. hres = S_OK;
  1313. } else
  1314. {
  1315. hres = E_NOTIMPL;
  1316. }
  1317. }
  1318. } else if(ppropi->dwDevType & DIDFT_RELAXIS)
  1319. {
  1320. /*
  1321. * All relative axes have a full range by default,
  1322. * so we don't need to do anything.
  1323. */
  1324. hres = E_NOTIMPL;
  1325. } else if(ppropi->dwDevType & DIDFT_ABSAXIS)
  1326. {
  1327. PJOYRANGECONVERT pjrc = this->rghoc[ppropi->iobj].pjrc;
  1328. /*
  1329. * Theoretically, every absolute axis will have
  1330. * calibration info. But test just in case something
  1331. * impossible happens.
  1332. */
  1333. if(pjrc)
  1334. {
  1335. hres = CCal_GetProperty(pjrc, ppropi->pguid, pdiph);
  1336. } else
  1337. {
  1338. hres = E_NOTIMPL;
  1339. }
  1340. } else
  1341. {
  1342. SquirtSqflPtszV(sqflHidDev | sqflError,
  1343. TEXT("CHid_GetProperty(iobj=%08x): E_NOTIMPL on guid: %08x"),
  1344. ppropi->iobj, ppropi->pguid);
  1345. hres = E_NOTIMPL;
  1346. }
  1347. }
  1348. } else if(ppropi->iobj == 0xFFFFFFFF)
  1349. { /* Device property */
  1350. switch((DWORD)(UINT_PTR)ppropi->pguid)
  1351. {
  1352. case (DWORD)(UINT_PTR)DIPROP_GUIDANDPATH:
  1353. hres = CHid_GetGuidAndPath(this, pdiph);
  1354. break;
  1355. case (DWORD)(UINT_PTR)DIPROP_INSTANCENAME:
  1356. {
  1357. /*
  1358. * Friendly names cause all manner of problems with devices that
  1359. * use auto detection so only allow non-predefined analog devices
  1360. * to use them.
  1361. */
  1362. if( ( this->VendorID == MSFT_SYSTEM_VID )
  1363. && ( this->ProductID >= MSFT_SYSTEM_PID + JOY_HW_PREDEFMAX )
  1364. && ( ( this->ProductID & 0xff00 ) == MSFT_SYSTEM_PID ) )
  1365. {
  1366. AssertF(this->hkType);
  1367. if( this->hkType )
  1368. {
  1369. LPDIPROPSTRING pstr = (PV)pdiph;
  1370. hres = JoyReg_GetValue(this->hkType,
  1371. REGSTR_VAL_JOYOEMNAME, REG_SZ,
  1372. pstr->wsz,
  1373. cbX(pstr->wsz));
  1374. if( SUCCEEDED(hres ) )
  1375. {
  1376. SquirtSqflPtszV(sqflHid | sqflVerbose,
  1377. TEXT( "Got instance name %s"), pstr->wsz );
  1378. if( ( this->diHacks.nMaxDeviceNameLength < MAX_PATH )
  1379. && ( this->diHacks.nMaxDeviceNameLength < lstrlenW(pstr->wsz) ) )
  1380. {
  1381. pstr->wsz[this->diHacks.nMaxDeviceNameLength] = L'\0';
  1382. }
  1383. hres = S_OK;
  1384. break;
  1385. }
  1386. }
  1387. }
  1388. /*
  1389. * Fall through to catch the product name
  1390. */
  1391. }
  1392. case (DWORD)(UINT_PTR)DIPROP_PRODUCTNAME:
  1393. {
  1394. LPDIPROPSTRING pdipstr = (PV)pdiph;
  1395. /*
  1396. * For now, don't deal with mice and keyboard names on NT
  1397. */
  1398. #ifdef WINNT
  1399. AssertF( ( GET_DIDEVICE_TYPE( this->dwDevType ) != DI8DEVTYPE_KEYBOARD )
  1400. && ( GET_DIDEVICE_TYPE( this->dwDevType ) != DI8DEVTYPE_MOUSE ) );
  1401. #endif
  1402. if( GET_DIDEVICE_TYPE( this->dwDevType ) < DI8DEVTYPE_GAMEMIN )
  1403. {
  1404. AssertF( GET_DIDEVICE_TYPE( this->dwDevType ) >= DI8DEVTYPE_DEVICE );
  1405. if( fHasSpecificHardwareMatch( this->ptszId )
  1406. && SUCCEEDED( hres = DIHid_GetParentRegistryProperty(this->ptszId, SPDRP_DEVICEDESC, pdiph, 0x0 ) ) )
  1407. {
  1408. SquirtSqflPtszV(sqflHid | sqflVerbose,
  1409. TEXT("Got sys dev description %S"), pdipstr->wsz );
  1410. }
  1411. else if( fGetProductStringFromDevice( this, pdipstr->wsz, cbX( pdipstr->wsz ) ) )
  1412. {
  1413. SquirtSqflPtszV(sqflHid | sqflVerbose,
  1414. TEXT( "Got sys dev name from device %S"), pdipstr->wsz );
  1415. hres = S_OK;
  1416. }
  1417. else
  1418. {
  1419. if( SUCCEEDED( hres = DIHid_GetRegistryProperty(this->ptszId, SPDRP_DEVICEDESC, pdiph ) ) )
  1420. {
  1421. SquirtSqflPtszV(sqflHid | sqflVerbose,
  1422. TEXT( "Got sys dev name from devnode registry %S"), pdipstr->wsz );
  1423. }
  1424. else
  1425. {
  1426. UINT uDefName;
  1427. switch( GET_DIDEVICE_TYPE( this->dwDevType ) )
  1428. {
  1429. case DI8DEVTYPE_MOUSE:
  1430. uDefName = IDS_STDMOUSE;
  1431. break;
  1432. case DI8DEVTYPE_KEYBOARD:
  1433. uDefName = IDS_STDKEYBOARD;
  1434. break;
  1435. default:
  1436. uDefName = IDS_DEVICE_NAME;
  1437. break;
  1438. }
  1439. if( LoadStringW(g_hinst, uDefName, pdipstr->wsz, cbX( pdipstr->wsz ) ) )
  1440. {
  1441. SquirtSqflPtszV(sqflHid | sqflVerbose,
  1442. TEXT( "Loaded default sys dev name %S"), pdipstr->wsz );
  1443. hres = S_OK;
  1444. }
  1445. else
  1446. {
  1447. /*
  1448. * Give up, this machine is toast if we can't
  1449. * even load a string from our own resources.
  1450. */
  1451. SquirtSqflPtszV(sqflHidDev | sqflError,
  1452. TEXT("CHid_GetProperty(guid:%08x) failed to get name"),
  1453. ppropi->pguid);
  1454. hres = E_FAIL;
  1455. }
  1456. }
  1457. }
  1458. }
  1459. else
  1460. {
  1461. /*
  1462. * For game controllers, first look in MediaProperties.
  1463. * This is the most likely place to find a localized string
  1464. * free from corruption by the setup process.
  1465. * This should only fail before the type key is created when
  1466. * it first used so other paths are rare.
  1467. */
  1468. DIJOYTYPEINFO dijti;
  1469. WCHAR wszType[cbszVIDPID];
  1470. /* Check the type key or get predefined name */
  1471. ZeroX(dijti);
  1472. dijti.dwSize = cbX(dijti);
  1473. if( ( this->VendorID == MSFT_SYSTEM_VID )
  1474. &&( ( this->ProductID >= MSFT_SYSTEM_PID + JOY_HW_PREDEFMIN )
  1475. &&( this->ProductID < MSFT_SYSTEM_PID + JOY_HW_PREDEFMAX ) ) )
  1476. {
  1477. wszType[0] = L'#';
  1478. wszType[1] = L'0' + (WCHAR)(this->ProductID-MSFT_SYSTEM_PID);
  1479. wszType[2] = L'\0';
  1480. hres = JoyReg_GetPredefTypeInfo( wszType, &dijti, DITC_DISPLAYNAME);
  1481. AssertF( SUCCEEDED( hres ) );
  1482. AssertF( dijti.wszDisplayName[0] != L'\0' );
  1483. lstrcpyW(pdipstr->wsz, dijti.wszDisplayName);
  1484. SquirtSqflPtszV(sqflHid | sqflVerbose,
  1485. TEXT( "Got name as predefined %s"), pdipstr->wsz );
  1486. }
  1487. else
  1488. {
  1489. #ifndef WINNT
  1490. static WCHAR wszDefHIDName[] = L"HID Game Controller";
  1491. #endif
  1492. BOOL fOverwriteDeviceName = FALSE;
  1493. #ifndef UNICODE
  1494. TCHAR tszType[cbszVIDPID];
  1495. wsprintf(tszType, VID_PID_TEMPLATE, this->VendorID, this->ProductID);
  1496. TToU( wszType, cA(wszType), tszType );
  1497. #else
  1498. wsprintf(wszType, VID_PID_TEMPLATE, this->VendorID, this->ProductID);
  1499. #endif
  1500. #ifdef WINNT
  1501. #define INPUT_INF_FILENAME L"\\INF\\INPUT.INF"
  1502. if( g_wszDefaultHIDName[0] == L'\0' )
  1503. {
  1504. WCHAR wszInputINF[MAX_PATH];
  1505. UINT uLen;
  1506. uLen = GetWindowsDirectoryW( wszInputINF, cA( wszInputINF ) );
  1507. /*
  1508. * If the path is too long, don't set the filename
  1509. * so the the default string gets used when the
  1510. * GetPrivateProfileString fails.
  1511. */
  1512. if( uLen < cA(wszInputINF) - cA(INPUT_INF_FILENAME) )
  1513. {
  1514. memcpy( (PBYTE)&wszInputINF[uLen], (PBYTE)INPUT_INF_FILENAME, cbX( INPUT_INF_FILENAME ) );
  1515. }
  1516. /*
  1517. * Remember the length, if the string was too long to
  1518. * fit in the buffer there will be plenty to make a
  1519. * reasonable comparison.
  1520. */
  1521. g_uLenDefaultHIDSize = 2 * GetPrivateProfileStringW(
  1522. L"strings", L"HID.DeviceDesc", L"USB Human Interface Device",
  1523. g_wszDefaultHIDName, cA( g_wszDefaultHIDName ) - 1, wszInputINF );
  1524. }
  1525. #undef INPUT_INF_FILENAME
  1526. #endif
  1527. if( SUCCEEDED(hres = JoyReg_GetTypeInfo(wszType, &dijti, DITC_DISPLAYNAME))
  1528. && (dijti.wszDisplayName[0] != L'\0')
  1529. #ifdef WINNT
  1530. && ( (g_uLenDefaultHIDSize == 0)
  1531. || memcmp(dijti.wszDisplayName, g_wszDefaultHIDName, g_uLenDefaultHIDSize) ) // not equal
  1532. #else
  1533. && memcmp(dijti.wszDisplayName, wszDefHIDName, cbX(wszDefHIDName)-2) //not equal
  1534. #endif
  1535. )
  1536. {
  1537. LPDIPROPSTRING pdipstr = (PV)pdiph;
  1538. lstrcpyW(pdipstr->wsz, dijti.wszDisplayName);
  1539. SquirtSqflPtszV(sqflHid | sqflVerbose,
  1540. TEXT("Got name from type info %s"), pdipstr->wsz );
  1541. }
  1542. #ifdef WINNT
  1543. else if( fHasSpecificHardwareMatch( this->ptszId )
  1544. && SUCCEEDED( hres = DIHid_GetParentRegistryProperty(this->ptszId, SPDRP_DEVICEDESC, pdiph, 0x0 ) ) )
  1545. {
  1546. fOverwriteDeviceName = TRUE;
  1547. SquirtSqflPtszV(sqflHid | sqflVerbose,
  1548. TEXT("Got device description %s"), pdipstr->wsz );
  1549. }
  1550. #endif
  1551. else
  1552. {
  1553. if( fGetProductStringFromDevice( this, pdipstr->wsz, cbX( pdipstr->wsz ) ) )
  1554. {
  1555. fOverwriteDeviceName = TRUE;
  1556. SquirtSqflPtszV(sqflHid | sqflVerbose,
  1557. TEXT("Got description %s from device"), pdipstr->wsz );
  1558. }
  1559. else
  1560. {
  1561. /*
  1562. * Just make up a name from the caps
  1563. */
  1564. CType_MakeGameCtrlName( pdipstr->wsz,
  1565. this->dwDevType, this->dwAxes, this->dwButtons, this->dwPOVs );
  1566. fOverwriteDeviceName = TRUE;
  1567. SquirtSqflPtszV(sqflHid | sqflVerbose,
  1568. TEXT("Made up name %s"), pdipstr->wsz );
  1569. }
  1570. hres = S_OK;
  1571. }
  1572. if( fOverwriteDeviceName ) {
  1573. /*
  1574. * If we have a better name, overwrite the old one with this better one.
  1575. * See manbug 46438.
  1576. */
  1577. AssertF(this->hkType);
  1578. AssertF(pdipstr->wsz[0]);
  1579. hres = JoyReg_SetValue(this->hkType,
  1580. REGSTR_VAL_JOYOEMNAME, REG_SZ,
  1581. pdipstr->wsz,
  1582. cbX(pdipstr->wsz));
  1583. if( FAILED(hres) ){
  1584. SquirtSqflPtszV(sqflHid | sqflVerbose,
  1585. TEXT("Unable to overwrite generic device name with %s"), pdipstr->wsz );
  1586. // This failure (unlikely) doesn't matter.
  1587. hres = S_OK;
  1588. }
  1589. }
  1590. }
  1591. }
  1592. if( SUCCEEDED(hres)
  1593. && ( this->diHacks.nMaxDeviceNameLength < MAX_PATH )
  1594. && ( this->diHacks.nMaxDeviceNameLength < lstrlenW(pdipstr->wsz) ) )
  1595. {
  1596. pdipstr->wsz[this->diHacks.nMaxDeviceNameLength] = L'\0';
  1597. }
  1598. break;
  1599. }
  1600. case (DWORD)(UINT_PTR)DIPROP_JOYSTICKID:
  1601. if( ( GET_DIDEVICE_TYPE( this->dwDevType ) >= DI8DEVTYPE_GAMEMIN )
  1602. && ( this->dwDevType & DIDEVTYPE_HID ) )
  1603. {
  1604. LPDIPROPDWORD pdipdw = (PV)pdiph;
  1605. pdipdw->dwData = this->idJoy;
  1606. hres = S_OK;
  1607. } else
  1608. {
  1609. hres = E_NOTIMPL;
  1610. }
  1611. break;
  1612. case (DWORD)(UINT_PTR)DIPROP_GETPORTDISPLAYNAME:
  1613. #ifdef WINNT
  1614. /* For HID devices Port Display Name is the grand parent name */
  1615. hres = DIHid_GetParentRegistryProperty(this->ptszId, SPDRP_FRIENDLYNAME, pdiph, TRUE);
  1616. if( FAILED(hres) )
  1617. {
  1618. /* Maybe we can use the Product Name */
  1619. hres = DIHid_GetParentRegistryProperty(this->ptszId, SPDRP_DEVICEDESC, pdiph, TRUE);
  1620. if( SUCCEEDED(hres) )
  1621. {
  1622. /* We only sort of succeeded */
  1623. hres = S_FALSE;
  1624. }
  1625. }
  1626. if( SUCCEEDED(hres)
  1627. && ( this->diHacks.nMaxDeviceNameLength < MAX_PATH ) )
  1628. {
  1629. LPDIPROPSTRING pdipstr = (PV)pdiph;
  1630. if( this->diHacks.nMaxDeviceNameLength < lstrlenW(pdipstr->wsz) )
  1631. {
  1632. pdipstr->wsz[this->diHacks.nMaxDeviceNameLength] = L'\0';
  1633. }
  1634. }
  1635. #else
  1636. // Not sure how this works on Win9x
  1637. hres = E_NOTIMPL;
  1638. #endif
  1639. break;
  1640. case (DWORD)(UINT_PTR)(DIPROP_ENABLEREPORTID):
  1641. hres = E_NOTIMPL;
  1642. break;
  1643. case (DWORD)(UINT_PTR)DIPROP_VIDPID:
  1644. {
  1645. LPDIPROPDWORD pdipdw = (PV)pdiph;
  1646. /* Assert that a DWORD copy is all that is needed */
  1647. CAssertF( FIELD_OFFSET( CHID, VendorID ) + cbX( this->VendorID )
  1648. == FIELD_OFFSET( CHID, ProductID ) );
  1649. pdipdw->dwData = *((PDWORD)(&this->VendorID));
  1650. hres = S_OK;
  1651. }
  1652. break;
  1653. case (DWORD)(UINT_PTR)(DIPROP_MAPFILE):
  1654. if( ( this->dwDevType == DI8DEVTYPE_MOUSE )
  1655. || ( this->dwDevType == DI8DEVTYPE_KEYBOARD ) )
  1656. {
  1657. hres = E_NOTIMPL;
  1658. }
  1659. else
  1660. {
  1661. LPDIPROPSTRING pdipstr = (PV)pdiph;
  1662. LONG lRes;
  1663. DWORD dwBufferSize = cbX(pdipstr->wsz);
  1664. lRes = RegQueryStringValueW( this->hkProp, REGSTR_VAL_JOYOEMMAPFILE, pdipstr->wsz, &dwBufferSize );
  1665. hres = ( pdipstr->wsz[0] && ( lRes == ERROR_SUCCESS ) ) ? S_OK : DIERR_OBJECTNOTFOUND;
  1666. }
  1667. break;
  1668. case (DWORD)(UINT_PTR)(DIPROP_TYPENAME):
  1669. if( ( this->dwDevType == DI8DEVTYPE_MOUSE )
  1670. || ( this->dwDevType == DI8DEVTYPE_KEYBOARD ) )
  1671. {
  1672. hres = E_NOTIMPL;
  1673. }
  1674. else
  1675. {
  1676. LPDIPROPSTRING pdipstr = (PV)pdiph;
  1677. if( ( this->VendorID == MSFT_SYSTEM_VID )
  1678. &&( ( this->ProductID >= MSFT_SYSTEM_PID + JOY_HW_PREDEFMIN )
  1679. &&( this->ProductID < MSFT_SYSTEM_PID + JOY_HW_PREDEFMAX ) ) )
  1680. {
  1681. pdipstr->wsz[0] = L'#';
  1682. pdipstr->wsz[1] = L'0' + (WCHAR)(this->ProductID-MSFT_SYSTEM_PID);
  1683. pdipstr->wsz[2] = L'\0';
  1684. }
  1685. else
  1686. {
  1687. #ifndef UNICODE
  1688. TCHAR tszType[cbszVIDPID];
  1689. wsprintf(tszType, VID_PID_TEMPLATE, this->VendorID, this->ProductID);
  1690. TToU( pdipstr->wsz, cA(pdipstr->wsz), tszType );
  1691. #else
  1692. wsprintf(pdipstr->wsz, VID_PID_TEMPLATE, this->VendorID, this->ProductID);
  1693. #endif
  1694. }
  1695. hres = S_OK;
  1696. }
  1697. break;
  1698. default:
  1699. SquirtSqflPtszV(sqflHid | sqflBenign ,
  1700. TEXT("CHid_GetProperty(iobj=0xFFFFFFFF): E_NOTIMPL on guid: %08x"),
  1701. ppropi->pguid);
  1702. hres = E_NOTIMPL;
  1703. break;
  1704. }
  1705. } else
  1706. {
  1707. SquirtSqflPtszV(sqflHidDev | sqflError,
  1708. TEXT("CHid_GetProperty(iobj=%08x): E_NOTIMPL on guid: %08x"),
  1709. ppropi->iobj, ppropi->pguid);
  1710. hres = E_NOTIMPL;
  1711. }
  1712. ExitOleProcR();
  1713. return hres;
  1714. }
  1715. /*****************************************************************************
  1716. *
  1717. * @doc INTERNAL
  1718. *
  1719. * @func LONG | CHid_CoordinateTransform |
  1720. *
  1721. * Convert numbers from logical to physical or vice versa.
  1722. *
  1723. * If either the To or From values look suspicious, then
  1724. * ignore them and leave the values alone.
  1725. *
  1726. * @parm PLMINMAX | Dst |
  1727. *
  1728. * Destination min/max information.
  1729. *
  1730. * @parm PLMINMAX | Src |
  1731. *
  1732. * Source min/max information.
  1733. *
  1734. * @parm LONG | lVal |
  1735. *
  1736. * Source value to be converted.
  1737. *
  1738. * @returns
  1739. *
  1740. * The destination value after conversion.
  1741. *
  1742. *****************************************************************************/
  1743. LONG EXTERNAL
  1744. CHid_CoordinateTransform(PLMINMAX Dst, PLMINMAX Src, LONG lVal)
  1745. {
  1746. /*
  1747. * Note that the sanity check is symmetric in Src and Dst.
  1748. * This is important, so that we never get into a weird
  1749. * case where we can convert one way but can't convert back.
  1750. */
  1751. if(Dst->Min < Dst->Max && Src->Min < Src->Max)
  1752. {
  1753. /*
  1754. * We need to perform a straight linear interpolation.
  1755. * The math comes out like this:
  1756. *
  1757. * x - x0 y - y0
  1758. * ------- = -------
  1759. * x1 - x0 y1 - y0
  1760. *
  1761. * If you now do a "solve for y", you get
  1762. *
  1763. *
  1764. * y1 - y0
  1765. * y = (x - x0) ------- + y0
  1766. * x1 - x0
  1767. *
  1768. * where "x" is Src, "y" is Dst, 0 is Min, and 1 is Max.
  1769. *
  1770. *
  1771. */
  1772. lVal = MulDiv(lVal - Src->Min, Dst->Max - Dst->Min,
  1773. Src->Max - Src->Min) + Dst->Min;
  1774. }
  1775. return lVal;
  1776. }
  1777. #ifndef WINNT
  1778. /*****************************************************************************
  1779. *
  1780. * @doc INTERNAL
  1781. *
  1782. * @method int | CHid | IsMatchingJoyDevice |
  1783. *
  1784. * Does the cached joystick ID match us?
  1785. *
  1786. * @parm OUT PVXDINITPARMS | pvip |
  1787. *
  1788. * On success, contains parameter values.
  1789. *
  1790. * @returns
  1791. *
  1792. * Nonzero on success.
  1793. *
  1794. *****************************************************************************/
  1795. BOOL INTERNAL
  1796. CHid_IsMatchingJoyDevice(PCHID this, PVXDINITPARMS pvip)
  1797. {
  1798. CHAR sz[MAX_PATH];
  1799. LPSTR pszPath;
  1800. BOOL fRc;
  1801. pszPath = JoyReg_JoyIdToDeviceInterface_95(this->idJoy, pvip, sz);
  1802. if(pszPath)
  1803. {
  1804. SquirtSqflPtszV(sqfl | sqflTrace,
  1805. TEXT("CHid_IsMatchingJoyDevice: %d -> %s"),
  1806. this->idJoy, pszPath);
  1807. #ifdef UNICODE
  1808. {
  1809. CHAR szpath[MAX_PATH];
  1810. UToA( szpath, cA(szpath), (LPWSTR)this->ptszPath);
  1811. fRc = ( lstrcmpiA(pszPath, szpath) == 0x0 );
  1812. }
  1813. #else
  1814. fRc = ( lstrcmpiA(pszPath, (PCHAR)this->ptszPath) == 0x0 );
  1815. #endif
  1816. } else
  1817. {
  1818. fRc = FALSE;
  1819. }
  1820. return fRc;
  1821. }
  1822. /*****************************************************************************
  1823. *
  1824. * @doc INTERNAL
  1825. *
  1826. * @method void | CHid | FindJoyDevice |
  1827. *
  1828. * Look for the VJOYD device that matches us, if any.
  1829. *
  1830. * On return, the <e CHID.idJoy> field contains the
  1831. * matching joystick number, or -1 if not found.
  1832. *
  1833. * @parm OUT PVXDINITPARMS | pvip |
  1834. *
  1835. * On success, contains parameter values.
  1836. *
  1837. *****************************************************************************/
  1838. void INTERNAL
  1839. CHid_FindJoyDevice(PCHID this, PVXDINITPARMS pvip)
  1840. {
  1841. /*
  1842. * If we have a cached value, and it still works, then
  1843. * our job is done.
  1844. */
  1845. if(this->idJoy >= 0 &&
  1846. CHid_IsMatchingJoyDevice(this, pvip))
  1847. {
  1848. } else
  1849. {
  1850. /*
  1851. * Need to keep looking. (Or start looking.)
  1852. *
  1853. * A countdown loop is nicer, but for efficiency, we count
  1854. * upwards, since the joystick we want tends to be near the
  1855. * beginning.
  1856. */
  1857. for(this->idJoy = 0; this->idJoy < cJoyMax; this->idJoy++)
  1858. {
  1859. if(CHid_IsMatchingJoyDevice(this, pvip))
  1860. {
  1861. goto done;
  1862. }
  1863. }
  1864. this->idJoy = -1;
  1865. }
  1866. done:;
  1867. }
  1868. #endif
  1869. /*****************************************************************************
  1870. *
  1871. * @doc INTERNAL
  1872. *
  1873. * @method int | CHid | MapAxis |
  1874. *
  1875. * Find VJOYD axis from HID axis, if one.
  1876. *
  1877. * @parm PVXDINITPARMS | pvip |
  1878. *
  1879. * Parameter values that let us known which axes VJOYD
  1880. * has mapped to which HID Axes.
  1881. *
  1882. * @parm UINT | iobj |
  1883. *
  1884. * Object index of the object whose axis value changed.
  1885. *
  1886. * @returns
  1887. *
  1888. * The VJOYD axis number that changed (0 to 5), or -1
  1889. * if there is no matching axis. There will be no matching
  1890. * axis if, for example, the device has something that is
  1891. * not expressible via VJOYD (e.g., a temperature sensor).
  1892. *
  1893. *****************************************************************************/
  1894. int INTERNAL
  1895. CHid_MapAxis(PCHID this, PVXDINITPARMS pvip, UINT iobj)
  1896. {
  1897. int iAxis;
  1898. DWORD dwUsage;
  1899. AssertF(this->dcb.lpVtbl->GetUsage == CHid_GetUsage);
  1900. dwUsage = CHid_GetUsage(&this->dcb, (int)iobj);
  1901. if(dwUsage)
  1902. {
  1903. /*
  1904. * A countdown loop lets us fall out with the correct failure
  1905. * code (namely, -1).
  1906. */
  1907. iAxis = cJoyPosAxisMax;
  1908. while(--iAxis >= 0)
  1909. {
  1910. if(pvip->Usages[iAxis] == dwUsage)
  1911. {
  1912. break;
  1913. }
  1914. }
  1915. } else
  1916. {
  1917. /*
  1918. * Eek! No usage information for the axis. Then it certainly
  1919. * isn't a VJOYD axis.
  1920. */
  1921. iAxis = -1;
  1922. }
  1923. return iAxis;
  1924. }
  1925. #ifndef WINNT
  1926. /*****************************************************************************
  1927. *
  1928. * @doc INTERNAL
  1929. *
  1930. * @method void | CHid | UpdateVjoydCalibration |
  1931. *
  1932. * Somebody changed the calibration on a single axis. If we
  1933. * are shadowing a joystick, then look for the VJOYD alias of
  1934. * our device and update its registry settings, too.
  1935. *
  1936. *
  1937. * @parm UINT | iobj |
  1938. *
  1939. * Object index of the object whose calibration changed.
  1940. *
  1941. *****************************************************************************/
  1942. void EXTERNAL
  1943. CHid_UpdateVjoydCalibration(PCHID this, UINT iobj)
  1944. {
  1945. HRESULT hres;
  1946. int iAxis;
  1947. VXDINITPARMS vip;
  1948. DIJOYCONFIG cfg;
  1949. PHIDGROUPCAPS pcaps;
  1950. PJOYRANGECONVERT pjrc;
  1951. AssertF(iobj < this->df.dwNumObjs);
  1952. /*
  1953. * Proceed if...
  1954. *
  1955. * - We can find the VJOYD device we correspond to.
  1956. * - We can find the axis that got updated.
  1957. * - The indicated axis has capability information.
  1958. * - The indicated axis has calibration information.
  1959. * - We can read the old calibration information.
  1960. */
  1961. CHid_FindJoyDevice(this, &vip);
  1962. if(this->idJoy >= 0 &&
  1963. (iAxis = CHid_MapAxis(this, &vip, iobj)) >= 0 &&
  1964. (pcaps = this->rghoc[iobj].pcaps) != NULL &&
  1965. (pjrc = this->rghoc[iobj].pjrc) != NULL &&
  1966. SUCCEEDED(hres = JoyReg_GetConfig(this->idJoy, &cfg,
  1967. DIJC_REGHWCONFIGTYPE)))
  1968. {
  1969. PLMINMAX Dst = &pcaps->Physical;
  1970. PLMINMAX Src = &pcaps->Logical;
  1971. AssertF(iAxis < cJoyPosAxisMax);
  1972. #define JoyPosValue(phwc, f, i) \
  1973. *(LPDWORD)pvAddPvCb(&(phwc)->hwv.jrvHardware.f, \
  1974. ibJoyPosAxisFromPosAxis(i))
  1975. /*
  1976. * We use logical coordinates, but VJOYD wants physical
  1977. * coordinates, so do the conversion while we copy the
  1978. * values.
  1979. */
  1980. #define ConvertValue(f1, f2) \
  1981. JoyPosValue(&cfg.hwc, f1, iAxis) = \
  1982. CHid_CoordinateTransform(Dst, Src, pjrc->f2) \
  1983. ConvertValue(jpMin , dwPmin);
  1984. ConvertValue(jpMax , dwPmax);
  1985. ConvertValue(jpCenter, dwPc );
  1986. #undef ConvertValue
  1987. #undef JoyPosValue
  1988. /*
  1989. * Notice that we do *not* pass the DIJC_UPDATEALIAS flag
  1990. * because WE ARE THE ALIAS! If we had passed the flag,
  1991. * then JoyReg would create us and attempt to update our
  1992. * calibration which we don't want it to do because the
  1993. * whole thing was our idea in the first place.
  1994. */
  1995. hres = JoyReg_SetConfig(this->idJoy, &cfg.hwc, &cfg,
  1996. DIJC_REGHWCONFIGTYPE);
  1997. }
  1998. }
  1999. /*****************************************************************************
  2000. *
  2001. * @doc INTERNAL
  2002. *
  2003. * @method void | CHid | UpdateCalibrationFromVjoyd |
  2004. *
  2005. * This function is only for Win9x. Joy.cpl uses winmm (through vjoyd)
  2006. * to calibrate the device, and save calibration information directly into
  2007. * registry without notifying HID. ANother issue is: vjoyd only use unsigned
  2008. * data (physical data), while HID also use signed data. When we read
  2009. * calibration information from VJOYD, we need do conversion.
  2010. *
  2011. * @parm UINT | iobj |
  2012. *
  2013. * Object index of the object whose calibration changed.
  2014. *
  2015. *****************************************************************************/
  2016. void EXTERNAL
  2017. CHid_UpdateCalibrationFromVjoyd(PCHID this, UINT iobj, LPDIOBJECTCALIBRATION pCal)
  2018. {
  2019. HRESULT hres;
  2020. int iAxis;
  2021. VXDINITPARMS vip;
  2022. DIJOYCONFIG cfg;
  2023. PHIDGROUPCAPS pcaps;
  2024. PJOYRANGECONVERT pjrc;
  2025. AssertF(iobj < this->df.dwNumObjs);
  2026. /*
  2027. * Proceed if...
  2028. *
  2029. * - We can find the VJOYD device we correspond to.
  2030. * - We can find the axis that got updated.
  2031. * - The indicated axis has capability information.
  2032. * - The indicated axis has calibration information.
  2033. * - We can read the calibration information.
  2034. */
  2035. CHid_FindJoyDevice(this, &vip);
  2036. if(this->idJoy >= 0 &&
  2037. (iAxis = CHid_MapAxis(this, &vip, iobj)) >= 0 &&
  2038. (pcaps = this->rghoc[iobj].pcaps) != NULL &&
  2039. (pjrc = this->rghoc[iobj].pjrc) != NULL &&
  2040. SUCCEEDED(hres = JoyReg_GetConfig(this->idJoy, &cfg,
  2041. DIJC_REGHWCONFIGTYPE)))
  2042. {
  2043. PLMINMAX Src = &pcaps->Physical;
  2044. PLMINMAX Dst = &pcaps->Logical;
  2045. AssertF(iAxis < cJoyPosAxisMax);
  2046. #define JoyPosValue(phwc, f, i) \
  2047. *(LPDWORD)pvAddPvCb(&(phwc)->hwv.jrvHardware.f, \
  2048. ibJoyPosAxisFromPosAxis(i))
  2049. /*
  2050. * We use logical coordinates, but VJOYD wants physical
  2051. * coordinates, so do the conversion while we copy the
  2052. * values.
  2053. */
  2054. #define ConvertValue(f1, f2) \
  2055. pCal->f2 = CHid_CoordinateTransform(Dst, Src, \
  2056. JoyPosValue(&cfg.hwc, f1, iAxis) )
  2057. ConvertValue(jpMin , lMin);
  2058. ConvertValue(jpMax , lMax);
  2059. ConvertValue(jpCenter, lCenter);
  2060. #undef ConvertValue
  2061. #undef JoyPosValue
  2062. }
  2063. }
  2064. #endif
  2065. #ifdef WINNT
  2066. /*****************************************************************************
  2067. *
  2068. * @doc INTERNAL
  2069. *
  2070. * @func HRESULT | DIHid_SetParentRegistryProperty |
  2071. *
  2072. * Wrapper around <f SetupDiSetDeviceRegistryProperty>
  2073. * that handles character set issues.
  2074. *
  2075. * @parm LPTSTR ptszId
  2076. *
  2077. * Device Instance ID.
  2078. *
  2079. * @parm DWORD | dwProperty |
  2080. *
  2081. * The property being queried.
  2082. *
  2083. * @parm LPCDIPROPHEADER | diph |
  2084. *
  2085. * Property data to be set.
  2086. *
  2087. *****************************************************************************/
  2088. HRESULT INTERNAL
  2089. DIHid_SetParentRegistryProperty(LPTSTR ptszId, DWORD dwProperty, LPCDIPROPHEADER pdiph)
  2090. {
  2091. HDEVINFO hdev;
  2092. TCHAR tsz[MAX_PATH];
  2093. LPDIPROPSTRING pstr = (PV)pdiph;
  2094. HRESULT hres = E_FAIL;
  2095. hdev = SetupDiCreateDeviceInfoList(NULL, NULL);
  2096. if(hdev != INVALID_HANDLE_VALUE)
  2097. {
  2098. SP_DEVINFO_DATA dinf;
  2099. ZeroX(tsz);
  2100. #ifdef UNICODE
  2101. lstrcpyW(tsz, pstr->wsz);
  2102. #else
  2103. UToA(tsz, cA(tsz), pstr->wsz);
  2104. #endif
  2105. dinf.cbSize = cbX(SP_DEVINFO_DATA);
  2106. if(SetupDiOpenDeviceInfo(hdev, ptszId, NULL, 0, &dinf))
  2107. {
  2108. CONFIGRET cr;
  2109. DEVINST DevInst;
  2110. if( (cr = CM_Get_Parent(&DevInst, dinf.DevInst, 0x0) ) == CR_SUCCESS )
  2111. {
  2112. CAssertF( SPDRP_DEVICEDESC +1 == CM_DRP_DEVICEDESC );
  2113. CAssertF( SPDRP_FRIENDLYNAME +1 == CM_DRP_FRIENDLYNAME );
  2114. if( ( cr = CM_Set_DevNode_Registry_Property(
  2115. DevInst,
  2116. dwProperty+1,
  2117. (LPBYTE)tsz,
  2118. MAX_PATH *cbX(TCHAR),
  2119. 0x0 ) ) == CR_SUCCESS )
  2120. {
  2121. hres = S_OK;
  2122. } else
  2123. {
  2124. SquirtSqflPtszV(sqfl | sqflVerbose,
  2125. TEXT("CM_Get_DevNode_Registry_Property FAILED") );
  2126. }
  2127. } else
  2128. {
  2129. SquirtSqflPtszV(sqfl | sqflVerbose,
  2130. TEXT("CM_Get_Parent FAILED") );
  2131. }
  2132. } else
  2133. {
  2134. SquirtSqflPtszV(sqfl | sqflVerbose,
  2135. TEXT("SetupDiOpenDeviceInfo FAILED, le = %d"), GetLastError() );
  2136. }
  2137. SetupDiDestroyDeviceInfoList(hdev);
  2138. } else
  2139. {
  2140. SquirtSqflPtszV(sqfl | sqflError,
  2141. TEXT("SetupDiCreateDeviceInfoList FAILED, le = %d"), GetLastError() );
  2142. }
  2143. return hres;
  2144. }
  2145. #else
  2146. /*****************************************************************************
  2147. *
  2148. * @doc INTERNAL
  2149. *
  2150. * @func HRESULT | DIHid_SetRegistryProperty |
  2151. *
  2152. * Wrapper around <f SetupDiSetDeviceRegistryProperty>
  2153. * that handles character set issues.
  2154. *
  2155. * @parm LPTSTR ptszId
  2156. *
  2157. * Device Instance ID.
  2158. *
  2159. * @parm DWORD | dwProperty |
  2160. *
  2161. * The property being queried.
  2162. *
  2163. * @parm LPCDIPROPHEADER | diph |
  2164. *
  2165. * Property data to be set.
  2166. *
  2167. *****************************************************************************/
  2168. HRESULT INTERNAL
  2169. DIHid_SetRegistryProperty(LPTSTR ptszId, DWORD dwProperty, LPCDIPROPHEADER pdiph)
  2170. {
  2171. HDEVINFO hdev;
  2172. TCHAR tsz[MAX_PATH];
  2173. LPDIPROPSTRING pstr = (PV)pdiph;
  2174. HRESULT hres;
  2175. hdev = SetupDiCreateDeviceInfoList(NULL, NULL);
  2176. if(hdev != INVALID_HANDLE_VALUE)
  2177. {
  2178. SP_DEVINFO_DATA dinf;
  2179. ZeroX(tsz);
  2180. #ifdef UNICODE
  2181. lstrcpyW(tsz, pstr->wsz);
  2182. #else
  2183. UToA(tsz, cA(tsz), pstr->wsz);
  2184. #endif
  2185. dinf.cbSize = cbX(SP_DEVINFO_DATA);
  2186. if(SetupDiOpenDeviceInfo(hdev, ptszId, NULL, 0, &dinf))
  2187. {
  2188. if(SetupDiSetDeviceRegistryProperty(hdev, &dinf, dwProperty,
  2189. (LPBYTE)tsz, MAX_PATH*cbX(TCHAR)) )
  2190. {
  2191. hres = S_OK;
  2192. } else
  2193. {
  2194. hres = E_FAIL;
  2195. }
  2196. } else
  2197. {
  2198. hres = E_FAIL;
  2199. }
  2200. SetupDiDestroyDeviceInfoList(hdev);
  2201. } else
  2202. {
  2203. hres = E_FAIL;
  2204. }
  2205. return hres;
  2206. }
  2207. #endif
  2208. /*****************************************************************************
  2209. *
  2210. * @doc INTERNAL
  2211. *
  2212. * @method HRESULT | CHid | SetAxisProperty |
  2213. *
  2214. * Set the appropriate axis property (or return E_NOTIMPL if the
  2215. * property is not an axis property).
  2216. * If the request is to set a property on the device,
  2217. * then convert it into separate requests, one for each
  2218. * axis.
  2219. *
  2220. * @parm PDCHID | this |
  2221. *
  2222. * The device object.
  2223. *
  2224. * @parm IN LPCDIPROPINFO | ppropi |
  2225. *
  2226. * Information describing the property being set.
  2227. *
  2228. * @parm LPCDIPROPHEADER | pdiph |
  2229. *
  2230. * Structure containing property value.
  2231. *
  2232. * @returns
  2233. *
  2234. * Returns a COM error code. The following error codes are
  2235. * intended to be illustrative and not necessarily comprehensive.
  2236. *
  2237. * <c DI_OK> = <c S_OK>: The operation completed successfully.
  2238. *
  2239. * <c E_NOTIMPL> nothing happened. The caller will do
  2240. * the default thing in response to <c E_NOTIMPL>.
  2241. *
  2242. *****************************************************************************/
  2243. STDMETHODIMP
  2244. CHid_SetAxisProperty
  2245. (
  2246. PCHID this,
  2247. LPCDIPROPINFO ppropi,
  2248. LPCDIPROPHEADER pdiph
  2249. )
  2250. {
  2251. HRESULT hres;
  2252. INT iObjLimit;
  2253. INT iObj;
  2254. if (ppropi->dwDevType == 0)
  2255. { /* For device try every object */
  2256. iObj = 0;
  2257. iObjLimit = this->df.dwNumObjs;
  2258. }
  2259. else
  2260. { /* For axis just do the requested one */
  2261. iObj = ppropi->iobj;
  2262. iObjLimit = ppropi->iobj + 1;
  2263. }
  2264. hres = S_OK;
  2265. for( ; iObj < iObjLimit; iObj++ )
  2266. {
  2267. PJOYRANGECONVERT pjrc;
  2268. if( pjrc = this->rghoc[iObj].pjrc )
  2269. {
  2270. if( (this->df.rgodf[iObj].dwType &
  2271. (DIDFT_ALIAS | DIDFT_VENDORDEFINED | DIDFT_OUTPUT | DIDFT_ABSAXIS)) == DIDFT_ABSAXIS)
  2272. {
  2273. PHIDGROUPCAPS pcaps = this->rghoc[iObj].pcaps;
  2274. DIPROPCAL cal;
  2275. /*
  2276. * Specific calibrations arrive in VJOYD coordinates.
  2277. * We need to convert them to DirectInput (logical)
  2278. * coordinates if so.
  2279. */
  2280. if(ppropi->pguid == DIPROP_SPECIFICCALIBRATION)
  2281. {
  2282. if( pcaps )
  2283. {
  2284. PLMINMAX Dst = &pcaps->Logical;
  2285. PLMINMAX Src = &pcaps->Physical;
  2286. LPDIPROPCAL pcal = CONTAINING_RECORD(pdiph, DIPROPCAL, diph);
  2287. cal.lMin = CHid_CoordinateTransform(Dst, Src, pcal->lMin);
  2288. cal.lCenter = CHid_CoordinateTransform(Dst, Src, pcal->lCenter);
  2289. cal.lMax = CHid_CoordinateTransform(Dst, Src, pcal->lMax);
  2290. pdiph = &cal.diph;
  2291. }
  2292. else
  2293. {
  2294. AssertF( ppropi->dwDevType == 0 );
  2295. /*
  2296. * Ignore the error. If this is an object set
  2297. * property validation should have caught this
  2298. * already and the DX7 patch code for device set
  2299. * property special cased E_NOTIMPL so that one bad
  2300. * axis would not cause the whole call to fail when
  2301. * other axes may be OK.
  2302. * A bit flakey but "this should never happen"
  2303. */
  2304. continue;
  2305. }
  2306. }
  2307. hres = CCal_SetProperty( pjrc, ppropi, pdiph, this->hkInstType );
  2308. #ifndef WINNT
  2309. /*
  2310. * If we successfully changed the calibration of a game
  2311. * controller device, then see if it's a VJOYD device.
  2312. */
  2313. if(SUCCEEDED(hres) &&
  2314. ppropi->pguid == DIPROP_CALIBRATION &&
  2315. GET_DIDEVICE_TYPE(this->dwDevType) >= DI8DEVTYPE_GAMEMIN)
  2316. {
  2317. CHid_UpdateVjoydCalibration(this, ppropi->iobj);
  2318. }
  2319. #endif
  2320. }
  2321. #ifdef WINNT
  2322. else if( (this->df.rgodf[iObj].dwType &
  2323. (DIDFT_ALIAS | DIDFT_VENDORDEFINED | DIDFT_OUTPUT | DIDFT_POV)) == DIDFT_POV)
  2324. {
  2325. PHIDGROUPCAPS pcaps = this->rghoc[iObj].pcaps;
  2326. if( pcaps )
  2327. {
  2328. if( pcaps->IsPolledPOV )
  2329. {
  2330. hres = CCal_SetProperty(pjrc, ppropi, pdiph, this->hkInstType);
  2331. if( SUCCEEDED(hres) ) {
  2332. CHid_LoadCalibrations(this);
  2333. CHid_InitParseData( this );
  2334. }
  2335. }
  2336. else
  2337. {
  2338. if( ppropi->dwDevType != 0 )
  2339. {
  2340. hres = E_NOTIMPL;
  2341. }
  2342. }
  2343. }
  2344. else
  2345. {
  2346. AssertF( ppropi->dwDevType == 0 );
  2347. /*
  2348. * Ignore the error. If this is an object set
  2349. * property validation should have caught this
  2350. * already and the DX7 patch code for device set
  2351. * property special cased E_NOTIMPL so that one bad
  2352. * axis would not cause the whole call to fail when
  2353. * other axes may be OK.
  2354. * A bit flakey but "this should never happen"
  2355. */
  2356. continue;
  2357. }
  2358. }
  2359. #endif
  2360. }
  2361. else
  2362. {
  2363. /*
  2364. * If the object cannot have an axis property set, the DX7 code
  2365. * returned E_NOTIMPL when it should have returned some parameter
  2366. * error. For a device, this is not an error as we are iterating
  2367. * all objects looking for absolute axes.
  2368. * If it is an absolute axis but has no range conversion, return
  2369. * E_NOTIMPL to match previous versions for an object but ignore
  2370. * for the device. This probably should be an E_FAIL...
  2371. */
  2372. if( ppropi->dwDevType != 0 )
  2373. {
  2374. hres = E_NOTIMPL;
  2375. }
  2376. }
  2377. }
  2378. /*
  2379. * Don't need to hold/unhold here because the app called us so it should
  2380. * not be releasing us at the same time. (mb:34570)
  2381. */
  2382. CHid_LoadCalibrations(this);
  2383. if( SUCCEEDED(hres) )
  2384. {
  2385. /*
  2386. * Until such time as CHid_InitParseData returns anything other than
  2387. * S_OK, don't update a possibly more informative result with this.
  2388. */
  2389. D( HRESULT hresDbg = )
  2390. CHid_InitParseData( this );
  2391. D( AssertF( hresDbg == S_OK ); )
  2392. }
  2393. return hres;
  2394. }
  2395. /*****************************************************************************
  2396. *
  2397. * @doc INTERNAL
  2398. *
  2399. * @method HRESULT | CHid | SetProperty |
  2400. *
  2401. * Set a hid device property.
  2402. *
  2403. * @parm PCHID | this |
  2404. *
  2405. * The hid object.
  2406. *
  2407. * @parm IN LPCDIPROPINFO | ppropi |
  2408. *
  2409. * Information describing the property being set.
  2410. *
  2411. * @parm LPCDIPROPHEADER | pdiph |
  2412. *
  2413. * Structure containing property value.
  2414. *
  2415. * @returns
  2416. *
  2417. * Returns a COM error code. The following error codes are
  2418. * intended to be illustrative and not necessarily comprehensive.
  2419. *
  2420. * <c DI_OK> = <c S_OK>: The operation completed successfully.
  2421. *
  2422. * <c E_NOTIMPL> nothing happened. The caller will do
  2423. * the default thing in response to <c E_NOTIMPL>.
  2424. *
  2425. *****************************************************************************/
  2426. STDMETHODIMP
  2427. CHid_SetProperty(PDICB pdcb, LPCDIPROPINFO ppropi, LPCDIPROPHEADER pdiph)
  2428. {
  2429. HRESULT hres;
  2430. PCHID this;
  2431. EnterProcI(IDirectInputDeviceCallback::Hid::SetProperty,
  2432. (_ "pxxp", pdcb, ppropi->pguid, ppropi->iobj, pdiph));
  2433. /*
  2434. * This is an internal interface, so we can skimp on validation.
  2435. */
  2436. this = _thisPvNm(pdcb, dcb);
  2437. if(ppropi->iobj < this->df.dwNumObjs)
  2438. {
  2439. /*
  2440. * Object Property
  2441. */
  2442. PHIDGROUPCAPS pcaps;
  2443. AssertF(ppropi->dwDevType == this->df.rgodf[ppropi->iobj].dwType);
  2444. AssertF(ppropi->iobj == CHid_ObjFromType(this, ppropi->dwDevType));
  2445. if( pcaps = this->rghoc[ppropi->iobj].pcaps )
  2446. {
  2447. switch((DWORD)(UINT_PTR)ppropi->pguid)
  2448. {
  2449. case (DWORD)(UINT_PTR)(DIPROP_ENABLEREPORTID):
  2450. {
  2451. LPDIPROPDWORD ppropdw = CONTAINING_RECORD(pdiph, DIPROPDWORD, diph);
  2452. AssertF(pcaps->wReportId < this->wMaxReportId[pcaps->type]);
  2453. AssertF(this->pEnableReportId[pcaps->type]);
  2454. hres = S_OK;
  2455. if( ppropdw->dwData == 0x1 )
  2456. {
  2457. *(this->pEnableReportId[pcaps->type] + pcaps->wReportId) = 0x1;
  2458. pcaps->fReportDisabled = FALSE;
  2459. } else
  2460. {
  2461. *(this->pEnableReportId[pcaps->type] + pcaps->wReportId) = 0x0;
  2462. pcaps->fReportDisabled = TRUE;
  2463. }
  2464. }
  2465. break;
  2466. default:
  2467. AssertF(ppropi->dwDevType == this->df.rgodf[ppropi->iobj].dwType);
  2468. AssertF(ppropi->iobj == CHid_ObjFromType(this, ppropi->dwDevType));
  2469. hres = CHid_SetAxisProperty( this, ppropi, pdiph );
  2470. }
  2471. } else
  2472. {
  2473. SquirtSqflPtszV(sqflHidDev | sqflError,
  2474. TEXT("CHid_SetProperty FAILED due to missing caps for type 0x%08x, obj %d"),
  2475. ppropi->dwDevType, ppropi->iobj );
  2476. hres = E_NOTIMPL;
  2477. }
  2478. } else if(ppropi->iobj == 0xFFFFFFFF)
  2479. { /* Device property */
  2480. switch((DWORD)(UINT_PTR)ppropi->pguid)
  2481. {
  2482. case (DWORD)(UINT_PTR)DIPROP_GUIDANDPATH:
  2483. SquirtSqflPtszV(sqflHidDev | sqflError,
  2484. TEXT("CHid_SetProperty(iobj=%08x): PROP_GUIDANDPATH is read only.") );
  2485. hres = E_NOTIMPL;
  2486. break;
  2487. case (DWORD)(UINT_PTR)DIPROP_INSTANCENAME:
  2488. /*
  2489. * Friendly names cause all manner of problems with devices that
  2490. * use auto detection so only allow non-predefined analog devices
  2491. * to use them.
  2492. */
  2493. if( ( this->VendorID == MSFT_SYSTEM_VID )
  2494. && ( this->ProductID >= MSFT_SYSTEM_PID + JOY_HW_PREDEFMAX )
  2495. && ( ( this->ProductID & 0xff00 ) == MSFT_SYSTEM_PID ) )
  2496. {
  2497. AssertF(this->hkType);
  2498. if( this->hkType )
  2499. {
  2500. LPDIPROPSTRING pstr = (PV)pdiph;
  2501. hres = JoyReg_SetValue(this->hkType,
  2502. REGSTR_VAL_JOYOEMNAME, REG_SZ,
  2503. pstr->wsz,
  2504. cbX(pstr->wsz));
  2505. if( SUCCEEDED(hres ) )
  2506. {
  2507. SquirtSqflPtszV(sqflHid | sqflVerbose,
  2508. TEXT( "Set instance name %s"), pstr->wsz );
  2509. hres = S_OK;
  2510. } else {
  2511. hres = E_FAIL;
  2512. }
  2513. } else {
  2514. hres = E_FAIL;
  2515. }
  2516. }
  2517. else
  2518. {
  2519. /*
  2520. * GenJ returns E_NOTIMPL for this property so do the same
  2521. */
  2522. hres = E_NOTIMPL;
  2523. }
  2524. break;
  2525. case (DWORD)(UINT_PTR)DIPROP_PRODUCTNAME:
  2526. #ifdef WINNT
  2527. hres = DIHid_SetParentRegistryProperty(this->ptszId, SPDRP_DEVICEDESC, pdiph);
  2528. #else
  2529. hres = DIHid_SetRegistryProperty(this->ptszId, SPDRP_DEVICEDESC, pdiph);
  2530. #endif
  2531. break;
  2532. case (DWORD)(UINT_PTR)(DIPROP_ENABLEREPORTID):
  2533. {
  2534. LPDIPROPDWORD ppropdw = CONTAINING_RECORD(pdiph, DIPROPDWORD, diph);
  2535. UINT iType;
  2536. if( ppropdw->dwData == 0x0 )
  2537. {
  2538. for( iType = 0x0; iType < HidP_Max; iType++)
  2539. {
  2540. ZeroBuf(this->pEnableReportId[iType], this->wMaxReportId[iType]);
  2541. }
  2542. } else
  2543. {
  2544. for( iType = 0x0; iType < HidP_Max; iType++)
  2545. {
  2546. memset(this->pEnableReportId[iType], 0x1, this->wMaxReportId[iType]);
  2547. }
  2548. }
  2549. hres = S_OK;
  2550. }
  2551. break;
  2552. case (DWORD)(UINT_PTR)DIPROP_RANGE:
  2553. case (DWORD)(UINT_PTR)DIPROP_DEADZONE:
  2554. case (DWORD)(UINT_PTR)DIPROP_SATURATION:
  2555. case (DWORD)(UINT_PTR)DIPROP_CALIBRATIONMODE:
  2556. case (DWORD)(UINT_PTR)DIPROP_CALIBRATION:
  2557. hres = CHid_SetAxisProperty( this, ppropi, pdiph );
  2558. break;
  2559. default:
  2560. SquirtSqflPtszV(sqflHidDev| sqflBenign,
  2561. TEXT("CHid_SetProperty(iobj=%08x): E_NOTIMPL on guid: %08x"),
  2562. ppropi->iobj, ppropi->pguid);
  2563. hres = E_NOTIMPL;
  2564. break;
  2565. }
  2566. } else
  2567. {
  2568. SquirtSqflPtszV(sqflHidDev | sqflError,
  2569. TEXT("CHid_SetProperty(iobj=%08x): E_NOTIMPL on guid: %08x"),
  2570. ppropi->iobj, ppropi->pguid);
  2571. hres = E_NOTIMPL;
  2572. }
  2573. ExitOleProcR();
  2574. return hres;
  2575. }
  2576. /*****************************************************************************
  2577. *
  2578. * @doc INTERNAL
  2579. *
  2580. * @method void | CHid | GetCapabilities |
  2581. *
  2582. * Get Hid device capabilities.
  2583. *
  2584. * @parm LPDIDEVCAPS | pdc |
  2585. *
  2586. * Device capabilities structure to receive result.
  2587. *
  2588. * @returns
  2589. * <c S_OK> on success.
  2590. *
  2591. *****************************************************************************/
  2592. STDMETHODIMP
  2593. CHid_GetCapabilities(PDICB pdcb, LPDIDEVCAPS pdc)
  2594. {
  2595. HRESULT hres;
  2596. PCHID this;
  2597. HANDLE h;
  2598. DWORD dwFlags2 = 0;
  2599. EnterProcI(IDirectInputDeviceCallback::Hid::GetCapabilities,
  2600. (_ "pp", pdcb, pdc));
  2601. /*
  2602. * This is an internal interface, so we can skimp on validation.
  2603. */
  2604. this = _thisPvNm(pdcb, dcb);
  2605. /*
  2606. * We must check connectivity by opening the device, because NT
  2607. * leaves the device in the info list even though it has
  2608. * been unplugged.
  2609. */
  2610. h = CHid_OpenDevicePath(this, FILE_FLAG_OVERLAPPED);
  2611. if(h != INVALID_HANDLE_VALUE)
  2612. {
  2613. #ifndef WINNT
  2614. if( this->hkType )
  2615. {
  2616. VXDINITPARMS vip;
  2617. CHid_FindJoyDevice(this, &vip);
  2618. if( TRUE == CHid_IsMatchingJoyDevice( this, &vip ) )
  2619. {
  2620. DWORD dwFlags1;
  2621. if( SUCCEEDED( JoyReg_GetValue( this->hkType,
  2622. REGSTR_VAL_FLAGS1, REG_BINARY,
  2623. &dwFlags1,
  2624. cbX(dwFlags1) ) ) )
  2625. {
  2626. if( dwFlags1 & JOYTYPE_NOHIDDIRECT )
  2627. {
  2628. pdc->dwFlags |= DIDC_ALIAS;
  2629. }
  2630. }
  2631. }
  2632. }
  2633. #endif // !WINNT
  2634. CloseHandle(h);
  2635. if( this->pvi->fl & VIFL_UNPLUGGED )
  2636. {
  2637. pdc->dwFlags &= ~DIDC_ATTACHED;
  2638. } else
  2639. {
  2640. pdc->dwFlags |= DIDC_ATTACHED;
  2641. }
  2642. } else
  2643. {
  2644. pdc->dwFlags &= ~DIDC_ATTACHED;
  2645. }
  2646. if( this->IsPolledInput )
  2647. {
  2648. pdc->dwFlags |= DIDC_POLLEDDEVICE;
  2649. }
  2650. if( this->hkProp )
  2651. {
  2652. JoyReg_GetValue( this->hkProp, REGSTR_VAL_FLAGS2, REG_BINARY,
  2653. &dwFlags2, cbX(dwFlags2) );
  2654. }
  2655. if( !( dwFlags2 & JOYTYPE_HIDEACTIVE ) )
  2656. {
  2657. // Currently we only hide "fictional" keyboards and mice.
  2658. if( ( GET_DIDEVICE_TYPE( this->dwDevType ) == DI8DEVTYPE_MOUSE )
  2659. ||( GET_DIDEVICE_TYPE( this->dwDevType ) == DI8DEVTYPE_KEYBOARD )
  2660. )
  2661. {
  2662. dwFlags2 = DIHid_DetectHideAndRevealFlags(this) ;
  2663. }
  2664. }
  2665. if( dwFlags2 & JOYTYPE_HIDEACTIVE )
  2666. {
  2667. switch( GET_DIDEVICE_TYPE( this->dwDevType ) )
  2668. {
  2669. case DI8DEVTYPE_DEVICE:
  2670. if( dwFlags2 & JOYTYPE_DEVICEHIDE )
  2671. {
  2672. pdc->dwFlags |= DIDC_HIDDEN;
  2673. }
  2674. break;
  2675. case DI8DEVTYPE_MOUSE:
  2676. if( dwFlags2 & JOYTYPE_MOUSEHIDE )
  2677. {
  2678. pdc->dwFlags |= DIDC_HIDDEN;
  2679. }
  2680. break;
  2681. case DI8DEVTYPE_KEYBOARD:
  2682. if( dwFlags2 & JOYTYPE_KEYBHIDE )
  2683. {
  2684. pdc->dwFlags |= DIDC_HIDDEN;
  2685. }
  2686. break;
  2687. default:
  2688. if( dwFlags2 & JOYTYPE_GAMEHIDE )
  2689. {
  2690. pdc->dwFlags |= DIDC_HIDDEN;
  2691. }
  2692. break;
  2693. }
  2694. }
  2695. pdc->dwDevType = this->dwDevType;
  2696. pdc->dwAxes = this->dwAxes;
  2697. pdc->dwButtons = this->dwButtons;
  2698. pdc->dwPOVs = this->dwPOVs;
  2699. hres = S_OK;
  2700. ExitOleProcR();
  2701. return hres;
  2702. }
  2703. /*****************************************************************************
  2704. *
  2705. * @doc INTERNAL
  2706. *
  2707. * @method HRESULT | CHid | GetDeviceState |
  2708. *
  2709. * Obtains the state of the Hid device.
  2710. *
  2711. * It is the caller's responsibility to have validated all the
  2712. * parameters and ensure that the device has been acquired.
  2713. *
  2714. * @parm OUT LPVOID | lpvData |
  2715. *
  2716. * Hid data in the preferred data format.
  2717. *
  2718. * @returns
  2719. *
  2720. * Returns a COM error code. The following error codes are
  2721. * intended to be illustrative and not necessarily comprehensive.
  2722. *
  2723. * <c DI_OK> = <c S_OK>: The operation completed successfully.
  2724. *
  2725. * <c DIERR_INVALIDPARAM> = <c E_INVALIDARG>: The
  2726. * <p lpmdr> parameter is not a valid pointer.
  2727. *
  2728. *****************************************************************************/
  2729. STDMETHODIMP
  2730. CHid_GetDeviceState(PDICB pdcb, LPVOID pvData)
  2731. {
  2732. HRESULT hres;
  2733. PCHID this;
  2734. EnterProcI(IDirectInputDeviceCallback::Hid::GetDeviceState,
  2735. (_ "pp", pdcb, pvData));
  2736. /*
  2737. * This is an internal interface, so we can skimp on validation.
  2738. */
  2739. this = _thisPvNm(pdcb, dcb);
  2740. AssertF(this->pvi);
  2741. AssertF(this->pvPhys);
  2742. AssertF(this->cbPhys);
  2743. if(this->pvi->fl & VIFL_ACQUIRED)
  2744. {
  2745. CHid_GetPhysicalState(this, pvData);
  2746. hres = S_OK;
  2747. } else
  2748. {
  2749. hres = DIERR_INPUTLOST;
  2750. }
  2751. ExitOleProcR();
  2752. return hres;
  2753. }
  2754. /*****************************************************************************
  2755. *
  2756. * @doc INTERNAL
  2757. *
  2758. * @method HRESULT | CHid | GetObjectInfo |
  2759. *
  2760. * Obtain the friendly name and FF/HID information
  2761. * of an object.
  2762. *
  2763. * @parm IN LPCDIPROPINFO | ppropi |
  2764. *
  2765. * Information describing the object being accessed.
  2766. *
  2767. * @parm IN OUT LPDIDEVICEOBJECTINSTANCEW | pdidioiW |
  2768. *
  2769. * Structure to receive information. All fields have been
  2770. * filled in up to the <e DIDEVICEOBJECTINSTANCE.tszObjName>.
  2771. *
  2772. * @returns
  2773. *
  2774. * Returns a COM error code.
  2775. *
  2776. *****************************************************************************/
  2777. STDMETHODIMP
  2778. CHid_GetObjectInfo(PDICB pdcb, LPCDIPROPINFO ppropi,
  2779. LPDIDEVICEOBJECTINSTANCEW pdidoiW)
  2780. {
  2781. HRESULT hres;
  2782. PCHID this;
  2783. EnterProcI(IDirectInputDeviceCallback::Hid::GetObjectInfo,
  2784. (_ "pxp", pdcb, ppropi->iobj, pdidoiW));
  2785. /*
  2786. * This is an internal interface, so we can skimp on validation.
  2787. */
  2788. this = _thisPvNm(pdcb, dcb);
  2789. AssertF((int) ppropi->iobj >= 0);
  2790. if(ppropi->iobj < this->df.dwNumObjs)
  2791. {
  2792. UINT uiInstance = ppropi->iobj;
  2793. PHIDGROUPCAPS pcaps;
  2794. AssertF(ppropi->dwDevType == this->df.rgodf[uiInstance].dwType);
  2795. AssertF(uiInstance == CHid_ObjFromType(this, ppropi->dwDevType));
  2796. pcaps = this->rghoc[uiInstance].pcaps;
  2797. /*
  2798. * pcaps might be NULL if HID messed up and left gaps
  2799. * in the index lists.
  2800. */
  2801. if(pcaps)
  2802. {
  2803. UINT ids, duiInstance;
  2804. AssertF(pcaps->dwSignature == HIDGROUPCAPS_SIGNATURE);
  2805. /*
  2806. * See if there's anything in the registry that will help.
  2807. */
  2808. CType_RegGetObjectInfo(this->hkType, ppropi->dwDevType, pdidoiW);
  2809. if(ppropi->dwDevType & DIDFT_COLLECTION)
  2810. {
  2811. ids = IDS_COLLECTIONTEMPLATE;
  2812. duiInstance = 0;
  2813. } else
  2814. {
  2815. if(ppropi->dwDevType & DIDFT_BUTTON)
  2816. {
  2817. ids = IDS_BUTTONTEMPLATE;
  2818. } else if(ppropi->dwDevType & DIDFT_AXIS)
  2819. {
  2820. ids = IDS_AXISTEMPLATE;
  2821. } else if(ppropi->dwDevType & DIDFT_POV)
  2822. {
  2823. ids = IDS_POVTEMPLATE;
  2824. } else
  2825. {
  2826. ids = IDS_UNKNOWNTEMPLATE;
  2827. }
  2828. /*
  2829. * Now convert the uiInstance to a duiInstance,
  2830. * giving the index of this object into the group.
  2831. */
  2832. AssertF(HidP_IsValidReportType(pcaps->type));
  2833. duiInstance = uiInstance -
  2834. (this->rgdwBase[pcaps->type] +
  2835. pcaps->DataIndexMin);
  2836. }
  2837. /*
  2838. * Okay, now we have all the info we need to proceed.
  2839. */
  2840. /*
  2841. * If there was no overriding name in the registry, then
  2842. * try to get a custom name from the usage page/usage.
  2843. * If even that fails, then use the generic name.
  2844. * Note, generic names will contain zero based numbers
  2845. * which can look wrong if some objects have names and
  2846. * others take defaults.
  2847. */
  2848. if(pdidoiW->tszName[0])
  2849. {
  2850. } else
  2851. if(GetHIDString(pcaps->UsageMin + duiInstance,
  2852. pcaps->UsagePage,
  2853. pdidoiW->tszName, cA(pdidoiW->tszName)))
  2854. {
  2855. if(ppropi->dwDevType & DIDFT_COLLECTION)
  2856. {
  2857. InsertCollectionNumber(DIDFT_GETINSTANCE( ppropi->dwDevType ),
  2858. pdidoiW->tszName);
  2859. }
  2860. } else
  2861. {
  2862. GetNthString(pdidoiW->tszName, ids,
  2863. DIDFT_GETINSTANCE( ppropi->dwDevType ));
  2864. }
  2865. if(pdidoiW->dwSize >= cbX(DIDEVICEOBJECTINSTANCE_DX5W))
  2866. {
  2867. pdidoiW->wCollectionNumber = pcaps->LinkCollection;
  2868. pdidoiW->wDesignatorIndex = pcaps->DesignatorMin + duiInstance;
  2869. if(pdidoiW->wDesignatorIndex > pcaps->DesignatorMax)
  2870. {
  2871. pdidoiW->wDesignatorIndex = pcaps->DesignatorMax;
  2872. }
  2873. /*
  2874. * Much as you may try, you cannot override the usage
  2875. * page and usage. Doing so would mess up the GUID
  2876. * selection code that happens in DIHIDINI.C.
  2877. *
  2878. * If you change your mind and allow overridden usage
  2879. * pages and usages, then you'll also have to change
  2880. * CHid_GetUsage.
  2881. *
  2882. * At this point, the registry overrides have already
  2883. * been read so defeat the override here.
  2884. */
  2885. pdidoiW->wUsagePage = pcaps->UsagePage;
  2886. pdidoiW->wUsage = pcaps->UsageMin + duiInstance;
  2887. pdidoiW->dwDimension = pcaps->Units;
  2888. pdidoiW->wExponent = pcaps->Exponent;
  2889. pdidoiW->wReportId = pcaps->wReportId;
  2890. }
  2891. hres = S_OK;
  2892. } else
  2893. {
  2894. hres = E_INVALIDARG;
  2895. }
  2896. } else
  2897. {
  2898. hres = E_INVALIDARG;
  2899. }
  2900. ExitOleProcR();
  2901. return hres;
  2902. }
  2903. /*****************************************************************************
  2904. *
  2905. * @doc INTERNAL
  2906. *
  2907. * @method DWORD | CHid | GetUsage |
  2908. *
  2909. * Given an object index, return the usage and usage page,
  2910. * packed into a single <t DWORD>.
  2911. *
  2912. * @parm int | iobj |
  2913. *
  2914. * The object index to convert.
  2915. *
  2916. * @returns
  2917. *
  2918. * Returns a <c DIMAKEUSAGEDWORD> of the resulting usage and
  2919. * usage page, or zero on error.
  2920. *
  2921. *****************************************************************************/
  2922. STDMETHODIMP_(DWORD)
  2923. CHid_GetUsage(PDICB pdcb, int iobj)
  2924. {
  2925. PCHID this;
  2926. PHIDGROUPCAPS pcaps;
  2927. DWORD dwRc;
  2928. EnterProcI(IDirectInputDeviceCallback::Hid::GetUsage,
  2929. (_ "pu", pdcb, iobj));
  2930. /*
  2931. * This is an internal interface, so we can skimp on validation.
  2932. */
  2933. this = _thisPvNm(pdcb, dcb);
  2934. AssertF(iobj >= 0);
  2935. AssertF((UINT)iobj < this->df.dwNumObjs);
  2936. pcaps = this->rghoc[iobj].pcaps;
  2937. /*
  2938. * pcaps might be NULL if HID messed up and left gaps
  2939. * in the index lists.
  2940. */
  2941. if(pcaps)
  2942. {
  2943. UINT duiInstance;
  2944. AssertF(pcaps->dwSignature == HIDGROUPCAPS_SIGNATURE);
  2945. if(this->df.rgodf[iobj].dwType & DIDFT_COLLECTION)
  2946. {
  2947. duiInstance = 0;
  2948. } else
  2949. {
  2950. /*
  2951. * Now convert the iobj to a duiInstance,
  2952. * giving the index of this object into the group.
  2953. */
  2954. AssertF(HidP_IsValidReportType(pcaps->type));
  2955. duiInstance = iobj -
  2956. (this->rgdwBase[pcaps->type] +
  2957. pcaps->DataIndexMin);
  2958. }
  2959. /*
  2960. * CHid_GetObjectInfo also assumes that there is no way
  2961. * to override the usage page and usage values in the
  2962. * registry.
  2963. */
  2964. dwRc = DIMAKEUSAGEDWORD(pcaps->UsagePage,
  2965. pcaps->UsageMin + duiInstance);
  2966. } else
  2967. {
  2968. dwRc = 0;
  2969. }
  2970. ExitProcX(dwRc);
  2971. return dwRc;
  2972. }
  2973. /*****************************************************************************
  2974. *
  2975. * @doc INTERNAL
  2976. *
  2977. * @method HRESULT | CHid | MapUsage |
  2978. *
  2979. *
  2980. * Given a usage and usage page (munged into a single
  2981. * <t DWORD>), find a device object that matches it.
  2982. *
  2983. * @parm DWORD | dwUsage |
  2984. *
  2985. * The usage page and usage combined into a single <t DWORD>
  2986. * with the <f DIMAKEUSAGEDWORD> macro.
  2987. *
  2988. * @parm PINT | piOut |
  2989. *
  2990. * Receives the object index of the found object, if successful.
  2991. *
  2992. * @returns
  2993. *
  2994. * Returns a COM error code.
  2995. *
  2996. * <c S_OK> if an object was found.
  2997. *
  2998. * <c DIERR_NOTFOUND> if no matching object was found.
  2999. *
  3000. *****************************************************************************/
  3001. STDMETHODIMP
  3002. CHid_MapUsage(PDICB pdcb, DWORD dwUsage, PINT piOut)
  3003. {
  3004. HRESULT hres;
  3005. PCHID this;
  3006. UINT icaps;
  3007. UINT uiObj;
  3008. UINT duiObj;
  3009. EnterProcI(IDirectInputDeviceCallback::Hid::MapUsage,
  3010. (_ "px", pdcb, dwUsage));
  3011. /*
  3012. * This is an internal interface, so we can skimp on validation.
  3013. */
  3014. this = _thisPvNm(pdcb, dcb);
  3015. for(icaps = 0; icaps < this->ccaps; icaps++)
  3016. {
  3017. PHIDGROUPCAPS pcaps = &this->rgcaps[icaps];
  3018. /*
  3019. * Shall we support mapping HidP_Output usage?
  3020. * If we should, it is easy to add it later.
  3021. */
  3022. uiObj = this->rgdwBase[HidP_Input] + pcaps->DataIndexMin;
  3023. for(duiObj = 0; duiObj < pcaps->cObj; duiObj++)
  3024. {
  3025. if( dwUsage == DIMAKEUSAGEDWORD(pcaps->UsagePage, pcaps->UsageMin+duiObj) )
  3026. {
  3027. *piOut = uiObj+duiObj;
  3028. AssertF(*piOut < (INT)this->df.dwNumObjs);
  3029. hres = S_OK;
  3030. goto done;
  3031. }
  3032. }
  3033. }
  3034. hres = DIERR_NOTFOUND;
  3035. done:;
  3036. ExitBenignOleProcR();
  3037. return hres;
  3038. }
  3039. /*****************************************************************************
  3040. *
  3041. * @doc INTERNAL
  3042. *
  3043. * @method HRESULT | CHid | SetCooperativeLevel |
  3044. *
  3045. * Notify the device of the cooperative level.
  3046. *
  3047. * @parm IN HWND | hwnd |
  3048. *
  3049. * The window handle.
  3050. *
  3051. * @parm IN DWORD | dwFlags |
  3052. *
  3053. * The cooperativity level.
  3054. *
  3055. * @returns
  3056. *
  3057. * Returns a COM error code.
  3058. *
  3059. *****************************************************************************/
  3060. STDMETHODIMP
  3061. CHid_SetCooperativeLevel(PDICB pdcb, HWND hwnd, DWORD dwFlags)
  3062. {
  3063. HRESULT hres;
  3064. PCHID this;
  3065. EnterProcI(IDirectInputDeviceCallback::Hid::SetCooperativityLevel,
  3066. (_ "pxx", pdcb, hwnd, dwFlags));
  3067. /*
  3068. * This is an internal interface, so we can skimp on validation.
  3069. */
  3070. this = _thisPvNm(pdcb, dcb);
  3071. /*
  3072. * We won't subclass Motocross Madness. See NT bug 262280.
  3073. * Use the app hacks for MCM and any app like it.
  3074. */
  3075. if( !this->diHacks.fNoSubClass )
  3076. {
  3077. AssertF(this->pvi);
  3078. /*
  3079. * First get out of the old window.
  3080. */
  3081. CHid_RemoveSubclass(this);
  3082. /*
  3083. * Prefix warns that "this" may have been freed (mb:34570) however
  3084. * If you're in SetCooperativeLevel and you have a window subclassed
  3085. * then there must be a hold for the subclassed window as well as
  3086. * one for the unreleased interface so the Common_Unhold won't free
  3087. * the pointer.
  3088. */
  3089. /*
  3090. * If a new window is passed, then subclass it so we can
  3091. * watch for joystick configuration change messages.
  3092. *
  3093. * If we can't, don't worry. All it means that we won't
  3094. * be able to catch when the user recalibrates a device,
  3095. * which isn't very often.
  3096. */
  3097. if(hwnd)
  3098. {
  3099. if(SetWindowSubclass(hwnd, CHid_SubclassProc, 0x0, (ULONG_PTR)this))
  3100. {
  3101. this->hwnd = hwnd;
  3102. Common_Hold(this);
  3103. }
  3104. } else
  3105. {
  3106. RPF("SetCooperativeLevel: You really shouldn't pass hwnd = 0; "
  3107. "device calibration may be dodgy");
  3108. }
  3109. }
  3110. hres = S_OK;
  3111. ExitOleProcR();
  3112. return hres;
  3113. }
  3114. /*****************************************************************************
  3115. *
  3116. * @doc INTERNAL
  3117. *
  3118. * @method HRESULT | CHid | RunControlPanel |
  3119. *
  3120. * Run the Hid control panel.
  3121. *
  3122. * @parm IN HWND | hwndOwner |
  3123. *
  3124. * The owner window.
  3125. *
  3126. * @parm DWORD | dwFlags |
  3127. *
  3128. * Flags.
  3129. *
  3130. *****************************************************************************/
  3131. STDMETHODIMP
  3132. CHid_RunControlPanel(PDICB pdcb, HWND hwnd, DWORD dwFlags)
  3133. {
  3134. HRESULT hres;
  3135. PCHID this;
  3136. EnterProcI(IDirectInputDeviceCallback::Hid::RunControlPanel,
  3137. (_ "pxx", pdcb, hwnd, dwFlags));
  3138. /*
  3139. * This is an internal interface, so we can skimp on validation.
  3140. */
  3141. this = _thisPvNm(pdcb, dcb);
  3142. /*
  3143. * How to invoke HID cpl?
  3144. *
  3145. * Currently, we just launch joy.cpl. If more HID devices show up
  3146. * which don't belong to game control panel, we may change it to
  3147. * proper cpl.
  3148. *
  3149. * on NT hresRunControlPanel(TEXT("srcmgr.cpl,@2"));
  3150. * on 9x hresRunControlPanel(TEXT("sysdm.cpl,@0,1"));
  3151. *
  3152. */
  3153. hres = hresRunControlPanel(TEXT("joy.cpl"));
  3154. ExitOleProcR();
  3155. return hres;
  3156. }
  3157. /*****************************************************************************
  3158. *
  3159. * @doc INTERNAL
  3160. *
  3161. * @method HRESULT | CHid | GetFFConfigKey |
  3162. *
  3163. * Open and return the registry key that contains
  3164. * force feedback configuration information.
  3165. *
  3166. * @parm DWORD | sam |
  3167. *
  3168. * Security access mask.
  3169. *
  3170. * @parm PHKEY | phk |
  3171. *
  3172. * Receives the registry key.
  3173. *
  3174. *****************************************************************************/
  3175. STDMETHODIMP
  3176. CHid_GetFFConfigKey(PDICB pdcb, DWORD sam, PHKEY phk)
  3177. {
  3178. HRESULT hres;
  3179. PCHID this;
  3180. EnterProcI(IDirectInputDeviceCallback::HID::GetFFConfigKey,
  3181. (_ "px", pdcb, sam));
  3182. /*
  3183. * This is an internal interface, so we can skimp on validation.
  3184. */
  3185. this = _thisPvNm(pdcb, dcb);
  3186. hres = JoyReg_OpenFFKey(this->hkType, sam, phk);
  3187. AssertF(fLeqvFF(SUCCEEDED(hres), *phk));
  3188. if(FAILED(hres) && this->fPIDdevice )
  3189. {
  3190. *phk = NULL;
  3191. hres = S_FALSE;
  3192. }
  3193. ExitBenignOleProcPpvR(phk);
  3194. return hres;
  3195. }
  3196. /*****************************************************************************
  3197. *
  3198. * @doc INTERNAL
  3199. *
  3200. * @method HRESULT | CHid | GetDeviceInfo |
  3201. *
  3202. * Obtain general information about the device.
  3203. *
  3204. * @parm OUT LPDIDEVICEINSTANCEW | pdiW |
  3205. *
  3206. * <t DEVICEINSTANCE> to be filled in. The
  3207. * <e DEVICEINSTANCE.dwSize> and <e DEVICEINSTANCE.guidInstance>
  3208. * have already been filled in.
  3209. *
  3210. * Secret convenience: <e DEVICEINSTANCE.guidProduct> is equal
  3211. * to <e DEVICEINSTANCE.guidInstance>.
  3212. *
  3213. *****************************************************************************/
  3214. STDMETHODIMP
  3215. CHid_GetDeviceInfo(PDICB pdcb, LPDIDEVICEINSTANCEW pdiW)
  3216. {
  3217. HRESULT hres;
  3218. PCHID this;
  3219. DIPROPINFO propi;
  3220. DIPROPSTRING dips;
  3221. EnterProcI(IDirectInputDeviceCallback::Hid::GetDeviceInfo,
  3222. (_ "pp", pdcb, pdiW));
  3223. /*
  3224. * This is an internal interface, so we can skimp on validation.
  3225. */
  3226. this = _thisPvNm(pdcb, dcb);
  3227. AssertF(IsValidSizeDIDEVICEINSTANCEW(pdiW->dwSize));
  3228. DICreateStaticGuid(&pdiW->guidProduct, this->ProductID, this->VendorID);
  3229. pdiW->dwDevType = this->dwDevType;
  3230. if(pdiW->dwSize >= cbX(DIDEVICEINSTANCE_DX5W))
  3231. {
  3232. pdiW->wUsagePage = this->caps.UsagePage;
  3233. pdiW->wUsage = this->caps.Usage;
  3234. }
  3235. propi.dwDevType = DIPH_DEVICE;
  3236. propi.iobj = 0xFFFFFFFF;
  3237. propi.pguid = DIPROP_PRODUCTNAME;
  3238. if(SUCCEEDED(hres = pdcb->lpVtbl->GetProperty(pdcb, &propi, &dips.diph)) )
  3239. {
  3240. lstrcpyW(pdiW->tszProductName, dips.wsz);
  3241. }
  3242. propi.pguid = DIPROP_INSTANCENAME;
  3243. if( FAILED(pdcb->lpVtbl->GetProperty(pdcb, &propi, &dips.diph)))
  3244. {
  3245. // Use Product Name
  3246. }
  3247. lstrcpyW(pdiW->tszInstanceName, dips.wsz);
  3248. if(pdiW->dwSize >= cbX(DIDEVICEINSTANCE_DX5W))
  3249. {
  3250. HKEY hkFF;
  3251. HRESULT hresFF;
  3252. /*
  3253. * If there is a force feedback driver, then fetch the driver CLSID
  3254. * as the FF GUID.
  3255. */
  3256. hresFF = CHid_GetFFConfigKey(pdcb, KEY_QUERY_VALUE, &hkFF);
  3257. if(SUCCEEDED(hresFF))
  3258. {
  3259. LONG lRc;
  3260. TCHAR tszClsid[ctchGuid];
  3261. lRc = RegQueryString(hkFF, TEXT("CLSID"), tszClsid, cA(tszClsid));
  3262. if(lRc == ERROR_SUCCESS &&
  3263. ParseGUID(&pdiW->guidFFDriver, tszClsid))
  3264. {
  3265. } else
  3266. {
  3267. ZeroX(pdiW->guidFFDriver);
  3268. }
  3269. RegCloseKey(hkFF);
  3270. }
  3271. }
  3272. ExitOleProcR();
  3273. return hres;
  3274. }
  3275. /*****************************************************************************
  3276. *
  3277. * @doc INTERNAL
  3278. *
  3279. * @method HRESULT | CHid | CreateEffect |
  3280. *
  3281. *
  3282. * Create an <i IDirectInputEffectDriver> interface.
  3283. *
  3284. * @parm LPDIRECTINPUTEFFECTSHEPHERD * | ppes |
  3285. *
  3286. * Receives the shepherd for the effect driver.
  3287. *
  3288. *****************************************************************************/
  3289. STDMETHODIMP
  3290. CHid_CreateEffect(PDICB pdcb, LPDIRECTINPUTEFFECTSHEPHERD *ppes)
  3291. {
  3292. HRESULT hres;
  3293. PCHID this;
  3294. HKEY hk;
  3295. EnterProcI(IDirectInputDeviceCallback::HID::CreateEffect, (_ "p", pdcb));
  3296. /*
  3297. * This is an internal interface, so we can skimp on validation.
  3298. */
  3299. this = _thisPvNm(pdcb, dcb);
  3300. hres = CHid_GetFFConfigKey(pdcb, KEY_QUERY_VALUE, &hk);
  3301. if(SUCCEEDED(hres))
  3302. {
  3303. DIHIDFFINITINFO init;
  3304. PHIDDEVICEINFO phdi;
  3305. hres = CEShep_New(hk, 0, &IID_IDirectInputEffectShepherd, ppes);
  3306. if(SUCCEEDED(hres))
  3307. {
  3308. #ifndef UNICODE
  3309. WCHAR wszPath[MAX_PATH];
  3310. #endif
  3311. init.dwSize = cbX(init);
  3312. #ifdef UNICODE
  3313. init.pwszDeviceInterface = this->ptszPath;
  3314. #else
  3315. init.pwszDeviceInterface = wszPath;
  3316. TToU(wszPath, cA(wszPath), this->ptszPath);
  3317. #endif
  3318. DllEnterCrit();
  3319. phdi = phdiFindHIDDeviceInterface(this->ptszPath);
  3320. if( phdi )
  3321. {
  3322. init.GuidInstance = phdi->guid;
  3323. } else
  3324. {
  3325. ZeroX(init.GuidInstance);
  3326. }
  3327. DllLeaveCrit();
  3328. hres = (*ppes)->lpVtbl->DeviceID((*ppes), this->idJoy, TRUE, &init);
  3329. if(SUCCEEDED(hres))
  3330. {
  3331. } else
  3332. {
  3333. Invoke_Release(ppes);
  3334. }
  3335. }
  3336. RegCloseKey(hk);
  3337. } else
  3338. {
  3339. hres = E_NOTIMPL;
  3340. *ppes = 0;
  3341. }
  3342. ExitOleProcPpvR(ppes);
  3343. return hres;
  3344. }
  3345. /*****************************************************************************
  3346. *
  3347. * @doc INTERNAL
  3348. *
  3349. * @method HRESULT | CHid | SendOutputReport |
  3350. *
  3351. * Actually send the report as an output report.
  3352. *
  3353. * @parm PHIDREPORTINFO | phri |
  3354. *
  3355. * The report being sent.
  3356. *
  3357. * @returns
  3358. *
  3359. * Returns a COM error code.
  3360. *
  3361. *****************************************************************************/
  3362. void CALLBACK
  3363. CHid_DummyCompletion(DWORD dwError, DWORD cbRead, LPOVERLAPPED po)
  3364. {
  3365. }
  3366. STDMETHODIMP
  3367. CHid_SendOutputReport(PCHID this, PHIDREPORTINFO phri)
  3368. {
  3369. HRESULT hres;
  3370. OVERLAPPED o;
  3371. AssertF(phri == &this->hriOut);
  3372. ZeroX(o);
  3373. /*
  3374. * Annoying API: Since this->hdev was opened
  3375. * as FILE_FLAG_OVERLAPPED, *all* I/O must be overlapped.
  3376. * So we simulate a synchronous I/O by issuing an
  3377. * overlapped I/O and waiting for the completion.
  3378. */
  3379. if(WriteFileEx(this->hdev, phri->pvReport,
  3380. phri->cbReport, &o, CHid_DummyCompletion))
  3381. {
  3382. do
  3383. {
  3384. SleepEx(INFINITE, TRUE);
  3385. } while(!HasOverlappedIoCompleted(&o));
  3386. if(phri->cbReport == o.InternalHigh)
  3387. {
  3388. hres = S_OK;
  3389. } else
  3390. {
  3391. RPF("SendDeviceData: Wrong HID output report size?");
  3392. hres = E_FAIL; /* Aigh! HID lied to me! */
  3393. }
  3394. } else
  3395. {
  3396. hres = hresLe(GetLastError());
  3397. /*
  3398. * Note, we have not broken the read loop so there is no need to
  3399. * force the device unaquired (though dinput.dll does).
  3400. *
  3401. * If this causes problems revert to the old behavior.
  3402. * CEm_ForceDeviceUnacquire(pemFromPvi(this->pvi)->ped, 0x0);
  3403. */
  3404. }
  3405. return hres;
  3406. }
  3407. /*****************************************************************************
  3408. *
  3409. * @doc INTERNAL
  3410. *
  3411. * @method HRESULT | CHid | SendFeatureReport |
  3412. *
  3413. * Actually send the report as an feature report.
  3414. *
  3415. * @parm PHIDREPORTINFO | phri |
  3416. *
  3417. * The report being sent.
  3418. *
  3419. * @returns
  3420. *
  3421. * Returns a COM error code.
  3422. *
  3423. *****************************************************************************/
  3424. STDMETHODIMP
  3425. CHid_SendFeatureReport(PCHID this, PHIDREPORTINFO phri)
  3426. {
  3427. HRESULT hres;
  3428. AssertF(phri == &this->hriFea);
  3429. if(HidD_SetFeature(this->hdev, phri->pvReport, phri->cbReport))
  3430. {
  3431. hres = S_OK;
  3432. } else
  3433. {
  3434. RPF("SendDeviceData: Unable to set HID feature");
  3435. hres = hresLe(GetLastError());
  3436. }
  3437. return hres;
  3438. }
  3439. /*****************************************************************************
  3440. *
  3441. * @doc INTERNAL
  3442. *
  3443. * @method HRESULT | CHid | SendDeviceData |
  3444. *
  3445. * Spew some data to the device.
  3446. *
  3447. * @parm DWORD | cbdod |
  3448. *
  3449. * Size of each object.
  3450. *
  3451. * @parm IN LPCDIDEVICEOBJECTDATA | rgdod |
  3452. *
  3453. * Array of <t DIDEVICEOBJECTDATA> structures.
  3454. *
  3455. * @parm INOUT LPDWORD | pdwInOut |
  3456. *
  3457. * On entry, number of items to send;
  3458. * on exit, number of items actually sent.
  3459. *
  3460. * @parm DWORD | fl |
  3461. *
  3462. * Flags.
  3463. *
  3464. * @returns
  3465. *
  3466. * Returns a COM error code. The following error codes are
  3467. * intended to be illustrative and not necessarily comprehensive.
  3468. *
  3469. * <c DI_OK> = <c S_OK>: The operation completed successfully.
  3470. *
  3471. * <c DIERR_REPORTFULL>: Too many items are set in the report.
  3472. * (More than can be sent to the device)
  3473. *
  3474. *****************************************************************************/
  3475. STDMETHODIMP
  3476. CHid_SendDeviceData(PDICB pdcb, DWORD cbdod, LPCDIDEVICEOBJECTDATA rgdod,
  3477. LPDWORD pdwInOut, DWORD fl)
  3478. {
  3479. HRESULT hres;
  3480. PCHID this;
  3481. DWORD dwIn, dw;
  3482. const BYTE * pbcod;
  3483. EnterProcI(IDirectInputDeviceCallback::Hid::SendDeviceData,
  3484. (_ "xpux", cbdod, pdcb, *pdwInOut, fl));
  3485. /*
  3486. * This is an internal interface, so we can skimp on validation.
  3487. */
  3488. this = _thisPvNm(pdcb, dcb);
  3489. dwIn = *pdwInOut;
  3490. *pdwInOut = 0;
  3491. if(fl & DISDD_CONTINUE)
  3492. {
  3493. } else
  3494. {
  3495. CHid_ResetDeviceData(this, &this->hriOut, HidP_Output);
  3496. CHid_ResetDeviceData(this, &this->hriFea, HidP_Feature);
  3497. }
  3498. for(dw = 0, pbcod = (const BYTE*)rgdod; dw < dwIn; dw++)
  3499. {
  3500. DWORD dwType = ((LPDIDEVICEOBJECTDATA)pbcod)->dwOfs;
  3501. UINT uiObj = CHid_ObjFromType(this, dwType);
  3502. if(uiObj < this->df.dwNumObjs &&
  3503. DIDFT_FINDMATCH(this->df.rgodf[uiObj].dwType, dwType))
  3504. {
  3505. hres = CHid_AddDeviceData(this, uiObj, ((LPDIDEVICEOBJECTDATA)pbcod)->dwData);
  3506. if(FAILED(hres))
  3507. {
  3508. *pdwInOut = dw;
  3509. goto done;
  3510. }
  3511. } else
  3512. {
  3513. hres = E_INVALIDARG;
  3514. goto done;
  3515. }
  3516. pbcod += cbdod;
  3517. }
  3518. /*
  3519. * All the items made it into the buffer.
  3520. */
  3521. *pdwInOut = dw;
  3522. /*
  3523. * Now send it all out.
  3524. */
  3525. if(SUCCEEDED(hres = CHid_SendHIDReport(this, &this->hriOut, HidP_Output,
  3526. CHid_SendOutputReport)) &&
  3527. SUCCEEDED(hres = CHid_SendHIDReport(this, &this->hriFea, HidP_Feature,
  3528. CHid_SendFeatureReport)))
  3529. {
  3530. }
  3531. done:;
  3532. ExitOleProcR();
  3533. return hres;
  3534. }
  3535. /*****************************************************************************
  3536. *
  3537. * @doc INTERNAL
  3538. *
  3539. * @method HRESULT | CHid | Poll |
  3540. *
  3541. * Read any polled input and features to see what's there.
  3542. *
  3543. * @returns
  3544. *
  3545. * <c S_OK> if we pinged okay.
  3546. * <c S_FALSE> doesn't require polling
  3547. * <c DIERR_UNPLUGGED> the device requires polling and is unplugged
  3548. * Other errors returned from HID are possible for a polled device.
  3549. *
  3550. *****************************************************************************/
  3551. STDMETHODIMP
  3552. CHid_Poll(PDICB pdcb)
  3553. {
  3554. //Prefix: 45082
  3555. HRESULT hres = S_FALSE; //We need use S_FALSE as default. See manbug 31874.
  3556. PCHID this;
  3557. EnterProcI(IDirectInputDeviceCallback::Hid::Poll, (_ "p", pdcb));
  3558. /*
  3559. * This is an internal interface, so we can skimp on validation.
  3560. */
  3561. this = _thisPvNm(pdcb, dcb);
  3562. if( this->IsPolledInput )
  3563. {
  3564. hres = DIERR_UNPLUGGED;
  3565. if(ReadFileEx(this->hdev, this->hriIn.pvReport,
  3566. this->hriIn.cbReport, &this->o, CHid_DummyCompletion))
  3567. {
  3568. do
  3569. {
  3570. SleepEx( INFINITE, TRUE);
  3571. } while(!HasOverlappedIoCompleted(&this->o));
  3572. if(this->hriIn.cbReport == this->o.InternalHigh)
  3573. {
  3574. NTSTATUS stat;
  3575. CopyMemory(this->pvStage, this->pvPhys, this->cbPhys);
  3576. stat = CHid_ParseData(this, HidP_Input, &this->hriIn);
  3577. if(SUCCEEDED(stat))
  3578. {
  3579. CEm_AddState(&this->ed, this->pvStage, GetTickCount());
  3580. this->pvi->fl &= ~VIFL_UNPLUGGED;
  3581. hres = S_OK;
  3582. } else
  3583. {
  3584. RPF( "CHid_ParseData failed in Poll, status = 0x%08x", stat );
  3585. hres = stat;
  3586. }
  3587. }
  3588. }
  3589. if( FAILED(hres) )
  3590. {
  3591. /*
  3592. * Note, we have not broken the read loop so there is no need to
  3593. * force the device unaquired (though dinput.dll does).
  3594. *
  3595. * If this causes problems revert to the old behavior.
  3596. * CEm_ForceDeviceUnacquire(pemFromPvi(this->pvi)->ped, 0x0);
  3597. */
  3598. hres = DIERR_UNPLUGGED;
  3599. this->pvi->fl |= VIFL_UNPLUGGED;
  3600. }
  3601. }
  3602. if( this->hriFea.cbReport )
  3603. {
  3604. UINT uReport;
  3605. /*
  3606. * We should never get here unless there really are any
  3607. * features that need to be polled.
  3608. */
  3609. AssertF(this->hriFea.cbReport);
  3610. AssertF(this->hriFea.pvReport);
  3611. /*
  3612. * Read the new features and parse/process them.
  3613. *
  3614. * Notice that we read the features into the same buffer
  3615. * that we log them into. That's okay; the "live" parts
  3616. * of the two buffers never actually overlap.
  3617. */
  3618. for( uReport = 0x0; uReport < this->wMaxReportId[HidP_Feature]; uReport++ )
  3619. {
  3620. if( *(this->pEnableReportId[HidP_Feature] + uReport ) == TRUE )
  3621. {
  3622. *((UCHAR*)(this->hriFea.pvReport)) = (UCHAR)uReport;
  3623. /*
  3624. * Wipe out all the old goo because we're taking over.
  3625. */
  3626. CHid_ResetDeviceData(this, &this->hriFea, HidP_Feature);
  3627. if(HidD_GetFeature(this->hdev, this->hriFea.pvReport,
  3628. this->hriFea.cbReport))
  3629. {
  3630. NTSTATUS stat;
  3631. stat = CHid_ParseData(this, HidP_Feature, &this->hriFea);
  3632. AssertF(SUCCEEDED(stat));
  3633. if(SUCCEEDED(stat))
  3634. {
  3635. CEm_AddState(&this->ed, this->pvStage, GetTickCount());
  3636. }
  3637. hres = stat;
  3638. } else
  3639. {
  3640. RPF("CHid_Poll: Unable to read HID features (ReportID%d) LastError(0x%x)", uReport, GetLastError() );
  3641. hres = hresLe(GetLastError());
  3642. }
  3643. }
  3644. }
  3645. }
  3646. ExitOleProcR();
  3647. return hres;
  3648. }
  3649. /*****************************************************************************
  3650. *
  3651. * CHid_New (constructor)
  3652. *
  3653. * Fail the create if we can't open the device.
  3654. *
  3655. *****************************************************************************/
  3656. STDMETHODIMP
  3657. CHid_New(PUNK punkOuter, REFGUID rguid, RIID riid, PPV ppvObj)
  3658. {
  3659. HRESULT hres;
  3660. EnterProcI(IDirectInputDeviceCallback::Hid::<constructor>,
  3661. (_ "Gp", riid, ppvObj));
  3662. hres = Common_NewRiid(CHid, punkOuter, riid, ppvObj);
  3663. if(SUCCEEDED(hres))
  3664. {
  3665. /* Must use _thisPv in case of aggregation */
  3666. PCHID this = _thisPv(*ppvObj);
  3667. if(SUCCEEDED(hres = CHid_Init(this, rguid)))
  3668. {
  3669. } else
  3670. {
  3671. Invoke_Release(ppvObj);
  3672. }
  3673. }
  3674. ExitOleProcPpvR(ppvObj);
  3675. return hres;
  3676. }
  3677. /*****************************************************************************
  3678. *
  3679. * @doc INTERNAL
  3680. *
  3681. * @method HRESULT | CHid | SetDIData |
  3682. *
  3683. * Set DirectInput version and apphack data from CDIDev *.
  3684. *
  3685. * @parm DWORD | dwVer |
  3686. *
  3687. * DirectInput version
  3688. *
  3689. * @parm LPVOID | lpdihacks |
  3690. *
  3691. * AppHack data
  3692. *
  3693. * @returns
  3694. *
  3695. * <c E_NOTIMPL> because we don't support usages.
  3696. *
  3697. *****************************************************************************/
  3698. STDMETHODIMP
  3699. CHid_SetDIData(PDICB pdcb, DWORD dwVer, LPVOID lpdihacks)
  3700. {
  3701. HRESULT hres;
  3702. PCHID this;
  3703. EnterProcI(IDirectInputDeviceCallback::Hid::SetDIData,
  3704. (_ "pup", pdcb, dwVer, lpdihacks));
  3705. /*
  3706. * This is an internal interface, so we can skimp on validation.
  3707. */
  3708. this = _thisPvNm(pdcb, dcb);
  3709. this->dwVersion = dwVer;
  3710. CopyMemory(&this->diHacks, (LPDIAPPHACKS)lpdihacks, sizeof(this->diHacks));
  3711. hres = S_OK;
  3712. ExitOleProcR();
  3713. return hres;
  3714. }
  3715. /*****************************************************************************
  3716. *
  3717. * @doc INTERNAL
  3718. *
  3719. * @method HRESULT | CHid | BuildDefaultActionMap |
  3720. *
  3721. * Generate default mappings for the objects on this device.
  3722. *
  3723. * @parm LPDIACTIONFORMATW | pActionFormat |
  3724. *
  3725. * Actions to map.
  3726. *
  3727. * @parm DWORD | dwFlags |
  3728. *
  3729. * Flags used to indicate mapping preferences.
  3730. *
  3731. * @parm REFGUID | guidInst |
  3732. *
  3733. * Device instance GUID.
  3734. *
  3735. * @returns
  3736. *
  3737. * <c E_NOTIMPL>
  3738. *
  3739. *****************************************************************************/
  3740. STDMETHODIMP
  3741. CHid_BuildDefaultActionMap
  3742. (
  3743. PDICB pdcb,
  3744. LPDIACTIONFORMATW paf,
  3745. DWORD dwFlags,
  3746. REFGUID guidInst
  3747. )
  3748. {
  3749. HRESULT hres;
  3750. PCHID this;
  3751. DWORD dwPhysicalGenre;
  3752. /*
  3753. * This is an internal interface, so we can skimp on validation.
  3754. */
  3755. EnterProcI(IDirectInputDeviceCallback::Hid::BuildDefaultActionMap,
  3756. (_ "ppxG", pdcb, paf, dwFlags, guidInst));
  3757. this = _thisPvNm(pdcb, dcb);
  3758. switch( GET_DIDEVICE_TYPE( this->dwDevType ) )
  3759. {
  3760. case DI8DEVTYPE_DEVICE:
  3761. hres = S_FALSE;
  3762. goto ExitBuildDefaultActionMap;
  3763. case DI8DEVTYPE_MOUSE:
  3764. dwPhysicalGenre = DIPHYSICAL_MOUSE;
  3765. break;
  3766. case DI8DEVTYPE_KEYBOARD:
  3767. dwPhysicalGenre = DIPHYSICAL_KEYBOARD;
  3768. break;
  3769. default:
  3770. dwPhysicalGenre = 0;
  3771. break;
  3772. }
  3773. if( dwPhysicalGenre )
  3774. {
  3775. hres = CMap_BuildDefaultSysActionMap( paf, dwFlags, dwPhysicalGenre,
  3776. guidInst, &this->df, 0 /* HID mice buttons start at instance zero */ );
  3777. }
  3778. else
  3779. {
  3780. PDIDOBJDEFSEM rgObjSem;
  3781. if( SUCCEEDED( hres = AllocCbPpv(cbCxX(
  3782. (this->dwAxes + this->dwPOVs + this->dwButtons ), DIDOBJDEFSEM),
  3783. &rgObjSem) ) )
  3784. {
  3785. PDIDOBJDEFSEM pAxis;
  3786. PDIDOBJDEFSEM pPOV;
  3787. PDIDOBJDEFSEM pButton;
  3788. BYTE rgbIndex[DISEM_FLAGS_GET(DISEM_FLAGS_S)];
  3789. UINT ObjIdx;
  3790. pAxis = rgObjSem;
  3791. pPOV = &pAxis[this->dwAxes];
  3792. pButton = &pPOV[this->dwPOVs];
  3793. ZeroMemory( rgbIndex, cbX(rgbIndex) );
  3794. for( ObjIdx = 0; ObjIdx < this->df.dwNumObjs; ObjIdx++ )
  3795. {
  3796. if( this->df.rgodf[ObjIdx].dwType & DIDFT_NODATA )
  3797. {
  3798. continue;
  3799. }
  3800. if( this->df.rgodf[ObjIdx].dwType & DIDFT_AXIS )
  3801. {
  3802. PHIDGROUPCAPS pcaps;
  3803. pcaps = this->rghoc[ObjIdx].pcaps;
  3804. pAxis->dwID = this->df.rgodf[ObjIdx].dwType;
  3805. if( this->rgbaxissemflags[DIDFT_GETINSTANCE( pAxis->dwID )] )
  3806. {
  3807. pAxis->dwSemantic = DISEM_TYPE_AXIS | DISEM_FLAGS_SET ( this->rgbaxissemflags[DIDFT_GETINSTANCE( pAxis->dwID )] );
  3808. /*
  3809. * The index is zero so that a real index can be ORed in.
  3810. * Also, assert that the rgbIndex is big enough and
  3811. * that subtracting 1 won't give a negative index!
  3812. */
  3813. AssertF( DISEM_INDEX_GET(pAxis->dwSemantic) == 0 );
  3814. AssertF( DISEM_FLAGS_GET(pAxis->dwSemantic) > 0 );
  3815. AssertF( DISEM_FLAGS_GET(pAxis->dwSemantic) <= DISEM_FLAGS_GET(DISEM_FLAGS_S) );
  3816. CAssertF( DISEM_FLAGS_GET(DISEM_FLAGS_X) == 1 );
  3817. pAxis->dwSemantic |= DISEM_INDEX_SET( rgbIndex[DISEM_FLAGS_GET(pAxis->dwSemantic)-1]++ );
  3818. }
  3819. else
  3820. {
  3821. /*
  3822. * If the axis has no semantic flags, it is
  3823. * unrecognized so short cut the above to produce
  3824. * a plain axis that can be matched only with "ANY".
  3825. */
  3826. pAxis->dwSemantic = DISEM_TYPE_AXIS;
  3827. }
  3828. if( !pcaps->IsAbsolute )
  3829. {
  3830. pAxis->dwSemantic |= DIAXIS_RELATIVE;
  3831. }
  3832. pAxis++;
  3833. }
  3834. else if( this->df.rgodf[ObjIdx].dwType & DIDFT_POV )
  3835. {
  3836. pPOV->dwID = this->df.rgodf[ObjIdx].dwType;
  3837. pPOV->dwSemantic = DISEM_TYPE_POV;
  3838. pPOV++;
  3839. }
  3840. else if( this->df.rgodf[ObjIdx].dwType & DIDFT_BUTTON )
  3841. {
  3842. pButton->dwID = this->df.rgodf[ObjIdx].dwType;
  3843. pButton->dwSemantic = DISEM_TYPE_BUTTON;
  3844. pButton++;
  3845. }
  3846. }
  3847. AssertF( pAxis == &rgObjSem[this->dwAxes] );
  3848. AssertF( pPOV == &rgObjSem[this->dwAxes + this->dwPOVs] );
  3849. AssertF( pButton == &rgObjSem[this->dwAxes + this->dwPOVs + this->dwButtons] );
  3850. hres = CMap_BuildDefaultDevActionMap( paf, dwFlags, guidInst, rgObjSem,
  3851. this->dwAxes, this->dwPOVs, this->dwButtons );
  3852. FreePv( rgObjSem );
  3853. }
  3854. }
  3855. ExitBuildDefaultActionMap:;
  3856. ExitOleProcR();
  3857. return hres;
  3858. }
  3859. /*****************************************************************************
  3860. *
  3861. * The long-awaited vtbls and templates
  3862. *
  3863. *****************************************************************************/
  3864. #pragma BEGIN_CONST_DATA
  3865. #define CHid_Signature 0x20444948 /* "HID " */
  3866. Primary_Interface_Begin(CHid, IDirectInputDeviceCallback)
  3867. CHid_GetInstance,
  3868. CDefDcb_GetVersions,
  3869. CHid_GetDataFormat,
  3870. CHid_GetObjectInfo,
  3871. CHid_GetCapabilities,
  3872. CHid_Acquire,
  3873. CHid_Unacquire,
  3874. CHid_GetDeviceState,
  3875. CHid_GetDeviceInfo,
  3876. CHid_GetProperty,
  3877. CHid_SetProperty,
  3878. CDefDcb_SetEventNotification,
  3879. #ifdef WINNT
  3880. CHid_SetCooperativeLevel,
  3881. #else
  3882. CDefDcb_SetCooperativeLevel,
  3883. #endif
  3884. CHid_RunControlPanel,
  3885. CDefDcb_CookDeviceData,
  3886. CHid_CreateEffect,
  3887. CHid_GetFFConfigKey,
  3888. CHid_SendDeviceData,
  3889. CHid_Poll,
  3890. CHid_GetUsage,
  3891. CHid_MapUsage,
  3892. CHid_SetDIData,
  3893. CHid_BuildDefaultActionMap,
  3894. Primary_Interface_End(CHid, IDirectInputDeviceCallback)