6c79852c95988da1564cd4198157cfa6e20a303c
[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 #include <winnt/osi_malloc.h>
24
25
26 HANDLE main_inst;
27 HWND main_wnd;
28 char main_statusText[100];
29 RECT main_rect;
30 osi_log_t *afsd_logp;
31
32 extern int traceOnPanic;
33
34 /*
35  * Notifier function for use by osi_panic
36  */
37 void afsd_notifier(char *msgp, char *filep, long line)
38 {
39         char tbuffer[100];
40         if (filep)
41                 sprintf(tbuffer, "Error at file %s, line %d", filep, line);
42         else
43                 strcpy(tbuffer, "Error at unknown location");
44
45         if (!msgp)
46                 msgp = "Assertion failure";
47
48         MessageBox(NULL, tbuffer, msgp, MB_OK|MB_ICONSTOP|MB_SETFOREGROUND);
49
50         afsd_ForceTrace(TRUE);
51
52         if (traceOnPanic) {
53                 _asm int 3h;
54         }
55
56         exit(1);
57 }
58
59 /* Init function called when window application starts.  Inits instance and
60  * application together, since in Win32 they're essentially the same.
61  *
62  * Function then goes into a loop handling user interface messages.  Most are
63  * used to handle redrawing the icon.
64  */
65 int WINAPI WinMain(
66         HINSTANCE hInstance,
67         HINSTANCE hPrevInstance,
68         char *lpCmdLine,
69         int nCmdShow)
70 {
71         MSG msg;
72         
73         if (!InitClass(hInstance))
74                 return (FALSE);
75
76         if (!InitInstance(hInstance, nCmdShow))
77                 return (FALSE);
78
79         while (GetMessage(&msg, NULL, 0, 0)) {
80                 TranslateMessage(&msg);
81                 DispatchMessage(&msg);
82         }
83         return (msg.wParam);
84 }
85
86
87 /* create the window type for our main window */
88 BOOL InitClass(HANDLE hInstance)
89 {
90         WNDCLASS  wc;
91
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(hInstance, "AFSDIcon");
98         wc.hCursor = LoadCursor(NULL, IDC_ARROW);
99         wc.hbrBackground = GetStockObject(WHITE_BRUSH); 
100         wc.lpszMenuName =  "AFSDMenu";
101         wc.lpszClassName = "AFSDWinClass";
102
103         return (RegisterClass(&wc));
104 }
105
106 /* initialize the process.  Reads the init files to get the appropriate
107  * information. */
108 BOOL InitInstance(
109         HANDLE hInstance,
110         int nCmdShow)
111 {
112         HWND hWnd;
113         HDC hDC;
114         TEXTMETRIC textmetric;
115         RECT rect;
116         INT nLineHeight;
117         long code;
118         char *reason;
119  
120         /* remember this, since it is a useful thing for some of the Windows
121          * calls */
122         main_inst = hInstance;
123
124         /* create our window */
125         hWnd = CreateWindow(
126                 "AFSDWinClass",
127                 "AFSD",
128                 WS_OVERLAPPEDWINDOW | WS_HSCROLL | WS_VSCROLL,
129                 CW_USEDEFAULT,
130                 CW_USEDEFAULT,
131                 CW_USEDEFAULT,
132                 CW_USEDEFAULT,
133                 NULL,
134                 NULL,
135                 hInstance,
136                 NULL
137         );
138
139         if (!hWnd)
140                 return (FALSE);
141
142
143         /* lookup text dimensions */
144         hDC = GetDC(hWnd);
145         GetTextMetrics(hDC, &textmetric);
146         nLineHeight = textmetric.tmExternalLeading + textmetric.tmHeight;
147         
148         main_rect.left   = GetDeviceCaps(hDC, LOGPIXELSX) / 4;   /* 1/4 inch */
149         main_rect.right  = GetDeviceCaps(hDC, HORZRES);
150         main_rect.top    = GetDeviceCaps(hDC, LOGPIXELSY) / 4;   /* 1/4 inch */
151         ReleaseDC(hWnd, hDC);
152         main_rect.bottom = rect.top + nLineHeight;
153
154         osi_InitPanic(afsd_notifier);
155
156         afsi_start();
157
158         code = afsd_InitCM(&reason);
159         if (code != 0)
160                 osi_panic(reason, __FILE__, __LINE__);
161
162         code = afsd_InitDaemons(&reason);
163         if (code != 0)
164                 osi_panic(reason, __FILE__, __LINE__);
165
166         code = afsd_InitSMB(&reason, MessageBox);
167         if (code != 0)
168                 osi_panic(reason, __FILE__, __LINE__);
169
170         ShowWindow(hWnd, SW_SHOWMINNOACTIVE);
171         UpdateWindow(hWnd);
172         return (TRUE);
173 }
174
175 /* called with no locks with translated messages */
176 LONG APIENTRY MainWndProc(
177         HWND hWnd,
178         unsigned int message,
179         unsigned int wParam,
180         long lParam)
181 {
182         HDC hDC;                         /* display-context variable     */
183         PAINTSTRUCT ps;                  /* paint structure              */
184
185         main_wnd = hWnd;
186
187         switch (message) {
188             case WM_QUERYOPEN:
189                 /* block attempts to open the window */
190                 return 0;
191
192             case WM_COMMAND:
193                 /* LOWORD(wParam) is command */
194                 return (DefWindowProc(hWnd, message, wParam, lParam));
195
196             case WM_CREATE:
197                 break;
198
199             case WM_PAINT:
200                 hDC = BeginPaint (hWnd, &ps);
201                 /* nothing to print, but this clears invalidated rectangle flag */
202                 EndPaint(hWnd, &ps);
203                 break;
204
205             case WM_DESTROY:
206                 RpcMgmtStopServerListening(NULL);
207                 PostQuitMessage(0);
208                 break;
209
210             default:
211                 return (DefWindowProc(hWnd, message, wParam, lParam));
212         }
213         return (0);
214 }