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.

521 lines
11 KiB

  1. /*++
  2. Copyright (c) 1991 - 2002 Microsoft Corporation
  3. Module Name:
  4. #### ## ## #### #### ##### #####
  5. ## # ## ## ## ## # ## ## ## ##
  6. ## ## ## ## ## ## ## ## ##
  7. ## ### ## ## ## ## ## ## ## ##
  8. ## ## ## ## ## ## ##### #####
  9. ## ## ## ## ## ## ## # ## ##
  10. ##### #### #### ## #### ## ##
  11. Abstract:
  12. This module contains the implementation for
  13. the GUI interface for the local display and
  14. keypad drivers.
  15. @@BEGIN_DDKSPLIT
  16. Author:
  17. Wesley Witt (wesw) 1-Oct-2001
  18. @@END_DDKSPLIT
  19. Environment:
  20. Kernel mode only.
  21. Notes:
  22. --*/
  23. #include <windows.h>
  24. #include <winioctl.h>
  25. #include <stdio.h>
  26. #include "resource.h"
  27. #include "saio.h"
  28. #include "..\inc\virtual.h"
  29. #define WIDTH(rect) (rect.right - rect.left)
  30. #define HEIGHT(rect) (rect.bottom - rect.top)
  31. #define WINDOW_SIZE_FACTOR 3
  32. #define Align(p, x) (((x) & ((p)-1)) ? (((x) & ~((p)-1)) + p) : (x))
  33. CRITICAL_SECTION WindowLock;
  34. HANDLE ReadyEvent;
  35. SA_DISPLAY_CAPS DisplayCaps;
  36. HWND hwndMain;
  37. HBITMAP hBitmap;
  38. PUCHAR DisplayBufferTmp;
  39. UCHAR DisplayBuffer[8192];
  40. ULONG DisplayBufferWidth;
  41. ULONG DisplayBufferHeight;
  42. HANDLE hFileKeypad;
  43. void
  44. dprintf(
  45. PSTR Format,
  46. ...
  47. )
  48. {
  49. char buf[512];
  50. va_list arg_ptr;
  51. PSTR s = buf;
  52. va_start(arg_ptr, Format);
  53. _vsnprintf( s, sizeof(buf), Format, arg_ptr );
  54. s += strlen(s);
  55. OutputDebugStringA( buf );
  56. }
  57. HANDLE
  58. OpenSaDevice(
  59. ULONG DeviceType
  60. )
  61. {
  62. HANDLE hDevice;
  63. WCHAR buf[128];
  64. wcscpy( buf, L"\\\\?\\GLOBALROOT" );
  65. switch (DeviceType) {
  66. case SA_DEVICE_DISPLAY:
  67. wcscat( buf, SA_DEVICE_DISPLAY_NAME_STRING );
  68. break;
  69. case SA_DEVICE_KEYPAD:
  70. wcscat( buf, SA_DEVICE_KEYPAD_NAME_STRING );
  71. break;
  72. case SA_DEVICE_NVRAM:
  73. wcscat( buf, SA_DEVICE_NVRAM_NAME_STRING );
  74. break;
  75. }
  76. hDevice = CreateFileW(
  77. buf,
  78. GENERIC_READ | GENERIC_WRITE,
  79. FILE_SHARE_READ | FILE_SHARE_WRITE,
  80. NULL,
  81. OPEN_EXISTING,
  82. 0,
  83. NULL
  84. );
  85. if (hDevice == INVALID_HANDLE_VALUE) {
  86. dprintf( "CreateFile failed [%ws], ec=%d\n", buf, GetLastError() );
  87. }
  88. return hDevice;
  89. }
  90. void
  91. CenterWindow (
  92. IN HWND hwnd,
  93. IN HWND Parent
  94. )
  95. {
  96. RECT WndRect, ParentRect;
  97. int x, y;
  98. if (!Parent) {
  99. ParentRect.left = 0;
  100. ParentRect.top = 0;
  101. ParentRect.right = GetSystemMetrics (SM_CXFULLSCREEN);
  102. ParentRect.bottom = GetSystemMetrics (SM_CYFULLSCREEN);
  103. } else {
  104. GetWindowRect (Parent, &ParentRect);
  105. }
  106. GetWindowRect (hwnd, &WndRect);
  107. x = ParentRect.left + (WIDTH(ParentRect) - WIDTH(WndRect)) / 2;
  108. y = ParentRect.top + (HEIGHT(ParentRect) - HEIGHT(WndRect)) / 2;
  109. SetWindowPos (hwnd, NULL, x, y, 0, 0, SWP_NOZORDER|SWP_NOSIZE);
  110. }
  111. void
  112. ConvertBottomLeft2TopLeft(
  113. PUCHAR Bits,
  114. ULONG Width,
  115. ULONG Height
  116. )
  117. {
  118. ULONG Row;
  119. ULONG Col;
  120. ULONG Size;
  121. ULONG i;
  122. UCHAR Temp;
  123. Size = (Width * Height) >> 3;
  124. for (i = 0; i < Size; i++) {
  125. Bits[i] = ~Bits[i];
  126. }
  127. Width = Width >> 3;
  128. for (Row = 0; Row < (Height / 2); Row++) {
  129. for (Col = 0; Col < Width; Col++) {
  130. Temp = Bits[Row * Width + Col];
  131. Bits[Row * Width + Col] = Bits[(Height - Row - 1) * Width + Col];
  132. Bits[(Height - Row - 1) * Width + Col] = Temp;
  133. }
  134. }
  135. }
  136. DWORD
  137. UpdateBitmap(
  138. HWND hwnd
  139. )
  140. {
  141. DWORD ec;
  142. HDC hdc;
  143. HDC hdcTemp;
  144. UCHAR BmiBuf[sizeof(BITMAPINFO)+sizeof(RGBQUAD)];
  145. PBITMAPINFO Bmi = (PBITMAPINFO)BmiBuf;
  146. PUCHAR Bits;
  147. int lines;
  148. if (hBitmap) {
  149. DeleteObject( hBitmap );
  150. }
  151. hdc = GetDC( hwnd );
  152. hdcTemp = CreateCompatibleDC( hdc );
  153. SetMapMode( hdcTemp, MM_TEXT );
  154. memset( BmiBuf, 0, sizeof(BmiBuf) );
  155. Bmi->bmiHeader.biSize = sizeof(BITMAPINFOHEADER);
  156. Bmi->bmiHeader.biWidth = DisplayCaps.DisplayWidth*3;
  157. Bmi->bmiHeader.biHeight = DisplayCaps.DisplayHeight*3;
  158. Bmi->bmiHeader.biPlanes = 1;
  159. Bmi->bmiHeader.biBitCount = 1;
  160. Bmi->bmiHeader.biCompression = BI_RGB;
  161. Bmi->bmiColors[0].rgbBlue = 0;
  162. Bmi->bmiColors[0].rgbGreen = 0;
  163. Bmi->bmiColors[0].rgbRed = 0;
  164. Bmi->bmiColors[1].rgbBlue = 0xff;
  165. Bmi->bmiColors[1].rgbGreen = 0xff;
  166. Bmi->bmiColors[1].rgbRed = 0xff;
  167. hBitmap = CreateDIBSection(
  168. hdcTemp,
  169. Bmi,
  170. DIB_RGB_COLORS,
  171. (void**)&Bits,
  172. NULL,
  173. 0
  174. );
  175. if (hBitmap == NULL) {
  176. ec = GetLastError();
  177. return ec;
  178. }
  179. SelectObject( hdcTemp, hBitmap );
  180. Bmi->bmiHeader.biWidth = DisplayCaps.DisplayWidth;
  181. Bmi->bmiHeader.biHeight = DisplayCaps.DisplayHeight;
  182. ConvertBottomLeft2TopLeft( DisplayBuffer, DisplayCaps.DisplayWidth, DisplayCaps.DisplayHeight );
  183. lines = StretchDIBits(
  184. hdcTemp,
  185. 0,
  186. 0,
  187. DisplayCaps.DisplayWidth*WINDOW_SIZE_FACTOR,
  188. DisplayCaps.DisplayHeight*WINDOW_SIZE_FACTOR,
  189. 0,
  190. 0,
  191. DisplayCaps.DisplayWidth,
  192. DisplayCaps.DisplayHeight,
  193. DisplayBuffer,
  194. Bmi,
  195. DIB_RGB_COLORS,
  196. SRCCOPY
  197. );
  198. if (lines == GDI_ERROR) {
  199. ec = GetLastError();
  200. return ec;
  201. }
  202. GdiFlush();
  203. DeleteDC( hdcTemp );
  204. ReleaseDC( hwnd, hdc );
  205. PostMessage( GetDlgItem(hwnd,IDC_BITMAP), STM_SETIMAGE, IMAGE_BITMAP, (LPARAM)hBitmap );
  206. return 0;
  207. }
  208. INT_PTR CALLBACK
  209. MsGuiWndProc(
  210. HWND hwnd,
  211. UINT message,
  212. WPARAM wParam,
  213. LPARAM lParam
  214. )
  215. {
  216. PAINTSTRUCT ps;
  217. BOOL b;
  218. HDC hdc;
  219. BITMAP BitMap;
  220. HBITMAP hBitMap;
  221. HBITMAP hBitMapBig;
  222. BITMAPINFO Bmi;
  223. HDC hdcTemp;
  224. HDC hdcBig;
  225. RECT Rect;
  226. int lines;
  227. DWORD ec;
  228. UCHAR Keypress;
  229. ULONG Bytes;
  230. switch (message) {
  231. case WM_CREATE:
  232. return 0;
  233. case WM_INITDIALOG:
  234. CenterWindow( hwnd, NULL );
  235. return 1;
  236. case WM_USER:
  237. UpdateBitmap( hwnd );
  238. return 0;
  239. case WM_COMMAND:
  240. switch (wParam) {
  241. case IDC_ESCAPE:
  242. Keypress = SA_KEYPAD_CANCEL;
  243. break;
  244. case IDC_ENTER:
  245. Keypress = SA_KEYPAD_SELECT;
  246. break;
  247. case IDC_LEFT:
  248. Keypress = SA_KEYPAD_LEFT;
  249. break;
  250. case IDC_RIGHT:
  251. Keypress = SA_KEYPAD_RIGHT;
  252. break;
  253. case IDC_UP:
  254. Keypress = SA_KEYPAD_UP;
  255. break;
  256. case IDC_DOWN:
  257. Keypress = SA_KEYPAD_DOWN;
  258. break;
  259. default:
  260. Keypress = 0;
  261. break;
  262. }
  263. if (Keypress) {
  264. b = DeviceIoControl(
  265. hFileKeypad,
  266. IOCTL_VDRIVER_INIT,
  267. &Keypress,
  268. sizeof(UCHAR),
  269. NULL,
  270. 0,
  271. &Bytes,
  272. NULL
  273. );
  274. if (!b) {
  275. OutputDebugString( L"IOCTL_VDRIVER_INIT failed\n" );
  276. ExitProcess( 0 );
  277. }
  278. }
  279. return 0;
  280. case WM_DESTROY:
  281. PostQuitMessage( 0 );
  282. return 0;
  283. }
  284. return DefWindowProc( hwnd, message, wParam, lParam );
  285. }
  286. DWORD
  287. DisplayBufferThread(
  288. PVOID pv
  289. )
  290. {
  291. HANDLE hFile;
  292. MSDISP_BUFFER_DATA BufferData;
  293. BOOL b;
  294. ULONG Bytes;
  295. HANDLE hEvent;
  296. hFile = OpenSaDevice(
  297. SA_DEVICE_DISPLAY
  298. );
  299. if (hFile == INVALID_HANDLE_VALUE) {
  300. OutputDebugString( L"OpenSaDevice failed\n" );
  301. ExitProcess( 0 );
  302. }
  303. DisplayCaps.SizeOfStruct = sizeof(SA_DISPLAY_CAPS);
  304. b = DeviceIoControl(
  305. hFile,
  306. IOCTL_SA_GET_CAPABILITIES,
  307. NULL,
  308. 0,
  309. &DisplayCaps,
  310. sizeof(SA_DISPLAY_CAPS),
  311. &Bytes,
  312. NULL
  313. );
  314. if (!b) {
  315. OutputDebugString( L"IOCTL_SA_GET_CAPABILITIES failed\n" );
  316. ExitProcess( 0 );
  317. }
  318. BufferData.DisplayBuffer = NULL;
  319. BufferData.DisplayBufferLength = 0;
  320. b = DeviceIoControl(
  321. hFile,
  322. IOCTL_VDRIVER_INIT,
  323. NULL,
  324. 0,
  325. &BufferData,
  326. sizeof(BufferData),
  327. &Bytes,
  328. NULL
  329. );
  330. if (!b) {
  331. OutputDebugString( L"IOCTL_MSDISP_INIT failed\n" );
  332. ExitProcess( 0 );
  333. }
  334. DisplayBufferTmp = (PUCHAR)BufferData.DisplayBuffer;
  335. hEvent = CreateEvent( NULL, FALSE, FALSE, MSDISP_EVENT_NAME );
  336. if (hEvent == NULL) {
  337. OutputDebugString( L"OpenEvent failed\n" );
  338. ExitProcess( 0 );
  339. }
  340. SetEvent( ReadyEvent );
  341. while (TRUE) {
  342. WaitForSingleObject( hEvent, INFINITE );
  343. EnterCriticalSection( &WindowLock );
  344. memcpy( DisplayBuffer, DisplayBufferTmp, (DisplayCaps.DisplayWidth/8)*DisplayCaps.DisplayHeight );
  345. LeaveCriticalSection( &WindowLock );
  346. PostMessage( hwndMain, WM_USER, 0, 0 );
  347. }
  348. return 0;
  349. }
  350. int
  351. __cdecl
  352. wmain(
  353. int argc,
  354. WCHAR *argv[]
  355. )
  356. {
  357. HWND hwnd;
  358. MSG msg;
  359. WNDCLASS wndclass;
  360. HINSTANCE hInst;
  361. HANDLE hThread;
  362. hInst = GetModuleHandle( NULL );
  363. InitializeCriticalSection( &WindowLock );
  364. ReadyEvent = CreateEvent( NULL, FALSE, FALSE, NULL );
  365. if (ReadyEvent == NULL) {
  366. OutputDebugString( L"CreateEvent failed\n" );
  367. return -1;
  368. }
  369. hFileKeypad = OpenSaDevice(
  370. SA_DEVICE_KEYPAD
  371. );
  372. if (hFileKeypad == INVALID_HANDLE_VALUE) {
  373. OutputDebugString( L"OpenSaDevice failed\n" );
  374. return -1;
  375. }
  376. hThread = CreateThread(
  377. NULL,
  378. 0,
  379. DisplayBufferThread,
  380. NULL,
  381. 0,
  382. NULL
  383. );
  384. if (hThread == NULL) {
  385. OutputDebugString( L"CreateThread failed\n" );
  386. return -1;
  387. }
  388. WaitForSingleObject( ReadyEvent, INFINITE );
  389. hInst = GetModuleHandle( NULL );
  390. wndclass.style = CS_HREDRAW | CS_VREDRAW;
  391. wndclass.lpfnWndProc = (WNDPROC)MsGuiWndProc;
  392. wndclass.cbClsExtra = 0;
  393. wndclass.cbWndExtra = DLGWINDOWEXTRA;
  394. wndclass.hInstance = hInst;
  395. wndclass.hIcon = LoadIcon( hInst, MAKEINTRESOURCE(APPICON) );
  396. wndclass.hCursor = LoadCursor( NULL, IDC_ARROW );
  397. wndclass.hbrBackground = (HBRUSH) (COLOR_3DFACE + 1);
  398. wndclass.lpszMenuName = NULL;
  399. wndclass.lpszClassName = L"SaDialog";
  400. RegisterClass( &wndclass );
  401. hwnd = CreateDialog(
  402. hInst,
  403. MAKEINTRESOURCE(IDD_DIALOG),
  404. 0,
  405. MsGuiWndProc
  406. );
  407. hwndMain = hwnd;
  408. ShowWindow( hwnd, SW_SHOWNORMAL );
  409. while (GetMessage (&msg, NULL, 0, 0)) {
  410. if (!IsDialogMessage( hwnd, &msg )) {
  411. TranslateMessage (&msg) ;
  412. DispatchMessage (&msg) ;
  413. }
  414. }
  415. return 0;
  416. }