2006-04-12 01:59:08 +02:00
|
|
|
/**
|
|
|
|
* =========================================================================
|
|
|
|
* File : win.cpp
|
|
|
|
* Project : 0 A.D.
|
|
|
|
* Description : various Windows-specific code and program entry point.
|
|
|
|
*
|
|
|
|
* @author Jan.Wassenberg@stud.uni-karlsruhe.de
|
|
|
|
* =========================================================================
|
|
|
|
*/
|
|
|
|
|
|
|
|
/*
|
|
|
|
* Copyright (c) 2003-2004 Jan Wassenberg
|
|
|
|
*
|
|
|
|
* Redistribution and/or modification are also permitted under the
|
|
|
|
* terms of the GNU General Public License as published by the
|
|
|
|
* Free Software Foundation (version 2 or later, at your option).
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful, but
|
|
|
|
* WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
|
|
|
|
*/
|
2004-03-03 00:56:51 +01:00
|
|
|
|
2004-05-08 03:11:51 +02:00
|
|
|
#include "precompiled.h"
|
|
|
|
|
2004-06-04 14:41:53 +02:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h> // __argc
|
2005-05-31 21:00:49 +02:00
|
|
|
|
2005-12-07 04:44:17 +01:00
|
|
|
#include "win_internal.h"
|
2006-04-22 18:26:16 +02:00
|
|
|
#include "lib/path_util.h"
|
2005-01-29 17:43:46 +01:00
|
|
|
|
2006-02-16 21:45:38 +01:00
|
|
|
#if MSC_VERSION >= 1400
|
|
|
|
#include <process.h> // __security_init_cookie
|
|
|
|
#define NEED_COOKIE_INIT
|
|
|
|
#endif
|
|
|
|
|
2004-07-12 16:25:39 +02:00
|
|
|
|
2005-06-22 05:23:22 +02:00
|
|
|
char win_sys_dir[MAX_PATH+1];
|
|
|
|
char win_exe_dir[MAX_PATH+1];
|
2005-01-30 18:40:24 +01:00
|
|
|
|
2005-12-11 23:23:55 +01:00
|
|
|
|
|
|
|
// only call after a Win32 function indicates failure.
|
2006-04-03 03:00:45 +02:00
|
|
|
static LibError LibError_from_GLE(bool warn_if_failed = true)
|
2005-12-11 23:23:55 +01:00
|
|
|
{
|
2006-04-03 03:00:45 +02:00
|
|
|
LibError err;
|
2005-12-11 23:23:55 +01:00
|
|
|
switch(GetLastError())
|
|
|
|
{
|
|
|
|
case ERROR_OUTOFMEMORY:
|
2006-04-03 03:00:45 +02:00
|
|
|
err = ERR_NO_MEM; break;
|
2005-12-11 23:23:55 +01:00
|
|
|
|
|
|
|
case ERROR_INVALID_PARAMETER:
|
2006-04-03 03:00:45 +02:00
|
|
|
err = ERR_INVALID_PARAM; break;
|
2005-12-11 23:23:55 +01:00
|
|
|
case ERROR_INSUFFICIENT_BUFFER:
|
2006-04-03 03:00:45 +02:00
|
|
|
err = ERR_BUF_SIZE; break;
|
2005-12-11 23:23:55 +01:00
|
|
|
|
|
|
|
case ERROR_ACCESS_DENIED:
|
2006-04-03 03:00:45 +02:00
|
|
|
err = ERR_FILE_ACCESS; break;
|
2005-12-11 23:23:55 +01:00
|
|
|
case ERROR_FILE_NOT_FOUND:
|
|
|
|
case ERROR_PATH_NOT_FOUND:
|
2006-05-17 16:48:18 +02:00
|
|
|
err = ERR_TNODE_NOT_FOUND; break;
|
2005-12-11 23:23:55 +01:00
|
|
|
|
|
|
|
default:
|
2006-04-03 03:00:45 +02:00
|
|
|
err = ERR_FAIL; break;
|
2005-12-11 23:23:55 +01:00
|
|
|
}
|
2006-04-03 03:00:45 +02:00
|
|
|
|
|
|
|
if(warn_if_failed)
|
|
|
|
DEBUG_WARN_ERR(err);
|
|
|
|
return err;
|
2005-12-11 23:23:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// return the LibError equivalent of GetLastError(), or ERR_FAIL if
|
|
|
|
// there's no equal.
|
|
|
|
// you should SetLastError(0) before calling whatever will set ret
|
|
|
|
// to make sure we do not return any stale errors.
|
2006-04-03 03:00:45 +02:00
|
|
|
LibError LibError_from_win32(DWORD ret, bool warn_if_failed)
|
2005-12-11 23:23:55 +01:00
|
|
|
{
|
2006-04-03 03:00:45 +02:00
|
|
|
if(ret != FALSE)
|
2006-06-05 00:27:40 +02:00
|
|
|
return INFO_OK;
|
2006-04-03 03:00:45 +02:00
|
|
|
return LibError_from_GLE(warn_if_failed);
|
2005-12-11 23:23:55 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-06-22 05:23:22 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
2005-06-21 18:44:12 +02:00
|
|
|
|
2005-01-30 18:40:24 +01:00
|
|
|
//
|
|
|
|
// safe allocator that may be used independently of libc malloc
|
|
|
|
// (in particular, before _cinit and while calling static dtors).
|
|
|
|
// used by wpthread critical section code.
|
|
|
|
//
|
|
|
|
|
|
|
|
void* win_alloc(size_t size)
|
|
|
|
{
|
|
|
|
const DWORD flags = HEAP_ZERO_MEMORY;
|
|
|
|
return HeapAlloc(GetProcessHeap(), flags, size);
|
|
|
|
}
|
|
|
|
|
|
|
|
void win_free(void* p)
|
|
|
|
{
|
|
|
|
const DWORD flags = 0;
|
|
|
|
HeapFree(GetProcessHeap(), flags, p);
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2004-06-13 18:06:23 +02:00
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
|
|
|
//
|
2005-05-12 00:40:19 +02:00
|
|
|
// module init and shutdown mechanism
|
2004-06-13 18:06:23 +02:00
|
|
|
//
|
|
|
|
///////////////////////////////////////////////////////////////////////////////
|
2004-03-03 00:56:51 +01:00
|
|
|
|
2004-05-27 19:30:06 +02:00
|
|
|
|
2004-09-02 04:46:50 +02:00
|
|
|
// init and shutdown mechanism: register a function to be called at
|
2005-01-23 19:04:34 +01:00
|
|
|
// pre-libc init, pre-main init or shutdown.
|
2004-09-02 04:46:50 +02:00
|
|
|
//
|
|
|
|
// each module has the linker add a pointer to its init or shutdown
|
2004-06-13 18:06:23 +02:00
|
|
|
// function to a table (at a user-defined position).
|
|
|
|
// zero runtime overhead, and there's no need for a central dispatcher
|
|
|
|
// that knows about all the modules.
|
|
|
|
//
|
|
|
|
// disadvantage: requires compiler support (MS VC-specific).
|
|
|
|
//
|
|
|
|
// alternatives:
|
|
|
|
// - initialize via constructor. however, that would leave the problem of
|
|
|
|
// shutdown order and timepoint, which is also taken care of here.
|
|
|
|
// - register init/shutdown functions from a NLSO constructor:
|
2004-09-02 04:46:50 +02:00
|
|
|
// clunky, and setting order is more difficult.
|
2004-06-13 18:06:23 +02:00
|
|
|
// - on-demand initialization: complicated; don't know in what order
|
|
|
|
// things happen. also, no way to determine how long init takes.
|
2004-09-02 04:46:50 +02:00
|
|
|
//
|
|
|
|
// the "segment name" determines when and in what order the functions are
|
2005-01-23 19:04:34 +01:00
|
|
|
// called: "LIB$W{type}{group}", where {type} is C for pre-libc init,
|
|
|
|
// I for pre-main init, or T for terminators (last of the atexit handlers).
|
2004-09-02 04:46:50 +02:00
|
|
|
// {group} is [B, Y]; groups are called in alphabetical order, but
|
|
|
|
// call order within the group itself is unspecified.
|
|
|
|
//
|
|
|
|
// define the segment via #pragma data_seg(name), register any functions
|
|
|
|
// to be called via WIN_REGISTER_FUNC, and then restore the previous segment
|
|
|
|
// with #pragma data_seg() .
|
|
|
|
//
|
|
|
|
// note: group must be [B, Y]. data declared in groups A or Z may
|
|
|
|
// be placed beyond the table start/end by the linker, since the linker's
|
|
|
|
// ordering WRT other source files' data is undefined within a segment.
|
2004-06-13 18:06:23 +02:00
|
|
|
|
2005-12-11 23:23:55 +01:00
|
|
|
typedef LibError (*_PIFV)(void);
|
2004-06-13 18:06:23 +02:00
|
|
|
|
|
|
|
// pointers to start and end of function tables.
|
2005-09-08 03:48:57 +02:00
|
|
|
// note: COFF tosses out empty segments, so we have to put in one value
|
2004-06-13 18:06:23 +02:00
|
|
|
// (zero, because call_func_tbl has to ignore NULL entries anyway).
|
2005-06-21 18:44:12 +02:00
|
|
|
#pragma data_seg(WIN_CALLBACK_PRE_LIBC(a))
|
2005-01-23 19:04:34 +01:00
|
|
|
_PIFV pre_libc_begin[] = { 0 };
|
2005-06-21 18:44:12 +02:00
|
|
|
#pragma data_seg(WIN_CALLBACK_PRE_LIBC(z))
|
2005-01-23 19:04:34 +01:00
|
|
|
_PIFV pre_libc_end[] = { 0 };
|
2005-06-21 18:44:12 +02:00
|
|
|
#pragma data_seg(WIN_CALLBACK_PRE_MAIN(a))
|
2005-01-23 19:04:34 +01:00
|
|
|
_PIFV pre_main_begin[] = { 0 };
|
2005-06-23 01:38:04 +02:00
|
|
|
#pragma data_seg(WIN_CALLBACK_PRE_MAIN(z))
|
2005-01-23 19:04:34 +01:00
|
|
|
_PIFV pre_main_end[] = { 0 };
|
2005-06-21 18:44:12 +02:00
|
|
|
#pragma data_seg(WIN_CALLBACK_POST_ATEXIT(a))
|
2004-06-13 18:06:23 +02:00
|
|
|
_PIFV shutdown_begin[] = { 0 };
|
2005-06-21 18:44:12 +02:00
|
|
|
#pragma data_seg(WIN_CALLBACK_POST_ATEXIT(z))
|
2004-06-13 18:06:23 +02:00
|
|
|
_PIFV shutdown_end[] = { 0 };
|
|
|
|
#pragma data_seg()
|
|
|
|
|
|
|
|
#pragma comment(linker, "/merge:.LIB=.data")
|
|
|
|
|
|
|
|
// call all non-NULL function pointers in [begin, end).
|
|
|
|
// note: the range may be larger than expected due to section padding.
|
|
|
|
// that (and the COFF empty section problem) is why we need to ignore zeroes.
|
|
|
|
static void call_func_tbl(_PIFV* begin, _PIFV* end)
|
2004-05-27 19:30:06 +02:00
|
|
|
{
|
2004-06-13 18:06:23 +02:00
|
|
|
for(_PIFV* p = begin; p < end; p++)
|
|
|
|
if(*p)
|
|
|
|
(*p)();
|
2004-05-27 19:30:06 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-06-22 05:23:22 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
2004-03-03 00:56:51 +01:00
|
|
|
// locking for win-specific code
|
2005-06-22 05:23:22 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
2005-05-12 00:40:19 +02:00
|
|
|
|
2005-06-21 18:44:12 +02:00
|
|
|
// several init functions are before called before _cinit.
|
|
|
|
// POSIX static mutex init may not have been done by then,
|
|
|
|
// so we need our own lightweight functions.
|
2004-03-03 00:56:51 +01:00
|
|
|
|
|
|
|
static CRITICAL_SECTION cs[NUM_CS];
|
2005-01-23 19:04:34 +01:00
|
|
|
static bool cs_valid;
|
2004-03-03 00:56:51 +01:00
|
|
|
|
|
|
|
void win_lock(uint idx)
|
|
|
|
{
|
2005-06-28 06:12:50 +02:00
|
|
|
debug_assert(idx < NUM_CS && "win_lock: invalid critical section index");
|
2005-01-23 19:04:34 +01:00
|
|
|
if(cs_valid)
|
|
|
|
EnterCriticalSection(&cs[idx]);
|
2004-03-03 00:56:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
void win_unlock(uint idx)
|
|
|
|
{
|
2005-06-28 06:12:50 +02:00
|
|
|
debug_assert(idx < NUM_CS && "win_unlock: invalid critical section index");
|
2005-01-23 19:04:34 +01:00
|
|
|
if(cs_valid)
|
|
|
|
LeaveCriticalSection(&cs[idx]);
|
|
|
|
}
|
|
|
|
|
2005-06-21 18:44:12 +02:00
|
|
|
int win_is_locked(uint idx)
|
|
|
|
{
|
2005-06-28 06:12:50 +02:00
|
|
|
debug_assert(idx < NUM_CS && "win_is_locked: invalid critical section index");
|
2005-06-21 18:44:12 +02:00
|
|
|
if(!cs_valid)
|
|
|
|
return -1;
|
|
|
|
BOOL got_it = TryEnterCriticalSection(&cs[idx]);
|
|
|
|
if(got_it)
|
|
|
|
LeaveCriticalSection(&cs[idx]);
|
|
|
|
return !got_it;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-01-23 19:04:34 +01:00
|
|
|
static void cs_init()
|
|
|
|
{
|
|
|
|
for(int i = 0; i < NUM_CS; i++)
|
|
|
|
InitializeCriticalSection(&cs[i]);
|
|
|
|
|
|
|
|
cs_valid = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void cs_shutdown()
|
|
|
|
{
|
|
|
|
cs_valid = false;
|
|
|
|
|
|
|
|
for(int i = 0; i < NUM_CS; i++)
|
|
|
|
DeleteCriticalSection(&cs[i]);
|
|
|
|
memset(cs, 0, sizeof(cs));
|
2004-03-03 00:56:51 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-06-22 05:23:22 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
2005-05-12 00:40:19 +02:00
|
|
|
// startup
|
2005-06-22 05:23:22 +02:00
|
|
|
//-----------------------------------------------------------------------------
|
2004-03-03 00:56:51 +01:00
|
|
|
|
|
|
|
// entry -> pre_libc -> WinMainCRTStartup -> WinMain -> pre_main -> main
|
|
|
|
// at_exit is called as the last of the atexit handlers
|
|
|
|
// (assuming, as documented in lib.cpp, constructors don't use atexit!)
|
|
|
|
//
|
2005-06-21 18:44:12 +02:00
|
|
|
// rationale: we need to gain control after _cinit and before main() to
|
|
|
|
// complete initialization.
|
2004-03-03 00:56:51 +01:00
|
|
|
// note: this way of getting control before main adds overhead
|
|
|
|
// (setting up the WinMain parameters), but is simpler and safer than
|
|
|
|
// SDL-style #define main SDL_main.
|
|
|
|
|
2005-07-16 19:52:05 +02:00
|
|
|
// explained where used.
|
|
|
|
static HMODULE hUser32Dll;
|
|
|
|
|
2004-03-03 00:56:51 +01:00
|
|
|
static void at_exit(void)
|
|
|
|
{
|
2004-06-13 18:06:23 +02:00
|
|
|
call_func_tbl(shutdown_begin, shutdown_end);
|
2004-05-27 19:30:06 +02:00
|
|
|
|
2005-01-23 19:04:34 +01:00
|
|
|
cs_shutdown();
|
2004-03-03 00:56:51 +01:00
|
|
|
|
2005-07-16 19:52:05 +02:00
|
|
|
// free the reference taken in win_pre_libc_init;
|
|
|
|
// this avoids Boundschecker warnings at exit.
|
|
|
|
FreeLibrary(hUser32Dll);
|
2005-06-25 09:54:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
2005-06-21 18:44:12 +02:00
|
|
|
void win_pre_main_init()
|
|
|
|
{
|
2005-06-22 05:23:22 +02:00
|
|
|
// enable memory tracking and leak detection;
|
2005-08-09 18:23:19 +02:00
|
|
|
// no effect if !HAVE_VC_DEBUG_ALLOC.
|
|
|
|
#if CONFIG_PARANOIA
|
2005-06-22 05:23:22 +02:00
|
|
|
debug_heap_enable(DEBUG_HEAP_ALL);
|
2005-07-16 19:52:05 +02:00
|
|
|
#elif !defined(NDEBUG)
|
2005-06-22 05:23:22 +02:00
|
|
|
debug_heap_enable(DEBUG_HEAP_NORMAL);
|
|
|
|
#endif
|
2005-06-21 18:44:12 +02:00
|
|
|
|
|
|
|
call_func_tbl(pre_main_begin, pre_main_end);
|
|
|
|
|
|
|
|
atexit(at_exit);
|
|
|
|
|
|
|
|
// no point redirecting stdout yet - the current directory
|
|
|
|
// may be incorrect (file_set_root not yet called).
|
|
|
|
// (w)sdl will take care of it anyway.
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
// perform all initialization that needs to run before _cinit
|
|
|
|
// (which calls C++ ctors).
|
2004-03-03 00:56:51 +01:00
|
|
|
// be very careful to avoid non-stateless libc functions!
|
|
|
|
static inline void pre_libc_init()
|
|
|
|
{
|
2005-02-02 04:26:38 +01:00
|
|
|
// enable low-fragmentation heap
|
2005-06-21 18:44:12 +02:00
|
|
|
#if WINVER >= 0x0501
|
2005-02-02 04:26:38 +01:00
|
|
|
HMODULE hKernel32Dll = LoadLibrary("kernel32.dll");
|
|
|
|
if(hKernel32Dll)
|
|
|
|
{
|
|
|
|
BOOL (WINAPI* pHeapSetInformation)(HANDLE, HEAP_INFORMATION_CLASS, void*, size_t);
|
|
|
|
*(void**)&pHeapSetInformation = GetProcAddress(hKernel32Dll, "HeapSetInformation");
|
|
|
|
if(pHeapSetInformation)
|
|
|
|
{
|
|
|
|
ULONG flags = 2; // enable LFH
|
2005-05-06 22:14:40 +02:00
|
|
|
pHeapSetInformation(GetProcessHeap(), HeapCompatibilityInformation, &flags, sizeof(flags));
|
2005-02-02 04:26:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
FreeLibrary(hKernel32Dll);
|
|
|
|
}
|
|
|
|
#endif // #if WINVER >= 0x0501
|
|
|
|
|
2005-01-23 19:04:34 +01:00
|
|
|
cs_init();
|
2004-11-19 23:41:37 +01:00
|
|
|
|
|
|
|
GetSystemDirectory(win_sys_dir, sizeof(win_sys_dir));
|
2004-11-22 08:40:32 +01:00
|
|
|
|
|
|
|
if(GetModuleFileName(GetModuleHandle(0), win_exe_dir, MAX_PATH) != 0)
|
2006-04-22 18:26:16 +02:00
|
|
|
path_strip_fn(win_exe_dir);
|
2004-11-22 08:40:32 +01:00
|
|
|
|
2005-07-16 19:52:05 +02:00
|
|
|
// HACK: make sure a reference to user32 is held, even if someone
|
|
|
|
// decides to delay-load it. this fixes bug #66, which was the
|
|
|
|
// Win32 mouse cursor (set via user32!SetCursor) appearing as a
|
|
|
|
// black 32x32(?) rectangle. underlying cause was as follows:
|
|
|
|
// powrprof.dll was the first client of user32, causing it to be
|
|
|
|
// loaded. after we were finished with powrprof, we freed it, in turn
|
|
|
|
// causing user32 to unload. later code would then reload user32,
|
|
|
|
// which apparently terminally confused the cursor implementation.
|
|
|
|
//
|
|
|
|
// since we hold a reference here, user32 will never unload.
|
|
|
|
// of course, the benefits of delay-loading are lost for this DLL,
|
|
|
|
// but that is unavoidable. it is safer to force loading it, rather
|
|
|
|
// than documenting the problem and asking it not be delay-loaded.
|
|
|
|
hUser32Dll = LoadLibrary("user32.dll");
|
|
|
|
|
2005-01-23 19:04:34 +01:00
|
|
|
call_func_tbl(pre_libc_begin, pre_libc_end);
|
2004-03-03 00:56:51 +01:00
|
|
|
}
|
|
|
|
|
2005-05-12 00:40:19 +02:00
|
|
|
|
2006-02-16 21:23:27 +01:00
|
|
|
static int SEH_wrapped_entry()
|
2004-03-03 00:56:51 +01:00
|
|
|
{
|
2006-02-16 21:23:27 +01:00
|
|
|
int ret;
|
2006-07-20 16:37:58 +02:00
|
|
|
//__try
|
2005-06-21 18:44:12 +02:00
|
|
|
{
|
|
|
|
pre_libc_init();
|
2005-06-26 19:12:48 +02:00
|
|
|
#ifdef USE_WINMAIN
|
|
|
|
ret = WinMainCRTStartup(); // calls _cinit and then our main
|
|
|
|
#else
|
2005-06-21 18:44:12 +02:00
|
|
|
ret = mainCRTStartup(); // calls _cinit and then our main
|
2005-06-26 19:12:48 +02:00
|
|
|
#endif
|
2005-06-21 18:44:12 +02:00
|
|
|
}
|
2006-07-20 16:37:58 +02:00
|
|
|
//__except(wdbg_exception_filter(GetExceptionInformation()))
|
2005-06-21 18:44:12 +02:00
|
|
|
{
|
2006-02-16 21:23:27 +01:00
|
|
|
ret = -1;
|
2005-06-21 18:44:12 +02:00
|
|
|
}
|
|
|
|
return ret;
|
2004-03-03 00:56:51 +01:00
|
|
|
}
|
2006-02-16 21:23:27 +01:00
|
|
|
|
|
|
|
|
|
|
|
int entry()
|
|
|
|
{
|
2006-02-16 21:45:38 +01:00
|
|
|
#ifdef NEED_COOKIE_INIT
|
2006-02-16 21:23:27 +01:00
|
|
|
// 2006-02-16 workaround for R6035 on VC8:
|
|
|
|
//
|
|
|
|
// SEH code compiled with /GS pushes a "security cookie" onto the
|
|
|
|
// stack. since we're called before _cinit, the cookie won't have
|
|
|
|
// been initialized yet, which would cause the CRT to FatalAppExit.
|
|
|
|
// to solve this, we must call __security_init_cookie before any
|
|
|
|
// hidden compiler-generated SEH registration code runs,
|
|
|
|
// which means the __try block must be moved into a helper function.
|
|
|
|
//
|
|
|
|
// NB: entry() must not contain local string buffers, either -
|
|
|
|
// /GS would install a cookie here as well (same problem).
|
|
|
|
//
|
|
|
|
// see http://msdn2.microsoft.com/en-US/library/ms235603.aspx
|
|
|
|
__security_init_cookie();
|
2006-02-16 21:45:38 +01:00
|
|
|
#endif
|
2006-02-16 21:23:27 +01:00
|
|
|
return SEH_wrapped_entry();
|
|
|
|
}
|