Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

3986 lines
124 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. this->fEnableInputReport = FALSE;
  577. dwFlags2 &= ~JOYTYPE_ENABLEINPUTREPORT;
  578. hres = JoyReg_OpenPropKey(wszType, MAXIMUM_ALLOWED, REG_OPTION_NON_VOLATILE, &hkProp);
  579. if (SUCCEEDED(hres))
  580. {
  581. hres = JoyReg_SetValue( hkProp, REGSTR_VAL_FLAGS2,
  582. REG_BINARY, (PV)&dwFlags2, cbX( dwFlags2 ) );
  583. RegCloseKey(hkProp);
  584. }
  585. break;
  586. }
  587. RPF("CHid_InitParse: Unable to read HID input report LastError(0x%x)", GetLastError() );
  588. }
  589. }
  590. }
  591. CloseHandle(h);
  592. /* Please finish for me */
  593. hres = S_FALSE;
  594. } else
  595. {
  596. hres = DIERR_UNPLUGGED;
  597. }
  598. ExitOleProcR();
  599. return hres;
  600. }
  601. /*****************************************************************************
  602. *
  603. * @doc INTERNAL
  604. *
  605. * @method HRESULT | CHid | GetInstance |
  606. *
  607. * Obtains the DirectInput instance handle.
  608. *
  609. * @parm OUT PPV | ppvi |
  610. *
  611. * Receives the instance handle.
  612. *
  613. *****************************************************************************/
  614. STDMETHODIMP
  615. CHid_GetInstance(PDICB pdcb, PPV ppvi)
  616. {
  617. HRESULT hres;
  618. PCHID this;
  619. EnterProcI(IDirectInputDeviceCallback::Hid::GetInstance, (_ "p", pdcb));
  620. /*
  621. * This is an internal interface, so we can skimp on validation.
  622. */
  623. this = _thisPvNm(pdcb, dcb);
  624. AssertF(this->pvi);
  625. *ppvi = (PV)this->pvi;
  626. hres = S_OK;
  627. ExitOleProcPpvR(ppvi);
  628. return hres;
  629. }
  630. /*****************************************************************************
  631. *
  632. * @doc INTERNAL
  633. *
  634. * @method HRESULT | CHid | GetDataFormat |
  635. *
  636. * Obtains the device's preferred data format.
  637. *
  638. * @parm OUT LPDIDEVICEFORMAT * | ppdf |
  639. *
  640. * <t LPDIDEVICEFORMAT> to receive pointer to device format.
  641. *
  642. * @returns
  643. *
  644. * Returns a COM error code. The following error codes are
  645. * intended to be illustrative and not necessarily comprehensive.
  646. *
  647. * <c DI_OK> = <c S_OK>: The operation completed successfully.
  648. *
  649. * <c DIERR_INVALIDPARAM> = <c E_INVALIDARG>: The
  650. * <p lpmdr> parameter is not a valid pointer.
  651. *
  652. *****************************************************************************/
  653. STDMETHODIMP
  654. CHid_GetDataFormat(PDICB pdcb, LPDIDATAFORMAT *ppdf)
  655. {
  656. HRESULT hres;
  657. PCHID this;
  658. EnterProcI(IDirectInputDeviceCallback::Hid::GetDataFormat,
  659. (_ "p", pdcb));
  660. /*
  661. * This is an internal interface, so we can skimp on validation.
  662. */
  663. this = _thisPvNm(pdcb, dcb);
  664. *ppdf = &this->df;
  665. hres = S_OK;
  666. ExitOleProcPpvR(ppdf);
  667. return hres;
  668. }
  669. /*****************************************************************************
  670. *
  671. * @doc INTERNAL
  672. *
  673. * @func HRESULT | DIHid_GetRegistryProperty |
  674. *
  675. * @parm LPTSTR | ptszId |
  676. *
  677. * Device Instance ID.
  678. *
  679. * @parm DWORD | dwProperty |
  680. *
  681. * The property being queried.
  682. *
  683. * @parm LPDIPROPHEADER | diph |
  684. *
  685. * Property data to be set.
  686. *
  687. *****************************************************************************/
  688. HRESULT INTERNAL
  689. DIHid_GetParentRegistryProperty(LPTSTR ptszId, DWORD dwProperty, LPDIPROPHEADER pdiph, BOOL bGrandParent)
  690. {
  691. HDEVINFO hdev;
  692. LPDIPROPSTRING pstr = (PV)pdiph;
  693. TCHAR tsz[MAX_PATH];
  694. HRESULT hres;
  695. ZeroX(tsz);
  696. hdev = SetupDiCreateDeviceInfoList(NULL, NULL);
  697. if(hdev != INVALID_HANDLE_VALUE)
  698. {
  699. SP_DEVINFO_DATA dinf;
  700. /*
  701. * For the instance name, use the friendly name if possible.
  702. * Else, use the device description.
  703. */
  704. dinf.cbSize = cbX(SP_DEVINFO_DATA);
  705. if(SetupDiOpenDeviceInfo(hdev, ptszId, NULL, 0, &dinf))
  706. {
  707. DEVINST DevInst;
  708. CONFIGRET cr;
  709. if( ( cr = CM_Get_Parent(&DevInst, dinf.DevInst, 0x0)) == CR_SUCCESS )
  710. {
  711. ULONG ulLength;
  712. CAssertF( SPDRP_DEVICEDESC +1 == CM_DRP_DEVICEDESC );
  713. CAssertF( SPDRP_FRIENDLYNAME +1 == CM_DRP_FRIENDLYNAME );
  714. if(bGrandParent)
  715. {
  716. cr = CM_Get_Parent(&DevInst, DevInst, 0x0);
  717. if( cr != CR_SUCCESS )
  718. {
  719. // No GrandParent ??
  720. }
  721. }
  722. ulLength = MAX_PATH * cbX(TCHAR);
  723. if( cr == CR_SUCCESS &&
  724. ( cr = CM_Get_DevNode_Registry_Property(
  725. DevInst,
  726. dwProperty+1,
  727. NULL,
  728. tsz,
  729. &ulLength,
  730. 0x0 ) ) == CR_SUCCESS )
  731. {
  732. // Success
  733. hres = S_OK;
  734. #ifdef UNICODE
  735. lstrcpyW(pstr->wsz, tsz);
  736. #else
  737. TToU(pstr->wsz, MAX_PATH, tsz);
  738. #endif
  739. } else
  740. {
  741. SquirtSqflPtszV(sqfl | sqflVerbose,
  742. TEXT("CM_Get_DevNode_Registry_Property FAILED") );
  743. hres = E_FAIL;
  744. }
  745. } else
  746. {
  747. SquirtSqflPtszV(sqfl | sqflVerbose,
  748. TEXT("CM_Get_Parent FAILED") );
  749. hres = E_FAIL;
  750. }
  751. }
  752. SetupDiDestroyDeviceInfoList(hdev);
  753. } else
  754. {
  755. hres = E_FAIL;
  756. }
  757. return hres;
  758. }
  759. HRESULT EXTERNAL
  760. DIHid_GetRegistryProperty(LPTSTR ptszId, DWORD dwProperty, LPDIPROPHEADER pdiph)
  761. {
  762. HDEVINFO hdev;
  763. LPDIPROPSTRING pstr = (PV)pdiph;
  764. TCHAR tsz[MAX_PATH];
  765. HRESULT hres;
  766. ZeroX(tsz);
  767. hdev = SetupDiCreateDeviceInfoList(NULL, NULL);
  768. if(hdev != INVALID_HANDLE_VALUE)
  769. {
  770. SP_DEVINFO_DATA dinf;
  771. /*
  772. * For the instance name, use the friendly name if possible.
  773. * Else, use the device description.
  774. */
  775. dinf.cbSize = cbX(SP_DEVINFO_DATA);
  776. if(SetupDiOpenDeviceInfo(hdev, ptszId, NULL, 0, &dinf))
  777. {
  778. if(SetupDiGetDeviceRegistryProperty(hdev, &dinf, dwProperty, NULL,
  779. (LPBYTE)tsz, MAX_PATH, NULL) )
  780. {
  781. hres = S_OK;
  782. #ifdef UNICODE
  783. lstrcpyW(pstr->wsz, tsz);
  784. #else
  785. TToU(pstr->wsz, MAX_PATH, tsz);
  786. #endif
  787. } else
  788. {
  789. hres = E_FAIL;
  790. }
  791. } else
  792. {
  793. hres = E_FAIL;
  794. }
  795. SetupDiDestroyDeviceInfoList(hdev);
  796. } else
  797. {
  798. hres = E_FAIL;
  799. }
  800. return hres;
  801. }
  802. /*****************************************************************************
  803. *
  804. * @doc INTERNAL
  805. *
  806. * @method void | CHid | GetGuidAndPath |
  807. *
  808. * Get a Hid device's class GUID (namely, the HID guid)
  809. * and device interface (path).
  810. *
  811. * @parm PCHID | this |
  812. *
  813. * The Hid object.
  814. *
  815. * @parm LPDIPROPHEADER | pdiph |
  816. *
  817. * Structure to receive property value.
  818. *
  819. *****************************************************************************/
  820. HRESULT INTERNAL
  821. CHid_GetGuidAndPath(PCHID this, LPDIPROPHEADER pdiph)
  822. {
  823. HRESULT hres;
  824. LPDIPROPGUIDANDPATH pgp = (PV)pdiph;
  825. pgp->guidClass = GUID_HIDClass;
  826. TToU(pgp->wszPath, cA(pgp->wszPath), this->ptszPath);
  827. hres = S_OK;
  828. return hres;
  829. }
  830. /*****************************************************************************
  831. *
  832. * @doc INTERNAL
  833. *
  834. * @func BOOL | fHasSpecificHardwareMatch |
  835. *
  836. * Find out from SetupAPI whether the device was matched with a
  837. * specific hardware ID match or generic match.
  838. * A specific match should have caused a device description to be
  839. * installed which is likely to be at least as good as what HID could
  840. * get from a product string in firmware. (a. because it's easier to
  841. * update an INF after release than firmware; b. because HID can only
  842. * get us an English string.) Generic matches on the other hand are,
  843. * by definition, all the same so cannot be used to tell two devices
  844. * apart.
  845. *
  846. * @parm LPTSTR ptszId
  847. *
  848. * Device Instance ID.
  849. *
  850. * @returns
  851. * <c TRUE> if the device was installed using a specific match.
  852. * <c FALSE> if it was not or if installation info was unobtainable.
  853. *
  854. * @comm
  855. * This is used on Win2k for game controllers and Win9x for mice and
  856. * keyboards. Win2k we can't read HID mice and keyboards and on
  857. * Win9x VJoyD should always create device names before DInput.dll.
  858. * On Win9x this is less of a big deal for game controllers because
  859. * IHVs are accoustomed to adding their display name to
  860. * MediaProperties.
  861. *
  862. *****************************************************************************/
  863. BOOL fHasSpecificHardwareMatch( LPTSTR ptszId )
  864. {
  865. HDEVINFO hInfo;
  866. BOOL fRc = FALSE;
  867. EnterProcI(fHasSpecificHardwareMatch,(_ "s", ptszId));
  868. hInfo = SetupDiCreateDeviceInfoList(NULL, NULL);
  869. if( hInfo != INVALID_HANDLE_VALUE )
  870. {
  871. SP_DEVINFO_DATA dinf;
  872. dinf.cbSize = cbX(SP_DEVINFO_DATA);
  873. if( SetupDiOpenDeviceInfo(hInfo, ptszId, NULL, 0, &dinf) )
  874. {
  875. CONFIGRET cr;
  876. DEVINST DevInst;
  877. cr = CM_Get_Parent( &DevInst, dinf.DevInst, 0x0 );
  878. if( cr == CR_SUCCESS )
  879. {
  880. TCHAR tszDevInst[MAX_PATH];
  881. cr = CM_Get_Device_ID( DevInst, (DEVINSTID)tszDevInst, MAX_PATH, 0 );
  882. if( cr == CR_SUCCESS )
  883. {
  884. if( SetupDiOpenDeviceInfo(hInfo, tszDevInst, NULL, 0, &dinf) )
  885. {
  886. HKEY hkDrv;
  887. hkDrv = SetupDiOpenDevRegKey( hInfo, &dinf, DICS_FLAG_GLOBAL, 0,
  888. DIREG_DRV, MAXIMUM_ALLOWED );
  889. if( hkDrv != INVALID_HANDLE_VALUE )
  890. {
  891. PTCHAR tszHardwareID = NULL;
  892. PTCHAR tszMatchingID = NULL;
  893. ULONG ulLength = 0;
  894. cr = CM_Get_DevNode_Registry_Property(DevInst,
  895. CM_DRP_HARDWAREID,
  896. NULL,
  897. NULL,
  898. &ulLength,
  899. 0x0 );
  900. /*
  901. * Win2k returns CR_BUFFER_SMALL but
  902. * Win9x returns CR_SUCCESS so allow both.
  903. */
  904. if( ( ( cr == CR_BUFFER_SMALL ) || ( cr == CR_SUCCESS ) )
  905. && ulLength )
  906. {
  907. #ifndef WINNT
  908. /*
  909. * Need to allocate extra for terminator on Win9x
  910. */
  911. ulLength++;
  912. #endif
  913. if( SUCCEEDED( AllocCbPpv( ulLength + ( MAX_PATH * cbX(tszMatchingID[0]) ), &tszMatchingID ) ) )
  914. {
  915. cr = CM_Get_DevNode_Registry_Property(DevInst,
  916. CM_DRP_HARDWAREID,
  917. NULL,
  918. (PBYTE)&tszMatchingID[MAX_PATH],
  919. &ulLength,
  920. 0x0 );
  921. if( cr == CR_SUCCESS )
  922. {
  923. tszHardwareID = &tszMatchingID[MAX_PATH];
  924. }
  925. else
  926. {
  927. SquirtSqflPtszV(sqfl | sqflError,
  928. TEXT("CR error %d getting HW ID"), cr );
  929. }
  930. }
  931. else
  932. {
  933. SquirtSqflPtszV(sqfl | sqflError,
  934. TEXT("No memory requesting %d bytes for HW ID"), ulLength );
  935. }
  936. }
  937. else
  938. {
  939. SquirtSqflPtszV(sqfl | sqflError,
  940. TEXT("Unexpected CR error %d getting HW ID size"), cr );
  941. }
  942. if( tszHardwareID )
  943. {
  944. ulLength = MAX_PATH * cbX(tszMatchingID[0]);
  945. cr = RegQueryValueEx( hkDrv, REGSTR_VAL_MATCHINGDEVID, 0, 0, (PBYTE)tszMatchingID, &ulLength );
  946. if( CR_SUCCESS == cr )
  947. {
  948. while( ulLength = lstrlen( tszHardwareID ) )
  949. {
  950. if( !lstrcmpi( tszHardwareID, tszMatchingID ) )
  951. {
  952. fRc = TRUE;
  953. break;
  954. }
  955. tszHardwareID += ulLength + 1;
  956. }
  957. }
  958. else
  959. {
  960. SquirtSqflPtszV(sqfl | sqflError,
  961. TEXT("No matching ID!, cr = %d"), cr );
  962. }
  963. }
  964. if( tszMatchingID )
  965. {
  966. FreePv( tszMatchingID );
  967. }
  968. RegCloseKey( hkDrv );
  969. }
  970. else
  971. {
  972. SquirtSqflPtszV(sqfl | sqflError,
  973. TEXT("SetupDiOpenDevRegKey failed, le = %d"), GetLastError() );
  974. }
  975. }
  976. else
  977. {
  978. SquirtSqflPtszV(sqfl | sqflError,
  979. TEXT("SetupDiOpenDeviceInfo failed for %S (parent), le = %d"),
  980. tszDevInst, GetLastError() );
  981. }
  982. }
  983. else
  984. {
  985. SquirtSqflPtszV(sqfl | sqflError,
  986. TEXT("CM_Get_Device_ID FAILED %d"), cr );
  987. }
  988. }
  989. else
  990. {
  991. SquirtSqflPtszV(sqfl | sqflError,
  992. TEXT("CM_Get_Parent FAILED %d"), cr );
  993. }
  994. }
  995. else
  996. {
  997. SquirtSqflPtszV(sqfl | sqflError,
  998. TEXT("SetupDiOpenDeviceInfo failed for %S (child), le = %d"),
  999. ptszId, GetLastError() );
  1000. }
  1001. SetupDiDestroyDeviceInfoList(hInfo);
  1002. }
  1003. else
  1004. {
  1005. SquirtSqflPtszV(sqfl | sqflError,
  1006. TEXT("SetupDiCreateDeviceInfoList failed, le = %d"), GetLastError() );
  1007. }
  1008. ExitProc();
  1009. return fRc;
  1010. }
  1011. /*****************************************************************************
  1012. *
  1013. * @doc INTERNAL
  1014. *
  1015. * @func BOOL | fGetProductStringFromDevice |
  1016. *
  1017. * Try getting the product name from HID.
  1018. * If the device has one of these, this is what is displayed
  1019. * when the device is initially recognized. Unfortunately
  1020. * this name does not land up in the friendly name registry
  1021. * entry so in case this gets fixed we go directly to HID.
  1022. *
  1023. * @parm PCHID | this |
  1024. *
  1025. * The Hid object.
  1026. *
  1027. * @parm PWCHAR | wszBuffer |
  1028. *
  1029. * Where to put the product string if found.
  1030. *
  1031. * @parm ULONG | ulBufferLen |
  1032. *
  1033. * How big the string buffer is in bytes
  1034. *
  1035. * @returns
  1036. * <c TRUE> if a string has been placed in the buffer
  1037. * <c FALSE> if no string was retrieved
  1038. *
  1039. *****************************************************************************/
  1040. BOOL fGetProductStringFromDevice
  1041. (
  1042. PCHID this,
  1043. PWCHAR wszBuffer,
  1044. ULONG ulBufferLen
  1045. )
  1046. {
  1047. BOOL fRc;
  1048. /*
  1049. * If we already have a handle open (device is acquired), use
  1050. * it, otherwise open one just for now.
  1051. */
  1052. if( this->hdev != INVALID_HANDLE_VALUE )
  1053. {
  1054. fRc = HidD_GetProductString( this->hdev, wszBuffer, ulBufferLen );
  1055. }
  1056. else
  1057. {
  1058. HANDLE hdev;
  1059. hdev = CHid_OpenDevicePath(this, FILE_FLAG_OVERLAPPED);
  1060. if(hdev != INVALID_HANDLE_VALUE)
  1061. {
  1062. wszBuffer[0] = 0;
  1063. fRc = HidD_GetProductString( hdev, wszBuffer, ulBufferLen );
  1064. fRc = (fRc)?(wszBuffer[0] != 0):FALSE;
  1065. CloseHandle(hdev);
  1066. }
  1067. else
  1068. {
  1069. fRc = FALSE;
  1070. }
  1071. }
  1072. return fRc;
  1073. }
  1074. /*****************************************************************************
  1075. *
  1076. * @doc INTERNAL
  1077. *
  1078. * @method HRESULT | CHid | GetProperty |
  1079. *
  1080. * Get a Hid device property.
  1081. *
  1082. * @parm PCHID | this |
  1083. *
  1084. * The Hid object.
  1085. *
  1086. * @parm IN LPCDIPROPINFO | ppropi |
  1087. *
  1088. * Information describing the property being retrieved.
  1089. *
  1090. * @parm LPDIPROPHEADER | pdiph |
  1091. *
  1092. * Structure to receive property value.
  1093. *
  1094. * @returns
  1095. *
  1096. * <c E_NOTIMPL> nothing happened. The caller will do
  1097. * the default thing in response to <c E_NOTIMPL>.
  1098. *
  1099. *****************************************************************************/
  1100. #ifdef WINNT
  1101. TCHAR g_wszDefaultHIDName[80];
  1102. UINT g_uLenDefaultHIDSize;
  1103. #endif
  1104. STDMETHODIMP
  1105. CHid_GetProperty(PDICB pdcb, LPCDIPROPINFO ppropi, LPDIPROPHEADER pdiph)
  1106. {
  1107. HRESULT hres;
  1108. PCHID this;
  1109. EnterProcI(IDirectInputDeviceCallback::Hid::GetProperty,
  1110. (_ "pxxp", pdcb, ppropi->pguid, ppropi->iobj, pdiph));
  1111. /*
  1112. * This is an internal interface, so we can skimp on validation.
  1113. */
  1114. this = _thisPvNm(pdcb, dcb);
  1115. if(ppropi->iobj < this->df.dwNumObjs)
  1116. { /* Object property */
  1117. AssertF(ppropi->dwDevType == this->df.rgodf[ppropi->iobj].dwType);
  1118. switch((DWORD)(UINT_PTR)(ppropi->pguid))
  1119. {
  1120. case (DWORD)(UINT_PTR)(DIPROP_ENABLEREPORTID):
  1121. {
  1122. LPDIPROPDWORD ppropdw = CONTAINING_RECORD(pdiph, DIPROPDWORD, diph);
  1123. PHIDGROUPCAPS pcaps = this->rghoc[ppropi->iobj].pcaps;
  1124. AssertF(fLimpFF(pcaps,
  1125. pcaps->dwSignature == HIDGROUPCAPS_SIGNATURE));
  1126. ppropdw->dwData = 0x0;
  1127. AssertF(pcaps->wReportId < this->wMaxReportId[pcaps->type]);
  1128. AssertF(this->pEnableReportId[pcaps->type]);
  1129. (UCHAR)ppropdw->dwData = *(this->pEnableReportId[pcaps->type] + pcaps->wReportId);
  1130. hres = S_OK;
  1131. }
  1132. break;
  1133. case (DWORD)(UINT_PTR)(DIPROP_PHYSICALRANGE):
  1134. {
  1135. LPDIPROPRANGE pdiprg = CONTAINING_RECORD(pdiph, DIPROPRANGE, diph);
  1136. PHIDGROUPCAPS pcaps = this->rghoc[ppropi->iobj].pcaps;
  1137. pdiprg->lMin = pcaps->Physical.Min;
  1138. pdiprg->lMax = pcaps->Physical.Max;
  1139. hres = S_OK;
  1140. break;
  1141. }
  1142. break;
  1143. case (DWORD)(UINT_PTR)(DIPROP_LOGICALRANGE):
  1144. {
  1145. LPDIPROPRANGE pdiprg = CONTAINING_RECORD(pdiph, DIPROPRANGE, diph);
  1146. PHIDGROUPCAPS pcaps = this->rghoc[ppropi->iobj].pcaps;
  1147. pdiprg->lMin = pcaps->Logical.Min;
  1148. pdiprg->lMax = pcaps->Logical.Max;
  1149. hres = S_OK;
  1150. break;
  1151. }
  1152. break;
  1153. default:
  1154. if(ppropi->dwDevType & DIDFT_POV)
  1155. {
  1156. PHIDGROUPCAPS pcaps = this->rghoc[ppropi->iobj].pcaps;
  1157. AssertF(fLimpFF(pcaps,
  1158. pcaps->dwSignature == HIDGROUPCAPS_SIGNATURE));
  1159. #ifdef WINNT
  1160. if( pcaps && pcaps->IsPolledPOV && ppropi->pguid == DIPROP_CALIBRATIONMODE ) {
  1161. PJOYRANGECONVERT pjrc = this->rghoc[ppropi->iobj].pjrc;
  1162. if(pjrc)
  1163. {
  1164. hres = CCal_GetProperty(pjrc, ppropi->pguid, pdiph, this->dwVersion);
  1165. } else
  1166. {
  1167. hres = E_NOTIMPL;
  1168. }
  1169. } else
  1170. #endif
  1171. if(pcaps && ppropi->pguid == DIPROP_GRANULARITY)
  1172. {
  1173. LPDIPROPDWORD pdipdw = (PV)pdiph;
  1174. pdipdw->dwData = pcaps->usGranularity;
  1175. hres = S_OK;
  1176. } else
  1177. {
  1178. hres = E_NOTIMPL;
  1179. }
  1180. } else if(ppropi->dwDevType & DIDFT_RELAXIS)
  1181. {
  1182. /*
  1183. * All relative axes have a full range by default,
  1184. * so we don't need to do anything.
  1185. */
  1186. hres = E_NOTIMPL;
  1187. } else if(ppropi->dwDevType & DIDFT_ABSAXIS)
  1188. {
  1189. PJOYRANGECONVERT pjrc = this->rghoc[ppropi->iobj].pjrc;
  1190. /*
  1191. * Theoretically, every absolute axis will have
  1192. * calibration info. But test just in case something
  1193. * impossible happens.
  1194. */
  1195. if(pjrc)
  1196. {
  1197. hres = CCal_GetProperty(pjrc, ppropi->pguid, pdiph, this->dwVersion);
  1198. } else
  1199. {
  1200. hres = E_NOTIMPL;
  1201. }
  1202. } else
  1203. {
  1204. SquirtSqflPtszV(sqflHidDev | sqflError,
  1205. TEXT("CHid_GetProperty(iobj=%08x): E_NOTIMPL on guid: %08x"),
  1206. ppropi->iobj, ppropi->pguid);
  1207. hres = E_NOTIMPL;
  1208. }
  1209. }
  1210. } else if(ppropi->iobj == 0xFFFFFFFF)
  1211. { /* Device property */
  1212. switch((DWORD)(UINT_PTR)ppropi->pguid)
  1213. {
  1214. case (DWORD)(UINT_PTR)DIPROP_GUIDANDPATH:
  1215. hres = CHid_GetGuidAndPath(this, pdiph);
  1216. break;
  1217. case (DWORD)(UINT_PTR)DIPROP_INSTANCENAME:
  1218. {
  1219. /*
  1220. * DX8 CHANGE !
  1221. *
  1222. * Friendly names cause all manner of problems with devices that
  1223. * use auto detection so only allow non-predefined analog devices
  1224. * to use them.
  1225. */
  1226. if( ( this->VendorID == MSFT_SYSTEM_VID )
  1227. && ( this->ProductID >= MSFT_SYSTEM_PID + JOY_HW_PREDEFMAX )
  1228. && ( ( this->ProductID & 0xff00 ) == MSFT_SYSTEM_PID ) )
  1229. {
  1230. AssertF(this->hkType);
  1231. if( this->hkType )
  1232. {
  1233. LPDIPROPSTRING pstr = (PV)pdiph;
  1234. hres = JoyReg_GetValue(this->hkType,
  1235. REGSTR_VAL_JOYOEMNAME, REG_SZ,
  1236. pstr->wsz,
  1237. cbX(pstr->wsz));
  1238. if( SUCCEEDED(hres ) )
  1239. {
  1240. SquirtSqflPtszV(sqflHid | sqflVerbose,
  1241. TEXT( "Got instance name %s"), pstr->wsz );
  1242. #if (DIRECTINPUT_VERSION > 0x061A)
  1243. if( ( this->diHacks.nMaxDeviceNameLength < MAX_PATH )
  1244. && ( this->diHacks.nMaxDeviceNameLength < lstrlenW(pstr->wsz) ) )
  1245. {
  1246. pstr->wsz[this->diHacks.nMaxDeviceNameLength] = L'\0';
  1247. }
  1248. #endif
  1249. hres = S_OK;
  1250. break;
  1251. }
  1252. }
  1253. }
  1254. /*
  1255. * Fall through to catch the product name
  1256. */
  1257. }
  1258. /*
  1259. * DX8 CHANGE !
  1260. *
  1261. * In Win2k, this is the way devices get named. The original DX7
  1262. * used SetupAPI to get a friendly name (which only ever seems to be
  1263. * written by DInput) and if that failed, device description.
  1264. * Unfortunately Setup gives all devices matched with a generic match
  1265. * the same "USB Human Input Device" name, which is useless to game
  1266. * players. Devices listed specifically in input.inf have much
  1267. * better names but all new devices are hosed.
  1268. * See bug 32586 for more links.
  1269. */
  1270. case (DWORD)(UINT_PTR)DIPROP_PRODUCTNAME:
  1271. {
  1272. LPDIPROPSTRING pdipstr = (PV)pdiph;
  1273. /*
  1274. * For now, don't deal with mice and keyboard names on NT
  1275. */
  1276. #ifdef WINNT
  1277. AssertF( ( GET_DIDEVICE_TYPE( this->dwDevType ) != DIDEVTYPE_KEYBOARD )
  1278. && ( GET_DIDEVICE_TYPE( this->dwDevType ) != DIDEVTYPE_MOUSE ) );
  1279. #endif
  1280. if( GET_DIDEVICE_TYPE( this->dwDevType ) < DIDEVTYPE_JOYSTICK )
  1281. {
  1282. if( fHasSpecificHardwareMatch( this->ptszId )
  1283. && SUCCEEDED( hres = DIHid_GetParentRegistryProperty(this->ptszId, SPDRP_DEVICEDESC, pdiph, 0x0 ) ) )
  1284. {
  1285. SquirtSqflPtszV(sqflHid | sqflVerbose,
  1286. TEXT("Got sys dev description %S"), pdipstr->wsz );
  1287. }
  1288. else if( fGetProductStringFromDevice( this, pdipstr->wsz, cbX( pdipstr->wsz ) ) )
  1289. {
  1290. SquirtSqflPtszV(sqflHid | sqflVerbose,
  1291. TEXT( "Got sys dev name from device %S"), pdipstr->wsz );
  1292. hres = S_OK;
  1293. }
  1294. else
  1295. {
  1296. if( SUCCEEDED( hres = DIHid_GetRegistryProperty(this->ptszId, SPDRP_DEVICEDESC, pdiph ) ) )
  1297. {
  1298. SquirtSqflPtszV(sqflHid | sqflVerbose,
  1299. TEXT( "Got sys dev name from devnode registry %S"), pdipstr->wsz );
  1300. }
  1301. else
  1302. {
  1303. UINT uDefName;
  1304. switch( GET_DIDEVICE_TYPE( this->dwDevType ) )
  1305. {
  1306. case DIDEVTYPE_MOUSE:
  1307. uDefName = IDS_STDMOUSE;
  1308. break;
  1309. case DIDEVTYPE_KEYBOARD:
  1310. uDefName = IDS_STDKEYBOARD;
  1311. break;
  1312. default:
  1313. uDefName = IDS_DEVICE_NAME;
  1314. break;
  1315. }
  1316. if( LoadStringW(g_hinst, uDefName, pdipstr->wsz, cA( pdipstr->wsz ) ) )
  1317. {
  1318. SquirtSqflPtszV(sqflHid | sqflVerbose,
  1319. TEXT( "Loaded default sys dev name %S"), pdipstr->wsz );
  1320. hres = S_OK;
  1321. }
  1322. else
  1323. {
  1324. /*
  1325. * Give up, this machine is toast if we can't
  1326. * even load a string from our own resources.
  1327. */
  1328. SquirtSqflPtszV(sqflHidDev | sqflError,
  1329. TEXT("CHid_GetProperty(guid:%08x) failed to get name"),
  1330. ppropi->pguid);
  1331. hres = E_FAIL;
  1332. }
  1333. }
  1334. }
  1335. }
  1336. else
  1337. {
  1338. /*
  1339. * For game controllers, first look in MediaProperties.
  1340. * This is the most likely place to find a localized string
  1341. * free from corruption by the setup process.
  1342. * This should only fail before the type key is created when
  1343. * it first used so other paths are rare.
  1344. */
  1345. DIJOYTYPEINFO dijti;
  1346. WCHAR wszType[cbszVIDPID];
  1347. /* Check the type key or get predefined name */
  1348. ZeroX(dijti);
  1349. dijti.dwSize = cbX(dijti);
  1350. if( ( this->VendorID == MSFT_SYSTEM_VID )
  1351. &&( ( this->ProductID >= MSFT_SYSTEM_PID + JOY_HW_PREDEFMIN )
  1352. &&( this->ProductID < MSFT_SYSTEM_PID + JOY_HW_PREDEFMAX ) ) )
  1353. {
  1354. wszType[0] = L'#';
  1355. wszType[1] = L'0' + (WCHAR)(this->ProductID-MSFT_SYSTEM_PID);
  1356. wszType[2] = L'\0';
  1357. hres = JoyReg_GetPredefTypeInfo( wszType, &dijti, DITC_DISPLAYNAME);
  1358. AssertF( SUCCEEDED( hres ) );
  1359. AssertF( dijti.wszDisplayName[0] != L'\0' );
  1360. lstrcpyW(pdipstr->wsz, dijti.wszDisplayName);
  1361. SquirtSqflPtszV(sqflHid | sqflVerbose,
  1362. TEXT( "Got name as predefined %s"), pdipstr->wsz );
  1363. }
  1364. else
  1365. {
  1366. #ifndef WINNT
  1367. static WCHAR wszDefHIDName[] = L"HID Game Controller";
  1368. #endif
  1369. BOOL fOverwriteDeviceName = FALSE;
  1370. #ifndef UNICODE
  1371. TCHAR tszType[cbszVIDPID];
  1372. wsprintf(tszType, VID_PID_TEMPLATE, this->VendorID, this->ProductID);
  1373. TToU( wszType, cA(wszType), tszType );
  1374. #else
  1375. wsprintf(wszType, VID_PID_TEMPLATE, this->VendorID, this->ProductID);
  1376. #endif
  1377. #ifdef WINNT
  1378. #define INPUT_INF_FILENAME L"\\INF\\INPUT.INF"
  1379. if( g_wszDefaultHIDName[0] == L'\0' )
  1380. {
  1381. WCHAR wszInputINF[MAX_PATH];
  1382. UINT uLen;
  1383. uLen = GetWindowsDirectoryW( wszInputINF, cA( wszInputINF ) );
  1384. /*
  1385. * If the path is too long, don't set the filename
  1386. * so the the default string gets used when the
  1387. * GetPrivateProfileString fails.
  1388. */
  1389. if( uLen < cA( wszInputINF ) - cA( INPUT_INF_FILENAME ) )
  1390. {
  1391. memcpy( (PBYTE)&wszInputINF[uLen], (PBYTE)INPUT_INF_FILENAME, cbX( INPUT_INF_FILENAME ) );
  1392. }
  1393. /*
  1394. * Remember the length, if the string was too long to
  1395. * fit in the buffer there will be plenty to make a
  1396. * reasonable comparison.
  1397. */
  1398. g_uLenDefaultHIDSize = 2 * GetPrivateProfileStringW(
  1399. L"strings", L"HID.DeviceDesc", L"USB Human Interface Device",
  1400. g_wszDefaultHIDName, cA( g_wszDefaultHIDName ) - 1, wszInputINF );
  1401. }
  1402. #undef INPUT_INF_FILENAME
  1403. #endif //#ifdef WINNT
  1404. if( SUCCEEDED(hres = JoyReg_GetTypeInfo(wszType, &dijti, DITC_DISPLAYNAME))
  1405. && (dijti.wszDisplayName[0] != L'\0')
  1406. #ifdef WINNT
  1407. && ( (g_uLenDefaultHIDSize == 0)
  1408. || memcmp(dijti.wszDisplayName, g_wszDefaultHIDName, g_uLenDefaultHIDSize) )// not equal
  1409. #else
  1410. && memcmp(dijti.wszDisplayName, wszDefHIDName, cbX(wszDefHIDName)-2) //not equal
  1411. #endif
  1412. )
  1413. {
  1414. lstrcpyW(pdipstr->wsz, dijti.wszDisplayName);
  1415. SquirtSqflPtszV(sqflHid | sqflVerbose,
  1416. TEXT("Got name from type info %s"), pdipstr->wsz );
  1417. }
  1418. #ifdef WINNT
  1419. else if( fHasSpecificHardwareMatch( this->ptszId )
  1420. && SUCCEEDED( hres = DIHid_GetParentRegistryProperty(this->ptszId, SPDRP_DEVICEDESC, pdiph, 0x0 ) ) )
  1421. {
  1422. fOverwriteDeviceName = TRUE;
  1423. SquirtSqflPtszV(sqflHid | sqflVerbose,
  1424. TEXT("Got specific description %s"), pdipstr->wsz );
  1425. }
  1426. #endif
  1427. else
  1428. {
  1429. if( fGetProductStringFromDevice( this, pdipstr->wsz, cbX( pdipstr->wsz ) ) )
  1430. {
  1431. fOverwriteDeviceName = TRUE;
  1432. SquirtSqflPtszV(sqflHid | sqflVerbose,
  1433. TEXT("Got description %s from device"), pdipstr->wsz );
  1434. }
  1435. else
  1436. {
  1437. /*
  1438. * Just make up a name from the caps
  1439. */
  1440. CType_MakeGameCtrlName( pdipstr->wsz,
  1441. this->dwDevType, this->dwAxes, this->dwButtons, this->dwPOVs );
  1442. fOverwriteDeviceName = TRUE;
  1443. SquirtSqflPtszV(sqflHid | sqflVerbose,
  1444. TEXT("Made up name %s"), pdipstr->wsz );
  1445. }
  1446. hres = S_OK;
  1447. }
  1448. if( fOverwriteDeviceName ) {
  1449. /*
  1450. * If we have a better name, overwrite the old one with this better one.
  1451. * See manbug 46438.
  1452. */
  1453. AssertF(this->hkType);
  1454. AssertF(pdipstr->wsz[0]);
  1455. hres = JoyReg_SetValue(this->hkType,
  1456. REGSTR_VAL_JOYOEMNAME, REG_SZ,
  1457. pdipstr->wsz,
  1458. cbX(pdipstr->wsz));
  1459. if( FAILED(hres) ){
  1460. SquirtSqflPtszV(sqflHid | sqflVerbose,
  1461. TEXT("Unable to overwrite generic device name with %s"), pdipstr->wsz );
  1462. // This failure (unlikely) doesn't matter.
  1463. hres = S_OK;
  1464. }
  1465. }
  1466. }
  1467. }
  1468. #if (DIRECTINPUT_VERSION > 0x061A)
  1469. if( SUCCEEDED(hres)
  1470. && ( this->diHacks.nMaxDeviceNameLength < MAX_PATH )
  1471. && ( this->diHacks.nMaxDeviceNameLength < lstrlenW(pdipstr->wsz) ) )
  1472. {
  1473. pdipstr->wsz[this->diHacks.nMaxDeviceNameLength] = L'\0';
  1474. }
  1475. #endif
  1476. break;
  1477. }
  1478. case (DWORD)(UINT_PTR)DIPROP_JOYSTICKID:
  1479. if( fHasAllBitsFlFl( this->dwDevType, DIDEVTYPE_JOYSTICK | DIDEVTYPE_HID ) )
  1480. {
  1481. LPDIPROPDWORD pdipdw = (PV)pdiph;
  1482. pdipdw->dwData = this->idJoy;
  1483. hres = S_OK;
  1484. } else
  1485. {
  1486. hres = E_NOTIMPL;
  1487. }
  1488. break;
  1489. case (DWORD)(UINT_PTR)DIPROP_GETPORTDISPLAYNAME:
  1490. if( fWinnt )
  1491. {
  1492. /* For HID devices Port Display Name is the grand parent name */
  1493. hres = DIHid_GetParentRegistryProperty(this->ptszId, SPDRP_FRIENDLYNAME, pdiph, TRUE);
  1494. if( FAILED(hres) )
  1495. {
  1496. /* Maybe we can use the Product Name */
  1497. hres = DIHid_GetParentRegistryProperty(this->ptszId, SPDRP_DEVICEDESC, pdiph, TRUE);
  1498. if( SUCCEEDED(hres) )
  1499. {
  1500. /* We only sort of succeeded */
  1501. hres = S_FALSE;
  1502. }
  1503. }
  1504. #if (DIRECTINPUT_VERSION > 0x061A)
  1505. if( SUCCEEDED(hres) )
  1506. {
  1507. LPDIPROPSTRING pdipstr = (PV)pdiph;
  1508. if( this->diHacks.nMaxDeviceNameLength < lstrlenW(pdipstr->wsz) ) {
  1509. pdipstr->wsz[this->diHacks.nMaxDeviceNameLength] = L'\0';
  1510. }
  1511. }
  1512. #endif
  1513. } else
  1514. {
  1515. // Not sure how this works on Win9x
  1516. hres = E_NOTIMPL;
  1517. }
  1518. break;
  1519. case (DWORD)(UINT_PTR)(DIPROP_ENABLEREPORTID):
  1520. hres = E_NOTIMPL;
  1521. break;
  1522. default:
  1523. SquirtSqflPtszV(sqflHid | sqflBenign ,
  1524. TEXT("CHid_GetProperty(iobj=0xFFFFFFFF): E_NOTIMPL on guid: %08x"),
  1525. ppropi->pguid);
  1526. hres = E_NOTIMPL;
  1527. break;
  1528. }
  1529. } else
  1530. {
  1531. SquirtSqflPtszV(sqflHidDev | sqflError,
  1532. TEXT("CHid_GetProperty(iobj=%08x): E_NOTIMPL on guid: %08x"),
  1533. ppropi->iobj, ppropi->pguid);
  1534. hres = E_NOTIMPL;
  1535. }
  1536. ExitOleProcR();
  1537. return hres;
  1538. }
  1539. /*****************************************************************************
  1540. *
  1541. * @doc INTERNAL
  1542. *
  1543. * @func LONG | CHid_CoordinateTransform |
  1544. *
  1545. * Convert numbers from logical to physical or vice versa.
  1546. *
  1547. * If either the To or From values look suspicious, then
  1548. * ignore them and leave the values alone.
  1549. *
  1550. * @parm PLMINMAX | Dst |
  1551. *
  1552. * Destination min/max information.
  1553. *
  1554. * @parm PLMINMAX | Src |
  1555. *
  1556. * Source min/max information.
  1557. *
  1558. * @parm LONG | lVal |
  1559. *
  1560. * Source value to be converted.
  1561. *
  1562. * @returns
  1563. *
  1564. * The destination value after conversion.
  1565. *
  1566. *****************************************************************************/
  1567. LONG EXTERNAL
  1568. CHid_CoordinateTransform(PLMINMAX Dst, PLMINMAX Src, LONG lVal)
  1569. {
  1570. /*
  1571. * Note that the sanity check is symmetric in Src and Dst.
  1572. * This is important, so that we never get into a weird
  1573. * case where we can convert one way but can't convert back.
  1574. */
  1575. if(Dst->Min < Dst->Max && Src->Min < Src->Max)
  1576. {
  1577. /*
  1578. * We need to perform a straight linear interpolation.
  1579. * The math comes out like this:
  1580. *
  1581. * x - x0 y - y0
  1582. * ------- = -------
  1583. * x1 - x0 y1 - y0
  1584. *
  1585. * If you now do a "solve for y", you get
  1586. *
  1587. *
  1588. * y1 - y0
  1589. * y = (x - x0) ------- + y0
  1590. * x1 - x0
  1591. *
  1592. * where "x" is Src, "y" is Dst, 0 is Min, and 1 is Max.
  1593. *
  1594. *
  1595. */
  1596. lVal = MulDiv(lVal - Src->Min, Dst->Max - Dst->Min,
  1597. Src->Max - Src->Min) + Dst->Min;
  1598. }
  1599. return lVal;
  1600. }
  1601. /*****************************************************************************
  1602. *
  1603. * @doc INTERNAL
  1604. *
  1605. * @method int | CHid | IsMatchingJoyDevice |
  1606. *
  1607. * Does the cached joystick ID match us?
  1608. *
  1609. * @parm OUT PVXDINITPARMS | pvip |
  1610. *
  1611. * On success, contains parameter values.
  1612. *
  1613. * @returns
  1614. *
  1615. * Nonzero on success.
  1616. *
  1617. *****************************************************************************/
  1618. BOOL INTERNAL
  1619. CHid_IsMatchingJoyDevice(PCHID this, PVXDINITPARMS pvip)
  1620. {
  1621. CHAR sz[MAX_PATH];
  1622. LPSTR pszPath;
  1623. BOOL fRc;
  1624. pszPath = JoyReg_JoyIdToDeviceInterface_95(this->idJoy, pvip, sz);
  1625. if(pszPath)
  1626. {
  1627. SquirtSqflPtszV(sqfl | sqflTrace,
  1628. TEXT("CHid_IsMatchingJoyDevice: %d -> %s"),
  1629. this->idJoy, pszPath);
  1630. #ifdef UNICODE
  1631. {
  1632. CHAR szpath[MAX_PATH];
  1633. UToA( szpath, cA(szpath), (LPWSTR)this->ptszPath);
  1634. fRc = ( lstrcmpiA(pszPath, szpath) == 0x0 );
  1635. }
  1636. #else
  1637. fRc = ( lstrcmpiA(pszPath, (PCHAR)this->ptszPath) == 0x0 );
  1638. #endif
  1639. } else
  1640. {
  1641. fRc = FALSE;
  1642. }
  1643. return fRc;
  1644. }
  1645. /*****************************************************************************
  1646. *
  1647. * @doc INTERNAL
  1648. *
  1649. * @method void | CHid | FindJoyDevice |
  1650. *
  1651. * Look for the VJOYD device that matches us, if any.
  1652. *
  1653. * On return, the <e CHID.idJoy> field contains the
  1654. * matching joystick number, or -1 if not found.
  1655. *
  1656. * @parm OUT PVXDINITPARMS | pvip |
  1657. *
  1658. * On success, contains parameter values.
  1659. *
  1660. *****************************************************************************/
  1661. void INTERNAL
  1662. CHid_FindJoyDevice(PCHID this, PVXDINITPARMS pvip)
  1663. {
  1664. /*
  1665. * If we have a cached value, and it still works, then
  1666. * our job is done.
  1667. */
  1668. if(this->idJoy >= 0 &&
  1669. CHid_IsMatchingJoyDevice(this, pvip))
  1670. {
  1671. } else
  1672. {
  1673. /*
  1674. * Need to keep looking. (Or start looking.)
  1675. *
  1676. * A countdown loop is nicer, but for efficiency, we count
  1677. * upwards, since the joystick we want tends to be near the
  1678. * beginning.
  1679. */
  1680. for(this->idJoy = 0; this->idJoy < cJoyMax; this->idJoy++)
  1681. {
  1682. if(CHid_IsMatchingJoyDevice(this, pvip))
  1683. {
  1684. goto done;
  1685. }
  1686. }
  1687. this->idJoy = -1;
  1688. }
  1689. done:;
  1690. }
  1691. /*****************************************************************************
  1692. *
  1693. * @doc INTERNAL
  1694. *
  1695. * @method int | CHid | MapAxis |
  1696. *
  1697. * Find VJOYD axis from HID axis, if one.
  1698. *
  1699. * @parm PVXDINITPARMS | pvip |
  1700. *
  1701. * Parameter values that let us known which axes VJOYD
  1702. * has mapped to which HID Axes.
  1703. *
  1704. * @parm UINT | iobj |
  1705. *
  1706. * Object index of the object whose axis value changed.
  1707. *
  1708. * @returns
  1709. *
  1710. * The VJOYD axis number that changed (0 to 5), or -1
  1711. * if there is no matching axis. There will be no matching
  1712. * axis if, for example, the device has something that is
  1713. * not expressible via VJOYD (e.g., a temperature sensor).
  1714. *
  1715. *****************************************************************************/
  1716. int INTERNAL
  1717. CHid_MapAxis(PCHID this, PVXDINITPARMS pvip, UINT iobj)
  1718. {
  1719. int iAxis;
  1720. DWORD dwUsage;
  1721. AssertF(this->dcb.lpVtbl->GetUsage == CHid_GetUsage);
  1722. dwUsage = CHid_GetUsage(&this->dcb, (int)iobj);
  1723. if(dwUsage)
  1724. {
  1725. /*
  1726. * A countdown loop lets us fall out with the correct failure
  1727. * code (namely, -1).
  1728. */
  1729. iAxis = cJoyPosAxisMax;
  1730. while(--iAxis >= 0)
  1731. {
  1732. if(pvip->Usages[iAxis] == dwUsage)
  1733. {
  1734. break;
  1735. }
  1736. }
  1737. } else
  1738. {
  1739. /*
  1740. * Eek! No usage information for the axis. Then it certainly
  1741. * isn't a VJOYD axis.
  1742. */
  1743. iAxis = -1;
  1744. }
  1745. return iAxis;
  1746. }
  1747. /*****************************************************************************
  1748. *
  1749. * @doc INTERNAL
  1750. *
  1751. * @method void | CHid | UpdateVjoydCalibration |
  1752. *
  1753. * Somebody changed the calibration on a single axis. If we
  1754. * are shadowing a joystick, then look for the VJOYD alias of
  1755. * our device and update its registry settings, too.
  1756. *
  1757. *
  1758. * @parm UINT | iobj |
  1759. *
  1760. * Object index of the object whose calibration changed.
  1761. *
  1762. *****************************************************************************/
  1763. void EXTERNAL
  1764. CHid_UpdateVjoydCalibration(PCHID this, UINT iobj)
  1765. {
  1766. HRESULT hres;
  1767. int iAxis;
  1768. VXDINITPARMS vip;
  1769. DIJOYCONFIG cfg;
  1770. PHIDGROUPCAPS pcaps;
  1771. PJOYRANGECONVERT pjrc;
  1772. AssertF(iobj < this->df.dwNumObjs);
  1773. /*
  1774. * Proceed if...
  1775. *
  1776. * - We can find the VJOYD device we correspond to.
  1777. * - We can find the axis that got updated.
  1778. * - The indicated axis has capability information.
  1779. * - The indicated axis has calibration information.
  1780. * - We can read the old calibration information.
  1781. */
  1782. CHid_FindJoyDevice(this, &vip);
  1783. if(this->idJoy >= 0 &&
  1784. (iAxis = CHid_MapAxis(this, &vip, iobj)) >= 0 &&
  1785. (pcaps = this->rghoc[iobj].pcaps) != NULL &&
  1786. (pjrc = this->rghoc[iobj].pjrc) != NULL &&
  1787. SUCCEEDED(hres = JoyReg_GetConfig(this->idJoy, NULL, &cfg,
  1788. DIJC_REGHWCONFIGTYPE)))
  1789. {
  1790. PLMINMAX Dst = &pcaps->Physical;
  1791. PLMINMAX Src = &pcaps->Logical;
  1792. AssertF(iAxis < cJoyPosAxisMax);
  1793. #define JoyPosValue(phwc, f, i) \
  1794. *(LPDWORD)pvAddPvCb(&(phwc)->hwv.jrvHardware.f, \
  1795. ibJoyPosAxisFromPosAxis(i))
  1796. /*
  1797. * We use logical coordinates, but VJOYD wants physical
  1798. * coordinates, so do the conversion while we copy the
  1799. * values.
  1800. */
  1801. #define ConvertValue(f1, f2) \
  1802. JoyPosValue(&cfg.hwc, f1, iAxis) = \
  1803. CHid_CoordinateTransform(Dst, Src, pjrc->f2) \
  1804. ConvertValue(jpMin , dwPmin);
  1805. ConvertValue(jpMax , dwPmax);
  1806. ConvertValue(jpCenter, dwPc );
  1807. #undef ConvertValue
  1808. #undef JoyPosValue
  1809. /*
  1810. * Notice that we do *not* pass the DIJC_UPDATEALIAS flag
  1811. * because WE ARE THE ALIAS! If we had passed the flag,
  1812. * then JoyReg would create us and attempt to update our
  1813. * calibration which we don't want it to do because the
  1814. * whole thing was our idea in the first place.
  1815. */
  1816. hres = JoyReg_SetConfig(this->idJoy, &cfg.hwc, &cfg,
  1817. DIJC_REGHWCONFIGTYPE);
  1818. }
  1819. }
  1820. /*****************************************************************************
  1821. *
  1822. * @doc INTERNAL
  1823. *
  1824. * @method void | CHid | UpdateCalibrationFromVjoyd |
  1825. *
  1826. * This function is only for Win9x. Joy.cpl uses winmm (through vjoyd)
  1827. * to calibrate the device, and save calibration information directly into
  1828. * registry without notifying HID. ANother issue is: vjoyd only use unsigned
  1829. * data (physical data), while HID also use signed data. When we read
  1830. * calibration information from VJOYD, we need do conversion.
  1831. *
  1832. * @parm UINT | iobj |
  1833. *
  1834. * Object index of the object whose calibration changed.
  1835. *
  1836. *****************************************************************************/
  1837. void EXTERNAL
  1838. CHid_UpdateCalibrationFromVjoyd(PCHID this, UINT iobj, LPDIOBJECTCALIBRATION pCal)
  1839. {
  1840. HRESULT hres;
  1841. int iAxis;
  1842. VXDINITPARMS vip;
  1843. DIJOYCONFIG cfg;
  1844. PHIDGROUPCAPS pcaps;
  1845. PJOYRANGECONVERT pjrc;
  1846. AssertF(iobj < this->df.dwNumObjs);
  1847. /*
  1848. * Proceed if...
  1849. *
  1850. * - We can find the VJOYD device we correspond to.
  1851. * - We can find the axis that got updated.
  1852. * - The indicated axis has capability information.
  1853. * - The indicated axis has calibration information.
  1854. * - We can read the calibration information.
  1855. */
  1856. CHid_FindJoyDevice(this, &vip);
  1857. if(this->idJoy >= 0 &&
  1858. (iAxis = CHid_MapAxis(this, &vip, iobj)) >= 0 &&
  1859. (pcaps = this->rghoc[iobj].pcaps) != NULL &&
  1860. (pjrc = this->rghoc[iobj].pjrc) != NULL &&
  1861. SUCCEEDED(hres = JoyReg_GetConfig(this->idJoy, NULL, &cfg,
  1862. DIJC_REGHWCONFIGTYPE)))
  1863. {
  1864. PLMINMAX Src = &pcaps->Physical;
  1865. PLMINMAX Dst = &pcaps->Logical;
  1866. AssertF(iAxis < cJoyPosAxisMax);
  1867. #define JoyPosValue(phwc, f, i) \
  1868. *(LPDWORD)pvAddPvCb(&(phwc)->hwv.jrvHardware.f, \
  1869. ibJoyPosAxisFromPosAxis(i))
  1870. /*
  1871. * We use logical coordinates, but VJOYD wants physical
  1872. * coordinates, so do the conversion while we copy the
  1873. * values.
  1874. */
  1875. #define ConvertValue(f1, f2) \
  1876. pCal->f2 = CHid_CoordinateTransform(Dst, Src, \
  1877. JoyPosValue(&cfg.hwc, f1, iAxis) )
  1878. ConvertValue(jpMin , lMin);
  1879. ConvertValue(jpMax , lMax);
  1880. ConvertValue(jpCenter, lCenter);
  1881. #undef ConvertValue
  1882. #undef JoyPosValue
  1883. }
  1884. }
  1885. /*****************************************************************************
  1886. *
  1887. * @doc INTERNAL
  1888. *
  1889. * @func HRESULT | DIHid_SetRegistryProperty |
  1890. *
  1891. * Wrapper around <f SetupDiSetDeviceRegistryProperty>
  1892. * that handles character set issues.
  1893. *
  1894. * @parm LPTSTR ptszId
  1895. *
  1896. * Device Instance ID.
  1897. *
  1898. * @parm DWORD | dwProperty |
  1899. *
  1900. * The property being queried.
  1901. *
  1902. * @parm LPCDIPROPHEADER | diph |
  1903. *
  1904. * Property data to be set.
  1905. *
  1906. *****************************************************************************/
  1907. HRESULT INTERNAL
  1908. DIHid_SetParentRegistryProperty(LPTSTR ptszId, DWORD dwProperty, LPCDIPROPHEADER pdiph, BOOL bGrandParent)
  1909. {
  1910. HDEVINFO hdev;
  1911. TCHAR tsz[MAX_PATH];
  1912. LPDIPROPSTRING pstr = (PV)pdiph;
  1913. HRESULT hres = E_FAIL;
  1914. hdev = SetupDiCreateDeviceInfoList(NULL, NULL);
  1915. if(hdev != INVALID_HANDLE_VALUE)
  1916. {
  1917. SP_DEVINFO_DATA dinf;
  1918. ZeroX(tsz);
  1919. #ifdef UNICODE
  1920. lstrcpyW(tsz, pstr->wsz);
  1921. #else
  1922. UToA(tsz, cA(tsz), pstr->wsz);
  1923. #endif
  1924. dinf.cbSize = cbX(SP_DEVINFO_DATA);
  1925. if(SetupDiOpenDeviceInfo(hdev, ptszId, NULL, 0, &dinf))
  1926. {
  1927. CONFIGRET cr;
  1928. DEVINST DevInst;
  1929. if( (cr = CM_Get_Parent(&DevInst, dinf.DevInst, 0x0) ) == CR_SUCCESS )
  1930. {
  1931. CAssertF( SPDRP_DEVICEDESC +1 == CM_DRP_DEVICEDESC );
  1932. CAssertF( SPDRP_FRIENDLYNAME +1 == CM_DRP_FRIENDLYNAME );
  1933. if(bGrandParent)
  1934. {
  1935. cr = CM_Get_Parent(&DevInst, DevInst, 0x0);
  1936. if( cr != CR_SUCCESS )
  1937. {
  1938. // No GrandParent ??
  1939. }
  1940. }
  1941. if( ( cr = CM_Set_DevNode_Registry_Property(
  1942. DevInst,
  1943. dwProperty+1,
  1944. (LPBYTE)tsz,
  1945. MAX_PATH *cbX(TCHAR),
  1946. 0x0 ) ) == CR_SUCCESS )
  1947. {
  1948. hres = S_OK;
  1949. } else
  1950. {
  1951. SquirtSqflPtszV(sqfl | sqflVerbose,
  1952. TEXT("CM_Get_DevNode_Registry_Property FAILED") );
  1953. }
  1954. } else
  1955. {
  1956. SquirtSqflPtszV(sqfl | sqflVerbose,
  1957. TEXT("CM_Get_Parent FAILED") );
  1958. }
  1959. } else
  1960. {
  1961. SquirtSqflPtszV(sqfl | sqflError,
  1962. TEXT("SetupDiOpenDeviceInfo FAILED, le = %d"), GetLastError() );
  1963. }
  1964. SetupDiDestroyDeviceInfoList(hdev);
  1965. } else
  1966. {
  1967. SquirtSqflPtszV(sqfl | sqflError,
  1968. TEXT("SetupDiCreateDeviceInfoList FAILED, le = %d"), GetLastError() );
  1969. }
  1970. return hres;
  1971. }
  1972. HRESULT INTERNAL
  1973. DIHid_SetRegistryProperty(LPTSTR ptszId, DWORD dwProperty, LPCDIPROPHEADER pdiph)
  1974. {
  1975. HDEVINFO hdev;
  1976. TCHAR tsz[MAX_PATH];
  1977. LPDIPROPSTRING pstr = (PV)pdiph;
  1978. HRESULT hres;
  1979. hdev = SetupDiCreateDeviceInfoList(NULL, NULL);
  1980. if(hdev != INVALID_HANDLE_VALUE)
  1981. {
  1982. SP_DEVINFO_DATA dinf;
  1983. ZeroX(tsz);
  1984. #ifdef UNICODE
  1985. lstrcpyW(tsz, pstr->wsz);
  1986. #else
  1987. UToA(tsz, cA(tsz), pstr->wsz);
  1988. #endif
  1989. dinf.cbSize = cbX(SP_DEVINFO_DATA);
  1990. if(SetupDiOpenDeviceInfo(hdev, ptszId, NULL, 0, &dinf))
  1991. {
  1992. if(SetupDiSetDeviceRegistryProperty(hdev, &dinf, dwProperty,
  1993. (LPBYTE)tsz, MAX_PATH*cbX(TCHAR)) )
  1994. {
  1995. hres = S_OK;
  1996. } else
  1997. {
  1998. hres = E_FAIL;
  1999. }
  2000. } else
  2001. {
  2002. hres = E_FAIL;
  2003. }
  2004. SetupDiDestroyDeviceInfoList(hdev);
  2005. } else
  2006. {
  2007. hres = E_FAIL;
  2008. }
  2009. return hres;
  2010. }
  2011. /*****************************************************************************
  2012. *
  2013. * @doc INTERNAL
  2014. *
  2015. * @method HRESULT | CHid | SetProperty |
  2016. *
  2017. * Set a hid device property.
  2018. *
  2019. * @parm PCHID | this |
  2020. *
  2021. * The hid object.
  2022. *
  2023. * @parm IN LPCDIPROPINFO | ppropi |
  2024. *
  2025. * Information describing the property being set.
  2026. *
  2027. * @parm LPCDIPROPHEADER | pdiph |
  2028. *
  2029. * Structure containing property value.
  2030. *
  2031. * @returns
  2032. *
  2033. * <c E_NOTIMPL> nothing happened. The caller will do
  2034. * the default thing in response to <c E_NOTIMPL>.
  2035. *
  2036. *****************************************************************************/
  2037. STDMETHODIMP
  2038. CHid_SetProperty(PDICB pdcb, LPCDIPROPINFO ppropi, LPCDIPROPHEADER pdiph)
  2039. {
  2040. HRESULT hres;
  2041. PCHID this;
  2042. EnterProcI(IDirectInputDeviceCallback::Hid::SetProperty,
  2043. (_ "pxxp", pdcb, ppropi->pguid, ppropi->iobj, pdiph));
  2044. /*
  2045. * This is an internal interface, so we can skimp on validation.
  2046. */
  2047. this = _thisPvNm(pdcb, dcb);
  2048. if(ppropi->iobj < this->df.dwNumObjs)
  2049. {
  2050. /*
  2051. * Object Property
  2052. */
  2053. PHIDGROUPCAPS pcaps;
  2054. AssertF(ppropi->dwDevType == this->df.rgodf[ppropi->iobj].dwType);
  2055. AssertF(ppropi->iobj == CHid_ObjFromType(this, ppropi->dwDevType));
  2056. if( pcaps = this->rghoc[ppropi->iobj].pcaps )
  2057. {
  2058. switch((DWORD)(UINT_PTR)ppropi->pguid)
  2059. {
  2060. case (DWORD)(UINT_PTR)(DIPROP_ENABLEREPORTID):
  2061. {
  2062. LPDIPROPDWORD ppropdw = CONTAINING_RECORD(pdiph, DIPROPDWORD, diph);
  2063. AssertF(pcaps->wReportId < this->wMaxReportId[pcaps->type]);
  2064. AssertF(this->pEnableReportId[pcaps->type]);
  2065. hres = S_OK;
  2066. if( ppropdw->dwData == 0x1 )
  2067. {
  2068. *(this->pEnableReportId[pcaps->type] + pcaps->wReportId) = 0x1;
  2069. pcaps->fReportDisabled = FALSE;
  2070. } else
  2071. {
  2072. *(this->pEnableReportId[pcaps->type] + pcaps->wReportId) = 0x0;
  2073. pcaps->fReportDisabled = TRUE;
  2074. }
  2075. }
  2076. break;
  2077. default:
  2078. { /* Object property */
  2079. PJOYRANGECONVERT pjrc;
  2080. AssertF(ppropi->dwDevType == this->df.rgodf[ppropi->iobj].dwType);
  2081. AssertF(ppropi->iobj == CHid_ObjFromType(this, ppropi->dwDevType));
  2082. if((pjrc = this->rghoc[ppropi->iobj].pjrc) &&
  2083. (pcaps = this->rghoc[ppropi->iobj].pcaps))
  2084. {
  2085. if( ppropi->dwDevType == DIDFT_POV )
  2086. {
  2087. #ifdef WINNT
  2088. /*
  2089. * Only allow POV calibration for the private
  2090. * DX5 version used by GCDEF. This stops WinMM
  2091. * and Nascar 4 from getting unexpected raw
  2092. * data for POVs when polling for raw axes.
  2093. */
  2094. if( ( this->dwVersion == 0x5B2 )
  2095. && ( pcaps->IsPolledPOV ) )
  2096. {
  2097. hres = CCal_SetProperty(pjrc, ppropi, pdiph, this->hkInstType, this->dwVersion);
  2098. if( SUCCEEDED(hres) ) {
  2099. CHid_LoadCalibrations(this);
  2100. /*
  2101. * If this doesn't succeed, no big deal. So, we needn't check hres.
  2102. */
  2103. hres = CHid_InitParseData( this );
  2104. }
  2105. } else
  2106. #endif
  2107. {
  2108. hres = E_NOTIMPL;
  2109. }
  2110. } else if (ppropi->dwDevType & DIDFT_RELAXIS)
  2111. {
  2112. /*
  2113. * All relative axes have a full range by default,
  2114. * so we don't need to do anything.
  2115. */
  2116. hres = E_NOTIMPL;
  2117. } else if(ppropi->dwDevType & DIDFT_ABSAXIS)
  2118. {
  2119. /*
  2120. * Specific calibrations arrive in VJOYD coordinates.
  2121. * We need to convert them to DirectInput (logical)
  2122. * coordinates if so.
  2123. */
  2124. DIPROPCAL cal;
  2125. if(ppropi->pguid == DIPROP_SPECIFICCALIBRATION)
  2126. {
  2127. PLMINMAX Dst = &pcaps->Logical;
  2128. PLMINMAX Src = &pcaps->Physical;
  2129. LPDIPROPCAL pcal = CONTAINING_RECORD(pdiph, DIPROPCAL, diph);
  2130. cal.lMin = CHid_CoordinateTransform(Dst, Src, pcal->lMin);
  2131. cal.lCenter = CHid_CoordinateTransform(Dst, Src, pcal->lCenter);
  2132. cal.lMax = CHid_CoordinateTransform(Dst, Src, pcal->lMax);
  2133. pdiph = &cal.diph;
  2134. }
  2135. hres = CCal_SetProperty(pjrc, ppropi, pdiph, this->hkInstType, this->dwVersion);
  2136. /*
  2137. * If we successfully changed the calibration of a joystick
  2138. * device, then see if it's a VJOYD device.
  2139. */
  2140. if(SUCCEEDED(hres) &&
  2141. ppropi->pguid == DIPROP_CALIBRATION &&
  2142. GET_DIDEVICE_TYPE(this->dwDevType) == DIDEVTYPE_JOYSTICK)
  2143. {
  2144. CHid_UpdateVjoydCalibration(this, ppropi->iobj);
  2145. }
  2146. /*
  2147. * We've been call by an app so there's no point in calling
  2148. * Common_Hold/Unhold around this.
  2149. */
  2150. CHid_LoadCalibrations(this);
  2151. if( SUCCEEDED(hres) )
  2152. {
  2153. /*
  2154. * If this doesn't succeed, no big deal. So, we needn't check hres.
  2155. */
  2156. hres = CHid_InitParseData( this );
  2157. }
  2158. } else {
  2159. hres = E_NOTIMPL;
  2160. }
  2161. } else
  2162. {
  2163. hres = E_NOTIMPL;
  2164. }
  2165. }
  2166. }
  2167. } else
  2168. {
  2169. SquirtSqflPtszV(sqflHidDev | sqflError,
  2170. TEXT("CHid_SetProperty FAILED due to missing caps for type 0x%08x, obj %d"),
  2171. ppropi->dwDevType, ppropi->iobj );
  2172. hres = E_NOTIMPL;
  2173. }
  2174. } else if(ppropi->iobj == 0xFFFFFFFF)
  2175. { /* Device property */
  2176. switch((DWORD)(UINT_PTR)ppropi->pguid)
  2177. {
  2178. case (DWORD)(UINT_PTR)DIPROP_GUIDANDPATH:
  2179. SquirtSqflPtszV(sqflHidDev | sqflError,
  2180. TEXT("CHid_SetProperty(iobj=%08x): PROP_GUIDANDPATH is read only.") );
  2181. hres = E_NOTIMPL;
  2182. break;
  2183. case (DWORD)(UINT_PTR)DIPROP_INSTANCENAME:
  2184. /*
  2185. * DX8 CHANGE !
  2186. *
  2187. * Friendly names cause all manner of problems with devices that
  2188. * use auto detection so only allow non-predefined analog devices
  2189. * to use them.
  2190. */
  2191. if( ( this->VendorID == MSFT_SYSTEM_VID )
  2192. && ( this->ProductID >= MSFT_SYSTEM_PID + JOY_HW_PREDEFMAX )
  2193. && ( ( this->ProductID & 0xff00 ) == MSFT_SYSTEM_PID ) )
  2194. {
  2195. AssertF(this->hkType);
  2196. if( this->hkType )
  2197. {
  2198. LPDIPROPSTRING pstr = (PV)pdiph;
  2199. hres = JoyReg_SetValue(this->hkType,
  2200. REGSTR_VAL_JOYOEMNAME, REG_SZ,
  2201. pstr->wsz,
  2202. cbX(pstr->wsz));
  2203. if( SUCCEEDED(hres ) )
  2204. {
  2205. SquirtSqflPtszV(sqflHid | sqflVerbose,
  2206. TEXT( "Set instance name %s"), pstr->wsz );
  2207. hres = S_OK;
  2208. } else {
  2209. hres = E_FAIL;
  2210. }
  2211. } else {
  2212. hres = E_FAIL;
  2213. }
  2214. }
  2215. else
  2216. {
  2217. /*
  2218. * GenJ returns E_NOTIMPL for this property so do the same
  2219. */
  2220. hres = E_NOTIMPL;
  2221. }
  2222. break;
  2223. case (DWORD)(UINT_PTR)DIPROP_PRODUCTNAME:
  2224. if(fWinnt)
  2225. {
  2226. hres = DIHid_SetParentRegistryProperty(this->ptszId, SPDRP_DEVICEDESC, pdiph, 0x0 );
  2227. } else
  2228. {
  2229. hres = DIHid_SetRegistryProperty(this->ptszId, SPDRP_DEVICEDESC, pdiph);
  2230. }
  2231. break;
  2232. case (DWORD)(UINT_PTR)(DIPROP_ENABLEREPORTID):
  2233. {
  2234. LPDIPROPDWORD ppropdw = CONTAINING_RECORD(pdiph, DIPROPDWORD, diph);
  2235. UINT iType;
  2236. if( ppropdw->dwData == 0x0 )
  2237. {
  2238. for( iType = 0x0; iType < HidP_Max; iType++)
  2239. {
  2240. ZeroBuf(this->pEnableReportId[iType], this->wMaxReportId[iType]);
  2241. }
  2242. } else
  2243. {
  2244. for( iType = 0x0; iType < HidP_Max; iType++)
  2245. {
  2246. memset(this->pEnableReportId[iType], 0x1, this->wMaxReportId[iType]);
  2247. }
  2248. }
  2249. hres = S_OK;
  2250. }
  2251. break;
  2252. case (DWORD)(UINT_PTR)DIPROP_RANGE:
  2253. case (DWORD)(UINT_PTR)DIPROP_DEADZONE:
  2254. case (DWORD)(UINT_PTR)DIPROP_SATURATION:
  2255. case (DWORD)(UINT_PTR)DIPROP_CALIBRATIONMODE:
  2256. case (DWORD)(UINT_PTR)DIPROP_CALIBRATION:
  2257. {
  2258. /*
  2259. * Post DX7 Gold fix
  2260. * For axis properties, iterate through all objects on the
  2261. * device, setting the property on each absolute axis.
  2262. */
  2263. /*
  2264. * ISSUE-2001/03/29-timgill DX7 compat fix should be fixed for ME
  2265. * For minimum delta, go through a whole callback set
  2266. * property for each axis. For Millennium this should
  2267. * be fixed to use a common subroutine.
  2268. */
  2269. DIPROPCAL axisprop;
  2270. DIPROPINFO axispropinfo;
  2271. INT iObj;
  2272. HRESULT hresAxis;
  2273. axispropinfo.pguid = ppropi->pguid;
  2274. /*
  2275. * The largest property data we handle here is for the
  2276. * DIPROP_CALIBRATION.
  2277. */
  2278. AssertF( pdiph->dwSize <= cbX( axisprop ) );
  2279. /*
  2280. * Copy whatever we have and modify it for each axis
  2281. */
  2282. memcpy( &axisprop, pdiph, pdiph->dwSize );
  2283. axisprop.diph.dwHow = DIPH_BYID;
  2284. /*
  2285. * Make sure we only report real failures.
  2286. */
  2287. hres = S_OK;
  2288. for( iObj = this->df.dwNumObjs; iObj >= 0; iObj-- )
  2289. {
  2290. if( ( ( this->df.rgodf[iObj].dwType
  2291. & ( DIDFT_ALIAS | DIDFT_VENDORDEFINED | DIDFT_OUTPUT | DIDFT_ABSAXIS ) )
  2292. == DIDFT_ABSAXIS )
  2293. #ifdef WINNT
  2294. || ( ( this->df.rgodf[iObj].dwType
  2295. & ( DIDFT_ALIAS | DIDFT_VENDORDEFINED | DIDFT_OUTPUT | DIDFT_POV ) )
  2296. == DIDFT_POV )
  2297. #endif
  2298. )
  2299. {
  2300. axisprop.diph.dwObj = axispropinfo.dwDevType = this->df.rgodf[iObj].dwType;
  2301. axispropinfo.iobj = (UINT)iObj;
  2302. hresAxis = CHid_SetProperty(pdcb, (LPCDIPROPINFO)&axispropinfo, &axisprop.diph );
  2303. if( FAILED( hresAxis ) && ( hresAxis != E_NOTIMPL ) )
  2304. {
  2305. hres = hresAxis;
  2306. break;
  2307. }
  2308. }
  2309. }
  2310. }
  2311. break;
  2312. default:
  2313. SquirtSqflPtszV(sqflHidDev| sqflBenign,
  2314. TEXT("CHid_SetProperty(iobj=%08x): E_NOTIMPL on guid: %08x"),
  2315. ppropi->iobj, ppropi->pguid);
  2316. hres = E_NOTIMPL;
  2317. break;
  2318. }
  2319. } else
  2320. {
  2321. SquirtSqflPtszV(sqflHidDev | sqflError,
  2322. TEXT("CHid_SetProperty(iobj=%08x): E_NOTIMPL on guid: %08x"),
  2323. ppropi->iobj, ppropi->pguid);
  2324. hres = E_NOTIMPL;
  2325. }
  2326. ExitOleProcR();
  2327. return hres;
  2328. }
  2329. /*****************************************************************************
  2330. *
  2331. * @doc INTERNAL
  2332. *
  2333. * @method void | CHid | GetCapabilities |
  2334. *
  2335. * Get Hid device capabilities.
  2336. *
  2337. * @parm LPDIDEVCAPS | pdc |
  2338. *
  2339. * Device capabilities structure to receive result.
  2340. *
  2341. * @returns
  2342. * <c S_OK> on success.
  2343. *
  2344. *****************************************************************************/
  2345. STDMETHODIMP
  2346. CHid_GetCapabilities(PDICB pdcb, LPDIDEVCAPS pdc)
  2347. {
  2348. HRESULT hres;
  2349. PCHID this;
  2350. HANDLE h;
  2351. EnterProcI(IDirectInputDeviceCallback::Hid::GetCapabilities,
  2352. (_ "pp", pdcb, pdc));
  2353. /*
  2354. * This is an internal interface, so we can skimp on validation.
  2355. */
  2356. this = _thisPvNm(pdcb, dcb);
  2357. /*
  2358. * We must check connectivity by opening the device, because NT
  2359. * leaves the device in the info list even though it has
  2360. * been unplugged.
  2361. */
  2362. h = CHid_OpenDevicePath(this, FILE_FLAG_OVERLAPPED);
  2363. if(h != INVALID_HANDLE_VALUE)
  2364. {
  2365. CloseHandle(h);
  2366. if( !fWinnt )
  2367. {
  2368. VXDINITPARMS vip;
  2369. CHid_FindJoyDevice(this, &vip);
  2370. if( TRUE == CHid_IsMatchingJoyDevice(this, &vip) )
  2371. {
  2372. #ifdef DEBUG //always use HID path
  2373. TCHAR szJoyProp[] = REGSTR_PATH_PRIVATEPROPERTIES TEXT("\\Joystick");
  2374. HKEY hkJoyProp;
  2375. TCHAR szUseHid[] = TEXT("UseHidPath");
  2376. DWORD dwUseHid;
  2377. hres = hresMumbleKeyEx(HKEY_LOCAL_MACHINE,
  2378. szJoyProp,
  2379. DI_KEY_ALL_ACCESS,
  2380. REG_OPTION_NON_VOLATILE,
  2381. &hkJoyProp);
  2382. if( SUCCEEDED(hres) )
  2383. {
  2384. DWORD cb = sizeof(dwUseHid);
  2385. LONG lRc;
  2386. lRc = RegQueryValueEx(hkJoyProp, szUseHid, 0, 0, (LPBYTE)&dwUseHid, &cb);
  2387. if( lRc != ERROR_SUCCESS )
  2388. {
  2389. DWORD dwDefault = 1;
  2390. dwUseHid = dwDefault;
  2391. lRc = RegSetValueEx(hkJoyProp, szUseHid, 0, REG_DWORD, (LPBYTE)&dwDefault, cb);
  2392. }
  2393. RegCloseKey(hkJoyProp);
  2394. }
  2395. if( !dwUseHid )
  2396. {
  2397. pdc->dwFlags |= DIDC_ALIAS ;
  2398. }
  2399. #endif
  2400. }
  2401. }
  2402. #if !defined(WINNT) && DIRECTINPUT_VERSION > 0x050A
  2403. if( ( this->dwVersion < 0x0700 ) && ( this->dwVersion != 0x05B2 ) )
  2404. {
  2405. /*
  2406. * Post DX7 Gold Fix
  2407. * Keep this an alias for older apps.
  2408. */
  2409. pdc->dwFlags |= DIDC_ALIAS;
  2410. }
  2411. else if( this->hkType )
  2412. {
  2413. DWORD dwFlags1;
  2414. if( SUCCEEDED( JoyReg_GetValue( this->hkType,
  2415. REGSTR_VAL_FLAGS1, REG_BINARY,
  2416. &dwFlags1,
  2417. cbX(dwFlags1) ) ) )
  2418. {
  2419. if( dwFlags1 & JOYTYPE_NOHIDDIRECT )
  2420. {
  2421. pdc->dwFlags |= DIDC_ALIAS;
  2422. }
  2423. }
  2424. }
  2425. #endif
  2426. if( this->pvi->fl & VIFL_UNPLUGGED )
  2427. {
  2428. pdc->dwFlags &= ~DIDC_ATTACHED;
  2429. } else
  2430. {
  2431. pdc->dwFlags |= DIDC_ATTACHED;
  2432. }
  2433. } else
  2434. {
  2435. pdc->dwFlags &= ~DIDC_ATTACHED;
  2436. }
  2437. if( this->IsPolledInput )
  2438. {
  2439. pdc->dwFlags |= DIDC_POLLEDDEVICE;
  2440. }
  2441. pdc->dwDevType = this->dwDevType;
  2442. pdc->dwAxes = this->dwAxes;
  2443. pdc->dwButtons = this->dwButtons;
  2444. pdc->dwPOVs = this->dwPOVs;
  2445. hres = S_OK;
  2446. ExitOleProcR();
  2447. return hres;
  2448. }
  2449. /*****************************************************************************
  2450. *
  2451. * @doc INTERNAL
  2452. *
  2453. * @method HRESULT | CHid | GetDeviceState |
  2454. *
  2455. * Obtains the state of the Hid device.
  2456. *
  2457. * It is the caller's responsibility to have validated all the
  2458. * parameters and ensure that the device has been acquired.
  2459. *
  2460. * @parm OUT LPVOID | lpvData |
  2461. *
  2462. * Hid data in the preferred data format.
  2463. *
  2464. * @returns
  2465. *
  2466. * Returns a COM error code. The following error codes are
  2467. * intended to be illustrative and not necessarily comprehensive.
  2468. *
  2469. * <c DI_OK> = <c S_OK>: The operation completed successfully.
  2470. *
  2471. * <c DIERR_INVALIDPARAM> = <c E_INVALIDARG>: The
  2472. * <p lpmdr> parameter is not a valid pointer.
  2473. *
  2474. *****************************************************************************/
  2475. STDMETHODIMP
  2476. CHid_GetDeviceState(PDICB pdcb, LPVOID pvData)
  2477. {
  2478. HRESULT hres;
  2479. PCHID this;
  2480. EnterProcI(IDirectInputDeviceCallback::Hid::GetDeviceState,
  2481. (_ "pp", pdcb, pvData));
  2482. /*
  2483. * This is an internal interface, so we can skimp on validation.
  2484. */
  2485. this = _thisPvNm(pdcb, dcb);
  2486. AssertF(this->pvi);
  2487. AssertF(this->pvPhys);
  2488. AssertF(this->cbPhys);
  2489. if(this->pvi->fl & VIFL_ACQUIRED)
  2490. {
  2491. CHid_GetPhysicalState(this, pvData);
  2492. hres = S_OK;
  2493. } else
  2494. {
  2495. hres = DIERR_INPUTLOST;
  2496. }
  2497. ExitOleProcR();
  2498. return hres;
  2499. }
  2500. /*****************************************************************************
  2501. *
  2502. * @doc INTERNAL
  2503. *
  2504. * @method HRESULT | CHid | GetObjectInfo |
  2505. *
  2506. * Obtain the friendly name and FF/HID information
  2507. * of an object.
  2508. *
  2509. * @parm IN LPCDIPROPINFO | ppropi |
  2510. *
  2511. * Information describing the object being accessed.
  2512. *
  2513. * @parm IN OUT LPDIDEVICEOBJECTINSTANCEW | pdidioiW |
  2514. *
  2515. * Structure to receive information. All fields have been
  2516. * filled in up to the <e DIDEVICEOBJECTINSTANCE.tszObjName>.
  2517. *
  2518. * @returns
  2519. *
  2520. * Returns a COM error code.
  2521. *
  2522. *****************************************************************************/
  2523. STDMETHODIMP
  2524. CHid_GetObjectInfo(PDICB pdcb, LPCDIPROPINFO ppropi,
  2525. LPDIDEVICEOBJECTINSTANCEW pdidoiW)
  2526. {
  2527. HRESULT hres;
  2528. PCHID this;
  2529. EnterProcI(IDirectInputDeviceCallback::Hid::GetObjectInfo,
  2530. (_ "pxp", pdcb, ppropi->iobj, pdidoiW));
  2531. /*
  2532. * This is an internal interface, so we can skimp on validation.
  2533. */
  2534. this = _thisPvNm(pdcb, dcb);
  2535. AssertF((int)ppropi->iobj >= 0);
  2536. if(ppropi->iobj < this->df.dwNumObjs)
  2537. {
  2538. UINT uiInstance = ppropi->iobj;
  2539. PHIDGROUPCAPS pcaps;
  2540. AssertF(ppropi->dwDevType == this->df.rgodf[uiInstance].dwType);
  2541. AssertF(uiInstance == CHid_ObjFromType(this, ppropi->dwDevType));
  2542. pcaps = this->rghoc[uiInstance].pcaps;
  2543. /*
  2544. * pcaps might be NULL if HID messed up and left gaps
  2545. * in the index lists.
  2546. */
  2547. if(pcaps)
  2548. {
  2549. UINT ids, duiInstance;
  2550. AssertF(pcaps->dwSignature == HIDGROUPCAPS_SIGNATURE);
  2551. /*
  2552. * See if there's anything in the registry that will help.
  2553. */
  2554. CType_RegGetObjectInfo(this->hkType, ppropi->dwDevType, pdidoiW);
  2555. if(ppropi->dwDevType & DIDFT_COLLECTION)
  2556. {
  2557. ids = IDS_COLLECTIONTEMPLATE;
  2558. duiInstance = 0;
  2559. } else
  2560. {
  2561. if(ppropi->dwDevType & DIDFT_BUTTON)
  2562. {
  2563. ids = IDS_BUTTONTEMPLATE;
  2564. } else if(ppropi->dwDevType & DIDFT_AXIS)
  2565. {
  2566. ids = IDS_AXISTEMPLATE;
  2567. } else if(ppropi->dwDevType & DIDFT_POV)
  2568. {
  2569. ids = IDS_POVTEMPLATE;
  2570. } else
  2571. {
  2572. ids = IDS_UNKNOWNTEMPLATE;
  2573. }
  2574. /*
  2575. * Now convert the uiInstance to a duiInstance,
  2576. * giving the index of this object into the group.
  2577. */
  2578. AssertF(HidP_IsValidReportType(pcaps->type));
  2579. duiInstance = uiInstance -
  2580. (this->rgdwBase[pcaps->type] +
  2581. pcaps->DataIndexMin);
  2582. }
  2583. /*
  2584. * Okay, now we have all the info we need to proceed.
  2585. */
  2586. /*
  2587. * If there was no overriding name in the registry, then
  2588. * try to get a custom name from the usage page/usage.
  2589. * If even that fails, then use the generic name.
  2590. * Note, generic names will contain zero based numbers
  2591. * which can look wrong if some objects have names and
  2592. * others take defaults.
  2593. */
  2594. if(pdidoiW->tszName[0])
  2595. {
  2596. } else
  2597. if(GetHIDString(pcaps->UsageMin + duiInstance,
  2598. pcaps->UsagePage,
  2599. pdidoiW->tszName, cA(pdidoiW->tszName)))
  2600. {
  2601. if(ppropi->dwDevType & DIDFT_COLLECTION)
  2602. {
  2603. InsertCollectionNumber(DIDFT_GETINSTANCE( ppropi->dwDevType ),
  2604. pdidoiW->tszName);
  2605. }
  2606. } else
  2607. {
  2608. GetNthString(pdidoiW->tszName, ids,
  2609. DIDFT_GETINSTANCE( ppropi->dwDevType ));
  2610. }
  2611. if(pdidoiW->dwSize >= cbX(DIDEVICEOBJECTINSTANCE_DX5W))
  2612. {
  2613. pdidoiW->wCollectionNumber = pcaps->LinkCollection;
  2614. pdidoiW->wDesignatorIndex = pcaps->DesignatorMin + duiInstance;
  2615. if(pdidoiW->wDesignatorIndex > pcaps->DesignatorMax)
  2616. {
  2617. pdidoiW->wDesignatorIndex = pcaps->DesignatorMax;
  2618. }
  2619. /*
  2620. * Much as you may try, you cannot override the usage
  2621. * page and usage. Doing so would mess up the GUID
  2622. * selection code that happens in DIHIDINI.C.
  2623. *
  2624. * If you change your mind and allow overridden usage
  2625. * pages and usages, then you'll also have to change
  2626. * CHid_GetUsage.
  2627. *
  2628. * At this point, the registry overrides have already
  2629. * been read so defeat the override here.
  2630. */
  2631. pdidoiW->wUsagePage = pcaps->UsagePage;
  2632. pdidoiW->wUsage = pcaps->UsageMin + duiInstance;
  2633. pdidoiW->dwDimension = pcaps->Units;
  2634. pdidoiW->wExponent = pcaps->Exponent;
  2635. pdidoiW->wReportId = pcaps->wReportId;
  2636. }
  2637. hres = S_OK;
  2638. } else
  2639. {
  2640. hres = E_INVALIDARG;
  2641. }
  2642. } else
  2643. {
  2644. hres = E_INVALIDARG;
  2645. }
  2646. ExitOleProcR();
  2647. return hres;
  2648. }
  2649. /*****************************************************************************
  2650. *
  2651. * @doc INTERNAL
  2652. *
  2653. * @method DWORD | CHid | GetUsage |
  2654. *
  2655. * Given an object index, return the usage and usage page,
  2656. * packed into a single <t DWORD>.
  2657. *
  2658. * @parm int | iobj |
  2659. *
  2660. * The object index to convert.
  2661. *
  2662. * @returns
  2663. *
  2664. * Returns a <c DIMAKEUSAGEDWORD> of the resulting usage and
  2665. * usage page, or zero on error.
  2666. *
  2667. *****************************************************************************/
  2668. STDMETHODIMP_(DWORD)
  2669. CHid_GetUsage(PDICB pdcb, int iobj)
  2670. {
  2671. PCHID this;
  2672. PHIDGROUPCAPS pcaps;
  2673. DWORD dwRc;
  2674. EnterProcI(IDirectInputDeviceCallback::Hid::GetUsage,
  2675. (_ "pu", pdcb, iobj));
  2676. /*
  2677. * This is an internal interface, so we can skimp on validation.
  2678. */
  2679. this = _thisPvNm(pdcb, dcb);
  2680. AssertF(iobj >= 0);
  2681. AssertF((UINT)iobj < this->df.dwNumObjs);
  2682. pcaps = this->rghoc[iobj].pcaps;
  2683. /*
  2684. * pcaps might be NULL if HID messed up and left gaps
  2685. * in the index lists.
  2686. */
  2687. if(pcaps)
  2688. {
  2689. UINT duiInstance;
  2690. AssertF(pcaps->dwSignature == HIDGROUPCAPS_SIGNATURE);
  2691. if(this->df.rgodf[iobj].dwType & DIDFT_COLLECTION)
  2692. {
  2693. duiInstance = 0;
  2694. } else
  2695. {
  2696. /*
  2697. * Now convert the iobj to a duiInstance,
  2698. * giving the index of this object into the group.
  2699. */
  2700. AssertF(HidP_IsValidReportType(pcaps->type));
  2701. duiInstance = iobj -
  2702. (this->rgdwBase[pcaps->type] +
  2703. pcaps->DataIndexMin);
  2704. }
  2705. /*
  2706. * CHid_GetObjectInfo also assumes that there is no way
  2707. * to override the usage page and usage values in the
  2708. * registry.
  2709. */
  2710. dwRc = DIMAKEUSAGEDWORD(pcaps->UsagePage,
  2711. pcaps->UsageMin + duiInstance);
  2712. } else
  2713. {
  2714. dwRc = 0;
  2715. }
  2716. ExitProcX(dwRc);
  2717. return dwRc;
  2718. }
  2719. /*****************************************************************************
  2720. *
  2721. * @doc INTERNAL
  2722. *
  2723. * @method HRESULT | CHid | MapUsage |
  2724. *
  2725. *
  2726. * Given a usage and usage page (munged into a single
  2727. * <t DWORD>), find a device object that matches it.
  2728. *
  2729. * @parm DWORD | dwUsage |
  2730. *
  2731. * The usage page and usage combined into a single <t DWORD>
  2732. * with the <f DIMAKEUSAGEDWORD> macro.
  2733. *
  2734. * @parm PINT | piOut |
  2735. *
  2736. * Receives the object index of the found object, if successful.
  2737. *
  2738. * @returns
  2739. *
  2740. * Returns a COM error code.
  2741. *
  2742. * <c S_OK> if an object was found.
  2743. *
  2744. * <c DIERR_NOTFOUND> if no matching object was found.
  2745. *
  2746. *****************************************************************************/
  2747. STDMETHODIMP
  2748. CHid_MapUsage(PDICB pdcb, DWORD dwUsage, PINT piOut)
  2749. {
  2750. HRESULT hres;
  2751. PCHID this;
  2752. UINT icaps;
  2753. UINT uiObj;
  2754. UINT duiObj;
  2755. EnterProcI(IDirectInputDeviceCallback::Hid::MapUsage,
  2756. (_ "px", pdcb, dwUsage));
  2757. /*
  2758. * This is an internal interface, so we can skimp on validation.
  2759. */
  2760. this = _thisPvNm(pdcb, dcb);
  2761. for(icaps = 0; icaps < this->ccaps; icaps++)
  2762. {
  2763. PHIDGROUPCAPS pcaps = &this->rgcaps[icaps];
  2764. LPDIOBJECTDATAFORMAT podf;
  2765. /*
  2766. * Shall we support mapping HidP_Output usage?
  2767. * If we should, it is easy to add it later.
  2768. */
  2769. uiObj = this->rgdwBase[HidP_Input] + pcaps->DataIndexMin;
  2770. for(duiObj = 0; duiObj < pcaps->cObj; duiObj++)
  2771. {
  2772. podf = &this->df.rgodf[uiObj + duiObj];
  2773. if( dwUsage == GuidToUsage(podf->pguid) )
  2774. {
  2775. *piOut = uiObj+duiObj;
  2776. AssertF(*piOut < (INT)this->df.dwNumObjs);
  2777. hres = S_OK;
  2778. goto done;
  2779. }
  2780. }
  2781. }
  2782. hres = DIERR_NOTFOUND;
  2783. done:;
  2784. ExitBenignOleProcR();
  2785. return hres;
  2786. }
  2787. /*****************************************************************************
  2788. *
  2789. * @doc INTERNAL
  2790. *
  2791. * @method HRESULT | CHid | SetCooperativeLevel |
  2792. *
  2793. * Notify the device of the cooperative level.
  2794. *
  2795. * @parm IN HWND | hwnd |
  2796. *
  2797. * The window handle.
  2798. *
  2799. * @parm IN DWORD | dwFlags |
  2800. *
  2801. * The cooperativity level.
  2802. *
  2803. * @returns
  2804. *
  2805. * Returns a COM error code.
  2806. *
  2807. *****************************************************************************/
  2808. STDMETHODIMP
  2809. CHid_SetCooperativeLevel(PDICB pdcb, HWND hwnd, DWORD dwFlags)
  2810. {
  2811. HRESULT hres;
  2812. PCHID this;
  2813. EnterProcI(IDirectInputDeviceCallback::Hid::SetCooperativityLevel,
  2814. (_ "pxx", pdcb, hwnd, dwFlags));
  2815. /*
  2816. * This is an internal interface, so we can skimp on validation.
  2817. */
  2818. this = _thisPvNm(pdcb, dcb);
  2819. /*
  2820. * We won't subclass Motocross Madness. See NT bug 262280.
  2821. * Use the app hacks for MCM and any app like it.
  2822. */
  2823. #if (DIRECTINPUT_VERSION > 0x061A)
  2824. if( !this->diHacks.fNoSubClass )
  2825. #endif
  2826. {
  2827. AssertF(this->pvi);
  2828. /*
  2829. * First get out of the old window.
  2830. */
  2831. CHid_RemoveSubclass(this);
  2832. /*
  2833. * If a new window is passed, then subclass it so we can
  2834. * watch for joystick configuration change messages.
  2835. *
  2836. * If we can't, don't worry. All it means that we won't
  2837. * be able to catch when the user recalibrates a device,
  2838. * which isn't very often.
  2839. */
  2840. if(hwnd)
  2841. {
  2842. if(SetWindowSubclass(hwnd, CHid_SubclassProc, 0x0, (ULONG_PTR)this))
  2843. {
  2844. this->hwnd = hwnd;
  2845. Common_Hold(this);
  2846. }
  2847. } else
  2848. {
  2849. RPF("SetCooperativeLevel: You really shouldn't pass hwnd = 0; "
  2850. "device calibration may be dodgy");
  2851. }
  2852. }
  2853. hres = S_OK;
  2854. ExitOleProcR();
  2855. return hres;
  2856. }
  2857. /*****************************************************************************
  2858. *
  2859. * @doc INTERNAL
  2860. *
  2861. * @method HRESULT | CHid | RunControlPanel |
  2862. *
  2863. * Run the Hid control panel.
  2864. *
  2865. * @parm IN HWND | hwndOwner |
  2866. *
  2867. * The owner window.
  2868. *
  2869. * @parm DWORD | dwFlags |
  2870. *
  2871. * Flags.
  2872. *
  2873. *****************************************************************************/
  2874. STDMETHODIMP
  2875. CHid_RunControlPanel(PDICB pdcb, HWND hwnd, DWORD dwFlags)
  2876. {
  2877. HRESULT hres;
  2878. PCHID this;
  2879. EnterProcI(IDirectInputDeviceCallback::Hid::RunControlPanel,
  2880. (_ "pxx", pdcb, hwnd, dwFlags));
  2881. /*
  2882. * This is an internal interface, so we can skimp on validation.
  2883. */
  2884. this = _thisPvNm(pdcb, dcb);
  2885. /*
  2886. * How to invoke HID cpl?
  2887. *
  2888. * hres = (fWinnt) ? hresRunControlPanel(TEXT("srcmgr.cpl,@2")) :
  2889. * hresRunControlPanel(TEXT("sysdm.cpl,@0,1"));
  2890. *
  2891. * Currently, we just launch joy.cpl. If more HID devices show up
  2892. * which don't belong to game control panel, we may change it to
  2893. * proper cpl.
  2894. */
  2895. hres = hresRunControlPanel(TEXT("joy.cpl"));
  2896. ExitOleProcR();
  2897. return hres;
  2898. }
  2899. /*****************************************************************************
  2900. *
  2901. * @doc INTERNAL
  2902. *
  2903. * @method HRESULT | CHid | GetFFConfigKey |
  2904. *
  2905. * Open and return the registry key that contains
  2906. * force feedback configuration information.
  2907. *
  2908. * @parm DWORD | sam |
  2909. *
  2910. * Security access mask.
  2911. *
  2912. * @parm PHKEY | phk |
  2913. *
  2914. * Receives the registry key.
  2915. *
  2916. *****************************************************************************/
  2917. STDMETHODIMP
  2918. CHid_GetFFConfigKey(PDICB pdcb, DWORD sam, PHKEY phk)
  2919. {
  2920. HRESULT hres;
  2921. PCHID this;
  2922. EnterProcI(IDirectInputDeviceCallback::HID::GetFFConfigKey,
  2923. (_ "px", pdcb, sam));
  2924. /*
  2925. * This is an internal interface, so we can skimp on validation.
  2926. */
  2927. this = _thisPvNm(pdcb, dcb);
  2928. hres = JoyReg_OpenFFKey(this->hkType, sam, phk);
  2929. AssertF(fLeqvFF(SUCCEEDED(hres), *phk));
  2930. if(FAILED(hres) && this->fPIDdevice )
  2931. {
  2932. *phk = NULL;
  2933. hres = S_FALSE;
  2934. }
  2935. ExitBenignOleProcPpvR(phk);
  2936. return hres;
  2937. }
  2938. /*****************************************************************************
  2939. *
  2940. * @doc INTERNAL
  2941. *
  2942. * @method HRESULT | CHid | GetDeviceInfo |
  2943. *
  2944. * Obtain general information about the device.
  2945. *
  2946. * @parm OUT LPDIDEVICEINSTANCEW | pdiW |
  2947. *
  2948. * <t DEVICEINSTANCE> to be filled in. The
  2949. * <e DEVICEINSTANCE.dwSize> and <e DEVICEINSTANCE.guidInstance>
  2950. * have already been filled in.
  2951. *
  2952. * Secret convenience: <e DEVICEINSTANCE.guidProduct> is equal
  2953. * to <e DEVICEINSTANCE.guidInstance>.
  2954. *
  2955. *****************************************************************************/
  2956. STDMETHODIMP
  2957. CHid_GetDeviceInfo(PDICB pdcb, LPDIDEVICEINSTANCEW pdiW)
  2958. {
  2959. HRESULT hres;
  2960. PCHID this;
  2961. DIPROPINFO propi;
  2962. DIPROPSTRING dips;
  2963. EnterProcI(IDirectInputDeviceCallback::Hid::GetDeviceInfo,
  2964. (_ "pp", pdcb, pdiW));
  2965. /*
  2966. * This is an internal interface, so we can skimp on validation.
  2967. */
  2968. this = _thisPvNm(pdcb, dcb);
  2969. AssertF(IsValidSizeDIDEVICEINSTANCEW(pdiW->dwSize));
  2970. DICreateStaticGuid(&pdiW->guidProduct, this->ProductID, this->VendorID);
  2971. pdiW->dwDevType = this->dwDevType;
  2972. if(pdiW->dwSize >= cbX(DIDEVICEINSTANCE_DX5W))
  2973. {
  2974. pdiW->wUsagePage = this->caps.UsagePage;
  2975. pdiW->wUsage = this->caps.Usage;
  2976. }
  2977. propi.dwDevType = DIPH_DEVICE;
  2978. propi.iobj = 0xFFFFFFFF;
  2979. propi.pguid = DIPROP_PRODUCTNAME;
  2980. if(SUCCEEDED(hres = pdcb->lpVtbl->GetProperty(pdcb, &propi, &dips.diph)) )
  2981. {
  2982. lstrcpyW(pdiW->tszProductName, dips.wsz);
  2983. }
  2984. propi.pguid = DIPROP_INSTANCENAME;
  2985. if( FAILED(pdcb->lpVtbl->GetProperty(pdcb, &propi, &dips.diph)))
  2986. {
  2987. // Use Product Name
  2988. }
  2989. lstrcpyW(pdiW->tszInstanceName, dips.wsz);
  2990. #ifdef IDirectInputDevice2Vtbl
  2991. if(pdiW->dwSize >= cbX(DIDEVICEINSTANCE_DX5W))
  2992. {
  2993. HKEY hkFF;
  2994. HRESULT hresFF;
  2995. /*
  2996. * If there is a force feedback driver, then fetch the driver CLSID
  2997. * as the FF GUID.
  2998. */
  2999. hresFF = CHid_GetFFConfigKey(pdcb, KEY_QUERY_VALUE, &hkFF);
  3000. if(SUCCEEDED(hresFF))
  3001. {
  3002. LONG lRc;
  3003. TCHAR tszClsid[ctchGuid];
  3004. lRc = RegQueryString(hkFF, TEXT("CLSID"), tszClsid, cA(tszClsid));
  3005. if(lRc == ERROR_SUCCESS &&
  3006. ParseGUID(&pdiW->guidFFDriver, tszClsid))
  3007. {
  3008. } else
  3009. {
  3010. ZeroX(pdiW->guidFFDriver);
  3011. }
  3012. RegCloseKey(hkFF);
  3013. }
  3014. }
  3015. #endif
  3016. ExitOleProcR();
  3017. return hres;
  3018. }
  3019. /*****************************************************************************
  3020. *
  3021. * @doc INTERNAL
  3022. *
  3023. * @method HRESULT | CHid | CreateEffect |
  3024. *
  3025. *
  3026. * Create an <i IDirectInputEffectDriver> interface.
  3027. *
  3028. * @parm LPDIRECTINPUTEFFECTSHEPHERD * | ppes |
  3029. *
  3030. * Receives the shepherd for the effect driver.
  3031. *
  3032. *****************************************************************************/
  3033. STDMETHODIMP
  3034. CHid_CreateEffect(PDICB pdcb, LPDIRECTINPUTEFFECTSHEPHERD *ppes)
  3035. {
  3036. HRESULT hres;
  3037. PCHID this;
  3038. HKEY hk;
  3039. EnterProcI(IDirectInputDeviceCallback::HID::CreateEffect, (_ "p", pdcb));
  3040. /*
  3041. * This is an internal interface, so we can skimp on validation.
  3042. */
  3043. this = _thisPvNm(pdcb, dcb);
  3044. hres = CHid_GetFFConfigKey(pdcb, KEY_QUERY_VALUE, &hk);
  3045. if(SUCCEEDED(hres))
  3046. {
  3047. DIHIDFFINITINFO init;
  3048. PHIDDEVICEINFO phdi;
  3049. hres = CEShep_New(hk, 0, &IID_IDirectInputEffectShepherd, ppes);
  3050. if(SUCCEEDED(hres))
  3051. {
  3052. #ifndef UNICODE
  3053. WCHAR wszPath[MAX_PATH];
  3054. #endif
  3055. init.dwSize = cbX(init);
  3056. #ifdef UNICODE
  3057. init.pwszDeviceInterface = this->ptszPath;
  3058. #else
  3059. init.pwszDeviceInterface = wszPath;
  3060. TToU(wszPath, cA(wszPath), this->ptszPath);
  3061. #endif
  3062. DllEnterCrit();
  3063. phdi = phdiFindHIDDeviceInterface(this->ptszPath);
  3064. if( phdi )
  3065. {
  3066. init.GuidInstance = phdi->guid;
  3067. } else
  3068. {
  3069. ZeroX(init.GuidInstance);
  3070. }
  3071. DllLeaveCrit();
  3072. hres = (*ppes)->lpVtbl->DeviceID((*ppes), this->idJoy, TRUE, &init);
  3073. if(SUCCEEDED(hres))
  3074. {
  3075. } else
  3076. {
  3077. Invoke_Release(ppes);
  3078. }
  3079. }
  3080. if (hk != NULL)
  3081. {
  3082. RegCloseKey(hk);
  3083. }
  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