Some more code refactoring.

This commit is contained in:
LoRd_MuldeR 2015-11-23 22:28:52 +01:00
parent 7f5d618eba
commit 670d4dc36c
5 changed files with 58 additions and 130 deletions

View File

@ -28,9 +28,6 @@
#include <MUtils/OSSupport.h> #include <MUtils/OSSupport.h>
#include "Utils_Win32.h" #include "Utils_Win32.h"
//Qt
#include <QLibrary>
MUtils::CPUFetaures::cpu_info_t MUtils::CPUFetaures::detect(void) MUtils::CPUFetaures::cpu_info_t MUtils::CPUFetaures::detect(void)
{ {
const OS::ArgumentMap &args = OS::arguments(); const OS::ArgumentMap &args = OS::arguments();

View File

@ -35,7 +35,6 @@
#include <QApplication> #include <QApplication>
#include <QWidget> #include <QWidget>
#include <QReadWriteLock> #include <QReadWriteLock>
#include <QLibrary>
#include <Dwmapi.h> #include <Dwmapi.h>
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
@ -43,47 +42,42 @@
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
static QReadWriteLock g_themes_lock; static QReadWriteLock g_themes_lock;
static bool g_themes_initialized = false; static int g_themes_initialized = 0;
static bool g_themes_enabled = false;
typedef int (WINAPI IsAppThemedFunction)(void); typedef int (WINAPI *IsAppThemedFunction)(void);
bool MUtils::GUI::themes_enabled(void) bool MUtils::GUI::themes_enabled(void)
{ {
QReadLocker readLock(&g_themes_lock); 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(); readLock.unlock();
QWriteLocker writeLock(&g_themes_lock); 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(); const MUtils::OS::Version::os_version_t &osVersion = MUtils::OS::os_version();
if(osVersion >= MUtils::OS::Version::WINDOWS_WINXP) if(osVersion >= MUtils::OS::Version::WINDOWS_WINXP)
{ {
QLibrary uxTheme("UxTheme.dll"); const IsAppThemedFunction isAppThemedPtr = MUtils::Win32Utils::resolve<IsAppThemedFunction>(QLatin1String("UxTheme"), QLatin1String("IsAppThemed"));
if(uxTheme.load()) 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_initialized > 0);
return g_themes_enabled;
} }
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
@ -180,74 +174,22 @@ bool MUtils::GUI::bring_to_front(const unsigned long pid)
// SHEET OF GLASS EFFECT // SHEET OF GLASS EFFECT
/////////////////////////////////////////////////////////////////////////////// ///////////////////////////////////////////////////////////////////////////////
static QReadWriteLock g_dwmapi_lock; typedef HRESULT (__stdcall *DwmIsCompositionEnabledFun) (BOOL *bEnabled);
static QScopedPointer<QLibrary> g_dwmapi_library; typedef HRESULT (__stdcall *DwmExtendFrameIntoClientAreaFun) (HWND hWnd, const MARGINS* pMarInset);
static bool g_dwmapi_initialized = false; typedef HRESULT (__stdcall *DwmEnableBlurBehindWindowFun) (HWND hWnd, const DWM_BLURBEHIND* pBlurBehind);
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;
}
bool MUtils::GUI::sheet_of_glass(QWidget *const window) bool MUtils::GUI::sheet_of_glass(QWidget *const window)
{ {
QReadLocker readLock(&g_dwmapi_lock); const DwmIsCompositionEnabledFun dwmIsCompositionEnabledFun = MUtils::Win32Utils::resolve<DwmIsCompositionEnabledFun> (QLatin1String("dwmapi"), QLatin1String("DwmIsCompositionEnabled") );
const DwmExtendFrameIntoClientAreaFun dwmExtendFrameIntoClientAreaFun = MUtils::Win32Utils::resolve<DwmExtendFrameIntoClientAreaFun>(QLatin1String("dwmapi"), QLatin1String("DwmExtendFrameIntoClientArea"));
//Initialize the DWM API const DwmEnableBlurBehindWindowFun dwmEnableBlurBehindWindowFun = MUtils::Win32Utils::resolve<DwmEnableBlurBehindWindowFun> (QLatin1String("dwmapi"), QLatin1String("DwmEnableBlurBehindWindow") );
if(!g_dwmapi_initialized)
{
readLock.unlock();
initialize_dwmapi();
readLock.relock();
}
//Required functions available? //Required functions available?
BOOL bCompositionEnabled = FALSE; 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 //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); qWarning("DwmIsCompositionEnabled function has failed! (error %d)", hr);
return false; return false;
@ -262,7 +204,7 @@ bool MUtils::GUI::sheet_of_glass(QWidget *const window)
//Enable the "sheet of glass" effect on this window //Enable the "sheet of glass" effect on this window
MARGINS margins = {-1, -1, -1, -1}; 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); qWarning("DwmExtendFrameIntoClientArea function has failed! (error %d)", hr);
return false; return false;
@ -273,7 +215,7 @@ bool MUtils::GUI::sheet_of_glass(QWidget *const window)
memset(&bb, 0, sizeof(DWM_BLURBEHIND)); memset(&bb, 0, sizeof(DWM_BLURBEHIND));
bb.fEnable = TRUE; bb.fEnable = TRUE;
bb.dwFlags = DWM_BB_ENABLE; 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); qWarning("DwmEnableBlurBehindWindow function has failed! (error %d)", hr);
return false; 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) bool MUtils::GUI::sheet_of_glass_update(QWidget *const window)
{ {
QReadLocker readLock(&g_dwmapi_lock); const DwmIsCompositionEnabledFun dwmIsCompositionEnabledFun = MUtils::Win32Utils::resolve<DwmIsCompositionEnabledFun> (QLatin1String("dwmapi"), QLatin1String("DwmIsCompositionEnabled") );
const DwmExtendFrameIntoClientAreaFun dwmExtendFrameIntoClientAreaFun = MUtils::Win32Utils::resolve<DwmExtendFrameIntoClientAreaFun>(QLatin1String("dwmapi"), QLatin1String("DwmExtendFrameIntoClientArea"));
//Initialize the DWM API const DwmEnableBlurBehindWindowFun dwmEnableBlurBehindWindowFun = MUtils::Win32Utils::resolve<DwmEnableBlurBehindWindowFun> (QLatin1String("dwmapi"), QLatin1String("DwmEnableBlurBehindWindow") );
if(!g_dwmapi_initialized)
{
readLock.unlock();
initialize_dwmapi();
readLock.relock();
}
//Required functions available? //Required functions available?
BOOL bCompositionEnabled = FALSE; 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 //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); qWarning("DwmIsCompositionEnabled function has failed! (error %d)", hr);
return false; return false;
@ -322,7 +258,7 @@ bool MUtils::GUI::sheet_of_glass_update(QWidget *const window)
memset(&bb, 0, sizeof(DWM_BLURBEHIND)); memset(&bb, 0, sizeof(DWM_BLURBEHIND));
bb.fEnable = TRUE; bb.fEnable = TRUE;
bb.dwFlags = DWM_BB_ENABLE; 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); qWarning("DwmEnableBlurBehindWindow function has failed! (error %d)", hr);
return false; return false;

View File

@ -45,7 +45,6 @@
//Qt //Qt
#include <QMap> #include <QMap>
#include <QReadWriteLock> #include <QReadWriteLock>
#include <QLibrary>
#include <QDir> #include <QDir>
#include <QWidget> #include <QWidget>
#include <QProcess> #include <QProcess>

View File

@ -36,7 +36,6 @@
#include <QFile> #include <QFile>
#include <QStringList> #include <QStringList>
#include <QIcon> #include <QIcon>
#include <QLibrary>
//CRT //CRT
#include <iostream> #include <iostream>
@ -407,18 +406,14 @@ void MUtils::Terminal::set_icon(const QIcon &icon)
if(g_terminal_attached && (!(icon.isNull() || MUtils::OS::running_on_wine()))) if(g_terminal_attached && (!(icon.isNull() || MUtils::OS::running_on_wine())))
{ {
QLibrary kernel32("kernel32.dll"); typedef DWORD(__stdcall *SetConsoleIconFun)(HICON);
if(kernel32.load()) 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); setConsoleIconFun(hIcon);
DestroyIcon(hIcon); DestroyIcon(hIcon);
} }
} }
} }
}
} }

View File

@ -63,17 +63,17 @@ static QHash<QString, LibraryItem> g_resolve_libs;
uintptr_t MUtils::Win32Utils::resolve_helper(const QString &libraryName, const QString &functionName) 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? //Fuction already loaded?
const QString libNameLower = libraryName.toLower(); QReadLocker rdLock(&g_resolve_lock);
if (g_resolve_libs.contains(libNameLower)) if (g_resolve_libs.contains(libraryNameFolded))
{ {
LibraryItem &lib = g_resolve_libs[libNameLower]; LibraryItem &lib = g_resolve_libs[libraryNameFolded];
if (lib.second.contains(functionName)) if (lib.second.contains(functionIdTrimmed))
{ {
qWarning("TEST: Function already there!"); return lib.second[functionIdTrimmed];
return lib.second[functionName];
} }
} }
@ -82,34 +82,35 @@ uintptr_t MUtils::Win32Utils::resolve_helper(const QString &libraryName, const Q
QWriteLocker wrLock(&g_resolve_lock); QWriteLocker wrLock(&g_resolve_lock);
//Load library //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(libraryNameFolded));
QSharedPointer<QLibrary> lib(new QLibrary(libNameLower));
if (!(lib->isLoaded() || lib->load())) if (!(lib->isLoaded() || lib->load()))
{ {
qWarning("Failed to load library: \"%s\"", MUTILS_UTF8(libNameLower)); qWarning("Failed to load dynamic library: %s", MUTILS_UTF8(libraryNameFolded));
return NULL; 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 //Lookup the function
LibraryItem &lib = g_resolve_libs[libNameLower]; if (!lib.second.contains(functionIdTrimmed))
while (!lib.second.contains(functionName))
{ {
qWarning("TEST: Function not there -> going to resolve now!"); void *const ptr = lib.first->resolve(functionIdTrimmed.toLatin1().constData());
void *const ptr = lib.first->resolve(functionName.toLatin1().constData());
if (!ptr) if (!ptr)
{ {
lib.second.insert(functionName, NULL); qWarning("Failed to resolve function: %s::%s", MUTILS_UTF8(libraryNameFolded), MUTILS_UTF8(functionIdTrimmed));
qWarning("Failed to resolve function: \"%s\"", MUTILS_UTF8(functionName));
return NULL;
} }
qWarning("TEST: Function resolved to 0x%p", ptr); lib.second.insert(functionIdTrimmed, reinterpret_cast<uintptr_t>(ptr));
lib.second.insert(functionName, reinterpret_cast<uintptr_t>(ptr));
} }
//Return function pointer //Return function pointer
return lib.second[functionName]; return lib.second[functionIdTrimmed];
} }