]> git.saurik.com Git - wxWidgets.git/blob - src/common/dynload.cpp
Undo/Redo update handlers now set the text as well
[wxWidgets.git] / src / common / dynload.cpp
1 /////////////////////////////////////////////////////////////////////////////
2 // Name: dynload.cpp
3 // Purpose: Dynamic loading framework
4 // Author: Ron Lee, David Falkinder, Vadim Zeitlin and a cast of 1000's
5 // (derived in part from dynlib.cpp (c) 1998 Guilhem Lavaux)
6 // Modified by:
7 // Created: 03/12/01
8 // RCS-ID: $Id$
9 // Copyright: (c) 2001 Ron Lee <ron@debian.org>
10 // Licence: wxWindows license
11 /////////////////////////////////////////////////////////////////////////////
12
13 #ifdef __GNUG__
14 #pragma implementation "dynload.h"
15 #endif
16
17 // ----------------------------------------------------------------------------
18 // headers
19 // ----------------------------------------------------------------------------
20
21 #include "wx/wxprec.h"
22
23 #ifdef __BORLANDC__
24 #pragma hdrstop
25 #endif
26
27 #if wxUSE_DYNAMIC_LOADER
28
29 #ifdef __WINDOWS__
30 #include "wx/msw/private.h"
31 #endif
32
33 #ifndef WX_PRECOMP
34 #include "wx/log.h"
35 #include "wx/intl.h"
36 #endif
37
38 #include "wx/filename.h" // for SplitPath()
39
40 #include "wx/dynload.h"
41 #include "wx/module.h"
42
43 #if defined(__DARWIN__)
44 /* Porting notes:
45 * The dlopen port is a port from dl_next.xs by Anno Siegel.
46 * dl_next.xs is itself a port from dl_dlopen.xs by Paul Marquess.
47 * The method used here is just to supply the sun style dlopen etc.
48 * functions in terms of Darwin NS*.
49 */
50 void *dlopen(const char *path, int mode /* mode is ignored */);
51 void *dlsym(void *handle, const char *symbol);
52 int dlclose(void *handle);
53 const char *dlerror(void);
54 #endif
55
56 // ============================================================================
57 // implementation
58 // ============================================================================
59
60 // ---------------------------------------------------------------------------
61 // wxDynamicLibrary
62 // ---------------------------------------------------------------------------
63
64 //FIXME: This class isn't really common at all, it should be moved into
65 // platform dependent files.
66
67 #if defined(__WINDOWS__) || defined(__WXPM__) || defined(__EMX__)
68 const wxString wxDynamicLibrary::ms_dllext( _T(".dll") );
69 #elif defined(__UNIX__)
70 #if defined(__HPUX__)
71 const wxString wxDynamicLibrary::ms_dllext( _T(".sl") );
72 #else
73 const wxString wxDynamicLibrary::ms_dllext( _T(".so") );
74 #endif
75 #endif
76
77 wxDllType wxDynamicLibrary::GetProgramHandle()
78 {
79 #if defined( HAVE_DLOPEN ) && !defined(__EMX__)
80 return dlopen(0, RTLD_LAZY);
81 #elif defined (HAVE_SHL_LOAD)
82 return PROG_HANDLE;
83 #else
84 wxFAIL_MSG( wxT("This method is not implemented under Windows or OS/2"));
85 return 0;
86 #endif
87 }
88
89 bool wxDynamicLibrary::Load(wxString libname, int flags)
90 {
91 wxASSERT_MSG(m_handle == 0, _T("Library already loaded."));
92
93 // add the proper extension for the DLL ourselves unless told not to
94 if ( !(flags & wxDL_VERBATIM) )
95 {
96 // and also check that the libname doesn't already have it
97 wxString ext;
98 wxFileName::SplitPath(libname, NULL, NULL, &ext);
99 if ( ext.empty() )
100 {
101 libname += GetDllExt();
102 }
103 }
104
105 #if defined(__WXMAC__) && !defined(__DARWIN__)
106 FSSpec myFSSpec;
107 Ptr myMainAddr;
108 Str255 myErrName;
109
110 wxMacFilename2FSSpec( libname , &myFSSpec );
111
112 if( GetDiskFragment( &myFSSpec,
113 0,
114 kCFragGoesToEOF,
115 "\p",
116 kPrivateCFragCopy,
117 &m_handle,
118 &myMainAddr,
119 myErrName ) != noErr )
120 {
121 p2cstr( myErrName );
122 wxLogSysError( _("Failed to load shared library '%s' Error '%s'"),
123 libname.c_str(),
124 (char*)myErrName );
125 m_handle = 0;
126 }
127
128 #elif defined(__WXPM__) || defined(__EMX__)
129 char err[256] = "";
130 DosLoadModule(err, sizeof(err), libname.c_str(), &m_handle);
131
132 #elif defined(HAVE_DLOPEN) || defined(__DARWIN__)
133
134 #if defined(__VMS) || defined(__DARWIN__)
135 m_handle = dlopen(libname.c_str(), 0); // The second parameter is ignored
136 #else
137 int rtldFlags = 0;
138
139 if( flags & wxDL_LAZY )
140 {
141 wxASSERT_MSG( (flags & wxDL_NOW) == 0,
142 _T("wxDL_LAZY and wxDL_NOW are mutually exclusive.") );
143 rtldFlags |= RTLD_LAZY;
144 }
145 else if( flags & wxDL_NOW )
146 {
147 rtldFlags |= RTLD_NOW;
148 }
149 if( flags & wxDL_GLOBAL )
150 {
151 #ifdef __osf__
152 wxLogDebug(_T("WARNING: RTLD_GLOBAL is not a supported on this platform."));
153 #endif
154 rtldFlags |= RTLD_GLOBAL;
155 }
156
157 m_handle = dlopen(libname.c_str(), rtldFlags);
158 #endif // __VMS || __DARWIN__
159
160 #elif defined(HAVE_SHL_LOAD)
161 int shlFlags = 0;
162
163 if( flags & wxDL_LAZY )
164 {
165 wxASSERT_MSG( (flags & wxDL_NOW) == 0,
166 _T("wxDL_LAZY and wxDL_NOW are mutually exclusive.") );
167 shlFlags |= BIND_DEFERRED;
168 }
169 else if( flags & wxDL_NOW )
170 {
171 shlFlags |= BIND_IMMEDIATE;
172 }
173 m_handle = shl_load(libname.c_str(), BIND_DEFERRED, 0);
174
175 #elif defined(__WINDOWS__)
176 m_handle = ::LoadLibrary(libname.c_str());
177
178 #else
179 #error "runtime shared lib support not implemented"
180 #endif
181
182 if ( m_handle == 0 )
183 {
184 wxString msg(_("Failed to load shared library '%s'"));
185 #if defined(HAVE_DLERROR) && !defined(__EMX__)
186 const wxChar *err = dlerror();
187 if( err )
188 wxLogError( msg, err );
189 #else
190 wxLogSysError( msg, libname.c_str() );
191 #endif
192 }
193
194 return IsLoaded();
195 }
196
197 void wxDynamicLibrary::Unload()
198 {
199 if( IsLoaded() )
200 {
201 #if defined(__WXPM__) || defined(__EMX__)
202 DosFreeModule( m_handle );
203 #elif defined(HAVE_DLOPEN) || defined(__DARWIN__)
204 dlclose( m_handle );
205 #elif defined(HAVE_SHL_LOAD)
206 shl_unload( m_handle );
207 #elif defined(__WINDOWS__)
208 ::FreeLibrary( m_handle );
209 #elif defined(__WXMAC__) && !defined(__DARWIN__)
210 CloseConnection( (CFragConnectionID*) &m_handle );
211 #else
212 #error "runtime shared lib support not implemented"
213 #endif
214 m_handle = 0;
215 }
216 }
217
218 void *wxDynamicLibrary::GetSymbol(const wxString &name, bool *success) const
219 {
220 wxCHECK_MSG( IsLoaded(), NULL,
221 _T("Can't load symbol from unloaded library") );
222
223 bool failed = FALSE;
224 void *symbol = 0;
225
226 #if defined(__WXMAC__) && !defined(__DARWIN__)
227 Ptr symAddress;
228 CFragSymbolClass symClass;
229 Str255 symName;
230 #if TARGET_CARBON
231 c2pstrcpy( (StringPtr) symName, name );
232 #else
233 strcpy( (char *)symName, name );
234 c2pstr( (char *)symName );
235 #endif
236 if( FindSymbol( dllHandle, symName, &symAddress, &symClass ) == noErr )
237 symbol = (void *)symAddress;
238
239 #elif defined(__WXPM__) || defined(__EMX__)
240 DosQueryProcAddr( m_handle, 1L, name.c_str(), (PFN*)symbol );
241
242 #elif defined(HAVE_DLOPEN) || defined(__DARWIN__)
243 symbol = dlsym( m_handle, name.c_str() );
244
245 #elif defined(HAVE_SHL_LOAD)
246 if( shl_findsym( &m_handle, name.c_str(), TYPE_UNDEFINED, &symbol ) != 0 )
247 symbol = 0;
248
249 #elif defined(__WINDOWS__)
250 symbol = (void*) ::GetProcAddress( m_handle, name.mb_str() );
251
252 #else
253 #error "runtime shared lib support not implemented"
254 #endif
255
256 if ( !symbol )
257 {
258 wxString msg(_("wxDynamicLibrary failed to GetSymbol '%s'"));
259 #if defined(HAVE_DLERROR) && !defined(__EMX__)
260 const wxChar *err = dlerror();
261 if( err )
262 {
263 failed = TRUE;
264 wxLogError( msg, err );
265 }
266 #else
267 failed = TRUE;
268 wxLogSysError(_("Couldn't find symbol '%s' in a dynamic library"),
269 name.c_str());
270 #endif
271 }
272 if( success )
273 *success = !failed;
274
275 return symbol;
276 }
277
278
279 // ---------------------------------------------------------------------------
280 // wxPluginLibrary
281 // ---------------------------------------------------------------------------
282
283
284 wxDLImports* wxPluginLibrary::ms_classes = NULL;
285
286 class wxPluginLibraryModule : public wxModule
287 {
288 public:
289 wxPluginLibraryModule() {}
290 bool OnInit() { wxPluginLibrary::ms_classes = new wxDLImports(wxKEY_STRING); return TRUE; }
291 void OnExit() { delete wxPluginLibrary::ms_classes; wxPluginLibrary::ms_classes = NULL;
292 wxPluginManager::ClearManifest(); }
293
294 private:
295 DECLARE_DYNAMIC_CLASS(wxPluginLibraryModule )
296 };
297
298 IMPLEMENT_DYNAMIC_CLASS(wxPluginLibraryModule, wxModule)
299
300
301 wxPluginLibrary::wxPluginLibrary(const wxString &libname, int flags)
302 : m_linkcount(1)
303 , m_objcount(0)
304 {
305 m_before = wxClassInfo::sm_first;
306 Load( libname, flags );
307 m_after = wxClassInfo::sm_first;
308
309 if( m_handle != 0 )
310 {
311 UpdateClassInfo();
312 RegisterModules();
313 }
314 else
315 --m_linkcount; // Flag us for deletion
316 }
317
318 wxPluginLibrary::~wxPluginLibrary()
319 {
320 if( m_handle != 0 )
321 {
322 UnregisterModules();
323 RestoreClassInfo();
324 }
325 }
326
327 bool wxPluginLibrary::UnrefLib()
328 {
329 wxASSERT_MSG( m_objcount == 0, _T("Library unloaded before all objects were destroyed") );
330 if( m_linkcount == 0 || --m_linkcount == 0 )
331 {
332 delete this;
333 return TRUE;
334 }
335 return FALSE;
336 }
337
338 // ------------------------
339 // Private methods
340 // ------------------------
341
342 void wxPluginLibrary::UpdateClassInfo()
343 {
344 wxClassInfo *info;
345 wxHashTable *t = wxClassInfo::sm_classTable;
346
347 // FIXME: Below is simply a cut and paste specialisation of
348 // wxClassInfo::InitializeClasses. Once this stabilises,
349 // the two should probably be merged.
350 //
351 // Actually it's becoming questionable whether we should merge
352 // this info with the main ClassInfo tables since we can nearly
353 // handle this completely internally now and it does expose
354 // certain (minimal % user_stupidy) risks.
355
356 for(info = m_after; info != m_before; info = info->m_next)
357 {
358 if( info->m_className )
359 {
360 if( t->Get(info->m_className) == 0 )
361 t->Put(info->m_className, (wxObject *)info);
362
363 // Hash all the class names into a local table too so
364 // we can quickly find the entry they correspond to.
365
366 if( ms_classes->Get(info->m_className) == 0 )
367 ms_classes->Put(info->m_className, (wxObject *) this);
368 }
369 }
370
371 for(info = m_after; info != m_before; info = info->m_next)
372 {
373 if( info->m_baseClassName1 )
374 info->m_baseInfo1 = (wxClassInfo *)t->Get(info->m_baseClassName1);
375 if( info->m_baseClassName2 )
376 info->m_baseInfo2 = (wxClassInfo *)t->Get(info->m_baseClassName2);
377 }
378 }
379
380 void wxPluginLibrary::RestoreClassInfo()
381 {
382 wxClassInfo *info;
383
384 for(info = m_after; info != m_before; info = info->m_next)
385 {
386 wxClassInfo::sm_classTable->Delete(info->m_className);
387 ms_classes->Delete(info->m_className);
388 }
389
390 if( wxClassInfo::sm_first == m_after )
391 wxClassInfo::sm_first = m_before;
392 else
393 {
394 info = wxClassInfo::sm_first;
395 while( info->m_next && info->m_next != m_after ) info = info->m_next;
396
397 wxASSERT_MSG( info, _T("ClassInfo from wxPluginLibrary not found on purge"))
398
399 info->m_next = m_before;
400 }
401 }
402
403 void wxPluginLibrary::RegisterModules()
404 {
405 // Plugin libraries might have wxModules, Register and initialise them if
406 // they do.
407 //
408 // Note that these classes are NOT included in the reference counting since
409 // it's implicit that they will be unloaded if and when the last handle to
410 // the library is. We do have to keep a copy of the module's pointer
411 // though, as there is currently no way to Unregister it without it.
412
413 wxASSERT_MSG( m_linkcount == 1,
414 _T("RegisterModules should only be called for the first load") );
415
416 for(wxClassInfo *info = m_after; info != m_before; info = info->m_next)
417 {
418 if( info->IsKindOf(CLASSINFO(wxModule)) )
419 {
420 wxModule *m = wxDynamicCast(info->CreateObject(), wxModule);
421
422 wxASSERT_MSG( m, _T("wxDynamicCast of wxModule failed") );
423
424 m_wxmodules.Append(m);
425 wxModule::RegisterModule(m);
426 }
427 }
428
429 // FIXME: Likewise this is (well was) very similar to InitializeModules()
430
431 for(wxModuleList::Node *node = m_wxmodules.GetFirst(); node; node->GetNext())
432 {
433 if( !node->GetData()->Init() )
434 {
435 wxLogDebug(_T("wxModule::Init() failed for wxPluginLibrary"));
436
437 // XXX: Watch this, a different hash implementation might break it,
438 // a good hash implementation would let us fix it though.
439
440 // The name of the game is to remove any uninitialised modules and
441 // let the dtor Exit the rest on shutdown, (which we'll initiate
442 // shortly).
443
444 wxModuleList::Node *oldNode = 0;
445 do {
446 node = node->GetNext();
447 delete oldNode;
448 wxModule::UnregisterModule( node->GetData() );
449 oldNode = node;
450 } while( node );
451
452 --m_linkcount; // Flag us for deletion
453 break;
454 }
455 }
456 }
457
458 void wxPluginLibrary::UnregisterModules()
459 {
460 wxModuleList::Node *node;
461
462 for(node = m_wxmodules.GetFirst(); node; node->GetNext())
463 node->GetData()->Exit();
464
465 for(node = m_wxmodules.GetFirst(); node; node->GetNext())
466 wxModule::UnregisterModule( node->GetData() );
467
468 m_wxmodules.DeleteContents(TRUE);
469 }
470
471
472 // ---------------------------------------------------------------------------
473 // wxPluginLibrary
474 // ---------------------------------------------------------------------------
475
476 wxDLManifest* wxPluginManager::ms_manifest = NULL;
477
478 // ------------------------
479 // Static accessors
480 // ------------------------
481
482 wxPluginLibrary *wxPluginManager::LoadLibrary(const wxString &libname, int flags)
483 {
484 wxString realname(libname);
485
486 if( !(flags & wxDL_VERBATIM) )
487 realname += wxDynamicLibrary::GetDllExt();
488
489 wxPluginLibrary *entry = (wxPluginLibrary*) ms_manifest->Get(realname);
490
491 if( entry != 0 )
492 {
493 entry->RefLib();
494 }
495 else
496 {
497 entry = new wxPluginLibrary( libname, flags );
498
499 if( entry->IsLoaded() )
500 {
501 ms_manifest->Put(realname, (wxObject*) entry);
502 }
503 else
504 {
505 wxCHECK_MSG( entry->UnrefLib(), 0,
506 _T("Currently linked library is, ..not loaded??") );
507 entry = 0;
508 }
509 }
510 return entry;
511 }
512
513 bool wxPluginManager::UnloadLibrary(const wxString &libname)
514 {
515 wxPluginLibrary *entry = (wxPluginLibrary*) ms_manifest->Get(libname);
516
517 if( !entry )
518 entry = (wxPluginLibrary*) ms_manifest->Get(libname + wxDynamicLibrary::GetDllExt());
519
520 if( entry )
521 return entry->UnrefLib();
522
523 wxLogDebug(_T("Attempt to Unlink library '%s' (which is not linked)."), libname.c_str());
524 return FALSE;
525 }
526
527 #if WXWIN_COMPATIBILITY_2_2
528 wxPluginLibrary *wxPluginManager::GetObjectFromHandle(wxDllType handle)
529 {
530 wxNode *node;
531 ms_manifest->BeginFind();
532
533 for(node = ms_manifest->Next(); node; node = ms_manifest->Next())
534 if( ((wxPluginLibrary*)node->GetData())->GetLibHandle() == handle )
535 return (wxPluginLibrary*)node->GetData();
536
537 return 0;
538 }
539 #endif
540
541 // ------------------------
542 // Class implementation
543 // ------------------------
544
545 bool wxPluginManager::Load(const wxString &libname, int flags)
546 {
547 m_entry = wxPluginManager::LoadLibrary(libname, flags);
548 return IsLoaded();
549 }
550
551 void wxPluginManager::Unload()
552 {
553 wxNode *node;
554 ms_manifest->BeginFind();
555
556 // It's either this or store the name of the lib just to do this.
557
558 for(node = ms_manifest->Next(); node; node = ms_manifest->Next())
559 if( (wxPluginLibrary*)node->GetData() == m_entry )
560 break;
561
562 if( m_entry && m_entry->UnrefLib() )
563 {
564 delete node;
565 m_entry = 0;
566 }
567 }
568
569 // ---------------------------------------------------------------------------
570 // wxDllLoader (all these methods are static)
571 // ---------------------------------------------------------------------------
572
573 #if WXWIN_COMPATIBILITY_2_2
574
575 wxDllType wxDllLoader::LoadLibrary(const wxString &name)
576 {
577 wxPluginLibrary *p = wxPluginManager::LoadLibrary(name, wxDL_DEFAULT | wxDL_VERBATIM);
578 return p->GetLibHandle();
579 }
580
581 void wxDllLoader::UnloadLibrary(wxDllType handle)
582 {
583 wxPluginLibrary *p = wxPluginManager::GetObjectFromHandle(handle);
584 p->UnrefLib();
585 }
586
587 void *wxDllLoader::GetSymbol(wxDllType dllHandle, const wxString &name, bool *success)
588 {
589 wxPluginLibrary *p = wxPluginManager::GetObjectFromHandle(dllHandle);
590 return p->GetSymbol(name, success);
591 }
592
593 #endif // WXWIN_COMPATIBILITY_2_2
594
595 #endif // wxUSE_DYNAMIC_LOADER
596
597 // vi:sts=4:sw=4:et