]> git.saurik.com Git - wxWidgets.git/blob - src/common/dynlib.cpp
Fixed doubled-up key effects in wxTextCtrl by resetting m_lastMsg to 0
[wxWidgets.git] / src / common / dynlib.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: dynlib.cpp
3 // Purpose: Dynamic library management
4 // Author: Guilhem Lavaux
5 // Modified by:
6 // Created: 20/07/98
7 // RCS-ID: $Id$
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows license
10 /////////////////////////////////////////////////////////////////////////////
11
12 // ============================================================================
13 // declarations
14 // ============================================================================
15
16 // ----------------------------------------------------------------------------
17 // headers
18 // ----------------------------------------------------------------------------
19
20 #ifdef __GNUG__
21 #pragma implementation "dynlib.h"
22 #endif
23
24 #include "wx/wxprec.h"
25
26 #ifdef __BORLANDC__
27 #pragma hdrstop
28 #endif //__BORLANDC__
29
30 #if wxUSE_DYNLIB_CLASS
31
32 #include "wx/dynlib.h"
33 #include "wx/filefn.h"
34 #include "wx/intl.h"
35 #include "wx/log.h"
36 #include "wx/tokenzr.h"
37
38 // ----------------------------------------------------------------------------
39 // conditional compilation
40 // ----------------------------------------------------------------------------
41
42 #if defined(HAVE_DLOPEN)
43 #define wxDllOpen(lib) dlopen(lib, RTLD_LAZY)
44 #define wxDllGetSymbol(handle, name) dlsym(handle, (char *)name)
45 #define wxDllClose dlclose
46 #elif defined(HAVE_SHL_LOAD)
47 #define wxDllOpen(lib) shl_load(lib, BIND_DEFERRED, 0)
48 #define wxDllClose shl_unload
49
50 static inline void *wxDllGetSymbol(shl_t handle, const char *name)
51 {
52 void *sym;
53 if ( shl_findsym(&handle, name, TYPE_UNDEFINED, &sym) == 0 )
54 return sym;
55 else
56 return (void *)0;
57 }
58 #elif defined(__WINDOWS__)
59 #include <windows.h>
60
61 // using LoadLibraryEx under Win32 to avoid name clash with LoadLibrary
62 #ifdef __WIN32__
63 #define wxDllOpen(lib) ::LoadLibraryEx(lib, 0, 0)
64 #else // Win16
65 #define wxDllOpen(lib) ::LoadLibrary(lib)
66 #endif // Win32/16
67 #define wxDllGetSymbol(handle, name) ::GetProcAddress(handle, name)
68 #define wxDllClose ::FreeLibrary
69 #else
70 #error "Don't know how to load shared libraries on this platform."
71 #endif // OS
72
73 // ---------------------------------------------------------------------------
74 // Global variables
75 // ---------------------------------------------------------------------------
76
77 wxLibraries wxTheLibraries;
78
79 // ----------------------------------------------------------------------------
80 // private functions
81 // ----------------------------------------------------------------------------
82
83 // construct the full name from the base shared object name: adds a .dll
84 // suffix under Windows or .so under Unix
85 static wxString ConstructLibraryName(const wxString& basename)
86 {
87 wxString fullname(basename);
88
89 #if defined(__UNIX__)
90 #if defined(__HPUX__)
91 fullname << ".sl";
92 #else //__HPUX__
93 fullname << ".so";
94 #endif //__HPUX__
95 #elif defined(__WINDOWS__)
96 fullname << ".dll";
97 #endif
98
99 return fullname;
100 }
101
102 // ============================================================================
103 // implementation
104 // ============================================================================
105
106 // ---------------------------------------------------------------------------
107 // wxLibrary (one instance per dynamic library)
108 // ---------------------------------------------------------------------------
109
110 wxLibrary::wxLibrary(wxDllType handle)
111 {
112 typedef wxClassInfo *(*t_get_first)(void);
113 t_get_first get_first;
114
115 m_handle = handle;
116
117 // Some system may use a local heap for library.
118 get_first = (t_get_first)GetSymbol("wxGetClassFirst");
119 // It is a wxWindows DLL.
120 if (get_first)
121 PrepareClasses(get_first());
122 }
123
124 wxLibrary::~wxLibrary()
125 {
126 if ( m_handle )
127 {
128 wxDllClose(m_handle);
129 }
130 }
131
132 wxObject *wxLibrary::CreateObject(const wxString& name)
133 {
134 wxClassInfo *info = (wxClassInfo *)classTable.Get(name);
135
136 if (!info)
137 return NULL;
138
139 return info->CreateObject();
140 }
141
142 void wxLibrary::PrepareClasses(wxClassInfo *first)
143 {
144 // Index all class infos by their class name
145 wxClassInfo *info = first;
146 while (info)
147 {
148 if (info->m_className)
149 classTable.Put(info->m_className, (wxObject *)info);
150 info = info->GetNext();
151 }
152
153 // Set base pointers for each wxClassInfo
154 info = first;
155 while (info)
156 {
157 if (info->GetBaseClassName1())
158 info->m_baseInfo1 = (wxClassInfo *)classTable.Get(info->GetBaseClassName1());
159 if (info->GetBaseClassName2())
160 info->m_baseInfo2 = (wxClassInfo *)classTable.Get(info->GetBaseClassName2());
161 info = info->m_next;
162 }
163 }
164
165 void *wxLibrary::GetSymbol(const wxString& symbname)
166 {
167 void *symbol = NULL; // return value
168
169 #if defined( __WXMAC__ )
170 Ptr symAddress ;
171 CFragSymbolClass symClass ;
172 Str255 symName ;
173
174 strcpy( (char*) symName , symbname ) ;
175 c2pstr( (char*) symName ) ;
176
177 if ( FindSymbol( m_handle , symName , &symAddress , &symClass ) == noErr )
178 {
179 symbol = (void *)symAddress ;
180 }
181 #else
182 // VZ: hmm... why is WXSTRINGCAST needed? if it's really modified, we
183 // should make a copy of it
184 symbol = wxDllGetSymbol(m_handle, WXSTRINGCAST symbname);
185 #endif
186
187 if ( !symbol )
188 {
189 wxLogSysError(_("Couldn't find symbol '%s' in a dynamic library"),
190 symbname.c_str());
191 }
192
193 return symbol;
194 }
195
196 // ---------------------------------------------------------------------------
197 // wxLibraries (only one instance should normally exist)
198 // ---------------------------------------------------------------------------
199
200 wxLibraries::wxLibraries()
201 {
202 }
203
204 wxLibraries::~wxLibraries()
205 {
206 wxNode *node = m_loaded.First();
207
208 while (node) {
209 wxLibrary *lib = (wxLibrary *)node->Data();
210 delete lib;
211
212 node = node->Next();
213 }
214 }
215
216 wxLibrary *wxLibraries::LoadLibrary(const wxString& name)
217 {
218 wxNode *node;
219 wxLibrary *lib;
220 wxClassInfo *old_sm_first;
221
222 if ( (node = m_loaded.Find(name.GetData())) )
223 return ((wxLibrary *)node->Data());
224
225 // If DLL shares data, this is necessary.
226 old_sm_first = wxClassInfo::sm_first;
227 wxClassInfo::sm_first = NULL;
228
229 wxString lib_name = ConstructLibraryName(name);
230
231 #if defined(__UNIX__)
232 // found the first file in LD_LIBRARY_PATH with this name
233 wxString libPath("/lib:/usr/lib"); // system path first
234 const char *envLibPath = getenv("LD_LIBRARY_PATH");
235 if ( envLibPath )
236 libPath << ':' << envLibPath;
237 wxStringTokenizer tokenizer(libPath, ':');
238 while ( tokenizer.HasMoreToken() )
239 {
240 wxString fullname(tokenizer.NextToken());
241
242 fullname << '/' << lib_name;
243 if ( wxFileExists(fullname) )
244 {
245 lib_name = fullname;
246
247 // found the library
248 break;
249 }
250 }
251 //else: not found in the path, leave the name as is (secutiry risk?)
252
253 #endif // __UNIX__
254
255 wxDllType handle;
256
257 #if defined(__WXMAC__)
258 FSSpec myFSSpec ;
259 Ptr myMainAddr ;
260 Str255 myErrName ;
261
262 wxMacPathToFSSpec( lib_name , &myFSSpec ) ;
263 if (GetDiskFragment( &myFSSpec , 0 , kCFragGoesToEOF , "\p" , kPrivateCFragCopy , &handle , &myMainAddr ,
264 myErrName ) != noErr )
265 {
266 p2cstr( myErrName ) ;
267 wxASSERT_MSG( 1 , (char*)myErrName ) ;
268 return NULL ;
269 }
270 #else // !Mac
271 handle = wxDllOpen(lib_name);
272 #endif // OS
273
274 if ( !handle )
275 {
276 wxLogSysError(_("Failed to load shared library '%s'"),
277 lib_name.c_str());
278
279 return NULL;
280 }
281
282 lib = new wxLibrary(handle);
283
284 wxClassInfo::sm_first = old_sm_first;
285
286 m_loaded.Append(name.GetData(), lib);
287
288 return lib;
289 }
290
291 wxObject *wxLibraries::CreateObject(const wxString& path)
292 {
293 wxNode *node = m_loaded.First();
294 wxObject *obj;
295
296 while (node) {
297 obj = ((wxLibrary *)node->Data())->CreateObject(path);
298 if (obj)
299 return obj;
300
301 node = node->Next();
302 }
303 return NULL;
304 }
305
306 #endif // wxUSE_DYNLIB_CLASS