- Create KD branch. All debugging support is removed in this branch (no symbols,...
[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 EnableConnectMenuItem(BOOL Enable);
39 VOID EnableDisconnectMenuItem(BOOL Enable);
40 VOID EnableStartCaptureMenuItem(BOOL Enable);
41 VOID EnableStopCaptureMenuItem(BOOL Enable);
42 VOID CheckLocalEchoMenuItem(BOOL Checked);
43 VOID Rs232Thread(VOID* Parameter);
44
45 int APIENTRY WinMain(HINSTANCE hInstance,
46 HINSTANCE hPrevInstance,
47 LPSTR lpCmdLine,
48 int nCmdShow)
49 {
50 // TODO: Place code here.
51 MSG msg;
52 HACCEL hAccelTable;
53
54 UNREFERENCED_PARAMETER(lpCmdLine);
55 UNREFERENCED_PARAMETER(hPrevInstance);
56
57 // Initialize global strings
58 LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
59 LoadString(hInstance, IDC_FDEBUG, szWindowClass, MAX_LOADSTRING);
60 MyRegisterClass(hInstance);
61
62 // Perform application initialization:
63 if (!InitInstance (hInstance, nCmdShow))
64 {
65 return FALSE;
66 }
67
68 hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_FDEBUG);
69
70 // Main message loop:
71 while (GetMessage(&msg, NULL, 0, 0))
72 {
73 if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
74 {
75 TranslateMessage(&msg);
76 DispatchMessage(&msg);
77 }
78 }
79
80 return (int)msg.wParam;
81 }
82
83
84
85 //
86 // FUNCTION: MyRegisterClass()
87 //
88 // PURPOSE: Registers the window class.
89 //
90 // COMMENTS:
91 //
92 // This function and its usage is only necessary if you want this code
93 // to be compatible with Win32 systems prior to the 'RegisterClassEx'
94 // function that was added to Windows 95. It is important to call this function
95 // so that the application will get 'well formed' small icons associated
96 // with it.
97 //
98 ATOM MyRegisterClass(HINSTANCE hInstance)
99 {
100 WNDCLASSEX wcex;
101
102 wcex.cbSize = sizeof(WNDCLASSEX);
103
104 wcex.style = CS_HREDRAW | CS_VREDRAW;
105 wcex.lpfnWndProc = (WNDPROC)WndProc;
106 wcex.cbClsExtra = 0;
107 wcex.cbWndExtra = 0;
108 wcex.hInstance = hInstance;
109 wcex.hIcon = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_FDEBUG));
110 wcex.hCursor = LoadCursor(NULL, IDC_ARROW);
111 wcex.hbrBackground = NULL;//(HBRUSH)(COLOR_WINDOW+1);
112 wcex.lpszMenuName = MAKEINTRESOURCE(IDC_FDEBUG);
113 wcex.lpszClassName = szWindowClass;
114 wcex.hIconSm = (HICON)LoadImage(hInstance,
115 MAKEINTRESOURCE(IDI_FDEBUG),
116 IMAGE_ICON,
117 16,
118 16,
119 LR_SHARED);
120
121 return RegisterClassEx(&wcex);
122 }
123
124 //
125 // FUNCTION: InitInstance(HANDLE, int)
126 //
127 // PURPOSE: Saves instance handle and creates main window
128 //
129 // COMMENTS:
130 //
131 // In this function, we save the instance handle in a global variable and
132 // create and display the main program window.
133 //
134 BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
135 {
136 HWND hWnd;
137
138 hInst = hInstance; // Store instance handle in our global variable
139
140 hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
141 CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
142
143 if (!hWnd)
144 {
145 return FALSE;
146 }
147
148 hMainWnd = hWnd;
149
150 ShowWindow(hWnd, nCmdShow);
151 UpdateWindow(hWnd);
152
153 return TRUE;
154 }
155
156 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
157 {
158 int wmId, wmEvent;
159 PAINTSTRUCT ps;
160 HDC hdc;
161 RECT rc;
162 TCHAR WndText[MAX_PATH];
163 DWORD Index;
164 NONCLIENTMETRICS ncm;
165 HFONT hFont;
166
167 switch (message)
168 {
169 case WM_CREATE:
170
171 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);
172 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);
173
174 memset(&ncm, 0, sizeof(NONCLIENTMETRICS));
175 ncm.cbSize = sizeof(NONCLIENTMETRICS);
176 SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS), &ncm, 0);
177
178 hFont = CreateFontIndirect(&ncm.lfMessageFont);
179
180 SendMessage(hEditWnd, WM_SETFONT, (WPARAM)hFont, TRUE);
181 SendMessage(hDisplayWnd, WM_SETFONT, (WPARAM)hFont, TRUE);
182
183 break;
184 case WM_COMMAND:
185 wmId = LOWORD(wParam);
186 wmEvent = HIWORD(wParam);
187
188 if (lParam == (LPARAM)hEditWnd && wmEvent == EN_CHANGE)
189 {
190 GetWindowText(hEditWnd, WndText, MAX_PATH);
191
192 if (_tcslen(WndText) > 0)
193 {
194 SetWindowText(hEditWnd, TEXT(""));
195
196 if (!bConnected)
197 {
198 MessageBox(hWnd, TEXT("You are not currently connected!"), TEXT("Error"), MB_OK|MB_ICONSTOP);
199 break;
200 }
201
202 for (Index=0; Index<_tcslen(WndText); Index++)
203 {
204 if (dwThreadId != 0)
205 {
206 PostThreadMessage(dwThreadId, WM_CHAR, (WPARAM)WndText[Index], (LPARAM)0);
207 }
208 }
209 }
210 }
211
212 // Parse the menu selections:
213 switch (wmId)
214 {
215 case IDM_ABOUT:
216 DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
217 break;
218 case IDM_EXIT:
219 DestroyWindow(hWnd);
220 break;
221 case ID_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 EnableDisconnectMenuItem(TRUE);
232 EnableConnectMenuItem(FALSE);
233 _beginthread(Rs232Thread, 0, NULL);
234 }
235 }
236 break;
237 case ID_FILE_DISCONNECT:
238 if (bConnected)
239 {
240 bConnected = FALSE;
241 EnableDisconnectMenuItem(FALSE);
242 EnableConnectMenuItem(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 ID_FILE_STARTCAPTURE:
250 if (DialogBox(hInst, (LPCTSTR)IDD_CAPTURE, hWnd, (DLGPROC)CaptureDialogProc) == IDOK)
251 {
252 bCapturing = TRUE;
253 EnableStopCaptureMenuItem(TRUE);
254 EnableStartCaptureMenuItem(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 ID_FILE_STOPCAPTURE:
259 if (bCapturing)
260 {
261 bCapturing = FALSE;
262 EnableStopCaptureMenuItem(FALSE);
263 EnableStartCaptureMenuItem(TRUE);
264 CloseHandle(hCaptureFile);
265 hCaptureFile = NULL;
266 }
267 break;
268 case ID_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 EnableConnectMenuItem(BOOL Enable)
420 {
421 HMENU hMenuBar;
422 HMENU hFileMenu;
423
424 hMenuBar = GetMenu(hMainWnd);
425 hFileMenu = GetSubMenu(hMenuBar, 0);
426
427 if (Enable)
428 {
429 EnableMenuItem(hFileMenu, ID_FILE_CONNECT, MF_BYCOMMAND|MF_ENABLED);
430 }
431 else
432 {
433 EnableMenuItem(hFileMenu, ID_FILE_CONNECT, MF_BYCOMMAND|MF_GRAYED);
434 }
435 }
436
437 VOID EnableDisconnectMenuItem(BOOL Enable)
438 {
439 HMENU hMenuBar;
440 HMENU hFileMenu;
441
442 hMenuBar = GetMenu(hMainWnd);
443 hFileMenu = GetSubMenu(hMenuBar, 0);
444
445 if (Enable)
446 {
447 EnableMenuItem(hFileMenu, ID_FILE_DISCONNECT, MF_BYCOMMAND|MF_ENABLED);
448 }
449 else
450 {
451 EnableMenuItem(hFileMenu, ID_FILE_DISCONNECT, MF_BYCOMMAND|MF_GRAYED);
452 }
453 }
454
455 VOID EnableStartCaptureMenuItem(BOOL Enable)
456 {
457 HMENU hMenuBar;
458 HMENU hFileMenu;
459
460 hMenuBar = GetMenu(hMainWnd);
461 hFileMenu = GetSubMenu(hMenuBar, 0);
462
463 if (Enable)
464 {
465 EnableMenuItem(hFileMenu, ID_FILE_STARTCAPTURE, MF_BYCOMMAND|MF_ENABLED);
466 }
467 else
468 {
469 EnableMenuItem(hFileMenu, ID_FILE_STARTCAPTURE, MF_BYCOMMAND|MF_GRAYED);
470 }
471 }
472
473 VOID EnableStopCaptureMenuItem(BOOL Enable)
474 {
475 HMENU hMenuBar;
476 HMENU hFileMenu;
477
478 hMenuBar = GetMenu(hMainWnd);
479 hFileMenu = GetSubMenu(hMenuBar, 0);
480
481 if (Enable)
482 {
483 EnableMenuItem(hFileMenu, ID_FILE_STOPCAPTURE, MF_BYCOMMAND|MF_ENABLED);
484 }
485 else
486 {
487 EnableMenuItem(hFileMenu, ID_FILE_STOPCAPTURE, MF_BYCOMMAND|MF_GRAYED);
488 }
489 }
490
491 VOID CheckLocalEchoMenuItem(BOOL Checked)
492 {
493 HMENU hMenuBar;
494 HMENU hFileMenu;
495
496 hMenuBar = GetMenu(hMainWnd);
497 hFileMenu = GetSubMenu(hMenuBar, 0);
498
499 if (Checked)
500 {
501 CheckMenuItem(hFileMenu, ID_FILE_LOCALECHO, MF_BYCOMMAND|MF_CHECKED);
502 }
503 else
504 {
505 CheckMenuItem(hFileMenu, ID_FILE_LOCALECHO, MF_BYCOMMAND|MF_UNCHECKED);
506 }
507 }
508
509 VOID Rs232Thread(VOID* Parameter)
510 {
511 BYTE Byte;
512 TCHAR String[MAX_PATH];
513 MSG msg;
514 DWORD dwNumberOfBytesWritten;
515
516 UNREFERENCED_PARAMETER(Parameter);
517
518 dwThreadId = GetCurrentThreadId();
519
520 if (!Rs232OpenPortWin32(strComPort))
521 {
522 MessageBox(hMainWnd, TEXT("Error opening port!"), TEXT("Error"), MB_OK|MB_ICONSTOP);
523 bConnected = FALSE;
524 return;
525 }
526
527 _stprintf(String, TEXT("%s,n,8,1"), strBaudRate);
528 if (!Rs232ConfigurePortWin32(String))
529 {
530 MessageBox(hMainWnd, TEXT("Error configuring port!"), TEXT("Error"), MB_OK|MB_ICONSTOP);
531 bConnected = FALSE;
532 return;
533 }
534
535 while (bConnected)
536 {
537 if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
538 {
539 if (msg.message == WM_CHAR)
540 {
541 Rs232WriteByteWin32((BYTE)msg.wParam);
542
543 if (bLocalEcho && msg.wParam != (WPARAM)TEXT('\r'))
544 {
545 PostMessage(hDisplayWnd, WM_CHAR, (WPARAM)msg.wParam, (LPARAM)0);
546
547 if (hCaptureFile)
548 {
549 WriteFile(hCaptureFile, &msg.wParam, sizeof(TCHAR), &dwNumberOfBytesWritten, NULL);
550 }
551 }
552 }
553 }
554
555 if (Rs232ReadByteWin32(&Byte))
556 {
557 _stprintf(String, TEXT("%c"), Byte);
558
559 PostMessage(hDisplayWnd, WM_CHAR, (WPARAM)String[0], (LPARAM)0);
560
561 if (hCaptureFile)
562 {
563 WriteFile(hCaptureFile, &String[0], sizeof(TCHAR), &dwNumberOfBytesWritten, NULL);
564 }
565 }
566 }
567
568 dwThreadId = 0;
569 Rs232ClosePortWin32();
570 }