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

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