OSDN Git Service

Some more code refactoring.
authorLoRd_MuldeR <mulder2@gmx.de>
Mon, 23 Nov 2015 21:28:52 +0000 (22:28 +0100)
committerLoRd_MuldeR <mulder2@gmx.de>
Mon, 23 Nov 2015 21:28:52 +0000 (22:28 +0100)
src/CPUFeatures_Win32.cpp
src/GUI_Win32.cpp
src/OSSupport_Win32.cpp
src/Terminal_Win32.cpp
src/Utils_Win32.cpp

index 9b6eb43..8fd70fd 100644 (file)
@@ -28,9 +28,6 @@
 #include <MUtils/OSSupport.h>
 #include "Utils_Win32.h"
 
-//Qt
-#include <QLibrary>
-
 MUtils::CPUFetaures::cpu_info_t MUtils::CPUFetaures::detect(void)
 {
        const OS::ArgumentMap &args = OS::arguments();
index 28957ac..1e6f6bb 100644 (file)
@@ -35,7 +35,6 @@
 #include <QApplication>
 #include <QWidget>
 #include <QReadWriteLock>
-#include <QLibrary>
 #include <Dwmapi.h>
 
 ///////////////////////////////////////////////////////////////////////////////
 ///////////////////////////////////////////////////////////////////////////////
 
 static QReadWriteLock g_themes_lock;
-static bool g_themes_initialized = false;
-static bool g_themes_enabled = false;
+static int g_themes_initialized = 0;
 
-typedef int (WINAPI IsAppThemedFunction)(void);
+typedef int (WINAPI *IsAppThemedFunction)(void);
 
 bool MUtils::GUI::themes_enabled(void)
 {
        QReadLocker readLock(&g_themes_lock);
 
-       if(g_themes_initialized)
+       if(g_themes_initialized != 0)
        {
-               return g_themes_enabled;
+               return (g_themes_initialized > 0);
        }
 
        readLock.unlock();
        QWriteLocker writeLock(&g_themes_lock);
 
-       if(g_themes_initialized)
+       if(g_themes_initialized != 0)
        {
-               return g_themes_enabled;
+               return (g_themes_initialized > 0);
        }
 
        const MUtils::OS::Version::os_version_t &osVersion = MUtils::OS::os_version();
        if(osVersion >= MUtils::OS::Version::WINDOWS_WINXP)
        {
-               QLibrary uxTheme("UxTheme.dll");
-               if(uxTheme.load())
+               const IsAppThemedFunction isAppThemedPtr = MUtils::Win32Utils::resolve<IsAppThemedFunction>(QLatin1String("UxTheme"), QLatin1String("IsAppThemed"));
+               if(isAppThemedPtr)
                {
-                       if(IsAppThemedFunction *const IsAppThemedPtr = (IsAppThemedFunction*) uxTheme.resolve("IsAppThemed"))
+                       g_themes_initialized = isAppThemedPtr() ? 1 : (-1);
+                       if(g_themes_initialized < 0)
                        {
-                               g_themes_enabled = IsAppThemedPtr();
-                               if(!g_themes_enabled)
-                               {
-                                       qWarning("Theme support is disabled for this process!");
-                               }
+                               qWarning("Theme support is disabled for this process!");
                        }
                }
        }
 
-       g_themes_initialized = true;
-       return g_themes_enabled;
+       return (g_themes_initialized > 0);
 }
 
 ///////////////////////////////////////////////////////////////////////////////
@@ -180,74 +174,22 @@ bool MUtils::GUI::bring_to_front(const unsigned long pid)
 // SHEET OF GLASS EFFECT
 ///////////////////////////////////////////////////////////////////////////////
 
-static QReadWriteLock g_dwmapi_lock;
-static QScopedPointer<QLibrary> g_dwmapi_library;
-static bool g_dwmapi_initialized = false;
-
-static struct
-{
-       HRESULT (__stdcall *dwmIsCompositionEnabled)(BOOL *bEnabled);
-       HRESULT (__stdcall *dwmExtendFrameIntoClientArea)(HWND hWnd, const MARGINS* pMarInset);
-       HRESULT (__stdcall *dwmEnableBlurBehindWindow)(HWND hWnd, const DWM_BLURBEHIND* pBlurBehind);
-}
-g_dwmapi_pointers = { NULL, NULL, NULL };
-
-static void initialize_dwmapi(void)
-{
-       QReadLocker writeLock(&g_dwmapi_lock);
-
-       //Not initialized yet?
-       if(g_dwmapi_initialized)
-       {
-               return;
-       }
-       
-       //Reset function pointers
-       g_dwmapi_pointers.dwmIsCompositionEnabled      = NULL;
-       g_dwmapi_pointers.dwmExtendFrameIntoClientArea = NULL;
-       g_dwmapi_pointers.dwmEnableBlurBehindWindow    = NULL;
-                       
-       //Does OS support DWM?
-       const MUtils::OS::Version::os_version_t &osVersion = MUtils::OS::os_version();
-       if(osVersion >= MUtils::OS::Version::WINDOWS_VISTA)
-       {
-               //Load DWMAPI.DLL
-               g_dwmapi_library.reset(new QLibrary("dwmapi.dll"));
-               if(g_dwmapi_library->load())
-               {
-                       //Initialize function pointers
-                       g_dwmapi_pointers.dwmIsCompositionEnabled      = (HRESULT (__stdcall*)(BOOL*))                       g_dwmapi_library->resolve("DwmIsCompositionEnabled");
-                       g_dwmapi_pointers.dwmExtendFrameIntoClientArea = (HRESULT (__stdcall*)(HWND, const MARGINS*))        g_dwmapi_library->resolve("DwmExtendFrameIntoClientArea");
-                       g_dwmapi_pointers.dwmEnableBlurBehindWindow    = (HRESULT (__stdcall*)(HWND, const DWM_BLURBEHIND*)) g_dwmapi_library->resolve("DwmEnableBlurBehindWindow");
-               }
-               else
-               {
-                       g_dwmapi_library.reset(NULL);
-                       qWarning("Failed to load DWMAPI.DLL on a DWM-enabled system!");
-               }
-       }
-
-       g_dwmapi_initialized = true;
-}
+typedef        HRESULT (__stdcall *DwmIsCompositionEnabledFun)      (BOOL *bEnabled);
+typedef HRESULT (__stdcall *DwmExtendFrameIntoClientAreaFun) (HWND hWnd, const MARGINS* pMarInset);
+typedef HRESULT (__stdcall *DwmEnableBlurBehindWindowFun)    (HWND hWnd, const DWM_BLURBEHIND* pBlurBehind);
 
 bool MUtils::GUI::sheet_of_glass(QWidget *const window)
 {
-       QReadLocker readLock(&g_dwmapi_lock);
-
-       //Initialize the DWM API
-       if(!g_dwmapi_initialized)
-       {
-               readLock.unlock();
-               initialize_dwmapi();
-               readLock.relock();
-       }
+       const DwmIsCompositionEnabledFun      dwmIsCompositionEnabledFun      = MUtils::Win32Utils::resolve<DwmIsCompositionEnabledFun>     (QLatin1String("dwmapi"), QLatin1String("DwmIsCompositionEnabled")     );
+       const DwmExtendFrameIntoClientAreaFun dwmExtendFrameIntoClientAreaFun = MUtils::Win32Utils::resolve<DwmExtendFrameIntoClientAreaFun>(QLatin1String("dwmapi"), QLatin1String("DwmExtendFrameIntoClientArea"));
+       const DwmEnableBlurBehindWindowFun    dwmEnableBlurBehindWindowFun    = MUtils::Win32Utils::resolve<DwmEnableBlurBehindWindowFun>   (QLatin1String("dwmapi"), QLatin1String("DwmEnableBlurBehindWindow")   );
 
        //Required functions available?
        BOOL bCompositionEnabled = FALSE;
-       if(g_dwmapi_pointers.dwmIsCompositionEnabled && g_dwmapi_pointers.dwmExtendFrameIntoClientArea && g_dwmapi_pointers.dwmEnableBlurBehindWindow)
+       if(dwmIsCompositionEnabledFun && dwmExtendFrameIntoClientAreaFun && dwmEnableBlurBehindWindowFun)
        {
                //Check if composition is currently enabled
-               if(HRESULT hr = g_dwmapi_pointers.dwmIsCompositionEnabled(&bCompositionEnabled))
+               if(HRESULT hr = dwmIsCompositionEnabledFun(&bCompositionEnabled))
                {
                        qWarning("DwmIsCompositionEnabled function has failed! (error %d)", hr);
                        return false;
@@ -262,7 +204,7 @@ bool MUtils::GUI::sheet_of_glass(QWidget *const window)
 
        //Enable the "sheet of glass" effect on this window
        MARGINS margins = {-1, -1, -1, -1};
-       if(HRESULT hr = g_dwmapi_pointers.dwmExtendFrameIntoClientArea(window->winId(), &margins))
+       if(HRESULT hr = dwmExtendFrameIntoClientAreaFun(window->winId(), &margins))
        {
                qWarning("DwmExtendFrameIntoClientArea function has failed! (error %d)", hr);
                return false;
@@ -273,7 +215,7 @@ bool MUtils::GUI::sheet_of_glass(QWidget *const window)
        memset(&bb, 0, sizeof(DWM_BLURBEHIND));
        bb.fEnable = TRUE;
        bb.dwFlags = DWM_BB_ENABLE;
-       if(HRESULT hr = g_dwmapi_pointers.dwmEnableBlurBehindWindow(window->winId(), &bb))
+       if(HRESULT hr = dwmEnableBlurBehindWindowFun(window->winId(), &bb))
        {
                qWarning("DwmEnableBlurBehindWindow function has failed! (error %d)", hr);
                return false;
@@ -289,22 +231,16 @@ bool MUtils::GUI::sheet_of_glass(QWidget *const window)
 
 bool MUtils::GUI::sheet_of_glass_update(QWidget *const window)
 {
-       QReadLocker readLock(&g_dwmapi_lock);
-
-       //Initialize the DWM API
-       if(!g_dwmapi_initialized)
-       {
-               readLock.unlock();
-               initialize_dwmapi();
-               readLock.relock();
-       }
-
+       const DwmIsCompositionEnabledFun      dwmIsCompositionEnabledFun      = MUtils::Win32Utils::resolve<DwmIsCompositionEnabledFun>     (QLatin1String("dwmapi"), QLatin1String("DwmIsCompositionEnabled")     );
+       const DwmExtendFrameIntoClientAreaFun dwmExtendFrameIntoClientAreaFun = MUtils::Win32Utils::resolve<DwmExtendFrameIntoClientAreaFun>(QLatin1String("dwmapi"), QLatin1String("DwmExtendFrameIntoClientArea"));
+       const DwmEnableBlurBehindWindowFun    dwmEnableBlurBehindWindowFun    = MUtils::Win32Utils::resolve<DwmEnableBlurBehindWindowFun>   (QLatin1String("dwmapi"), QLatin1String("DwmEnableBlurBehindWindow")   );
+       
        //Required functions available?
        BOOL bCompositionEnabled = FALSE;
-       if(g_dwmapi_pointers.dwmIsCompositionEnabled && g_dwmapi_pointers.dwmExtendFrameIntoClientArea && g_dwmapi_pointers.dwmEnableBlurBehindWindow)
+       if(dwmIsCompositionEnabledFun && dwmExtendFrameIntoClientAreaFun && dwmEnableBlurBehindWindowFun)
        {
                //Check if composition is currently enabled
-               if(HRESULT hr = g_dwmapi_pointers.dwmIsCompositionEnabled(&bCompositionEnabled))
+               if(HRESULT hr = dwmIsCompositionEnabledFun(&bCompositionEnabled))
                {
                        qWarning("DwmIsCompositionEnabled function has failed! (error %d)", hr);
                        return false;
@@ -322,7 +258,7 @@ bool MUtils::GUI::sheet_of_glass_update(QWidget *const window)
        memset(&bb, 0, sizeof(DWM_BLURBEHIND));
        bb.fEnable = TRUE;
        bb.dwFlags = DWM_BB_ENABLE;
-       if(HRESULT hr = g_dwmapi_pointers.dwmEnableBlurBehindWindow(window->winId(), &bb))
+       if(HRESULT hr = dwmEnableBlurBehindWindowFun(window->winId(), &bb))
        {
                qWarning("DwmEnableBlurBehindWindow function has failed! (error %d)", hr);
                return false;
index 88e32cb..dde3443 100644 (file)
@@ -45,7 +45,6 @@
 //Qt
 #include <QMap>
 #include <QReadWriteLock>
-#include <QLibrary>
 #include <QDir>
 #include <QWidget>
 #include <QProcess>
index 773f022..a4d5968 100644 (file)
@@ -36,7 +36,6 @@
 #include <QFile>
 #include <QStringList>
 #include <QIcon>
-#include <QLibrary>
 
 //CRT
 #include <iostream>
@@ -407,17 +406,13 @@ void MUtils::Terminal::set_icon(const QIcon &icon)
 
        if(g_terminal_attached && (!(icon.isNull() || MUtils::OS::running_on_wine())))
        {
-               QLibrary kernel32("kernel32.dll");
-               if(kernel32.load())
+               typedef DWORD(__stdcall *SetConsoleIconFun)(HICON);
+               if(const SetConsoleIconFun setConsoleIconFun = MUtils::Win32Utils::resolve<SetConsoleIconFun>(QLatin1String("kernel32"), QLatin1String("SetConsoleIcon")))
                {
-                       typedef DWORD (__stdcall *SetConsoleIconFun)(HICON);
-                       if(SetConsoleIconFun SetConsoleIconPtr = (SetConsoleIconFun) kernel32.resolve("SetConsoleIcon"))
+                       if(HICON hIcon = (HICON) MUtils::Win32Utils::qicon_to_hicon(icon, 16, 16))
                        {
-                               if(HICON hIcon = (HICON) MUtils::Win32Utils::qicon_to_hicon(icon, 16, 16))
-                               {
-                                       SetConsoleIconPtr(hIcon);
-                                       DestroyIcon(hIcon);
-                               }
+                               setConsoleIconFun(hIcon);
+                               DestroyIcon(hIcon);
                        }
                }
        }
index f891780..0be05b3 100644 (file)
@@ -63,17 +63,17 @@ static QHash<QString, LibraryItem> g_resolve_libs;
 
 uintptr_t MUtils::Win32Utils::resolve_helper(const QString &libraryName, const QString &functionName)
 {
-       QReadLocker rdLock(&g_resolve_lock);
+       const QString libraryNameFolded = libraryName.toCaseFolded().trimmed();
+       const QString functionIdTrimmed = functionName.trimmed();
 
        //Fuction already loaded?
-       const QString libNameLower = libraryName.toLower();
-       if (g_resolve_libs.contains(libNameLower))
+       QReadLocker rdLock(&g_resolve_lock);
+       if (g_resolve_libs.contains(libraryNameFolded))
        {
-               LibraryItem &lib = g_resolve_libs[libNameLower];
-               if (lib.second.contains(functionName))
+               LibraryItem &lib = g_resolve_libs[libraryNameFolded];
+               if (lib.second.contains(functionIdTrimmed))
                {
-                       qWarning("TEST: Function already there!");
-                       return lib.second[functionName];
+                       return lib.second[functionIdTrimmed];
                }
        }
 
@@ -82,34 +82,35 @@ uintptr_t MUtils::Win32Utils::resolve_helper(const QString &libraryName, const Q
        QWriteLocker wrLock(&g_resolve_lock);
 
        //Load library
-       while (!g_resolve_libs.contains(libNameLower))
+       if (!g_resolve_libs.contains(libraryNameFolded))
        {
-               qWarning("TEST: Library not there -> going to load now!");
-               QSharedPointer<QLibrary> lib(new QLibrary(libNameLower));
+               QSharedPointer<QLibrary> lib(new QLibrary(libraryNameFolded));
                if (!(lib->isLoaded() || lib->load()))
                {
-                       qWarning("Failed to load library: \"%s\"", MUTILS_UTF8(libNameLower));
-                       return NULL;
+                       qWarning("Failed to load dynamic library: %s", MUTILS_UTF8(libraryNameFolded));
+                       lib.clear();
                }
-               g_resolve_libs.insert(libNameLower, qMakePair(lib, FunctionMap()));
+               g_resolve_libs.insert(libraryNameFolded, qMakePair(lib, FunctionMap()));
+       }
+
+       //Is library available?
+       LibraryItem &lib = g_resolve_libs[libraryNameFolded];
+       if (lib.first.isNull() || (!lib.first->isLoaded()))
+       {
+               return NULL; /*library unavailable*/
        }
 
        //Lookup the function
-       LibraryItem &lib = g_resolve_libs[libNameLower];
-       while (!lib.second.contains(functionName))
+       if (!lib.second.contains(functionIdTrimmed))
        {
-               qWarning("TEST: Function not there -> going to resolve now!");
-               void *const ptr = lib.first->resolve(functionName.toLatin1().constData());
+               void *const ptr = lib.first->resolve(functionIdTrimmed.toLatin1().constData());
                if (!ptr)
                {
-                       lib.second.insert(functionName, NULL);
-                       qWarning("Failed to resolve function: \"%s\"", MUTILS_UTF8(functionName));
-                       return NULL;
+                       qWarning("Failed to resolve function: %s::%s", MUTILS_UTF8(libraryNameFolded), MUTILS_UTF8(functionIdTrimmed));
                }
-               qWarning("TEST: Function resolved to 0x%p", ptr);
-               lib.second.insert(functionName, reinterpret_cast<uintptr_t>(ptr));
+               lib.second.insert(functionIdTrimmed, reinterpret_cast<uintptr_t>(ptr));
        }
 
        //Return function pointer
-       return lib.second[functionName];
+       return lib.second[functionIdTrimmed];
 }