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.

392 lines
12 KiB

  1. #ifndef __ControlItemCollection_h__
  2. #define __ControlItemCollection_h__
  3. //@doc
  4. /*********************************************************************
  5. *
  6. * @module ControlItemCollection.h |
  7. *
  8. * Declares the ControlItemCollection class and related classes
  9. *
  10. * History
  11. * ----------------------------------------------------------
  12. * Mitchell S. Dernis Original
  13. *
  14. * (c) 1986-1998 Microsoft Corporation. All right reserved.
  15. *
  16. * @topic CControlItemCollection |
  17. * The ControlItemCollection class is a templatized class used for
  18. * representing the controls on a device. The depends on the client
  19. * using the class are non-trivial and therefore the following sections
  20. * should be read carefully.<nl>
  21. *
  22. * @topic Items in ControlItemCollection |
  23. * <c CControlItem> is the base class for all of the items in the collection.
  24. * X and Y axes on a joystick like device use <c XAxesItem>, DPAD uses
  25. * <c CDPADItem>, a proportional DPAD (as on Zorro, and also use for Tilt)
  26. * uses <c CPropDPADItem>, Buttons use the class <c CButtonsItem>, Throttle
  27. * uses <c CThrottleItem>, a Rudder uses <c CRudderItem>, etc. All of there
  28. * class are derived virtually from <c CControlITem>.<nl>
  29. *
  30. * @topic Custom Base class for <c CControlItem> |
  31. * The class was designed so that a client may create a custom base class.
  32. * To declare a collection of <c CMyBaseClass>, derive it *virtually*
  33. * from CControlItem. Then derive each of the classes created by your factory
  34. * from the appropriate control specific derivative and from <c CMyBaseClass>.
  35. * For example, <c CMyButtonsItems> would be derived from <c CButtonsItems> and
  36. * <c CMyBaseClass>. When creating the collection specify <c CMyBaseClass> in
  37. * template specifier for CControlItemCollection.<nl>
  38. *
  39. *
  40. *********************************************************************/
  41. //
  42. // Warning 4250 notifies you that some virtual functions are inherited via
  43. // dominance (See a good C++ text on virtual base classes). This warning
  44. // would be generated by client code if they use scheme described above
  45. // for a custom base class for controls.
  46. //
  47. #pragma warning( disable : 4250 )
  48. #include "ListAsArray.h"
  49. #include "ControlItems.h"
  50. //
  51. // @struct DEVICE_CONTROLS_DESC | Describes a paritcular device in terms
  52. // of its VidPid, it CONTROL_ITEM_DESC and it MODIFIER_DESC_TABLE.
  53. //
  54. struct DEVICE_CONTROLS_DESC
  55. {
  56. ULONG ulVidPid; //@field VID in high word and PID in low
  57. ULONG ulControlItemCount; //@field Number of control descriptors
  58. RAW_CONTROL_ITEM_DESC *pControlItems; //@field Array of control descriptors
  59. MODIFIER_DESC_TABLE *pModifierDescTable; //@field Pointer to modifier descriptor table
  60. };
  61. typedef DEVICE_CONTROLS_DESC *PDEVICE_CONTROLS_DESC;
  62. //
  63. // This table is assumed to be linked in.
  64. //
  65. extern DEVICE_CONTROLS_DESC DeviceControlsDescList[];
  66. typedef HRESULT (*PFNGENERICFACTORY)
  67. (
  68. USHORT usType,
  69. const CONTROL_ITEM_DESC* cpControlItemDesc,
  70. PVOID *ppControlItem
  71. );
  72. typedef CControlItem *(*PFNGETCONTROLITEM)(PVOID);
  73. typedef void (*PFNGENERICDELETEFUNC)(PVOID pItem);
  74. //
  75. // @class | Implementation of CControlItemCollection
  76. //
  77. class CControlItemCollectionImpl
  78. {
  79. public:
  80. //
  81. // @method | CControlItemCollection | CControlItemCollection |
  82. // C'tor, takes preparsed data and a factory to generate classes
  83. //
  84. CControlItemCollectionImpl():m_ulMaxXfers(0), m_ulModifiers(0){}
  85. HRESULT Init(
  86. ULONG ulVidPid, //@parm [in] Vid in the high word, Pid in the low word
  87. PFNGENERICFACTORY pfnFactory, //@parm [in] pointer to function which acts
  88. // on pFactoryHandle
  89. PFNGENERICDELETEFUNC pfnDeleteFunc //@parm [in] delete function to used for control items
  90. );
  91. //
  92. // @method | CControlItemCollection | GetFirst |
  93. // Returns next control item in collection. If *pulCookie=0 on entry , returns first object.
  94. // @rdesc TRUE on success, FALSE if no more items in collection
  95. //
  96. HRESULT GetNext(
  97. PVOID *ppControlItem, //@parm [out] Next control item in collection
  98. ULONG& rulCookie //@parm [in\out] Cookie to continue enumeration
  99. ) const;
  100. //
  101. // @method | CControlItemCollection | GetFromControlItemXfer |
  102. // Given a ControlItemXfer gets the corresponding control item from the collection.
  103. // @rdesc Pointer to item, or NULL if not found
  104. PVOID GetFromControlItemXfer(
  105. const CONTROL_ITEM_XFER& crControlItemXfer //@parm [in] report selector to get object for
  106. );
  107. //
  108. // Read\Write to Report
  109. //
  110. NTSTATUS ReadFromReport(
  111. PHIDP_PREPARSED_DATA pHidPreparsedData,
  112. PCHAR pcReport,
  113. LONG lReportLength,
  114. PFNGETCONTROLITEM GetControlFromPt
  115. );
  116. NTSTATUS WriteToReport(
  117. PHIDP_PREPARSED_DATA pHidPreparsedData,
  118. PCHAR pcReport,
  119. LONG lReportLength,
  120. PFNGETCONTROLITEM GetControlFromPtr
  121. ) const;
  122. //
  123. // Functions dealing with internal state
  124. //
  125. inline ULONG GetMaxXferRequired() const
  126. {
  127. return m_ulMaxXfers;
  128. }
  129. void SetDefaultState(
  130. PFNGETCONTROLITEM GetControlFromPtr
  131. );
  132. HRESULT GetState(
  133. ULONG& ulXferCount,
  134. PCONTROL_ITEM_XFER pControlItemXfers,
  135. PFNGETCONTROLITEM GetControlFromPtr
  136. );
  137. HRESULT SetState(
  138. ULONG ulXferCount,
  139. PCONTROL_ITEM_XFER pControlItemXfers,
  140. PFNGETCONTROLITEM GetControlFromPtr
  141. );
  142. void SetStateOverlayMode(
  143. PFNGETCONTROLITEM GetControlFromPtr,
  144. BOOLEAN fEnable
  145. );
  146. inline ULONG GetModifiers() const
  147. {
  148. return m_ulModifiers;
  149. }
  150. inline void SetModifiers(ULONG ulModifiers)
  151. {
  152. m_ulModifiers = ulModifiers;
  153. }
  154. private:
  155. CListAsArray m_ObjectList;
  156. ULONG m_ulDeviceIndex;
  157. ULONG m_ulModifiers;
  158. ULONG m_ulMaxXfers;
  159. };
  160. // @class CControlItemCollection |
  161. // An instance of this class contains a list of control item objects which represent
  162. // the controls on the device. The numbers and types of objects depend
  163. // not only the configuration of the device (obtained through the VidPid and a lookup table
  164. // - passed in the c'tor), but also on the CControlItemFactory which pairs
  165. // each control item with an object.
  166. // @tcarg class | T | Custom base class for control items to store in collection.
  167. template<class T>
  168. class CControlItemCollection
  169. {
  170. public:
  171. typedef HRESULT (*PFNFACTORY)
  172. (
  173. USHORT usType,
  174. const CONTROL_ITEM_DESC* cpControlItemDesc,
  175. T **ppDeviceUsage
  176. );
  177. typedef void (*PFNDELETEFUNC)(T *pItem);
  178. //
  179. // @method | CControlItemCollection | CControlItemCollection |
  180. // C'tor, takes a VidPid and a factory to generate classes
  181. //
  182. CControlItemCollection(
  183. ULONG ulVidPid, //@parm [in] Vid in the high word, Pid in the low word
  184. PFNFACTORY pfnFactory, //@parm [in] pointer to factory method for control item objects
  185. PFNDELETEFUNC pfnDeleteFunc=NULL //@parm [in] pointer to function for deleting the objects
  186. )
  187. {
  188. if(!pfnDeleteFunc)
  189. {
  190. pfnDeleteFunc = DefaultDeleteFunc;
  191. }
  192. //
  193. // Init implementation
  194. //
  195. collectionImpl.Init(
  196. ulVidPid,
  197. reinterpret_cast<PFNGENERICFACTORY>(pfnFactory),
  198. reinterpret_cast<PFNGENERICDELETEFUNC>(pfnDeleteFunc)
  199. );
  200. }
  201. //
  202. // @method | CControlItemCollection | CControlItemCollection |
  203. // C'tor, which takes no parameters, much call init before using!
  204. //
  205. CControlItemCollection() : collectionImpl() {};
  206. //
  207. // @method | CControlItemCollection | Init |
  208. // Must call after the default constructor, does the collection initialization
  209. //
  210. HRESULT Init(
  211. ULONG ulVidPid, //@parm [in] Vid in the high word, Pid in the low word
  212. PFNFACTORY pfnFactory, //@parm [in] pointer to factory method for control item objects
  213. PFNDELETEFUNC pfnDeleteFunc=NULL //@parm [in] pointer to function for deleting the objects
  214. )
  215. {
  216. if(!pfnDeleteFunc)
  217. {
  218. pfnDeleteFunc = DefaultDeleteFunc;
  219. }
  220. //
  221. // Init implementation
  222. //
  223. return collectionImpl.Init(
  224. ulVidPid,
  225. reinterpret_cast<PFNGENERICFACTORY>(pfnFactory),
  226. reinterpret_cast<PFNGENERICDELETEFUNC>(pfnDeleteFunc)
  227. );
  228. }
  229. //
  230. // @method | CControlItemCollection | GetFirst |
  231. // Returns next control item in the collection. If *pulCookie=0 on entry , returns first object.
  232. // @rdesc TRUE on success, FALSE if no more objects in collection
  233. //
  234. inline HRESULT GetNext(
  235. T **ppControlItem, //@parm [out] Next control item object in collection
  236. ULONG& rulCookie //@parm [in\out] Cookie to continue enumeration
  237. ) const
  238. {
  239. //
  240. // Defer to implementation
  241. //
  242. return collectionImpl.GetNext( reinterpret_cast<PVOID *>(ppControlItem), rulCookie );
  243. }
  244. //
  245. // @method | CControlItemCollection | GetFromControlItemXfer |
  246. // Given a ControlItemXfer gets the corresponding control item object from the collection.
  247. // @rdesc Pointer to Control Item, or NULL if not found.
  248. inline T *GetFromControlItemXfer(
  249. const CONTROL_ITEM_XFER& crControlItemXfer //@parm [in] ControlItemXfer to get control item object for
  250. )
  251. {
  252. //
  253. // Defer to implementation
  254. //
  255. return reinterpret_cast<T *>(collectionImpl.GetFromControlItemXfer(crControlItemXfer));
  256. };
  257. static CControlItem *GetControlFromPtr(PVOID pvItem)
  258. {
  259. return static_cast<CControlItem *>(reinterpret_cast<T *>(pvItem));
  260. }
  261. static void DefaultDeleteFunc(T *pItem)
  262. {
  263. delete pItem;
  264. }
  265. //
  266. // Read\Write to Report
  267. //
  268. inline NTSTATUS ReadFromReport(
  269. PHIDP_PREPARSED_DATA pHidPPreparsedData,
  270. PCHAR pcReport,
  271. LONG lReportLength
  272. )
  273. {
  274. return collectionImpl.ReadFromReport(
  275. pHidPPreparsedData,
  276. pcReport,
  277. lReportLength,
  278. &GetControlFromPtr
  279. );
  280. }
  281. inline NTSTATUS WriteToReport(
  282. PHIDP_PREPARSED_DATA pHidPPreparsedData,
  283. PCHAR pcReport,
  284. LONG lReportLength
  285. ) const
  286. {
  287. return collectionImpl.WriteToReport(
  288. pHidPPreparsedData,
  289. pcReport,
  290. lReportLength,
  291. &GetControlFromPtr
  292. );
  293. }
  294. inline void SetDefaultState()
  295. {
  296. collectionImpl.SetDefaultState(&GetControlFromPtr);
  297. }
  298. inline ULONG GetMaxXferRequired() const
  299. {
  300. return collectionImpl.GetMaxXferRequired();
  301. }
  302. inline HRESULT GetState( ULONG& ulXferCount, PCONTROL_ITEM_XFER pControlItemXfers)
  303. {
  304. return collectionImpl.GetState(
  305. ulXferCount,
  306. pControlItemXfers,
  307. &GetControlFromPtr
  308. );
  309. }
  310. inline HRESULT SetState( ULONG& ulXferCount, PCONTROL_ITEM_XFER pControlItemXfers)
  311. {
  312. return collectionImpl.SetState(
  313. ulXferCount,
  314. pControlItemXfers,
  315. &GetControlFromPtr
  316. );
  317. }
  318. inline ULONG GetModifiers() const
  319. {
  320. return collectionImpl.GetModifiers();
  321. }
  322. inline void SetModifiers(ULONG ulModifiers)
  323. {
  324. collectionImpl.SetModifiers(ulModifiers);
  325. }
  326. inline void SetStateOverlayMode(BOOLEAN fEnable)
  327. {
  328. collectionImpl.SetStateOverlayMode(&GetControlFromPtr, fEnable);
  329. }
  330. private:
  331. CControlItemCollectionImpl collectionImpl;
  332. };
  333. HRESULT
  334. ControlItemDefaultFactory
  335. (
  336. USHORT usType,
  337. const CONTROL_ITEM_DESC* cpControlItemDesc,
  338. CControlItem **ppControlItem
  339. );
  340. //
  341. // @class Default collection where objects in collection have only the
  342. // the CControlItem class as their base classes.
  343. //
  344. class CControlItemDefaultCollection : public CControlItemCollection<CControlItem>
  345. {
  346. public:
  347. CControlItemDefaultCollection(ULONG ulVidPid)
  348. : CControlItemCollection<CControlItem>(ulVidPid, &ControlItemDefaultFactory)
  349. {}
  350. };
  351. #endif //__ControlItemCollection_h__