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.

738 lines
18 KiB

  1. /*++
  2. Copyright (c) 1989-1998 Microsoft Corporation
  3. Module Name:
  4. camopen.cpp
  5. Abstract:
  6. Enumerate disk images to emulate camera
  7. Author:
  8. Mark Enstrom (marke) 1/13/1999
  9. Environment:
  10. user mode
  11. Revision History:
  12. --*/
  13. #include <stdio.h>
  14. #include <objbase.h>
  15. #include <tchar.h>
  16. #include "sti.h"
  17. #include "testusd.h"
  18. extern HINSTANCE g_hInst; // Global hInstance
  19. #define __GLOBALPROPVARS__
  20. #include "defprop.h"
  21. /**************************************************************************\
  22. * CamOpenCamera
  23. *
  24. * Load the camera driver
  25. *
  26. * Arguments:
  27. *
  28. * pGenericStatus - camera status
  29. *
  30. * Return Value:
  31. *
  32. * status
  33. *
  34. * History:
  35. *
  36. * 2/5/1998 Mark Enstrom [marke]
  37. *
  38. *
  39. \**************************************************************************/
  40. HRESULT
  41. TestUsdDevice::CamOpenCamera(
  42. CAMERA_STATUS *pGenericStatus
  43. )
  44. {
  45. HRESULT hr = S_OK;
  46. WIAS_TRACE((g_hInst,"CamOpenCamera"));
  47. //
  48. // init memory camera
  49. //
  50. pGenericStatus->FirmwareVersion = 0x00000001;
  51. pGenericStatus->NumPictTaken = 20;
  52. pGenericStatus->NumPictRemaining = 0;
  53. pGenericStatus->ThumbWidth = 80;
  54. pGenericStatus->ThumbHeight = 60;
  55. pGenericStatus->PictWidth = 300;
  56. pGenericStatus->PictHeight = 300;
  57. pGenericStatus->CameraTime.wSecond = 30;
  58. pGenericStatus->CameraTime.wMinute = 20;
  59. pGenericStatus->CameraTime.wHour = 13;
  60. pGenericStatus->CameraTime.wDay = 13;
  61. pGenericStatus->CameraTime.wMonth = 2;
  62. pGenericStatus->CameraTime.wYear = 98;
  63. pGenericStatus->CameraTime.wDayOfWeek = 6;
  64. pGenericStatus->CameraTime.wMilliseconds = 1;
  65. return(hr);
  66. }
  67. /**************************************************************************\
  68. * CamBuildImageTree
  69. *
  70. * Build the tree of camera images by enumerating a disk directory
  71. *
  72. * Arguments:
  73. *
  74. * pCamStatus - device status
  75. * ppRootItem - return new root of item tree
  76. *
  77. * Return Value:
  78. *
  79. * status
  80. *
  81. * History:
  82. *
  83. * 6/26/1998 Mark Enstrom [marke]
  84. *
  85. \**************************************************************************/
  86. HRESULT
  87. TestUsdDevice::CamBuildImageTree(
  88. CAMERA_STATUS *pCamStatus,
  89. IWiaDrvItem **ppRootItem)
  90. {
  91. HRESULT hr = S_OK;
  92. WIAS_TRACE((g_hInst,"CamBuildImageTree"));
  93. //
  94. // Create the new root
  95. //
  96. BSTR bstrRoot = SysAllocString(L"Root");
  97. if (bstrRoot == NULL) {
  98. return E_OUTOFMEMORY;
  99. }
  100. //
  101. // Call Wia service library to create new root item
  102. //
  103. hr = wiasCreateDrvItem(
  104. WiaItemTypeFolder | WiaItemTypeRoot | WiaItemTypeDevice,
  105. bstrRoot,
  106. m_bstrRootFullItemName,
  107. (IWiaMiniDrv *)this,
  108. sizeof(MEMCAM_IMAGE_CONTEXT),
  109. NULL,
  110. ppRootItem);
  111. SysFreeString(bstrRoot);
  112. if (FAILED(hr)) {
  113. WIAS_ERROR((g_hInst,"ddevBuildDeviceItemTree, CreateDeviceItem failed"));
  114. return hr;
  115. }
  116. //
  117. // Enumerate throught the root directory
  118. //
  119. hr = EnumDiskImages(*ppRootItem, gpszPath);
  120. return (hr);
  121. }
  122. /**************************************************************************\
  123. FindExtension
  124. **************************************************************************/
  125. LPTSTR
  126. FindExtension (LPTSTR pszPath)
  127. {
  128. LPTSTR pszDot = NULL;
  129. if (pszPath)
  130. {
  131. for (; *pszPath; pszPath = CharNext(pszPath))
  132. {
  133. switch (*pszPath)
  134. {
  135. case TEXT('.'):
  136. pszDot = pszPath; // remember the last dot
  137. break;
  138. case '\\':
  139. case TEXT(' '): // extensions can't have spaces
  140. pszDot = NULL; // forget last dot, it was in a directory
  141. break;
  142. }
  143. }
  144. }
  145. // if we found the extension, return ptr to the dot, else
  146. // ptr to end of the string (NULL extension)
  147. return pszDot ? pszDot : pszPath;
  148. }
  149. /**************************************************************************\
  150. * EnumDiskImages
  151. *
  152. * Walk through disk looking for BMP and WAV files to use as camera images
  153. *
  154. * Arguments:
  155. *
  156. * pRootItem
  157. * pwszDirName
  158. *
  159. * Return Value:
  160. *
  161. * status
  162. *
  163. * History:
  164. *
  165. * 2/17/1998 Mark Enstrom [marke]
  166. *
  167. \**************************************************************************/
  168. HRESULT
  169. TestUsdDevice::EnumDiskImages(
  170. IWiaDrvItem *pRootItem,
  171. LPTSTR pszDirName)
  172. {
  173. HRESULT hr = E_FAIL;
  174. WIN32_FIND_DATA FindData;
  175. PTCHAR pTempName = (PTCHAR)ALLOC(MAX_PATH);
  176. WIAS_TRACE((g_hInst,"EnumDiskImages"));
  177. if (pTempName != NULL) {
  178. HANDLE hFile;
  179. _tcscpy(pTempName, pszDirName);
  180. _tcscat(pTempName, TEXT("\\*.*"));
  181. //
  182. // look for image,audio files and directories at this level
  183. //
  184. hFile = FindFirstFile(pTempName, &FindData);
  185. if (hFile != INVALID_HANDLE_VALUE) {
  186. BOOL bStatus;
  187. do
  188. {
  189. _tcscpy(pTempName, pszDirName);
  190. _tcscat(pTempName, TEXT("\\"));
  191. _tcscat(pTempName, FindData.cFileName);
  192. if ( (FindData.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY)
  193. && lstrcmp(FindData.cFileName, TEXT("."))
  194. && lstrcmp(FindData.cFileName, TEXT("..")))
  195. {
  196. //
  197. // create a new folder for the sub-directory
  198. //
  199. IWiaDrvItem *pNewFolder;
  200. hr = CreateItemFromFileName(
  201. WiaItemTypeFolder,
  202. pTempName,
  203. FindData.cFileName,
  204. &pNewFolder);
  205. if (SUCCEEDED(hr)) {
  206. hr = pNewFolder->AddItemToFolder(pRootItem);
  207. if (hr == S_OK) {
  208. //
  209. // enumerate sub-folder
  210. //
  211. EnumDiskImages(pNewFolder, pTempName);
  212. }
  213. pNewFolder->Release();
  214. }
  215. }
  216. else
  217. {
  218. LONG lType = WiaItemTypeFile;
  219. //
  220. // add an image to this folder
  221. //
  222. // generate file name
  223. //
  224. //
  225. // Create a new DrvItem for this image and add it to the
  226. // DrvItem tree.
  227. //
  228. LPTSTR pExt = FindExtension (FindData.cFileName);
  229. if (!lstrcmpi(pExt, TEXT(".bmp")))
  230. {
  231. lType |= WiaItemTypeImage;
  232. }
  233. else if (!lstrcmpi(pExt,TEXT(".wav")))
  234. {
  235. lType |= WiaItemTypeAudio;
  236. }
  237. else
  238. {
  239. lType = 0;
  240. }
  241. if (lType)
  242. {
  243. IWiaDrvItem *pNewFolder;
  244. hr = CreateItemFromFileName(
  245. lType,
  246. pTempName,
  247. FindData.cFileName,
  248. &pNewFolder);
  249. if (SUCCEEDED(hr)) {
  250. pNewFolder->AddItemToFolder(pRootItem);
  251. pNewFolder->Release();
  252. }
  253. }
  254. }
  255. bStatus = FindNextFile(hFile,&FindData);
  256. } while (bStatus);
  257. FindClose(hFile);
  258. }
  259. FREE(pTempName);
  260. }
  261. return (S_OK);
  262. }
  263. /**************************************************************************\
  264. * CreateItemFromFileName
  265. *
  266. * helper funtion to create dev items and names
  267. *
  268. * Arguments:
  269. *
  270. * FolderType - type of item to create
  271. * pszPath - complete path name
  272. * pszName - file name
  273. * ppNewFolder - return new item
  274. *
  275. * Return Value:
  276. *
  277. * status
  278. *
  279. * History:
  280. *
  281. * 1/17/1999 Mark Enstrom [marke]
  282. *
  283. \**************************************************************************/
  284. HRESULT
  285. TestUsdDevice::CreateItemFromFileName(
  286. LONG FolderType,
  287. PTCHAR pszPath,
  288. PTCHAR pszName,
  289. IWiaDrvItem **ppNewFolder
  290. )
  291. {
  292. HRESULT hr = S_OK;
  293. IWiaDrvItem *pNewFolder;
  294. WCHAR szFullItemName[MAX_PATH];
  295. WCHAR szTemp[MAX_PATH];
  296. BSTR bstrItemName;
  297. BSTR bstrFullItemName;
  298. WIAS_TRACE((g_hInst,"CreateItemFromFileName"));
  299. *ppNewFolder = NULL;
  300. //
  301. // convert path to wide char
  302. //
  303. #ifndef UNICODE
  304. MultiByteToWideChar(
  305. CP_ACP,
  306. 0,
  307. pszPath + strlen(gpszPath),
  308. -1,
  309. szTemp, MAX_PATH);
  310. #else
  311. wcscpy(szTemp, pszPath + wcslen(gpszPath));
  312. #endif
  313. if (FolderType & ~WiaItemTypeFolder) {
  314. szTemp[_tcslen(pszPath) - _tcslen(gpszPath) - 4] = 0;
  315. }
  316. wcscpy(szFullItemName, m_bstrRootFullItemName);
  317. wcscat(szFullItemName, szTemp);
  318. //
  319. // convert item name to wide char
  320. //
  321. #ifndef UNICODE
  322. MultiByteToWideChar(
  323. CP_ACP, 0, pszName, -1, szTemp, MAX_PATH);
  324. #else
  325. wcscpy(szTemp, pszName);
  326. #endif
  327. if (FolderType & ~WiaItemTypeFolder) {
  328. szTemp[_tcslen(pszName)-4] = 0;
  329. }
  330. bstrItemName = SysAllocString(szTemp);
  331. if (bstrItemName) {
  332. bstrFullItemName = SysAllocString(szFullItemName);
  333. if (bstrFullItemName) {
  334. //
  335. // call Wia to create new DrvItem
  336. //
  337. PMEMCAM_IMAGE_CONTEXT pContext;
  338. hr = wiasCreateDrvItem(
  339. FolderType,
  340. bstrItemName,
  341. bstrFullItemName,
  342. (IWiaMiniDrv *)this,
  343. sizeof(MEMCAM_IMAGE_CONTEXT),
  344. (BYTE **)&pContext,
  345. &pNewFolder);
  346. if (hr == S_OK) {
  347. //
  348. // init device specific context (image path)
  349. //
  350. pContext->pszCameraImagePath = _tcsdup(pszPath);
  351. } else {
  352. WIAS_ERROR((g_hInst,"ddevBuildDeviceItemTree, wiasCreateDrvItem failed"));
  353. }
  354. SysFreeString(bstrFullItemName);
  355. }
  356. else {
  357. WIAS_ERROR((g_hInst,"ddevBuildDeviceItemTree, unable to allocate full item name"));
  358. hr = E_OUTOFMEMORY;
  359. }
  360. SysFreeString(bstrItemName);
  361. }
  362. else {
  363. WIAS_ERROR((g_hInst,"ddevBuildDeviceItemTree, unable to allocate item name"));
  364. hr = E_OUTOFMEMORY;
  365. }
  366. //
  367. // assign output value or cleanup
  368. //
  369. if (hr == S_OK) {
  370. *ppNewFolder = pNewFolder;
  371. } else {
  372. //
  373. // delete item
  374. //
  375. }
  376. return hr;
  377. }
  378. /**************************************************************************\
  379. * GetItemSize
  380. *
  381. * call wias to calc new item size
  382. *
  383. * Arguments:
  384. *
  385. * pWiasContext - item
  386. * pItemSize - return size of item
  387. *
  388. * Return Value:
  389. *
  390. * Status
  391. *
  392. * History:
  393. *
  394. * 4/21/1999 Original Version
  395. *
  396. \**************************************************************************/
  397. HRESULT
  398. SetItemSize(BYTE* pWiasContext)
  399. {
  400. HRESULT hr;
  401. MINIDRV_TRANSFER_CONTEXT drvTranCtx;
  402. memset(&drvTranCtx, 0, sizeof(MINIDRV_TRANSFER_CONTEXT));
  403. GUID guidFormatID;
  404. hr = wiasReadPropGuid(pWiasContext, WIA_IPA_FORMAT, (GUID*)&drvTranCtx.guidFormatID, NULL, FALSE);
  405. if (FAILED(hr)) {
  406. return hr;
  407. }
  408. hr = wiasReadPropLong(pWiasContext, WIA_IPA_TYMED, (LONG*)&drvTranCtx.tymed, NULL, false);
  409. if (FAILED(hr)) {
  410. return hr;
  411. }
  412. //
  413. // wias works for DIB,TIFF formats
  414. //
  415. // driver doesn't support JPEG
  416. //
  417. hr = wiasGetImageInformation(pWiasContext,
  418. WIAS_INIT_CONTEXT,
  419. &drvTranCtx);
  420. if (hr == S_OK) {
  421. hr = wiasWritePropLong(pWiasContext, WIA_IPA_ITEM_SIZE, drvTranCtx.lItemSize);
  422. hr = wiasWritePropLong(pWiasContext, WIA_IPA_BYTES_PER_LINE, drvTranCtx.cbWidthInBytes);
  423. }
  424. return hr;
  425. }
  426. /**************************************************************************\
  427. * InitImageInformation
  428. *
  429. * Init image properties
  430. *
  431. * Arguments:
  432. *
  433. * pFile MINI_DEV_OBJECT to support item
  434. * pszCameraImagePath path and name of bmp file
  435. *
  436. * Return Value:
  437. *
  438. * Status
  439. *
  440. * History:
  441. *
  442. * 2/12/1998 Mark Enstrom [marke]
  443. *
  444. \**************************************************************************/
  445. HRESULT
  446. TestUsdDevice::InitImageInformation(
  447. BYTE *pWiasContext,
  448. PMEMCAM_IMAGE_CONTEXT pContext,
  449. LONG *plDevErrVal)
  450. {
  451. HRESULT hr = S_OK;
  452. CAMERA_PICTURE_INFO camInfo;
  453. PBITMAPINFO pBitmapinfo = NULL;
  454. LONG szBitmapInfo = 0;
  455. int i;
  456. PROPVARIANT propVar;
  457. WIAS_TRACE((g_hInst,"InitImageInformation"));
  458. //
  459. // GET image info
  460. //
  461. hr = CamGetPictureInfo(
  462. pContext, &camInfo, (PBYTE*)&pBitmapinfo, &szBitmapInfo);
  463. if (hr != S_OK) {
  464. if (pBitmapinfo != NULL) {
  465. FREE(pBitmapinfo);
  466. }
  467. return (hr);
  468. }
  469. //
  470. // Use WIA services to write image properties.
  471. //
  472. wiasWritePropLong(pWiasContext, WIA_IPC_THUMB_WIDTH, camInfo.ThumbWidth);
  473. wiasWritePropLong(pWiasContext, WIA_IPC_THUMB_HEIGHT, camInfo.ThumbHeight);
  474. wiasWritePropLong(
  475. pWiasContext, WIA_IPA_PIXELS_PER_LINE, pBitmapinfo->bmiHeader.biWidth);
  476. wiasWritePropLong(
  477. pWiasContext, WIA_IPA_NUMBER_OF_LINES, pBitmapinfo->bmiHeader.biHeight);
  478. wiasWritePropGuid(pWiasContext, WIA_IPA_PREFERRED_FORMAT, WiaImgFmt_BMP);
  479. wiasWritePropLong(
  480. pWiasContext, WIA_IPA_DEPTH, pBitmapinfo->bmiHeader.biBitCount);
  481. wiasWritePropBin(
  482. pWiasContext, WIA_IPA_ITEM_TIME,
  483. sizeof(SYSTEMTIME), (PBYTE)&camInfo.TimeStamp);
  484. wiasWritePropLong(pWiasContext, WIA_IPA_DATATYPE, WIA_DATA_COLOR);
  485. //
  486. // Free the BITMAPINFO
  487. //
  488. FREE(pBitmapinfo);
  489. //
  490. // calc item size
  491. //
  492. hr = SetItemSize(pWiasContext);
  493. //
  494. // load thumbnail
  495. //
  496. PBYTE pThumb;
  497. LONG lSize;
  498. hr = CamLoadThumbnail(pContext, &pThumb, &lSize);
  499. if (hr == S_OK) {
  500. //
  501. // write thumb property
  502. //
  503. PROPSPEC propSpec;
  504. PROPVARIANT propVar;
  505. propVar.vt = VT_VECTOR | VT_UI1;
  506. propVar.caub.cElems = lSize;
  507. propVar.caub.pElems = pThumb;
  508. propSpec.ulKind = PRSPEC_PROPID;
  509. propSpec.propid = WIA_IPC_THUMBNAIL;
  510. hr = wiasWriteMultiple(pWiasContext, 1, &propSpec, &propVar);
  511. FREE(pThumb);
  512. }
  513. hr = SetFormatAttribs();
  514. if (FAILED(hr)) {
  515. return (hr);
  516. }
  517. //
  518. // Use WIA services to set the extended property access and
  519. // valid value information from gItemPropInfos.
  520. //
  521. hr = wiasSetItemPropAttribs(pWiasContext,
  522. NUM_CAM_ITEM_PROPS,
  523. gPropSpecDefaults,
  524. gItemPropInfos);
  525. return (hr);
  526. }
  527. HRESULT
  528. TestUsdDevice::InitAudioInformation(
  529. BYTE *pWiasContext,
  530. PMEMCAM_IMAGE_CONTEXT pContext,
  531. LONG *plDevErrVal)
  532. {
  533. HRESULT hr = E_FAIL;
  534. WIN32_FILE_ATTRIBUTE_DATA wfd;
  535. if (GetFileAttributesEx (pContext->pszCameraImagePath, GetFileExInfoStandard, &wfd))
  536. {
  537. SYSTEMTIME st;
  538. FileTimeToSystemTime (&wfd.ftLastWriteTime, &st);
  539. wiasWritePropLong (pWiasContext, WIA_IPA_ITEM_SIZE, wfd.nFileSizeLow);
  540. wiasWritePropBin (pWiasContext, WIA_IPA_ITEM_TIME, sizeof(SYSTEMTIME),
  541. (PBYTE)&st);
  542. hr = S_OK;
  543. }
  544. return hr;
  545. }
  546. /**************************************************************************\
  547. * SetFormatAttribs
  548. *
  549. *
  550. * Arguments:
  551. *
  552. *
  553. *
  554. * Return Value:
  555. *
  556. * Status
  557. *
  558. * History:
  559. *
  560. * 1/5/2000 Original Version
  561. *
  562. \**************************************************************************/
  563. HRESULT
  564. SetFormatAttribs()
  565. {
  566. gItemPropInfos[FORMAT_INDEX].lAccessFlags = WIA_PROP_RW | WIA_PROP_LIST;
  567. gItemPropInfos[FORMAT_INDEX].vt = VT_CLSID;
  568. gItemPropInfos[FORMAT_INDEX].ValidVal.ListGuid.cNumList = NUM_FORMAT;
  569. gItemPropInfos[FORMAT_INDEX].ValidVal.ListGuid.pList = gGuidFormats;
  570. //
  571. // Set the norm
  572. //
  573. gItemPropInfos[FORMAT_INDEX].ValidVal.ListGuid.Nom = WiaImgFmt_BMP;
  574. //
  575. // Set up the format clsid list
  576. //
  577. gGuidFormats[0] = WiaImgFmt_BMP;
  578. gGuidFormats[1] = WiaImgFmt_MEMORYBMP;
  579. return (S_OK);
  580. }