2025-02-09 11:31:32 -08:00
|
|
|
/* Copyright (C) 2025 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
|
|
|
*/
|
|
|
|
|
|
2009-04-18 10:51:05 -07:00
|
|
|
/*
|
|
|
|
|
* Windows backend of the sysdep interface
|
2006-04-23 16:14:18 -07:00
|
|
|
*/
|
|
|
|
|
|
|
|
|
|
#include "precompiled.h"
|
2007-01-01 13:25:47 -08:00
|
|
|
#include "lib/sysdep/sysdep.h"
|
2006-04-23 16:14:18 -07:00
|
|
|
|
2011-04-30 06:22:46 -07:00
|
|
|
#include "lib/alignment.h"
|
2010-03-01 06:52:58 -08:00
|
|
|
#include "lib/sysdep/os/win/win.h" // includes windows.h; must come before shlobj
|
2020-05-05 04:18:00 -07:00
|
|
|
#include <SDL_clipboard.h> // SDL_SetClipboardText
|
2006-04-23 16:14:18 -07:00
|
|
|
#include <shlobj.h> // pick_dir
|
2010-07-07 15:23:18 -07:00
|
|
|
#include <shellapi.h> // open_url
|
2010-06-30 16:40:51 -07:00
|
|
|
#include <Wincrypt.h>
|
2010-12-09 03:16:21 -08:00
|
|
|
#include <WindowsX.h> // message crackers
|
2011-02-21 13:54:47 -08:00
|
|
|
#include <winhttp.h>
|
2006-04-23 16:14:18 -07:00
|
|
|
|
2010-03-01 06:52:58 -08:00
|
|
|
#include "lib/sysdep/os/win/error_dialog.h"
|
|
|
|
|
#include "lib/sysdep/os/win/wutil.h"
|
2006-04-23 16:14:18 -07:00
|
|
|
|
2025-02-09 11:31:32 -08:00
|
|
|
#include <boost/algorithm/string/classification.hpp>
|
|
|
|
|
#include <boost/algorithm/string/split.hpp>
|
2006-04-23 16:14:18 -07:00
|
|
|
|
2022-12-04 11:56:12 -08:00
|
|
|
#include <string_view>
|
|
|
|
|
|
2007-09-23 03:15:28 -07:00
|
|
|
|
2006-04-23 16:14:18 -07:00
|
|
|
#if MSC_VERSION
|
|
|
|
|
#pragma comment(lib, "shell32.lib") // for sys_pick_directory SH* calls
|
2011-02-21 13:54:47 -08:00
|
|
|
#pragma comment(lib, "winhttp.lib")
|
2006-04-23 16:14:18 -07:00
|
|
|
#endif
|
|
|
|
|
|
|
|
|
|
|
2011-03-11 08:29:27 -08:00
|
|
|
bool sys_IsDebuggerPresent()
|
|
|
|
|
{
|
|
|
|
|
return (IsDebuggerPresent() != 0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-01-04 11:53:38 -08:00
|
|
|
std::wstring sys_WideFromArgv(const char* argv_i)
|
|
|
|
|
{
|
|
|
|
|
// NB: despite http://cbloomrants.blogspot.com/2008/06/06-14-08-1.html,
|
|
|
|
|
// WinXP x64 EN cmd.exe (chcp reports 437) encodes argv u-umlaut
|
|
|
|
|
// (entered manually or via auto-complete) via cp1252. the same applies
|
|
|
|
|
// to WinXP SP2 DE (where chcp reports 850).
|
|
|
|
|
const UINT cp = CP_ACP;
|
|
|
|
|
const DWORD flags = MB_PRECOMPOSED|MB_ERR_INVALID_CHARS;
|
|
|
|
|
const int inputSize = -1; // null-terminated
|
2011-02-25 08:31:42 -08:00
|
|
|
std::vector<wchar_t> buf(strlen(argv_i)+1); // (upper bound on number of characters)
|
2011-01-04 11:53:38 -08:00
|
|
|
// NB: avoid mbstowcs because it may specify another locale
|
2011-04-29 12:10:34 -07:00
|
|
|
const int ret = MultiByteToWideChar(cp, flags, argv_i, (int)inputSize, &buf[0], (int)buf.size());
|
2011-04-30 06:01:45 -07:00
|
|
|
ENSURE(ret != 0);
|
2011-02-25 08:31:42 -08:00
|
|
|
return std::wstring(&buf[0]);
|
2011-01-04 11:53:38 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2008-09-18 04:27:55 -07:00
|
|
|
void sys_display_msg(const wchar_t* caption, const wchar_t* msg)
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
|
|
|
|
MessageBoxW(0, msg, caption, MB_ICONEXCLAMATION|MB_TASKMODAL|MB_SETFOREGROUND);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
2011-04-30 06:01:45 -07:00
|
|
|
// "program error" dialog (triggered by ENSURE and exception)
|
2006-04-23 16:14:18 -07:00
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
2010-12-09 03:16:21 -08:00
|
|
|
// support for resizing the dialog / its controls (must be done manually)
|
|
|
|
|
|
|
|
|
|
static POINTS dlg_clientOrigin;
|
|
|
|
|
static POINTS dlg_prevClientSize;
|
|
|
|
|
|
2025-06-03 05:13:41 -07:00
|
|
|
static void dlg_OnMove(HWND /*hDlg*/, int x, int y)
|
2010-12-09 03:16:21 -08:00
|
|
|
{
|
2011-01-04 11:53:38 -08:00
|
|
|
dlg_clientOrigin.x = (short)x;
|
|
|
|
|
dlg_clientOrigin.y = (short)y;
|
2010-12-09 03:16:21 -08:00
|
|
|
}
|
2006-04-23 16:14:18 -07:00
|
|
|
|
|
|
|
|
|
had to remove uint and ulong from lib/types.h due to conflict with other library.
this snowballed into a massive search+destroy of the hodgepodge of
mostly equivalent types we had in use (int, uint, unsigned, unsigned
int, i32, u32, ulong, uintN).
it is more efficient to use 64-bit types in 64-bit mode, so the
preferred default is size_t (for anything remotely resembling a size or
index). tile coordinates are ssize_t to allow more efficient conversion
to/from floating point. flags are int because we almost never need more
than 15 distinct bits, bit test/set is not slower and int is fastest to
type. finally, some data that is pretty much directly passed to OpenGL
is now typed accordingly.
after several hours, the code now requires fewer casts and less
guesswork.
other changes:
- unit and player IDs now have an "invalid id" constant in the
respective class to avoid casting and -1
- fix some endian/64-bit bugs in the map (un)packing. added a
convenience function to write/read a size_t.
- ia32: change CPUID interface to allow passing in ecx (required for
cache topology detection, which I need at work). remove some unneeded
functions from asm, replace with intrinsics where possible.
This was SVN commit r5942.
2008-05-11 11:48:32 -07:00
|
|
|
static const size_t ANCHOR_LEFT = 0x01;
|
|
|
|
|
static const size_t ANCHOR_RIGHT = 0x02;
|
|
|
|
|
static const size_t ANCHOR_TOP = 0x04;
|
|
|
|
|
static const size_t ANCHOR_BOTTOM = 0x08;
|
2010-12-09 03:16:21 -08:00
|
|
|
static const size_t ANCHOR_ALL = 0x0F;
|
2006-04-23 16:14:18 -07:00
|
|
|
|
2010-12-09 03:16:21 -08:00
|
|
|
static void dlg_ResizeControl(HWND hDlg, int dlgItem, int dx, int dy, size_t anchors)
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
2010-12-09 03:16:21 -08:00
|
|
|
HWND hControl = GetDlgItem(hDlg, dlgItem);
|
2006-04-23 16:14:18 -07:00
|
|
|
RECT r;
|
|
|
|
|
GetWindowRect(hControl, &r);
|
|
|
|
|
|
|
|
|
|
int w = r.right - r.left, h = r.bottom - r.top;
|
2010-12-09 03:16:21 -08:00
|
|
|
int x = r.left - dlg_clientOrigin.x, y = r.top - dlg_clientOrigin.y;
|
2006-04-23 16:14:18 -07:00
|
|
|
|
|
|
|
|
if(anchors & ANCHOR_RIGHT)
|
|
|
|
|
{
|
|
|
|
|
// right only
|
|
|
|
|
if(!(anchors & ANCHOR_LEFT))
|
|
|
|
|
x += dx;
|
|
|
|
|
// horizontal (stretch width)
|
|
|
|
|
else
|
|
|
|
|
w += dx;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(anchors & ANCHOR_BOTTOM)
|
|
|
|
|
{
|
|
|
|
|
// bottom only
|
|
|
|
|
if(!(anchors & ANCHOR_TOP))
|
|
|
|
|
y += dy;
|
|
|
|
|
// vertical (stretch height)
|
|
|
|
|
else
|
|
|
|
|
h += dy;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
SetWindowPos(hControl, 0, x,y, w,h, SWP_NOZORDER);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2010-12-09 03:16:21 -08:00
|
|
|
static void dlg_OnSize(HWND hDlg, UINT state, int clientSizeX, int clientSizeY)
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
|
|
|
|
// 'minimize' was clicked. we need to ignore this, otherwise
|
|
|
|
|
// dx/dy would reduce some control positions to less than 0.
|
|
|
|
|
// since Windows clips them, we wouldn't later be able to
|
|
|
|
|
// reconstruct the previous values when 'restoring'.
|
2010-12-09 03:16:21 -08:00
|
|
|
if(state == SIZE_MINIMIZED)
|
2006-04-23 16:14:18 -07:00
|
|
|
return;
|
|
|
|
|
|
2010-12-09 03:16:21 -08:00
|
|
|
// NB: origin might legitimately be 0, but we know it is invalid
|
|
|
|
|
// on the first call to this function, where dlg_prevClientSize is 0.
|
|
|
|
|
const bool isOriginValid = (dlg_prevClientSize.y != 0);
|
2006-04-23 16:14:18 -07:00
|
|
|
|
2010-12-09 03:16:21 -08:00
|
|
|
const int dx = clientSizeX - dlg_prevClientSize.x;
|
|
|
|
|
const int dy = clientSizeY - dlg_prevClientSize.y;
|
2011-01-04 11:53:38 -08:00
|
|
|
dlg_prevClientSize.x = (short)clientSizeX;
|
|
|
|
|
dlg_prevClientSize.y = (short)clientSizeY;
|
2006-04-23 16:14:18 -07:00
|
|
|
|
2010-12-09 03:16:21 -08:00
|
|
|
if(!isOriginValid) // must not call dlg_ResizeControl
|
2006-04-23 16:14:18 -07:00
|
|
|
return;
|
|
|
|
|
|
2010-12-09 03:16:21 -08:00
|
|
|
dlg_ResizeControl(hDlg, IDC_CONTINUE, dx,dy, ANCHOR_LEFT|ANCHOR_BOTTOM);
|
|
|
|
|
dlg_ResizeControl(hDlg, IDC_SUPPRESS, dx,dy, ANCHOR_LEFT|ANCHOR_BOTTOM);
|
|
|
|
|
dlg_ResizeControl(hDlg, IDC_BREAK , dx,dy, ANCHOR_LEFT|ANCHOR_BOTTOM);
|
|
|
|
|
dlg_ResizeControl(hDlg, IDC_EXIT , dx,dy, ANCHOR_LEFT|ANCHOR_BOTTOM);
|
|
|
|
|
dlg_ResizeControl(hDlg, IDC_COPY , dx,dy, ANCHOR_RIGHT|ANCHOR_BOTTOM);
|
|
|
|
|
dlg_ResizeControl(hDlg, IDC_EDIT1 , dx,dy, ANCHOR_ALL);
|
2006-04-23 16:14:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2025-06-03 05:13:41 -07:00
|
|
|
static void dlg_OnGetMinMaxInfo(HWND /*hDlg*/, LPMINMAXINFO mmi)
|
2010-12-09 03:16:21 -08:00
|
|
|
{
|
|
|
|
|
// we must make sure resize_control will never set negative coords -
|
|
|
|
|
// Windows would clip them, and its real position would be lost.
|
|
|
|
|
// restrict to a reasonable and good looking minimum size [pixels].
|
|
|
|
|
mmi->ptMinTrackSize.x = 407;
|
|
|
|
|
mmi->ptMinTrackSize.y = 159; // determined experimentally
|
|
|
|
|
}
|
|
|
|
|
|
2006-04-23 16:14:18 -07:00
|
|
|
|
|
|
|
|
struct DialogParams
|
|
|
|
|
{
|
|
|
|
|
const wchar_t* text;
|
2008-09-18 04:27:55 -07:00
|
|
|
size_t flags;
|
2006-04-23 16:14:18 -07:00
|
|
|
};
|
|
|
|
|
|
2025-06-03 05:13:41 -07:00
|
|
|
static BOOL dlg_OnInitDialog(HWND hDlg, HWND /*hWndFocus*/, LPARAM lParam)
|
2010-12-09 03:16:21 -08:00
|
|
|
{
|
2020-10-25 14:00:52 -07:00
|
|
|
const DialogParams* params = reinterpret_cast<const DialogParams*>(lParam);
|
|
|
|
|
SetWindowLongPtr(hDlg, DWLP_USER, lParam);
|
2010-12-09 03:16:21 -08:00
|
|
|
HWND hWnd;
|
|
|
|
|
|
|
|
|
|
// need to reset for new instance of dialog
|
|
|
|
|
dlg_clientOrigin.x = dlg_clientOrigin.y = 0;
|
|
|
|
|
dlg_prevClientSize.x = dlg_prevClientSize.y = 0;
|
|
|
|
|
|
|
|
|
|
if(!(params->flags & DE_ALLOW_SUPPRESS))
|
|
|
|
|
{
|
|
|
|
|
hWnd = GetDlgItem(hDlg, IDC_SUPPRESS);
|
|
|
|
|
EnableWindow(hWnd, FALSE);
|
|
|
|
|
}
|
|
|
|
|
|
2020-10-25 14:00:52 -07:00
|
|
|
if(params->flags & DE_NO_CONTINUE)
|
|
|
|
|
{
|
|
|
|
|
hWnd = GetDlgItem(hDlg, IDC_CONTINUE);
|
|
|
|
|
EnableWindow(hWnd, FALSE);
|
|
|
|
|
}
|
|
|
|
|
|
2010-12-09 03:16:21 -08:00
|
|
|
// set fixed font for readability
|
|
|
|
|
hWnd = GetDlgItem(hDlg, IDC_EDIT1);
|
|
|
|
|
HGDIOBJ hObj = (HGDIOBJ)GetStockObject(SYSTEM_FIXED_FONT);
|
|
|
|
|
LPARAM redraw = FALSE;
|
|
|
|
|
SendMessage(hWnd, WM_SETFONT, (WPARAM)hObj, redraw);
|
2006-04-23 16:14:18 -07:00
|
|
|
|
2010-12-09 03:16:21 -08:00
|
|
|
SetDlgItemTextW(hDlg, IDC_EDIT1, params->text);
|
|
|
|
|
return TRUE; // set default keyboard focus
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2025-06-03 05:13:41 -07:00
|
|
|
static void dlg_OnCommand(HWND hDlg, int id, HWND /*hWndCtl*/, UINT /*codeNotify*/)
|
2010-12-09 03:16:21 -08:00
|
|
|
{
|
|
|
|
|
switch(id)
|
|
|
|
|
{
|
|
|
|
|
case IDC_COPY:
|
|
|
|
|
{
|
|
|
|
|
std::vector<wchar_t> buf(128*KiB); // (too big for stack)
|
|
|
|
|
GetDlgItemTextW(hDlg, IDC_EDIT1, &buf[0], (int)buf.size());
|
2020-05-05 04:18:00 -07:00
|
|
|
std::string string = utf8_from_wstring(&buf[0]);
|
|
|
|
|
SDL_SetClipboardText(string.c_str());
|
2010-12-09 03:16:21 -08:00
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
case IDC_CONTINUE:
|
|
|
|
|
EndDialog(hDlg, ERI_CONTINUE);
|
|
|
|
|
break;
|
|
|
|
|
case IDC_SUPPRESS:
|
|
|
|
|
EndDialog(hDlg, ERI_SUPPRESS);
|
|
|
|
|
break;
|
|
|
|
|
case IDC_BREAK:
|
|
|
|
|
EndDialog(hDlg, ERI_BREAK);
|
|
|
|
|
break;
|
|
|
|
|
case IDC_EXIT:
|
|
|
|
|
EndDialog(hDlg, ERI_EXIT);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2020-10-25 14:00:52 -07:00
|
|
|
static void dlg_OnClose(HWND hDlg)
|
|
|
|
|
{
|
|
|
|
|
const DialogParams* params = reinterpret_cast<const DialogParams*>(
|
|
|
|
|
GetWindowLongPtr(hDlg, DWLP_USER));
|
|
|
|
|
if (!params)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
// Interpret close as exit in case we can't continue.
|
|
|
|
|
if(params->flags & DE_NO_CONTINUE)
|
|
|
|
|
EndDialog(hDlg, ERI_EXIT);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2025-06-03 05:13:41 -07:00
|
|
|
static void dlg_OnSysCommand(HWND hDlg, UINT cmd, int /*x*/, int /*y*/)
|
2010-12-09 03:16:21 -08:00
|
|
|
{
|
|
|
|
|
switch(cmd & 0xFFF0) // NB: lower 4 bits are reserved
|
|
|
|
|
{
|
|
|
|
|
// [X] clicked -> close dialog (doesn't happen automatically)
|
|
|
|
|
case SC_CLOSE:
|
|
|
|
|
EndDialog(hDlg, 0);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
default:
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static INT_PTR CALLBACK dlg_OnMessage(HWND hDlg, unsigned int msg, WPARAM wParam, LPARAM lParam)
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
|
|
|
|
switch(msg)
|
|
|
|
|
{
|
|
|
|
|
case WM_INITDIALOG:
|
2010-12-09 03:16:21 -08:00
|
|
|
return HANDLE_WM_INITDIALOG(hDlg, wParam, lParam, dlg_OnInitDialog);
|
2006-04-23 16:14:18 -07:00
|
|
|
|
|
|
|
|
case WM_SYSCOMMAND:
|
2010-12-09 03:16:21 -08:00
|
|
|
return HANDLE_WM_SYSCOMMAND(hDlg, wParam, lParam, dlg_OnSysCommand);
|
2006-04-23 16:14:18 -07:00
|
|
|
|
|
|
|
|
case WM_COMMAND:
|
2010-12-09 03:16:21 -08:00
|
|
|
return HANDLE_WM_COMMAND(hDlg, wParam, lParam, dlg_OnCommand);
|
2006-04-23 16:14:18 -07:00
|
|
|
|
|
|
|
|
case WM_MOVE:
|
2010-12-09 03:16:21 -08:00
|
|
|
return HANDLE_WM_MOVE(hDlg, wParam, lParam, dlg_OnMove);
|
2006-04-23 16:14:18 -07:00
|
|
|
|
|
|
|
|
case WM_GETMINMAXINFO:
|
2010-12-09 03:16:21 -08:00
|
|
|
return HANDLE_WM_GETMINMAXINFO(hDlg, wParam, lParam, dlg_OnGetMinMaxInfo);
|
2006-04-23 16:14:18 -07:00
|
|
|
|
|
|
|
|
case WM_SIZE:
|
2010-12-09 03:16:21 -08:00
|
|
|
return HANDLE_WM_SIZE(hDlg, wParam, lParam, dlg_OnSize);
|
2006-04-23 16:14:18 -07:00
|
|
|
|
2020-10-25 14:00:52 -07:00
|
|
|
case WM_CLOSE:
|
|
|
|
|
return HANDLE_WM_CLOSE(hDlg, wParam, lParam, dlg_OnClose);
|
|
|
|
|
|
2006-04-23 16:14:18 -07:00
|
|
|
default:
|
2010-12-09 03:16:21 -08:00
|
|
|
// we didn't process the message; caller will perform default action.
|
|
|
|
|
return FALSE;
|
2006-04-23 16:14:18 -07:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2010-12-09 03:16:21 -08:00
|
|
|
ErrorReactionInternal sys_display_error(const wchar_t* text, size_t flags)
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
|
|
|
|
// note: other threads might still be running, crash and take down the
|
|
|
|
|
// process before we have a chance to display this error message.
|
|
|
|
|
// ideally we would suspend them all and resume when finished; however,
|
2006-06-22 11:26:08 -07:00
|
|
|
// they may be holding system-wide locks (e.g. heap or loader) that
|
2006-04-23 16:14:18 -07:00
|
|
|
// are potentially needed by DialogBoxParam. in that case, deadlock
|
|
|
|
|
// would result; this is much worse than a crash because no error
|
|
|
|
|
// at all is displayed to the end-user. therefore, do nothing here.
|
|
|
|
|
|
|
|
|
|
// temporarily remove any pending quit message from the queue because
|
|
|
|
|
// it would prevent the dialog from being displayed (DialogBoxParam
|
|
|
|
|
// returns IDOK without doing anything). will be restored below.
|
|
|
|
|
// notes:
|
|
|
|
|
// - this isn't only relevant at exit - Windows also posts one if
|
|
|
|
|
// window init fails. therefore, it is important that errors can be
|
|
|
|
|
// displayed regardless.
|
|
|
|
|
// - by passing hWnd=0, we check all windows belonging to the current
|
|
|
|
|
// thread. there is no reason to use hWndParent below.
|
|
|
|
|
MSG msg;
|
2010-12-09 03:16:21 -08:00
|
|
|
const BOOL isQuitPending = PeekMessage(&msg, 0, WM_QUIT, WM_QUIT, PM_REMOVE);
|
2006-04-23 16:14:18 -07:00
|
|
|
|
2010-07-08 03:18:42 -07:00
|
|
|
const HINSTANCE hInstance = wutil_LibModuleHandle();
|
2010-04-03 03:46:28 -07:00
|
|
|
LPCWSTR lpTemplateName = MAKEINTRESOURCEW(IDD_DIALOG1);
|
2006-04-23 16:14:18 -07:00
|
|
|
const DialogParams params = { text, flags };
|
|
|
|
|
// get the enclosing app's window handle. we can't just pass 0 or
|
2007-09-23 03:15:28 -07:00
|
|
|
// the desktop window because the dialog must be modal (if the app
|
|
|
|
|
// continues running, it may crash and take down the process before
|
|
|
|
|
// we've managed to show the dialog).
|
|
|
|
|
const HWND hWndParent = wutil_AppWindow();
|
2006-04-23 16:14:18 -07:00
|
|
|
|
2010-12-09 03:16:21 -08:00
|
|
|
INT_PTR ret = DialogBoxParamW(hInstance, lpTemplateName, hWndParent, dlg_OnMessage, (LPARAM)¶ms);
|
2006-04-23 16:14:18 -07:00
|
|
|
|
2010-12-09 03:16:21 -08:00
|
|
|
if(isQuitPending)
|
2006-04-23 16:14:18 -07:00
|
|
|
PostQuitMessage((int)msg.wParam);
|
|
|
|
|
|
2010-12-09 03:16:21 -08:00
|
|
|
// failed; warn user and make sure we return an ErrorReactionInternal.
|
2006-04-23 16:14:18 -07:00
|
|
|
if(ret == 0 || ret == -1)
|
|
|
|
|
{
|
2008-09-27 03:05:11 -07:00
|
|
|
debug_DisplayMessage(L"Error", L"Unable to display detailed error dialog.");
|
2010-12-09 03:16:21 -08:00
|
|
|
return ERI_CONTINUE;
|
2006-04-23 16:14:18 -07:00
|
|
|
}
|
2010-12-09 03:16:21 -08:00
|
|
|
return (ErrorReactionInternal)ret;
|
2006-04-23 16:14:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
// misc
|
|
|
|
|
//-----------------------------------------------------------------------------
|
|
|
|
|
|
2011-05-03 05:38:42 -07:00
|
|
|
Status sys_StatusDescription(int user_err, wchar_t* buf, size_t max_chars)
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
2008-05-01 08:41:42 -07:00
|
|
|
// validate user_err - Win32 doesn't have negative error numbers
|
2006-06-23 10:41:55 -07:00
|
|
|
if(user_err < 0)
|
2006-09-22 06:19:40 -07:00
|
|
|
return ERR::FAIL; // NOWARN
|
2008-05-01 08:41:42 -07:00
|
|
|
|
2021-04-21 23:53:03 -07:00
|
|
|
const DWORD errorCode = user_err? (DWORD)user_err : GetLastError();
|
2008-05-01 08:41:42 -07:00
|
|
|
|
|
|
|
|
// no one likes to see "The operation completed successfully" in
|
|
|
|
|
// error messages, so return more descriptive text instead.
|
2021-04-21 23:53:03 -07:00
|
|
|
if(errorCode == 0)
|
2008-05-01 08:41:42 -07:00
|
|
|
{
|
2009-11-03 13:46:35 -08:00
|
|
|
wcscpy_s(buf, max_chars, L"0 (no error code was set)");
|
2008-05-01 08:41:42 -07:00
|
|
|
return INFO::OK;
|
|
|
|
|
}
|
2006-04-23 16:14:18 -07:00
|
|
|
|
2011-12-29 12:15:47 -08:00
|
|
|
wchar_t message[400];
|
2021-04-21 23:53:03 -07:00
|
|
|
if(errorCode == ERROR_NOT_ENOUGH_MEMORY)
|
|
|
|
|
{
|
|
|
|
|
// We handle out of memory separately to prevent possible subsequent/nested allocations.
|
|
|
|
|
swprintf_s(message, ARRAY_SIZE(message), L"Not enough memory resources are available to process this command.");
|
|
|
|
|
}
|
|
|
|
|
else
|
2009-07-31 09:42:39 -07:00
|
|
|
{
|
|
|
|
|
const LPCVOID source = 0; // ignored (we're not using FROM_HMODULE etc.)
|
|
|
|
|
const DWORD lang_id = 0; // look for neutral, then current locale
|
|
|
|
|
va_list* args = 0; // we don't care about "inserts"
|
2021-04-21 23:53:03 -07:00
|
|
|
const DWORD charsWritten = FormatMessageW(FORMAT_MESSAGE_FROM_SYSTEM, source, errorCode, lang_id, message, (DWORD)ARRAY_SIZE(message), args);
|
2009-07-31 09:42:39 -07:00
|
|
|
if(!charsWritten)
|
|
|
|
|
WARN_RETURN(ERR::FAIL);
|
2011-04-30 06:01:45 -07:00
|
|
|
ENSURE(charsWritten < max_chars);
|
2011-05-23 07:10:45 -07:00
|
|
|
if(message[charsWritten-1] == '\n')
|
|
|
|
|
message[charsWritten-1] = '\0';
|
|
|
|
|
if(message[charsWritten-2] == '\r')
|
|
|
|
|
message[charsWritten-2] = '\0';
|
2009-07-31 09:42:39 -07:00
|
|
|
}
|
|
|
|
|
|
2021-04-21 23:53:03 -07:00
|
|
|
const int charsWritten = swprintf_s(buf, max_chars, L"%d (%ls)", errorCode, message);
|
2011-04-30 06:01:45 -07:00
|
|
|
ENSURE(charsWritten != -1);
|
2006-09-22 06:19:40 -07:00
|
|
|
return INFO::OK;
|
2006-04-23 16:14:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-05-03 05:38:42 -07:00
|
|
|
static Status GetModulePathname(HMODULE hModule, OsPath& pathname)
|
2011-03-23 09:14:47 -07:00
|
|
|
{
|
|
|
|
|
wchar_t pathnameBuf[32768]; // NTFS limit
|
|
|
|
|
const DWORD length = (DWORD)ARRAY_SIZE(pathnameBuf);
|
|
|
|
|
const DWORD charsWritten = GetModuleFileNameW(hModule, pathnameBuf, length);
|
|
|
|
|
if(charsWritten == 0) // failed
|
2011-05-03 05:38:42 -07:00
|
|
|
WARN_RETURN(StatusFromWin());
|
2011-04-30 06:01:45 -07:00
|
|
|
ENSURE(charsWritten < length); // why would the above buffer ever be exceeded?
|
2011-03-23 09:14:47 -07:00
|
|
|
pathname = pathnameBuf;
|
|
|
|
|
return INFO::OK;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-05-03 05:38:42 -07:00
|
|
|
Status sys_get_module_filename(void* addr, OsPath& pathname)
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
|
|
|
|
MEMORY_BASIC_INFORMATION mbi;
|
2009-11-03 13:46:35 -08:00
|
|
|
const SIZE_T bytesWritten = VirtualQuery(addr, &mbi, sizeof(mbi));
|
|
|
|
|
if(!bytesWritten)
|
2011-05-03 05:38:42 -07:00
|
|
|
WARN_RETURN(StatusFromWin());
|
2011-04-30 06:01:45 -07:00
|
|
|
ENSURE(bytesWritten >= sizeof(mbi));
|
2011-03-23 09:14:47 -07:00
|
|
|
return GetModulePathname((HMODULE)mbi.AllocationBase, pathname);
|
2006-04-23 16:14:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2011-03-23 09:14:47 -07:00
|
|
|
OsPath sys_ExecutablePathname()
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
2011-05-03 06:46:35 -07:00
|
|
|
WinScopedPreserveLastError s;
|
2011-03-23 09:14:47 -07:00
|
|
|
OsPath pathname;
|
2011-04-30 06:01:45 -07:00
|
|
|
ENSURE(GetModulePathname(0, pathname) == INFO::OK);
|
2011-03-23 09:14:47 -07:00
|
|
|
return pathname;
|
2006-04-23 16:14:18 -07:00
|
|
|
}
|
|
|
|
|
|
2011-03-21 10:53:13 -07:00
|
|
|
|
2010-06-30 14:18:29 -07:00
|
|
|
std::wstring sys_get_user_name()
|
|
|
|
|
{
|
|
|
|
|
wchar_t usernameBuf[256];
|
|
|
|
|
DWORD size = ARRAY_SIZE(usernameBuf);
|
|
|
|
|
if(!GetUserNameW(usernameBuf, &size))
|
|
|
|
|
return L"";
|
|
|
|
|
return usernameBuf;
|
|
|
|
|
}
|
2006-04-23 16:14:18 -07:00
|
|
|
|
2011-03-21 10:53:13 -07:00
|
|
|
|
2010-06-23 09:17:25 -07:00
|
|
|
// callback for shell directory picker: used to set starting directory
|
|
|
|
|
// (for user convenience).
|
2025-06-03 05:13:41 -07:00
|
|
|
static int CALLBACK BrowseCallback(HWND hWnd, unsigned int msg, LPARAM /*lParam*/, LPARAM lpData)
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
|
|
|
|
if(msg == BFFM_INITIALIZED)
|
|
|
|
|
{
|
2010-06-23 09:17:25 -07:00
|
|
|
const WPARAM wParam = TRUE; // lpData is a Unicode string, not PIDL.
|
2010-07-10 11:24:02 -07:00
|
|
|
// (MSDN: the return values for both of these BFFM_ notifications are ignored)
|
|
|
|
|
(void)SendMessage(hWnd, BFFM_SETSELECTIONW, wParam, lpData);
|
2006-04-23 16:14:18 -07:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-03 05:38:42 -07:00
|
|
|
Status sys_pick_directory(OsPath& path)
|
2006-04-23 16:14:18 -07:00
|
|
|
{
|
2010-06-23 09:17:25 -07:00
|
|
|
// (must not use multi-threaded apartment due to BIF_NEWDIALOGSTYLE)
|
|
|
|
|
const HRESULT hr = CoInitialize(0);
|
2011-04-30 06:01:45 -07:00
|
|
|
ENSURE(hr == S_OK || hr == S_FALSE); // S_FALSE == already initialized
|
2010-06-23 09:17:25 -07:00
|
|
|
|
|
|
|
|
// note: bi.pszDisplayName isn't the full path, so it isn't of any use.
|
|
|
|
|
BROWSEINFOW bi;
|
2006-04-23 16:14:18 -07:00
|
|
|
memset(&bi, 0, sizeof(bi));
|
2010-06-23 09:17:25 -07:00
|
|
|
bi.ulFlags = BIF_RETURNONLYFSDIRS|BIF_NEWDIALOGSTYLE|BIF_NONEWFOLDERBUTTON;
|
|
|
|
|
// for setting starting directory:
|
|
|
|
|
bi.lpfn = (BFFCALLBACK)BrowseCallback;
|
2011-03-23 06:36:20 -07:00
|
|
|
const Path::String initialPath = OsString(path); // NB: BFFM_SETSELECTIONW can't deal with '/' separators
|
2010-06-23 09:17:25 -07:00
|
|
|
bi.lParam = (LPARAM)initialPath.c_str();
|
|
|
|
|
const LPITEMIDLIST pidl = SHBrowseForFolderW(&bi);
|
|
|
|
|
if(!pidl) // user canceled
|
|
|
|
|
return INFO::SKIPPED;
|
2006-04-23 16:14:18 -07:00
|
|
|
|
2009-11-03 13:46:35 -08:00
|
|
|
// translate ITEMIDLIST to string
|
2011-03-23 09:14:47 -07:00
|
|
|
wchar_t pathBuf[MAX_PATH]; // mandated by SHGetPathFromIDListW
|
2010-06-23 09:17:25 -07:00
|
|
|
const BOOL ok = SHGetPathFromIDListW(pidl, pathBuf);
|
2006-04-23 16:14:18 -07:00
|
|
|
|
|
|
|
|
// free the ITEMIDLIST
|
2011-12-28 17:50:18 -08:00
|
|
|
CoTaskMemFree(pidl);
|
2006-04-23 16:14:18 -07:00
|
|
|
|
2010-06-23 09:17:25 -07:00
|
|
|
if(ok == TRUE)
|
|
|
|
|
{
|
|
|
|
|
path = pathBuf;
|
|
|
|
|
return INFO::OK;
|
|
|
|
|
}
|
|
|
|
|
|
2011-09-20 15:49:02 -07:00
|
|
|
// Balance call to CoInitialize, which must have been successful
|
|
|
|
|
CoUninitialize();
|
|
|
|
|
|
2011-05-03 05:38:42 -07:00
|
|
|
WARN_RETURN(StatusFromWin());
|
2006-04-23 16:14:18 -07:00
|
|
|
}
|
2010-06-30 16:40:51 -07:00
|
|
|
|
2011-03-21 10:53:13 -07:00
|
|
|
|
2011-05-03 05:38:42 -07:00
|
|
|
Status sys_open_url(const std::string& url)
|
2010-07-07 14:47:31 -07:00
|
|
|
{
|
2010-07-07 15:23:18 -07:00
|
|
|
HINSTANCE r = ShellExecuteA(NULL, "open", url.c_str(), NULL, NULL, SW_SHOWNORMAL);
|
2010-07-10 11:24:02 -07:00
|
|
|
if ((int)(intptr_t)r > 32)
|
2010-07-07 14:47:31 -07:00
|
|
|
return INFO::OK;
|
|
|
|
|
|
|
|
|
|
WARN_RETURN(ERR::FAIL);
|
|
|
|
|
}
|
|
|
|
|
|
2011-03-21 10:53:13 -07:00
|
|
|
|
2011-05-03 05:38:42 -07:00
|
|
|
Status sys_generate_random_bytes(u8* buffer, size_t size)
|
2010-06-30 16:40:51 -07:00
|
|
|
{
|
2011-01-28 05:40:07 -08:00
|
|
|
HCRYPTPROV hCryptProv = 0;
|
|
|
|
|
if(!CryptAcquireContext(&hCryptProv, 0, 0, PROV_RSA_FULL, CRYPT_VERIFYCONTEXT))
|
2011-05-03 05:38:42 -07:00
|
|
|
WARN_RETURN(StatusFromWin());
|
2010-06-30 16:40:51 -07:00
|
|
|
|
2011-01-28 05:40:07 -08:00
|
|
|
memset(buffer, 0, size);
|
|
|
|
|
if(!CryptGenRandom(hCryptProv, (DWORD)size, (BYTE*)buffer))
|
2011-05-03 05:38:42 -07:00
|
|
|
WARN_RETURN(StatusFromWin());
|
2010-06-30 16:40:51 -07:00
|
|
|
|
2011-01-28 05:40:07 -08:00
|
|
|
if(!CryptReleaseContext(hCryptProv, 0))
|
2011-05-03 05:38:42 -07:00
|
|
|
WARN_RETURN(StatusFromWin());
|
2010-06-30 16:40:51 -07:00
|
|
|
|
|
|
|
|
return INFO::OK;
|
|
|
|
|
}
|
2011-02-21 13:54:47 -08:00
|
|
|
|
2011-03-21 10:53:13 -07:00
|
|
|
|
2011-02-21 13:54:47 -08:00
|
|
|
/*
|
|
|
|
|
* Given a string of the form
|
|
|
|
|
* "example.com:80"
|
|
|
|
|
* or
|
|
|
|
|
* "ftp=ftp.example.com:80;http=example.com:80;https=example.com:80"
|
|
|
|
|
* separated by semicolons or whitespace,
|
|
|
|
|
* return the string "example.com:80".
|
|
|
|
|
*/
|
|
|
|
|
static std::wstring parse_proxy(const std::wstring& input)
|
|
|
|
|
{
|
|
|
|
|
if(input.find('=') == input.npos)
|
|
|
|
|
return input;
|
|
|
|
|
|
|
|
|
|
std::vector<std::wstring> parts;
|
|
|
|
|
split(parts, input, boost::algorithm::is_any_of("; \t\r\n"), boost::algorithm::token_compress_on);
|
2016-11-23 06:09:58 -08:00
|
|
|
|
2022-12-04 11:56:12 -08:00
|
|
|
constexpr std::wstring_view http{L"http="};
|
|
|
|
|
for(const std::wstring& part : parts)
|
|
|
|
|
if(std::wstring_view{part}.substr(0, http.size()) == http)
|
|
|
|
|
return part.substr(http.size());
|
2016-11-23 06:09:58 -08:00
|
|
|
|
2011-02-21 13:54:47 -08:00
|
|
|
// If we got this far, proxies were only set for non-HTTP protocols
|
|
|
|
|
return L"";
|
|
|
|
|
}
|
|
|
|
|
|
2011-05-03 05:38:42 -07:00
|
|
|
Status sys_get_proxy_config(const std::wstring& url, std::wstring& proxy)
|
2011-02-21 13:54:47 -08:00
|
|
|
{
|
|
|
|
|
WINHTTP_AUTOPROXY_OPTIONS autoProxyOptions;
|
|
|
|
|
memset(&autoProxyOptions, 0, sizeof(autoProxyOptions));
|
|
|
|
|
autoProxyOptions.dwFlags = WINHTTP_AUTOPROXY_AUTO_DETECT;
|
|
|
|
|
autoProxyOptions.dwAutoDetectFlags = WINHTTP_AUTO_DETECT_TYPE_DHCP | WINHTTP_AUTO_DETECT_TYPE_DNS_A;
|
|
|
|
|
autoProxyOptions.fAutoLogonIfChallenged = TRUE;
|
|
|
|
|
|
|
|
|
|
WINHTTP_PROXY_INFO proxyInfo;
|
|
|
|
|
memset(&proxyInfo, 0, sizeof(proxyInfo));
|
|
|
|
|
|
|
|
|
|
WINHTTP_CURRENT_USER_IE_PROXY_CONFIG ieConfig;
|
|
|
|
|
memset(&ieConfig, 0, sizeof(ieConfig));
|
|
|
|
|
|
|
|
|
|
HINTERNET hSession = NULL;
|
|
|
|
|
|
2011-05-03 05:38:42 -07:00
|
|
|
Status err = INFO::SKIPPED;
|
2011-02-21 13:54:47 -08:00
|
|
|
|
|
|
|
|
bool useAutoDetect;
|
|
|
|
|
|
|
|
|
|
if(WinHttpGetIEProxyConfigForCurrentUser(&ieConfig))
|
|
|
|
|
{
|
|
|
|
|
if(ieConfig.lpszAutoConfigUrl)
|
|
|
|
|
{
|
|
|
|
|
// Use explicit auto-config script if specified
|
|
|
|
|
useAutoDetect = true;
|
|
|
|
|
autoProxyOptions.dwFlags |= WINHTTP_AUTOPROXY_CONFIG_URL;
|
|
|
|
|
autoProxyOptions.lpszAutoConfigUrl = ieConfig.lpszAutoConfigUrl;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Use auto-discovery if enabled
|
|
|
|
|
useAutoDetect = (ieConfig.fAutoDetect == TRUE);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
// Can't find IE config settings - fall back to auto-discovery
|
|
|
|
|
useAutoDetect = true;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if(useAutoDetect)
|
|
|
|
|
{
|
|
|
|
|
hSession = WinHttpOpen(NULL, WINHTTP_ACCESS_TYPE_DEFAULT_PROXY,
|
|
|
|
|
WINHTTP_NO_PROXY_NAME, WINHTTP_NO_PROXY_BYPASS, 0);
|
|
|
|
|
|
2011-04-22 09:05:00 -07:00
|
|
|
if(hSession && WinHttpGetProxyForUrl(hSession, url.c_str(), &autoProxyOptions, &proxyInfo) && proxyInfo.lpszProxy)
|
2011-02-21 13:54:47 -08:00
|
|
|
{
|
|
|
|
|
proxy = parse_proxy(proxyInfo.lpszProxy);
|
|
|
|
|
if(!proxy.empty())
|
|
|
|
|
{
|
|
|
|
|
err = INFO::OK;
|
|
|
|
|
goto done;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
// No valid auto-config; try explicit proxy instead
|
|
|
|
|
if(ieConfig.lpszProxy)
|
|
|
|
|
{
|
|
|
|
|
proxy = parse_proxy(ieConfig.lpszProxy);
|
|
|
|
|
if(!proxy.empty())
|
|
|
|
|
{
|
|
|
|
|
err = INFO::OK;
|
|
|
|
|
goto done;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
done:
|
|
|
|
|
if(ieConfig.lpszProxy)
|
|
|
|
|
GlobalFree(ieConfig.lpszProxy);
|
|
|
|
|
if(ieConfig.lpszProxyBypass)
|
|
|
|
|
GlobalFree(ieConfig.lpszProxyBypass);
|
|
|
|
|
if(ieConfig.lpszAutoConfigUrl)
|
|
|
|
|
GlobalFree(ieConfig.lpszAutoConfigUrl);
|
|
|
|
|
if(proxyInfo.lpszProxy)
|
|
|
|
|
GlobalFree(proxyInfo.lpszProxy);
|
|
|
|
|
if(proxyInfo.lpszProxyBypass)
|
|
|
|
|
GlobalFree(proxyInfo.lpszProxyBypass);
|
|
|
|
|
if(hSession)
|
|
|
|
|
WinHttpCloseHandle(hSession);
|
|
|
|
|
|
|
|
|
|
return err;
|
|
|
|
|
}
|
2011-03-21 10:53:13 -07:00
|
|
|
|
2011-03-23 06:36:20 -07:00
|
|
|
FILE* sys_OpenFile(const OsPath& pathname, const char* mode)
|
|
|
|
|
{
|
|
|
|
|
FILE* f = 0;
|
|
|
|
|
const std::wstring wmode(mode, mode+strlen(mode));
|
|
|
|
|
(void)_wfopen_s(&f, OsString(pathname).c_str(), wmode.c_str());
|
|
|
|
|
return f;
|
|
|
|
|
}
|