OSDN Git Service

Bump version.
[mutilities/MUtilities.git] / src / Terminal_Win32.cpp
index ceb2530..7676145 100644 (file)
@@ -1,6 +1,6 @@
 ///////////////////////////////////////////////////////////////////////////////
 // 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
@@ -59,7 +59,7 @@
 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;
@@ -72,6 +72,9 @@ static QScopedPointer<std::filebuf> g_fileBuf_stderr;
 //The log file
 static QScopedPointer<QFile> g_terminal_log_file;
 
+//Terminal icon
+static HICON g_terminal_icon = NULL;
+
 ///////////////////////////////////////////////////////////////////////////////
 // HELPER FUNCTIONS
 ///////////////////////////////////////////////////////////////////////////////
@@ -145,6 +148,15 @@ static inline size_t clean_string(char *const str)
        return out;
 }
 
+static inline void set_hicon(HICON *const ptr, const HICON val)
+{
+       if (*ptr)
+       {
+               DestroyIcon(*ptr);
+       }
+       *ptr = val;
+}
+
 ///////////////////////////////////////////////////////////////////////////////
 // TERMINAL SETUP
 ///////////////////////////////////////////////////////////////////////////////
@@ -164,7 +176,7 @@ static void terminal_shutdown(void)
 {
        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();
@@ -172,7 +184,7 @@ static void terminal_shutdown(void)
                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);
        }
 }
 
@@ -191,7 +203,7 @@ void MUtils::Terminal::setup(int &argc, char **argv, const char* const appName,
                                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);
@@ -216,7 +228,7 @@ void MUtils::Terminal::setup(int &argc, char **argv, const char* const appName,
 
        if(enableConsole)
        {
-               if(!g_terminal_attached)
+               if(!g_terminal_attached.fetchAndStoreOrdered(1))
                {
                        if(AllocConsole() != FALSE)
                        {
@@ -228,11 +240,14 @@ void MUtils::Terminal::setup(int &argc, char **argv, const char* const appName,
                                        _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));
@@ -381,7 +396,7 @@ void MUtils::Terminal::write(const int &type, const char *const message)
 {
        MUtils::Internal::CSLocker lock(g_terminal_lock);
 
-       if(g_terminal_attached)
+       if(MUTILS_BOOLIFY(g_terminal_attached))
        {
                write_to_terminal(type, message);
        }
@@ -404,15 +419,37 @@ void MUtils::Terminal::set_icon(const QIcon &icon)
 {
        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);
                        }
                }
        }