Leaked source code of windows server 2003
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.

4487 lines
139 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, cA( 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. lstrcpyW(pdipstr->wsz, dijti.wszDisplayName);
  1538. SquirtSqflPtszV(sqflHid | sqflVerbose,
  1539. TEXT("Got name from type info %s"), pdipstr->wsz );
  1540. }
  1541. #ifdef WINNT
  1542. else if( fHasSpecificHardwareMatch( this->ptszId )
  1543. && SUCCEEDED( hres = DIHid_GetParentRegistryProperty(this->ptszId, SPDRP_DEVICEDESC, pdiph, 0x0 ) ) )
  1544. {
  1545. fOverwriteDeviceName = TRUE;
  1546. SquirtSqflPtszV(sqflHid | sqflVerbose,
  1547. TEXT("Got device description %s"), pdipstr->wsz );
  1548. }
  1549. #endif
  1550. else
  1551. {
  1552. if( fGetProductStringFromDevice( this, pdipstr->wsz, cbX( pdipstr->wsz ) ) )
  1553. {
  1554. fOverwriteDeviceName = TRUE;
  1555. SquirtSqflPtszV(sqflHid | sqflVerbose,
  1556. TEXT("Got description %s from device"), pdipstr->wsz );
  1557. }
  1558. else
  1559. {
  1560. /*
  1561. * Just make up a name from the caps
  1562. */
  1563. CType_MakeGameCtrlName( pdipstr->wsz,
  1564. this->dwDevType, this->dwAxes, this->dwButtons, this->dwPOVs );
  1565. fOverwriteDeviceName = TRUE;
  1566. SquirtSqflPtszV(sqflHid | sqflVerbose,
  1567. TEXT("Made up name %s"), pdipstr->wsz );
  1568. }
  1569. hres = S_OK;
  1570. }
  1571. if( fOverwriteDeviceName ) {
  1572. /*
  1573. * If we have a better name, overwrite the old one with this better one.
  1574. * See manbug 46438.
  1575. */
  1576. AssertF(this->hkType);
  1577. AssertF(pdipstr->wsz[0]);
  1578. hres = JoyReg_SetValue(this->hkType,
  1579. REGSTR_VAL_JOYOEMNAME, REG_SZ,
  1580. pdipstr->wsz,
  1581. cbX(pdipstr->wsz));
  1582. if( FAILED(hres) ){
  1583. SquirtSqflPtszV(sqflHid | sqflVerbose,
  1584. TEXT("Unable to overwrite generic device name with %s"), pdipstr->wsz );
  1585. // This failure (unlikely) doesn't matter.
  1586. hres = S_OK;
  1587. }
  1588. }
  1589. }
  1590. }
  1591. if( SUCCEEDED(hres)
  1592. && ( this->diHacks.nMaxDeviceNameLength < MAX_PATH )
  1593. && ( this->diHacks.nMaxDeviceNameLength < lstrlenW(pdipstr->wsz) ) )
  1594. {
  1595. pdipstr->wsz[this->diHacks.nMaxDeviceNameLength] = L'\0';
  1596. }
  1597. break;
  1598. }
  1599. case (DWORD)(UINT_PTR)DIPROP_JOYSTICKID:
  1600. if( ( GET_DIDEVICE_TYPE( this->dwDevType ) >= DI8DEVTYPE_GAMEMIN )
  1601. && ( this->dwDevType & DIDEVTYPE_HID ) )
  1602. {
  1603. LPDIPROPDWORD pdipdw = (PV)pdiph;
  1604. pdipdw->dwData = this->idJoy;
  1605. hres = S_OK;
  1606. } else
  1607. {
  1608. hres = E_NOTIMPL;
  1609. }
  1610. break;
  1611. case (DWORD)(UINT_PTR)DIPROP_GETPORTDISPLAYNAME:
  1612. #ifdef WINNT
  1613. /* For HID devices Port Display Name is the grand parent name */
  1614. hres = DIHid_GetParentRegistryProperty(this->ptszId, SPDRP_FRIENDLYNAME, pdiph, TRUE);
  1615. if( FAILED(hres) )
  1616. {
  1617. /* Maybe we can use the Product Name */
  1618. hres = DIHid_GetParentRegistryProperty(this->ptszId, SPDRP_DEVICEDESC, pdiph, TRUE);
  1619. if( SUCCEEDED(hres) )
  1620. {
  1621. /* We only sort of succeeded */
  1622. hres = S_FALSE;
  1623. }
  1624. }
  1625. if( SUCCEEDED(hres)
  1626. && ( this->diHacks.nMaxDeviceNameLength < MAX_PATH ) )
  1627. {
  1628. LPDIPROPSTRING pdipstr = (PV)pdiph;
  1629. if( this->diHacks.nMaxDeviceNameLength < lstrlenW(pdipstr->wsz) )
  1630. {
  1631. pdipstr->wsz[this->diHacks.nMaxDeviceNameLength] = L'\0';
  1632. }
  1633. }
  1634. #else
  1635. // Not sure how this works on Win9x
  1636. hres = E_NOTIMPL;
  1637. #endif
  1638. break;
  1639. case (DWORD)(UINT_PTR)(DIPROP_ENABLEREPORTID):
  1640. hres = E_NOTIMPL;
  1641. break;
  1642. case (DWORD)(UINT_PTR)DIPROP_VIDPID:
  1643. {
  1644. LPDIPROPDWORD pdipdw = (PV)pdiph;
  1645. /* Assert that a DWORD copy is all that is needed */
  1646. CAssertF( FIELD_OFFSET( CHID, VendorID ) + cbX( this->VendorID )
  1647. == FIELD_OFFSET( CHID, ProductID ) );
  1648. pdipdw->dwData = *((PDWORD)(&this->VendorID));
  1649. hres = S_OK;
  1650. }
  1651. break;
  1652. case (DWORD)(UINT_PTR)(DIPROP_MAPFILE):
  1653. if( ( this->dwDevType == DI8DEVTYPE_MOUSE )
  1654. || ( this->dwDevType == DI8DEVTYPE_KEYBOARD ) )
  1655. {
  1656. hres = E_NOTIMPL;
  1657. }
  1658. else
  1659. {
  1660. LPDIPROPSTRING pdipstr = (PV)pdiph;
  1661. LONG lRes;
  1662. DWORD dwBufferSize = cbX(pdipstr->wsz);
  1663. lRes = RegQueryStringValueW( this->hkProp, REGSTR_VAL_JOYOEMMAPFILE, pdipstr->wsz, &dwBufferSize );
  1664. hres = ( pdipstr->wsz[0] && ( lRes == ERROR_SUCCESS ) ) ? S_OK : DIERR_OBJECTNOTFOUND;
  1665. #ifdef WINNT
  1666. if (SUCCEEDED(hres))
  1667. {
  1668. // there is a file name in the registry
  1669. #define MAP_INI_FILEPATH L"\\DIRECTX\\DINPUT"
  1670. WCHAR wszMapINI[MAX_PATH+1];
  1671. UINT uLen;
  1672. WCHAR wszDrive[_MAX_DRIVE];
  1673. WCHAR wszFullPath[MAX_PATH];
  1674. LPWSTR pFilename = NULL;
  1675. // construct the directory path to the INIs
  1676. ZeroMemory(wszMapINI, cbX(wszMapINI));
  1677. uLen = GetSystemDirectoryW( wszMapINI, cA( wszMapINI ) );
  1678. if( uLen < cA(wszMapINI) - cA(MAP_INI_FILEPATH) )
  1679. {
  1680. memcpy( (PBYTE)&wszMapINI[uLen], (PBYTE)MAP_INI_FILEPATH, cbX( MAP_INI_FILEPATH ) );
  1681. }
  1682. // Valmel: ISSUE 2001/03/22: note that we do not handle paths that
  1683. // start with "%windir%" or "%SystemRoot%"; this is because dimap.dll doesn't
  1684. // handle them correctly either
  1685. wszDrive[0]=0;
  1686. ZeroMemory(wszFullPath, cbX(wszFullPath));
  1687. _wsplitpath(pdipstr->wsz,wszDrive,NULL,NULL,NULL);
  1688. if (wszDrive[0] == 0)
  1689. {
  1690. // relative path -- attach directory path to the front
  1691. _snwprintf(wszFullPath, MAX_PATH, TEXT("%s\\%s"), wszMapINI, pdipstr->wsz);
  1692. }
  1693. else
  1694. {
  1695. // absolute path -- copy it
  1696. lstrcpynW(wszFullPath, pdipstr->wsz, MAX_PATH);
  1697. }
  1698. // take care of ".." and ".",
  1699. // and check that our path starts w/ the correct directory path
  1700. uLen = GetFullPathNameW(wszFullPath, cA(pdipstr->wsz), pdipstr->wsz, &pFilename);
  1701. if ((uLen > cA(pdipstr->wsz)) || (_wcsnicmp(pdipstr->wsz, wszMapINI, lstrlenW(wszMapINI)) != 0))
  1702. {
  1703. // either the path is longer than the space we have for it, or
  1704. // it doesn't start w/ the correct directory path;
  1705. // return a "not found" error different from the one we return if there's no registry entry
  1706. ZeroMemory(pdipstr->wsz, cbX(pdipstr->wsz));
  1707. hres = DIERR_NOTFOUND;
  1708. }
  1709. #undef MAP_INI_FILEPATH
  1710. }
  1711. #endif
  1712. }
  1713. break;
  1714. case (DWORD)(UINT_PTR)(DIPROP_TYPENAME):
  1715. if( ( this->dwDevType == DI8DEVTYPE_MOUSE )
  1716. || ( this->dwDevType == DI8DEVTYPE_KEYBOARD ) )
  1717. {
  1718. hres = E_NOTIMPL;
  1719. }
  1720. else
  1721. {
  1722. LPDIPROPSTRING pdipstr = (PV)pdiph;
  1723. if( ( this->VendorID == MSFT_SYSTEM_VID )
  1724. &&( ( this->ProductID >= MSFT_SYSTEM_PID + JOY_HW_PREDEFMIN )
  1725. &&( this->ProductID < MSFT_SYSTEM_PID + JOY_HW_PREDEFMAX ) ) )
  1726. {
  1727. pdipstr->wsz[0] = L'#';
  1728. pdipstr->wsz[1] = L'0' + (WCHAR)(this->ProductID-MSFT_SYSTEM_PID);
  1729. pdipstr->wsz[2] = L'\0';
  1730. }
  1731. else
  1732. {
  1733. #ifndef UNICODE
  1734. TCHAR tszType[cbszVIDPID];
  1735. wsprintf(tszType, VID_PID_TEMPLATE, this->VendorID, this->ProductID);
  1736. TToU( pdipstr->wsz, cA(pdipstr->wsz), tszType );
  1737. #else
  1738. wsprintf(pdipstr->wsz, VID_PID_TEMPLATE, this->VendorID, this->ProductID);
  1739. #endif
  1740. }
  1741. hres = S_OK;
  1742. }
  1743. break;
  1744. default:
  1745. SquirtSqflPtszV(sqflHid | sqflBenign ,
  1746. TEXT("CHid_GetProperty(iobj=0xFFFFFFFF): E_NOTIMPL on guid: %08x"),
  1747. ppropi->pguid);
  1748. hres = E_NOTIMPL;
  1749. break;
  1750. }
  1751. } else
  1752. {
  1753. SquirtSqflPtszV(sqflHidDev | sqflError,
  1754. TEXT("CHid_GetProperty(iobj=%08x): E_NOTIMPL on guid: %08x"),
  1755. ppropi->iobj, ppropi->pguid);
  1756. hres = E_NOTIMPL;
  1757. }
  1758. ExitOleProcR();
  1759. return hres;
  1760. }
  1761. /*****************************************************************************
  1762. *
  1763. * @doc INTERNAL
  1764. *
  1765. * @func LONG | CHid_CoordinateTransform |
  1766. *
  1767. * Convert numbers from logical to physical or vice versa.
  1768. *
  1769. * If either the To or From values look suspicious, then
  1770. * ignore them and leave the values alone.
  1771. *
  1772. * @parm PLMINMAX | Dst |
  1773. *
  1774. * Destination min/max information.
  1775. *
  1776. * @parm PLMINMAX | Src |
  1777. *
  1778. * Source min/max information.
  1779. *
  1780. * @parm LONG | lVal |
  1781. *
  1782. * Source value to be converted.
  1783. *
  1784. * @returns
  1785. *
  1786. * The destination value after conversion.
  1787. *
  1788. *****************************************************************************/
  1789. LONG EXTERNAL
  1790. CHid_CoordinateTransform(PLMINMAX Dst, PLMINMAX Src, LONG lVal)
  1791. {
  1792. /*
  1793. * Note that the sanity check is symmetric in Src and Dst.
  1794. * This is important, so that we never get into a weird
  1795. * case where we can convert one way but can't convert back.
  1796. */
  1797. if(Dst->Min < Dst->Max && Src->Min < Src->Max)
  1798. {
  1799. /*
  1800. * We need to perform a straight linear interpolation.
  1801. * The math comes out like this:
  1802. *
  1803. * x - x0 y - y0
  1804. * ------- = -------
  1805. * x1 - x0 y1 - y0
  1806. *
  1807. * If you now do a "solve for y", you get
  1808. *
  1809. *
  1810. * y1 - y0
  1811. * y = (x - x0) ------- + y0
  1812. * x1 - x0
  1813. *
  1814. * where "x" is Src, "y" is Dst, 0 is Min, and 1 is Max.
  1815. *
  1816. *
  1817. */
  1818. lVal = MulDiv(lVal - Src->Min, Dst->Max - Dst->Min,
  1819. Src->Max - Src->Min) + Dst->Min;
  1820. }
  1821. return lVal;
  1822. }
  1823. #ifndef WINNT
  1824. /*****************************************************************************
  1825. *
  1826. * @doc INTERNAL
  1827. *
  1828. * @method int | CHid | IsMatchingJoyDevice |
  1829. *
  1830. * Does the cached joystick ID match us?
  1831. *
  1832. * @parm OUT PVXDINITPARMS | pvip |
  1833. *
  1834. * On success, contains parameter values.
  1835. *
  1836. * @returns
  1837. *
  1838. * Nonzero on success.
  1839. *
  1840. *****************************************************************************/
  1841. BOOL INTERNAL
  1842. CHid_IsMatchingJoyDevice(PCHID this, PVXDINITPARMS pvip)
  1843. {
  1844. CHAR sz[MAX_PATH];
  1845. LPSTR pszPath;
  1846. BOOL fRc;
  1847. pszPath = JoyReg_JoyIdToDeviceInterface_95(this->idJoy, pvip, sz);
  1848. if(pszPath)
  1849. {
  1850. SquirtSqflPtszV(sqfl | sqflTrace,
  1851. TEXT("CHid_IsMatchingJoyDevice: %d -> %s"),
  1852. this->idJoy, pszPath);
  1853. #ifdef UNICODE
  1854. {
  1855. CHAR szpath[MAX_PATH];
  1856. UToA( szpath, cA(szpath), (LPWSTR)this->ptszPath);
  1857. fRc = ( lstrcmpiA(pszPath, szpath) == 0x0 );
  1858. }
  1859. #else
  1860. fRc = ( lstrcmpiA(pszPath, (PCHAR)this->ptszPath) == 0x0 );
  1861. #endif
  1862. } else
  1863. {
  1864. fRc = FALSE;
  1865. }
  1866. return fRc;
  1867. }
  1868. /*****************************************************************************
  1869. *
  1870. * @doc INTERNAL
  1871. *
  1872. * @method void | CHid | FindJoyDevice |
  1873. *
  1874. * Look for the VJOYD device that matches us, if any.
  1875. *
  1876. * On return, the <e CHID.idJoy> field contains the
  1877. * matching joystick number, or -1 if not found.
  1878. *
  1879. * @parm OUT PVXDINITPARMS | pvip |
  1880. *
  1881. * On success, contains parameter values.
  1882. *
  1883. *****************************************************************************/
  1884. void INTERNAL
  1885. CHid_FindJoyDevice(PCHID this, PVXDINITPARMS pvip)
  1886. {
  1887. /*
  1888. * If we have a cached value, and it still works, then
  1889. * our job is done.
  1890. */
  1891. if(this->idJoy >= 0 &&
  1892. CHid_IsMatchingJoyDevice(this, pvip))
  1893. {
  1894. } else
  1895. {
  1896. /*
  1897. * Need to keep looking. (Or start looking.)
  1898. *
  1899. * A countdown loop is nicer, but for efficiency, we count
  1900. * upwards, since the joystick we want tends to be near the
  1901. * beginning.
  1902. */
  1903. for(this->idJoy = 0; this->idJoy < cJoyMax; this->idJoy++)
  1904. {
  1905. if(CHid_IsMatchingJoyDevice(this, pvip))
  1906. {
  1907. goto done;
  1908. }
  1909. }
  1910. this->idJoy = -1;
  1911. }
  1912. done:;
  1913. }
  1914. #endif
  1915. /*****************************************************************************
  1916. *
  1917. * @doc INTERNAL
  1918. *
  1919. * @method int | CHid | MapAxis |
  1920. *
  1921. * Find VJOYD axis from HID axis, if one.
  1922. *
  1923. * @parm PVXDINITPARMS | pvip |
  1924. *
  1925. * Parameter values that let us known which axes VJOYD
  1926. * has mapped to which HID Axes.
  1927. *
  1928. * @parm UINT | iobj |
  1929. *
  1930. * Object index of the object whose axis value changed.
  1931. *
  1932. * @returns
  1933. *
  1934. * The VJOYD axis number that changed (0 to 5), or -1
  1935. * if there is no matching axis. There will be no matching
  1936. * axis if, for example, the device has something that is
  1937. * not expressible via VJOYD (e.g., a temperature sensor).
  1938. *
  1939. *****************************************************************************/
  1940. int INTERNAL
  1941. CHid_MapAxis(PCHID this, PVXDINITPARMS pvip, UINT iobj)
  1942. {
  1943. int iAxis;
  1944. DWORD dwUsage;
  1945. AssertF(this->dcb.lpVtbl->GetUsage == CHid_GetUsage);
  1946. dwUsage = CHid_GetUsage(&this->dcb, (int)iobj);
  1947. if(dwUsage)
  1948. {
  1949. /*
  1950. * A countdown loop lets us fall out with the correct failure
  1951. * code (namely, -1).
  1952. */
  1953. iAxis = cJoyPosAxisMax;
  1954. while(--iAxis >= 0)
  1955. {
  1956. if(pvip->Usages[iAxis] == dwUsage)
  1957. {
  1958. break;
  1959. }
  1960. }
  1961. } else
  1962. {
  1963. /*
  1964. * Eek! No usage information for the axis. Then it certainly
  1965. * isn't a VJOYD axis.
  1966. */
  1967. iAxis = -1;
  1968. }
  1969. return iAxis;
  1970. }
  1971. #ifndef WINNT
  1972. /*****************************************************************************
  1973. *
  1974. * @doc INTERNAL
  1975. *
  1976. * @method void | CHid | UpdateVjoydCalibration |
  1977. *
  1978. * Somebody changed the calibration on a single axis. If we
  1979. * are shadowing a joystick, then look for the VJOYD alias of
  1980. * our device and update its registry settings, too.
  1981. *
  1982. *
  1983. * @parm UINT | iobj |
  1984. *
  1985. * Object index of the object whose calibration changed.
  1986. *
  1987. *****************************************************************************/
  1988. void EXTERNAL
  1989. CHid_UpdateVjoydCalibration(PCHID this, UINT iobj)
  1990. {
  1991. HRESULT hres;
  1992. int iAxis;
  1993. VXDINITPARMS vip;
  1994. DIJOYCONFIG cfg;
  1995. PHIDGROUPCAPS pcaps;
  1996. PJOYRANGECONVERT pjrc;
  1997. AssertF(iobj < this->df.dwNumObjs);
  1998. /*
  1999. * Proceed if...
  2000. *
  2001. * - We can find the VJOYD device we correspond to.
  2002. * - We can find the axis that got updated.
  2003. * - The indicated axis has capability information.
  2004. * - The indicated axis has calibration information.
  2005. * - We can read the old calibration information.
  2006. */
  2007. CHid_FindJoyDevice(this, &vip);
  2008. if(this->idJoy >= 0 &&
  2009. (iAxis = CHid_MapAxis(this, &vip, iobj)) >= 0 &&
  2010. (pcaps = this->rghoc[iobj].pcaps) != NULL &&
  2011. (pjrc = this->rghoc[iobj].pjrc) != NULL &&
  2012. SUCCEEDED(hres = JoyReg_GetConfig(this->idJoy, &cfg,
  2013. DIJC_REGHWCONFIGTYPE)))
  2014. {
  2015. PLMINMAX Dst = &pcaps->Physical;
  2016. PLMINMAX Src = &pcaps->Logical;
  2017. AssertF(iAxis < cJoyPosAxisMax);
  2018. #define JoyPosValue(phwc, f, i) \
  2019. *(LPDWORD)pvAddPvCb(&(phwc)->hwv.jrvHardware.f, \
  2020. ibJoyPosAxisFromPosAxis(i))
  2021. /*
  2022. * We use logical coordinates, but VJOYD wants physical
  2023. * coordinates, so do the conversion while we copy the
  2024. * values.
  2025. */
  2026. #define ConvertValue(f1, f2) \
  2027. JoyPosValue(&cfg.hwc, f1, iAxis) = \
  2028. CHid_CoordinateTransform(Dst, Src, pjrc->f2) \
  2029. ConvertValue(jpMin , dwPmin);
  2030. ConvertValue(jpMax , dwPmax);
  2031. ConvertValue(jpCenter, dwPc );
  2032. #undef ConvertValue
  2033. #undef JoyPosValue
  2034. /*
  2035. * Notice that we do *not* pass the DIJC_UPDATEALIAS flag
  2036. * because WE ARE THE ALIAS! If we had passed the flag,
  2037. * then JoyReg would create us and attempt to update our
  2038. * calibration which we don't want it to do because the
  2039. * whole thing was our idea in the first place.
  2040. */
  2041. hres = JoyReg_SetConfig(this->idJoy, &cfg.hwc, &cfg,
  2042. DIJC_REGHWCONFIGTYPE);
  2043. }
  2044. }
  2045. /*****************************************************************************
  2046. *
  2047. * @doc INTERNAL
  2048. *
  2049. * @method void | CHid | UpdateCalibrationFromVjoyd |
  2050. *
  2051. * This function is only for Win9x. Joy.cpl uses winmm (through vjoyd)
  2052. * to calibrate the device, and save calibration information directly into
  2053. * registry without notifying HID. ANother issue is: vjoyd only use unsigned
  2054. * data (physical data), while HID also use signed data. When we read
  2055. * calibration information from VJOYD, we need do conversion.
  2056. *
  2057. * @parm UINT | iobj |
  2058. *
  2059. * Object index of the object whose calibration changed.
  2060. *
  2061. *****************************************************************************/
  2062. void EXTERNAL
  2063. CHid_UpdateCalibrationFromVjoyd(PCHID this, UINT iobj, LPDIOBJECTCALIBRATION pCal)
  2064. {
  2065. HRESULT hres;
  2066. int iAxis;
  2067. VXDINITPARMS vip;
  2068. DIJOYCONFIG cfg;
  2069. PHIDGROUPCAPS pcaps;
  2070. PJOYRANGECONVERT pjrc;
  2071. AssertF(iobj < this->df.dwNumObjs);
  2072. /*
  2073. * Proceed if...
  2074. *
  2075. * - We can find the VJOYD device we correspond to.
  2076. * - We can find the axis that got updated.
  2077. * - The indicated axis has capability information.
  2078. * - The indicated axis has calibration information.
  2079. * - We can read the calibration information.
  2080. */
  2081. CHid_FindJoyDevice(this, &vip);
  2082. if(this->idJoy >= 0 &&
  2083. (iAxis = CHid_MapAxis(this, &vip, iobj)) >= 0 &&
  2084. (pcaps = this->rghoc[iobj].pcaps) != NULL &&
  2085. (pjrc = this->rghoc[iobj].pjrc) != NULL &&
  2086. SUCCEEDED(hres = JoyReg_GetConfig(this->idJoy, &cfg,
  2087. DIJC_REGHWCONFIGTYPE)))
  2088. {
  2089. PLMINMAX Src = &pcaps->Physical;
  2090. PLMINMAX Dst = &pcaps->Logical;
  2091. AssertF(iAxis < cJoyPosAxisMax);
  2092. #define JoyPosValue(phwc, f, i) \
  2093. *(LPDWORD)pvAddPvCb(&(phwc)->hwv.jrvHardware.f, \
  2094. ibJoyPosAxisFromPosAxis(i))
  2095. /*
  2096. * We use logical coordinates, but VJOYD wants physical
  2097. * coordinates, so do the conversion while we copy the
  2098. * values.
  2099. */
  2100. #define ConvertValue(f1, f2) \
  2101. pCal->f2 = CHid_CoordinateTransform(Dst, Src, \
  2102. JoyPosValue(&cfg.hwc, f1, iAxis) )
  2103. ConvertValue(jpMin , lMin);
  2104. ConvertValue(jpMax , lMax);
  2105. ConvertValue(jpCenter, lCenter);
  2106. #undef ConvertValue
  2107. #undef JoyPosValue
  2108. }
  2109. }
  2110. #endif
  2111. #ifdef WINNT
  2112. /*****************************************************************************
  2113. *
  2114. * @doc INTERNAL
  2115. *
  2116. * @func HRESULT | DIHid_SetParentRegistryProperty |
  2117. *
  2118. * Wrapper around <f SetupDiSetDeviceRegistryProperty>
  2119. * that handles character set issues.
  2120. *
  2121. * @parm LPTSTR ptszId
  2122. *
  2123. * Device Instance ID.
  2124. *
  2125. * @parm DWORD | dwProperty |
  2126. *
  2127. * The property being queried.
  2128. *
  2129. * @parm LPCDIPROPHEADER | diph |
  2130. *
  2131. * Property data to be set.
  2132. *
  2133. *****************************************************************************/
  2134. HRESULT INTERNAL
  2135. DIHid_SetParentRegistryProperty(LPTSTR ptszId, DWORD dwProperty, LPCDIPROPHEADER pdiph)
  2136. {
  2137. HDEVINFO hdev;
  2138. TCHAR tsz[MAX_PATH];
  2139. LPDIPROPSTRING pstr = (PV)pdiph;
  2140. HRESULT hres = E_FAIL;
  2141. hdev = SetupDiCreateDeviceInfoList(NULL, NULL);
  2142. if(hdev != INVALID_HANDLE_VALUE)
  2143. {
  2144. SP_DEVINFO_DATA dinf;
  2145. ZeroX(tsz);
  2146. #ifdef UNICODE
  2147. lstrcpyW(tsz, pstr->wsz);
  2148. #else
  2149. UToA(tsz, cA(tsz), pstr->wsz);
  2150. #endif
  2151. dinf.cbSize = cbX(SP_DEVINFO_DATA);
  2152. if(SetupDiOpenDeviceInfo(hdev, ptszId, NULL, 0, &dinf))
  2153. {
  2154. CONFIGRET cr;
  2155. DEVINST DevInst;
  2156. if( (cr = CM_Get_Parent(&DevInst, dinf.DevInst, 0x0) ) == CR_SUCCESS )
  2157. {
  2158. CAssertF( SPDRP_DEVICEDESC +1 == CM_DRP_DEVICEDESC );
  2159. CAssertF( SPDRP_FRIENDLYNAME +1 == CM_DRP_FRIENDLYNAME );
  2160. if( ( cr = CM_Set_DevNode_Registry_Property(
  2161. DevInst,
  2162. dwProperty+1,
  2163. (LPBYTE)tsz,
  2164. MAX_PATH *cbX(TCHAR),
  2165. 0x0 ) ) == CR_SUCCESS )
  2166. {
  2167. hres = S_OK;
  2168. } else
  2169. {
  2170. SquirtSqflPtszV(sqfl | sqflVerbose,
  2171. TEXT("CM_Get_DevNode_Registry_Property FAILED") );
  2172. }
  2173. } else
  2174. {
  2175. SquirtSqflPtszV(sqfl | sqflVerbose,
  2176. TEXT("CM_Get_Parent FAILED") );
  2177. }
  2178. } else
  2179. {
  2180. SquirtSqflPtszV(sqfl | sqflVerbose,
  2181. TEXT("SetupDiOpenDeviceInfo FAILED, le = %d"), GetLastError() );
  2182. }
  2183. SetupDiDestroyDeviceInfoList(hdev);
  2184. } else
  2185. {
  2186. SquirtSqflPtszV(sqfl | sqflError,
  2187. TEXT("SetupDiCreateDeviceInfoList FAILED, le = %d"), GetLastError() );
  2188. }
  2189. return hres;
  2190. }
  2191. #else
  2192. /*****************************************************************************
  2193. *
  2194. * @doc INTERNAL
  2195. *
  2196. * @func HRESULT | DIHid_SetRegistryProperty |
  2197. *
  2198. * Wrapper around <f SetupDiSetDeviceRegistryProperty>
  2199. * that handles character set issues.
  2200. *
  2201. * @parm LPTSTR ptszId
  2202. *
  2203. * Device Instance ID.
  2204. *
  2205. * @parm DWORD | dwProperty |
  2206. *
  2207. * The property being queried.
  2208. *
  2209. * @parm LPCDIPROPHEADER | diph |
  2210. *
  2211. * Property data to be set.
  2212. *
  2213. *****************************************************************************/
  2214. HRESULT INTERNAL
  2215. DIHid_SetRegistryProperty(LPTSTR ptszId, DWORD dwProperty, LPCDIPROPHEADER pdiph)
  2216. {
  2217. HDEVINFO hdev;
  2218. TCHAR tsz[MAX_PATH];
  2219. LPDIPROPSTRING pstr = (PV)pdiph;
  2220. HRESULT hres;
  2221. hdev = SetupDiCreateDeviceInfoList(NULL, NULL);
  2222. if(hdev != INVALID_HANDLE_VALUE)
  2223. {
  2224. SP_DEVINFO_DATA dinf;
  2225. ZeroX(tsz);
  2226. #ifdef UNICODE
  2227. lstrcpyW(tsz, pstr->wsz);
  2228. #else
  2229. UToA(tsz, cA(tsz), pstr->wsz);
  2230. #endif
  2231. dinf.cbSize = cbX(SP_DEVINFO_DATA);
  2232. if(SetupDiOpenDeviceInfo(hdev, ptszId, NULL, 0, &dinf))
  2233. {
  2234. if(SetupDiSetDeviceRegistryProperty(hdev, &dinf, dwProperty,
  2235. (LPBYTE)tsz, MAX_PATH*cbX(TCHAR)) )
  2236. {
  2237. hres = S_OK;
  2238. } else
  2239. {
  2240. hres = E_FAIL;
  2241. }
  2242. } else
  2243. {
  2244. hres = E_FAIL;
  2245. }
  2246. SetupDiDestroyDeviceInfoList(hdev);
  2247. } else
  2248. {
  2249. hres = E_FAIL;
  2250. }
  2251. return hres;
  2252. }
  2253. #endif
  2254. /*****************************************************************************
  2255. *
  2256. * @doc INTERNAL
  2257. *
  2258. * @method HRESULT | CHid | SetAxisProperty |
  2259. *
  2260. * Set the appropriate axis property (or return E_NOTIMPL if the
  2261. * property is not an axis property).
  2262. * If the request is to set a property on the device,
  2263. * then convert it into separate requests, one for each
  2264. * axis.
  2265. *
  2266. * @parm PDCHID | this |
  2267. *
  2268. * The device object.
  2269. *
  2270. * @parm IN LPCDIPROPINFO | ppropi |
  2271. *
  2272. * Information describing the property being set.
  2273. *
  2274. * @parm LPCDIPROPHEADER | pdiph |
  2275. *
  2276. * Structure containing property value.
  2277. *
  2278. * @returns
  2279. *
  2280. * Returns a COM error code. The following error codes are
  2281. * intended to be illustrative and not necessarily comprehensive.
  2282. *
  2283. * <c DI_OK> = <c S_OK>: The operation completed successfully.
  2284. *
  2285. * <c E_NOTIMPL> nothing happened. The caller will do
  2286. * the default thing in response to <c E_NOTIMPL>.
  2287. *
  2288. *****************************************************************************/
  2289. STDMETHODIMP
  2290. CHid_SetAxisProperty
  2291. (
  2292. PCHID this,
  2293. LPCDIPROPINFO ppropi,
  2294. LPCDIPROPHEADER pdiph
  2295. )
  2296. {
  2297. HRESULT hres;
  2298. INT iObjLimit;
  2299. INT iObj;
  2300. if (ppropi->dwDevType == 0)
  2301. { /* For device try every object */
  2302. iObj = 0;
  2303. iObjLimit = this->df.dwNumObjs;
  2304. }
  2305. else
  2306. { /* For axis just do the requested one */
  2307. iObj = ppropi->iobj;
  2308. iObjLimit = ppropi->iobj + 1;
  2309. }
  2310. hres = S_OK;
  2311. for( ; iObj < iObjLimit; iObj++ )
  2312. {
  2313. PJOYRANGECONVERT pjrc;
  2314. if( pjrc = this->rghoc[iObj].pjrc )
  2315. {
  2316. if( (this->df.rgodf[iObj].dwType &
  2317. (DIDFT_ALIAS | DIDFT_VENDORDEFINED | DIDFT_OUTPUT | DIDFT_ABSAXIS)) == DIDFT_ABSAXIS)
  2318. {
  2319. PHIDGROUPCAPS pcaps = this->rghoc[iObj].pcaps;
  2320. DIPROPCAL cal;
  2321. /*
  2322. * Specific calibrations arrive in VJOYD coordinates.
  2323. * We need to convert them to DirectInput (logical)
  2324. * coordinates if so.
  2325. */
  2326. if(ppropi->pguid == DIPROP_SPECIFICCALIBRATION)
  2327. {
  2328. if( pcaps )
  2329. {
  2330. PLMINMAX Dst = &pcaps->Logical;
  2331. PLMINMAX Src = &pcaps->Physical;
  2332. LPDIPROPCAL pcal = CONTAINING_RECORD(pdiph, DIPROPCAL, diph);
  2333. cal.lMin = CHid_CoordinateTransform(Dst, Src, pcal->lMin);
  2334. cal.lCenter = CHid_CoordinateTransform(Dst, Src, pcal->lCenter);
  2335. cal.lMax = CHid_CoordinateTransform(Dst, Src, pcal->lMax);
  2336. pdiph = &cal.diph;
  2337. }
  2338. else
  2339. {
  2340. AssertF( ppropi->dwDevType == 0 );
  2341. /*
  2342. * Ignore the error. If this is an object set
  2343. * property validation should have caught this
  2344. * already and the DX7 patch code for device set
  2345. * property special cased E_NOTIMPL so that one bad
  2346. * axis would not cause the whole call to fail when
  2347. * other axes may be OK.
  2348. * A bit flakey but "this should never happen"
  2349. */
  2350. continue;
  2351. }
  2352. }
  2353. hres = CCal_SetProperty( pjrc, ppropi, pdiph, this->hkInstType );
  2354. #ifndef WINNT
  2355. /*
  2356. * If we successfully changed the calibration of a game
  2357. * controller device, then see if it's a VJOYD device.
  2358. */
  2359. if(SUCCEEDED(hres) &&
  2360. ppropi->pguid == DIPROP_CALIBRATION &&
  2361. GET_DIDEVICE_TYPE(this->dwDevType) >= DI8DEVTYPE_GAMEMIN)
  2362. {
  2363. CHid_UpdateVjoydCalibration(this, ppropi->iobj);
  2364. }
  2365. #endif
  2366. }
  2367. #ifdef WINNT
  2368. else if( (this->df.rgodf[iObj].dwType &
  2369. (DIDFT_ALIAS | DIDFT_VENDORDEFINED | DIDFT_OUTPUT | DIDFT_POV)) == DIDFT_POV)
  2370. {
  2371. PHIDGROUPCAPS pcaps = this->rghoc[iObj].pcaps;
  2372. if( pcaps )
  2373. {
  2374. if( pcaps->IsPolledPOV )
  2375. {
  2376. hres = CCal_SetProperty(pjrc, ppropi, pdiph, this->hkInstType);
  2377. if( SUCCEEDED(hres) ) {
  2378. CHid_LoadCalibrations(this);
  2379. CHid_InitParseData( this );
  2380. }
  2381. }
  2382. else
  2383. {
  2384. if( ppropi->dwDevType != 0 )
  2385. {
  2386. hres = E_NOTIMPL;
  2387. }
  2388. }
  2389. }
  2390. else
  2391. {
  2392. AssertF( ppropi->dwDevType == 0 );
  2393. /*
  2394. * Ignore the error. If this is an object set
  2395. * property validation should have caught this
  2396. * already and the DX7 patch code for device set
  2397. * property special cased E_NOTIMPL so that one bad
  2398. * axis would not cause the whole call to fail when
  2399. * other axes may be OK.
  2400. * A bit flakey but "this should never happen"
  2401. */
  2402. continue;
  2403. }
  2404. }
  2405. #endif
  2406. }
  2407. else
  2408. {
  2409. /*
  2410. * If the object cannot have an axis property set, the DX7 code
  2411. * returned E_NOTIMPL when it should have returned some parameter
  2412. * error. For a device, this is not an error as we are iterating
  2413. * all objects looking for absolute axes.
  2414. * If it is an absolute axis but has no range conversion, return
  2415. * E_NOTIMPL to match previous versions for an object but ignore
  2416. * for the device. This probably should be an E_FAIL...
  2417. */
  2418. if( ppropi->dwDevType != 0 )
  2419. {
  2420. hres = E_NOTIMPL;
  2421. }
  2422. }
  2423. }
  2424. /*
  2425. * Don't need to hold/unhold here because the app called us so it should
  2426. * not be releasing us at the same time. (mb:34570)
  2427. */
  2428. CHid_LoadCalibrations(this);
  2429. if( SUCCEEDED(hres) )
  2430. {
  2431. /*
  2432. * Until such time as CHid_InitParseData returns anything other than
  2433. * S_OK, don't update a possibly more informative result with this.
  2434. */
  2435. D( HRESULT hresDbg = )
  2436. CHid_InitParseData( this );
  2437. D( AssertF( hresDbg == S_OK ); )
  2438. }
  2439. return hres;
  2440. }
  2441. /*****************************************************************************
  2442. *
  2443. * @doc INTERNAL
  2444. *
  2445. * @method HRESULT | CHid | SetProperty |
  2446. *
  2447. * Set a hid device property.
  2448. *
  2449. * @parm PCHID | this |
  2450. *
  2451. * The hid object.
  2452. *
  2453. * @parm IN LPCDIPROPINFO | ppropi |
  2454. *
  2455. * Information describing the property being set.
  2456. *
  2457. * @parm LPCDIPROPHEADER | pdiph |
  2458. *
  2459. * Structure containing property value.
  2460. *
  2461. * @returns
  2462. *
  2463. * Returns a COM error code. The following error codes are
  2464. * intended to be illustrative and not necessarily comprehensive.
  2465. *
  2466. * <c DI_OK> = <c S_OK>: The operation completed successfully.
  2467. *
  2468. * <c E_NOTIMPL> nothing happened. The caller will do
  2469. * the default thing in response to <c E_NOTIMPL>.
  2470. *
  2471. *****************************************************************************/
  2472. STDMETHODIMP
  2473. CHid_SetProperty(PDICB pdcb, LPCDIPROPINFO ppropi, LPCDIPROPHEADER pdiph)
  2474. {
  2475. HRESULT hres;
  2476. PCHID this;
  2477. EnterProcI(IDirectInputDeviceCallback::Hid::SetProperty,
  2478. (_ "pxxp", pdcb, ppropi->pguid, ppropi->iobj, pdiph));
  2479. /*
  2480. * This is an internal interface, so we can skimp on validation.
  2481. */
  2482. this = _thisPvNm(pdcb, dcb);
  2483. if(ppropi->iobj < this->df.dwNumObjs)
  2484. {
  2485. /*
  2486. * Object Property
  2487. */
  2488. PHIDGROUPCAPS pcaps;
  2489. AssertF(ppropi->dwDevType == this->df.rgodf[ppropi->iobj].dwType);
  2490. AssertF(ppropi->iobj == CHid_ObjFromType(this, ppropi->dwDevType));
  2491. if( pcaps = this->rghoc[ppropi->iobj].pcaps )
  2492. {
  2493. switch((DWORD)(UINT_PTR)ppropi->pguid)
  2494. {
  2495. case (DWORD)(UINT_PTR)(DIPROP_ENABLEREPORTID):
  2496. {
  2497. LPDIPROPDWORD ppropdw = CONTAINING_RECORD(pdiph, DIPROPDWORD, diph);
  2498. AssertF(pcaps->wReportId < this->wMaxReportId[pcaps->type]);
  2499. AssertF(this->pEnableReportId[pcaps->type]);
  2500. hres = S_OK;
  2501. if( ppropdw->dwData == 0x1 )
  2502. {
  2503. *(this->pEnableReportId[pcaps->type] + pcaps->wReportId) = 0x1;
  2504. pcaps->fReportDisabled = FALSE;
  2505. } else
  2506. {
  2507. *(this->pEnableReportId[pcaps->type] + pcaps->wReportId) = 0x0;
  2508. pcaps->fReportDisabled = TRUE;
  2509. }
  2510. }
  2511. break;
  2512. default:
  2513. AssertF(ppropi->dwDevType == this->df.rgodf[ppropi->iobj].dwType);
  2514. AssertF(ppropi->iobj == CHid_ObjFromType(this, ppropi->dwDevType));
  2515. hres = CHid_SetAxisProperty( this, ppropi, pdiph );
  2516. }
  2517. } else
  2518. {
  2519. SquirtSqflPtszV(sqflHidDev | sqflError,
  2520. TEXT("CHid_SetProperty FAILED due to missing caps for type 0x%08x, obj %d"),
  2521. ppropi->dwDevType, ppropi->iobj );
  2522. hres = E_NOTIMPL;
  2523. }
  2524. } else if(ppropi->iobj == 0xFFFFFFFF)
  2525. { /* Device property */
  2526. switch((DWORD)(UINT_PTR)ppropi->pguid)
  2527. {
  2528. case (DWORD)(UINT_PTR)DIPROP_GUIDANDPATH:
  2529. SquirtSqflPtszV(sqflHidDev | sqflError,
  2530. TEXT("CHid_SetProperty(iobj=%08x): PROP_GUIDANDPATH is read only.") );
  2531. hres = E_NOTIMPL;
  2532. break;
  2533. case (DWORD)(UINT_PTR)DIPROP_INSTANCENAME:
  2534. /*
  2535. * Friendly names cause all manner of problems with devices that
  2536. * use auto detection so only allow non-predefined analog devices
  2537. * to use them.
  2538. */
  2539. if( ( this->VendorID == MSFT_SYSTEM_VID )
  2540. && ( this->ProductID >= MSFT_SYSTEM_PID + JOY_HW_PREDEFMAX )
  2541. && ( ( this->ProductID & 0xff00 ) == MSFT_SYSTEM_PID ) )
  2542. {
  2543. AssertF(this->hkType);
  2544. if( this->hkType )
  2545. {
  2546. LPDIPROPSTRING pstr = (PV)pdiph;
  2547. hres = JoyReg_SetValue(this->hkType,
  2548. REGSTR_VAL_JOYOEMNAME, REG_SZ,
  2549. pstr->wsz,
  2550. cbX(pstr->wsz));
  2551. if( SUCCEEDED(hres ) )
  2552. {
  2553. SquirtSqflPtszV(sqflHid | sqflVerbose,
  2554. TEXT( "Set instance name %s"), pstr->wsz );
  2555. hres = S_OK;
  2556. } else {
  2557. hres = E_FAIL;
  2558. }
  2559. } else {
  2560. hres = E_FAIL;
  2561. }
  2562. }
  2563. else
  2564. {
  2565. /*
  2566. * GenJ returns E_NOTIMPL for this property so do the same
  2567. */
  2568. hres = E_NOTIMPL;
  2569. }
  2570. break;
  2571. case (DWORD)(UINT_PTR)DIPROP_PRODUCTNAME:
  2572. #ifdef WINNT
  2573. hres = DIHid_SetParentRegistryProperty(this->ptszId, SPDRP_DEVICEDESC, pdiph);
  2574. #else
  2575. hres = DIHid_SetRegistryProperty(this->ptszId, SPDRP_DEVICEDESC, pdiph);
  2576. #endif
  2577. break;
  2578. case (DWORD)(UINT_PTR)(DIPROP_ENABLEREPORTID):
  2579. {
  2580. LPDIPROPDWORD ppropdw = CONTAINING_RECORD(pdiph, DIPROPDWORD, diph);
  2581. UINT iType;
  2582. if( ppropdw->dwData == 0x0 )
  2583. {
  2584. for( iType = 0x0; iType < HidP_Max; iType++)
  2585. {
  2586. ZeroBuf(this->pEnableReportId[iType], this->wMaxReportId[iType]);
  2587. }
  2588. } else
  2589. {
  2590. for( iType = 0x0; iType < HidP_Max; iType++)
  2591. {
  2592. memset(this->pEnableReportId[iType], 0x1, this->wMaxReportId[iType]);
  2593. }
  2594. }
  2595. hres = S_OK;
  2596. }
  2597. break;
  2598. case (DWORD)(UINT_PTR)DIPROP_RANGE:
  2599. case (DWORD)(UINT_PTR)DIPROP_DEADZONE:
  2600. case (DWORD)(UINT_PTR)DIPROP_SATURATION:
  2601. case (DWORD)(UINT_PTR)DIPROP_CALIBRATIONMODE:
  2602. case (DWORD)(UINT_PTR)DIPROP_CALIBRATION:
  2603. hres = CHid_SetAxisProperty( this, ppropi, pdiph );
  2604. break;
  2605. default:
  2606. SquirtSqflPtszV(sqflHidDev| sqflBenign,
  2607. TEXT("CHid_SetProperty(iobj=%08x): E_NOTIMPL on guid: %08x"),
  2608. ppropi->iobj, ppropi->pguid);
  2609. hres = E_NOTIMPL;
  2610. break;
  2611. }
  2612. } else
  2613. {
  2614. SquirtSqflPtszV(sqflHidDev | sqflError,
  2615. TEXT("CHid_SetProperty(iobj=%08x): E_NOTIMPL on guid: %08x"),
  2616. ppropi->iobj, ppropi->pguid);
  2617. hres = E_NOTIMPL;
  2618. }
  2619. ExitOleProcR();
  2620. return hres;
  2621. }
  2622. /*****************************************************************************
  2623. *
  2624. * @doc INTERNAL
  2625. *
  2626. * @method void | CHid | GetCapabilities |
  2627. *
  2628. * Get Hid device capabilities.
  2629. *
  2630. * @parm LPDIDEVCAPS | pdc |
  2631. *
  2632. * Device capabilities structure to receive result.
  2633. *
  2634. * @returns
  2635. * <c S_OK> on success.
  2636. *
  2637. *****************************************************************************/
  2638. STDMETHODIMP
  2639. CHid_GetCapabilities(PDICB pdcb, LPDIDEVCAPS pdc)
  2640. {
  2641. HRESULT hres;
  2642. PCHID this;
  2643. HANDLE h;
  2644. DWORD dwFlags2 = 0;
  2645. EnterProcI(IDirectInputDeviceCallback::Hid::GetCapabilities,
  2646. (_ "pp", pdcb, pdc));
  2647. /*
  2648. * This is an internal interface, so we can skimp on validation.
  2649. */
  2650. this = _thisPvNm(pdcb, dcb);
  2651. /*
  2652. * We must check connectivity by opening the device, because NT
  2653. * leaves the device in the info list even though it has
  2654. * been unplugged.
  2655. */
  2656. h = CHid_OpenDevicePath(this, FILE_FLAG_OVERLAPPED);
  2657. if(h != INVALID_HANDLE_VALUE)
  2658. {
  2659. #ifndef WINNT
  2660. if( this->hkType )
  2661. {
  2662. VXDINITPARMS vip;
  2663. CHid_FindJoyDevice(this, &vip);
  2664. if( TRUE == CHid_IsMatchingJoyDevice( this, &vip ) )
  2665. {
  2666. DWORD dwFlags1;
  2667. if( SUCCEEDED( JoyReg_GetValue( this->hkType,
  2668. REGSTR_VAL_FLAGS1, REG_BINARY,
  2669. &dwFlags1,
  2670. cbX(dwFlags1) ) ) )
  2671. {
  2672. if( dwFlags1 & JOYTYPE_NOHIDDIRECT )
  2673. {
  2674. pdc->dwFlags |= DIDC_ALIAS;
  2675. }
  2676. }
  2677. }
  2678. }
  2679. #endif // !WINNT
  2680. CloseHandle(h);
  2681. if( this->pvi->fl & VIFL_UNPLUGGED )
  2682. {
  2683. pdc->dwFlags &= ~DIDC_ATTACHED;
  2684. } else
  2685. {
  2686. pdc->dwFlags |= DIDC_ATTACHED;
  2687. }
  2688. } else
  2689. {
  2690. pdc->dwFlags &= ~DIDC_ATTACHED;
  2691. }
  2692. if( this->IsPolledInput )
  2693. {
  2694. pdc->dwFlags |= DIDC_POLLEDDEVICE;
  2695. }
  2696. if( this->hkProp )
  2697. {
  2698. JoyReg_GetValue( this->hkProp, REGSTR_VAL_FLAGS2, REG_BINARY,
  2699. &dwFlags2, cbX(dwFlags2) );
  2700. }
  2701. if( !( dwFlags2 & JOYTYPE_HIDEACTIVE ) )
  2702. {
  2703. // Currently we only hide "fictional" keyboards and mice.
  2704. if( ( GET_DIDEVICE_TYPE( this->dwDevType ) == DI8DEVTYPE_MOUSE )
  2705. ||( GET_DIDEVICE_TYPE( this->dwDevType ) == DI8DEVTYPE_KEYBOARD )
  2706. )
  2707. {
  2708. dwFlags2 = DIHid_DetectHideAndRevealFlags(this) ;
  2709. }
  2710. }
  2711. if( dwFlags2 & JOYTYPE_HIDEACTIVE )
  2712. {
  2713. switch( GET_DIDEVICE_TYPE( this->dwDevType ) )
  2714. {
  2715. case DI8DEVTYPE_DEVICE:
  2716. if( dwFlags2 & JOYTYPE_DEVICEHIDE )
  2717. {
  2718. pdc->dwFlags |= DIDC_HIDDEN;
  2719. }
  2720. break;
  2721. case DI8DEVTYPE_MOUSE:
  2722. if( dwFlags2 & JOYTYPE_MOUSEHIDE )
  2723. {
  2724. pdc->dwFlags |= DIDC_HIDDEN;
  2725. }
  2726. break;
  2727. case DI8DEVTYPE_KEYBOARD:
  2728. if( dwFlags2 & JOYTYPE_KEYBHIDE )
  2729. {
  2730. pdc->dwFlags |= DIDC_HIDDEN;
  2731. }
  2732. break;
  2733. default:
  2734. if( dwFlags2 & JOYTYPE_GAMEHIDE )
  2735. {
  2736. pdc->dwFlags |= DIDC_HIDDEN;
  2737. }
  2738. break;
  2739. }
  2740. }
  2741. pdc->dwDevType = this->dwDevType;
  2742. pdc->dwAxes = this->dwAxes;
  2743. pdc->dwButtons = this->dwButtons;
  2744. pdc->dwPOVs = this->dwPOVs;
  2745. hres = S_OK;
  2746. ExitOleProcR();
  2747. return hres;
  2748. }
  2749. /*****************************************************************************
  2750. *
  2751. * @doc INTERNAL
  2752. *
  2753. * @method HRESULT | CHid | GetDeviceState |
  2754. *
  2755. * Obtains the state of the Hid device.
  2756. *
  2757. * It is the caller's responsibility to have validated all the
  2758. * parameters and ensure that the device has been acquired.
  2759. *
  2760. * @parm OUT LPVOID | lpvData |
  2761. *
  2762. * Hid data in the preferred data format.
  2763. *
  2764. * @returns
  2765. *
  2766. * Returns a COM error code. The following error codes are
  2767. * intended to be illustrative and not necessarily comprehensive.
  2768. *
  2769. * <c DI_OK> = <c S_OK>: The operation completed successfully.
  2770. *
  2771. * <c DIERR_INVALIDPARAM> = <c E_INVALIDARG>: The
  2772. * <p lpmdr> parameter is not a valid pointer.
  2773. *
  2774. *****************************************************************************/
  2775. STDMETHODIMP
  2776. CHid_GetDeviceState(PDICB pdcb, LPVOID pvData)
  2777. {
  2778. HRESULT hres;
  2779. PCHID this;
  2780. EnterProcI(IDirectInputDeviceCallback::Hid::GetDeviceState,
  2781. (_ "pp", pdcb, pvData));
  2782. /*
  2783. * This is an internal interface, so we can skimp on validation.
  2784. */
  2785. this = _thisPvNm(pdcb, dcb);
  2786. AssertF(this->pvi);
  2787. AssertF(this->pvPhys);
  2788. AssertF(this->cbPhys);
  2789. if(this->pvi->fl & VIFL_ACQUIRED)
  2790. {
  2791. CHid_GetPhysicalState(this, pvData);
  2792. hres = S_OK;
  2793. } else
  2794. {
  2795. hres = DIERR_INPUTLOST;
  2796. }
  2797. ExitOleProcR();
  2798. return hres;
  2799. }
  2800. /*****************************************************************************
  2801. *
  2802. * @doc INTERNAL
  2803. *
  2804. * @method HRESULT | CHid | GetObjectInfo |
  2805. *
  2806. * Obtain the friendly name and FF/HID information
  2807. * of an object.
  2808. *
  2809. * @parm IN LPCDIPROPINFO | ppropi |
  2810. *
  2811. * Information describing the object being accessed.
  2812. *
  2813. * @parm IN OUT LPDIDEVICEOBJECTINSTANCEW | pdidioiW |
  2814. *
  2815. * Structure to receive information. All fields have been
  2816. * filled in up to the <e DIDEVICEOBJECTINSTANCE.tszObjName>.
  2817. *
  2818. * @returns
  2819. *
  2820. * Returns a COM error code.
  2821. *
  2822. *****************************************************************************/
  2823. STDMETHODIMP
  2824. CHid_GetObjectInfo(PDICB pdcb, LPCDIPROPINFO ppropi,
  2825. LPDIDEVICEOBJECTINSTANCEW pdidoiW)
  2826. {
  2827. HRESULT hres;
  2828. PCHID this;
  2829. EnterProcI(IDirectInputDeviceCallback::Hid::GetObjectInfo,
  2830. (_ "pxp", pdcb, ppropi->iobj, pdidoiW));
  2831. /*
  2832. * This is an internal interface, so we can skimp on validation.
  2833. */
  2834. this = _thisPvNm(pdcb, dcb);
  2835. AssertF((int) ppropi->iobj >= 0);
  2836. if(ppropi->iobj < this->df.dwNumObjs)
  2837. {
  2838. UINT uiInstance = ppropi->iobj;
  2839. PHIDGROUPCAPS pcaps;
  2840. AssertF(ppropi->dwDevType == this->df.rgodf[uiInstance].dwType);
  2841. AssertF(uiInstance == CHid_ObjFromType(this, ppropi->dwDevType));
  2842. pcaps = this->rghoc[uiInstance].pcaps;
  2843. /*
  2844. * pcaps might be NULL if HID messed up and left gaps
  2845. * in the index lists.
  2846. */
  2847. if(pcaps)
  2848. {
  2849. UINT ids, duiInstance;
  2850. AssertF(pcaps->dwSignature == HIDGROUPCAPS_SIGNATURE);
  2851. /*
  2852. * See if there's anything in the registry that will help.
  2853. */
  2854. CType_RegGetObjectInfo(this->hkType, ppropi->dwDevType, pdidoiW);
  2855. if(ppropi->dwDevType & DIDFT_COLLECTION)
  2856. {
  2857. ids = IDS_COLLECTIONTEMPLATE;
  2858. duiInstance = 0;
  2859. } else
  2860. {
  2861. if(ppropi->dwDevType & DIDFT_BUTTON)
  2862. {
  2863. ids = IDS_BUTTONTEMPLATE;
  2864. } else if(ppropi->dwDevType & DIDFT_AXIS)
  2865. {
  2866. ids = IDS_AXISTEMPLATE;
  2867. } else if(ppropi->dwDevType & DIDFT_POV)
  2868. {
  2869. ids = IDS_POVTEMPLATE;
  2870. } else
  2871. {
  2872. ids = IDS_UNKNOWNTEMPLATE;
  2873. }
  2874. /*
  2875. * Now convert the uiInstance to a duiInstance,
  2876. * giving the index of this object into the group.
  2877. */
  2878. AssertF(HidP_IsValidReportType(pcaps->type));
  2879. duiInstance = uiInstance -
  2880. (this->rgdwBase[pcaps->type] +
  2881. pcaps->DataIndexMin);
  2882. }
  2883. /*
  2884. * Okay, now we have all the info we need to proceed.
  2885. */
  2886. /*
  2887. * If there was no overriding name in the registry, then
  2888. * try to get a custom name from the usage page/usage.
  2889. * If even that fails, then use the generic name.
  2890. * Note, generic names will contain zero based numbers
  2891. * which can look wrong if some objects have names and
  2892. * others take defaults.
  2893. */
  2894. if(pdidoiW->tszName[0])
  2895. {
  2896. } else
  2897. if(GetHIDString(pcaps->UsageMin + duiInstance,
  2898. pcaps->UsagePage,
  2899. pdidoiW->tszName, cA(pdidoiW->tszName)))
  2900. {
  2901. if(ppropi->dwDevType & DIDFT_COLLECTION)
  2902. {
  2903. InsertCollectionNumber(DIDFT_GETINSTANCE( ppropi->dwDevType ),
  2904. pdidoiW->tszName);
  2905. }
  2906. } else
  2907. {
  2908. GetNthString(pdidoiW->tszName, ids,
  2909. DIDFT_GETINSTANCE( ppropi->dwDevType ));
  2910. }
  2911. if(pdidoiW->dwSize >= cbX(DIDEVICEOBJECTINSTANCE_DX5W))
  2912. {
  2913. pdidoiW->wCollectionNumber = pcaps->LinkCollection;
  2914. pdidoiW->wDesignatorIndex = pcaps->DesignatorMin + duiInstance;
  2915. if(pdidoiW->wDesignatorIndex > pcaps->DesignatorMax)
  2916. {
  2917. pdidoiW->wDesignatorIndex = pcaps->DesignatorMax;
  2918. }
  2919. /*
  2920. * Much as you may try, you cannot override the usage
  2921. * page and usage. Doing so would mess up the GUID
  2922. * selection code that happens in DIHIDINI.C.
  2923. *
  2924. * If you change your mind and allow overridden usage
  2925. * pages and usages, then you'll also have to change
  2926. * CHid_GetUsage.
  2927. *
  2928. * At this point, the registry overrides have already
  2929. * been read so defeat the override here.
  2930. */
  2931. pdidoiW->wUsagePage = pcaps->UsagePage;
  2932. pdidoiW->wUsage = pcaps->UsageMin + duiInstance;
  2933. pdidoiW->dwDimension = pcaps->Units;
  2934. pdidoiW->wExponent = pcaps->Exponent;
  2935. pdidoiW->wReportId = pcaps->wReportId;
  2936. }
  2937. hres = S_OK;
  2938. } else
  2939. {
  2940. hres = E_INVALIDARG;
  2941. }
  2942. } else
  2943. {
  2944. hres = E_INVALIDARG;
  2945. }
  2946. ExitOleProcR();
  2947. return hres;
  2948. }
  2949. /*****************************************************************************
  2950. *
  2951. * @doc INTERNAL
  2952. *
  2953. * @method DWORD | CHid | GetUsage |
  2954. *
  2955. * Given an object index, return the usage and usage page,
  2956. * packed into a single <t DWORD>.
  2957. *
  2958. * @parm int | iobj |
  2959. *
  2960. * The object index to convert.
  2961. *
  2962. * @returns
  2963. *
  2964. * Returns a <c DIMAKEUSAGEDWORD> of the resulting usage and
  2965. * usage page, or zero on error.
  2966. *
  2967. *****************************************************************************/
  2968. STDMETHODIMP_(DWORD)
  2969. CHid_GetUsage(PDICB pdcb, int iobj)
  2970. {
  2971. PCHID this;
  2972. PHIDGROUPCAPS pcaps;
  2973. DWORD dwRc;
  2974. EnterProcI(IDirectInputDeviceCallback::Hid::GetUsage,
  2975. (_ "pu", pdcb, iobj));
  2976. /*
  2977. * This is an internal interface, so we can skimp on validation.
  2978. */
  2979. this = _thisPvNm(pdcb, dcb);
  2980. AssertF(iobj >= 0);
  2981. AssertF((UINT)iobj < this->df.dwNumObjs);
  2982. pcaps = this->rghoc[iobj].pcaps;
  2983. /*
  2984. * pcaps might be NULL if HID messed up and left gaps
  2985. * in the index lists.
  2986. */
  2987. if(pcaps)
  2988. {
  2989. UINT duiInstance;
  2990. AssertF(pcaps->dwSignature == HIDGROUPCAPS_SIGNATURE);
  2991. if(this->df.rgodf[iobj].dwType & DIDFT_COLLECTION)
  2992. {
  2993. duiInstance = 0;
  2994. } else
  2995. {
  2996. /*
  2997. * Now convert the iobj to a duiInstance,
  2998. * giving the index of this object into the group.
  2999. */
  3000. AssertF(HidP_IsValidReportType(pcaps->type));
  3001. duiInstance = iobj -
  3002. (this->rgdwBase[pcaps->type] +
  3003. pcaps->DataIndexMin);
  3004. }
  3005. /*
  3006. * CHid_GetObjectInfo also assumes that there is no way
  3007. * to override the usage page and usage values in the
  3008. * registry.
  3009. */
  3010. dwRc = DIMAKEUSAGEDWORD(pcaps->UsagePage,
  3011. pcaps->UsageMin + duiInstance);
  3012. } else
  3013. {
  3014. dwRc = 0;
  3015. }
  3016. ExitProcX(dwRc);
  3017. return dwRc;
  3018. }
  3019. /*****************************************************************************
  3020. *
  3021. * @doc INTERNAL
  3022. *
  3023. * @method HRESULT | CHid | MapUsage |
  3024. *
  3025. *
  3026. * Given a usage and usage page (munged into a single
  3027. * <t DWORD>), find a device object that matches it.
  3028. *
  3029. * @parm DWORD | dwUsage |
  3030. *
  3031. * The usage page and usage combined into a single <t DWORD>
  3032. * with the <f DIMAKEUSAGEDWORD> macro.
  3033. *
  3034. * @parm PINT | piOut |
  3035. *
  3036. * Receives the object index of the found object, if successful.
  3037. *
  3038. * @returns
  3039. *
  3040. * Returns a COM error code.
  3041. *
  3042. * <c S_OK> if an object was found.
  3043. *
  3044. * <c DIERR_NOTFOUND> if no matching object was found.
  3045. *
  3046. *****************************************************************************/
  3047. STDMETHODIMP
  3048. CHid_MapUsage(PDICB pdcb, DWORD dwUsage, PINT piOut)
  3049. {
  3050. HRESULT hres;
  3051. PCHID this;
  3052. UINT icaps;
  3053. UINT uiObj;
  3054. UINT duiObj;
  3055. EnterProcI(IDirectInputDeviceCallback::Hid::MapUsage,
  3056. (_ "px", pdcb, dwUsage));
  3057. /*
  3058. * This is an internal interface, so we can skimp on validation.
  3059. */
  3060. this = _thisPvNm(pdcb, dcb);
  3061. for(icaps = 0; icaps < this->ccaps; icaps++)
  3062. {
  3063. PHIDGROUPCAPS pcaps = &this->rgcaps[icaps];
  3064. /*
  3065. * Shall we support mapping HidP_Output usage?
  3066. * If we should, it is easy to add it later.
  3067. */
  3068. uiObj = this->rgdwBase[HidP_Input] + pcaps->DataIndexMin;
  3069. for(duiObj = 0; duiObj < pcaps->cObj; duiObj++)
  3070. {
  3071. if( dwUsage == DIMAKEUSAGEDWORD(pcaps->UsagePage, pcaps->UsageMin+duiObj) )
  3072. {
  3073. *piOut = uiObj+duiObj;
  3074. AssertF(*piOut < (INT)this->df.dwNumObjs);
  3075. hres = S_OK;
  3076. goto done;
  3077. }
  3078. }
  3079. }
  3080. hres = DIERR_NOTFOUND;
  3081. done:;
  3082. ExitBenignOleProcR();
  3083. return hres;
  3084. }
  3085. /*****************************************************************************
  3086. *
  3087. * @doc INTERNAL
  3088. *
  3089. * @method HRESULT | CHid | SetCooperativeLevel |
  3090. *
  3091. * Notify the device of the cooperative level.
  3092. *
  3093. * @parm IN HWND | hwnd |
  3094. *
  3095. * The window handle.
  3096. *
  3097. * @parm IN DWORD | dwFlags |
  3098. *
  3099. * The cooperativity level.
  3100. *
  3101. * @returns
  3102. *
  3103. * Returns a COM error code.
  3104. *
  3105. *****************************************************************************/
  3106. STDMETHODIMP
  3107. CHid_SetCooperativeLevel(PDICB pdcb, HWND hwnd, DWORD dwFlags)
  3108. {
  3109. HRESULT hres;
  3110. PCHID this;
  3111. EnterProcI(IDirectInputDeviceCallback::Hid::SetCooperativityLevel,
  3112. (_ "pxx", pdcb, hwnd, dwFlags));
  3113. /*
  3114. * This is an internal interface, so we can skimp on validation.
  3115. */
  3116. this = _thisPvNm(pdcb, dcb);
  3117. /*
  3118. * We won't subclass Motocross Madness. See NT bug 262280.
  3119. * Use the app hacks for MCM and any app like it.
  3120. */
  3121. if( !this->diHacks.fNoSubClass )
  3122. {
  3123. AssertF(this->pvi);
  3124. /*
  3125. * First get out of the old window.
  3126. */
  3127. CHid_RemoveSubclass(this);
  3128. /*
  3129. * Prefix warns that "this" may have been freed (mb:34570) however
  3130. * If you're in SetCooperativeLevel and you have a window subclassed
  3131. * then there must be a hold for the subclassed window as well as
  3132. * one for the unreleased interface so the Common_Unhold won't free
  3133. * the pointer.
  3134. */
  3135. /*
  3136. * If a new window is passed, then subclass it so we can
  3137. * watch for joystick configuration change messages.
  3138. *
  3139. * If we can't, don't worry. All it means that we won't
  3140. * be able to catch when the user recalibrates a device,
  3141. * which isn't very often.
  3142. */
  3143. if(hwnd)
  3144. {
  3145. if(SetWindowSubclass(hwnd, CHid_SubclassProc, 0x0, (ULONG_PTR)this))
  3146. {
  3147. this->hwnd = hwnd;
  3148. Common_Hold(this);
  3149. }
  3150. } else
  3151. {
  3152. RPF("SetCooperativeLevel: You really shouldn't pass hwnd = 0; "
  3153. "device calibration may be dodgy");
  3154. }
  3155. }
  3156. hres = S_OK;
  3157. ExitOleProcR();
  3158. return hres;
  3159. }
  3160. /*****************************************************************************
  3161. *
  3162. * @doc INTERNAL
  3163. *
  3164. * @method HRESULT | CHid | RunControlPanel |
  3165. *
  3166. * Run the Hid control panel.
  3167. *
  3168. * @parm IN HWND | hwndOwner |
  3169. *
  3170. * The owner window.
  3171. *
  3172. * @parm DWORD | dwFlags |
  3173. *
  3174. * Flags.
  3175. *
  3176. *****************************************************************************/
  3177. STDMETHODIMP
  3178. CHid_RunControlPanel(PDICB pdcb, HWND hwnd, DWORD dwFlags)
  3179. {
  3180. HRESULT hres;
  3181. PCHID this;
  3182. EnterProcI(IDirectInputDeviceCallback::Hid::RunControlPanel,
  3183. (_ "pxx", pdcb, hwnd, dwFlags));
  3184. /*
  3185. * This is an internal interface, so we can skimp on validation.
  3186. */
  3187. this = _thisPvNm(pdcb, dcb);
  3188. /*
  3189. * How to invoke HID cpl?
  3190. *
  3191. * Currently, we just launch joy.cpl. If more HID devices show up
  3192. * which don't belong to game control panel, we may change it to
  3193. * proper cpl.
  3194. *
  3195. * on NT hresRunControlPanel(TEXT("srcmgr.cpl,@2"));
  3196. * on 9x hresRunControlPanel(TEXT("sysdm.cpl,@0,1"));
  3197. *
  3198. */
  3199. hres = hresRunControlPanel(TEXT("joy.cpl"));
  3200. ExitOleProcR();
  3201. return hres;
  3202. }
  3203. /*****************************************************************************
  3204. *
  3205. * @doc INTERNAL
  3206. *
  3207. * @method HRESULT | CHid | GetFFConfigKey |
  3208. *
  3209. * Open and return the registry key that contains
  3210. * force feedback configuration information.
  3211. *
  3212. * @parm DWORD | sam |
  3213. *
  3214. * Security access mask.
  3215. *
  3216. * @parm PHKEY | phk |
  3217. *
  3218. * Receives the registry key.
  3219. *
  3220. *****************************************************************************/
  3221. STDMETHODIMP
  3222. CHid_GetFFConfigKey(PDICB pdcb, DWORD sam, PHKEY phk)
  3223. {
  3224. HRESULT hres;
  3225. PCHID this;
  3226. EnterProcI(IDirectInputDeviceCallback::HID::GetFFConfigKey,
  3227. (_ "px", pdcb, sam));
  3228. /*
  3229. * This is an internal interface, so we can skimp on validation.
  3230. */
  3231. this = _thisPvNm(pdcb, dcb);
  3232. hres = JoyReg_OpenFFKey(this->hkType, sam, phk);
  3233. AssertF(fLeqvFF(SUCCEEDED(hres), *phk));
  3234. if(FAILED(hres) && this->fPIDdevice )
  3235. {
  3236. *phk = NULL;
  3237. hres = S_FALSE;
  3238. }
  3239. ExitBenignOleProcPpvR(phk);
  3240. return hres;
  3241. }
  3242. /*****************************************************************************
  3243. *
  3244. * @doc INTERNAL
  3245. *
  3246. * @method HRESULT | CHid | GetDeviceInfo |
  3247. *
  3248. * Obtain general information about the device.
  3249. *
  3250. * @parm OUT LPDIDEVICEINSTANCEW | pdiW |
  3251. *
  3252. * <t DEVICEINSTANCE> to be filled in. The
  3253. * <e DEVICEINSTANCE.dwSize> and <e DEVICEINSTANCE.guidInstance>
  3254. * have already been filled in.
  3255. *
  3256. * Secret convenience: <e DEVICEINSTANCE.guidProduct> is equal
  3257. * to <e DEVICEINSTANCE.guidInstance>.
  3258. *
  3259. *****************************************************************************/
  3260. STDMETHODIMP
  3261. CHid_GetDeviceInfo(PDICB pdcb, LPDIDEVICEINSTANCEW pdiW)
  3262. {
  3263. HRESULT hres;
  3264. PCHID this;
  3265. DIPROPINFO propi;
  3266. DIPROPSTRING dips;
  3267. EnterProcI(IDirectInputDeviceCallback::Hid::GetDeviceInfo,
  3268. (_ "pp", pdcb, pdiW));
  3269. /*
  3270. * This is an internal interface, so we can skimp on validation.
  3271. */
  3272. this = _thisPvNm(pdcb, dcb);
  3273. AssertF(IsValidSizeDIDEVICEINSTANCEW(pdiW->dwSize));
  3274. DICreateStaticGuid(&pdiW->guidProduct, this->ProductID, this->VendorID);
  3275. pdiW->dwDevType = this->dwDevType;
  3276. if(pdiW->dwSize >= cbX(DIDEVICEINSTANCE_DX5W))
  3277. {
  3278. pdiW->wUsagePage = this->caps.UsagePage;
  3279. pdiW->wUsage = this->caps.Usage;
  3280. }
  3281. propi.dwDevType = DIPH_DEVICE;
  3282. propi.iobj = 0xFFFFFFFF;
  3283. propi.pguid = DIPROP_PRODUCTNAME;
  3284. if(SUCCEEDED(hres = pdcb->lpVtbl->GetProperty(pdcb, &propi, &dips.diph)) )
  3285. {
  3286. lstrcpyW(pdiW->tszProductName, dips.wsz);
  3287. }
  3288. propi.pguid = DIPROP_INSTANCENAME;
  3289. if( FAILED(pdcb->lpVtbl->GetProperty(pdcb, &propi, &dips.diph)))
  3290. {
  3291. // Use Product Name
  3292. }
  3293. lstrcpyW(pdiW->tszInstanceName, dips.wsz);
  3294. if(pdiW->dwSize >= cbX(DIDEVICEINSTANCE_DX5W))
  3295. {
  3296. HKEY hkFF;
  3297. HRESULT hresFF;
  3298. /*
  3299. * If there is a force feedback driver, then fetch the driver CLSID
  3300. * as the FF GUID.
  3301. */
  3302. hresFF = CHid_GetFFConfigKey(pdcb, KEY_QUERY_VALUE, &hkFF);
  3303. if(SUCCEEDED(hresFF))
  3304. {
  3305. LONG lRc;
  3306. TCHAR tszClsid[ctchGuid];
  3307. lRc = RegQueryString(hkFF, TEXT("CLSID"), tszClsid, cA(tszClsid));
  3308. if(lRc == ERROR_SUCCESS &&
  3309. ParseGUID(&pdiW->guidFFDriver, tszClsid))
  3310. {
  3311. } else
  3312. {
  3313. ZeroX(pdiW->guidFFDriver);
  3314. }
  3315. RegCloseKey(hkFF);
  3316. }
  3317. }
  3318. ExitOleProcR();
  3319. return hres;
  3320. }
  3321. /*****************************************************************************
  3322. *
  3323. * @doc INTERNAL
  3324. *
  3325. * @method HRESULT | CHid | CreateEffect |
  3326. *
  3327. *
  3328. * Create an <i IDirectInputEffectDriver> interface.
  3329. *
  3330. * @parm LPDIRECTINPUTEFFECTSHEPHERD * | ppes |
  3331. *
  3332. * Receives the shepherd for the effect driver.
  3333. *
  3334. *****************************************************************************/
  3335. STDMETHODIMP
  3336. CHid_CreateEffect(PDICB pdcb, LPDIRECTINPUTEFFECTSHEPHERD *ppes)
  3337. {
  3338. HRESULT hres;
  3339. PCHID this;
  3340. HKEY hk;
  3341. EnterProcI(IDirectInputDeviceCallback::HID::CreateEffect, (_ "p", pdcb));
  3342. /*
  3343. * This is an internal interface, so we can skimp on validation.
  3344. */
  3345. this = _thisPvNm(pdcb, dcb);
  3346. hres = CHid_GetFFConfigKey(pdcb, KEY_QUERY_VALUE, &hk);
  3347. if(SUCCEEDED(hres))
  3348. {
  3349. DIHIDFFINITINFO init;
  3350. PHIDDEVICEINFO phdi;
  3351. hres = CEShep_New(hk, 0, &IID_IDirectInputEffectShepherd, ppes);
  3352. if(SUCCEEDED(hres))
  3353. {
  3354. #ifndef UNICODE
  3355. WCHAR wszPath[MAX_PATH];
  3356. #endif
  3357. init.dwSize = cbX(init);
  3358. #ifdef UNICODE
  3359. init.pwszDeviceInterface = this->ptszPath;
  3360. #else
  3361. init.pwszDeviceInterface = wszPath;
  3362. TToU(wszPath, cA(wszPath), this->ptszPath);
  3363. #endif
  3364. DllEnterCrit();
  3365. phdi = phdiFindHIDDeviceInterface(this->ptszPath);
  3366. if( phdi )
  3367. {
  3368. init.GuidInstance = phdi->guid;
  3369. } else
  3370. {
  3371. ZeroX(init.GuidInstance);
  3372. }
  3373. DllLeaveCrit();
  3374. hres = (*ppes)->lpVtbl->DeviceID((*ppes), this->idJoy, TRUE, &init);
  3375. if(SUCCEEDED(hres))
  3376. {
  3377. } else
  3378. {
  3379. Invoke_Release(ppes);
  3380. }
  3381. }
  3382. if (hk != NULL)
  3383. {
  3384. RegCloseKey(hk);
  3385. }
  3386. } else
  3387. {
  3388. hres = E_NOTIMPL;
  3389. *ppes = 0;
  3390. }
  3391. ExitOleProcPpvR(ppes);
  3392. return hres;
  3393. }
  3394. /*****************************************************************************
  3395. *
  3396. * @doc INTERNAL
  3397. *
  3398. * @method HRESULT | CHid | SendOutputReport |
  3399. *
  3400. * Actually send the report as an output report.
  3401. *
  3402. * @parm PHIDREPORTINFO | phri |
  3403. *
  3404. * The report being sent.
  3405. *
  3406. * @returns
  3407. *
  3408. * Returns a COM error code.
  3409. *
  3410. *****************************************************************************/
  3411. void CALLBACK
  3412. CHid_DummyCompletion(DWORD dwError, DWORD cbRead, LPOVERLAPPED po)
  3413. {
  3414. }
  3415. STDMETHODIMP
  3416. CHid_SendOutputReport(PCHID this, PHIDREPORTINFO phri)
  3417. {
  3418. HRESULT hres;
  3419. OVERLAPPED o;
  3420. AssertF(phri == &this->hriOut);
  3421. ZeroX(o);
  3422. /*
  3423. * Annoying API: Since this->hdev was opened
  3424. * as FILE_FLAG_OVERLAPPED, *all* I/O must be overlapped.
  3425. * So we simulate a synchronous I/O by issuing an
  3426. * overlapped I/O and waiting for the completion.
  3427. */
  3428. if(WriteFileEx(this->hdev, phri->pvReport,
  3429. phri->cbReport, &o, CHid_DummyCompletion))
  3430. {
  3431. do
  3432. {
  3433. SleepEx(INFINITE, TRUE);
  3434. } while(!HasOverlappedIoCompleted(&o));
  3435. if(phri->cbReport == o.InternalHigh)
  3436. {
  3437. hres = S_OK;
  3438. } else
  3439. {
  3440. RPF("SendDeviceData: Wrong HID output report size?");
  3441. hres = E_FAIL; /* Aigh! HID lied to me! */
  3442. }
  3443. } else
  3444. {
  3445. hres = hresLe(GetLastError());
  3446. /*
  3447. * Note, we have not broken the read loop so there is no need to
  3448. * force the device unaquired (though dinput.dll does).
  3449. *
  3450. * If this causes problems revert to the old behavior.
  3451. * CEm_ForceDeviceUnacquire(pemFromPvi(this->pvi)->ped, 0x0);
  3452. */
  3453. }
  3454. return hres;
  3455. }
  3456. /*****************************************************************************
  3457. *
  3458. * @doc INTERNAL
  3459. *
  3460. * @method HRESULT | CHid | SendFeatureReport |
  3461. *
  3462. * Actually send the report as an feature report.
  3463. *
  3464. * @parm PHIDREPORTINFO | phri |
  3465. *
  3466. * The report being sent.
  3467. *
  3468. * @returns
  3469. *
  3470. * Returns a COM error code.
  3471. *
  3472. *****************************************************************************/
  3473. STDMETHODIMP
  3474. CHid_SendFeatureReport(PCHID this, PHIDREPORTINFO phri)
  3475. {
  3476. HRESULT hres;
  3477. AssertF(phri == &this->hriFea);
  3478. if(HidD_SetFeature(this->hdev, phri->pvReport, phri->cbReport))
  3479. {
  3480. hres = S_OK;
  3481. } else
  3482. {
  3483. RPF("SendDeviceData: Unable to set HID feature");
  3484. hres = hresLe(GetLastError());
  3485. }
  3486. return hres;
  3487. }
  3488. /*****************************************************************************
  3489. *
  3490. * @doc INTERNAL
  3491. *
  3492. * @method HRESULT | CHid | SendDeviceData |
  3493. *
  3494. * Spew some data to the device.
  3495. *
  3496. * @parm DWORD | cbdod |
  3497. *
  3498. * Size of each object.
  3499. *
  3500. * @parm IN LPCDIDEVICEOBJECTDATA | rgdod |
  3501. *
  3502. * Array of <t DIDEVICEOBJECTDATA> structures.
  3503. *
  3504. * @parm INOUT LPDWORD | pdwInOut |
  3505. *
  3506. * On entry, number of items to send;
  3507. * on exit, number of items actually sent.
  3508. *
  3509. * @parm DWORD | fl |
  3510. *
  3511. * Flags.
  3512. *
  3513. * @returns
  3514. *
  3515. * Returns a COM error code. The following error codes are
  3516. * intended to be illustrative and not necessarily comprehensive.
  3517. *
  3518. * <c DI_OK> = <c S_OK>: The operation completed successfully.
  3519. *
  3520. * <c DIERR_REPORTFULL>: Too many items are set in the report.
  3521. * (More than can be sent to the device)
  3522. *
  3523. *****************************************************************************/
  3524. STDMETHODIMP
  3525. CHid_SendDeviceData(PDICB pdcb, DWORD cbdod, LPCDIDEVICEOBJECTDATA rgdod,
  3526. LPDWORD pdwInOut, DWORD fl)
  3527. {
  3528. HRESULT hres;
  3529. PCHID this;
  3530. DWORD dwIn, dw;
  3531. const BYTE * pbcod;
  3532. EnterProcI(IDirectInputDeviceCallback::Hid::SendDeviceData,
  3533. (_ "xpux", cbdod, pdcb, *pdwInOut, fl));
  3534. /*
  3535. * This is an internal interface, so we can skimp on validation.
  3536. */
  3537. this = _thisPvNm(pdcb, dcb);
  3538. dwIn = *pdwInOut;
  3539. *pdwInOut = 0;
  3540. if(fl & DISDD_CONTINUE)
  3541. {
  3542. } else
  3543. {
  3544. CHid_ResetDeviceData(this, &this->hriOut, HidP_Output);
  3545. CHid_ResetDeviceData(this, &this->hriFea, HidP_Feature);
  3546. }
  3547. for(dw = 0, pbcod = (const BYTE*)rgdod; dw < dwIn; dw++)
  3548. {
  3549. DWORD dwType = ((LPDIDEVICEOBJECTDATA)pbcod)->dwOfs;
  3550. UINT uiObj = CHid_ObjFromType(this, dwType);
  3551. if(uiObj < this->df.dwNumObjs &&
  3552. DIDFT_FINDMATCH(this->df.rgodf[uiObj].dwType, dwType))
  3553. {
  3554. hres = CHid_AddDeviceData(this, uiObj, ((LPDIDEVICEOBJECTDATA)pbcod)->dwData);
  3555. if(FAILED(hres))
  3556. {
  3557. *pdwInOut = dw;
  3558. goto done;
  3559. }
  3560. } else
  3561. {
  3562. hres = E_INVALIDARG;
  3563. goto done;
  3564. }
  3565. pbcod += cbdod;
  3566. }
  3567. /*
  3568. * All the items made it into the buffer.
  3569. */
  3570. *pdwInOut = dw;
  3571. /*
  3572. * Now send it all out.
  3573. */
  3574. if(SUCCEEDED(hres = CHid_SendHIDReport(this, &this->hriOut, HidP_Output,
  3575. CHid_SendOutputReport)) &&
  3576. SUCCEEDED(hres = CHid_SendHIDReport(this, &this->hriFea, HidP_Feature,
  3577. CHid_SendFeatureReport)))
  3578. {
  3579. }
  3580. done:;
  3581. ExitOleProcR();
  3582. return hres;
  3583. }
  3584. /*****************************************************************************
  3585. *
  3586. * @doc INTERNAL
  3587. *
  3588. * @method HRESULT | CHid | Poll |
  3589. *
  3590. * Read any polled input and features to see what's there.
  3591. *
  3592. * @returns
  3593. *
  3594. * <c S_OK> if we pinged okay.
  3595. * <c S_FALSE> doesn't require polling
  3596. * <c DIERR_UNPLUGGED> the device requires polling and is unplugged
  3597. * Other errors returned from HID are possible for a polled device.
  3598. *
  3599. *****************************************************************************/
  3600. STDMETHODIMP
  3601. CHid_Poll(PDICB pdcb)
  3602. {
  3603. //Prefix: 45082
  3604. HRESULT hres = S_FALSE; //We need use S_FALSE as default. See manbug 31874.
  3605. PCHID this;
  3606. EnterProcI(IDirectInputDeviceCallback::Hid::Poll, (_ "p", pdcb));
  3607. /*
  3608. * This is an internal interface, so we can skimp on validation.
  3609. */
  3610. this = _thisPvNm(pdcb, dcb);
  3611. if( this->IsPolledInput )
  3612. {
  3613. hres = DIERR_UNPLUGGED;
  3614. if(ReadFileEx(this->hdev, this->hriIn.pvReport,
  3615. this->hriIn.cbReport, &this->o, CHid_DummyCompletion))
  3616. {
  3617. do
  3618. {
  3619. SleepEx( INFINITE, TRUE);
  3620. } while(!HasOverlappedIoCompleted(&this->o));
  3621. if(this->hriIn.cbReport == this->o.InternalHigh)
  3622. {
  3623. NTSTATUS stat;
  3624. CopyMemory(this->pvStage, this->pvPhys, this->cbPhys);
  3625. stat = CHid_ParseData(this, HidP_Input, &this->hriIn);
  3626. if(SUCCEEDED(stat))
  3627. {
  3628. CEm_AddState(&this->ed, this->pvStage, GetTickCount());
  3629. this->pvi->fl &= ~VIFL_UNPLUGGED;
  3630. hres = S_OK;
  3631. } else
  3632. {
  3633. RPF( "CHid_ParseData failed in Poll, status = 0x%08x", stat );
  3634. hres = stat;
  3635. }
  3636. }
  3637. }
  3638. if( FAILED(hres) )
  3639. {
  3640. /*
  3641. * Note, we have not broken the read loop so there is no need to
  3642. * force the device unaquired (though dinput.dll does).
  3643. *
  3644. * If this causes problems revert to the old behavior.
  3645. * CEm_ForceDeviceUnacquire(pemFromPvi(this->pvi)->ped, 0x0);
  3646. */
  3647. hres = DIERR_UNPLUGGED;
  3648. this->pvi->fl |= VIFL_UNPLUGGED;
  3649. }
  3650. }
  3651. if( this->hriFea.cbReport )
  3652. {
  3653. UINT uReport;
  3654. /*
  3655. * We should never get here unless there really are any
  3656. * features that need to be polled.
  3657. */
  3658. AssertF(this->hriFea.cbReport);
  3659. AssertF(this->hriFea.pvReport);
  3660. /*
  3661. * Read the new features and parse/process them.
  3662. *
  3663. * Notice that we read the features into the same buffer
  3664. * that we log them into. That's okay; the "live" parts
  3665. * of the two buffers never actually overlap.
  3666. */
  3667. for( uReport = 0x0; uReport < this->wMaxReportId[HidP_Feature]; uReport++ )
  3668. {
  3669. if( *(this->pEnableReportId[HidP_Feature] + uReport ) == TRUE )
  3670. {
  3671. *((UCHAR*)(this->hriFea.pvReport)) = (UCHAR)uReport;
  3672. /*
  3673. * Wipe out all the old goo because we're taking over.
  3674. */
  3675. CHid_ResetDeviceData(this, &this->hriFea, HidP_Feature);
  3676. if(HidD_GetFeature(this->hdev, this->hriFea.pvReport,
  3677. this->hriFea.cbReport))
  3678. {
  3679. NTSTATUS stat;
  3680. stat = CHid_ParseData(this, HidP_Feature, &this->hriFea);
  3681. AssertF(SUCCEEDED(stat));
  3682. if(SUCCEEDED(stat))
  3683. {
  3684. CEm_AddState(&this->ed, this->pvStage, GetTickCount());
  3685. }
  3686. hres = stat;
  3687. } else
  3688. {
  3689. RPF("CHid_Poll: Unable to read HID features (ReportID%d) LastError(0x%x)", uReport, GetLastError() );
  3690. hres = hresLe(GetLastError());
  3691. }
  3692. }
  3693. }
  3694. }
  3695. ExitOleProcR();
  3696. return hres;
  3697. }
  3698. /*****************************************************************************
  3699. *
  3700. * CHid_New (constructor)
  3701. *
  3702. * Fail the create if we can't open the device.
  3703. *
  3704. *****************************************************************************/
  3705. STDMETHODIMP
  3706. CHid_New(PUNK punkOuter, REFGUID rguid, RIID riid, PPV ppvObj)
  3707. {
  3708. HRESULT hres;
  3709. EnterProcI(IDirectInputDeviceCallback::Hid::<constructor>,
  3710. (_ "Gp", riid, ppvObj));
  3711. hres = Common_NewRiid(CHid, punkOuter, riid, ppvObj);
  3712. if(SUCCEEDED(hres))
  3713. {
  3714. /* Must use _thisPv in case of aggregation */
  3715. PCHID this = _thisPv(*ppvObj);
  3716. if(SUCCEEDED(hres = CHid_Init(this, rguid)))
  3717. {
  3718. } else
  3719. {
  3720. Invoke_Release(ppvObj);
  3721. }
  3722. }
  3723. ExitOleProcPpvR(ppvObj);
  3724. return hres;
  3725. }
  3726. /*****************************************************************************
  3727. *
  3728. * @doc INTERNAL
  3729. *
  3730. * @method HRESULT | CHid | SetDIData |
  3731. *
  3732. * Set DirectInput version and apphack data from CDIDev *.
  3733. *
  3734. * @parm DWORD | dwVer |
  3735. *
  3736. * DirectInput version
  3737. *
  3738. * @parm LPVOID | lpdihacks |
  3739. *
  3740. * AppHack data
  3741. *
  3742. * @returns
  3743. *
  3744. * <c E_NOTIMPL> because we don't support usages.
  3745. *
  3746. *****************************************************************************/
  3747. STDMETHODIMP
  3748. CHid_SetDIData(PDICB pdcb, DWORD dwVer, LPVOID lpdihacks)
  3749. {
  3750. HRESULT hres;
  3751. PCHID this;
  3752. EnterProcI(IDirectInputDeviceCallback::Hid::SetDIData,
  3753. (_ "pup", pdcb, dwVer, lpdihacks));
  3754. /*
  3755. * This is an internal interface, so we can skimp on validation.
  3756. */
  3757. this = _thisPvNm(pdcb, dcb);
  3758. this->dwVersion = dwVer;
  3759. CopyMemory(&this->diHacks, (LPDIAPPHACKS)lpdihacks, sizeof(this->diHacks));
  3760. hres = S_OK;
  3761. ExitOleProcR();
  3762. return hres;
  3763. }
  3764. /*****************************************************************************
  3765. *
  3766. * @doc INTERNAL
  3767. *
  3768. * @method HRESULT | CHid | BuildDefaultActionMap |
  3769. *
  3770. * Generate default mappings for the objects on this device.
  3771. *
  3772. * @parm LPDIACTIONFORMATW | pActionFormat |
  3773. *
  3774. * Actions to map.
  3775. *
  3776. * @parm DWORD | dwFlags |
  3777. *
  3778. * Flags used to indicate mapping preferences.
  3779. *
  3780. * @parm REFGUID | guidInst |
  3781. *
  3782. * Device instance GUID.
  3783. *
  3784. * @returns
  3785. *
  3786. * <c E_NOTIMPL>
  3787. *
  3788. *****************************************************************************/
  3789. STDMETHODIMP
  3790. CHid_BuildDefaultActionMap
  3791. (
  3792. PDICB pdcb,
  3793. LPDIACTIONFORMATW paf,
  3794. DWORD dwFlags,
  3795. REFGUID guidInst
  3796. )
  3797. {
  3798. HRESULT hres;
  3799. PCHID this;
  3800. DWORD dwPhysicalGenre;
  3801. /*
  3802. * This is an internal interface, so we can skimp on validation.
  3803. */
  3804. EnterProcI(IDirectInputDeviceCallback::Hid::BuildDefaultActionMap,
  3805. (_ "ppxG", pdcb, paf, dwFlags, guidInst));
  3806. this = _thisPvNm(pdcb, dcb);
  3807. switch( GET_DIDEVICE_TYPE( this->dwDevType ) )
  3808. {
  3809. case DI8DEVTYPE_DEVICE:
  3810. hres = S_FALSE;
  3811. goto ExitBuildDefaultActionMap;
  3812. case DI8DEVTYPE_MOUSE:
  3813. dwPhysicalGenre = DIPHYSICAL_MOUSE;
  3814. break;
  3815. case DI8DEVTYPE_KEYBOARD:
  3816. dwPhysicalGenre = DIPHYSICAL_KEYBOARD;
  3817. break;
  3818. default:
  3819. dwPhysicalGenre = 0;
  3820. break;
  3821. }
  3822. if( dwPhysicalGenre )
  3823. {
  3824. hres = CMap_BuildDefaultSysActionMap( paf, dwFlags, dwPhysicalGenre,
  3825. guidInst, &this->df, 0 /* HID mice buttons start at instance zero */ );
  3826. }
  3827. else
  3828. {
  3829. PDIDOBJDEFSEM rgObjSem;
  3830. if( SUCCEEDED( hres = AllocCbPpv(cbCxX(
  3831. (this->dwAxes + this->dwPOVs + this->dwButtons ), DIDOBJDEFSEM),
  3832. &rgObjSem) ) )
  3833. {
  3834. PDIDOBJDEFSEM pAxis;
  3835. PDIDOBJDEFSEM pPOV;
  3836. PDIDOBJDEFSEM pButton;
  3837. BYTE rgbIndex[DISEM_FLAGS_GET(DISEM_FLAGS_S)];
  3838. UINT ObjIdx;
  3839. pAxis = rgObjSem;
  3840. pPOV = &pAxis[this->dwAxes];
  3841. pButton = &pPOV[this->dwPOVs];
  3842. ZeroMemory( rgbIndex, cbX(rgbIndex) );
  3843. for( ObjIdx = 0; ObjIdx < this->df.dwNumObjs; ObjIdx++ )
  3844. {
  3845. if( this->df.rgodf[ObjIdx].dwType & DIDFT_NODATA )
  3846. {
  3847. continue;
  3848. }
  3849. if( this->df.rgodf[ObjIdx].dwType & DIDFT_AXIS )
  3850. {
  3851. PHIDGROUPCAPS pcaps;
  3852. pcaps = this->rghoc[ObjIdx].pcaps;
  3853. pAxis->dwID = this->df.rgodf[ObjIdx].dwType;
  3854. if( this->rgbaxissemflags[DIDFT_GETINSTANCE( pAxis->dwID )] )
  3855. {
  3856. pAxis->dwSemantic = DISEM_TYPE_AXIS | DISEM_FLAGS_SET ( this->rgbaxissemflags[DIDFT_GETINSTANCE( pAxis->dwID )] );
  3857. /*
  3858. * The index is zero so that a real index can be ORed in.
  3859. * Also, assert that the rgbIndex is big enough and
  3860. * that subtracting 1 won't give a negative index!
  3861. */
  3862. AssertF( DISEM_INDEX_GET(pAxis->dwSemantic) == 0 );
  3863. AssertF( DISEM_FLAGS_GET(pAxis->dwSemantic) > 0 );
  3864. AssertF( DISEM_FLAGS_GET(pAxis->dwSemantic) <= DISEM_FLAGS_GET(DISEM_FLAGS_S) );
  3865. CAssertF( DISEM_FLAGS_GET(DISEM_FLAGS_X) == 1 );
  3866. pAxis->dwSemantic |= DISEM_INDEX_SET( rgbIndex[DISEM_FLAGS_GET(pAxis->dwSemantic)-1]++ );
  3867. }
  3868. else
  3869. {
  3870. /*
  3871. * If the axis has no semantic flags, it is
  3872. * unrecognized so short cut the above to produce
  3873. * a plain axis that can be matched only with "ANY".
  3874. */
  3875. pAxis->dwSemantic = DISEM_TYPE_AXIS;
  3876. }
  3877. if( !pcaps->IsAbsolute )
  3878. {
  3879. pAxis->dwSemantic |= DIAXIS_RELATIVE;
  3880. }
  3881. pAxis++;
  3882. }
  3883. else if( this->df.rgodf[ObjIdx].dwType & DIDFT_POV )
  3884. {
  3885. pPOV->dwID = this->df.rgodf[ObjIdx].dwType;
  3886. pPOV->dwSemantic = DISEM_TYPE_POV;
  3887. pPOV++;
  3888. }
  3889. else if( this->df.rgodf[ObjIdx].dwType & DIDFT_BUTTON )
  3890. {
  3891. pButton->dwID = this->df.rgodf[ObjIdx].dwType;
  3892. pButton->dwSemantic = DISEM_TYPE_BUTTON;
  3893. pButton++;
  3894. }
  3895. }
  3896. AssertF( pAxis == &rgObjSem[this->dwAxes] );
  3897. AssertF( pPOV == &rgObjSem[this->dwAxes + this->dwPOVs] );
  3898. AssertF( pButton == &rgObjSem[this->dwAxes + this->dwPOVs + this->dwButtons] );
  3899. hres = CMap_BuildDefaultDevActionMap( paf, dwFlags, guidInst, rgObjSem,
  3900. this->dwAxes, this->dwPOVs, this->dwButtons );
  3901. FreePv( rgObjSem );
  3902. }
  3903. }
  3904. ExitBuildDefaultActionMap:;
  3905. ExitOleProcR();
  3906. return hres;
  3907. }
  3908. /*****************************************************************************
  3909. *
  3910. * The long-awaited vtbls and templates
  3911. *
  3912. *****************************************************************************/
  3913. #pragma BEGIN_CONST_DATA
  3914. #define CHid_Signature 0x20444948 /* "HID " */
  3915. Primary_Interface_Begin(CHid, IDirectInputDeviceCallback)
  3916. CHid_GetInstance,
  3917. CDefDcb_GetVersions,
  3918. CHid_GetDataFormat,
  3919. CHid_GetObjectInfo,
  3920. CHid_GetCapabilities,
  3921. CHid_Acquire,
  3922. CHid_Unacquire,
  3923. CHid_GetDeviceState,
  3924. CHid_GetDeviceInfo,
  3925. CHid_GetProperty,
  3926. CHid_SetProperty,
  3927. CDefDcb_SetEventNotification,
  3928. #ifdef WINNT
  3929. CHid_SetCooperativeLevel,
  3930. #else
  3931. CDefDcb_SetCooperativeLevel,
  3932. #endif
  3933. CHid_RunControlPanel,
  3934. CDefDcb_CookDeviceData,
  3935. CHid_CreateEffect,
  3936. CHid_GetFFConfigKey,
  3937. CHid_SendDeviceData,
  3938. CHid_Poll,
  3939. CHid_GetUsage,
  3940. CHid_MapUsage,
  3941. CHid_SetDIData,
  3942. CHid_BuildDefaultActionMap,
  3943. Primary_Interface_End(CHid, IDirectInputDeviceCallback)