2022-01-30 22:53:30 -08:00
|
|
|
/* Copyright (C) 2022 Wildfire Games.
|
2009-04-18 10:00:33 -07:00
|
|
|
*
|
2010-02-08 08:23:39 -08:00
|
|
|
* Permission is hereby granted, free of charge, to any person obtaining
|
|
|
|
|
* a copy of this software and associated documentation files (the
|
|
|
|
|
* "Software"), to deal in the Software without restriction, including
|
|
|
|
|
* without limitation the rights to use, copy, modify, merge, publish,
|
|
|
|
|
* distribute, sublicense, and/or sell copies of the Software, and to
|
|
|
|
|
* permit persons to whom the Software is furnished to do so, subject to
|
|
|
|
|
* the following conditions:
|
2016-11-23 05:02:58 -08:00
|
|
|
*
|
2010-02-08 08:23:39 -08:00
|
|
|
* The above copyright notice and this permission notice shall be included
|
|
|
|
|
* in all copies or substantial portions of the Software.
|
2016-11-23 05:02:58 -08:00
|
|
|
*
|
2010-02-08 08:23:39 -08:00
|
|
|
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
|
|
|
|
|
* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
|
|
|
|
|
* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
|
|
|
|
|
* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY
|
|
|
|
|
* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
|
|
|
|
|
* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
|
|
|
|
|
* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
|
2009-04-18 10:00:33 -07:00
|
|
|
*/
|
|
|
|
|
|
2004-05-07 18:11:51 -07:00
|
|
|
#include "precompiled.h"
|
2010-03-01 06:52:58 -08:00
|
|
|
#include "lib/sysdep/os/win/wutil.h"
|
2004-05-07 18:11:51 -07:00
|
|
|
|
2004-06-04 05:41:53 -07:00
|
|
|
#include <stdio.h>
|
|
|
|
|
#include <stdlib.h> // __argc
|
2005-05-31 12:00:49 -07:00
|
|
|
|
2010-07-08 03:18:42 -07:00
|
|
|
#include "lib/file/file.h"
|
2007-04-25 11:19:35 -07:00
|
|
|
#include "lib/posix/posix.h"
|
2011-03-23 09:14:47 -07:00
|
|
|
#include "lib/sysdep/sysdep.h"
|
2010-03-01 06:52:58 -08:00
|
|
|
#include "lib/sysdep/os/win/win.h"
|
2010-07-14 05:23:53 -07:00
|
|
|
#include "lib/sysdep/os/win/wdbg.h" // wdbg_assert
|
2010-03-01 06:52:58 -08:00
|
|
|
#include "lib/sysdep/os/win/winit.h"
|
2005-01-29 08:43:46 -08:00
|
|
|
|
2009-08-02 04:07:42 -07:00
|
|
|
#include <shlobj.h> // SHGetFolderPath
|
|
|
|
|
|
2021-06-05 12:01:37 -07:00
|
|
|
#include <SDL_loadso.h>
|
2021-06-06 08:31:55 -07:00
|
|
|
#include <SDL_syswm.h>
|
2021-06-05 12:01:37 -07:00
|
|
|
|
2009-08-02 04:07:42 -07:00
|
|
|
|
2007-06-04 15:59:14 -07:00
|
|
|
WINIT_REGISTER_EARLY_INIT(wutil_Init);
|
|
|
|
|
WINIT_REGISTER_LATE_SHUTDOWN(wutil_Shutdown);
|
2004-07-12 07:25:39 -07:00
|
|
|
|
2007-06-24 06:24:40 -07:00
|
|
|
|
2021-01-10 00:39:54 -08:00
|
|
|
// Defined in ps/Pyrogenesis.h
|
|
|
|
|
extern const char* main_window_name;
|
|
|
|
|
|
2005-06-21 20:23:22 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
2007-05-23 19:32:53 -07:00
|
|
|
// safe allocator
|
2005-06-21 09:44:12 -07:00
|
|
|
|
2010-07-08 03:18:42 -07:00
|
|
|
// may be used independently of libc malloc
|
2005-01-30 09:40:24 -08:00
|
|
|
// (in particular, before _cinit and while calling static dtors).
|
|
|
|
|
// used by wpthread critical section code.
|
|
|
|
|
|
2010-07-08 03:18:42 -07:00
|
|
|
void* wutil_Allocate(size_t size)
|
2005-01-30 09:40:24 -08:00
|
|
|
{
|
|
|
|
|
const DWORD flags = HEAP_ZERO_MEMORY;
|
|
|
|
|
return HeapAlloc(GetProcessHeap(), flags, size);
|
|
|
|
|
}
|
|
|
|
|
|
2010-07-08 03:18:42 -07:00
|
|
|
void wutil_Free(void* p)
|
2005-01-30 09:40:24 -08:00
|
|
|
{
|
|
|
|
|
const DWORD flags = 0;
|
|
|
|
|
HeapFree(GetProcessHeap(), flags, p);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2005-06-21 20:23:22 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
2007-05-23 19:32:53 -07:00
|
|
|
// locks
|
2005-05-11 15:40:19 -07:00
|
|
|
|
2005-06-21 09:44:12 -07: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-02 15:56:51 -08:00
|
|
|
|
|
|
|
|
static CRITICAL_SECTION cs[NUM_CS];
|
2005-01-23 10:04:34 -08:00
|
|
|
static bool cs_valid;
|
2004-03-02 15:56:51 -08:00
|
|
|
|
2010-07-08 03:18:42 -07:00
|
|
|
void wutil_Lock(WinLockId id)
|
2004-03-02 15:56:51 -08:00
|
|
|
{
|
2008-01-19 03:33:11 -08:00
|
|
|
if(!cs_valid)
|
|
|
|
|
return;
|
|
|
|
|
EnterCriticalSection(&cs[id]);
|
2004-03-02 15:56:51 -08:00
|
|
|
}
|
|
|
|
|
|
2010-07-08 03:18:42 -07:00
|
|
|
void wutil_Unlock(WinLockId id)
|
2004-03-02 15:56:51 -08:00
|
|
|
{
|
2008-01-19 03:33:11 -08:00
|
|
|
if(!cs_valid)
|
|
|
|
|
return;
|
|
|
|
|
LeaveCriticalSection(&cs[id]);
|
2005-01-23 10:04:34 -08:00
|
|
|
}
|
|
|
|
|
|
2010-07-08 03:18:42 -07:00
|
|
|
bool wutil_IsLocked(WinLockId id)
|
2005-06-21 09:44:12 -07:00
|
|
|
{
|
|
|
|
|
if(!cs_valid)
|
2008-01-19 03:33:11 -08:00
|
|
|
return false;
|
|
|
|
|
const BOOL successfullyEntered = TryEnterCriticalSection(&cs[id]);
|
|
|
|
|
if(!successfullyEntered)
|
|
|
|
|
return true; // still locked
|
|
|
|
|
LeaveCriticalSection(&cs[id]);
|
|
|
|
|
return false; // probably not locked
|
2005-06-21 09:44:12 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-05-04 10:30:32 -07:00
|
|
|
static void InitLocks()
|
2005-01-23 10:04:34 -08:00
|
|
|
{
|
|
|
|
|
for(int i = 0; i < NUM_CS; i++)
|
|
|
|
|
InitializeCriticalSection(&cs[i]);
|
|
|
|
|
|
|
|
|
|
cs_valid = true;
|
|
|
|
|
}
|
|
|
|
|
|
2007-05-04 10:30:32 -07:00
|
|
|
static void ShutdownLocks()
|
2005-01-23 10:04:34 -08:00
|
|
|
{
|
|
|
|
|
cs_valid = false;
|
|
|
|
|
|
|
|
|
|
for(int i = 0; i < NUM_CS; i++)
|
|
|
|
|
DeleteCriticalSection(&cs[i]);
|
|
|
|
|
memset(cs, 0, sizeof(cs));
|
2004-03-02 15:56:51 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2005-06-21 20:23:22 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
2007-05-23 19:32:53 -07:00
|
|
|
// error codes
|
|
|
|
|
|
|
|
|
|
// only call after a Win32 function indicates failure.
|
2011-05-03 05:38:42 -07:00
|
|
|
Status StatusFromWin()
|
2007-05-23 19:32:53 -07:00
|
|
|
{
|
|
|
|
|
switch(GetLastError())
|
|
|
|
|
{
|
2011-05-06 11:45:30 -07:00
|
|
|
case ERROR_BUSY:
|
|
|
|
|
case WAIT_TIMEOUT:
|
|
|
|
|
return ERR::AGAIN;
|
|
|
|
|
case ERROR_OPERATION_ABORTED:
|
|
|
|
|
return ERR::ABORTED;
|
|
|
|
|
|
2010-07-08 03:18:42 -07:00
|
|
|
case ERROR_INVALID_HANDLE:
|
2011-05-06 11:45:30 -07:00
|
|
|
return ERR::INVALID_HANDLE;
|
|
|
|
|
case ERROR_INSUFFICIENT_BUFFER:
|
|
|
|
|
return ERR::INVALID_SIZE;
|
2007-05-23 19:32:53 -07:00
|
|
|
case ERROR_INVALID_PARAMETER:
|
2010-07-08 03:18:42 -07:00
|
|
|
case ERROR_BAD_ARGUMENTS:
|
2011-05-03 05:38:42 -07:00
|
|
|
return ERR::INVALID_PARAM;
|
2011-05-06 11:45:30 -07:00
|
|
|
|
|
|
|
|
case ERROR_OUTOFMEMORY:
|
|
|
|
|
case ERROR_NOT_ENOUGH_MEMORY:
|
|
|
|
|
return ERR::NO_MEM;
|
2010-07-08 03:18:42 -07:00
|
|
|
case ERROR_NOT_SUPPORTED:
|
|
|
|
|
case ERROR_CALL_NOT_IMPLEMENTED:
|
|
|
|
|
case ERROR_PROC_NOT_FOUND:
|
2011-05-05 06:03:34 -07:00
|
|
|
return ERR::NOT_SUPPORTED;
|
2011-05-06 11:45:30 -07:00
|
|
|
|
2007-05-23 19:32:53 -07:00
|
|
|
case ERROR_FILE_NOT_FOUND:
|
|
|
|
|
case ERROR_PATH_NOT_FOUND:
|
2011-05-05 06:03:34 -07:00
|
|
|
return ERR::FILE_NOT_FOUND;
|
2011-05-06 11:45:30 -07:00
|
|
|
case ERROR_ACCESS_DENIED:
|
|
|
|
|
return ERR::FILE_ACCESS;
|
|
|
|
|
|
2010-07-08 03:18:42 -07:00
|
|
|
default:
|
2011-05-03 05:38:42 -07:00
|
|
|
return ERR::FAIL;
|
2007-05-23 19:32:53 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// directories
|
|
|
|
|
|
2009-11-03 13:46:35 -08:00
|
|
|
// (NB: wutil_Init is called before static ctors => use placement new)
|
2011-03-23 06:36:20 -07:00
|
|
|
static OsPath* systemPath;
|
|
|
|
|
static OsPath* executablePath;
|
2012-03-21 17:23:31 -07:00
|
|
|
static OsPath* localAppdataPath;
|
|
|
|
|
static OsPath* roamingAppdataPath;
|
|
|
|
|
static OsPath* personalPath;
|
2009-11-03 13:46:35 -08:00
|
|
|
|
2011-03-23 06:36:20 -07:00
|
|
|
const OsPath& wutil_SystemPath()
|
2009-11-03 13:46:35 -08:00
|
|
|
{
|
|
|
|
|
return *systemPath;
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-23 06:36:20 -07:00
|
|
|
const OsPath& wutil_ExecutablePath()
|
2009-11-03 13:46:35 -08:00
|
|
|
{
|
|
|
|
|
return *executablePath;
|
|
|
|
|
}
|
|
|
|
|
|
2012-03-21 17:23:31 -07:00
|
|
|
const OsPath& wutil_LocalAppdataPath()
|
2009-11-03 13:46:35 -08:00
|
|
|
{
|
2012-03-21 17:23:31 -07:00
|
|
|
return *localAppdataPath;
|
2009-11-03 13:46:35 -08:00
|
|
|
}
|
|
|
|
|
|
2012-03-21 17:23:31 -07:00
|
|
|
const OsPath& wutil_RoamingAppdataPath()
|
|
|
|
|
{
|
|
|
|
|
return *roamingAppdataPath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
const OsPath& wutil_PersonalPath()
|
|
|
|
|
{
|
|
|
|
|
return *personalPath;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// Helper to avoid duplicating this setup
|
|
|
|
|
static OsPath* GetFolderPath(int csidl)
|
|
|
|
|
{
|
|
|
|
|
HWND hwnd = 0; // ignored unless a dial-up connection is needed to access the folder
|
|
|
|
|
HANDLE token = 0;
|
|
|
|
|
wchar_t path[MAX_PATH]; // mandated by SHGetFolderPathW
|
|
|
|
|
const HRESULT ret = SHGetFolderPathW(hwnd, csidl, token, 0, path);
|
2015-05-26 19:02:32 -07:00
|
|
|
if (!SUCCEEDED(ret))
|
|
|
|
|
{
|
|
|
|
|
debug_printf("SHGetFolderPathW failed with HRESULT = 0x%08lx for csidl = 0x%04x\n", ret, csidl);
|
|
|
|
|
debug_warn("SHGetFolderPathW failed (see debug output)");
|
|
|
|
|
}
|
2012-03-21 17:23:31 -07:00
|
|
|
if(GetLastError() == ERROR_NO_TOKEN) // avoid polluting last error
|
|
|
|
|
SetLastError(0);
|
|
|
|
|
return new(wutil_Allocate(sizeof(OsPath))) OsPath(path);
|
|
|
|
|
}
|
2007-05-23 19:32:53 -07:00
|
|
|
|
|
|
|
|
static void GetDirectories()
|
|
|
|
|
{
|
2009-08-02 04:07:42 -07:00
|
|
|
WinScopedPreserveLastError s;
|
|
|
|
|
|
|
|
|
|
// system directory
|
|
|
|
|
{
|
2011-03-23 09:14:47 -07:00
|
|
|
const UINT length = GetSystemDirectoryW(0, 0);
|
2011-04-30 06:01:45 -07:00
|
|
|
ENSURE(length != 0);
|
2011-03-23 09:14:47 -07:00
|
|
|
std::wstring path(length, '\0');
|
|
|
|
|
const UINT charsWritten = GetSystemDirectoryW(&path[0], length);
|
2011-04-30 06:01:45 -07:00
|
|
|
ENSURE(charsWritten == length-1);
|
2011-03-23 06:36:20 -07:00
|
|
|
systemPath = new(wutil_Allocate(sizeof(OsPath))) OsPath(path);
|
2009-08-02 04:07:42 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// executable's directory
|
2011-03-23 09:14:47 -07:00
|
|
|
executablePath = new(wutil_Allocate(sizeof(OsPath))) OsPath(sys_ExecutablePathname().Parent());
|
2009-08-02 04:07:42 -07:00
|
|
|
|
2012-03-21 17:23:31 -07:00
|
|
|
// roaming application data
|
|
|
|
|
roamingAppdataPath = GetFolderPath(CSIDL_APPDATA);
|
|
|
|
|
|
|
|
|
|
// local application data
|
|
|
|
|
localAppdataPath = GetFolderPath(CSIDL_LOCAL_APPDATA);
|
|
|
|
|
|
|
|
|
|
// my documents
|
|
|
|
|
personalPath = GetFolderPath(CSIDL_PERSONAL);
|
2007-05-23 19:32:53 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2009-11-03 13:46:35 -08:00
|
|
|
static void FreeDirectories()
|
|
|
|
|
{
|
2011-03-23 06:36:20 -07:00
|
|
|
systemPath->~OsPath();
|
2010-07-08 03:18:42 -07:00
|
|
|
wutil_Free(systemPath);
|
2011-03-23 06:36:20 -07:00
|
|
|
executablePath->~OsPath();
|
2010-07-08 03:18:42 -07:00
|
|
|
wutil_Free(executablePath);
|
2012-03-21 17:23:31 -07:00
|
|
|
localAppdataPath->~OsPath();
|
|
|
|
|
wutil_Free(localAppdataPath);
|
|
|
|
|
roamingAppdataPath->~OsPath();
|
|
|
|
|
wutil_Free(roamingAppdataPath);
|
|
|
|
|
personalPath->~OsPath();
|
|
|
|
|
wutil_Free(personalPath);
|
2009-11-03 13:46:35 -08:00
|
|
|
}
|
|
|
|
|
|
2007-05-23 19:32:53 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// memory
|
|
|
|
|
|
|
|
|
|
static void EnableLowFragmentationHeap()
|
|
|
|
|
{
|
2011-05-03 06:46:35 -07:00
|
|
|
if(IsDebuggerPresent())
|
|
|
|
|
{
|
|
|
|
|
// and the debug heap isn't explicitly disabled,
|
|
|
|
|
char* var = getenv("_NO_DEBUG_HEAP");
|
|
|
|
|
if(!var || var[0] != '1')
|
|
|
|
|
return; // we can't enable the LFH
|
|
|
|
|
}
|
|
|
|
|
|
2005-06-21 09:44:12 -07:00
|
|
|
#if WINVER >= 0x0501
|
2010-07-08 03:18:42 -07:00
|
|
|
WUTIL_FUNC(pHeapSetInformation, BOOL, (HANDLE, HEAP_INFORMATION_CLASS, void*, size_t));
|
|
|
|
|
WUTIL_IMPORT_KERNEL32(HeapSetInformation, pHeapSetInformation);
|
|
|
|
|
if(pHeapSetInformation)
|
|
|
|
|
{
|
|
|
|
|
ULONG flags = 2; // enable LFH
|
|
|
|
|
pHeapSetInformation(GetProcessHeap(), HeapCompatibilityInformation, &flags, sizeof(flags));
|
|
|
|
|
}
|
2007-05-23 19:32:53 -07:00
|
|
|
#endif // #if WINVER >= 0x0501
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2007-05-24 05:31:16 -07:00
|
|
|
// Wow64
|
|
|
|
|
|
|
|
|
|
// Wow64 'helpfully' redirects all 32-bit apps' accesses of
|
|
|
|
|
// %windir%\\system32\\drivers to %windir%\\system32\\drivers\\SysWOW64.
|
|
|
|
|
// that's bad, because the actual drivers are not in the subdirectory. to
|
|
|
|
|
// work around this, provide for temporarily disabling redirection.
|
|
|
|
|
|
2010-07-08 03:18:42 -07:00
|
|
|
static WUTIL_FUNC(pIsWow64Process, BOOL, (HANDLE, PBOOL));
|
|
|
|
|
static WUTIL_FUNC(pWow64DisableWow64FsRedirection, BOOL, (PVOID*));
|
|
|
|
|
static WUTIL_FUNC(pWow64RevertWow64FsRedirection, BOOL, (PVOID));
|
2007-05-23 19:32:53 -07:00
|
|
|
|
|
|
|
|
static bool isWow64;
|
|
|
|
|
|
2007-05-24 05:31:16 -07:00
|
|
|
static void ImportWow64Functions()
|
2007-05-23 19:32:53 -07:00
|
|
|
{
|
2010-07-08 03:18:42 -07:00
|
|
|
WUTIL_IMPORT_KERNEL32(IsWow64Process, pIsWow64Process);
|
|
|
|
|
WUTIL_IMPORT_KERNEL32(Wow64DisableWow64FsRedirection, pWow64DisableWow64FsRedirection);
|
|
|
|
|
WUTIL_IMPORT_KERNEL32(Wow64RevertWow64FsRedirection, pWow64RevertWow64FsRedirection);
|
2007-05-24 05:31:16 -07:00
|
|
|
}
|
2007-05-23 19:32:53 -07:00
|
|
|
|
2007-05-24 05:31:16 -07:00
|
|
|
static void DetectWow64()
|
|
|
|
|
{
|
2007-05-23 19:32:53 -07:00
|
|
|
// function not found => running on 32-bit Windows
|
|
|
|
|
if(!pIsWow64Process)
|
2005-02-01 19:26:38 -08:00
|
|
|
{
|
2007-05-23 19:32:53 -07:00
|
|
|
isWow64 = false;
|
|
|
|
|
return;
|
2005-02-01 19:26:38 -08:00
|
|
|
}
|
|
|
|
|
|
2007-05-23 19:32:53 -07:00
|
|
|
BOOL isWow64Process = FALSE;
|
2007-06-08 19:40:48 -07:00
|
|
|
const BOOL ok = pIsWow64Process(GetCurrentProcess(), &isWow64Process);
|
2007-05-23 19:32:53 -07:00
|
|
|
WARN_IF_FALSE(ok);
|
|
|
|
|
isWow64 = (isWow64Process == TRUE);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool wutil_IsWow64()
|
|
|
|
|
{
|
|
|
|
|
return isWow64;
|
|
|
|
|
}
|
|
|
|
|
|
2007-12-22 10:15:52 -08:00
|
|
|
|
|
|
|
|
WinScopedDisableWow64Redirection::WinScopedDisableWow64Redirection()
|
2007-05-24 05:31:16 -07:00
|
|
|
{
|
|
|
|
|
// note: don't just check if the function pointers are valid. 32-bit
|
|
|
|
|
// Vista includes them but isn't running Wow64, so calling the functions
|
|
|
|
|
// would fail. since we have to check if actually on Wow64, there's no
|
|
|
|
|
// more need to verify the pointers (their existence is implied).
|
|
|
|
|
if(!wutil_IsWow64())
|
|
|
|
|
return;
|
2010-07-08 03:18:42 -07:00
|
|
|
const BOOL ok = pWow64DisableWow64FsRedirection(&m_wasRedirectionEnabled);
|
2007-05-24 05:31:16 -07:00
|
|
|
WARN_IF_FALSE(ok);
|
|
|
|
|
}
|
|
|
|
|
|
2007-12-22 10:15:52 -08:00
|
|
|
WinScopedDisableWow64Redirection::~WinScopedDisableWow64Redirection()
|
2007-05-24 05:31:16 -07:00
|
|
|
{
|
|
|
|
|
if(!wutil_IsWow64())
|
|
|
|
|
return;
|
2010-07-08 03:18:42 -07:00
|
|
|
const BOOL ok = pWow64RevertWow64FsRedirection(m_wasRedirectionEnabled);
|
2007-05-24 05:31:16 -07:00
|
|
|
WARN_IF_FALSE(ok);
|
|
|
|
|
}
|
|
|
|
|
|
2007-05-23 19:32:53 -07:00
|
|
|
|
2011-04-29 12:10:34 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
2011-05-03 05:38:42 -07:00
|
|
|
Status wutil_SetPrivilege(const wchar_t* privilege, bool enable)
|
2011-04-29 12:10:34 -07:00
|
|
|
{
|
|
|
|
|
WinScopedPreserveLastError s;
|
|
|
|
|
|
|
|
|
|
HANDLE hToken;
|
|
|
|
|
if(!OpenProcessToken(GetCurrentProcess(), TOKEN_ADJUST_PRIVILEGES|TOKEN_QUERY, &hToken))
|
|
|
|
|
return ERR::_1;
|
|
|
|
|
|
|
|
|
|
TOKEN_PRIVILEGES tp;
|
|
|
|
|
if (!LookupPrivilegeValueW(NULL, privilege, &tp.Privileges[0].Luid))
|
|
|
|
|
return ERR::_2;
|
|
|
|
|
tp.PrivilegeCount = 1;
|
|
|
|
|
tp.Privileges[0].Attributes = enable? SE_PRIVILEGE_ENABLED : 0;
|
|
|
|
|
|
|
|
|
|
SetLastError(0);
|
|
|
|
|
const BOOL ok = AdjustTokenPrivileges(hToken, FALSE, &tp, 0, 0, 0);
|
|
|
|
|
if(!ok || GetLastError() != 0)
|
|
|
|
|
return ERR::_3;
|
|
|
|
|
|
|
|
|
|
WARN_IF_FALSE(CloseHandle(hToken));
|
|
|
|
|
return INFO::OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2007-12-20 12:09:19 -08:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// module handle
|
|
|
|
|
|
2010-07-08 03:18:42 -07:00
|
|
|
HMODULE wutil_LibModuleHandle()
|
|
|
|
|
{
|
|
|
|
|
return GetModuleHandle(0);
|
|
|
|
|
}
|
2007-12-20 12:09:19 -08:00
|
|
|
|
2007-09-23 03:15:28 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// find main window
|
|
|
|
|
|
|
|
|
|
// this is required by the error dialog and clipboard code.
|
|
|
|
|
// note that calling from wutil_Init won't work, because the app will not
|
|
|
|
|
// have created its window by then.
|
|
|
|
|
|
|
|
|
|
static HWND hAppWindow;
|
|
|
|
|
|
2021-06-06 08:31:55 -07:00
|
|
|
void wutil_SetAppWindow(SDL_Window* window)
|
2007-09-23 03:15:28 -07:00
|
|
|
{
|
2021-06-06 08:31:55 -07:00
|
|
|
SDL_SysWMinfo wmInfo;
|
|
|
|
|
SDL_VERSION(&wmInfo.version);
|
|
|
|
|
if (SDL_GetWindowWMInfo(window, &wmInfo) && wmInfo.subsystem == SDL_SYSWM_WINDOWS)
|
|
|
|
|
hAppWindow = wmInfo.info.win.window;
|
|
|
|
|
}
|
2007-09-23 03:15:28 -07:00
|
|
|
|
2021-12-21 14:03:31 -08:00
|
|
|
void* wutil_GetAppHDC()
|
|
|
|
|
{
|
|
|
|
|
return GetDC(hAppWindow);
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-06 08:31:55 -07:00
|
|
|
void wutil_SetAppWindow(void* hwnd)
|
|
|
|
|
{
|
|
|
|
|
hAppWindow = reinterpret_cast<HWND>(hwnd);
|
2007-09-23 03:15:28 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
HWND wutil_AppWindow()
|
|
|
|
|
{
|
2021-06-07 11:45:28 -07:00
|
|
|
if (hAppWindow)
|
|
|
|
|
{
|
|
|
|
|
// In case of an assertion we might not receive a notification about the
|
|
|
|
|
// closed window. So check it in-place.
|
|
|
|
|
if (IsWindow(hAppWindow))
|
|
|
|
|
{
|
|
|
|
|
// There is a chance that a new window might be opened with the
|
|
|
|
|
// same handle.
|
|
|
|
|
DWORD pid;
|
|
|
|
|
GetWindowThreadProcessId(hAppWindow, &pid);
|
|
|
|
|
if (pid != GetCurrentProcessId())
|
|
|
|
|
hAppWindow = 0;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
hAppWindow = 0;
|
|
|
|
|
}
|
2007-09-23 03:15:28 -07:00
|
|
|
return hAppWindow;
|
|
|
|
|
}
|
|
|
|
|
|
2021-06-05 12:01:37 -07:00
|
|
|
void wutil_EnableHiDPIOnWindows()
|
|
|
|
|
{
|
|
|
|
|
// We build with VS using XP toolkit which doesn't support DPI awareness.
|
|
|
|
|
// It was introduced in 8.1.
|
|
|
|
|
// https://docs.microsoft.com/en-us/windows/win32/api/shellscalingapi/ne-shellscalingapi-process_dpi_awareness
|
|
|
|
|
typedef enum PROCESS_DPI_AWARENESS {
|
|
|
|
|
PROCESS_DPI_UNAWARE,
|
|
|
|
|
PROCESS_SYSTEM_DPI_AWARE,
|
|
|
|
|
PROCESS_PER_MONITOR_DPI_AWARE
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
// https://docs.microsoft.com/en-us/windows/win32/api/shellscalingapi/nf-shellscalingapi-setprocessdpiawareness
|
|
|
|
|
using SetProcessDpiAwarenessFunc = HRESULT(WINAPI *)(PROCESS_DPI_AWARENESS);
|
|
|
|
|
void* shcoreDLL = SDL_LoadObject("SHCORE.DLL");
|
|
|
|
|
if (!shcoreDLL)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
SetProcessDpiAwarenessFunc SetProcessDpiAwareness =
|
|
|
|
|
reinterpret_cast<SetProcessDpiAwarenessFunc>(SDL_LoadFunction(shcoreDLL, "SetProcessDpiAwareness"));
|
|
|
|
|
if (SetProcessDpiAwareness)
|
|
|
|
|
SetProcessDpiAwareness(PROCESS_PER_MONITOR_DPI_AWARE);
|
|
|
|
|
|
|
|
|
|
SDL_UnloadObject(shcoreDLL);
|
|
|
|
|
}
|
2007-09-23 03:15:28 -07:00
|
|
|
|
2007-05-23 19:32:53 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
2011-05-03 05:38:42 -07:00
|
|
|
static Status wutil_Init()
|
2007-05-23 19:32:53 -07:00
|
|
|
{
|
2007-05-04 10:30:32 -07:00
|
|
|
InitLocks();
|
2004-11-19 14:41:37 -08:00
|
|
|
|
2007-05-23 19:32:53 -07:00
|
|
|
EnableLowFragmentationHeap();
|
|
|
|
|
|
|
|
|
|
GetDirectories();
|
|
|
|
|
|
2007-05-24 05:31:16 -07:00
|
|
|
ImportWow64Functions();
|
2007-05-23 19:32:53 -07:00
|
|
|
DetectWow64();
|
2005-07-16 10:52:05 -07:00
|
|
|
|
2007-05-04 10:30:32 -07:00
|
|
|
return INFO::OK;
|
2004-03-02 15:56:51 -08:00
|
|
|
}
|
|
|
|
|
|
2005-05-11 15:40:19 -07:00
|
|
|
|
2011-05-03 05:38:42 -07:00
|
|
|
static Status wutil_Shutdown()
|
2004-03-02 15:56:51 -08:00
|
|
|
{
|
2007-05-23 19:32:53 -07:00
|
|
|
ShutdownLocks();
|
2006-12-09 06:39:52 -08:00
|
|
|
|
2009-11-03 13:46:35 -08:00
|
|
|
FreeDirectories();
|
|
|
|
|
|
2007-05-04 10:30:32 -07:00
|
|
|
return INFO::OK;
|
2006-12-09 06:39:52 -08:00
|
|
|
}
|