comparison src/main/win32/SDL_win32_main.c @ 1718:ed4d4f1ea201 SDL-1.3

Further progress on the new Windows video driver: * SDL_SetModuleHandle() is obsolete, I hope. * SDL 1.3 uses the UNICODE API * I'm ignoring Windows CE for the moment, we'll reevaluate what needs to be different for Windows CE later. * Pulled the stdio redirection from WinMain()
author Sam Lantinga <slouken@libsdl.org>
date Tue, 27 Jun 2006 07:46:36 +0000
parents 4da1ee79c9af
children
comparison
equal deleted inserted replaced
1717:e3637569ab86 1718:ed4d4f1ea201
8 #include <stdlib.h> 8 #include <stdlib.h>
9 9
10 #define WIN32_LEAN_AND_MEAN 10 #define WIN32_LEAN_AND_MEAN
11 #include <windows.h> 11 #include <windows.h>
12 12
13 #ifdef _WIN32_WCE
14 # define DIR_SEPERATOR TEXT("\\")
15 # undef _getcwd
16 # define _getcwd(str,len) wcscpy(str,TEXT(""))
17 # define setbuf(f,b)
18 # define setvbuf(w,x,y,z)
19 # define fopen _wfopen
20 # define freopen _wfreopen
21 # define remove(x) DeleteFile(x)
22 #else
23 # define DIR_SEPERATOR TEXT("/")
24 # include <direct.h>
25 #endif
26
27 /* Include the SDL main definition header */ 13 /* Include the SDL main definition header */
28 #include "SDL.h" 14 #include "SDL.h"
29 #include "SDL_main.h" 15 #include "SDL_main.h"
30 16
31 #ifdef main 17 #ifdef main
32 # ifndef _WIN32_WCE_EMULATION 18 # ifndef _WIN32_WCE_EMULATION
33 # undef main 19 # undef main
34 # endif /* _WIN32_WCE_EMULATION */ 20 # endif /* _WIN32_WCE_EMULATION */
35 #endif /* main */ 21 #endif /* main */
36
37 /* The standard output files */
38 #define STDOUT_FILE TEXT("stdout.txt")
39 #define STDERR_FILE TEXT("stderr.txt")
40
41 #ifndef NO_STDIO_REDIRECT
42 # ifdef _WIN32_WCE
43 static wchar_t stdoutPath[MAX_PATH];
44 static wchar_t stderrPath[MAX_PATH];
45 # else
46 static char stdoutPath[MAX_PATH];
47 static char stderrPath[MAX_PATH];
48 # endif
49 #endif
50 22
51 #if defined(_WIN32_WCE) && _WIN32_WCE < 300 23 #if defined(_WIN32_WCE) && _WIN32_WCE < 300
52 /* seems to be undefined in Win CE although in online help */ 24 /* seems to be undefined in Win CE although in online help */
53 #define isspace(a) (((CHAR)a == ' ') || ((CHAR)a == '\t')) 25 #define isspace(a) (((CHAR)a == ' ') || ((CHAR)a == '\t'))
54 #endif /* _WIN32_WCE < 300 */ 26 #endif /* _WIN32_WCE < 300 */
122 { 94 {
123 ShowError("Fatal Error", "Out of memory - aborting"); 95 ShowError("Fatal Error", "Out of memory - aborting");
124 return FALSE; 96 return FALSE;
125 } 97 }
126 98
127 /* SDL_Quit() shouldn't be used with atexit() directly because
128 calling conventions may differ... */
129 static void
130 cleanup(void)
131 {
132 SDL_Quit();
133 }
134
135 /* Remove the output files if there was no output written */
136 static void
137 cleanup_output(void)
138 {
139 #ifndef NO_STDIO_REDIRECT
140 FILE *file;
141 int empty;
142 #endif
143
144 /* Flush the output in case anything is queued */
145 fclose(stdout);
146 fclose(stderr);
147
148 #ifndef NO_STDIO_REDIRECT
149 /* See if the files have any output in them */
150 if (stdoutPath[0]) {
151 file = fopen(stdoutPath, TEXT("rb"));
152 if (file) {
153 empty = (fgetc(file) == EOF) ? 1 : 0;
154 fclose(file);
155 if (empty) {
156 remove(stdoutPath);
157 }
158 }
159 }
160 if (stderrPath[0]) {
161 file = fopen(stderrPath, TEXT("rb"));
162 if (file) {
163 empty = (fgetc(file) == EOF) ? 1 : 0;
164 fclose(file);
165 if (empty) {
166 remove(stderrPath);
167 }
168 }
169 }
170 #endif
171 }
172
173 #if defined(_MSC_VER) && !defined(_WIN32_WCE) 99 #if defined(_MSC_VER) && !defined(_WIN32_WCE)
174 /* The VC++ compiler needs main defined */ 100 /* The VC++ compiler needs main defined */
175 #define console_main main 101 #define console_main main
176 #endif 102 #endif
177 103
178 /* This is where execution begins [console apps] */ 104 /* This is where execution begins [console apps] */
179 int 105 int
180 console_main(int argc, char *argv[]) 106 console_main(int argc, char *argv[])
181 { 107 {
182 size_t n;
183 char *bufp, *appname;
184 int status; 108 int status;
185
186 /* Get the class name from argv[0] */
187 appname = argv[0];
188 if ((bufp = SDL_strrchr(argv[0], '\\')) != NULL) {
189 appname = bufp + 1;
190 } else if ((bufp = SDL_strrchr(argv[0], '/')) != NULL) {
191 appname = bufp + 1;
192 }
193
194 if ((bufp = SDL_strrchr(appname, '.')) == NULL)
195 n = SDL_strlen(appname);
196 else
197 n = (bufp - appname);
198
199 bufp = SDL_stack_alloc(char, n + 1);
200 if (bufp == NULL) {
201 return OutOfMemory();
202 }
203 SDL_strlcpy(bufp, appname, n + 1);
204 appname = bufp;
205
206 /* Load SDL dynamic link library */
207 if (SDL_Init(SDL_INIT_NOPARACHUTE) < 0) {
208 ShowError("WinMain() error", SDL_GetError());
209 return (FALSE);
210 }
211 atexit(cleanup_output);
212 atexit(cleanup);
213
214 /* Sam:
215 We still need to pass in the application handle so that
216 DirectInput will initialize properly when SDL_RegisterApp()
217 is called later in the video initialization.
218 */
219 SDL_SetModuleHandle(GetModuleHandle(NULL));
220 109
221 /* Run the application main() code */ 110 /* Run the application main() code */
222 status = SDL_main(argc, argv); 111 status = SDL_main(argc, argv);
223 112
224 /* Exit cleanly, calling atexit() functions */ 113 /* Exit cleanly, calling atexit() functions */
227 /* Hush little compiler, don't you cry... */ 116 /* Hush little compiler, don't you cry... */
228 return 0; 117 return 0;
229 } 118 }
230 119
231 /* This is where execution begins [windowed apps] */ 120 /* This is where execution begins [windowed apps] */
232 #ifdef _WIN32_WCE
233 int WINAPI 121 int WINAPI
234 WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPWSTR szCmdLine, int sw) 122 WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPTSTR szCmdLine, int sw)
235 #else
236 int WINAPI
237 WinMain(HINSTANCE hInst, HINSTANCE hPrev, LPSTR szCmdLine, int sw)
238 #endif
239 { 123 {
240 HINSTANCE handle;
241 char **argv; 124 char **argv;
242 int argc; 125 int argc;
243 char *cmdline; 126 char *cmdline;
244 DWORD pathlen;
245 #ifdef _WIN32_WCE
246 wchar_t path[MAX_PATH];
247 #else
248 char path[MAX_PATH];
249 #endif
250 #ifdef _WIN32_WCE 127 #ifdef _WIN32_WCE
251 wchar_t *bufp; 128 wchar_t *bufp;
252 int nLen; 129 int nLen;
253 #else 130 #else
254 char *bufp; 131 char *bufp;
255 size_t nLen; 132 size_t nLen;
256 #endif 133 #endif
257 #ifndef NO_STDIO_REDIRECT
258 FILE *newfp;
259 #endif
260
261 /* Start up DDHELP.EXE before opening any files, so DDHELP doesn't
262 keep them open. This is a hack.. hopefully it will be fixed
263 someday. DDHELP.EXE starts up the first time DDRAW.DLL is loaded.
264 */
265 handle = LoadLibrary(TEXT("DDRAW.DLL"));
266 if (handle != NULL) {
267 FreeLibrary(handle);
268 }
269 #ifndef NO_STDIO_REDIRECT
270 pathlen = GetModuleFileName(NULL, path, SDL_arraysize(path));
271 while (pathlen > 0 && path[pathlen] != '\\') {
272 --pathlen;
273 }
274 path[pathlen] = '\0';
275
276 #ifdef _WIN32_WCE
277 wcsncpy(stdoutPath, path, SDL_arraysize(stdoutPath));
278 wcsncat(stdoutPath, DIR_SEPERATOR STDOUT_FILE, SDL_arraysize(stdoutPath));
279 #else
280 SDL_strlcpy(stdoutPath, path, SDL_arraysize(stdoutPath));
281 SDL_strlcat(stdoutPath, DIR_SEPERATOR STDOUT_FILE,
282 SDL_arraysize(stdoutPath));
283 #endif
284
285 /* Redirect standard input and standard output */
286 newfp = freopen(stdoutPath, TEXT("w"), stdout);
287
288 #ifndef _WIN32_WCE
289 if (newfp == NULL) { /* This happens on NT */
290 #if !defined(stdout)
291 stdout = fopen(stdoutPath, TEXT("w"));
292 #else
293 newfp = fopen(stdoutPath, TEXT("w"));
294 if (newfp) {
295 *stdout = *newfp;
296 }
297 #endif
298 }
299 #endif /* _WIN32_WCE */
300
301 #ifdef _WIN32_WCE
302 wcsncpy(stderrPath, path, SDL_arraysize(stdoutPath));
303 wcsncat(stderrPath, DIR_SEPERATOR STDOUT_FILE, SDL_arraysize(stdoutPath));
304 #else
305 SDL_strlcpy(stderrPath, path, SDL_arraysize(stderrPath));
306 SDL_strlcat(stderrPath, DIR_SEPERATOR STDERR_FILE,
307 SDL_arraysize(stderrPath));
308 #endif
309
310 newfp = freopen(stderrPath, TEXT("w"), stderr);
311 #ifndef _WIN32_WCE
312 if (newfp == NULL) { /* This happens on NT */
313 #if !defined(stderr)
314 stderr = fopen(stderrPath, TEXT("w"));
315 #else
316 newfp = fopen(stderrPath, TEXT("w"));
317 if (newfp) {
318 *stderr = *newfp;
319 }
320 #endif
321 }
322 #endif /* _WIN32_WCE */
323
324 setvbuf(stdout, NULL, _IOLBF, BUFSIZ); /* Line buffered */
325 setbuf(stderr, NULL); /* No buffering */
326 #endif /* !NO_STDIO_REDIRECT */
327 134
328 #ifdef _WIN32_WCE 135 #ifdef _WIN32_WCE
329 nLen = wcslen(szCmdLine) + 128 + 1; 136 nLen = wcslen(szCmdLine) + 128 + 1;
330 bufp = SDL_stack_alloc(wchar_t, nLen * 2); 137 bufp = SDL_stack_alloc(wchar_t, nLen * 2);
331 wcscpy(bufp, TEXT("\"")); 138 wcscpy(bufp, TEXT("\""));
355 if (argv == NULL) { 162 if (argv == NULL) {
356 return OutOfMemory(); 163 return OutOfMemory();
357 } 164 }
358 ParseCommandLine(cmdline, argv); 165 ParseCommandLine(cmdline, argv);
359 166
360 /* Run the main program (after a little SDL initialization) */ 167 /* Run the main program */
361 console_main(argc, argv); 168 console_main(argc, argv);
362 169
363 /* Hush little compiler, don't you cry... */ 170 /* Hush little compiler, don't you cry... */
364 return 0; 171 return 0;
365 } 172 }