2017-04-27 18:18:03 +00:00
|
|
|
// SPDX-License-Identifier: GPL-2.0
|
2011-11-24 06:56:57 +00:00
|
|
|
/* windows.c */
|
|
|
|
/* implements Windows specific functions */
|
2014-11-13 17:36:08 +00:00
|
|
|
#include <io.h>
|
2022-03-12 09:44:37 +00:00
|
|
|
#include "device.h"
|
|
|
|
#include "libdivecomputer.h"
|
2019-08-05 18:07:10 +00:00
|
|
|
#include "file.h"
|
|
|
|
#include "errorhelper.h"
|
2024-06-30 19:17:17 +00:00
|
|
|
#include "subsurfacestartup.h"
|
2020-10-27 20:03:14 +00:00
|
|
|
#include "subsurfacesysinfo.h"
|
2014-06-09 21:05:56 +00:00
|
|
|
#undef _WIN32_WINNT
|
2014-03-25 14:55:56 +00:00
|
|
|
#define _WIN32_WINNT 0x500
|
2011-11-24 06:56:57 +00:00
|
|
|
#include <windows.h>
|
2012-09-09 16:06:44 +00:00
|
|
|
#include <shlobj.h>
|
2013-12-19 13:00:50 +00:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
#include <assert.h>
|
|
|
|
#include <dirent.h>
|
|
|
|
#include <zip.h>
|
2015-10-06 10:10:16 +00:00
|
|
|
#include <lmcons.h>
|
2023-04-29 09:43:17 +00:00
|
|
|
#include <string>
|
|
|
|
|
|
|
|
/* this function converts a win32's utf-16 2 byte string to utf-8.
|
|
|
|
* note: the standard library's <codecvt> was deprecated and is in
|
|
|
|
* an ominous state, so use the native Windows version for now.
|
|
|
|
*/
|
2023-04-29 12:22:00 +00:00
|
|
|
static std::string utf16_to_utf8_fl(const std::wstring &utf16, const char *file, int line)
|
2023-04-29 09:43:17 +00:00
|
|
|
{
|
|
|
|
assert(file != NULL);
|
|
|
|
assert(line);
|
|
|
|
/* estimate buffer size */
|
|
|
|
const int sz = WideCharToMultiByte(CP_UTF8, 0, utf16.c_str(), -1, NULL, 0, NULL, NULL);
|
|
|
|
if (!sz) {
|
2024-03-24 20:03:08 +00:00
|
|
|
report_info("%s:%d: cannot estimate buffer size", file, line);
|
2023-04-29 09:43:17 +00:00
|
|
|
return std::string();
|
|
|
|
}
|
|
|
|
std::string utf8(sz, ' '); // Note: includes the terminating '\0', just in case.
|
|
|
|
if (WideCharToMultiByte(CP_UTF8, 0, utf16.c_str(), -1, &utf8[0], sz, NULL, NULL)) {
|
|
|
|
utf8.resize(sz - 1); // Chop off final '\0' byte
|
|
|
|
return utf8;
|
|
|
|
}
|
2024-03-24 20:03:08 +00:00
|
|
|
report_info("%s:%d: cannot convert string", file, line);
|
2023-04-29 09:43:17 +00:00
|
|
|
return std::string();
|
|
|
|
}
|
|
|
|
|
2023-04-29 13:09:20 +00:00
|
|
|
/* this function converts a utf-8 string to win32's utf-16 2 byte string.
|
|
|
|
*/
|
|
|
|
static std::wstring utf8_to_utf16_fl(const char *utf8, const char *file, int line)
|
|
|
|
{
|
|
|
|
assert(file != NULL);
|
|
|
|
assert(line);
|
|
|
|
/* estimate buffer size */
|
|
|
|
const int sz = strlen(utf8) + 1;
|
|
|
|
std::wstring utf16(sz, ' '); // Note: includes the terminating '\0', just in case.
|
|
|
|
int actual_size = MultiByteToWideChar(CP_UTF8, 0, utf8, -1, &utf16[0], sz);
|
|
|
|
if (actual_size) {
|
|
|
|
utf16.resize(actual_size - 1); // Chop off final '\0' character
|
|
|
|
return utf16;
|
|
|
|
}
|
2024-03-24 20:03:08 +00:00
|
|
|
report_info("%s:%d: cannot convert string", file, line);
|
2023-04-29 13:09:20 +00:00
|
|
|
return std::wstring();
|
|
|
|
}
|
|
|
|
|
2023-04-29 09:43:17 +00:00
|
|
|
#define utf16_to_utf8(s) utf16_to_utf8_fl(s, __FILE__, __LINE__)
|
|
|
|
|
|
|
|
/* this function returns the Win32 Roaming path for the current user as UTF-8.
|
|
|
|
* it never returns an empty string but fallsback to .\ instead!
|
|
|
|
*/
|
|
|
|
static std::wstring system_default_path()
|
|
|
|
{
|
|
|
|
wchar_t wpath[MAX_PATH] = { 0 };
|
|
|
|
const char *fname = "system_default_path()";
|
|
|
|
|
|
|
|
/* obtain the user path via SHGetFolderPathW.
|
|
|
|
* this API is deprecated but still supported on modern Win32.
|
|
|
|
* fallback to .\ if it fails.
|
|
|
|
*/
|
|
|
|
std::wstring path;
|
|
|
|
if (SUCCEEDED(SHGetFolderPathW(NULL, CSIDL_APPDATA, NULL, 0, wpath))) {
|
|
|
|
path = wpath;
|
|
|
|
} else {
|
2024-03-24 20:03:08 +00:00
|
|
|
report_info("%s: cannot obtain path!", fname);
|
2023-04-29 09:43:17 +00:00
|
|
|
path = L'.';
|
|
|
|
}
|
|
|
|
return path + L"\\Subsurface";
|
|
|
|
}
|
|
|
|
|
|
|
|
/* obtain the Roaming path and append "\\<USERNAME>.xml" to it.
|
|
|
|
*/
|
|
|
|
static std::wstring make_default_filename()
|
|
|
|
{
|
|
|
|
wchar_t username[UNLEN + 1] = { 0 };
|
|
|
|
DWORD username_len = UNLEN + 1;
|
|
|
|
GetUserNameW(username, &username_len);
|
|
|
|
std::wstring filename = username;
|
|
|
|
filename += L".xml";
|
|
|
|
|
|
|
|
std::wstring path = system_default_path();
|
|
|
|
return path + L"\\" + filename;
|
|
|
|
}
|
|
|
|
|
2024-06-13 20:59:32 +00:00
|
|
|
using namespace std::string_literals;
|
|
|
|
static std::string non_standard_system_divelist_default_font = "Calibri"s;
|
|
|
|
static std::string current_system_divelist_default_font = "Segoe UI"s;
|
|
|
|
std::string system_divelist_default_font;
|
2014-08-27 13:00:10 +00:00
|
|
|
double system_divelist_default_font_size = -1;
|
2011-11-24 06:56:57 +00:00
|
|
|
|
2024-05-04 16:53:41 +00:00
|
|
|
void subsurface_OS_pref_setup()
|
2014-08-22 22:22:02 +00:00
|
|
|
{
|
2024-06-13 20:59:32 +00:00
|
|
|
system_divelist_default_font = isWin7Or8() ? current_system_divelist_default_font
|
|
|
|
: non_standard_system_divelist_default_font;
|
2014-08-22 22:22:02 +00:00
|
|
|
}
|
|
|
|
|
2024-06-13 20:59:32 +00:00
|
|
|
bool subsurface_ignore_font(const std::string &font)
|
2014-08-27 13:00:10 +00:00
|
|
|
{
|
|
|
|
// if this is running on a recent enough version of Windows and the font
|
|
|
|
// passed in is the pre 4.3 default font, ignore it
|
2024-06-13 20:59:32 +00:00
|
|
|
return isWin7Or8() && font == non_standard_system_divelist_default_font;
|
2014-08-27 13:00:10 +00:00
|
|
|
}
|
|
|
|
|
2018-09-03 18:58:42 +00:00
|
|
|
#define utf8_to_utf16(s) utf8_to_utf16_fl(s, __FILE__, __LINE__)
|
|
|
|
|
2023-04-29 09:43:17 +00:00
|
|
|
/* '\' not included at the end.
|
2015-10-06 10:10:16 +00:00
|
|
|
*/
|
2024-06-13 20:59:32 +00:00
|
|
|
std::string system_default_directory()
|
2012-09-09 16:06:44 +00:00
|
|
|
{
|
2023-04-29 09:43:17 +00:00
|
|
|
static std::string path = utf16_to_utf8(system_default_path());
|
2024-06-13 20:59:32 +00:00
|
|
|
return path;
|
2015-10-06 10:10:16 +00:00
|
|
|
}
|
2012-09-09 16:06:44 +00:00
|
|
|
|
2024-06-13 20:59:32 +00:00
|
|
|
std::string system_default_filename()
|
2015-10-06 10:10:16 +00:00
|
|
|
{
|
2023-04-29 09:43:17 +00:00
|
|
|
static std::string path = utf16_to_utf8(make_default_filename());
|
2024-06-13 20:59:32 +00:00
|
|
|
return path;
|
2012-09-09 16:06:44 +00:00
|
|
|
}
|
|
|
|
|
2018-08-27 17:32:14 +00:00
|
|
|
int enumerate_devices(device_callback_t callback, void *userdata, unsigned int transport)
|
2013-09-16 21:04:42 +00:00
|
|
|
{
|
|
|
|
int index = -1;
|
2013-09-17 11:50:54 +00:00
|
|
|
DWORD i;
|
2018-08-27 17:32:14 +00:00
|
|
|
if (transport & DC_TRANSPORT_SERIAL) {
|
2014-05-18 04:29:40 +00:00
|
|
|
// Open the registry key.
|
|
|
|
HKEY hKey;
|
|
|
|
LONG rc = RegOpenKeyEx(HKEY_LOCAL_MACHINE, "HARDWARE\\DEVICEMAP\\SERIALCOMM", 0, KEY_QUERY_VALUE, &hKey);
|
2013-09-16 21:04:42 +00:00
|
|
|
if (rc != ERROR_SUCCESS) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
2014-05-18 04:29:40 +00:00
|
|
|
// Get the number of values.
|
|
|
|
DWORD count = 0;
|
|
|
|
rc = RegQueryInfoKey(hKey, NULL, NULL, NULL, NULL, NULL, NULL, &count, NULL, NULL, NULL, NULL);
|
|
|
|
if (rc != ERROR_SUCCESS) {
|
2013-09-16 21:04:42 +00:00
|
|
|
RegCloseKey(hKey);
|
|
|
|
return -1;
|
|
|
|
}
|
2014-05-18 04:29:40 +00:00
|
|
|
for (i = 0; i < count; ++i) {
|
|
|
|
// Get the value name, data and type.
|
|
|
|
char name[512], data[512];
|
|
|
|
DWORD name_len = sizeof(name);
|
|
|
|
DWORD data_len = sizeof(data);
|
|
|
|
DWORD type = 0;
|
|
|
|
rc = RegEnumValue(hKey, i, name, &name_len, NULL, &type, (LPBYTE)data, &data_len);
|
|
|
|
if (rc != ERROR_SUCCESS) {
|
|
|
|
RegCloseKey(hKey);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Ignore non-string values.
|
|
|
|
if (type != REG_SZ)
|
|
|
|
continue;
|
2013-09-16 21:04:42 +00:00
|
|
|
|
2014-05-18 04:29:40 +00:00
|
|
|
// Prevent a possible buffer overflow.
|
|
|
|
if (data_len >= sizeof(data)) {
|
|
|
|
RegCloseKey(hKey);
|
|
|
|
return -1;
|
|
|
|
}
|
2013-09-16 21:04:42 +00:00
|
|
|
|
2014-05-18 04:29:40 +00:00
|
|
|
// Null terminate the string.
|
|
|
|
data[data_len] = 0;
|
|
|
|
|
|
|
|
callback(data, userdata);
|
|
|
|
index++;
|
|
|
|
if (is_default_dive_computer_device(name))
|
|
|
|
index = i;
|
|
|
|
}
|
|
|
|
|
|
|
|
RegCloseKey(hKey);
|
2013-09-16 21:04:42 +00:00
|
|
|
}
|
2018-08-27 17:32:14 +00:00
|
|
|
if (transport & DC_TRANSPORT_USBSTORAGE) {
|
2014-05-18 04:29:40 +00:00
|
|
|
int i;
|
2014-05-18 05:13:23 +00:00
|
|
|
int count_drives = 0;
|
2014-05-18 04:29:40 +00:00
|
|
|
const int bufdef = 512;
|
2018-08-27 15:27:07 +00:00
|
|
|
const char *dlabels[] = {"UEMISSDA", "GARMIN", NULL};
|
2014-05-18 04:29:40 +00:00
|
|
|
char bufname[bufdef], bufval[bufdef], *p;
|
|
|
|
DWORD bufname_len;
|
2013-09-16 21:04:42 +00:00
|
|
|
|
2014-05-18 04:29:40 +00:00
|
|
|
/* add drive letters that match labels */
|
|
|
|
memset(bufname, 0, bufdef);
|
|
|
|
bufname_len = bufdef;
|
|
|
|
if (GetLogicalDriveStringsA(bufname_len, bufname)) {
|
|
|
|
p = bufname;
|
|
|
|
|
|
|
|
while (*p) {
|
|
|
|
memset(bufval, 0, bufdef);
|
|
|
|
if (GetVolumeInformationA(p, bufval, bufdef, NULL, NULL, NULL, NULL, 0)) {
|
|
|
|
for (i = 0; dlabels[i] != NULL; i++)
|
|
|
|
if (!strcmp(bufval, dlabels[i])) {
|
|
|
|
char data[512];
|
|
|
|
snprintf(data, sizeof(data), "%s (%s)", p, dlabels[i]);
|
|
|
|
callback(data, userdata);
|
|
|
|
if (is_default_dive_computer_device(p))
|
2014-05-18 05:13:23 +00:00
|
|
|
index = count_drives;
|
|
|
|
count_drives++;
|
2014-05-18 04:29:40 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
p = &p[strlen(p) + 1];
|
|
|
|
}
|
2014-05-18 05:13:23 +00:00
|
|
|
if (count_drives == 1) /* we found exactly one Uemis "drive" */
|
|
|
|
index = 0; /* make it the selected "device" */
|
2014-05-18 04:29:40 +00:00
|
|
|
}
|
|
|
|
}
|
2013-09-16 21:04:42 +00:00
|
|
|
return index;
|
|
|
|
}
|
2013-12-19 13:00:50 +00:00
|
|
|
|
|
|
|
/* bellow we provide a set of wrappers for some I/O functions to use wchar_t.
|
|
|
|
* on win32 this solves the issue that we need paths to be utf-16 encoded.
|
|
|
|
*/
|
2014-02-16 21:25:02 +00:00
|
|
|
int subsurface_rename(const char *path, const char *newpath)
|
|
|
|
{
|
|
|
|
if (!path || !newpath)
|
2023-04-29 13:09:20 +00:00
|
|
|
return -1;
|
2014-02-16 21:25:02 +00:00
|
|
|
|
2023-04-29 13:09:20 +00:00
|
|
|
std::wstring wpath = utf8_to_utf16(path);
|
|
|
|
std::wstring wnewpath = utf8_to_utf16(newpath);
|
2014-02-16 21:25:02 +00:00
|
|
|
|
2023-04-29 13:09:20 +00:00
|
|
|
if (!wpath.empty() && !wnewpath.empty())
|
|
|
|
return _wrename(wpath.c_str(), wnewpath.c_str());
|
|
|
|
return -1;
|
2014-02-16 21:25:02 +00:00
|
|
|
}
|
|
|
|
|
2015-09-26 16:35:21 +00:00
|
|
|
// if the QDir based rename fails, we try this one
|
|
|
|
int subsurface_dir_rename(const char *path, const char *newpath)
|
|
|
|
{
|
|
|
|
// check if the folder exists
|
|
|
|
BOOL exists = FALSE;
|
|
|
|
DWORD attrib = GetFileAttributes(path);
|
|
|
|
if (attrib != INVALID_FILE_ATTRIBUTES && attrib & FILE_ATTRIBUTE_DIRECTORY)
|
|
|
|
exists = TRUE;
|
|
|
|
if (!exists && verbose) {
|
2024-03-24 20:03:08 +00:00
|
|
|
report_info("folder not found or path is not a folder: %s", path);
|
2015-09-26 16:35:21 +00:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
|
|
|
|
// list of error codes:
|
|
|
|
// https://msdn.microsoft.com/en-us/library/windows/desktop/ms681381(v=vs.85).aspx
|
|
|
|
DWORD errorCode;
|
|
|
|
|
|
|
|
// if this fails something has already obatained (more) exclusive access to the folder
|
|
|
|
HANDLE h = CreateFile(path, GENERIC_WRITE, FILE_SHARE_WRITE |
|
|
|
|
FILE_SHARE_DELETE, 0, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, 0);
|
|
|
|
if (h == INVALID_HANDLE_VALUE) {
|
|
|
|
errorCode = GetLastError();
|
|
|
|
if (verbose)
|
2024-03-24 20:03:08 +00:00
|
|
|
report_info("cannot obtain exclusive write access for folder: %u", (unsigned int)errorCode );
|
2015-09-26 16:35:21 +00:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
} else {
|
|
|
|
if (verbose)
|
2024-03-24 20:03:08 +00:00
|
|
|
report_info("exclusive write access obtained...closing handle!");
|
2015-09-26 16:35:21 +00:00
|
|
|
CloseHandle(h);
|
|
|
|
|
|
|
|
// attempt to rename
|
|
|
|
BOOL result = MoveFile(path, newpath);
|
|
|
|
if (!result) {
|
|
|
|
errorCode = GetLastError();
|
|
|
|
if (verbose)
|
2024-03-24 20:03:08 +00:00
|
|
|
report_info("rename failed: %u", (unsigned int)errorCode);
|
2015-09-26 16:35:21 +00:00
|
|
|
return EXIT_FAILURE;
|
|
|
|
}
|
|
|
|
if (verbose > 1)
|
2024-03-24 20:03:08 +00:00
|
|
|
report_info("folder rename success: %s ---> %s", path, newpath);
|
2015-09-26 16:35:21 +00:00
|
|
|
}
|
|
|
|
return EXIT_SUCCESS;
|
|
|
|
}
|
|
|
|
|
2013-12-19 13:00:50 +00:00
|
|
|
int subsurface_open(const char *path, int oflags, mode_t mode)
|
|
|
|
{
|
2014-01-07 14:41:19 +00:00
|
|
|
if (!path)
|
2023-04-29 13:09:20 +00:00
|
|
|
return -1;
|
|
|
|
std::wstring wpath = utf8_to_utf16(path);
|
|
|
|
if (!wpath.empty())
|
|
|
|
return _wopen(wpath.c_str(), oflags, mode);
|
|
|
|
return -1;
|
2013-12-19 13:00:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
FILE *subsurface_fopen(const char *path, const char *mode)
|
|
|
|
{
|
2014-01-07 14:41:19 +00:00
|
|
|
if (!path)
|
2023-04-29 13:09:20 +00:00
|
|
|
return NULL;
|
|
|
|
std::wstring wpath = utf8_to_utf16(path);
|
|
|
|
if (!wpath.empty()) {
|
2013-12-19 13:00:50 +00:00
|
|
|
const int len = strlen(mode);
|
2023-04-29 13:09:20 +00:00
|
|
|
std::wstring wmode(len, ' ');
|
2013-12-19 13:00:50 +00:00
|
|
|
for (int i = 0; i < len; i++)
|
|
|
|
wmode[i] = (wchar_t)mode[i];
|
2023-04-29 13:09:20 +00:00
|
|
|
return _wfopen(wpath.c_str(), wmode.c_str());
|
2013-12-19 13:00:50 +00:00
|
|
|
}
|
2023-04-29 13:09:20 +00:00
|
|
|
return NULL;
|
2013-12-19 13:00:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
/* here we return a void pointer instead of _WDIR or DIR pointer */
|
|
|
|
void *subsurface_opendir(const char *path)
|
|
|
|
{
|
2014-01-07 14:41:19 +00:00
|
|
|
if (!path)
|
2023-04-29 13:09:20 +00:00
|
|
|
return NULL;
|
|
|
|
std::wstring wpath = utf8_to_utf16(path);
|
|
|
|
if (!wpath.empty())
|
|
|
|
return (void *)_wopendir(wpath.c_str());
|
|
|
|
return NULL;
|
2013-12-19 13:00:50 +00:00
|
|
|
}
|
|
|
|
|
2014-11-13 17:36:08 +00:00
|
|
|
int subsurface_access(const char *path, int mode)
|
|
|
|
{
|
|
|
|
if (!path)
|
2023-04-29 13:09:20 +00:00
|
|
|
return -1;
|
|
|
|
std::wstring wpath = utf8_to_utf16(path);
|
|
|
|
if (!wpath.empty())
|
|
|
|
return _waccess(wpath.c_str(), mode);
|
|
|
|
return -1;
|
2017-02-24 07:06:48 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int subsurface_stat(const char* path, struct stat* buf)
|
|
|
|
{
|
|
|
|
if (!path)
|
2023-04-29 13:09:20 +00:00
|
|
|
return -1;
|
|
|
|
std::wstring wpath = utf8_to_utf16(path);
|
|
|
|
if (!wpath.empty())
|
|
|
|
return wstat(wpath.c_str(), buf);
|
|
|
|
return -1;
|
2014-11-13 17:36:08 +00:00
|
|
|
}
|
|
|
|
|
2013-12-19 13:00:50 +00:00
|
|
|
#ifndef O_BINARY
|
|
|
|
#define O_BINARY 0
|
|
|
|
#endif
|
|
|
|
|
|
|
|
struct zip *subsurface_zip_open_readonly(const char *path, int flags, int *errorp)
|
|
|
|
{
|
2013-12-20 01:01:54 +00:00
|
|
|
#if defined(LIBZIP_VERSION_MAJOR)
|
|
|
|
/* libzip 0.10 has zip_fdopen, let's use it since zip_open doesn't have a
|
|
|
|
* wchar_t version */
|
2013-12-19 13:00:50 +00:00
|
|
|
int fd = subsurface_open(path, O_RDONLY | O_BINARY, 0);
|
|
|
|
struct zip *ret = zip_fdopen(fd, flags, errorp);
|
|
|
|
if (!ret)
|
|
|
|
close(fd);
|
|
|
|
return ret;
|
2013-12-20 01:01:54 +00:00
|
|
|
#else
|
|
|
|
return zip_open(path, flags, errorp);
|
|
|
|
#endif
|
2013-12-19 13:00:50 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
int subsurface_zip_close(struct zip *zip)
|
|
|
|
{
|
|
|
|
return zip_close(zip);
|
|
|
|
}
|
2014-03-25 14:55:56 +00:00
|
|
|
|
|
|
|
/* win32 console */
|
2023-04-29 09:43:17 +00:00
|
|
|
#ifndef WIN32_CONSOLE_APP
|
2014-03-25 14:55:56 +00:00
|
|
|
static struct {
|
|
|
|
bool allocated;
|
|
|
|
UINT cp;
|
|
|
|
FILE *out, *err;
|
|
|
|
} console_desc;
|
2023-04-29 09:43:17 +00:00
|
|
|
#endif
|
2014-03-25 14:55:56 +00:00
|
|
|
|
2024-05-04 16:53:41 +00:00
|
|
|
void subsurface_console_init()
|
2014-03-25 14:55:56 +00:00
|
|
|
{
|
|
|
|
/* if this is a console app already, do nothing */
|
|
|
|
#ifndef WIN32_CONSOLE_APP
|
2017-02-02 19:50:47 +00:00
|
|
|
|
2014-03-25 14:55:56 +00:00
|
|
|
/* just in case of multiple calls */
|
|
|
|
memset((void *)&console_desc, 0, sizeof(console_desc));
|
2017-11-02 23:51:33 +00:00
|
|
|
|
|
|
|
/* if AttachConsole(ATTACH_PARENT_PROCESS) returns true the parent process
|
|
|
|
* is a terminal. based on the result, either redirect to that terminal or
|
|
|
|
* to log files.
|
2014-03-25 14:55:56 +00:00
|
|
|
*/
|
|
|
|
console_desc.allocated = AttachConsole(ATTACH_PARENT_PROCESS);
|
2017-11-02 23:51:33 +00:00
|
|
|
if (console_desc.allocated) {
|
|
|
|
console_desc.cp = GetConsoleCP();
|
|
|
|
SetConsoleOutputCP(CP_UTF8); /* make the ouput utf8 */
|
|
|
|
console_desc.out = freopen("CON", "w", stdout);
|
|
|
|
console_desc.err = freopen("CON", "w", stderr);
|
|
|
|
} else {
|
2017-11-15 20:04:03 +00:00
|
|
|
verbose = 1; /* set the verbose level to '1' */
|
2023-04-29 09:43:17 +00:00
|
|
|
std::wstring path = system_default_path();
|
|
|
|
std::wstring wpath_out = path + L"\\subsurface_out.log";
|
|
|
|
std::wstring wpath_err = path + L"\\subsurface_err.log";
|
|
|
|
console_desc.out = _wfreopen(wpath_out.c_str(), L"w", stdout);
|
|
|
|
console_desc.err = _wfreopen(wpath_err.c_str(), L"w", stderr);
|
2014-03-25 14:55:56 +00:00
|
|
|
}
|
|
|
|
|
2017-11-02 23:51:33 +00:00
|
|
|
puts(""); /* add an empty line */
|
2014-03-25 14:55:56 +00:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2024-05-04 16:53:41 +00:00
|
|
|
void subsurface_console_exit()
|
2014-03-25 14:55:56 +00:00
|
|
|
{
|
|
|
|
#ifndef WIN32_CONSOLE_APP
|
|
|
|
/* close handles */
|
|
|
|
if (console_desc.out)
|
|
|
|
fclose(console_desc.out);
|
|
|
|
if (console_desc.err)
|
|
|
|
fclose(console_desc.err);
|
|
|
|
/* reset code page and free */
|
2017-11-02 23:51:33 +00:00
|
|
|
if (console_desc.allocated) {
|
|
|
|
SetConsoleOutputCP(console_desc.cp);
|
|
|
|
FreeConsole();
|
|
|
|
}
|
2014-03-25 14:55:56 +00:00
|
|
|
#endif
|
|
|
|
}
|
2016-03-25 08:21:45 +00:00
|
|
|
|
|
|
|
bool subsurface_user_is_root()
|
|
|
|
{
|
|
|
|
/* FIXME: Detect admin rights */
|
2018-02-17 20:21:16 +00:00
|
|
|
return false;
|
2016-03-25 08:21:45 +00:00
|
|
|
}
|