Adding Doxygen-style comments to variables
[privoxy.git] / src / win32.c
1 const char win32_rcs[] = "$Id: win32.c,v 2.0 2002/06/04 14:34:21 jongfoster Exp $";
2 /*********************************************************************
3  *
4  * File        :  $Source: /cvsroot/ijbswa/current/src/win32.c,v $
5  *
6  * Purpose     :  Win32 User Interface initialization and message loop
7  *
8  * Copyright   :  Written by and Copyright (C) 2001-2002 members of
9  *                the Privoxy team.  http://www.privoxy.org/
10  *
11  *                Written by and Copyright (C) 1999 Adam Lock
12  *                <locka@iol.ie>
13  *
14  *                This program is free software; you can redistribute it 
15  *                and/or modify it under the terms of the GNU General
16  *                Public License as published by the Free Software
17  *                Foundation; either version 2 of the License, or (at
18  *                your option) any later version.
19  *
20  *                This program is distributed in the hope that it will
21  *                be useful, but WITHOUT ANY WARRANTY; without even the
22  *                implied warranty of MERCHANTABILITY or FITNESS FOR A
23  *                PARTICULAR PURPOSE.  See the GNU General Public
24  *                License for more details.
25  *
26  *                The GNU General Public License should be included with
27  *                this file.  If not, you can view it at
28  *                http://www.gnu.org/copyleft/gpl.html
29  *                or write to the Free Software Foundation, Inc., 59
30  *                Temple Place - Suite 330, Boston, MA  02111-1307, USA.
31  *
32  * Revisions   :
33  *    $Log: win32.c,v $
34  *    Revision 2.0  2002/06/04 14:34:21  jongfoster
35  *    Moving source files to src/
36  *
37  *    Revision 1.9  2002/03/31 17:19:00  jongfoster
38  *    Win32 only: Enabling STRICT to fix a VC++ compile warning.
39  *
40  *    Revision 1.8  2002/03/26 22:57:10  jongfoster
41  *    Web server name should begin www.
42  *
43  *    Revision 1.7  2002/03/24 12:03:47  jongfoster
44  *    Name change
45  *
46  *    Revision 1.6  2002/03/16 21:53:28  jongfoster
47  *    VC++ Heap debug option
48  *
49  *    Revision 1.5  2002/03/04 23:47:30  jongfoster
50  *    - Rewritten, simpler command-line pre-parser
51  *    - not using raise(SIGINT) any more
52  *
53  *    Revision 1.4  2001/11/30 21:29:33  jongfoster
54  *    Fixing a warning
55  *
56  *    Revision 1.3  2001/11/16 00:46:31  jongfoster
57  *    Fixing compiler warnings
58  *
59  *    Revision 1.2  2001/07/29 19:32:00  jongfoster
60  *    Renaming _main() [mingw32 only] to real_main(), for ANSI compliance.
61  *
62  *    Revision 1.1.1.1  2001/05/15 13:59:08  oes
63  *    Initial import of version 2.9.3 source tree
64  *
65  *
66  *********************************************************************/
67 \f
68
69 #include "config.h"
70
71 #ifdef _WIN32
72
73 #include <stdio.h>
74
75 #include "project.h"
76 #include "jcc.h"
77 #include "miscutil.h"
78
79 /* Uncomment this if you want to build Win32 as a console app */
80 /* #define _WIN_CONSOLE */
81
82 #ifndef STRICT
83 #define STRICT
84 #endif
85 #include <windows.h>
86
87 #include <stdarg.h>
88 #include <process.h>
89
90 #if defined(_WIN32) && defined(_MSC_VER) && defined(_DEBUG)
91 /* Visual C++ Heap debugging */
92 #include <crtdbg.h>
93 #endif /* defined(_WIN32) && defined(_MSC_VER) && defined(_DEBUG) */
94
95 #include "win32.h"
96
97 const char win32_h_rcs[] = WIN32_H_VERSION;
98
99 /**
100  * A short introductory text about Privoxy.  Used for the "About" box
101  * or the console startup message.
102  */
103 const char win32_blurb[] =
104 "Privoxy version " VERSION " for Windows\n"
105 "Copyright (C) 2000-2002 by members of the Privoxy Team\n"
106 "Copyright (C) 1997-8 by Junkbusters Corp.\n"
107 "This is free software; it may be used and copied under the\n"
108 "GNU General Public License: http://www.gnu.org/copyleft/gpl.html .\n"
109 "This program comes with ABSOLUTELY NO WARRANTY OF ANY KIND.\n"
110 "\n"
111 "For information about how to to configure the proxy and your browser, see\n"
112 "        " HOME_PAGE_URL "\n"
113 "\n";
114
115
116 #ifdef _WIN_CONSOLE
117
118
119 /**
120  * Hide the console.  If set, the program will disconnect from the 
121  * console and run in the background.  This allows the command-prompt
122  * window to close.
123  */
124 int hideConsole     = 0;
125
126
127 #else /* ndef _WIN_CONSOLE */
128
129
130 /**
131  * The application instance handle.
132  */
133 HINSTANCE g_hInstance;
134
135
136 /**
137  * The command to show the window that was specified at startup.
138  */
139 int g_nCmdShow;
140
141
142 static void  __cdecl UserInterfaceThread(void *);
143
144
145 #endif /* ndef _WIN_CONSOLE */
146
147
148
149 /*********************************************************************
150  *
151  * Function    :  WinMain
152  *
153  * Description :  M$ Windows "main" routine:
154  *                parse the `lpCmdLine' param into main's argc and argv variables,
155  *                start the user interface thread (for the systray window), and
156  *                call main (i.e. patch execution into normal startup).
157  *
158  * Parameters  :
159  *          1  :  hInstance = instance handle of this execution
160  *          2  :  hPrevInstance = instance handle of previous execution
161  *          3  :  lpCmdLine = command line string which started us
162  *          4  :  nCmdShow = window show value (MIN, MAX, NORMAL, etc...)
163  *
164  * Returns     :  `main' never returns, so WinMain will also never return.
165  *
166  *********************************************************************/
167 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
168 {
169    int i;
170    int res;
171    int argc = 1;
172    const char *argv[3];
173    char szModule[MAX_PATH+1];
174 #ifndef _WIN_CONSOLE
175    HANDLE hInitCompleteEvent = NULL;
176 #endif
177
178
179 #if defined(_WIN32) && defined(_MSC_VER) && defined(_DEBUG)
180 #if 0
181    /* Visual C++ Heap debugging */
182
183    /* Get current flag*/
184    int tmpFlag = _CrtSetDbgFlag( _CRTDBG_REPORT_FLAG );
185
186    /* Turn on leak-checking bit */
187    tmpFlag |= _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF | _CRTDBG_CHECK_ALWAYS_DF;
188
189    /* Turn off CRT block checking bit */
190    tmpFlag &= ~(_CRTDBG_CHECK_CRT_DF | _CRTDBG_DELAY_FREE_MEM_DF);
191
192    /* Set flag to the new value */
193    _CrtSetDbgFlag( tmpFlag );
194 #endif
195 #endif /* defined(_WIN32) && defined(_MSC_VER) && defined(_DEBUG) */
196
197    /*
198     * Cheat in parsing the command line.  We only ever have at most one
199     * paramater, which may optionally be specified inside double quotes.
200     */
201
202    if (lpCmdLine != NULL)
203    {
204       /* Make writable copy */
205       lpCmdLine = strdup(lpCmdLine);
206    }
207    if (lpCmdLine != NULL)
208    {
209       chomp(lpCmdLine);
210       i = strlen(lpCmdLine);
211       if ((i >= 2) && (lpCmdLine[0] == '\"') && (lpCmdLine[i - 1] == '\"'))
212       {
213          lpCmdLine[i - 1] = '\0';
214          lpCmdLine++;
215       }
216       if (lpCmdLine[0] == '\0')
217       {
218          lpCmdLine = NULL;
219       }
220    }
221
222    GetModuleFileName(hInstance, szModule, MAX_PATH);
223    argv[0] = szModule;
224    argv[1] = lpCmdLine;
225    argv[2] = NULL;
226    argc = ((lpCmdLine != NULL) ? 2 : 1);
227
228 #ifndef _WIN_CONSOLE
229    /* Create a user-interface thread and wait for it to initialise */
230    hInitCompleteEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
231    g_hInstance = hInstance;
232    g_nCmdShow = nCmdShow;
233    _beginthread(UserInterfaceThread, 0, &hInitCompleteEvent);
234    WaitForSingleObject(hInitCompleteEvent, INFINITE);
235    DeleteObject(hInitCompleteEvent);
236 #endif
237
238 #ifdef __MINGW32__
239    res = real_main( argc, argv );
240 #else
241    res = main( argc, argv );
242 #endif
243
244    return res;
245
246 }
247
248 #endif
249
250 /*********************************************************************
251  *
252  * Function    :  InitWin32
253  *
254  * Description :  Initialise windows, setting up the console or windows as appropriate.
255  *
256  * Parameters  :  None
257  *
258  * Returns     :  N/A
259  *
260  *********************************************************************/
261 void InitWin32(void)
262 {
263    WORD wVersionRequested;
264    WSADATA wsaData;
265
266 #ifdef _WIN_CONSOLE
267    SetProcessShutdownParameters(0x100, SHUTDOWN_NORETRY);
268    if (hideConsole)
269    {
270       FreeConsole();
271    }
272 #endif
273    wVersionRequested = MAKEWORD(2, 0);
274    if (WSAStartup(wVersionRequested, &wsaData) != 0)
275    {
276 #ifndef _WIN_CONSOLE
277       MessageBox(NULL, "Cannot initialize WinSock library", "Privoxy Error", 
278          MB_OK | MB_ICONERROR | MB_TASKMODAL | MB_SETFOREGROUND | MB_TOPMOST);  
279 #endif
280       exit(1);
281    }
282
283 }
284
285
286 #ifndef _WIN_CONSOLE
287 #include <signal.h>
288 #include <assert.h>
289
290 #include "win32.h"
291 #include "w32log.h"
292
293
294 /*********************************************************************
295  *
296  * Function    :  UserInterfaceThread
297  *
298  * Description :  User interface thread.  WinMain will wait for us to set
299  *                the hInitCompleteEvent before patching over to `main'.
300  *                This ensures the systray window is active before beginning
301  *                operations.
302  *
303  * Parameters  :
304  *          1  :  pData = pointer to `hInitCompleteEvent'.
305  *
306  * Returns     :  N/A
307  *
308  *********************************************************************/
309 static void __cdecl UserInterfaceThread(void *pData)
310 {
311    MSG msg;
312    HANDLE hInitCompleteEvent = *((HANDLE *) pData);
313
314    /* Initialise */
315    InitLogWindow();
316    SetEvent(hInitCompleteEvent);
317
318    /* Enter a message processing loop */
319    while (GetMessage(&msg, (HWND) NULL, 0, 0))
320    {
321       TranslateMessage(&msg);
322       DispatchMessage(&msg);
323    }
324
325    /* Cleanup */
326    TermLogWindow();
327
328    /* Time to die... */
329    exit(0);
330
331 }
332
333
334 #endif
335
336
337 /*
338   Local Variables:
339   tab-width: 3
340   end:
341 */