]> git.sesse.net Git - vlc/blob - src/misc/modules_core.h
* DirectX plugin by Gildas Bazin <gbazin@netcourrier.com>.
[vlc] / src / misc / modules_core.h
1 /*****************************************************************************
2  * modules_core.h : Module management functions used by the core application.
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  * $Id: modules_core.h,v 1.1 2001/06/02 01:09:03 sam Exp $
6  *
7  * Authors: Samuel Hocevar <sam@zoy.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  * 
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Inline functions for handling dynamic modules
26  *****************************************************************************/
27
28 /*****************************************************************************
29  * module_load: load a dynamic library
30  *****************************************************************************
31  * This function loads a dynamically linked library using a system dependant
32  * method, and returns a non-zero value on error, zero otherwise.
33  *****************************************************************************/
34 static __inline__ int
35 module_load( char * psz_filename, module_handle_t * handle )
36 {
37 #ifdef SYS_BEOS
38     *handle = load_add_on( psz_filename );
39     return( *handle < 0 );
40
41 #elif defined(WIN32)
42     *handle = LoadLibrary( psz_filename );
43     return( *handle == NULL ); 
44
45 #else
46     /* Do not open modules with RTLD_GLOBAL, or we are going to get namespace
47      * collisions when two modules have common public symbols */
48     *handle = dlopen( psz_filename, RTLD_NOW );
49     return( *handle == NULL );
50
51 #endif
52 }
53
54 /*****************************************************************************
55  * module_unload: unload a dynamic library
56  *****************************************************************************
57  * This function unloads a previously opened dynamically linked library
58  * using a system dependant method. No return value is taken in consideration,
59  * since some libraries sometimes refuse to close properly.
60  *****************************************************************************/
61 static __inline__ void
62 module_unload( module_handle_t handle )
63 {
64 #ifdef SYS_BEOS
65     unload_add_on( handle );
66
67 #elif defined(WIN32)
68     FreeLibrary( handle );
69
70 #else
71     dlclose( handle );
72
73 #endif
74     return;
75 }
76
77 /*****************************************************************************
78  * module_getsymbol: get a symbol from a dynamic library
79  *****************************************************************************
80  * This function queries a loaded library for a symbol specified in a
81  * string, and returns a pointer to it.
82  * FIXME: under Unix we should maybe check for dlerror() instead of the
83  * return value of dlsym, since we could have loaded a symbol really set
84  * to NULL (quite unlikely, though).
85  *****************************************************************************/
86 static __inline__ void *
87 module_getsymbol( module_handle_t handle, char * psz_function )
88 {
89 #ifdef SYS_BEOS
90     void * p_symbol;
91     if( B_OK == get_image_symbol( handle, psz_function,
92                                   B_SYMBOL_TYPE_TEXT, &p_symbol ) )
93     {
94         return( p_symbol );
95     }
96     else
97     {
98         return( NULL );
99     }
100
101 #elif defined(SYS_DARWIN1_3)
102     /* MacOS X dl library expects symbols to begin with "_". That's
103      * really lame, but hey, what can we do ? */
104     char *  psz_call = malloc( strlen( psz_function ) + 2 );
105     void *  p_return;
106     strcpy( psz_call + 1, psz_function );
107     psz_call[ 0 ] = '_';
108
109     p_return = dlsym( handle, psz_call );
110
111     free( psz_call );
112     return( p_return );
113
114 #elif defined(WIN32)
115     return( (void *)GetProcAddress( handle, psz_function ) );
116
117 #else
118     return( dlsym( handle, psz_function ) );
119 #endif
120 }
121
122 /*****************************************************************************
123  * module_error: wrapper for dlerror()
124  *****************************************************************************
125  * This function returns the error message of the last module operation. It
126  * returns the string "failed" on systems which do not have the dlerror()
127  * function.
128  *****************************************************************************/
129 static __inline__ const char *
130 module_error( void )
131 {
132 #if defined(SYS_BEOS) || defined(WIN32)
133     return( "failed" );
134
135 #else
136     return( dlerror() );
137
138 #endif
139 }
140