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.

818 lines
20 KiB

  1. /*++
  2. Copyright (c) 1985 - 1999, Microsoft Corporation
  3. Module Name:
  4. private.c
  5. Abstract:
  6. This file implements private APIs for Hardware Desktop Support.
  7. Author:
  8. Therese Stowell (thereses) 12-13-1991
  9. Revision History:
  10. Notes:
  11. --*/
  12. #include "precomp.h"
  13. #pragma hdrstop
  14. #pragma hdrstop
  15. #if !defined(BUILD_WOW64)
  16. typedef HANDLE (*PCONVPALFUNC)(HANDLE);
  17. PCONVPALFUNC pfnGdiConvertPalette;
  18. #endif //!defined(BUILD_WOW64)
  19. #if !defined(BUILD_WOW6432)
  20. BOOL
  21. WINAPI
  22. SetConsoleCursor(
  23. IN HANDLE hConsoleOutput,
  24. IN HCURSOR hCursor
  25. )
  26. /*++
  27. Description:
  28. Sets the mouse pointer for the specified screen buffer.
  29. Parameters:
  30. hConsoleOutput - Supplies a console output handle.
  31. hCursor - win32 cursor handle, should be NULL to set the default
  32. cursor.
  33. Return value:
  34. TRUE - The operation was successful.
  35. FALSE/NULL - The operation failed. Extended error status is available
  36. using GetLastError.
  37. --*/
  38. {
  39. CONSOLE_API_MSG m;
  40. PCONSOLE_SETCURSOR_MSG a = &m.u.SetConsoleCursor;
  41. a->ConsoleHandle = GET_CONSOLE_HANDLE;
  42. a->OutputHandle = hConsoleOutput;
  43. a->CursorHandle = hCursor;
  44. CsrClientCallServer( (PCSR_API_MSG)&m,
  45. NULL,
  46. CSR_MAKE_API_NUMBER( CONSRV_SERVERDLL_INDEX,
  47. ConsolepSetCursor
  48. ),
  49. sizeof( *a )
  50. );
  51. if (!NT_SUCCESS( m.ReturnValue)) {
  52. SET_LAST_NT_ERROR(m.ReturnValue);
  53. return FALSE;
  54. }
  55. else {
  56. return TRUE;
  57. }
  58. }
  59. int
  60. WINAPI
  61. ShowConsoleCursor(
  62. IN HANDLE hConsoleOutput,
  63. IN BOOL bShow
  64. )
  65. /*++
  66. Description:
  67. Sets the mouse pointer visibility counter. If the counter is less than
  68. zero, the mouse pointer is not shown.
  69. Parameters:
  70. hOutput - Supplies a console output handle.
  71. bShow - if TRUE, the display count is to be increased. if FALSE,
  72. decreased.
  73. Return value:
  74. The return value specifies the new display count.
  75. --*/
  76. {
  77. CONSOLE_API_MSG m;
  78. PCONSOLE_SHOWCURSOR_MSG a = &m.u.ShowConsoleCursor;
  79. a->ConsoleHandle = GET_CONSOLE_HANDLE;
  80. a->OutputHandle = hConsoleOutput;
  81. a->bShow = bShow;
  82. CsrClientCallServer( (PCSR_API_MSG)&m,
  83. NULL,
  84. CSR_MAKE_API_NUMBER( CONSRV_SERVERDLL_INDEX,
  85. ConsolepShowCursor
  86. ),
  87. sizeof( *a )
  88. );
  89. return a->DisplayCount;
  90. }
  91. HMENU
  92. APIENTRY
  93. ConsoleMenuControl(
  94. IN HANDLE hConsoleOutput,
  95. IN UINT dwCommandIdLow,
  96. IN UINT dwCommandIdHigh
  97. )
  98. /*++
  99. Description:
  100. Sets the command id range for the current screen buffer and returns the
  101. menu handle.
  102. Parameters:
  103. hConsoleOutput - Supplies a console output handle.
  104. dwCommandIdLow - Specifies the lowest command id to store in the input buffer.
  105. dwCommandIdHigh - Specifies the highest command id to store in the input
  106. buffer.
  107. Return value:
  108. TRUE - The operation was successful.
  109. FALSE/NULL - The operation failed. Extended error status is available
  110. using GetLastError.
  111. --*/
  112. {
  113. CONSOLE_API_MSG m;
  114. PCONSOLE_MENUCONTROL_MSG a = &m.u.ConsoleMenuControl;
  115. a->ConsoleHandle = GET_CONSOLE_HANDLE;
  116. a->OutputHandle = hConsoleOutput;
  117. a->CommandIdLow =dwCommandIdLow;
  118. a->CommandIdHigh = dwCommandIdHigh;
  119. CsrClientCallServer( (PCSR_API_MSG)&m,
  120. NULL,
  121. CSR_MAKE_API_NUMBER( CONSRV_SERVERDLL_INDEX,
  122. ConsolepMenuControl
  123. ),
  124. sizeof( *a )
  125. );
  126. return a->hMenu;
  127. }
  128. BOOL
  129. APIENTRY
  130. SetConsolePaletteInternal(
  131. IN HANDLE hConsoleOutput,
  132. IN HPALETTE hPalette,
  133. IN UINT dwUsage
  134. )
  135. {
  136. CONSOLE_API_MSG m;
  137. PCONSOLE_SETPALETTE_MSG a = &m.u.SetConsolePalette;
  138. NTSTATUS Status;
  139. a->ConsoleHandle = GET_CONSOLE_HANDLE;
  140. a->OutputHandle = hConsoleOutput;
  141. a->hPalette = hPalette;
  142. a->dwUsage = dwUsage;
  143. CsrClientCallServer( (PCSR_API_MSG)&m,
  144. NULL,
  145. CSR_MAKE_API_NUMBER( CONSRV_SERVERDLL_INDEX,
  146. ConsolepSetPalette
  147. ),
  148. sizeof( *a )
  149. );
  150. if (!NT_SUCCESS( m.ReturnValue)) {
  151. SET_LAST_NT_ERROR(m.ReturnValue);
  152. return FALSE;
  153. }
  154. else {
  155. return TRUE;
  156. }
  157. }
  158. BOOL
  159. APIENTRY
  160. SetConsoleDisplayMode(
  161. IN HANDLE hConsoleOutput,
  162. IN DWORD dwFlags,
  163. OUT PCOORD lpNewScreenBufferDimensions
  164. )
  165. /*++
  166. Description:
  167. This routine sets the console display mode for an output buffer.
  168. This API is only supported on x86 machines. Frame buffer consoles
  169. are always windowed.
  170. Parameters:
  171. hConsoleOutput - Supplies a console output handle.
  172. dwFlags - Specifies the display mode. Options are:
  173. CONSOLE_FULLSCREEN_MODE - data is displayed fullscreen
  174. CONSOLE_WINDOWED_MODE - data is displayed in a window
  175. lpNewScreenBufferDimensions - On output, contains the new dimensions of
  176. the screen buffer. The dimensions are in rows and columns for
  177. textmode screen buffers.
  178. Return value:
  179. TRUE - The operation was successful.
  180. FALSE/NULL - The operation failed. Extended error status is available
  181. using GetLastError.
  182. --*/
  183. {
  184. CONSOLE_API_MSG m;
  185. PCONSOLE_SETDISPLAYMODE_MSG a = &m.u.SetConsoleDisplayMode;
  186. NTSTATUS Status;
  187. #if !defined(_X86_)
  188. return FALSE;
  189. #else
  190. Status = NtCreateEvent(&(a->hEvent),
  191. EVENT_ALL_ACCESS,
  192. NULL,
  193. SynchronizationEvent,
  194. (BOOLEAN)FALSE
  195. );
  196. if (!NT_SUCCESS(Status)) {
  197. SET_LAST_NT_ERROR(Status);
  198. return FALSE;
  199. }
  200. a->ConsoleHandle = GET_CONSOLE_HANDLE;
  201. a->OutputHandle = hConsoleOutput;
  202. a->dwFlags = dwFlags;
  203. CsrClientCallServer( (PCSR_API_MSG)&m,
  204. NULL,
  205. CSR_MAKE_API_NUMBER( CONSRV_SERVERDLL_INDEX,
  206. ConsolepSetDisplayMode
  207. ),
  208. sizeof( *a )
  209. );
  210. if (!NT_SUCCESS( m.ReturnValue)) {
  211. SET_LAST_NT_ERROR(m.ReturnValue);
  212. NtClose(a->hEvent);
  213. return FALSE;
  214. }
  215. else {
  216. Status = NtWaitForSingleObject(a->hEvent, FALSE, NULL);
  217. NtClose(a->hEvent);
  218. if (Status != 0) {
  219. SET_LAST_NT_ERROR(Status);
  220. return FALSE;
  221. }
  222. try {
  223. *lpNewScreenBufferDimensions = a->ScreenBufferDimensions;
  224. } except( EXCEPTION_EXECUTE_HANDLER ) {
  225. SET_LAST_ERROR(ERROR_INVALID_ACCESS);
  226. return FALSE;
  227. }
  228. return TRUE;
  229. }
  230. #endif
  231. }
  232. BOOL
  233. APIENTRY
  234. RegisterConsoleVDM(
  235. IN DWORD dwRegisterFlags,
  236. IN HANDLE hStartHardwareEvent,
  237. IN HANDLE hEndHardwareEvent,
  238. IN HANDLE hErrorHardwareEvent,
  239. IN DWORD Reserved,
  240. OUT LPDWORD lpStateLength,
  241. OUT PVOID *lpState,
  242. IN LPWSTR lpVDMBufferSectionName,
  243. IN DWORD dwVDMBufferSectionNameLength,
  244. IN COORD VDMBufferSize OPTIONAL,
  245. OUT PVOID *lpVDMBuffer
  246. )
  247. /*++
  248. Description:
  249. This routine registers the VDM with the console.
  250. Parameters:
  251. hStartHardwareEvent - the event the VDM waits on to be
  252. notified of gaining/losing control of the hardware.
  253. hEndHardwareEvent - the event the VDM sets when it is done
  254. saving/restoring the hardware.
  255. Return value:
  256. TRUE - The operation was successful.
  257. FALSE/NULL - The operation failed. Extended error status is available
  258. using GetLastError.
  259. --*/
  260. {
  261. CONSOLE_API_MSG m;
  262. PCONSOLE_REGISTERVDM_MSG a = &m.u.RegisterConsoleVDM;
  263. PCSR_CAPTURE_HEADER CaptureBuffer;
  264. a->ConsoleHandle = GET_CONSOLE_HANDLE;
  265. a->RegisterFlags = dwRegisterFlags;
  266. if (dwRegisterFlags) {
  267. a->StartEvent = hStartHardwareEvent;
  268. a->EndEvent = hEndHardwareEvent;
  269. a->ErrorEvent = hErrorHardwareEvent;
  270. a->VDMBufferSectionNameLength = dwVDMBufferSectionNameLength;
  271. a->VDMBufferSize = VDMBufferSize;
  272. CaptureBuffer = CsrAllocateCaptureBuffer( 1,
  273. dwVDMBufferSectionNameLength
  274. );
  275. if (CaptureBuffer == NULL) {
  276. SET_LAST_ERROR(ERROR_NOT_ENOUGH_MEMORY);
  277. return FALSE;
  278. }
  279. CsrCaptureMessageBuffer( CaptureBuffer,
  280. lpVDMBufferSectionName,
  281. dwVDMBufferSectionNameLength,
  282. (PVOID *) &a->VDMBufferSectionName
  283. );
  284. } else {
  285. CaptureBuffer = NULL;
  286. }
  287. CsrClientCallServer( (PCSR_API_MSG)&m,
  288. CaptureBuffer,
  289. CSR_MAKE_API_NUMBER( CONSRV_SERVERDLL_INDEX,
  290. ConsolepRegisterVDM
  291. ),
  292. sizeof( *a )
  293. );
  294. if (CaptureBuffer != NULL) {
  295. CsrFreeCaptureBuffer( CaptureBuffer );
  296. }
  297. if (!NT_SUCCESS( m.ReturnValue)) {
  298. SET_LAST_NT_ERROR(m.ReturnValue);
  299. return FALSE;
  300. }
  301. else {
  302. if (dwRegisterFlags) {
  303. try {
  304. *lpStateLength = a->StateLength;
  305. *lpState = a->StateBuffer;
  306. *lpVDMBuffer = a->VDMBuffer;
  307. } except (EXCEPTION_EXECUTE_HANDLER) {
  308. SET_LAST_ERROR(ERROR_INVALID_ACCESS);
  309. return FALSE;
  310. }
  311. }
  312. return TRUE;
  313. }
  314. }
  315. BOOL
  316. APIENTRY
  317. GetConsoleHardwareState(
  318. IN HANDLE hConsoleOutput,
  319. OUT PCOORD lpResolution,
  320. OUT PCOORD lpFontSize
  321. )
  322. /*++
  323. Description:
  324. This routine returns the video resolution and font.
  325. Parameters:
  326. hConsoleOutput - Supplies a console output handle.
  327. lpResolution - Pointer to structure to store screen
  328. resolution in. Resolution is returned in pixels.
  329. lpFontSize - Pointer to structure to store font size in.
  330. Font size is returned in pixels.
  331. Return value:
  332. TRUE - The operation was successful.
  333. FALSE/NULL - The operation failed. Extended error status is available
  334. using GetLastError.
  335. --*/
  336. {
  337. CONSOLE_API_MSG m;
  338. PCONSOLE_GETHARDWARESTATE_MSG a = &m.u.GetConsoleHardwareState;
  339. a->ConsoleHandle = GET_CONSOLE_HANDLE;
  340. a->OutputHandle = hConsoleOutput;
  341. CsrClientCallServer( (PCSR_API_MSG)&m,
  342. NULL,
  343. CSR_MAKE_API_NUMBER( CONSRV_SERVERDLL_INDEX,
  344. ConsolepGetHardwareState
  345. ),
  346. sizeof( *a )
  347. );
  348. if (!NT_SUCCESS( m.ReturnValue)) {
  349. SET_LAST_NT_ERROR(m.ReturnValue);
  350. return FALSE;
  351. }
  352. else {
  353. try {
  354. *lpResolution = a->Resolution;
  355. *lpFontSize = a->FontSize;
  356. } except( EXCEPTION_EXECUTE_HANDLER ) {
  357. SET_LAST_ERROR(ERROR_INVALID_ACCESS);
  358. return FALSE;
  359. }
  360. return TRUE;
  361. }
  362. }
  363. BOOL
  364. APIENTRY
  365. SetConsoleHardwareState(
  366. IN HANDLE hConsoleOutput,
  367. IN COORD dwResolution,
  368. IN COORD dwFontSize
  369. )
  370. /*++
  371. Description:
  372. This routine set the video resolution and font.
  373. Parameters:
  374. hConsoleOutput - Supplies a console output handle.
  375. dwResolution - Contains screen resolution to set.
  376. Resolution is returned in pixels.
  377. dwFontSize - Contains font size to set.
  378. Font size is returned in pixels.
  379. Return value:
  380. TRUE - The operation was successful.
  381. FALSE/NULL - The operation failed. Extended error status is available
  382. using GetLastError.
  383. --*/
  384. {
  385. CONSOLE_API_MSG m;
  386. PCONSOLE_SETHARDWARESTATE_MSG a = &m.u.SetConsoleHardwareState;
  387. a->ConsoleHandle = GET_CONSOLE_HANDLE;
  388. a->OutputHandle = hConsoleOutput;
  389. a->Resolution = dwResolution;
  390. a->FontSize = dwFontSize;
  391. CsrClientCallServer( (PCSR_API_MSG)&m,
  392. NULL,
  393. CSR_MAKE_API_NUMBER( CONSRV_SERVERDLL_INDEX,
  394. ConsolepSetHardwareState
  395. ),
  396. sizeof( *a )
  397. );
  398. if (!NT_SUCCESS( m.ReturnValue)) {
  399. SET_LAST_NT_ERROR(m.ReturnValue);
  400. return FALSE;
  401. }
  402. else {
  403. return TRUE;
  404. }
  405. }
  406. BOOL
  407. APIENTRY
  408. GetConsoleDisplayMode(
  409. OUT LPDWORD lpModeFlags
  410. )
  411. /*++
  412. Description:
  413. This routine returns the display mode of the console.
  414. Parameters:
  415. lpModeFlags - pointer to store display mode in.
  416. Return value:
  417. TRUE - The operation was successful.
  418. FALSE/NULL - The operation failed. Extended error status is available
  419. using GetLastError.
  420. --*/
  421. {
  422. CONSOLE_API_MSG m;
  423. PCONSOLE_GETDISPLAYMODE_MSG a = &m.u.GetConsoleDisplayMode;
  424. a->ConsoleHandle = GET_CONSOLE_HANDLE;
  425. CsrClientCallServer( (PCSR_API_MSG)&m,
  426. NULL,
  427. CSR_MAKE_API_NUMBER( CONSRV_SERVERDLL_INDEX,
  428. ConsolepGetDisplayMode
  429. ),
  430. sizeof( *a )
  431. );
  432. if (!NT_SUCCESS( m.ReturnValue)) {
  433. SET_LAST_NT_ERROR(m.ReturnValue);
  434. return FALSE;
  435. }
  436. else {
  437. try {
  438. *lpModeFlags = a->ModeFlags;
  439. } except( EXCEPTION_EXECUTE_HANDLER ) {
  440. SET_LAST_ERROR(ERROR_INVALID_ACCESS);
  441. return FALSE;
  442. }
  443. return TRUE;
  444. }
  445. }
  446. BOOL
  447. APIENTRY
  448. SetConsoleKeyShortcuts(
  449. IN BOOL bSet,
  450. IN BYTE bReserveKeys,
  451. IN LPAPPKEY lpAppKeys,
  452. IN DWORD dwNumAppKeys
  453. )
  454. /*++
  455. Description:
  456. Only one set of key shortcuts is valid per console. Calling
  457. SetConsoleKeyShortcuts(set) overwrites any previous settings.
  458. SetConsoleKeyShortcuts(!set) removes any shortcuts.
  459. Parameters:
  460. bSet - if TRUE, set shortcuts. else remove shortcuts.
  461. bReserveKeys - byte containing reserve key info.
  462. lpAppKeys - pointer to application-defined shortcut keys. can be null.
  463. dwNumAppKeys - number of app keys contained in lpAppKeys.
  464. Return value:
  465. TRUE - The operation was successful.
  466. FALSE/NULL - The operation failed. Extended error status is available
  467. using GetLastError.
  468. --*/
  469. {
  470. CONSOLE_API_MSG m;
  471. PCONSOLE_SETKEYSHORTCUTS_MSG a = &m.u.SetConsoleKeyShortcuts;
  472. PCSR_CAPTURE_HEADER CaptureBuffer;
  473. a->ConsoleHandle = GET_CONSOLE_HANDLE;
  474. a->Set = bSet;
  475. a->ReserveKeys = bReserveKeys;
  476. if (lpAppKeys == NULL || dwNumAppKeys == 0) {
  477. a->NumAppKeys = 0;
  478. CaptureBuffer = NULL;
  479. } else {
  480. a->NumAppKeys = dwNumAppKeys;
  481. CaptureBuffer = CsrAllocateCaptureBuffer( 1,
  482. dwNumAppKeys * sizeof(APPKEY)
  483. );
  484. if (CaptureBuffer == NULL) {
  485. SET_LAST_ERROR(ERROR_NOT_ENOUGH_MEMORY);
  486. return FALSE;
  487. }
  488. CsrCaptureMessageBuffer( CaptureBuffer,
  489. lpAppKeys,
  490. dwNumAppKeys * sizeof(APPKEY),
  491. (PVOID *) &a->AppKeys
  492. );
  493. }
  494. CsrClientCallServer( (PCSR_API_MSG)&m,
  495. CaptureBuffer,
  496. CSR_MAKE_API_NUMBER( CONSRV_SERVERDLL_INDEX,
  497. ConsolepSetKeyShortcuts
  498. ),
  499. sizeof( *a )
  500. );
  501. if (CaptureBuffer)
  502. CsrFreeCaptureBuffer( CaptureBuffer );
  503. if (!NT_SUCCESS( m.ReturnValue)) {
  504. SET_LAST_NT_ERROR(m.ReturnValue);
  505. return FALSE;
  506. }
  507. else {
  508. return TRUE;
  509. }
  510. }
  511. BOOL
  512. APIENTRY
  513. SetConsoleMenuClose(
  514. IN BOOL bEnable
  515. )
  516. /*++
  517. Description:
  518. This routine returns the display mode of the console.
  519. Parameters:
  520. bEnable - if TRUE, close is enabled in the system menu.
  521. Return value:
  522. TRUE - The operation was successful.
  523. FALSE/NULL - The operation failed. Extended error status is available
  524. using GetLastError.
  525. --*/
  526. {
  527. CONSOLE_API_MSG m;
  528. PCONSOLE_SETMENUCLOSE_MSG a = &m.u.SetConsoleMenuClose;
  529. a->ConsoleHandle = GET_CONSOLE_HANDLE;
  530. a->Enable = bEnable;
  531. CsrClientCallServer( (PCSR_API_MSG)&m,
  532. NULL,
  533. CSR_MAKE_API_NUMBER( CONSRV_SERVERDLL_INDEX,
  534. ConsolepSetMenuClose
  535. ),
  536. sizeof( *a )
  537. );
  538. if (!NT_SUCCESS( m.ReturnValue)) {
  539. SET_LAST_NT_ERROR(m.ReturnValue);
  540. return FALSE;
  541. }
  542. else {
  543. return TRUE;
  544. }
  545. }
  546. #endif //!defined(BUILD_WOW6432)
  547. #if !defined(BUILD_WOW64)
  548. BOOL
  549. SetConsolePalette(
  550. IN HANDLE hConsoleOutput,
  551. IN HPALETTE hPalette,
  552. IN UINT dwUsage
  553. )
  554. /*++
  555. Description:
  556. Sets the palette for the console screen buffer.
  557. Parameters:
  558. hOutput - Supplies a console output handle.
  559. hPalette - Supplies a handle to the palette to set.
  560. dwUsage - Specifies use of the system palette.
  561. SYSPAL_NOSTATIC - System palette contains no static colors
  562. except black and white.
  563. SYSPAL_STATIC - System palette contains static colors
  564. which will not change when an application
  565. realizes its logical palette.
  566. Return value:
  567. TRUE - The operation was successful.
  568. FALSE/NULL - The operation failed. Extended error status is available
  569. using GetLastError.
  570. --*/
  571. {
  572. HANDLE hmodGdi;
  573. UNICODE_STRING ModuleNameString;
  574. NTSTATUS Status;
  575. if ( !pfnGdiConvertPalette ) {
  576. RtlInitUnicodeString( &ModuleNameString, L"gdi32" );
  577. Status = LdrLoadDll( UNICODE_NULL, NULL, &ModuleNameString, &hmodGdi );
  578. if ( !NT_SUCCESS(Status) ) {
  579. SET_LAST_NT_ERROR(Status);
  580. return FALSE;
  581. }
  582. pfnGdiConvertPalette = (PCONVPALFUNC)GetProcAddress(hmodGdi, "GdiConvertPalette");
  583. if (pfnGdiConvertPalette == NULL) {
  584. SET_LAST_NT_ERROR(STATUS_PROCEDURE_NOT_FOUND);
  585. return FALSE;
  586. }
  587. }
  588. hPalette = (*pfnGdiConvertPalette)(hPalette);
  589. return SetConsolePaletteInternal(hConsoleOutput,
  590. hPalette,
  591. dwUsage);
  592. }
  593. BOOL
  594. APIENTRY
  595. WriteConsoleInputVDMA(
  596. HANDLE hConsoleInput,
  597. PINPUT_RECORD lpBuffer,
  598. DWORD nLength,
  599. LPDWORD lpNumberOfEventsWritten
  600. )
  601. {
  602. return WriteConsoleInputInternal(hConsoleInput,lpBuffer,nLength,lpNumberOfEventsWritten,FALSE,FALSE);
  603. }
  604. BOOL
  605. APIENTRY
  606. WriteConsoleInputVDMW(
  607. HANDLE hConsoleInput,
  608. PINPUT_RECORD lpBuffer,
  609. DWORD nLength,
  610. LPDWORD lpNumberOfEventsWritten
  611. )
  612. {
  613. return WriteConsoleInputInternal(hConsoleInput,lpBuffer,nLength,lpNumberOfEventsWritten,TRUE,FALSE);
  614. }
  615. #endif //!defined(BUILD_WOW6432)