ReactOS Fundraising Campaign 2012
 
€ 4,410 / € 30,000

Information | Donate

Home | Info | Community | Development | myReactOS | Contact Us

  1. Home
  2. Community
  3. Development
  4. myReactOS
  5. Fundraiser 2012

  1. Main Page
  2. Alphabetical List
  3. Data Structures
  4. Directories
  5. File List
  6. Data Fields
  7. Globals
  8. Related Pages

ReactOS Development > Doxygen

fdebug.c
Go to the documentation of this file.
00001 // fdebug.cpp : Defines the entry point for the application.
00002 //
00003 
00004 #include <windows.h>
00005 #include <commdlg.h>
00006 #include <process.h>
00007 #include <stdio.h>
00008 #include <tchar.h>
00009 
00010 #include "resource.h"
00011 #include "rs232.h"
00012 
00013 #define MAX_LOADSTRING 100
00014 
00015 // Global Variables:
00016 HINSTANCE   hInst;                                      // current instance
00017 TCHAR       szTitle[MAX_LOADSTRING];                    // The title bar text
00018 TCHAR       szWindowClass[MAX_LOADSTRING];              // The title bar text
00019 HWND        hMainWnd;                                   // The main window handle
00020 HWND        hDisplayWnd;                                // The window to display the incoming data
00021 HWND        hEditWnd;                                   // The edit window to get input from the user
00022 TCHAR       strComPort[MAX_PATH] = TEXT("COM1");        // The COM port to use
00023 TCHAR       strBaudRate[MAX_PATH] = TEXT("115200");     // The baud rate to use
00024 TCHAR       strCaptureFileName[MAX_PATH] = TEXT("");    // The file name to capture to
00025 BOOL        bConnected = FALSE;                         // Tells us if we are currently connected
00026 BOOL        bCapturing = FALSE;                         // Tells us if we are currently capturing data
00027 BOOL        bLocalEcho = FALSE;                         // Tells us if local echo is currently enabled
00028 HANDLE      hCaptureFile;                               // Handle to the capture file
00029 DWORD       dwThreadId = 0;                             // Thread id of RS232 communication thread
00030 
00031 // Foward declarations of functions included in this code module:
00032 ATOM                MyRegisterClass(HINSTANCE hInstance);
00033 BOOL                InitInstance(HINSTANCE, int);
00034 LRESULT CALLBACK    WndProc(HWND, UINT, WPARAM, LPARAM);
00035 LRESULT CALLBACK    About(HWND, UINT, WPARAM, LPARAM);
00036 LRESULT CALLBACK    ConnectionDialogProc(HWND, UINT, WPARAM, LPARAM);
00037 LRESULT CALLBACK    CaptureDialogProc(HWND, UINT, WPARAM, LPARAM);
00038 VOID                EnableFileMenuItemByID(UINT Id, BOOL Enable);
00039 VOID                CheckLocalEchoMenuItem(BOOL Checked);
00040 VOID                Rs232Thread(VOID* Parameter);
00041 
00042 int APIENTRY _tWinMain(HINSTANCE hInstance,
00043                        HINSTANCE hPrevInstance,
00044                        LPTSTR     lpCmdLine,
00045                        int       nCmdShow)
00046 {
00047     // TODO: Place code here.
00048     MSG msg;
00049     HACCEL hAccelTable;
00050 
00051     UNREFERENCED_PARAMETER(lpCmdLine);
00052     UNREFERENCED_PARAMETER(hPrevInstance);
00053 
00054     // Initialize global strings
00055     LoadString(hInstance, IDS_APP_TITLE, szTitle, MAX_LOADSTRING);
00056     LoadString(hInstance, IDC_FDEBUG, szWindowClass, MAX_LOADSTRING);
00057     MyRegisterClass(hInstance);
00058 
00059     // Perform application initialization:
00060     if (!InitInstance (hInstance, nCmdShow))
00061     {
00062         return FALSE;
00063     }
00064 
00065     hAccelTable = LoadAccelerators(hInstance, (LPCTSTR)IDC_FDEBUG);
00066 
00067     // Main message loop:
00068     while (GetMessage(&msg, NULL, 0, 0))
00069     {
00070         if (!TranslateAccelerator(msg.hwnd, hAccelTable, &msg))
00071         {
00072             TranslateMessage(&msg);
00073             DispatchMessage(&msg);
00074         }
00075     }
00076 
00077     return (int)msg.wParam;
00078 }
00079 
00080 
00081 
00082 //
00083 //  FUNCTION: MyRegisterClass()
00084 //
00085 //  PURPOSE: Registers the window class.
00086 //
00087 //  COMMENTS:
00088 //
00089 //    This function and its usage is only necessary if you want this code
00090 //    to be compatible with Win32 systems prior to the 'RegisterClassEx'
00091 //    function that was added to Windows 95. It is important to call this function
00092 //    so that the application will get 'well formed' small icons associated
00093 //    with it.
00094 //
00095 ATOM MyRegisterClass(HINSTANCE hInstance)
00096 {
00097     WNDCLASSEX wcex;
00098 
00099     wcex.cbSize = sizeof(WNDCLASSEX);
00100 
00101     wcex.style          = CS_HREDRAW | CS_VREDRAW;
00102     wcex.lpfnWndProc    = (WNDPROC)WndProc;
00103     wcex.cbClsExtra     = 0;
00104     wcex.cbWndExtra     = 0;
00105     wcex.hInstance      = hInstance;
00106     wcex.hIcon          = LoadIcon(hInstance, MAKEINTRESOURCE(IDI_FDEBUG));
00107     wcex.hCursor        = LoadCursor(NULL, IDC_ARROW);
00108     wcex.hbrBackground  = NULL;//(HBRUSH)(COLOR_WINDOW+1);
00109     wcex.lpszMenuName   = MAKEINTRESOURCE(IDC_FDEBUG);
00110     wcex.lpszClassName  = szWindowClass;
00111     wcex.hIconSm        = (HICON)LoadImage(hInstance,
00112                                            MAKEINTRESOURCE(IDI_FDEBUG),
00113                                            IMAGE_ICON,
00114                                            16,
00115                                            16,
00116                                            LR_SHARED);
00117 
00118     return RegisterClassEx(&wcex);
00119 }
00120 
00121 //
00122 //   FUNCTION: InitInstance(HANDLE, int)
00123 //
00124 //   PURPOSE: Saves instance handle and creates main window
00125 //
00126 //   COMMENTS:
00127 //
00128 //        In this function, we save the instance handle in a global variable and
00129 //        create and display the main program window.
00130 //
00131 BOOL InitInstance(HINSTANCE hInstance, int nCmdShow)
00132 {
00133    HWND hWnd;
00134 
00135    hInst = hInstance; // Store instance handle in our global variable
00136 
00137    hWnd = CreateWindow(szWindowClass, szTitle, WS_OVERLAPPEDWINDOW,
00138       CW_USEDEFAULT, 0, CW_USEDEFAULT, 0, NULL, NULL, hInstance, NULL);
00139 
00140    if (!hWnd)
00141    {
00142       return FALSE;
00143    }
00144 
00145    hMainWnd = hWnd;
00146 
00147    ShowWindow(hWnd, nCmdShow);
00148    UpdateWindow(hWnd);
00149 
00150    return TRUE;
00151 }
00152 
00153 LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
00154 {
00155     int                 wmId, wmEvent;
00156     PAINTSTRUCT         ps;
00157     HDC                 hdc;
00158     RECT                rc;
00159     TCHAR               WndText[MAX_PATH];
00160     DWORD               Index;
00161     NONCLIENTMETRICS    ncm;
00162     HFONT               hFont;
00163 
00164     switch (message)
00165     {
00166     case WM_CREATE:
00167 
00168         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);
00169         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);
00170 
00171         memset(&ncm, 0, sizeof(NONCLIENTMETRICS));
00172         ncm.cbSize = sizeof(NONCLIENTMETRICS);
00173         SystemParametersInfo(SPI_GETNONCLIENTMETRICS, sizeof(NONCLIENTMETRICS), &ncm, 0);
00174 
00175         hFont = CreateFontIndirect(&ncm.lfMessageFont);
00176 
00177         SendMessage(hEditWnd, WM_SETFONT, (WPARAM)hFont, TRUE);
00178         SendMessage(hDisplayWnd, WM_SETFONT, (WPARAM)hFont, TRUE);
00179 
00180         break;
00181     case WM_COMMAND:
00182         wmId    = LOWORD(wParam);
00183         wmEvent = HIWORD(wParam);
00184 
00185         if (lParam == (LPARAM)hEditWnd && wmEvent == EN_CHANGE)
00186         {
00187             GetWindowText(hEditWnd, WndText, MAX_PATH);
00188 
00189             if (_tcslen(WndText) > 0)
00190             {
00191                 SetWindowText(hEditWnd, TEXT(""));
00192 
00193                 if (!bConnected)
00194                 {
00195                     MessageBox(hWnd, TEXT("You are not currently connected!"), TEXT("Error"), MB_OK|MB_ICONSTOP);
00196                     break;
00197                 }
00198 
00199                 for (Index=0; Index<_tcslen(WndText); Index++)
00200                 {
00201                     if (dwThreadId != 0)
00202                     {
00203                         PostThreadMessage(dwThreadId, WM_CHAR, (WPARAM)WndText[Index], (LPARAM)0);
00204                     }
00205                 }
00206             }
00207         }
00208 
00209         // Parse the menu selections:
00210         switch (wmId)
00211         {
00212         case IDM_ABOUT:
00213            DialogBox(hInst, (LPCTSTR)IDD_ABOUTBOX, hWnd, (DLGPROC)About);
00214            break;
00215         case IDM_EXIT:
00216            DestroyWindow(hWnd);
00217            break;
00218         case IDM_FILE_CLEARDISPLAY:
00219             SetWindowText(hDisplayWnd, TEXT(""));
00220             break;
00221         case IDM_FILE_CONNECT:
00222             if (bConnected)
00223             {
00224                 MessageBox(hWnd, TEXT("You are already connected!"), TEXT("Error"), MB_OK|MB_ICONSTOP);
00225             }
00226             else
00227             {
00228                 if (DialogBox(hInst, (LPCTSTR)IDD_CONNECTION, hWnd, (DLGPROC)ConnectionDialogProc) == IDOK)
00229                 {
00230                     bConnected = TRUE;
00231                     EnableFileMenuItemByID(IDM_FILE_DISCONNECT, TRUE);
00232                     EnableFileMenuItemByID(IDM_FILE_CONNECT, FALSE);
00233                     _beginthread(Rs232Thread, 0, NULL);
00234                 }
00235             }
00236             break;
00237         case IDM_FILE_DISCONNECT:
00238             if (bConnected)
00239             {
00240                 bConnected = FALSE;
00241                 EnableFileMenuItemByID(IDM_FILE_DISCONNECT, FALSE);
00242                 EnableFileMenuItemByID(IDM_FILE_CONNECT, TRUE);
00243             }
00244             else
00245             {
00246                 MessageBox(hWnd, TEXT("You are not currently connected!"), TEXT("Error"), MB_OK|MB_ICONSTOP);
00247             }
00248             break;
00249         case IDM_FILE_STARTCAPTURE:
00250             if (DialogBox(hInst, (LPCTSTR)IDD_CAPTURE, hWnd, (DLGPROC)CaptureDialogProc) == IDOK)
00251             {
00252                 bCapturing = TRUE;
00253                 EnableFileMenuItemByID(IDM_FILE_STOPCAPTURE, TRUE);
00254                 EnableFileMenuItemByID(IDM_FILE_STARTCAPTURE, FALSE);
00255                 hCaptureFile = CreateFile(strCaptureFileName, FILE_APPEND_DATA, FILE_SHARE_READ|FILE_SHARE_WRITE, NULL, OPEN_ALWAYS, FILE_ATTRIBUTE_NORMAL, NULL);
00256             }
00257             break;
00258         case IDM_FILE_STOPCAPTURE:
00259             if (bCapturing)
00260             {
00261                 bCapturing = FALSE;
00262                 EnableFileMenuItemByID(IDM_FILE_STOPCAPTURE, FALSE);
00263                 EnableFileMenuItemByID(IDM_FILE_STARTCAPTURE, TRUE);
00264                 CloseHandle(hCaptureFile);
00265                 hCaptureFile = NULL;
00266             }
00267             break;
00268         case IDM_FILE_LOCALECHO:
00269             if (bLocalEcho)
00270             {
00271                 bLocalEcho = FALSE;
00272                 CheckLocalEchoMenuItem(bLocalEcho);
00273             }
00274             else
00275             {
00276                 bLocalEcho = TRUE;
00277                 CheckLocalEchoMenuItem(bLocalEcho);
00278             }
00279             break;
00280         default:
00281            return DefWindowProc(hWnd, message, wParam, lParam);
00282         }
00283         break;
00284     case WM_PAINT:
00285         hdc = BeginPaint(hWnd, &ps);
00286         EndPaint(hWnd, &ps);
00287         break;
00288     case WM_SIZE:
00289 
00290         GetClientRect(hWnd, &rc);
00291 
00292         MoveWindow(hDisplayWnd, rc.left, rc.top, rc.right - rc.left, rc.bottom - rc.top - 20, TRUE);
00293         MoveWindow(hEditWnd, rc.left, rc.bottom - 20, rc.right - rc.left, 20, TRUE);
00294 
00295         break;
00296     case WM_DESTROY:
00297         PostQuitMessage(0);
00298         break;
00299     default:
00300         return DefWindowProc(hWnd, message, wParam, lParam);
00301    }
00302    return 0;
00303 }
00304 
00305 LRESULT CALLBACK About(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
00306 {
00307     HWND    hLicenseEditWnd;
00308     TCHAR   strLicense[0x1000];
00309 
00310     UNREFERENCED_PARAMETER(lParam);
00311 
00312     switch (message)
00313     {
00314     case WM_INITDIALOG:
00315 
00316         hLicenseEditWnd = GetDlgItem(hDlg, IDC_LICENSE_EDIT);
00317 
00318         LoadString(hInst, IDS_LICENSE, strLicense, 0x1000);
00319 
00320         SetWindowText(hLicenseEditWnd, strLicense);
00321 
00322         return TRUE;
00323 
00324     case WM_COMMAND:
00325         if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
00326         {
00327             EndDialog(hDlg, LOWORD(wParam));
00328             return TRUE;
00329         }
00330         break;
00331     }
00332     return FALSE;
00333 }
00334 
00335 LRESULT CALLBACK ConnectionDialogProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
00336 {
00337     UNREFERENCED_PARAMETER(lParam);
00338 
00339     switch (message)
00340     {
00341     case WM_INITDIALOG:
00342 
00343         SetWindowText(GetDlgItem(hDlg, IDC_COMPORT), strComPort);
00344         SetWindowText(GetDlgItem(hDlg, IDC_BAUTRATE), strBaudRate);
00345 
00346         return TRUE;
00347 
00348     case WM_COMMAND:
00349         if (LOWORD(wParam) == IDOK)
00350         {
00351             GetWindowText(GetDlgItem(hDlg, IDC_COMPORT), strComPort, MAX_PATH);
00352             GetWindowText(GetDlgItem(hDlg, IDC_BAUTRATE), strBaudRate, MAX_PATH);
00353         }
00354 
00355         if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
00356         {
00357             EndDialog(hDlg, LOWORD(wParam));
00358             return TRUE;
00359         }
00360         break;
00361     }
00362     return FALSE;
00363 }
00364 
00365 LRESULT CALLBACK CaptureDialogProc(HWND hDlg, UINT message, WPARAM wParam, LPARAM lParam)
00366 {
00367     OPENFILENAME    ofn;
00368 
00369     UNREFERENCED_PARAMETER(lParam);
00370 
00371     switch (message)
00372     {
00373     case WM_INITDIALOG:
00374 
00375         SetWindowText(GetDlgItem(hDlg, IDC_CAPTUREFILENAME), strCaptureFileName);
00376 
00377         return TRUE;
00378 
00379     case WM_COMMAND:
00380         if (LOWORD(wParam) == IDC_BROWSE)
00381         {
00382             memset(&ofn, 0, sizeof(OPENFILENAME));
00383             ofn.lStructSize = sizeof(OPENFILENAME);
00384             ofn.hwndOwner = hDlg;
00385             ofn.hInstance = hInst;
00386             ofn.lpstrFilter = NULL;
00387             ofn.lpstrCustomFilter = NULL;
00388             ofn.nMaxCustFilter = 0;
00389             ofn.nFilterIndex = 0;
00390             ofn.lpstrFile = strCaptureFileName;
00391             ofn.nMaxFile = MAX_PATH;
00392             ofn.lpstrFileTitle = NULL;
00393             ofn.nMaxFileTitle = 0;
00394             ofn.lpstrInitialDir = NULL;
00395             ofn.lpstrTitle = NULL;
00396             ofn.Flags = OFN_HIDEREADONLY|OFN_NOREADONLYRETURN;
00397 
00398             if (GetOpenFileName(&ofn))
00399             {
00400                 SetWindowText(GetDlgItem(hDlg, IDC_CAPTUREFILENAME), strCaptureFileName);
00401             }
00402         }
00403 
00404         if (LOWORD(wParam) == IDOK)
00405         {
00406             GetWindowText(GetDlgItem(hDlg, IDC_CAPTUREFILENAME), strCaptureFileName, MAX_PATH);
00407         }
00408 
00409         if (LOWORD(wParam) == IDOK || LOWORD(wParam) == IDCANCEL)
00410         {
00411             EndDialog(hDlg, LOWORD(wParam));
00412             return TRUE;
00413         }
00414         break;
00415     }
00416     return FALSE;
00417 }
00418 
00419 VOID EnableFileMenuItemByID(UINT Id, BOOL Enable)
00420 {
00421     HMENU   hMenuBar;
00422     HMENU   hFileMenu;
00423 
00424     hMenuBar = GetMenu(hMainWnd);
00425     hFileMenu = GetSubMenu(hMenuBar, 0);
00426     EnableMenuItem(hFileMenu, Id, MF_BYCOMMAND|(Enable ? MF_ENABLED : MF_GRAYED));
00427 }
00428 
00429 VOID CheckLocalEchoMenuItem(BOOL Checked)
00430 {
00431     HMENU   hMenuBar;
00432     HMENU   hFileMenu;
00433 
00434     hMenuBar = GetMenu(hMainWnd);
00435     hFileMenu = GetSubMenu(hMenuBar, 0);
00436     CheckMenuItem(hFileMenu, IDM_FILE_LOCALECHO, MF_BYCOMMAND|(Checked ? MF_CHECKED : MF_UNCHECKED));
00437 }
00438 
00439 VOID Rs232Thread(VOID* Parameter)
00440 {
00441     BYTE    Byte;
00442     TCHAR   String[MAX_PATH];
00443     MSG     msg;
00444     DWORD   dwNumberOfBytesWritten;
00445 
00446     UNREFERENCED_PARAMETER(Parameter);
00447 
00448     dwThreadId = GetCurrentThreadId();
00449 
00450     if (!Rs232OpenPortWin32(strComPort))
00451     {
00452         MessageBox(hMainWnd, TEXT("Error opening port!"), TEXT("Error"), MB_OK|MB_ICONSTOP);
00453         bConnected = FALSE;
00454         return;
00455     }
00456 
00457     _stprintf(String, TEXT("%s,n,8,1"), strBaudRate);
00458     if (!Rs232ConfigurePortWin32(String))
00459     {
00460         MessageBox(hMainWnd, TEXT("Error configuring port!"), TEXT("Error"), MB_OK|MB_ICONSTOP);
00461         bConnected = FALSE;
00462         return;
00463     }
00464 
00465     while (bConnected)
00466     {
00467         if (PeekMessage(&msg, NULL, 0, 0, PM_REMOVE))
00468         {
00469             if (msg.message == WM_CHAR)
00470             {
00471                 Rs232WriteByteWin32((BYTE)msg.wParam);
00472 
00473                 if (bLocalEcho && msg.wParam != (WPARAM)TEXT('\r'))
00474                 {
00475                     PostMessage(hDisplayWnd, WM_CHAR, (WPARAM)msg.wParam, (LPARAM)0);
00476 
00477                     if (hCaptureFile)
00478                     {
00479                         WriteFile(hCaptureFile, &msg.wParam, sizeof(TCHAR), &dwNumberOfBytesWritten, NULL);
00480                     }
00481                 }
00482             }
00483         }
00484 
00485         if (Rs232ReadByteWin32(&Byte))
00486         {
00487             _stprintf(String, TEXT("%c"), Byte);
00488 
00489             PostMessage(hDisplayWnd, WM_CHAR, (WPARAM)String[0], (LPARAM)0);
00490 
00491             if (hCaptureFile)
00492             {
00493                 WriteFile(hCaptureFile, &String[0], sizeof(TCHAR), &dwNumberOfBytesWritten, NULL);
00494             }
00495         }
00496     }
00497 
00498     dwThreadId = 0;
00499     Rs232ClosePortWin32();
00500 }

Generated on Sat May 26 2012 04:17:51 for ReactOS by doxygen 1.7.6.1

ReactOS is a registered trademark or a trademark of ReactOS Foundation in the United States and other countries.