#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);
}
///////////////////////////////////////////////////////////////////////////////
// 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;
//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;
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;
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;
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;
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];
}
}
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];
}