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