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>
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.
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.
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
22 #include "mlt_repository.h"
23 #include "mlt_properties.h"
24 #include "mlt_tokeniser.h"
31 struct mlt_repository_s
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
;
40 /** Construct a new repository
43 mlt_repository
mlt_repository_init( const char *directory
)
46 if ( directory
== NULL
|| strcmp( directory
, "" ) == 0 )
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();
57 // Get the directory list
58 mlt_properties dir
= mlt_properties_new();
59 int count
= mlt_properties_dir_list( dir
, directory
, NULL
, 0 );
63 for ( i
= 0; i
< count
; i
++ )
66 const char *object_name
= mlt_properties_get_value( dir
, i
);
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" ) )
73 // Open the shared object
74 void *object
= dlopen( object_name
, flags
);
77 // Get the registration function
78 mlt_repository_callback symbol_ptr
= dlsym( object
, "mlt_register" );
80 // Call the registration function
81 if ( symbol_ptr
!= NULL
)
85 // Register the object file for closure
86 mlt_properties_set_data( &this->parent
, object_name
, object
, 0, ( mlt_destructor
)dlclose
, NULL
);
93 else if ( strstr( object_name
, "libmlt" ) )
95 fprintf( stderr
, "%s, %s: failed to dlopen %s\n", __FILE__
, __FUNCTION__
, object_name
);
102 static mlt_properties
new_service( void *symbol
)
104 mlt_properties properties
= mlt_properties_new();
105 mlt_properties_set_data( properties
, "symbol", symbol
, 0, NULL
, NULL
);
109 /** Register a service with the repository
110 Typically, this is invoked by a module within its mlt_register().
113 void mlt_repository_register( mlt_repository
this, mlt_service_type service_type
, const char *service
, mlt_register_callback symbol
)
115 // Add the entry point to the corresponding service list
116 switch ( service_type
)
119 mlt_properties_set_data( this->consumers
, service
, new_service( symbol
), 0, ( mlt_destructor
)mlt_properties_close
, NULL
);
122 mlt_properties_set_data( this->filters
, service
, new_service( symbol
), 0, ( mlt_destructor
)mlt_properties_close
, NULL
);
125 mlt_properties_set_data( this->producers
, service
, new_service( symbol
), 0, ( mlt_destructor
)mlt_properties_close
, NULL
);
127 case transition_type
:
128 mlt_properties_set_data( this->transitions
, service
, new_service( symbol
), 0, ( mlt_destructor
)mlt_properties_close
, NULL
);
135 static mlt_properties
get_service_properties( mlt_repository
this, mlt_service_type type
, const char *service
)
137 mlt_properties service_properties
= NULL
;
139 // Get the entry point from the corresponding service list
143 service_properties
= mlt_properties_get_data( this->consumers
, service
, NULL
);
146 service_properties
= mlt_properties_get_data( this->filters
, service
, NULL
);
149 service_properties
= mlt_properties_get_data( this->producers
, service
, NULL
);
151 case transition_type
:
152 service_properties
= mlt_properties_get_data( this->transitions
, service
, NULL
);
157 return service_properties
;
160 /** Construct a new instance of a service
163 void *mlt_repository_create( mlt_repository
this, mlt_profile profile
, mlt_service_type type
, const char *service
, void *input
)
165 mlt_properties properties
= get_service_properties( this, type
, service
);
166 if ( properties
!= NULL
)
168 mlt_register_callback symbol_ptr
= mlt_properties_get_data( properties
, "symbol", NULL
);
170 // Construct the service
171 return ( symbol_ptr
!= NULL
) ?
symbol_ptr( profile
, type
, service
, input
) : NULL
;
176 /** Destroy a repository
179 void mlt_repository_close( mlt_repository
this )
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
);
189 /** Get the list of registered consumers
192 mlt_properties
mlt_repository_consumers( mlt_repository self
)
194 return self
->consumers
;
197 /** Get the list of registered filters
200 mlt_properties
mlt_repository_filters( mlt_repository self
)
202 return self
->filters
;
205 /** Get the list of registered producers
208 mlt_properties
mlt_repository_producers( mlt_repository self
)
210 return self
->producers
;
213 /** Get the list of registered transitions
216 mlt_properties
mlt_repository_transitions( mlt_repository self
)
218 return self
->transitions
;
221 /** Register the metadata for a service
222 IMPORTANT: mlt_repository will take responsibility for deallocating the metadata properties that you supply!
224 void mlt_repository_register_metadata( mlt_repository self
, mlt_service_type type
, const char *service
, mlt_metadata_callback callback
, void *callback_data
)
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
);
231 /** Get the metadata about a service
232 Returns NULL if service or its metadata are unavailable.
235 mlt_properties
mlt_repository_metadata( mlt_repository self
, mlt_service_type type
, const char *service
)
237 mlt_properties metadata
= NULL
;
238 mlt_properties properties
= get_service_properties( self
, type
, service
);
240 // If this is a valid service
243 // Lookup cached metadata
244 metadata
= mlt_properties_get_data( properties
, "metadata", NULL
);
247 // Not cached, so get the registered metadata callback function
248 mlt_metadata_callback callback
= mlt_properties_get_data( properties
, "metadata_cb", NULL
);
250 // If a metadata callback function is registered
253 // Fetch the callback data arg
254 void *data
= mlt_properties_get_data( properties
, "metadata_cb_data", NULL
);
256 // Fetch the metadata through the callback
257 metadata
= callback( type
, service
, data
);
259 // Cache the 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
);
269 static char *getenv_locale()
271 char *s
= getenv( "LANGUAGE" );
274 s
= getenv( "LC_ALL" );
277 s
= getenv( "LC_MESSAGES" );
280 s
= getenv( "LANG" );
286 /** Return a list of user-preferred language codes taken from environment variables.
289 mlt_properties
mlt_repository_languages( mlt_repository self
)
291 mlt_properties languages
= mlt_properties_get_data( &self
->parent
, "languages", NULL
);
295 const char *locale
= getenv_locale();
296 languages
= mlt_properties_new();
299 mlt_tokeniser tokeniser
= mlt_tokeniser_init();
300 int count
= mlt_tokeniser_parse_new( tokeniser
, locale
, ":" );
304 for ( i
= 0; i
< count
; i
++ )
306 char *locale
= mlt_tokeniser_get_string( tokeniser
, i
);
307 if ( strcmp( locale
, "C" ) == 0 || strcmp( locale
, "POSIX" ) == 0 )
309 else if ( strlen( locale
) > 2 )
312 snprintf( string
, sizeof(string
), "%d", i
);
313 mlt_properties_set( languages
, string
, locale
);
318 mlt_properties_set( languages
, "0", "en" );
320 mlt_tokeniser_close( tokeniser
);
324 mlt_properties_set( languages
, "0", "en" );
326 mlt_properties_set_data( &self
->parent
, "languages", languages
, 0, ( mlt_destructor
)mlt_properties_close
, NULL
);