mlt_repository.c: fix to previous string const fix in mlt_repository_languages
[melted] / src / framework / mlt_repository.c
1 /*
2 * repository.c -- provides a map between service and shared objects
3 * Copyright (C) 2003-2004 Ushodaya Enterprises Limited
4 * Author: Charles Yates <charles.yates@pandora.be>
5 * Contributor: Dan Dennedy <dan@dennedy.org>
6 *
7 * This library is free software; you can redistribute it and/or
8 * modify it under the terms of the GNU Lesser General Public
9 * License as published by the Free Software Foundation; either
10 * version 2.1 of the License, or (at your option) any later version.
11 *
12 * This library is distributed in the hope that it will be useful,
13 * but WITHOUT ANY WARRANTY; without even the implied warranty of
14 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
15 * Lesser General Public License for more details.
16 *
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, write to the Free Software
19 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
20 */
21
22 #include "mlt_repository.h"
23 #include "mlt_properties.h"
24 #include "mlt_tokeniser.h"
25
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <dlfcn.h>
29 #include <string.h>
30
31 struct mlt_repository_s
32 {
33 struct mlt_properties_s parent; // a list of object files
34 mlt_properties consumers; // lists of entry points
35 mlt_properties filters;
36 mlt_properties producers;
37 mlt_properties transitions;
38 };
39
40 /** Construct a new repository
41 */
42
43 mlt_repository mlt_repository_init( const char *directory )
44 {
45 // Safety check
46 if ( directory == NULL || strcmp( directory, "" ) == 0 )
47 return NULL;
48
49 // Construct the repository
50 mlt_repository this = calloc( sizeof( struct mlt_repository_s ), 1 );
51 mlt_properties_init( &this->parent, this );
52 this->consumers = mlt_properties_new();
53 this->filters = mlt_properties_new();
54 this->producers = mlt_properties_new();
55 this->transitions = mlt_properties_new();
56
57 // Get the directory list
58 mlt_properties dir = mlt_properties_new();
59 int count = mlt_properties_dir_list( dir, directory, NULL, 0 );
60 int i;
61
62 // Iterate over files
63 for ( i = 0; i < count; i++ )
64 {
65 int flags = RTLD_NOW;
66 const char *object_name = mlt_properties_get_value( dir, i);
67
68 // Very temporary hack to allow the quicktime plugins to work
69 // TODO: extend repository to allow this to be used on a case by case basis
70 if ( strstr( object_name, "libmltkino" ) )
71 flags |= RTLD_GLOBAL;
72
73 // Open the shared object
74 void *object = dlopen( object_name, flags );
75 if ( object != NULL )
76 {
77 // Get the registration function
78 mlt_repository_callback symbol_ptr = dlsym( object, "mlt_register" );
79
80 // Call the registration function
81 if ( symbol_ptr != NULL )
82 {
83 symbol_ptr( this );
84
85 // Register the object file for closure
86 mlt_properties_set_data( &this->parent, object_name, object, 0, ( mlt_destructor )dlclose, NULL );
87 }
88 else
89 {
90 dlclose( object );
91 }
92 }
93 else if ( strstr( object_name, "libmlt" ) )
94 {
95 fprintf( stderr, "%s, %s: failed to dlopen %s\n", __FILE__, __FUNCTION__, object_name );
96 }
97 }
98
99 return this;
100 }
101
102 static mlt_properties new_service( void *symbol )
103 {
104 mlt_properties properties = mlt_properties_new();
105 mlt_properties_set_data( properties, "symbol", symbol, 0, NULL, NULL );
106 return properties;
107 }
108
109 /** Register a service with the repository
110 Typically, this is invoked by a module within its mlt_register().
111 */
112
113 void mlt_repository_register( mlt_repository this, mlt_service_type service_type, const char *service, mlt_register_callback symbol )
114 {
115 // Add the entry point to the corresponding service list
116 switch ( service_type )
117 {
118 case consumer_type:
119 mlt_properties_set_data( this->consumers, service, new_service( symbol ), 0, ( mlt_destructor )mlt_properties_close, NULL );
120 break;
121 case filter_type:
122 mlt_properties_set_data( this->filters, service, new_service( symbol ), 0, ( mlt_destructor )mlt_properties_close, NULL );
123 break;
124 case producer_type:
125 mlt_properties_set_data( this->producers, service, new_service( symbol ), 0, ( mlt_destructor )mlt_properties_close, NULL );
126 break;
127 case transition_type:
128 mlt_properties_set_data( this->transitions, service, new_service( symbol ), 0, ( mlt_destructor )mlt_properties_close, NULL );
129 break;
130 default:
131 break;
132 }
133 }
134
135 static mlt_properties get_service_properties( mlt_repository this, mlt_service_type type, const char *service )
136 {
137 mlt_properties service_properties = NULL;
138
139 // Get the entry point from the corresponding service list
140 switch ( type )
141 {
142 case consumer_type:
143 service_properties = mlt_properties_get_data( this->consumers, service, NULL );
144 break;
145 case filter_type:
146 service_properties = mlt_properties_get_data( this->filters, service, NULL );
147 break;
148 case producer_type:
149 service_properties = mlt_properties_get_data( this->producers, service, NULL );
150 break;
151 case transition_type:
152 service_properties = mlt_properties_get_data( this->transitions, service, NULL );
153 break;
154 default:
155 break;
156 }
157 return service_properties;
158 }
159
160 /** Construct a new instance of a service
161 */
162
163 void *mlt_repository_create( mlt_repository this, mlt_profile profile, mlt_service_type type, const char *service, void *input )
164 {
165 mlt_properties properties = get_service_properties( this, type, service );
166 if ( properties != NULL )
167 {
168 mlt_register_callback symbol_ptr = mlt_properties_get_data( properties, "symbol", NULL );
169
170 // Construct the service
171 return ( symbol_ptr != NULL ) ? symbol_ptr( profile, type, service, input ) : NULL;
172 }
173 return NULL;
174 }
175
176 /** Destroy a repository
177 */
178
179 void mlt_repository_close( mlt_repository this )
180 {
181 mlt_properties_close( this->consumers );
182 mlt_properties_close( this->filters );
183 mlt_properties_close( this->producers );
184 mlt_properties_close( this->transitions );
185 mlt_properties_close( &this->parent );
186 free( this );
187 }
188
189 /** Get the list of registered consumers
190 */
191
192 mlt_properties mlt_repository_consumers( mlt_repository self )
193 {
194 return self->consumers;
195 }
196
197 /** Get the list of registered filters
198 */
199
200 mlt_properties mlt_repository_filters( mlt_repository self )
201 {
202 return self->filters;
203 }
204
205 /** Get the list of registered producers
206 */
207
208 mlt_properties mlt_repository_producers( mlt_repository self )
209 {
210 return self->producers;
211 }
212
213 /** Get the list of registered transitions
214 */
215
216 mlt_properties mlt_repository_transitions( mlt_repository self )
217 {
218 return self->transitions;
219 }
220
221 /** Register the metadata for a service
222 IMPORTANT: mlt_repository will take responsibility for deallocating the metadata properties that you supply!
223 */
224 void mlt_repository_register_metadata( mlt_repository self, mlt_service_type type, const char *service, mlt_metadata_callback callback, void *callback_data )
225 {
226 mlt_properties service_properties = get_service_properties( self, type, service );
227 mlt_properties_set_data( service_properties, "metadata_cb", callback, 0, NULL, NULL );
228 mlt_properties_set_data( service_properties, "metadata_cb_data", callback_data, 0, NULL, NULL );
229 }
230
231 /** Get the metadata about a service
232 Returns NULL if service or its metadata are unavailable.
233 */
234
235 mlt_properties mlt_repository_metadata( mlt_repository self, mlt_service_type type, const char *service )
236 {
237 mlt_properties metadata = NULL;
238 mlt_properties properties = get_service_properties( self, type, service );
239
240 // If this is a valid service
241 if ( properties )
242 {
243 // Lookup cached metadata
244 metadata = mlt_properties_get_data( properties, "metadata", NULL );
245 if ( ! metadata )
246 {
247 // Not cached, so get the registered metadata callback function
248 mlt_metadata_callback callback = mlt_properties_get_data( properties, "metadata_cb", NULL );
249
250 // If a metadata callback function is registered
251 if ( callback )
252 {
253 // Fetch the callback data arg
254 void *data = mlt_properties_get_data( properties, "metadata_cb_data", NULL );
255
256 // Fetch the metadata through the callback
257 metadata = callback( type, service, data );
258
259 // Cache the metadata
260 if ( metadata )
261 // Include dellocation and serialisation
262 mlt_properties_set_data( properties, "metadata", metadata, 0, ( mlt_destructor )mlt_properties_close, ( mlt_serialiser )mlt_properties_serialise_yaml );
263 }
264 }
265 }
266 return metadata;
267 }
268
269 static const char *getenv_locale()
270 {
271 char *s = getenv( "LANGUAGE" );
272 if ( s && s[0] )
273 return s;
274 s = getenv( "LC_ALL" );
275 if ( s && s[0] )
276 return s;
277 s = getenv( "LC_MESSAGES" );
278 if ( s && s[0] )
279 return s;
280 s = getenv( "LANG" );
281 if ( s && s[0] )
282 return s;
283 return NULL;
284 }
285
286 /** Return a list of user-preferred language codes taken from environment variables.
287 */
288
289 mlt_properties mlt_repository_languages( mlt_repository self )
290 {
291 mlt_properties languages = mlt_properties_get_data( &self->parent, "languages", NULL );
292 if ( languages )
293 return languages;
294
295 languages = mlt_properties_new();
296 char *locale = getenv_locale();
297 if ( locale )
298 {
299 locale = strdup( locale );
300 mlt_tokeniser tokeniser = mlt_tokeniser_init();
301 int count = mlt_tokeniser_parse_new( tokeniser, locale, ":" );
302 if ( count )
303 {
304 int i;
305 for ( i = 0; i < count; i++ )
306 {
307 char *locale = mlt_tokeniser_get_string( tokeniser, i );
308 if ( strcmp( locale, "C" ) == 0 || strcmp( locale, "POSIX" ) == 0 )
309 locale = "en";
310 else if ( strlen( locale ) > 2 )
311 locale[2] = 0;
312 char string[21];
313 snprintf( string, sizeof(string), "%d", i );
314 mlt_properties_set( languages, string, locale );
315 }
316 }
317 else
318 {
319 mlt_properties_set( languages, "0", "en" );
320 }
321 free( locale );
322 mlt_tokeniser_close( tokeniser );
323 }
324 else
325 {
326 mlt_properties_set( languages, "0", "en" );
327 }
328 mlt_properties_set_data( &self->parent, "languages", languages, 0, ( mlt_destructor )mlt_properties_close, NULL );
329 return languages;
330 }