-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.c
149 lines (131 loc) · 4.97 KB
/
main.c
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
/*------------------------------------------------------------------------------------------------------------------
-- SOURCE FILE: main.c - Contains WinMain, initializes the program.
--
-- PROGRAM: dte
--
-- Functions:
-- int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hprevInstance, LPSTR lspszCmdParam, int nCmdShow)
--
-- DATE: September 29th 2010
--
-- REVISIONS: (Date and Description)
--
-- DESIGNER: Tom Nightingale
--
-- PROGRAMMER: Tom Nightingale
--
-- NOTES: Contains win32 entry point, main function WinMain(). Initiates message loop and translate's and
-- dispatches received messages to Session.c WndProc().
-- The message loop uses PeekMessage to check if messages exist in the queue before performing any
-- handling. This technique allows the program to handle the polling of an open serial port only when there
-- are no messages to be handled. Ensuring a smooth running application.
----------------------------------------------------------------------------------------------------------------------*/
#include "main.h"
#include "session.h"
/*------------------------------------------------------------------------------------------------------------------
-- FUNCTION: WinMain
--
-- DATE: September 29th, 2010
--
-- REVISIONS: (Date and Description)
--
-- DESIGNER: Tom Nightingale
--
-- PROGRAMMER: Tom Nightingale
--
-- INTERFACE: WinMain(HINSTANCE hInst, HINSTANCE hprevInstance, LPSTR lspszCmdParam, int nCmdShow)
-- HINSTANCE hInst
-- HINSTANCE hprevInstance
-- LPSTR lspszCmdParam
-- int nCmdShow
--
-- RETURNS: WINAPI.
--
-- NOTES:
-- The message loop is handled with PeekMessage() as opposed to GetMessage() to allow non-interferring
-- polling on the COMM port when there are no messages in the queue.
--
----------------------------------------------------------------------------------------------------------------------*/
int WINAPI WinMain(HINSTANCE hInst, HINSTANCE hprevInstance, LPSTR lspszCmdParam, int nCmdShow) {
HWND hwnd;
MSG Msg;
WNDCLASSEX Wcl;
PWDATA pWData;
int i;
TCHAR Name[] = TEXT("Dumb Terminial Emulator");
Wcl.cbSize = sizeof (WNDCLASSEX);
Wcl.style = 0;
Wcl.hIcon = LoadIcon(NULL, IDI_APPLICATION); // large icon
Wcl.hIconSm = NULL; // use small version of large icon
Wcl.hCursor = LoadCursor(NULL, IDC_ARROW); // cursor style
Wcl.lpfnWndProc = WndProc;
Wcl.hInstance = hInst;
Wcl.hbrBackground = (HBRUSH) GetStockObject(WHITE_BRUSH); //white background
Wcl.lpszClassName = Name;
Wcl.lpszMenuName = TEXT("DTEMENU"); // The menu Class
Wcl.cbClsExtra = 0; // no extra memory needed
Wcl.cbWndExtra = sizeof(PWDATA);
if (!RegisterClassEx(&Wcl)) {
return 0;
}
hwnd = CreateWindow (
Name,
Name,
WS_OVERLAPPEDWINDOW,
10,
10,
600,
400,
NULL,
NULL,
hInst,
NULL
);
// Create window storage struct.
pWData = (PWDATA) malloc(sizeof(PWDATA));
// Set default window storage values.
pWData->state = COMMAND; // Program state.
pWData->hCom = INVALID_HANDLE_VALUE; // Comm port handle.
// Prepare output storage struct.
pWData->pOutput = (POUTPUT) malloc(sizeof(OUTPUT));
pWData->pOutput->size = OUTPUTBUFFSIZE;
pWData->pOutput->pos = 0;
pWData->pOutput->out = (TCHAR*) malloc(pWData->pOutput->size * sizeof(TCHAR));
// Initially white-out the output buffer.
for (i = 0; i < pWData->pOutput->size; i++) {
pWData->pOutput->out[i] = ' ';
}
// Set up the cursor struct with default values.
pWData->cursor.xCaret = 0;
pWData->cursor.yCaret = 0;
pWData->cursor.cxBuffer = 0;
pWData->cursor.cyBuffer = 0;
pWData->cursor.cxChar = 0;
pWData->cursor.cyChar = 0;
// Set window struct to the window.
SetWindowLongPtr(hwnd, GWLP_USERDATA, (LONG_PTR) pWData);
ShowWindow(hwnd, nCmdShow);
UpdateWindow(hwnd);
// Message handling uses PeekMessage() instead of GetMessage.
// Essentially this will handle all messages when they arrive
// on the queue. But whenever the queue is empty, poll the
// serial port.
// This provides us with a very clean method of constantly polling the
// open serial port with out jamming the program.
while (TRUE) {
if (PeekMessage(&Msg, NULL, 0, 0, PM_REMOVE)) {
if (Msg.message == WM_QUIT) {
return Msg.wParam;
}
TranslateMessage(&Msg);
DispatchMessage(&Msg);
}
else {
if (pWData->state == CONNECT) {
// If no messages, poll the open serial port.
pollPort(hwnd, pWData);
}
}
}
return Msg.wParam;
}