///////////////////////////////////////////////////////////////////////////////
// MuldeR's Utilities for Qt
-// Copyright (C) 2004-2016 LoRd_MuldeR <MuldeR2@GMX.de>
+// Copyright (C) 2004-2023 LoRd_MuldeR <MuldeR2@GMX.de>
//
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
static MUtils::Internal::CriticalSection g_terminal_lock;
//Is terminal attached?
-static volatile bool g_terminal_attached = false;
+static QAtomicInt g_terminal_attached;
//Terminal output buffer
static const size_t BUFF_SIZE = 8192;
//The log file
static QScopedPointer<QFile> g_terminal_log_file;
+//Terminal icon
+static HICON g_terminal_icon = NULL;
+
///////////////////////////////////////////////////////////////////////////////
// HELPER FUNCTIONS
///////////////////////////////////////////////////////////////////////////////
return out;
}
+static inline void set_hicon(HICON *const ptr, const HICON val)
+{
+ if (*ptr)
+ {
+ DestroyIcon(*ptr);
+ }
+ *ptr = val;
+}
+
///////////////////////////////////////////////////////////////////////////////
// TERMINAL SETUP
///////////////////////////////////////////////////////////////////////////////
{
MUtils::Internal::CSLocker lock(g_terminal_lock);
- if (g_terminal_attached)
+ if (g_terminal_attached.fetchAndStoreOrdered(0) > 0)
{
g_fileBuf_stdout.reset();
g_fileBuf_stderr.reset();
if(stdout) freopen_s(&temp[0], "NUL", "wb", stdout);
if(stderr) freopen_s(&temp[1], "NUL", "wb", stderr);
FreeConsole();
- g_terminal_attached = false;
+ set_hicon(&g_terminal_icon, NULL);
}
}
g_terminal_log_file.reset(new QFile(MUTILS_QSTR(logfile)));
if(g_terminal_log_file->open(QIODevice::WriteOnly))
{
- static const char MARKER[3] = { char(0xEF), char(0xBB), char(0xBF) };
+ static const char MARKER[3] = { '\xEF', '\xBB', '\xBF' };
g_terminal_log_file->write(MARKER, 3);
}
free(logfile);
if(enableConsole)
{
- if(!g_terminal_attached)
+ if(!g_terminal_attached.fetchAndStoreOrdered(1))
{
if(AllocConsole() != FALSE)
{
_snprintf_s(title, 128, _TRUNCATE, "%s | Debug Console", appName);
SetConsoleTitleA(title);
}
- g_terminal_attached = true;
+ }
+ else
+ {
+ g_terminal_attached.fetchAndStoreOrdered(0); /*failed*/
}
}
- if(g_terminal_attached)
+ if(MUTILS_BOOLIFY(g_terminal_attached))
{
g_fileBuf_stdout.reset(terminal_connect(stdout, std::cout));
g_fileBuf_stderr.reset(terminal_connect(stderr, std::cerr));
{
MUtils::Internal::CSLocker lock(g_terminal_lock);
- if(g_terminal_attached)
+ if(MUTILS_BOOLIFY(g_terminal_attached))
{
write_to_terminal(type, message);
}
{
MUtils::Internal::CSLocker lock(g_terminal_lock);
- if(g_terminal_attached && (!(icon.isNull() || MUtils::OS::running_on_wine())))
+ if(MUTILS_BOOLIFY(g_terminal_attached) && (!(icon.isNull() || MUtils::OS::running_on_wine())))
{
- typedef DWORD(__stdcall *SetConsoleIconFun)(HICON);
- if(const SetConsoleIconFun setConsoleIconFun = MUtils::Win32Utils::resolve<SetConsoleIconFun>(QLatin1String("kernel32"), QLatin1String("SetConsoleIcon")))
+ if(const HICON hIcon = (HICON) MUtils::Win32Utils::qicon_to_hicon(&icon, 16, 16))
{
- if(HICON hIcon = (HICON) MUtils::Win32Utils::qicon_to_hicon(icon, 16, 16))
+ typedef BOOL(__stdcall *SetConsoleIconFun)(HICON);
+ bool success = false;
+ if (const SetConsoleIconFun pSetConsoleIconFun = MUtils::Win32Utils::resolve<SetConsoleIconFun>(QLatin1String("kernel32"), QLatin1String("SetConsoleIcon")))
+ {
+ //const DWORD before = GetLastError();
+ if (pSetConsoleIconFun(hIcon))
+ {
+ success = true;
+ }
+ else
+ {
+ const DWORD error = GetLastError();
+ qWarning("SetConsoleIcon() has failed! [Error: 0x%08X]", error);
+ }
+ }
+ if (!success)
+ {
+ const HWND hwndConsole = GetConsoleWindow();
+ if ((hwndConsole != NULL) && (hwndConsole != INVALID_HANDLE_VALUE))
+ {
+ SendMessage(hwndConsole, WM_SETICON, ICON_SMALL, LPARAM(hIcon));
+ success = true;
+ }
+ }
+ if (success)
{
- setConsoleIconFun(hIcon);
- DestroyIcon(hIcon);
+ set_hicon(&g_terminal_icon, hIcon);
}
}
}