win95-initial-port-20010430
[openafs.git] / src / WINNT / client_osi / main.c
1 /* 
2  * Copyright (C) 1998, 1989 Transarc Corporation - All rights reserved
3  *
4  * (C) COPYRIGHT IBM CORPORATION 1987, 1988
5  * LICENSED MATERIALS - PROPERTY OF IBM
6  *
7  *
8  */
9
10 /* Copyright (C) 1994 Cazamar Systems, Inc. */
11
12 /****************************************************************************
13
14     PROGRAM: Main.c
15
16     PURPOSE: system test code for osi package.
17
18 ****************************************************************************/
19
20
21 #include <afs/param.h>
22 #include <afs/stds.h>
23
24 #include "windows.h"
25 #include <string.h>
26 #include "main.h"
27 #include "basic.h"
28 #include "trylock.h"
29 #include "perf.h"
30 #include "osi.h"
31 #include <assert.h>
32
33 /* global state for test program */
34 HANDLE hInst;
35
36 HWND globalWnd;
37
38 /* screen image */
39 char main_screenText[HW_NLINES][80];
40
41 /* screen display image size */
42 RECT screenRect;
43
44 /* height of a text line */
45 int lineHeight;
46
47 /****************************************************************************
48
49     FUNCTION: WinMain(HANDLE, HANDLE, LPSTR, int)
50
51     PURPOSE: calls initialization function, processes message loop
52
53 ****************************************************************************/
54
55 int APIENTRY WinMain(
56     HANDLE hInstance,
57     HANDLE hPrevInstance,
58     LPSTR lpCmdLine,
59     int nCmdShow
60     )
61 {
62     MSG msg;
63
64
65     if (!InitApplication(hInstance))
66         return (FALSE);
67
68     if (!InitInstance(hInstance, nCmdShow))
69         return (FALSE);
70
71     while (GetMessage(&msg, NULL, 0, 0)) {
72         TranslateMessage(&msg);
73         DispatchMessage(&msg);
74     }
75     return (msg.wParam);
76 }
77
78
79 /****************************************************************************
80
81     FUNCTION: InitApplication(HANDLE)
82
83     PURPOSE: Initializes window data and registers window class
84
85 ****************************************************************************/
86
87 BOOL InitApplication(HANDLE hInstance)
88 {
89     WNDCLASS  wc;
90
91     /* create window class */
92     wc.style = CS_DBLCLKS;          /* double-click messages */
93     wc.lpfnWndProc = (WNDPROC) MainWndProc;
94     wc.cbClsExtra = 0;
95     wc.cbWndExtra = 0;
96     wc.hInstance = hInstance;
97     wc.hIcon = LoadIcon(NULL, IDI_APPLICATION);
98     wc.hCursor = LoadCursor(NULL, IDC_ARROW);
99     wc.hbrBackground = GetStockObject(WHITE_BRUSH); 
100     wc.lpszMenuName =  "InputMenu";
101     wc.lpszClassName = "InputWClass";
102
103     return (RegisterClass(&wc));
104 }
105
106
107 /****************************************************************************
108
109     FUNCTION:  InitInstance(HANDLE, int)
110
111     PURPOSE:  Saves instance handle and creates main window
112
113 ****************************************************************************/
114
115 BOOL InitInstance(
116     HANDLE          hInstance,
117     INT             nCmdShow)
118 {
119     HWND            hWnd;
120     HDC             hDC;
121     TEXTMETRIC      textmetric;
122     RECT            rect;
123     UUID            debugID;
124     long code;
125
126     hInst = hInstance;
127
128     /* create window itself */
129     hWnd = CreateWindow(
130         "InputWClass",
131         "OSI Lock Test Application",
132         WS_OVERLAPPEDWINDOW | WS_HSCROLL | WS_VSCROLL,  /* horz & vert scroll bars */
133         CW_USEDEFAULT,
134         CW_USEDEFAULT,
135         CW_USEDEFAULT,
136         CW_USEDEFAULT,
137         NULL,
138         NULL,
139         hInstance,
140         NULL
141     );
142
143     if (!hWnd)
144         return (FALSE);
145
146     globalWnd = hWnd;
147
148     hDC = GetDC(hWnd);
149     GetTextMetrics(hDC, &textmetric);
150     lineHeight = textmetric.tmExternalLeading + textmetric.tmHeight;
151
152     rect.left   = GetDeviceCaps(hDC, LOGPIXELSX) / 4;   /* 1/4 inch */
153     rect.right  = GetDeviceCaps(hDC, HORZRES);
154     rect.top    = GetDeviceCaps(hDC, LOGPIXELSY) / 4;   /* 1/4 inch */
155     ReleaseDC(hWnd, hDC);
156     rect.bottom = rect.top + HW_NLINES * lineHeight;
157     screenRect = rect;
158
159     /* init RPC system */
160     osi_LongToUID(1, &debugID);
161     code = osi_InitDebug(&debugID);
162
163     if (code == 0) wsprintf(main_screenText[0], "Initialized successfully.");
164     else wsprintf(main_screenText[0], "Failed to init debug system, code %ld", code);
165     
166     ShowWindow(hWnd, nCmdShow);
167     UpdateWindow(hWnd);
168     return (TRUE);
169
170 }
171
172 /****************************************************************************
173
174     FUNCTION: MainWndProc(HWND, unsigned, WORD, LONG)
175
176     PURPOSE:  Processes messages
177
178 ****************************************************************************/
179
180 LONG APIENTRY MainWndProc(
181         HWND hWnd,
182         UINT message,
183         UINT wParam,
184         LONG lParam)
185 {
186     FARPROC lpProcAbout;
187
188     HDC hDC;                         /* display-context variable     */
189     HMENU hMenu;                     /* menu */
190     PAINTSTRUCT ps;                  /* paint structure              */
191     RECT rect;
192     long i;
193     long code;
194
195     switch (message) {
196         case WM_COMMAND:
197             if (LOWORD(wParam) == IDM_ABOUT) {
198                 lpProcAbout = (FARPROC) About;
199
200                 DialogBox(hInst,
201                     "AboutBox",
202                     hWnd,
203                     lpProcAbout);
204
205                 break;
206             }
207             if (LOWORD(wParam) == IDM_DEBUGON) {
208                 osi_LockTypeSetDefault("stat");
209                 hMenu = GetMenu(globalWnd);
210                 CheckMenuItem(hMenu, IDM_DEBUGON, MF_CHECKED);
211                 CheckMenuItem(hMenu, IDM_DEBUGOFF, MF_UNCHECKED);
212             }
213             if (LOWORD(wParam) == IDM_DEBUGOFF) {
214                 osi_LockTypeSetDefault((char *) 0);
215                 hMenu = GetMenu(globalWnd);
216                 CheckMenuItem(hMenu, IDM_DEBUGON, MF_UNCHECKED);
217                 CheckMenuItem(hMenu, IDM_DEBUGOFF, MF_CHECKED);
218             }
219             if (LOWORD(wParam) == IDM_BASICTEST) {
220                 main_ClearDisplay();
221                 wsprintf(main_screenText[0], "Starting basic test run...");
222                 code = main_BasicTest(hWnd);
223                 wsprintf(main_screenText[0], "Basic test returned code %d", code);
224                 InvalidateRect(hWnd, &screenRect, TRUE);
225             }
226             else if (LOWORD(wParam) == IDM_PERFTEST) {
227                 main_ClearDisplay();
228                 wsprintf(main_screenText[0], "Starting performance test run...");
229                 code = main_PerfTest(hWnd);
230                 wsprintf(main_screenText[0], "Performance test returned code %d", code);
231                 InvalidateRect(hWnd, &screenRect, TRUE);
232             }
233             else if (LOWORD(wParam) == IDM_TRYLOCKTEST) {
234                 main_ClearDisplay();
235                 wsprintf(main_screenText[0], "Starting TryLock test run...");
236                 code = main_TryLockTest(hWnd);
237                 wsprintf(main_screenText[0], "TryLock test returned code %d", code);
238                 InvalidateRect(hWnd, &screenRect, TRUE);
239             }
240             else
241                 return (DefWindowProc(hWnd, message, wParam, lParam));
242             break;
243
244         case WM_CHAR:
245             wsprintf(main_screenText[0], "WM_CHAR: %c, %x, %x",
246                 wParam, LOWORD(lParam), HIWORD(lParam));
247             InvalidateRect(hWnd, &screenRect, TRUE);
248             break;
249
250         case WM_PAINT:
251             hDC = BeginPaint (hWnd, &ps);
252
253             if (IntersectRect(&rect, &screenRect, &ps.rcPaint)) {
254                 for(i=0; i<HW_NLINES; i++) {
255                     TextOut(hDC, screenRect.left, screenRect.top + i*lineHeight,
256                         main_screenText[i], strlen(main_screenText[i]));
257                 }
258             }
259
260             EndPaint(hWnd, &ps);
261             break;
262
263         case WM_DESTROY:
264             PostQuitMessage(0);
265             break;
266
267         default:
268             return (DefWindowProc(hWnd, message, wParam, lParam));
269     }
270     return (0);
271 }
272
273
274 /****************************************************************************
275
276     FUNCTION: About(HWND, unsigned, WORD, LONG)
277
278     PURPOSE:  Processes messages for "About" dialog box
279
280     MESSAGES:
281
282         WM_INITDIALOG - initialize dialog box
283         WM_COMMAND    - Input received
284
285 ****************************************************************************/
286
287 BOOL APIENTRY About(
288         HWND hDlg,
289         UINT message,
290         UINT wParam,
291         LONG lParam)
292 {
293     switch (message) {
294         case WM_INITDIALOG:
295             return (TRUE);
296
297         case WM_COMMAND:
298             if (LOWORD(wParam) == IDOK) {
299                 EndDialog(hDlg, TRUE);
300                 return (TRUE);
301             }
302             break;
303     }
304     return (FALSE);
305         UNREFERENCED_PARAMETER(lParam);
306 }
307
308 void main_ClearDisplay(void)
309 {
310         int i;
311         for(i=0; i<HW_NLINES; i++) {
312                 /* make the line an empty line */
313                 main_screenText[i][0] = 0;
314         }
315 }
316
317 void main_ForceDisplay(HANDLE hWnd)
318 {
319         HDC hDC;
320         HGDIOBJ hBrush;
321         int i;
322
323         hDC = GetDC(hWnd);
324         hBrush = GetStockObject(WHITE_BRUSH);
325         FillRect(hDC, &screenRect, hBrush);
326         DeleteObject(hBrush);
327         for(i=0; i<HW_NLINES; i++) {
328             TextOut(hDC, screenRect.left, screenRect.top + i*lineHeight,
329                 main_screenText[i], strlen(main_screenText[i]));
330         }
331         ReleaseDC(hWnd, hDC);
332 }
333