Rebuild docs for 3.0.25 beta
[privoxy.git] / win32.c
1 const char win32_rcs[] = "$Id: win32.c,v 1.19 2012/03/09 16:23:50 fabiankeil Exp $";
2 /*********************************************************************
3  *
4  * File        :  $Source: /cvsroot/ijbswa/current/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  *********************************************************************/
33
34
35 #include "config.h"
36
37 #ifdef _WIN32
38
39 #include <stdio.h>
40
41 #include "project.h"
42 #include "jcc.h"
43 #include "miscutil.h"
44
45 /* Uncomment this if you want to build Win32 as a console app */
46 /* #define _WIN_CONSOLE */
47
48 #ifndef STRICT
49 #define STRICT
50 #endif
51 #include <windows.h>
52
53 #include <stdarg.h>
54 #include <process.h>
55
56 #if defined(_WIN32) && defined(_MSC_VER) && defined(_DEBUG)
57 /* Visual C++ Heap debugging */
58 #include <crtdbg.h>
59 #endif /* defined(_WIN32) && defined(_MSC_VER) && defined(_DEBUG) */
60
61 #include "win32.h"
62
63 const char win32_h_rcs[] = WIN32_H_VERSION;
64
65 /**
66  * A short introductory text about Privoxy.  Used for the "About" box
67  * or the console startup message.
68  */
69 const char win32_blurb[] =
70 "Privoxy version " VERSION " for Windows\n"
71 "Copyright (C) 2000-2010 the Privoxy Team (" HOME_PAGE_URL ")\n"
72 "Based on the Internet Junkbuster by Junkbusters Corp.\n"
73 "This is free software; it may be used and copied under the\n"
74 "GNU General Public License, version 2: http://www.gnu.org/licenses/old-licenses/gpl-2.0.html\n"
75 "This program comes with ABSOLUTELY NO WARRANTY OF ANY KIND.\n";
76
77 #ifdef _WIN_CONSOLE
78
79 /**
80  * Hide the console.  If set, the program will disconnect from the
81  * console and run in the background.  This allows the command-prompt
82  * window to close.
83  */
84 int hideConsole     = 0;
85
86
87 #else /* ndef _WIN_CONSOLE */
88
89
90 /**
91  * The application instance handle.
92  */
93 HINSTANCE g_hInstance;
94
95
96 /**
97  * The command to show the window that was specified at startup.
98  */
99 int g_nCmdShow;
100
101 static void  __cdecl UserInterfaceThread(void *);
102
103
104 #endif /* ndef _WIN_CONSOLE */
105
106 /*********************************************************************
107  *
108  * Function    :  WinMain
109  *
110  * Description :  M$ Windows "main" routine:
111  *                parse the `lpCmdLine' param into main's argc and argv variables,
112  *                start the user interface thread (for the systray window), and
113  *                call main (i.e. patch execution into normal startup).
114  *
115  * Parameters  :
116  *          1  :  hInstance = instance handle of this execution
117  *          2  :  hPrevInstance = instance handle of previous execution
118  *          3  :  lpCmdLine = command line string which started us
119  *          4  :  nCmdShow = window show value (MIN, MAX, NORMAL, etc...)
120  *
121  * Returns     :  `main' never returns, so WinMain will also never return.
122  *
123  *********************************************************************/
124 int WINAPI WinMain(HINSTANCE hInstance, HINSTANCE hPrevInstance, LPSTR lpCmdLine, int nCmdShow)
125 {
126 #if 0   /* See comment about __argc & __argv below */
127    int i;
128    int argc = 1;
129    const char *argv[3];
130    char szModule[MAX_PATH+1];
131 #endif
132
133    int res;
134 #ifndef _WIN_CONSOLE
135    HANDLE hInitCompleteEvent = NULL;
136 #endif
137
138
139 #if defined(_WIN32) && defined(_MSC_VER) && defined(_DEBUG)
140 #if 0
141    /* Visual C++ Heap debugging */
142
143    /* Get current flag*/
144    int tmpFlag = _CrtSetDbgFlag(_CRTDBG_REPORT_FLAG);
145
146    /* Turn on leak-checking bit */
147    tmpFlag |= _CRTDBG_ALLOC_MEM_DF | _CRTDBG_LEAK_CHECK_DF | _CRTDBG_CHECK_ALWAYS_DF;
148
149    /* Turn off CRT block checking bit */
150    tmpFlag &= ~(_CRTDBG_CHECK_CRT_DF | _CRTDBG_DELAY_FREE_MEM_DF);
151
152    /* Set flag to the new value */
153    _CrtSetDbgFlag(tmpFlag);
154 #endif
155 #endif /* defined(_WIN32) && defined(_MSC_VER) && defined(_DEBUG) */
156
157
158 /************
159  * I couldn't figure out why the command line was being sorta parsed here
160  * instead of using the __argc & __argv globals usually defined in stdlib.h
161  *
162  * From what I can tell by looking at the MinWG source, it supports these
163  * globals, so i'd hope that the other compilers do so as well.
164  * Obviously, if i'm wrong i'll find out soon enough!  :)
165  ************/
166 #if 0
167    /*
168     * Cheat in parsing the command line.  We only ever have at most one
169     * paramater, which may optionally be specified inside double quotes.
170     */
171
172    if (lpCmdLine != NULL)
173    {
174       /* Make writable copy */
175       lpCmdLine = strdup(lpCmdLine);
176    }
177    if (lpCmdLine != NULL)
178    {
179       chomp(lpCmdLine);
180       i = strlen(lpCmdLine);
181       if ((i >= 2) && (lpCmdLine[0] == '\"') && (lpCmdLine[i - 1] == '\"'))
182       {
183          lpCmdLine[i - 1] = '\0';
184          lpCmdLine++;
185       }
186       if (lpCmdLine[0] == '\0')
187       {
188          lpCmdLine = NULL;
189       }
190    }
191
192    GetModuleFileName(hInstance, szModule, MAX_PATH);
193    argv[0] = szModule;
194    argv[1] = lpCmdLine;
195    argv[2] = NULL;
196    argc = ((lpCmdLine != NULL) ? 2 : 1);
197 #endif /* -END- 0 */
198
199
200 #ifndef _WIN_CONSOLE
201    /* Create a user-interface thread and wait for it to initialise */
202    hInitCompleteEvent = CreateEvent(NULL, TRUE, FALSE, NULL);
203    g_hInstance = hInstance;
204    g_nCmdShow = nCmdShow;
205    _beginthread(UserInterfaceThread, 0, &hInitCompleteEvent);
206    WaitForSingleObject(hInitCompleteEvent, INFINITE);
207    CloseHandle(hInitCompleteEvent);
208 #endif
209
210 #ifdef __MINGW32__
211    res = real_main(__argc, __argv);
212 #else
213    res = main(__argc, __argv);
214 #endif
215
216    return res;
217
218 }
219
220 #endif
221
222 /*********************************************************************
223  *
224  * Function    :  InitWin32
225  *
226  * Description :  Initialise windows, setting up the console or windows as appropriate.
227  *
228  * Parameters  :  None
229  *
230  * Returns     :  N/A
231  *
232  *********************************************************************/
233 void InitWin32(void)
234 {
235    WORD wVersionRequested;
236    WSADATA wsaData;
237
238 #ifdef _WIN_CONSOLE
239    SetProcessShutdownParameters(0x100, SHUTDOWN_NORETRY);
240    if (hideConsole)
241    {
242       FreeConsole();
243    }
244 #endif
245    wVersionRequested = MAKEWORD(2, 0);
246    if (WSAStartup(wVersionRequested, &wsaData) != 0)
247    {
248 #ifndef _WIN_CONSOLE
249       MessageBox(NULL, "Cannot initialize WinSock library", "Privoxy Error",
250          MB_OK | MB_ICONERROR | MB_TASKMODAL | MB_SETFOREGROUND | MB_TOPMOST);
251 #endif
252       exit(1);
253    }
254
255 }
256
257
258 #ifndef _WIN_CONSOLE
259 #include <signal.h>
260 #include <assert.h>
261
262 #include "win32.h"
263 #include "w32log.h"
264
265
266 /*********************************************************************
267  *
268  * Function    :  UserInterfaceThread
269  *
270  * Description :  User interface thread.  WinMain will wait for us to set
271  *                the hInitCompleteEvent before patching over to `main'.
272  *                This ensures the systray window is active before beginning
273  *                operations.
274  *
275  * Parameters  :
276  *          1  :  pData = pointer to `hInitCompleteEvent'.
277  *
278  * Returns     :  N/A
279  *
280  *********************************************************************/
281 static void __cdecl UserInterfaceThread(void *pData)
282 {
283    MSG msg;
284    HANDLE hInitCompleteEvent = *((HANDLE *) pData);
285
286    /* Initialise */
287    InitLogWindow();
288    SetEvent(hInitCompleteEvent);
289
290    /* Enter a message processing loop */
291    while (GetMessage(&msg, (HWND) NULL, 0, 0))
292    {
293       TranslateMessage(&msg);
294       DispatchMessage(&msg);
295    }
296
297    /* Cleanup */
298    TermLogWindow();
299
300    /* Time to die... */
301    exit(0);
302
303 }
304
305
306 #endif /* ndef _WIN_CONSOLE */
307
308
309 /*
310   Local Variables:
311   tab-width: 3
312   end:
313 */