OSDN Git Service

Bump version.
[mutilities/MUtilities.git] / src / Utils_Win32.cpp
1 ///////////////////////////////////////////////////////////////////////////////
2 // MuldeR's Utilities for Qt
3 // Copyright (C) 2004-2019 LoRd_MuldeR <MuldeR2@GMX.de>
4 //
5 // This library is free software; you can redistribute it and/or
6 // modify it under the terms of the GNU Lesser General Public
7 // License as published by the Free Software Foundation; either
8 // version 2.1 of the License, or (at your option) any later version.
9 //
10 // This library is distributed in the hope that it will be useful,
11 // but WITHOUT ANY WARRANTY; without even the implied warranty of
12 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13 // Lesser General Public License for more details.
14 //
15 // You should have received a copy of the GNU Lesser General Public
16 // License along with this library; if not, write to the Free Software
17 // Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301  USA
18 //
19 // http://www.gnu.org/licenses/lgpl-2.1.txt
20 //////////////////////////////////////////////////////////////////////////////////
21
22 #include "Utils_Win32.h"
23
24 //Win32 API
25 #ifndef _INC_WINDOWS
26 #define WIN32_LEAN_AND_MEAN 1
27 #include <Windows.h>
28 #include <ObjIdl.h>  // required by QWinMime in QtWinExtras
29 #endif //_INC_WINDOWS
30
31 //Qt
32 #include <QIcon>
33 #include <QPair>
34 #include <QReadWriteLock>
35 #include <QLibrary>
36 #include <QHash>
37 #if QT_VERSION > QT_VERSION_CHECK(5,0,0)
38 #include <QtWinExtras>
39 #endif
40
41 //Qt5 support
42 #if QT_VERSION > QT_VERSION_CHECK(5,0,0)
43 #define PIXMAP2HICON(X) QtWin::toHICON((X))
44 #else
45 #define PIXMAP2HICON(X) (X).toWinHICON()
46 #endif
47
48 ///////////////////////////////////////////////////////////////////////////////
49 // QICON TO HICON
50 ///////////////////////////////////////////////////////////////////////////////
51
52 uintptr_t MUtils::Win32Utils::qicon_to_hicon(const QIcon *const icon, const int w, const int h)
53 {
54         if(!icon->isNull())
55         {
56                 QPixmap pixmap = icon->pixmap(w, h);
57                 if(!pixmap.isNull())
58                 {
59                         return (uintptr_t) PIXMAP2HICON(pixmap);
60                 }
61         }
62         return NULL;
63 }
64
65 ///////////////////////////////////////////////////////////////////////////////
66 // RESOLVE FUNCTION
67 ///////////////////////////////////////////////////////////////////////////////
68
69 typedef QHash<QString, uintptr_t> FunctionMap;
70 typedef QPair<QSharedPointer<QLibrary>, FunctionMap> LibraryItem;
71
72 static QReadWriteLock              g_resolve_lock;
73 static QHash<QString, LibraryItem> g_resolve_libs;
74
75 const uintptr_t &MUtils::Win32Utils::resolve_helper(const QString &libraryName, const QString &functionName)
76 {
77         const QString libraryNameFolded = libraryName.toCaseFolded().trimmed();
78         const QString functionIdTrimmed = functionName.trimmed();
79
80         //Fuction already loaded?
81         QReadLocker rdLock(&g_resolve_lock);
82         if (g_resolve_libs.contains(libraryNameFolded))
83         {
84                 LibraryItem &lib = g_resolve_libs[libraryNameFolded];
85                 if (lib.second.contains(functionIdTrimmed))
86                 {
87                         return lib.second[functionIdTrimmed];
88                 }
89         }
90
91         //Accquire write access!
92         rdLock.unlock();
93         QWriteLocker wrLock(&g_resolve_lock);
94
95         //Load library
96         if (!g_resolve_libs.contains(libraryNameFolded))
97         {
98                 QSharedPointer<QLibrary> lib(new QLibrary(libraryNameFolded));
99                 if (!(lib->isLoaded() || lib->load()))
100                 {
101                         qWarning("Failed to load dynamic library: %s", MUTILS_UTF8(libraryNameFolded));
102                         lib.clear();
103                 }
104                 g_resolve_libs.insert(libraryNameFolded, qMakePair(lib, FunctionMap()));
105         }
106
107         //Is library available?
108         LibraryItem &lib = g_resolve_libs[libraryNameFolded];
109         if (lib.first.isNull() || (!lib.first->isLoaded()))
110         {
111                 static const uintptr_t null = NULL;
112                 return null; /*library unavailable*/
113         }
114
115         //Lookup the function
116         if (!lib.second.contains(functionIdTrimmed))
117         {
118                 void *const ptr = lib.first->resolve(functionIdTrimmed.toLatin1().constData());
119                 if (!ptr)
120                 {
121                         qWarning("Failed to resolve function: %s::%s", MUTILS_UTF8(libraryNameFolded), MUTILS_UTF8(functionIdTrimmed));
122                 }
123                 lib.second.insert(functionIdTrimmed, reinterpret_cast<uintptr_t>(ptr));
124         }
125
126         //Return function pointer
127         return lib.second[functionIdTrimmed];
128 }