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