[REACTOS]
[reactos.git] / reactos / boot / freeldr / fdebug / fdebug.c
1 // fdebug.cpp : Defines the entry point for the application.
2 //
3
4 #include <windows.h>
5 #include <commdlg.h>
6 #include <process.h>
7 #include <stdio.h>
8 #include <tchar.h>
9
10 #include "resource.h"
11 #include "rs232.h"
12
13 #define MAX_LOADSTRING 100
14
15 // Global Variables:
16 HINSTANCE hInst; // current instance
17 TCHAR szTitle[MAX_LOADSTRING]; // The title bar text
18 TCHAR szWindowClass[MAX_LOADSTRING]; // The title bar text
19 HWND hMainWnd; // The main window handle
20 HWND hDisplayWnd; // The window to display the incoming data
21 HWND hEditWnd; // The edit window to get input from the user
22 TCHAR strComPort[MAX_PATH] = TEXT("COM1"); // The COM port to use
23 TCHAR strBaudRate[MAX_PATH] = TEXT("115200"); // The baud rate to use
24 TCHAR strCaptureFileName[MAX_PATH] = TEXT(""); // The file name to capture to
25 BOOL bConnected = FALSE; // Tells us if we are currently connected
26 BOOL bCapturing = FALSE; // Tells us if we are currently capturing data
27 BOOL bLocalEcho = FALSE; // Tells us if local echo is currently enabled
28 HANDLE hCaptureFile; // Handle to the capture file
29 DWORD dwThreadId = 0; // Thread id of RS232 communication thread
30
31 // Foward declarations of functions included in this code module:
32 ATOM MyRegisterClass(HINSTANCE hInstance);
33 BOOL InitInstance(HINSTANCE, int);
34 LRESULT CALLBACK WndProc(HWND, UINT, WPARAM, LPARAM);
35 LRESULT CALLBACK About(HWND, UINT, WPARAM, LPARAM);
36 LRESULT CALLBACK ConnectionDialogProc(HWND, UINT, WPARAM, LPARAM);
37 LRESULT CALLBACK CaptureDialogProc(HWND, UINT, WPARAM, LPARAM);
38 VOID EnableFileMenuItemByID(UINT Id, BOOL Enable);
39 VOID CheckLocalEchoMenuItem(BOOL Checked);
40 VOID Rs232Thread(VOID* Parameter);
41
42 int APIENTRY _tWinMain(HINSTANCE hInstance,
43 HINSTANCE hPrevInstance,
44 LPTSTR lpCmdLine,
45 int nCmdShow)
46 {
47 // TODO: Place code here.
48 MSG msg;
49 HACCEL hAccelTable;
50
51 UNREFERENCED_PARAMETER(lpCmdLine);
52 UNREFERENCED_PARAMETER(hPrevInstance);
53
54 // Initialize global strings
55 LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
56 LoadString(hInstance, IDC_FDEBUG, szWindowClass, MAX_LOADSTRING);
57 MyRegisterClass(hInstance);
58
59 // Perform application initialization:
60 if (!InitInstance (hInstance, nCmdShow))
61 {
62 return FALSE;
63 }
64
65 hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_FDEBUG);
66
67 // Main message loop:
68 while (GetMessage(&msg, NULL, 0, 0))
69 {
70 if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
71 {
72 TranslateMessage(&msg);
73 DispatchMessage(&msg);
74 }
75 }
76
77 return (int)msg.wParam;
78 }
79
80
81
82 //
83 // FUNCTION: MyRegisterClass()
84 //
85 // PURPOSE: Registers the window class.
86 //
87 // COMMENTS:
88 //
89 // This function and its usage is only necessary if you want this code
90 // to be compatible with Win32 systems prior to the 'RegisterClassEx'
91 // function that was added to Windows 95. It is important to call this function
92 // so that the application will get 'well formed' small icons associated
93 // with it.
94 //
95 ATOM MyRegisterClass(HINSTANCE hInstance)
96 {
97 WNDCLASSEX wcex;
98
99 wcex.cbSize = sizeof(WNDCLASSEX);
100
101 wcex.style = CS_HREDRAW | CS_VREDRAW;
102 wcex.lpfnWndProc = (WNDPROC)WndProc;
103 wcex.cbClsExtra = 0;
104 wcex.cbWndExtra = 0;
105 wcex.hInstance = hInstance;
106 wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_FDEBUG));
107 wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
108 wcex.hbrBackground = NULL;//(HBRUSH)(COLOR_WINDOW+1);
109 wcex.lpszMenuName = MAKEINTRESOURCE(IDC_FDEBUG);
110 wcex.lpszClassName = szWindowClass;
111 wcex.hIconSm = (HICON)LoadImage(hInstance,
112 MAKEINTRESOURCE(IDI_FDEBUG),
113 IMAGE_ICON,
114 16,
115 16,
116 LR_SHARED);
117
118 return RegisterClassEx(&wcex);
119 }
120
121 //
122 // FUNCTION: InitInstance(HANDLE, int)
123 //
124 // PURPOSE: Saves instance handle and creates main window
125 //
126 // COMMENTS:
127 //
128 // In this function, we save the instance handle in a global variable and
129 // create and display the main program window.
130 //
131 BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
132 {
133 HWND hWnd;
134
135 hInst = hInstance; // Store instance handle in our global variable
136
137 hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
138 CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
139
140 if (!hWnd)
141 {
142 return FALSE;
143 }
144
145 hMainWnd = hWnd;
146
147 ShowWindow(hWnd, nCmdShow);
148 UpdateWindow(hWnd);
149
150 return TRUE;
151 }
152
153 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
154 {
155 int wmId, wmEvent;
156 PAINTSTRUCT ps;
157 HDC hdc;
158 RECT rc;
159 TCHAR WndText[MAX_PATH];
160 DWORD Index;
161 NONCLIENTMETRICS ncm;
162 HFONT hFont;
163
164 switch (message)
165 {
166 case WM_CREATE:
167
168 hEditWnd = CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("EDIT"), TEXT(""), WS_CHILD|WS_VISIBLE|WS_VSCROLL|ES_AUTOHSCROLL|ES_LEFT|ES_MULTILINE, 0, 0, 0, 0, hWnd, NULL, hInst, NULL);
169 hDisplayWnd = CreateWindowEx(WS_EX_CLIENTEDGE, TEXT("EDIT"), TEXT(""), WS_CHILD|WS_VISIBLE|WS_HSCROLL|WS_VSCROLL|ES_MULTILINE, 0, 0, 0, 0, hWnd, NULL, hInst, NULL);
170
171 memset(&ncm, 0, sizeof(NONCLIENTMETRICS));
172 ncm.cbSize = sizeof(NONCLIENTMETRICS);
173 SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS), &ncm, 0);
174
175 hFont = CreateFontIndirect(&ncm.lfMessageFont);
176
177 SendMessage(hEditWnd, WM_SETFONT, (WPARAM)hFont, TRUE);
178 SendMessage(hDisplayWnd, WM_SETFONT, (WPARAM)hFont, TRUE);
179
180 break;
181 case WM_COMMAND:
182 wmId = LOWORD(wParam);
183 wmEvent = HIWORD(wParam);
184
185 if (lParam == (LPARAM)hEditWnd && wmEvent == EN_CHANGE)
186 {
187 GetWindowText(hEditWnd, WndText, MAX_PATH);
188
189 if (_tcslen(WndText) > 0)
190 {
191 SetWindowText(hEditWnd, TEXT(""));
192
193 if (!bConnected)
194 {
195 MessageBox(hWnd, TEXT("You are not currently connected!"), TEXT("Error"), MB_OK|MB_ICONSTOP);
196 break;
197 }
198
199 for (Index=0; Index<_tcslen(WndText); Index++)
200 {
201 if (dwThreadId != 0)
202 {
203 PostThreadMessage(dwThreadId, WM_CHAR, (WPARAM)WndText[Index], (LPARAM)0);
204 }
205 }
206 }
207 }
208
209 // Parse the menu selections:
210 switch (wmId)
211 {
212 case IDM_ABOUT:
213 DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
214 break;
215 case IDM_EXIT:
216 DestroyWindow(hWnd);
217 break;
218 case IDM_FILE_CLEARDISPLAY:
219 SetWindowText(hDisplayWnd, TEXT(""));
220 break;
221 case IDM_FILE_CONNECT:
222 if (bConnected)
223 {
224 MessageBox(hWnd, TEXT("You are already connected!"), TEXT("Error"), MB_OK|MB_ICONSTOP);
225 }
226 else
227 {
228 if (DialogBox(hInst, (LPCTSTR)IDD_CONNECTION, hWnd, (DLGPROC)ConnectionDialogProc) == IDOK)
229 {
230 bConnected = TRUE;
231 EnableFileMenuItemByID(IDM_FILE_DISCONNECT, TRUE);
232 EnableFileMenuItemByID(IDM_FILE_CONNECT, FALSE);
233 _beginthread(Rs232Thread, 0, NULL);
234 }
235 }
236 break;
237 case IDM_FILE_DISCONNECT:
238 if (bConnected)
239 {
240 bConnected = FALSE;
241 EnableFileMenuItemByID(IDM_FILE_DISCONNECT, FALSE);
242 EnableFileMenuItemByID(IDM_FILE_CONNECT, TRUE);
243 }
244 else
245 {
246 MessageBox(hWnd, TEXT("You are not currently connected!"), TEXT("Error"), MB_OK|MB_ICONSTOP);
247 }
248 break;
249 case IDM_FILE_STARTCAPTURE:
250 if (DialogBox(hInst, (LPCTSTR)IDD_CAPTURE, hWnd, (DLGPROC)CaptureDialogProc) == IDOK)
251 {
252 bCapturing = TRUE;
253 EnableFileMenuItemByID(IDM_FILE_STOPCAPTURE, TRUE);
254 EnableFileMenuItemByID(IDM_FILE_STARTCAPTURE, FALSE);
255 hCaptureFile = CreateFile(strCaptureFileName, FILE_APPEND_DATA, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
256 }
257 break;
258 case IDM_FILE_STOPCAPTURE:
259 if (bCapturing)
260 {
261 bCapturing = FALSE;
262 EnableFileMenuItemByID(IDM_FILE_STOPCAPTURE, FALSE);
263 EnableFileMenuItemByID(IDM_FILE_STARTCAPTURE, TRUE);
264 CloseHandle(hCaptureFile);
265 hCaptureFile = NULL;
266 }
267 break;
268 case IDM_FILE_LOCALECHO:
269 if (bLocalEcho)
270 {
271 bLocalEcho = FALSE;
272 CheckLocalEchoMenuItem(bLocalEcho);
273 }
274 else
275 {
276 bLocalEcho = TRUE;
277 CheckLocalEchoMenuItem(bLocalEcho);
278 }
279 break;
280 default:
281 return DefWindowProc(hWnd, message, wParam, lParam);
282 }
283 break;
284 case WM_PAINT:
285 hdc = BeginPaint(hWnd, &ps);
286 EndPaint(hWnd, &ps);
287 break;
288 case WM_SIZE:
289
290 GetClientRect(hWnd, &rc);
291
292 MoveWindow(hDisplayWnd, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top - 20, TRUE);
293 MoveWindow(hEditWnd, rc.left, rc.bottom - 20, rc.right - rc.left, 20, TRUE);
294
295 break;
296 case WM_DESTROY:
297 PostQuitMessage(0);
298 break;
299 default:
300 return DefWindowProc(hWnd, message, wParam, lParam);
301 }
302 return 0;
303 }
304
305 LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
306 {
307 HWND hLicenseEditWnd;
308 TCHAR strLicense[0x1000];
309
310 UNREFERENCED_PARAMETER(lParam);
311
312 switch (message)
313 {
314 case WM_INITDIALOG:
315
316 hLicenseEditWnd = GetDlgItem(hDlg, IDC_LICENSE_EDIT);
317
318 LoadString(hInst, IDS_LICENSE, strLicense, 0x1000);
319
320 SetWindowText(hLicenseEditWnd, strLicense);
321
322 return TRUE;
323
324 case WM_COMMAND:
325 if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
326 {
327 EndDialog(hDlg, LOWORD(wParam));
328 return TRUE;
329 }
330 break;
331 }
332 return FALSE;
333 }
334
335 LRESULT CALLBACK ConnectionDialogProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
336 {
337 UNREFERENCED_PARAMETER(lParam);
338
339 switch (message)
340 {
341 case WM_INITDIALOG:
342
343 SetWindowText(GetDlgItem(hDlg, IDC_COMPORT), strComPort);
344 SetWindowText(GetDlgItem(hDlg, IDC_BAUTRATE), strBaudRate);
345
346 return TRUE;
347
348 case WM_COMMAND:
349 if (LOWORD(wParam) == IDOK)
350 {
351 GetWindowText(GetDlgItem(hDlg, IDC_COMPORT), strComPort, MAX_PATH);
352 GetWindowText(GetDlgItem(hDlg, IDC_BAUTRATE), strBaudRate, MAX_PATH);
353 }
354
355 if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
356 {
357 EndDialog(hDlg, LOWORD(wParam));
358 return TRUE;
359 }
360 break;
361 }
362 return FALSE;
363 }
364
365 LRESULT CALLBACK CaptureDialogProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
366 {
367 OPENFILENAME ofn;
368
369 UNREFERENCED_PARAMETER(lParam);
370
371 switch (message)
372 {
373 case WM_INITDIALOG:
374
375 SetWindowText(GetDlgItem(hDlg, IDC_CAPTUREFILENAME), strCaptureFileName);
376
377 return TRUE;
378
379 case WM_COMMAND:
380 if (LOWORD(wParam) == IDC_BROWSE)
381 {
382 memset(&ofn, 0, sizeof(OPENFILENAME));
383 ofn.lStructSize = sizeof(OPENFILENAME);
384 ofn.hwndOwner = hDlg;
385 ofn.hInstance = hInst;
386 ofn.lpstrFilter = NULL;
387 ofn.lpstrCustomFilter = NULL;
388 ofn.nMaxCustFilter = 0;
389 ofn.nFilterIndex = 0;
390 ofn.lpstrFile = strCaptureFileName;
391 ofn.nMaxFile = MAX_PATH;
392 ofn.lpstrFileTitle = NULL;
393 ofn.nMaxFileTitle = 0;
394 ofn.lpstrInitialDir = NULL;
395 ofn.lpstrTitle = NULL;
396 ofn.Flags = OFN_HIDEREADONLY|OFN_NOREADONLYRETURN;
397
398 if (GetOpenFileName(&ofn))
399 {
400 SetWindowText(GetDlgItem(hDlg, IDC_CAPTUREFILENAME), strCaptureFileName);
401 }
402 }
403
404 if (LOWORD(wParam) == IDOK)
405 {
406 GetWindowText(GetDlgItem(hDlg, IDC_CAPTUREFILENAME), strCaptureFileName, MAX_PATH);
407 }
408
409 if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
410 {
411 EndDialog(hDlg, LOWORD(wParam));
412 return TRUE;
413 }
414 break;
415 }
416 return FALSE;
417 }
418
419 VOID EnableFileMenuItemByID(UINT Id, BOOL Enable)
420 {
421 HMENU hMenuBar;
422 HMENU hFileMenu;
423
424 hMenuBar = GetMenu(hMainWnd);
425 hFileMenu = GetSubMenu(hMenuBar, 0);
426 EnableMenuItem(hFileMenu, Id, MF_BYCOMMAND|(Enable ? MF_ENABLED : MF_GRAYED));
427 }
428
429 VOID CheckLocalEchoMenuItem(BOOL Checked)
430 {
431 HMENU hMenuBar;
432 HMENU hFileMenu;
433
434 hMenuBar = GetMenu(hMainWnd);
435 hFileMenu = GetSubMenu(hMenuBar, 0);
436 CheckMenuItem(hFileMenu, IDM_FILE_LOCALECHO, MF_BYCOMMAND|(Checked ? MF_CHECKED : MF_UNCHECKED));
437 }
438
439 VOID Rs232Thread(VOID* Parameter)
440 {
441 BYTE Byte;
442 TCHAR String[MAX_PATH];
443 MSG msg;
444 DWORD dwNumberOfBytesWritten;
445
446 UNREFERENCED_PARAMETER(Parameter);
447
448 dwThreadId = GetCurrentThreadId();
449
450 if (!Rs232OpenPortWin32(strComPort))
451 {
452 MessageBox(hMainWnd, TEXT("Error opening port!"), TEXT("Error"), MB_OK|MB_ICONSTOP);
453 bConnected = FALSE;
454 return;
455 }
456
457 _stprintf(String, TEXT("%s,n,8,1"), strBaudRate);
458 if (!Rs232ConfigurePortWin32(String))
459 {
460 MessageBox(hMainWnd, TEXT("Error configuring port!"), TEXT("Error"), MB_OK|MB_ICONSTOP);
461 bConnected = FALSE;
462 return;
463 }
464
465 while (bConnected)
466 {
467 if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
468 {
469 if (msg.message == WM_CHAR)
470 {
471 Rs232WriteByteWin32((BYTE)msg.wParam);
472
473 if (bLocalEcho && msg.wParam != (WPARAM)TEXT('\r'))
474 {
475 PostMessage(hDisplayWnd, WM_CHAR, (WPARAM)msg.wParam, (LPARAM)0);
476
477 if (hCaptureFile)
478 {
479 WriteFile(hCaptureFile, &msg.wParam, sizeof(TCHAR), &dwNumberOfBytesWritten, NULL);
480 }
481 }
482 }
483 }
484
485 if (Rs232ReadByteWin32(&Byte))
486 {
487 _stprintf(String, TEXT("%c"), Byte);
488
489 PostMessage(hDisplayWnd, WM_CHAR, (WPARAM)String[0], (LPARAM)0);
490
491 if (hCaptureFile)
492 {
493 WriteFile(hCaptureFile, &String[0], sizeof(TCHAR), &dwNumberOfBytesWritten, NULL);
494 }
495 }
496 }
497
498 dwThreadId = 0;
499 Rs232ClosePortWin32();
500 }