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
);
99 mlt_properties_close( dir
);
104 static mlt_properties
new_service( void *symbol
)
106 mlt_properties properties
= mlt_properties_new();
107 mlt_properties_set_data( properties
, "symbol", symbol
, 0, NULL
, NULL
);
111 /** Register a service with the repository
112 Typically, this is invoked by a module within its mlt_register().
115 void mlt_repository_register( mlt_repository
this, mlt_service_type service_type
, const char *service
, mlt_register_callback symbol
)
117 // Add the entry point to the corresponding service list
118 switch ( service_type
)
121 mlt_properties_set_data( this->consumers
, service
, new_service( symbol
), 0, ( mlt_destructor
)mlt_properties_close
, NULL
);
124 mlt_properties_set_data( this->filters
, service
, new_service( symbol
), 0, ( mlt_destructor
)mlt_properties_close
, NULL
);
127 mlt_properties_set_data( this->producers
, service
, new_service( symbol
), 0, ( mlt_destructor
)mlt_properties_close
, NULL
);
129 case transition_type
:
130 mlt_properties_set_data( this->transitions
, service
, new_service( symbol
), 0, ( mlt_destructor
)mlt_properties_close
, NULL
);
137 static mlt_properties
get_service_properties( mlt_repository
this, mlt_service_type type
, const char *service
)
139 mlt_properties service_properties
= NULL
;
141 // Get the entry point from the corresponding service list
145 service_properties
= mlt_properties_get_data( this->consumers
, service
, NULL
);
148 service_properties
= mlt_properties_get_data( this->filters
, service
, NULL
);
151 service_properties
= mlt_properties_get_data( this->producers
, service
, NULL
);
153 case transition_type
:
154 service_properties
= mlt_properties_get_data( this->transitions
, service
, NULL
);
159 return service_properties
;
162 /** Construct a new instance of a service
165 void *mlt_repository_create( mlt_repository
this, mlt_profile profile
, mlt_service_type type
, const char *service
, void *input
)
167 mlt_properties properties
= get_service_properties( this, type
, service
);
168 if ( properties
!= NULL
)
170 mlt_register_callback symbol_ptr
= mlt_properties_get_data( properties
, "symbol", NULL
);
172 // Construct the service
173 return ( symbol_ptr
!= NULL
) ?
symbol_ptr( profile
, type
, service
, input
) : NULL
;
178 /** Destroy a repository
181 void mlt_repository_close( mlt_repository
this )
183 mlt_properties_close( this->consumers
);
184 mlt_properties_close( this->filters
);
185 mlt_properties_close( this->producers
);
186 mlt_properties_close( this->transitions
);
187 mlt_properties_close( &this->parent
);
191 /** Get the list of registered consumers
194 mlt_properties
mlt_repository_consumers( mlt_repository self
)
196 return self
->consumers
;
199 /** Get the list of registered filters
202 mlt_properties
mlt_repository_filters( mlt_repository self
)
204 return self
->filters
;
207 /** Get the list of registered producers
210 mlt_properties
mlt_repository_producers( mlt_repository self
)
212 return self
->producers
;
215 /** Get the list of registered transitions
218 mlt_properties
mlt_repository_transitions( mlt_repository self
)
220 return self
->transitions
;
223 /** Register the metadata for a service
224 IMPORTANT: mlt_repository will take responsibility for deallocating the metadata properties that you supply!
226 void mlt_repository_register_metadata( mlt_repository self
, mlt_service_type type
, const char *service
, mlt_metadata_callback callback
, void *callback_data
)
228 mlt_properties service_properties
= get_service_properties( self
, type
, service
);
229 mlt_properties_set_data( service_properties
, "metadata_cb", callback
, 0, NULL
, NULL
);
230 mlt_properties_set_data( service_properties
, "metadata_cb_data", callback_data
, 0, NULL
, NULL
);
233 /** Get the metadata about a service
234 Returns NULL if service or its metadata are unavailable.
237 mlt_properties
mlt_repository_metadata( mlt_repository self
, mlt_service_type type
, const char *service
)
239 mlt_properties metadata
= NULL
;
240 mlt_properties properties
= get_service_properties( self
, type
, service
);
242 // If this is a valid service
245 // Lookup cached metadata
246 metadata
= mlt_properties_get_data( properties
, "metadata", NULL
);
249 // Not cached, so get the registered metadata callback function
250 mlt_metadata_callback callback
= mlt_properties_get_data( properties
, "metadata_cb", NULL
);
252 // If a metadata callback function is registered
255 // Fetch the callback data arg
256 void *data
= mlt_properties_get_data( properties
, "metadata_cb_data", NULL
);
258 // Fetch the metadata through the callback
259 metadata
= callback( type
, service
, data
);
261 // Cache the metadata
263 // Include dellocation and serialisation
264 mlt_properties_set_data( properties
, "metadata", metadata
, 0, ( mlt_destructor
)mlt_properties_close
, ( mlt_serialiser
)mlt_properties_serialise_yaml
);
271 static char *getenv_locale()
273 char *s
= getenv( "LANGUAGE" );
276 s
= getenv( "LC_ALL" );
279 s
= getenv( "LC_MESSAGES" );
282 s
= getenv( "LANG" );
288 /** Return a list of user-preferred language codes taken from environment variables.
291 mlt_properties
mlt_repository_languages( mlt_repository self
)
293 mlt_properties languages
= mlt_properties_get_data( &self
->parent
, "languages", NULL
);
297 languages
= mlt_properties_new();
298 char *locale
= getenv_locale();
301 locale
= strdup( locale
);
302 mlt_tokeniser tokeniser
= mlt_tokeniser_init();
303 int count
= mlt_tokeniser_parse_new( tokeniser
, locale
, ":" );
307 for ( i
= 0; i
< count
; i
++ )
309 char *locale
= mlt_tokeniser_get_string( tokeniser
, i
);
310 if ( strcmp( locale
, "C" ) == 0 || strcmp( locale
, "POSIX" ) == 0 )
312 else if ( strlen( locale
) > 2 )
315 snprintf( string
, sizeof(string
), "%d", i
);
316 mlt_properties_set( languages
, string
, locale
);
321 mlt_properties_set( languages
, "0", "en" );
324 mlt_tokeniser_close( tokeniser
);
328 mlt_properties_set( languages
, "0", "en" );
330 mlt_properties_set_data( &self
->parent
, "languages", languages
, 0, ( mlt_destructor
)mlt_properties_close
, NULL
);