/**************************************************************************** * * * FUNCTION : CERClosePort(void) * * * * PURPOSE : Close the communications port or socket. * * * * RETURNS : FALSE = success, TRUE = error * * * ****************************************************************************/ BOOL CERClosePort(void) { int rc; switch (iCommType) { case VREMOTE_MEDIA_COM1: case VREMOTE_MEDIA_COM2: case VREMOTE_MEDIA_USB: rc = CloseHandle(hPort); if (rc == 0) // problem closing the port; must be in use return TRUE; break; case VREMOTE_MEDIA_IRDA: case VREMOTE_MEDIA_LAN: case VREMOTE_MEDIA_INTERNET: case VREMOTE_MEDIA_ACTIVESYNC: // shutdown(CommSocket, 0); closesocket(CommSocket); if (BroadcastSocket != -1) { // shutdown(BroadcastSocket, 0); closesocket(BroadcastSocket); } break; } hPort = 0; CommSocket = -1; BroadcastSocket = -1; return FALSE; } /* CERClosePort() */ /**************************************************************************** * * * FUNCTION : EMUCenterDialog(HWND) * * * * PURPOSE : Centers a child window within its parent. * * * * PARAMETERS : hdlg = Window handle of the dialog box to center * * * * RETURNS : NOTHING * * * ****************************************************************************/ void EMUCenterDialog(HWND hdlg) { RECT rectParent; RECT rectDialog; HWND hwndParent; int x,y, cx, cy; int cxScreen, cyScreen; /*---- Get Parent Window Rect ----*/ hwndParent = GetParent(hdlg); if (hwndParent == HWND_DESKTOP) /* Desktop window won't return valid rect */ { rectParent.left = rectParent.top = 0; rectParent.right = GetSystemMetrics(SM_CXSCREEN); rectParent.bottom = GetSystemMetrics(SM_CYSCREEN); } else GetWindowRect(hwndParent, &rectParent); /*----- Get Dialog Window Rect ----*/ GetWindowRect(hdlg, &rectDialog); /*----- Center Dialog ----*/ cx = rectDialog.right - rectDialog.left; cy = rectDialog.bottom - rectDialog.top; if (rectParent.right - rectParent.left > cx && rectParent.bottom-rectParent.top > cy) { /*----Center the dialog box within the parents client area----*/ x = (rectParent.right - rectParent.left - cx)/2 + rectParent.left; y = (rectParent.bottom - rectParent.top - cy)/2 + rectParent.top; } else { /*----Center the dialog box within the screen----*/ cxScreen = GetSystemMetrics(SM_CXSCREEN); cyScreen = GetSystemMetrics(SM_CYSCREEN); x = (cxScreen - cx)/2; y = (cyScreen - cy)/2; } /*----- Reposition Dialog Window ----*/ MoveWindow(hdlg,x,y,cx,cy,FALSE); } /* EMUCenterDialog() */ /**************************************************************************** * * * FUNCTION : CERWritePrefs() * * * * PURPOSE : Write the user preferences to the registry. * * * ****************************************************************************/ void CERWritePrefs(void) { HKEY hkey; int x, rc; rc = RegCreateKeyEx(HKEY_LOCAL_MACHINE, TEXT("SOFTWARE\\BitBank\\VirtualCE"), 0,TEXT(""),REG_OPTION_NON_VOLATILE,KEY_ALL_ACCESS,NULL,&hkey,(unsigned long *)&x); RegSetValueEx(hkey, TEXT("AVI"),0,REG_DWORD,(unsigned char *)&bAVI,sizeof(int)); RegSetValueEx(hkey, TEXT("CommType"),0,REG_DWORD,(unsigned char *)&iCommType,sizeof(int)); RegSetValueEx(hkey, TEXT("IPAddress"),0,REG_DWORD,(unsigned char *)&ulIPAddress,sizeof(int)); RegSetValueEx(hkey, TEXT("Refresh"),0,REG_DWORD,(unsigned char *)&iRefresh,sizeof(int)); RegSetValueEx(hkey, TEXT("Scale"),0,REG_DWORD,(unsigned char *)&iScale,sizeof(int)); RegSetValueEx(hkey, TEXT("Skin"),0,REG_DWORD,(unsigned char *)&bSkin,sizeof(int)); RegSetValueEx(hkey, TEXT("Capture"),0,REG_DWORD,(unsigned char *)&iCaptureFile,sizeof(int)); RegSetValueEx(hkey, TEXT("ColorMode"),0,REG_DWORD,(unsigned char *)&bLowColor,sizeof(int)); RegCloseKey(hkey); } /* CERWritePrefs() */ /**************************************************************************** FUNCTION: VCEFreeSkin(void) PURPOSE: Free the skin resources if loaded ****************************************************************************/ void VCEFreeSkin(void) { if (bSkin) { DeleteObject(hbmtop); DeleteObject(hbmleft); DeleteObject(hbmright); DeleteObject(hbmbottom); iSkinX = iSkinY = 0; iNumHotspots = 0; bSkin = FALSE; } } /* VCEFreeSkin() */ /**************************************************************************** * * * FUNCTION : SubThread(void) * * * * PURPOSE : Secondary thread used for doing background communication. * * * ****************************************************************************/ DWORD WINAPI SubThread(void) { /* This thread will continuously attempt to open the communication ports and */ /* communicate with the CE server application. */ while (!bThreadExit) { if (bConnected == FALSE) // If not connected to CE remote server { switch (iCommType) { case VREMOTE_MEDIA_COM1: case VREMOTE_MEDIA_COM2: case VREMOTE_MEDIA_USB: if (hPort == 0) // If the port was previously busy, try to open it again { CERFindPorts(); CEROpenPort(FALSE); } if (hPort != 0) { InvalidateRect(hwndClient, NULL, FALSE); // show new status bConnected = CERInitialize(); // try to connect } break; case VREMOTE_MEDIA_IRDA: case VREMOTE_MEDIA_LAN: case VREMOTE_MEDIA_INTERNET: case VREMOTE_MEDIA_ACTIVESYNC: bConnected = CERConnect(); break; } } Sleep(500); // try to connect 2 times per second or sit idle while connected } return 0; } /* SubThread() */ /**************************************************************************** * * * FUNCTION : CERRead(char *, int) * * * * PURPOSE : Read a block of data from the comm port. Determines if * * data is coming from a socket or handle to comm port. * * * ****************************************************************************/ int CERRead(unsigned char *pBuf, int iReadLen) { DWORD dwLength, dwError, dwTempLen; COMSTAT cs; int i; if (iCommType == VREMOTE_MEDIA_IRDA || iCommType == VREMOTE_MEDIA_INTERNET || iCommType == VREMOTE_MEDIA_LAN || iCommType == VREMOTE_MEDIA_ACTIVESYNC) { // use socket communications dwTempLen = dwLength = 0; while (dwTempLen < (unsigned)iReadLen) { dwTempLen = recv(CommSocket, pBuf, iReadLen, 0); if (dwTempLen == 0) { bConnected = FALSE; // the socket got disconnected VCEFreeSkin(); if (iCommType == VREMOTE_MEDIA_IRDA || iCommType == VREMOTE_MEDIA_LAN || iCommType == VREMOTE_MEDIA_INTERNET || iCommType == VREMOTE_MEDIA_ACTIVESYNC) { CERClosePort(); // create new sockets to try again CEROpenPort(FALSE); } // resize the main window to the default size SetWindowPos(hwndClient, HWND_TOP, 0,0,DEFAULT_WIDTH,DEFAULT_HEIGHT, SWP_NOMOVE | SWP_NOZORDER); // resize the toolbar if visible if (hwndToolbar) SetWindowPos(hwndToolbar, HWND_TOP, 0, 0, DEFAULT_WIDTH, cyToolbar, SWP_SHOWWINDOW); InvalidateRect(hwndClient, NULL, TRUE); // show new status return 0; } if (dwTempLen == SOCKET_ERROR) { i = WSAGetLastError(); if (i == WSAEWOULDBLOCK) { dwTempLen = 0; continue; } else return 0; } if (dwTempLen < (unsigned)iReadLen) { iReadLen -= dwTempLen; pBuf += dwTempLen; dwLength += dwTempLen; dwTempLen = 0; Sleep(8); } } dwLength += dwTempLen; } else // use handle to communication port { if (hPort == NULL) // the communication port is not open { Sleep(10); // don't eat all the CPU return 0; // don't try to read invalid port } dwLength = 0; if (!ReadFile(hPort, pBuf, iReadLen, &dwLength, NULL)) { // clear the error ClearCommError(hPort, &dwError, &cs); } } return dwLength; } /* CERRead() */