📄 init.c
字号:
/*-----------------------------------------------------------------------------
This is a part of the Microsoft Source Code Samples.
Copyright (C) 1995 Microsoft Corporation.
All rights reserved.
This source code is only intended as a supplement to
Microsoft Development Tools and/or WinHelp documentation.
See these sources for detailed information regarding the
Microsoft samples programs.
MODULE: Init.c
PURPOSE: Intializes global data and comm port connects.
Closes comm ports and cleans up global data.
FUNCTIONS:
GlobalInitialize - Init global variables and system objects
GlobalCleanup - cleanup global variables and system objects
ClearTTYContents - Clears the tty buffer
InitNewFont - Creates a new font for the TTY child window
CreateTTYInfo - Creates the dynamic tty info structure controlling
behavior of tty
DestroyTTYInfo - deallocates tty info structure
StartThreads - Starts worker threads when a port is opened
SetupCommPort - Opens the port for the first time
WaitForThreads - Sets the thread exit event and wait for worker
threads to exit
BreakDownCommPort - Closes a connection to the comm port
DisconnectOK - Asks user if it is ok to disconnect
-----------------------------------------------------------------------------*/
#include <windows.h>
#include <commctrl.h>
#include "mttty.h"
/*
Prototypes for functions called only within this file
*/
void StartThreads( void );
DWORD WaitForThreads( DWORD );
/*
TimeoutsDefault
We need ReadIntervalTimeout here to cause the read operations
that we do to actually timeout and become overlapped.
Specifying 1 here causes ReadFile to return very quickly
so that our reader thread will continue execution.
*/
COMMTIMEOUTS gTimeoutsDefault = { 0x01, 0, 0, 0, 0 };
/*-----------------------------------------------------------------------------
FUNCTION: GlobalInitialize
PURPOSE: Intializes global variables before any windows are created
COMMENTS: Partner to GlobalCleanup
HISTORY: Date: Author: Comment:
10/27/95 AllenD Wrote it
-----------------------------------------------------------------------------*/
void GlobalInitialize()
{
int cyMenuHeight, cyCaptionHeight, cyFrameHeight;
//
// critical sections in status reporting & node management
//
InitializeCriticalSection(&gStatusCritical);
InitializeCriticalSection(&gcsWriterHeap);
InitializeCriticalSection(&gcsDataHeap);
//
// status message event
//
ghStatusMessageEvent = CreateEvent(NULL, FALSE, FALSE, NULL);
if (ghStatusMessageEvent == NULL)
ErrorReporter("CreateEvent (Status message event)");
//
// thread exit event
//
ghThreadExitEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
if (ghThreadExitEvent == NULL)
ErrorReporter("CreateEvent (Thread exit event)");
//
// used in file transfer status bar
//
//InitCommonControls();
//
// font for status reporting control
//
ghFontStatus = CreateStatusEditFont();
//
// the following are used for sizing the tty window and dialog windows
//
gwBaseY = HIWORD(GetDialogBaseUnits());
cyMenuHeight = GetSystemMetrics(SM_CYMENU);
cyCaptionHeight = GetSystemMetrics(SM_CYCAPTION);
cyFrameHeight = GetSystemMetrics(SM_CYFRAME);
gcyMinimumWindowHeight = cyMenuHeight + \
4 * cyCaptionHeight + \
2 * cyFrameHeight +
(SETTINGSFACTOR + STATUSFACTOR) * gwBaseY ;
return ;
}
/*-----------------------------------------------------------------------------
FUNCTION: GlobalCleanup
PURPOSE: Cleans up any global variables
COMMENTS: Partner to GlobalInitialize
HISTORY: Date: Author: Comment:
10/27/95 AllenD Wrote it
-----------------------------------------------------------------------------*/
void GlobalCleanup()
{
DeleteCriticalSection(&gStatusCritical);
DeleteCriticalSection(&gcsWriterHeap);
DeleteCriticalSection(&gcsDataHeap);
DeleteObject(ghFontStatus);
CloseHandle(ghStatusMessageEvent);
CloseHandle(ghThreadExitEvent);
HeapDestroy(ghStatusMessageHeap);
return;
}
/*-----------------------------------------------------------------------------
FUNCTION: ClearTTYContents
PURPOSE: Clears the tty buffer
RETURN: always TRUE
HISTORY: Date: Author: Comment:
10/27/95 AllenD Wrote it
-----------------------------------------------------------------------------*/
BOOL ClearTTYContents()
{
FillMemory(SCREEN(TTYInfo), MAXCOLS*MAXROWS, ' ');
return TRUE;
}
/*-----------------------------------------------------------------------------
FUNCTION: InitNewFont(LOGFONT, COLORREF)
PURPOSE: Prepares a font for use in the TTY screen
PARAMETERS:
LogFont - New logical font for tty screen
rgbColor - New color for TTY painting
COMMENTS: Called when a new connection is made, or the TTY font
is changed by the user.
HISTORY: Date: Author: Comment:
10/27/95 AllenD Wrote it
-----------------------------------------------------------------------------*/
void InitNewFont(LOGFONT LogFont, COLORREF rgbColor)
{
TEXTMETRIC tm;
HDC hDC;
//
// if old one exists, then I should delete it
//
if (HTTYFONT(TTYInfo))
DeleteObject(HTTYFONT(TTYInfo));
LFTTYFONT(TTYInfo) = LogFont;
HTTYFONT(TTYInfo) = CreateFontIndirect(&(LFTTYFONT(TTYInfo)));
FGCOLOR(TTYInfo) = rgbColor;
hDC = GetDC( ghwndMain ) ;
SelectObject( hDC, HTTYFONT( TTYInfo ) ) ;
GetTextMetrics( hDC, &tm ) ;
ReleaseDC( ghwndMain, hDC ) ;
//
// character width and height
//
XCHAR( TTYInfo ) = tm.tmAveCharWidth ;
YCHAR( TTYInfo ) = tm.tmHeight + tm.tmExternalLeading ;
XOFFSET( TTYInfo ) = 0 ;
YOFFSET( TTYInfo ) = YCHAR(TTYInfo) * ROW(TTYInfo);
}
/*-----------------------------------------------------------------------------
FUNCTION: InitTTYInfo
PURPOSE: Initializes TTY structure
COMMENTS: This structure is a collection of TTY attributes
used by all parts of this program
HISTORY: Date: Author: Comment:
10/27/95 AllenD Wrote it
2/14/96 AllenD Removed npTTYInfo
-----------------------------------------------------------------------------*/
BOOL InitTTYInfo()
{
//
// initialize generial TTY info
//
COMDEV( TTYInfo ) = NULL ;
CONNECTED( TTYInfo ) = FALSE ;
LOCALECHO( TTYInfo ) = FALSE ;
CURSORSTATE( TTYInfo ) = CS_HIDE ;
PORT( TTYInfo ) = 1 ;
BAUDRATE( TTYInfo ) = CBR_9600 ;
BYTESIZE( TTYInfo ) = 8 ;
PARITY( TTYInfo ) = NOPARITY ;
STOPBITS( TTYInfo ) = ONESTOPBIT ;
AUTOWRAP( TTYInfo ) = TRUE;
NEWLINE( TTYInfo ) = FALSE;
XSIZE( TTYInfo ) = 0 ;
YSIZE( TTYInfo ) = 0 ;
XSCROLL( TTYInfo ) = 0 ;
YSCROLL( TTYInfo ) = 0 ;
COLUMN( TTYInfo ) = 0 ;
ROW( TTYInfo ) = MAXROWS - 1 ;
DISPLAYERRORS( TTYInfo ) = TRUE ;
//
// timeouts
//
TIMEOUTSNEW( TTYInfo ) = gTimeoutsDefault;
//
// read state and status events
//
gdwReceiveState = RECEIVE_TTY;
EVENTFLAGS( TTYInfo ) = EVENTFLAGS_DEFAULT;
FLAGCHAR( TTYInfo ) = FLAGCHAR_DEFAULT;
//
// Flow Control Settings
//
DTRCONTROL( TTYInfo ) = DTR_CONTROL_ENABLE;
RTSCONTROL( TTYInfo ) = RTS_CONTROL_ENABLE;
XONCHAR( TTYInfo ) = ASCII_XON;
XOFFCHAR( TTYInfo ) = ASCII_XOFF;
XONLIMIT( TTYInfo ) = 0;
XOFFLIMIT( TTYInfo ) = 0;
CTSOUTFLOW( TTYInfo ) = FALSE;
DSROUTFLOW( TTYInfo ) = FALSE;
DSRINFLOW( TTYInfo ) = FALSE;
XONXOFFOUTFLOW(TTYInfo) = FALSE;
XONXOFFINFLOW(TTYInfo) = FALSE;
TXAFTERXOFFSENT(TTYInfo) = FALSE;
NOREADING(TTYInfo) = FALSE;
NOWRITING(TTYInfo) = FALSE;
NOEVENTS(TTYInfo) = FALSE;
NOSTATUS(TTYInfo) = FALSE;
SHOWTIMEOUTS(TTYInfo) = FALSE;
//
// setup default font information
//
LFTTYFONT( TTYInfo ).lfHeight = 12 ;
LFTTYFONT( TTYInfo ).lfWidth = 0 ;
LFTTYFONT( TTYInfo ).lfEscapement = 0 ;
LFTTYFONT( TTYInfo ).lfOrientation = 0 ;
LFTTYFONT( TTYInfo ).lfWeight = 0 ;
LFTTYFONT( TTYInfo ).lfItalic = 0 ;
LFTTYFONT( TTYInfo ).lfUnderline = 0 ;
LFTTYFONT( TTYInfo ).lfStrikeOut = 0 ;
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -