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.

509 lines
11 KiB

  1. /******************************Module*Header*******************************\
  2. * Module Name: usermode.cxx
  3. *
  4. * Copyright (c) 1998-1999 Microsoft Corporation
  5. *
  6. * This file contains all the system abstraction routines required to allow
  7. * GDI to run as a stand-alone graphics library in user-mode.
  8. *
  9. * Created: 29-Apr-1998
  10. * Author: J. Andrew Goossen [andrewgo]
  11. *
  12. \**************************************************************************/
  13. #include "precomp.hxx"
  14. extern "C" {
  15. BOOL gbRemoteSession=0;
  16. }
  17. // Temporary HDEV for first bringing up user-mode GDI+, which represents
  18. // the screen 'device'. This sure as heck had better be temporary!
  19. HDEV ghdevDisplay;
  20. // Copy of a drvsup.cxx structure that should really be moved to engine.h:
  21. typedef struct _DRV_NAMES {
  22. ULONG cNames;
  23. struct {
  24. HANDLE hDriver;
  25. LPWSTR lpDisplayName;
  26. } D[1];
  27. } DRV_NAMES, *PDRV_NAMES;
  28. HDEV hCreateHDEV(PGRAPHICS_DEVICE,PDRV_NAMES,PDEVMODEW,PVOID,BOOL,FLONG,HDEV*);
  29. // Some random goop needed for linking user-mode GDI+:
  30. extern "C" {
  31. PEPROCESS gpepCSRSS;
  32. ULONG gSessionId;
  33. };
  34. /*****************************Private*Routine******************************\
  35. * BOOL GpInitialize
  36. *
  37. * History:
  38. * 3-May-1998 -by- J. Andrew Goossen [andrewgo]
  39. * Wrote it.
  40. \**************************************************************************/
  41. extern "C"
  42. BOOL
  43. GpInitialize(
  44. HWND hwnd)
  45. {
  46. DRV_NAMES drvName;
  47. HDEV hdevDisabled;
  48. BOOL bRet = FALSE;
  49. drvName.cNames = 1;
  50. drvName.D[0].hDriver = NULL;
  51. drvName.D[0].lpDisplayName = (LPWSTR)GpsEnableDriver;
  52. if (ghdevDisplay == NULL)
  53. {
  54. KdPrint(("hCreateDEV.................\n"));
  55. ghdevDisplay = hCreateHDEV(NULL,
  56. &drvName,
  57. ((PDEVMODEW)(hwnd)),
  58. NULL,
  59. TRUE, // ignored in this case
  60. GCH_GDIPLUS_DISPLAY,
  61. &hdevDisabled);
  62. bRet = (ghdevDisplay != NULL);
  63. }
  64. else
  65. {
  66. WARNING("Can't yet create multiple windows");
  67. }
  68. return(bRet);
  69. }
  70. /*****************************Private*Routine******************************\
  71. * HDC UserGetDesktopDC
  72. *
  73. * History:
  74. * 3-May-1998 -by- J. Andrew Goossen [andrewgo]
  75. * Wrote it.
  76. \**************************************************************************/
  77. HDC
  78. UserGetDesktopDC(
  79. ULONG iType,
  80. BOOL bAltType,
  81. BOOL bValidate)
  82. {
  83. HDC hdc = NULL;
  84. if ((iType == DCTYPE_DIRECT) && (bValidate))
  85. {
  86. if (ghdevDisplay != NULL)
  87. {
  88. hdc = GreCreateDisplayDC(ghdevDisplay, DCTYPE_DIRECT, bAltType);
  89. }
  90. else
  91. {
  92. WARNING("ghdevDisplay not initialized");
  93. }
  94. }
  95. else
  96. {
  97. WARNING("UserGetDesktopDC functionality not yet handled");
  98. }
  99. return(hdc);
  100. }
  101. /*****************************Private*Routine******************************\
  102. * HDEV UserGetHDEV
  103. *
  104. \**************************************************************************/
  105. HDEV
  106. UserGetHDEV()
  107. {
  108. return(ghdevDisplay);
  109. }
  110. /*****************************Private*Routine******************************\
  111. * BOOL UserVisrgnFromHwnd
  112. *
  113. \**************************************************************************/
  114. BOOL
  115. UserVisrgnFromHwnd(
  116. HRGN* phrgn,
  117. HWND hwnd)
  118. {
  119. *phrgn = NULL;
  120. return(FALSE);
  121. }
  122. /*****************************Private*Routine******************************\
  123. * DWORD GetAppCompatFlags
  124. *
  125. \**************************************************************************/
  126. extern "C"
  127. DWORD
  128. GetAppCompatFlags(
  129. PVOID pv)
  130. {
  131. return(0);
  132. }
  133. /*****************************Private*Routine******************************\
  134. * VOID* pAllocMem
  135. *
  136. * Hack for screen.c header mess.
  137. *
  138. \**************************************************************************/
  139. extern "C"
  140. VOID*
  141. pAllocMem(
  142. ULONG size,
  143. ULONG tag)
  144. {
  145. return(PALLOCMEM(size, tag));
  146. }
  147. /*****************************Private*Routine******************************\
  148. * VOID vFreeMem
  149. *
  150. * Hack for screen.c header mess.
  151. *
  152. \**************************************************************************/
  153. extern "C"
  154. VOID
  155. vFreeMem(
  156. VOID* p)
  157. {
  158. VFREEMEM(p);
  159. }
  160. NTSTATUS
  161. MapViewInProcessSpace(
  162. PVOID pv,
  163. PVOID *ppv,
  164. ULONG *psize
  165. )
  166. /*++
  167. Routine Description:
  168. Map view of a file into current process' address space
  169. Arguments:
  170. pv - Points to a MEMORY_MAPPED_FILE structure
  171. ppv - Return the base address of the mapped view
  172. psize - Return the size of the mapped view
  173. Return Value:
  174. STATUS_SUCCESS if successful, error code otherwise
  175. --*/
  176. {
  177. MEMORY_MAPPED_FILE *mappedFile = (MEMORY_MAPPED_FILE *) pv;
  178. if (!mappedFile ||
  179. !mappedFile->fileMap ||
  180. !(*ppv = MapViewOfFile(mappedFile->fileMap,
  181. mappedFile->readOnly ? FILE_MAP_READ : FILE_MAP_WRITE,
  182. 0,
  183. 0,
  184. mappedFile->fileSize)))
  185. {
  186. WARNING("MapViewInProcessSpace failed\n");
  187. *ppv = NULL;
  188. *psize = 0;
  189. return STATUS_UNSUCCESSFUL;
  190. }
  191. *psize = mappedFile->fileSize;
  192. return STATUS_SUCCESS;
  193. }
  194. NTSTATUS
  195. UnmapViewInProcessSpace(
  196. PVOID pv
  197. )
  198. /*++
  199. Routine Description:
  200. Unmap view of a file from the current process' address space
  201. Arguments:
  202. pv - Base address of the view to be unmapped
  203. Return Value:
  204. STATUS_SUCCESS if successful, error code otherwise
  205. --*/
  206. {
  207. return UnmapViewOfFile(pv) ? STATUS_SUCCESS : STATUS_UNSUCCESSFUL;
  208. }
  209. BOOL
  210. CreateMemoryMappedSection(
  211. PWSTR filename,
  212. FILEVIEW *fileview,
  213. INT size
  214. )
  215. /*++
  216. Routine Description:
  217. Create a memory-mapped file structure
  218. Arguments:
  219. filename - Specifies the name of the file to be mapped
  220. fileview - Return information about the memory-mapped file
  221. size - Size to be mapped
  222. 0 : map entire content of an existing file for reading
  223. else : create a file of the specified size for reading & writing
  224. Return Value:
  225. TRUE if successful, FALSE otherwise
  226. --*/
  227. #define SYSTEM32_STR L"SYSTEM32"
  228. #define SYSTEM32_LEN 8
  229. #define SYSTEMROOT_STR L"SYSTEMROOT"
  230. #define SYSTEMROOT_LEN 10
  231. {
  232. FILEVIEW fv;
  233. HANDLE fileHandle;
  234. MEMORY_MAPPED_FILE *mappedFile;
  235. BY_HANDLE_FILE_INFORMATION fileinfo;
  236. WCHAR fullpath[MAX_PATH];
  237. PWSTR p;
  238. ZeroMemory(&fv, sizeof(FILEVIEW));
  239. ZeroMemory(fileview, sizeof(FILEVIEW));
  240. //
  241. // Expand the input filename to fully qualified path
  242. //
  243. p = filename;
  244. while (*p && _wcsnicmp(p, SYSTEMROOT_STR, SYSTEMROOT_LEN))
  245. p++;
  246. if (*p)
  247. {
  248. if (GetWindowsDirectoryW(fullpath, MAX_PATH))
  249. {
  250. wcscat(fullpath, p + SYSTEMROOT_LEN);
  251. filename = fullpath;
  252. }
  253. else
  254. {
  255. WARNING("GetWindowsDirectory failed\n");
  256. return FALSE;
  257. }
  258. }
  259. else
  260. {
  261. p = filename;
  262. while (*p && _wcsnicmp(p, SYSTEM32_STR, SYSTEM32_LEN))
  263. p++;
  264. if (*p)
  265. {
  266. if (GetSystemDirectoryW(fullpath, MAX_PATH))
  267. {
  268. wcscat(fullpath, p + SYSTEM32_LEN);
  269. filename = fullpath;
  270. }
  271. else
  272. {
  273. WARNING("GetSystemDirectory failed\n");
  274. return FALSE;
  275. }
  276. }
  277. }
  278. //
  279. // Allocate memory for a MEMORY_MAPPED_FILE structure
  280. // (assume content is zero-initialized)
  281. //
  282. mappedFile = (MEMORY_MAPPED_FILE *) PALLOCMEM(sizeof(MEMORY_MAPPED_FILE), 'fmmG');
  283. if (mappedFile == NULL)
  284. return NULL;
  285. //
  286. // Open the file for reading or writing
  287. //
  288. if (size != 0)
  289. {
  290. mappedFile->readOnly = FALSE;
  291. fileHandle = CreateFileW(filename,
  292. GENERIC_READ|GENERIC_WRITE,
  293. FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
  294. NULL,
  295. OPEN_ALWAYS,
  296. FILE_ATTRIBUTE_NORMAL,
  297. NULL);
  298. }
  299. else
  300. {
  301. mappedFile->readOnly = TRUE;
  302. fileHandle = CreateFileW(filename,
  303. GENERIC_READ,
  304. FILE_SHARE_READ,
  305. NULL,
  306. OPEN_EXISTING,
  307. 0,
  308. NULL);
  309. }
  310. if (fileHandle == INVALID_HANDLE_VALUE)
  311. goto errorExit;
  312. //
  313. // Get timestamp information about the file
  314. //
  315. if (!GetFileInformationByHandle(fileHandle, &fileinfo))
  316. goto errorExit;
  317. fv.LastWriteTime.LowPart = fileinfo.ftLastWriteTime.dwLowDateTime;
  318. fv.LastWriteTime.HighPart = fileinfo.ftLastWriteTime.dwHighDateTime;
  319. //
  320. // If file was opened for writing, set it to requested size
  321. //
  322. if (size != 0)
  323. {
  324. if (size > 0)
  325. fileinfo.nFileSizeLow = (DWORD) size;
  326. fileinfo.nFileSizeHigh = 0;
  327. if (!SetFilePointer(fileHandle, fileinfo.nFileSizeLow, 0, FILE_BEGIN))
  328. goto errorExit;
  329. }
  330. if (fileinfo.nFileSizeHigh != 0)
  331. goto errorExit;
  332. mappedFile->fileSize = fv.cjView = fileinfo.nFileSizeLow;
  333. //
  334. // Create a file mapping handle
  335. //
  336. mappedFile->fileMap = CreateFileMapping(
  337. fileHandle,
  338. NULL,
  339. mappedFile->readOnly ? PAGE_READONLY : PAGE_READWRITE,
  340. 0,
  341. fileinfo.nFileSizeLow,
  342. NULL);
  343. if (mappedFile->fileMap != NULL)
  344. {
  345. CloseHandle(fileHandle);
  346. fv.pSection = mappedFile;
  347. CopyMemory(fileview, &fv, sizeof(FILEVIEW));
  348. return TRUE;
  349. }
  350. errorExit:
  351. WARNING("CreateMemoryMappedSection failed\n");
  352. if (fileHandle != INVALID_HANDLE_VALUE)
  353. CloseHandle(fileHandle);
  354. DeleteMemoryMappedSection(mappedFile);
  355. return FALSE;
  356. }
  357. VOID
  358. DeleteMemoryMappedSection(
  359. PVOID pv
  360. )
  361. /*++
  362. Routine Description:
  363. Dispose of a memory-mapped file structure
  364. Arguments:
  365. pv - Points to a MEMORY_MAPPED_FILE structure
  366. Return Value:
  367. NONE
  368. --*/
  369. {
  370. MEMORY_MAPPED_FILE *mappedFile = (MEMORY_MAPPED_FILE *) pv;
  371. if (mappedFile != NULL)
  372. {
  373. if (mappedFile->fileMap != NULL)
  374. CloseHandle(mappedFile->fileMap);
  375. VFREEMEM(mappedFile);
  376. }
  377. }
  378. NTSYSAPI
  379. NTSTATUS
  380. NTAPI
  381. ZwOpenKey(
  382. PHANDLE KeyHandle,
  383. ACCESS_MASK DesiredAccess,
  384. POBJECT_ATTRIBUTES ObjectAttributes
  385. )
  386. {
  387. if (DesiredAccess == GENERIC_READ || DesiredAccess == 0)
  388. DesiredAccess = KEY_READ;
  389. return NtOpenKey(KeyHandle, DesiredAccess, ObjectAttributes);
  390. }