]> git.saurik.com Git - wxWidgets.git/blob - src/common/dynlib.cpp
Added version of Quantize that manages the palette itself
[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 # define wxDllOpen(lib) dlopen(lib.fn_str(), RTLD_NOW/*RTLD_LAZY*/)
54 # define wxDllGetSymbol(handle, name) dlsym(handle, name)
55 # define wxDllClose dlclose
56 #elif defined(HAVE_SHL_LOAD)
57 # define wxDllOpen(lib) shl_load(lib.fn_str(), BIND_DEFERRED, 0)
58 # define wxDllClose shl_unload
59
60 static inline void *wxDllGetSymbol(shl_t handle, const wxString& name)
61 {
62 void *sym;
63 if ( shl_findsym(&handle, name.mb_str(), TYPE_UNDEFINED, &sym) == 0 )
64 return sym;
65 else
66 return (void *)0;
67 }
68 #elif defined(__WINDOWS__)
69 // using LoadLibraryEx under Win32 to avoid name clash with LoadLibrary
70 # ifdef __WIN32__
71 #ifdef _UNICODE
72 # define wxDllOpen(lib) ::LoadLibraryExW(lib, 0, 0)
73 #else
74 # define wxDllOpen(lib) ::LoadLibraryExA(lib, 0, 0)
75 #endif
76 # else // Win16
77 # define wxDllOpen(lib) ::LoadLibrary(lib)
78 # endif // Win32/16
79 # define wxDllGetSymbol(handle, name) ::GetProcAddress(handle, name)
80 # define wxDllClose ::FreeLibrary
81 #else
82 # error "Don't know how to load shared libraries on this platform."
83 #endif // OS
84
85 // ---------------------------------------------------------------------------
86 // Global variables
87 // ---------------------------------------------------------------------------
88
89 wxLibraries wxTheLibraries;
90
91 // ============================================================================
92 // implementation
93 // ============================================================================
94
95 // construct the full name from the base shared object name: adds a .dll
96 // suffix under Windows or .so under Unix
97 static wxString ConstructLibraryName(const wxString& basename)
98 {
99 wxString fullname;
100 fullname << basename << wxDllLoader::GetDllExt();
101
102 return fullname;
103 }
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 return wxDllLoader::GetSymbol(m_handle, symbname);
168 }
169
170 // ---------------------------------------------------------------------------
171 // wxDllLoader
172 // ---------------------------------------------------------------------------
173
174 /* static */
175 wxString wxDllLoader::GetDllExt()
176 {
177 wxString ext;
178
179 #if defined(__WINDOWS__) || defined(__WXPM__) || defined(__EMX__)
180 ext = _T(".dll");
181 #elif defined(__UNIX__)
182 # if defined(__HPUX__)
183 ext = _T(".sl");
184 # else //__HPUX__
185 ext = _T(".so");
186 # endif //__HPUX__
187 #endif
188
189 return ext;
190 }
191
192 /* static */
193 wxDllType
194 wxDllLoader::GetProgramHandle(void)
195 {
196 #if defined( HAVE_DLOPEN ) && !defined(__EMX__)
197 // optain handle for main program
198 return dlopen(NULL, RTLD_NOW/*RTLD_LAZY*/);
199 #elif defined (HAVE_SHL_LOAD)
200 // shl_findsymbol with NULL handle looks up in main program
201 return 0;
202 #else
203 wxFAIL_MSG( wxT("This method is not implemented under Windows or OS/2"));
204 return 0;
205 #endif
206 }
207
208 /* static */
209 wxDllType
210 wxDllLoader::LoadLibrary(const wxString & libname, bool *success)
211 {
212 wxDllType handle;
213
214 #if defined(__WXMAC__)
215 FSSpec myFSSpec ;
216 Ptr myMainAddr ;
217 Str255 myErrName ;
218
219 wxMacPathToFSSpec( libname , &myFSSpec ) ;
220 if (GetDiskFragment( &myFSSpec , 0 , kCFragGoesToEOF , "\p" , kPrivateCFragCopy , &handle , &myMainAddr ,
221 myErrName ) != noErr )
222 {
223 p2cstr( myErrName ) ;
224 wxASSERT_MSG( 1 , (char*)myErrName ) ;
225 return NULL ;
226 }
227 #elif defined(__WXPM__) || defined(__EMX__)
228 char zError[256] = "";
229 wxDllOpen(zError, libname, handle);
230 #else // !Mac
231 handle = wxDllOpen(libname);
232 #endif // OS
233
234 if ( !handle )
235 {
236 wxString msg(_("Failed to load shared library '%s'"));
237
238 #ifdef HAVE_DLERROR
239 const char *errmsg = dlerror();
240 if ( errmsg )
241 {
242 // the error string format is "libname: ...", but we already have
243 // libname, so cut it off
244 const char *p = strchr(errmsg, ':');
245 if ( p )
246 {
247 if ( *++p == ' ' )
248 p++;
249 }
250 else
251 {
252 p = errmsg;
253 }
254
255 msg += _T(" (%s)");
256 wxLogError(msg, libname.c_str(), p);
257 }
258 else
259 #endif // HAVE_DLERROR
260 {
261 wxLogSysError(msg, libname.c_str());
262 }
263 }
264
265 if ( success )
266 {
267 *success = handle != 0;
268 }
269
270 return handle;
271 }
272
273
274 /* static */
275 void
276 wxDllLoader::UnloadLibrary(wxDllType handle)
277 {
278 wxDllClose(handle);
279 }
280
281 /* static */
282 void *
283 wxDllLoader::GetSymbol(wxDllType dllHandle, const wxString &name)
284 {
285 void *symbol = NULL; // return value
286
287 #if defined( __WXMAC__ )
288 Ptr symAddress ;
289 CFragSymbolClass symClass ;
290 Str255 symName ;
291
292 strcpy( (char*) symName , name ) ;
293 c2pstr( (char*) symName ) ;
294
295 if ( FindSymbol( dllHandle , symName , &symAddress , &symClass ) == noErr )
296 symbol = (void *)symAddress ;
297 #elif defined( __WXPM__ ) || defined(__EMX__)
298 wxDllGetSymbol(dllHandle, symbol);
299 #else
300 // mb_str() is necessary in Unicode build
301 symbol = wxDllGetSymbol(dllHandle, name.mb_str());
302 #endif
303
304 if ( !symbol )
305 {
306 wxLogSysError(_("Couldn't find symbol '%s' in a dynamic library"),
307 name.c_str());
308 }
309 return symbol;
310 }
311
312 // ---------------------------------------------------------------------------
313 // wxLibraries (only one instance should normally exist)
314 // ---------------------------------------------------------------------------
315
316 wxLibraries::wxLibraries():m_loaded(wxKEY_STRING)
317 {
318 }
319
320 wxLibraries::~wxLibraries()
321 {
322 wxNode *node = m_loaded.First();
323
324 while (node) {
325 wxLibrary *lib = (wxLibrary *)node->Data();
326 delete lib;
327
328 node = node->Next();
329 }
330 }
331
332 wxLibrary *wxLibraries::LoadLibrary(const wxString& name)
333 {
334 wxNode *node;
335 wxLibrary *lib;
336 wxClassInfo *old_sm_first;
337
338 #if defined(__VISAGECPP__)
339 node = m_loaded.Find(name.GetData());
340 if (node != NULL)
341 return ((wxLibrary *)node->Data());
342 #else // !OS/2
343 if ( (node = m_loaded.Find(name.GetData())) )
344 return ((wxLibrary *)node->Data());
345 #endif
346 // If DLL shares data, this is necessary.
347 old_sm_first = wxClassInfo::sm_first;
348 wxClassInfo::sm_first = NULL;
349
350 wxString libname = ConstructLibraryName(name);
351
352 /*
353 Unix automatically builds that library name, at least for dlopen()
354 */
355 #if 0
356 #if defined(__UNIX__)
357 // found the first file in LD_LIBRARY_PATH with this name
358 wxString libPath("/lib:/usr/lib"); // system path first
359 const char *envLibPath = getenv("LD_LIBRARY_PATH");
360 if ( envLibPath )
361 libPath << wxT(':') << envLibPath;
362 wxStringTokenizer tokenizer(libPath, wxT(':'));
363 while ( tokenizer.HasMoreToken() )
364 {
365 wxString fullname(tokenizer.NextToken());
366
367 fullname << wxT('/') << libname;
368 if ( wxFileExists(fullname) )
369 {
370 libname = fullname;
371
372 // found the library
373 break;
374 }
375 }
376 //else: not found in the path, leave the name as is (secutiry risk?)
377
378 #endif // __UNIX__
379 #endif
380
381 bool success = FALSE;
382 wxDllType handle = wxDllLoader::LoadLibrary(libname, &success);
383 if(success)
384 {
385 lib = new wxLibrary(handle);
386 wxClassInfo::sm_first = old_sm_first;
387 m_loaded.Append(name.GetData(), lib);
388 }
389 else
390 lib = NULL;
391 return lib;
392 }
393
394 wxObject *wxLibraries::CreateObject(const wxString& path)
395 {
396 wxNode *node = m_loaded.First();
397 wxObject *obj;
398
399 while (node) {
400 obj = ((wxLibrary *)node->Data())->CreateObject(path);
401 if (obj)
402 return obj;
403
404 node = node->Next();
405 }
406 return NULL;
407 }
408
409 #endif // wxUSE_DYNLIB_CLASS