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.

660 lines
15 KiB

  1. /*******************************************************************************
  2. *
  3. * (C) COPYRIGHT MICROSOFT CORP., 1998
  4. *
  5. * TITLE: WiaDev.Cpp
  6. *
  7. * VERSION: 2.0
  8. *
  9. * AUTHOR: ReedB
  10. *
  11. * DATE: 30 Aug, 1998
  12. *
  13. * DESCRIPTION:
  14. * Implementation of the WIA test scanner mini driver
  15. * device methods.
  16. *
  17. *******************************************************************************/
  18. #define __FORMATS_AND_MEDIA_TYPES__
  19. #include <stdio.h>
  20. #include <objbase.h>
  21. #include <sti.h>
  22. #include "testusd.h"
  23. #include "defprop.h"
  24. extern HINSTANCE g_hInst; // Global hInstance
  25. extern WIA_FORMAT_INFO* g_wfiTable;
  26. /**************************************************************************\
  27. * TestUsdDevice::InitializWia
  28. *
  29. *
  30. *
  31. * Arguments:
  32. *
  33. *
  34. *
  35. * Return Value:
  36. *
  37. * Status
  38. *
  39. * History:
  40. *
  41. * 9/11/1998 Original Version
  42. *
  43. \**************************************************************************/
  44. HRESULT _stdcall TestUsdDevice::drvInitializeWia(
  45. BYTE *pWiasContext,
  46. LONG lFlags,
  47. BSTR bstrDeviceID,
  48. BSTR bstrRootFullItemName,
  49. IUnknown *pStiDevice,
  50. IUnknown *pIUnknownOuter,
  51. IWiaDrvItem **ppIDrvItemRoot,
  52. IUnknown **ppIUnknownInner,
  53. LONG *plDevErrVal)
  54. {
  55. HRESULT hr;
  56. LONG lDevErrVal;
  57. WIAS_TRACE((g_hInst,"drvInitializeWia, device ID: %ws", bstrDeviceID));
  58. *ppIDrvItemRoot = NULL;
  59. *ppIUnknownInner = NULL;
  60. *plDevErrVal = 0;
  61. //
  62. // Need to init names and STI pointer?
  63. //
  64. if (m_pStiDevice == NULL) {
  65. //
  66. // save STI device inteface for locking
  67. //
  68. m_pStiDevice = (IStiDevice *)pStiDevice;
  69. //
  70. // Cache the device ID
  71. //
  72. m_bstrDeviceID = SysAllocString(bstrDeviceID);
  73. if (! m_bstrDeviceID) {
  74. return (E_OUTOFMEMORY);
  75. }
  76. m_bstrRootFullItemName = SysAllocString(bstrRootFullItemName);
  77. if (! m_bstrRootFullItemName) {
  78. return (E_OUTOFMEMORY);
  79. }
  80. }
  81. //
  82. // Build the device item tree
  83. //
  84. hr = drvDeviceCommand(NULL, 0, &WIA_CMD_SYNCHRONIZE, NULL, &lDevErrVal);
  85. if (SUCCEEDED(hr)) {
  86. *ppIDrvItemRoot = m_pIDrvItemRoot;
  87. }
  88. return (hr);
  89. }
  90. /**************************************************************************\
  91. * TestUsdDevice::drvUnInitializeWia
  92. *
  93. * Gets called when a client connection is going away.
  94. *
  95. * Arguments:
  96. *
  97. * pWiasContext - Pointer to the WIA Root item context of the client's
  98. * item tree.
  99. *
  100. * Return Value:
  101. * Status
  102. *
  103. * History:
  104. *
  105. * 30/12/1999 Original Version
  106. *
  107. \**************************************************************************/
  108. HRESULT _stdcall TestUsdDevice::drvUnInitializeWia(
  109. BYTE *pWiasContext)
  110. {
  111. return S_OK;
  112. }
  113. /**************************************************************************\
  114. *
  115. * Mini Driver Device Services
  116. *
  117. \**************************************************************************/
  118. /**************************************************************************\
  119. * drvGetDeviceErrorStr
  120. *
  121. * Map a device error value to a string.
  122. *
  123. * Arguments:
  124. *
  125. *
  126. *
  127. * Return Value:
  128. *
  129. * Status
  130. *
  131. * History:
  132. *
  133. * 10/2/1998 Original Version
  134. *
  135. \**************************************************************************/
  136. HRESULT _stdcall TestUsdDevice::drvGetDeviceErrorStr(
  137. LONG lFlags,
  138. LONG lDevErrVal,
  139. LPOLESTR *ppszDevErrStr,
  140. LONG *plDevErr)
  141. {
  142. *plDevErr = 0;
  143. if (!ppszDevErrStr) {
  144. WIAS_ERROR((g_hInst,"drvGetDeviceErrorStr, NULL ppszDevErrStr"));
  145. return E_POINTER;
  146. }
  147. // Map device errors to a string to be placed in the event log.
  148. switch (lDevErrVal) {
  149. case 0:
  150. *ppszDevErrStr = L"No Error";
  151. break;
  152. default:
  153. *ppszDevErrStr = L"Device Error, Unknown Error";
  154. return E_FAIL;
  155. }
  156. return S_OK;
  157. }
  158. /**************************************************************************\
  159. * DeleteDeviceItemTree
  160. *
  161. * Recursive device item tree delete routine. Deletes the whole tree.
  162. *
  163. * Arguments:
  164. *
  165. *
  166. *
  167. * Return Value:
  168. *
  169. * Status
  170. *
  171. * History:
  172. *
  173. * 10/2/1998 Original Version
  174. *
  175. \**************************************************************************/
  176. HRESULT TestUsdDevice::DeleteDeviceItemTree(
  177. LONG *plDevErrVal)
  178. {
  179. HRESULT hr;
  180. //
  181. // does tree exist
  182. //
  183. if (m_pIDrvItemRoot == NULL) {
  184. return S_OK;
  185. }
  186. //
  187. // Unlink and release the driver item tree.
  188. //
  189. hr = m_pIDrvItemRoot->UnlinkItemTree(WiaItemTypeDisconnected);
  190. m_pIDrvItemRoot = NULL;
  191. return hr;
  192. }
  193. /**************************************************************************\
  194. * BuildDeviceItemTree
  195. *
  196. * The device uses the IWiaDrvServices methods to build up a tree of
  197. * device items.
  198. *
  199. * Arguments:
  200. *
  201. *
  202. *
  203. * Return Value:
  204. *
  205. * Status
  206. *
  207. * History:
  208. *
  209. * 10/2/1998 Original Version
  210. *
  211. \**************************************************************************/
  212. HRESULT TestUsdDevice::BuildDeviceItemTree(
  213. LONG *plDevErrVal)
  214. {
  215. HRESULT hr = S_OK;
  216. //
  217. // This device doesn't touch hardware to build the tree.
  218. //
  219. if (plDevErrVal) {
  220. *plDevErrVal = 0;
  221. }
  222. CAMERA_STATUS camStatus;
  223. if (! m_pIDrvItemRoot) {
  224. hr = CamBuildImageTree(&camStatus, &m_pIDrvItemRoot);
  225. }
  226. return hr;
  227. }
  228. /**************************************************************************\
  229. * InitDeviceProperties
  230. *
  231. *
  232. *
  233. * Arguments:
  234. *
  235. *
  236. *
  237. * Return Value:
  238. *
  239. * Status
  240. *
  241. * History:
  242. *
  243. * 10/2/1998 Original Version
  244. *
  245. \**************************************************************************/
  246. HRESULT TestUsdDevice::InitDeviceProperties(
  247. BYTE *pWiasContext,
  248. LONG *plDevErrVal)
  249. {
  250. HRESULT hr;
  251. BSTR bstrFirmwreVer;
  252. SYSTEMTIME camTime;
  253. int i;
  254. PROPVARIANT propVar;
  255. //
  256. // This device doesn't touch hardware to initialize the device properties.
  257. //
  258. if (plDevErrVal) {
  259. *plDevErrVal = 0;
  260. }
  261. //
  262. // Parameter validation.
  263. //
  264. if (! pWiasContext) {
  265. WIAS_ERROR((g_hInst,"InitDeviceProperties, invalid input pointers"));
  266. return E_POINTER;
  267. }
  268. //
  269. // Write standard property names
  270. //
  271. hr = wiasSetItemPropNames(pWiasContext,
  272. sizeof(gDevicePropIDs)/sizeof(PROPID),
  273. gDevicePropIDs,
  274. gDevicePropNames);
  275. if (FAILED(hr)) {
  276. WIAS_TRACE((g_hInst,"InitDeviceProperties() WritePropertyNames() failed"));
  277. return (hr);
  278. }
  279. //
  280. // Write the properties supported by all WIA devices
  281. //
  282. bstrFirmwreVer = SysAllocString(L"02161999");
  283. if (bstrFirmwreVer) {
  284. wiasWritePropStr(
  285. pWiasContext, WIA_DPA_FIRMWARE_VERSION, bstrFirmwreVer);
  286. SysFreeString(bstrFirmwreVer);
  287. }
  288. wiasWritePropLong(
  289. pWiasContext, WIA_DPA_CONNECT_STATUS, 1);
  290. wiasWritePropLong(
  291. pWiasContext, WIA_DPC_PICTURES_TAKEN, 0);
  292. GetSystemTime(&camTime);
  293. wiasWritePropBin(
  294. pWiasContext, WIA_DPA_DEVICE_TIME,
  295. sizeof(SYSTEMTIME), (PBYTE)&camTime);
  296. //
  297. // Write the camera properties, just default values, it may vary with items
  298. //
  299. wiasWritePropLong(
  300. pWiasContext, WIA_DPC_PICTURES_REMAINING, 0);
  301. wiasWritePropLong(
  302. pWiasContext, WIA_DPC_THUMB_WIDTH, 80);
  303. wiasWritePropLong(
  304. pWiasContext, WIA_DPC_THUMB_HEIGHT, 60);
  305. wiasWritePropLong(
  306. pWiasContext, WIA_DPC_PICT_WIDTH, 1024);
  307. wiasWritePropLong(
  308. pWiasContext, WIA_DPC_PICT_HEIGHT, 768);
  309. // Give WIA_DPC_EXPOSURE_MODE to WIA_DPC_TIMER_VALUE some default.
  310. wiasWritePropLong(
  311. pWiasContext, WIA_DPC_EXPOSURE_MODE, 0);
  312. wiasWritePropLong(
  313. pWiasContext, WIA_DPC_FLASH_MODE, 1);
  314. wiasWritePropLong(
  315. pWiasContext, WIA_DPC_FOCUS_MODE, 0);
  316. wiasWritePropLong(
  317. pWiasContext, WIA_DPC_ZOOM_POSITION, 0);
  318. wiasWritePropLong(
  319. pWiasContext, WIA_DPC_BATTERY_STATUS, 1);
  320. wiasWritePropLong(
  321. pWiasContext, WIA_DPC_TIMER_MODE, 0);
  322. wiasWritePropLong(
  323. pWiasContext, WIA_DPC_TIMER_VALUE, 0);
  324. //
  325. // Write the WIA_DPP_TCAM_ROOT_PATH property
  326. //
  327. BSTR bstrRootPath;
  328. #ifdef UNICODE
  329. bstrRootPath = SysAllocString(gpszPath);
  330. #else
  331. WCHAR wszPath[MAX_PATH];
  332. mbstowcs(wszPath, gpszPath, strlen(gpszPath)+1);
  333. bstrRootPath = SysAllocString(wszPath);
  334. #endif
  335. if (! bstrRootPath) {
  336. return (E_OUTOFMEMORY);
  337. }
  338. wiasWritePropStr(pWiasContext, WIA_DPP_TCAM_ROOT_PATH, bstrRootPath);
  339. //
  340. // Use WIA services to set the property access and
  341. // valid value information from gDevPropInfoDefaults.
  342. //
  343. hr = wiasSetItemPropAttribs(pWiasContext,
  344. NUM_CAM_DEV_PROPS,
  345. gDevicePropSpecDefaults,
  346. gDevPropInfoDefaults);
  347. return (S_OK);
  348. }
  349. /**************************************************************************\
  350. * drvDeviceCommand
  351. *
  352. *
  353. *
  354. * Arguments:
  355. *
  356. *
  357. *
  358. * Return Value:
  359. *
  360. * Status
  361. *
  362. * History:
  363. *
  364. * 9/11/1998 Original Version
  365. *
  366. \**************************************************************************/
  367. HRESULT _stdcall TestUsdDevice::drvDeviceCommand(
  368. BYTE *pWiasContext,
  369. LONG lFlags,
  370. const GUID *plCommand,
  371. IWiaDrvItem **ppWiaDrvItem,
  372. LONG *plErr)
  373. {
  374. HRESULT hr;
  375. //
  376. // init return value
  377. //
  378. if (ppWiaDrvItem != NULL) {
  379. *ppWiaDrvItem = NULL;
  380. }
  381. //
  382. // dispatch command
  383. //
  384. if (*plCommand == WIA_CMD_SYNCHRONIZE) {
  385. hr = drvLockWiaDevice(pWiasContext, lFlags, plErr);
  386. if (FAILED(hr)) {
  387. return (hr);
  388. }
  389. //
  390. // SYNCHRONIZE - make sure tree is up to date with device
  391. //
  392. // The driver's responsibility is to make sure the tree is accurate.
  393. //
  394. hr = BuildDeviceItemTree(plErr);
  395. drvUnLockWiaDevice(pWiasContext, lFlags, plErr);
  396. return (hr);
  397. } else {
  398. WIAS_TRACE((g_hInst,"drvDeviceCommand: Unsupported command"));
  399. hr = E_NOTIMPL;
  400. }
  401. return hr;
  402. }
  403. /**************************************************************************\
  404. * drvGetCapabilities
  405. *
  406. *
  407. *
  408. * Arguments:
  409. *
  410. *
  411. *
  412. * Return Value:
  413. *
  414. * Status
  415. *
  416. * History:
  417. *
  418. * 17/3/1999 Original Version
  419. *
  420. \**************************************************************************/
  421. HRESULT _stdcall TestUsdDevice::drvGetCapabilities(
  422. BYTE *pWiasContext,
  423. LONG ulFlags,
  424. LONG *pCelt,
  425. WIA_DEV_CAP_DRV **ppCapabilities,
  426. LONG *plDevErrVal)
  427. {
  428. *plDevErrVal = 0;
  429. //
  430. // Return Commmand &| Events depending on flags
  431. //
  432. switch (ulFlags) {
  433. case WIA_DEVICE_COMMANDS:
  434. //
  435. // Only commands
  436. //
  437. *pCelt = NUM_CAP_ENTRIES - NUM_EVENTS;
  438. *ppCapabilities = &gCapabilities[NUM_EVENTS];
  439. break;
  440. case WIA_DEVICE_EVENTS:
  441. //
  442. // Only events
  443. //
  444. *pCelt = NUM_EVENTS;
  445. *ppCapabilities = gCapabilities;
  446. break;
  447. case (WIA_DEVICE_COMMANDS | WIA_DEVICE_EVENTS):
  448. //
  449. // Both events and commands
  450. //
  451. *pCelt = NUM_CAP_ENTRIES;
  452. *ppCapabilities = gCapabilities;
  453. break;
  454. default:
  455. //
  456. // Flags is invalid
  457. //
  458. WIAS_ERROR((g_hInst, "drvGetCapabilities, flags was invalid"));
  459. return (E_INVALIDARG);
  460. }
  461. return (S_OK);
  462. }
  463. /**************************************************************************\
  464. * drvGetWiaFormatInfo
  465. *
  466. * Returns an array of formats and media types supported.
  467. *
  468. * Arguments:
  469. *
  470. * pWiasContext - Pointer to the WIA item context, unused.
  471. * lFlags - Operation flags, unused.
  472. * pcelt - Pointer to returned number of elements in
  473. * returned WiaFormatInfo array.
  474. * ppfe - Pointer to returned WiaFormatInfo array.
  475. * plDevErrVal - Pointer to the device error value.
  476. *
  477. * Return Value:
  478. * A pointer to an array of FORMATETC. These are the formats and media
  479. * types supported.
  480. *
  481. * Status
  482. *
  483. * History:
  484. *
  485. * 16/03/1999 Original Version
  486. *
  487. \**************************************************************************/
  488. #define NUM_WIA_FORMAT_INFO 3
  489. HRESULT _stdcall TestUsdDevice::drvGetWiaFormatInfo(
  490. BYTE *pWiasContext,
  491. LONG lFlags,
  492. LONG *pcelt,
  493. WIA_FORMAT_INFO **ppwfi,
  494. LONG *plDevErrVal)
  495. {
  496. //
  497. // If it hasn't been done already, set up the g_wfiTable table
  498. //
  499. if (! g_wfiTable) {
  500. g_wfiTable = (WIA_FORMAT_INFO*)
  501. CoTaskMemAlloc(sizeof(WIA_FORMAT_INFO) * NUM_WIA_FORMAT_INFO);
  502. if (! g_wfiTable) {
  503. WIAS_ERROR((g_hInst, "drvGetWiaFormatInfo, out of memory"));
  504. return (E_OUTOFMEMORY);
  505. }
  506. //
  507. // Set up the format/tymed pairs
  508. //
  509. g_wfiTable[0].guidFormatID = WiaImgFmt_MEMORYBMP;
  510. g_wfiTable[0].lTymed = TYMED_CALLBACK;
  511. g_wfiTable[1].guidFormatID = WiaImgFmt_BMP;
  512. g_wfiTable[1].lTymed = TYMED_FILE;
  513. g_wfiTable[2].guidFormatID = WiaAudFmt_WAV;
  514. g_wfiTable[2].lTymed = TYMED_FILE;
  515. }
  516. *plDevErrVal = 0;
  517. *pcelt = NUM_WIA_FORMAT_INFO;
  518. *ppwfi = g_wfiTable;
  519. return S_OK;
  520. }
  521. /**************************************************************************\
  522. * drvNotifyPnpEvent
  523. *
  524. * Notify Pnp Event received by device manager
  525. *
  526. * Arguments:
  527. *
  528. *
  529. *
  530. * Return Value:
  531. *
  532. * Status
  533. *
  534. * History:
  535. *
  536. * Aug/3rd/1999 Original Version
  537. *
  538. \**************************************************************************/
  539. HRESULT _stdcall TestUsdDevice::drvNotifyPnpEvent(
  540. const GUID *pEventGUID,
  541. BSTR bstrDeviceID,
  542. ULONG ulReserved)
  543. {
  544. return (S_OK);
  545. }