]> git.saurik.com Git - wxWidgets.git/blob - src/common/dynlib.cpp
merged fixes for accel and command event non propagation from 2.2
[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
29
30 #if wxUSE_DYNLIB_CLASS
31
32 #if defined(__WINDOWS__)
33 #include "wx/msw/private.h"
34 #endif
35
36 #include "wx/dynlib.h"
37 #include "wx/filefn.h"
38 #include "wx/intl.h"
39 #include "wx/log.h"
40 #include "wx/tokenzr.h"
41
42 // ----------------------------------------------------------------------------
43 // conditional compilation
44 // ----------------------------------------------------------------------------
45
46 #if defined(__WXPM__) || defined(__EMX__)
47 # define INCL_DOS
48 # include <os2.h>
49 # define wxDllOpen(error, lib, handle) DosLoadModule(error, sizeof(error), lib, &handle)
50 # define wxDllGetSymbol(handle, modaddr) DosQueryProcAddr(handle, 1L, NULL, (PFN*)modaddr)
51 # define wxDllClose(handle) DosFreeModule(handle)
52 #elif defined(HAVE_DLOPEN)
53 // note about dlopen() flags: we use RTLD_NOW to have more Windows-like
54 // behaviour (Win won't let you load a library with missing symbols) and
55 // RTLD_GLOBAL because it is needed sometimes and probably doesn't hurt
56 // otherwise. On True64-Unix RTLD_GLOBAL is not allowed and on VMS the
57 // second argument on dlopen is ignored.
58 #ifdef __VMS
59 # define wxDllOpen(lib) dlopen(lib.fn_str(), 0 )
60 #elif defined( __osf__ )
61 # define wxDllOpen(lib) dlopen(lib.fn_str(), RTLD_LAZY )
62 #else
63 # define wxDllOpen(lib) dlopen(lib.fn_str(), RTLD_LAZY | RTLD_GLOBAL)
64 #endif
65 #define wxDllGetSymbol(handle, name) dlsym(handle, name)
66 # define wxDllClose dlclose
67 #elif defined(HAVE_SHL_LOAD)
68 # define wxDllOpen(lib) shl_load(lib.fn_str(), BIND_DEFERRED, 0)
69 # define wxDllClose shl_unload
70
71 static inline void *wxDllGetSymbol(shl_t handle, const wxString& name)
72 {
73 void *sym;
74 if ( shl_findsym(&handle, name.mb_str(), TYPE_UNDEFINED, &sym) == 0 )
75 return sym;
76 else
77 return (void *)0;
78 }
79 #elif defined(__APPLE__) && defined(__UNIX__)
80 void *dlopen(const char *path, int mode /* mode is ignored */);
81 void *dlsym(void *handle, const char *symbol);
82 int dlclose(void *handle);
83 const char *dlerror(void);
84
85 # define wxDllOpen(lib) dlopen(lib.fn_str(), 0)
86 # define wxDllGetSymbol(handle, name) dlsym(handle, name)
87 # define wxDllClose dlclose
88 #elif defined(__WINDOWS__)
89 // using LoadLibraryEx under Win32 to avoid name clash with LoadLibrary
90 # ifdef __WIN32__
91 #ifdef _UNICODE
92 # define wxDllOpen(lib) ::LoadLibraryExW(lib, 0, 0)
93 #else
94 # define wxDllOpen(lib) ::LoadLibraryExA(lib, 0, 0)
95 #endif
96 # else // Win16
97 # define wxDllOpen(lib) ::LoadLibrary(lib)
98 # endif // Win32/16
99 # define wxDllGetSymbol(handle, name) ::GetProcAddress(handle, name)
100 # define wxDllClose ::FreeLibrary
101 #else
102 # error "Don't know how to load shared libraries on this platform."
103 #endif // OS
104
105 // ---------------------------------------------------------------------------
106 // Global variables
107 // ---------------------------------------------------------------------------
108
109 wxLibraries wxTheLibraries;
110
111 // ============================================================================
112 // implementation
113 // ============================================================================
114
115 // construct the full name from the base shared object name: adds a .dll
116 // suffix under Windows or .so under Unix
117 static wxString ConstructLibraryName(const wxString& basename)
118 {
119 wxString fullname;
120 fullname << basename << wxDllLoader::GetDllExt();
121
122 return fullname;
123 }
124
125 // ---------------------------------------------------------------------------
126 // wxLibrary (one instance per dynamic library)
127 // ---------------------------------------------------------------------------
128
129 wxLibrary::wxLibrary(wxDllType handle)
130 {
131 typedef wxClassInfo *(*t_get_first)(void);
132 t_get_first get_first;
133
134 m_handle = handle;
135
136 // Some system may use a local heap for library.
137 get_first = (t_get_first)GetSymbol("wxGetClassFirst");
138 // It is a wxWindows DLL.
139 if (get_first)
140 PrepareClasses(get_first());
141 }
142
143 wxLibrary::~wxLibrary()
144 {
145 if ( m_handle )
146 {
147 wxDllClose(m_handle);
148 }
149 }
150
151 wxObject *wxLibrary::CreateObject(const wxString& name)
152 {
153 wxClassInfo *info = (wxClassInfo *)classTable.Get(name);
154
155 if (!info)
156 return NULL;
157
158 return info->CreateObject();
159 }
160
161 void wxLibrary::PrepareClasses(wxClassInfo *first)
162 {
163 // Index all class infos by their class name
164 wxClassInfo *info = first;
165 while (info)
166 {
167 if (info->m_className)
168 classTable.Put(info->m_className, (wxObject *)info);
169 info = (wxClassInfo *)info->GetNext();
170 }
171
172 // Set base pointers for each wxClassInfo
173 info = first;
174 while (info)
175 {
176 if (info->GetBaseClassName1())
177 info->m_baseInfo1 = (wxClassInfo *)classTable.Get(info->GetBaseClassName1());
178 if (info->GetBaseClassName2())
179 info->m_baseInfo2 = (wxClassInfo *)classTable.Get(info->GetBaseClassName2());
180 info = info->m_next;
181 }
182 }
183
184 void *wxLibrary::GetSymbol(const wxString& symbname)
185 {
186 return wxDllLoader::GetSymbol(m_handle, symbname);
187 }
188
189 // ---------------------------------------------------------------------------
190 // wxDllLoader
191 // ---------------------------------------------------------------------------
192
193 /* static */
194 wxString wxDllLoader::GetDllExt()
195 {
196 wxString ext;
197
198 #if defined(__WINDOWS__) || defined(__WXPM__) || defined(__EMX__)
199 ext = _T(".dll");
200 #elif defined(__UNIX__)
201 # if defined(__HPUX__)
202 ext = _T(".sl");
203 # else //__HPUX__
204 ext = _T(".so");
205 # endif //__HPUX__
206 #endif
207
208 return ext;
209 }
210
211 /* static */
212 wxDllType
213 wxDllLoader::GetProgramHandle(void)
214 {
215 #if defined( HAVE_DLOPEN ) && !defined(__EMX__)
216 // optain handle for main program
217 return dlopen(NULL, RTLD_NOW/*RTLD_LAZY*/);
218 #elif defined (HAVE_SHL_LOAD)
219 // shl_findsymbol with NULL handle looks up in main program
220 return 0;
221 #else
222 wxFAIL_MSG( wxT("This method is not implemented under Windows or OS/2"));
223 return 0;
224 #endif
225 }
226
227 /* static */
228 wxDllType
229 wxDllLoader::LoadLibrary(const wxString & libname, bool *success)
230 {
231 wxDllType handle;
232
233 #if defined(__WXMAC__) && !defined(__UNIX__)
234 FSSpec myFSSpec ;
235 Ptr myMainAddr ;
236 Str255 myErrName ;
237
238 wxMacPathToFSSpec( libname , &myFSSpec ) ;
239 if (GetDiskFragment( &myFSSpec , 0 , kCFragGoesToEOF , "\p" , kPrivateCFragCopy , &handle , &myMainAddr ,
240 myErrName ) != noErr )
241 {
242 p2cstr( myErrName ) ;
243 wxASSERT_MSG( 1 , (char*)myErrName ) ;
244 return NULL ;
245 }
246 #elif defined(__WXPM__) || defined(__EMX__)
247 char zError[256] = "";
248 wxDllOpen(zError, libname, handle);
249 #else // !Mac
250 handle = wxDllOpen(libname);
251 #endif // OS
252
253 if ( !handle )
254 {
255 wxString msg(_("Failed to load shared library '%s'"));
256
257 #ifdef HAVE_DLERROR
258 const char *errmsg = dlerror();
259 if ( errmsg )
260 {
261 // the error string format is "libname: ...", but we already have
262 // libname, so cut it off
263 const char *p = strchr(errmsg, ':');
264 if ( p )
265 {
266 if ( *++p == ' ' )
267 p++;
268 }
269 else
270 {
271 p = errmsg;
272 }
273
274 msg += _T(" (%s)");
275 wxLogError(msg, libname.c_str(), p);
276 }
277 else
278 #endif // HAVE_DLERROR
279 {
280 wxLogSysError(msg, libname.c_str());
281 }
282 }
283
284 if ( success )
285 {
286 *success = handle != 0;
287 }
288
289 return handle;
290 }
291
292
293 /* static */
294 void
295 wxDllLoader::UnloadLibrary(wxDllType handle)
296 {
297 wxDllClose(handle);
298 }
299
300 /* static */
301 void *
302 wxDllLoader::GetSymbol(wxDllType dllHandle, const wxString &name)
303 {
304 void *symbol = NULL; // return value
305
306 #if defined(__WXMAC__) && !defined(__UNIX__)
307 Ptr symAddress ;
308 CFragSymbolClass symClass ;
309 Str255 symName ;
310
311 #if TARGET_CARBON
312 c2pstrcpy( (StringPtr) symName , name ) ;
313 #else
314 strcpy( (char *) symName , name ) ;
315 c2pstr( (char *) symName ) ;
316 #endif
317
318 if ( FindSymbol( dllHandle , symName , &symAddress , &symClass ) == noErr )
319 symbol = (void *)symAddress ;
320 #elif defined( __WXPM__ ) || defined(__EMX__)
321 wxDllGetSymbol(dllHandle, symbol);
322 #else
323 // mb_str() is necessary in Unicode build
324 symbol = wxDllGetSymbol(dllHandle, name.mb_str());
325 #endif
326
327 if ( !symbol )
328 {
329 wxLogSysError(_("Couldn't find symbol '%s' in a dynamic library"),
330 name.c_str());
331 }
332 return symbol;
333 }
334
335 // ---------------------------------------------------------------------------
336 // wxLibraries (only one instance should normally exist)
337 // ---------------------------------------------------------------------------
338
339 wxLibraries::wxLibraries():m_loaded(wxKEY_STRING)
340 {
341 }
342
343 wxLibraries::~wxLibraries()
344 {
345 wxNode *node = m_loaded.First();
346
347 while (node) {
348 wxLibrary *lib = (wxLibrary *)node->Data();
349 delete lib;
350
351 node = node->Next();
352 }
353 }
354
355 wxLibrary *wxLibraries::LoadLibrary(const wxString& name)
356 {
357 wxNode *node;
358 wxLibrary *lib;
359 wxClassInfo *old_sm_first;
360
361 #if defined(__VISAGECPP__)
362 node = m_loaded.Find(name.GetData());
363 if (node != NULL)
364 return ((wxLibrary *)node->Data());
365 #else // !OS/2
366 if ( (node = m_loaded.Find(name.GetData())) != NULL)
367 return ((wxLibrary *)node->Data());
368 #endif
369 // If DLL shares data, this is necessary.
370 old_sm_first = wxClassInfo::sm_first;
371 wxClassInfo::sm_first = NULL;
372
373 wxString libname = ConstructLibraryName(name);
374
375 /*
376 Unix automatically builds that library name, at least for dlopen()
377 */
378 #if 0
379 #if defined(__UNIX__)
380 // found the first file in LD_LIBRARY_PATH with this name
381 wxString libPath("/lib:/usr/lib"); // system path first
382 const char *envLibPath = getenv("LD_LIBRARY_PATH");
383 if ( envLibPath )
384 libPath << wxT(':') << envLibPath;
385 wxStringTokenizer tokenizer(libPath, wxT(':'));
386 while ( tokenizer.HasMoreToken() )
387 {
388 wxString fullname(tokenizer.NextToken());
389
390 fullname << wxT('/') << libname;
391 if ( wxFileExists(fullname) )
392 {
393 libname = fullname;
394
395 // found the library
396 break;
397 }
398 }
399 //else: not found in the path, leave the name as is (secutiry risk?)
400
401 #endif // __UNIX__
402 #endif
403
404 bool success = FALSE;
405 wxDllType handle = wxDllLoader::LoadLibrary(libname, &success);
406 if(success)
407 {
408 lib = new wxLibrary(handle);
409 wxClassInfo::sm_first = old_sm_first;
410 m_loaded.Append(name.GetData(), lib);
411 }
412 else
413 lib = NULL;
414 return lib;
415 }
416
417 wxObject *wxLibraries::CreateObject(const wxString& path)
418 {
419 wxNode *node = m_loaded.First();
420 wxObject *obj;
421
422 while (node) {
423 obj = ((wxLibrary *)node->Data())->CreateObject(path);
424 if (obj)
425 return obj;
426
427 node = node->Next();
428 }
429 return NULL;
430 }
431
432 #endif // wxUSE_DYNLIB_CLASS