d5d31d193a3932bb20d9629b447d353012bc38b7
[openafs.git] / src / WINNT / afsd / afsd.c
1 /*
2  * Copyright 2000, International Business Machines Corporation and others.
3  * All Rights Reserved.
4  * 
5  * This software has been released under the terms of the IBM Public
6  * License.  For details, see the LICENSE file in the top-level source
7  * directory or online at http://www.openafs.org/dl/license10.html
8  */
9
10 #include <afs/param.h>
11 #include <afs/stds.h>
12
13 #include <windows.h>
14 #include <string.h>
15 #include <nb30.h>
16
17 #include <osi.h>
18 #include "afsd.h"
19 #include "afsd_init.h"
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <winsock2.h>
23
24
25 HANDLE main_inst;
26 HWND main_wnd;
27 char main_statusText[100];
28 RECT main_rect;
29 osi_log_t *afsd_logp;
30
31 extern int traceOnPanic;
32
33 extern void afsd_DbgBreakAllocInit();
34 extern void afsd_DbgBreakAdd(DWORD requestNumber);
35
36 /*
37  * Notifier function for use by osi_panic
38  */
39 void afsd_notifier(char *msgp, char *filep, long line)
40 {
41         char tbuffer[100];
42         if (filep)
43                 sprintf(tbuffer, "Error at file %s, line %d", filep, line);
44         else
45                 strcpy(tbuffer, "Error at unknown location");
46
47         if (!msgp)
48                 msgp = "Assertion failure";
49
50         MessageBox(NULL, tbuffer, msgp, MB_OK|MB_ICONSTOP|MB_SETFOREGROUND);
51
52         afsd_ForceTrace(TRUE);
53
54         if (traceOnPanic) {
55                 _asm int 3h;
56         }
57
58         exit(1);
59 }
60
61 /* Init function called when window application starts.  Inits instance and
62  * application together, since in Win32 they're essentially the same.
63  *
64  * Function then goes into a loop handling user interface messages.  Most are
65  * used to handle redrawing the icon.
66  */
67 int WINAPI WinMain(
68         HINSTANCE hInstance,
69         HINSTANCE hPrevInstance,
70         char *lpCmdLine,
71         int nCmdShow)
72 {
73         MSG msg;
74         
75     afsd_SetUnhandledExceptionFilter();
76        
77 #ifdef _DEBUG
78     afsd_DbgBreakAllocInit();
79     _CrtSetDbgFlag(_CRTDBG_ALLOC_MEM_DF /* | _CRTDBG_CHECK_ALWAYS_DF */ | _CRTDBG_CHECK_CRT_DF | _CRTDBG_DELAY_FREE_MEM_DF );
80     if (lpCmdLine)
81     {
82         char *allocRequest = strtok(lpCmdLine, " \t");
83         while (allocRequest)
84         {
85             afsd_DbgBreakAdd(atoi(allocRequest));
86             allocRequest = strtok(NULL, " \t");
87         }
88     }
89 #endif 
90
91     if (!InitClass(hInstance))
92                 return (FALSE);
93
94         if (!InitInstance(hInstance, nCmdShow))
95                 return (FALSE);
96
97         while (GetMessage(&msg, NULL, 0, 0)) {
98                 TranslateMessage(&msg);
99                 DispatchMessage(&msg);
100         }
101         return (msg.wParam);
102 }
103
104
105 /* create the window type for our main window */
106 BOOL InitClass(HANDLE hInstance)
107 {
108         WNDCLASS  wc;
109
110         wc.style = CS_DBLCLKS;          /* double-click messages */
111         wc.lpfnWndProc = (WNDPROC) MainWndProc;
112         wc.cbClsExtra = 0;
113         wc.cbWndExtra = 0;
114         wc.hInstance = hInstance;
115         wc.hIcon = LoadIcon(hInstance, "AFSDIcon");
116         wc.hCursor = LoadCursor(NULL, IDC_ARROW);
117         wc.hbrBackground = GetStockObject(WHITE_BRUSH); 
118         wc.lpszMenuName =  "AFSDMenu";
119         wc.lpszClassName = "AFSDWinClass";
120
121         return (RegisterClass(&wc));
122 }
123
124 /* initialize the process.  Reads the init files to get the appropriate
125  * information. */
126 BOOL InitInstance(
127         HANDLE hInstance,
128         int nCmdShow)
129 {
130         HWND hWnd;
131         HDC hDC;
132         TEXTMETRIC textmetric;
133         INT nLineHeight;
134     long code;
135         char *reason;
136  
137         /* remember this, since it is a useful thing for some of the Windows
138          * calls */
139         main_inst = hInstance;
140
141         /* create our window */
142         hWnd = CreateWindow(
143                 "AFSDWinClass",
144                 "AFSD",
145                 WS_OVERLAPPEDWINDOW | WS_HSCROLL | WS_VSCROLL,
146                 CW_USEDEFAULT,
147                 CW_USEDEFAULT,
148                 CW_USEDEFAULT,
149                 CW_USEDEFAULT,
150                 NULL,
151                 NULL,
152                 hInstance,
153                 NULL
154         );
155
156         if (!hWnd)
157                 return (FALSE);
158
159         /* lookup text dimensions */
160         hDC = GetDC(hWnd);
161         GetTextMetrics(hDC, &textmetric);
162         nLineHeight = textmetric.tmExternalLeading + textmetric.tmHeight;
163         
164         main_rect.left   = GetDeviceCaps(hDC, LOGPIXELSX) / 4;   /* 1/4 inch */
165         main_rect.right  = GetDeviceCaps(hDC, HORZRES);
166         main_rect.top    = GetDeviceCaps(hDC, LOGPIXELSY) / 4;   /* 1/4 inch */
167         ReleaseDC(hWnd, hDC);
168         main_rect.bottom = main_rect.top + nLineHeight;
169
170         osi_InitPanic(afsd_notifier);
171
172         afsi_start();
173
174         code = afsd_InitCM(&reason);
175         if (code != 0)
176                 osi_panic(reason, __FILE__, __LINE__);
177
178         code = afsd_InitDaemons(&reason);
179         if (code != 0)
180                 osi_panic(reason, __FILE__, __LINE__);
181
182         code = afsd_InitSMB(&reason, MessageBox);
183         if (code != 0)
184                 osi_panic(reason, __FILE__, __LINE__);
185
186         ShowWindow(hWnd, SW_SHOWMINNOACTIVE);
187         UpdateWindow(hWnd);
188         return (TRUE);
189 }
190
191 /* called with no locks with translated messages */
192 LONG APIENTRY MainWndProc(
193         HWND hWnd,
194         unsigned int message,
195         unsigned int wParam,
196         long lParam)
197 {
198         HDC hDC;                         /* display-context variable     */
199         PAINTSTRUCT ps;                  /* paint structure              */
200
201         main_wnd = hWnd;
202
203         switch (message) {
204             case WM_QUERYOPEN:
205                 /* block attempts to open the window */
206                 return 0;
207
208             case WM_COMMAND:
209                 /* LOWORD(wParam) is command */
210                 return (DefWindowProc(hWnd, message, wParam, lParam));
211
212             case WM_CREATE:
213                 break;
214
215             case WM_PAINT:
216                 hDC = BeginPaint (hWnd, &ps);
217                 /* nothing to print, but this clears invalidated rectangle flag */
218                 EndPaint(hWnd, &ps);
219                 break;
220
221             case WM_DESTROY:
222                 RpcMgmtStopServerListening(NULL);
223                 PostQuitMessage(0);
224                 break;
225
226             default:
227                 return (DefWindowProc(hWnd, message, wParam, lParam));
228         }
229         return (0);
230 }