cleanup some names since we are changing the interface
[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 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 */
20
21 #include "mlt_repository.h"
22 #include "mlt_properties.h"
23
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <dlfcn.h>
27 #include <string.h>
28
29 struct mlt_repository_s
30 {
31 struct mlt_properties_s parent; // a list of object files
32 mlt_properties consumers; // lists of entry points
33 mlt_properties filters;
34 mlt_properties producers;
35 mlt_properties transitions;
36 };
37
38 mlt_repository mlt_repository_init( const char *directory )
39 {
40 // Safety check
41 if ( directory == NULL || strcmp( directory, "" ) == 0 )
42 return NULL;
43
44 // Construct the repository
45 mlt_repository this = calloc( sizeof( struct mlt_repository_s ), 1 );
46 mlt_properties_init( &this->parent, this );
47 this->consumers = mlt_properties_new();
48 this->filters = mlt_properties_new();
49 this->producers = mlt_properties_new();
50 this->transitions = mlt_properties_new();
51
52 // Get the directory list
53 mlt_properties dir = mlt_properties_new();
54 int count = mlt_properties_dir_list( dir, directory, NULL, 0 );
55 int i;
56
57 // Iterate over files
58 for ( i = 0; i < count; i++ )
59 {
60 int flags = RTLD_NOW;
61 const char *object_name = mlt_properties_get_value( dir, i);
62
63 // Very temporary hack to allow the quicktime plugins to work
64 // TODO: extend repository to allow this to be used on a case by case basis
65 if ( strstr( object_name, "libmltkino" ) )
66 flags |= RTLD_GLOBAL;
67
68 // Open the shared object
69 void *object = dlopen( object_name, flags );
70 if ( object != NULL )
71 {
72 // Get the registration function
73 int ( *symbol_ptr )( mlt_repository ) = dlsym( object, "mlt_register" );
74
75 // Call the registration function
76 if ( symbol_ptr != NULL )
77 {
78 symbol_ptr( this );
79
80 // Register the object file for closure
81 mlt_properties_set_data( &this->parent, object_name, object, 0, ( mlt_destructor )dlclose, NULL );
82 }
83 else
84 {
85 dlclose( object );
86 }
87 }
88 }
89
90 return this;
91 }
92
93 void mlt_repository_register( mlt_repository this, mlt_service_type service_type, const char *service, void *symbol )
94 {
95 // Add the entry point to the corresponding service list
96 switch ( service_type )
97 {
98 case consumer_type:
99 mlt_properties_set_data( this->consumers, service, symbol, 0, NULL, NULL );
100 break;
101 case filter_type:
102 mlt_properties_set_data( this->filters, service, symbol, 0, NULL, NULL );
103 break;
104 case producer_type:
105 mlt_properties_set_data( this->producers, service, symbol, 0, NULL, NULL );
106 break;
107 case transition_type:
108 mlt_properties_set_data( this->transitions, service, symbol, 0, NULL, NULL );
109 break;
110 default:
111 break;
112 }
113 }
114
115 void *mlt_repository_create( mlt_repository this, mlt_profile profile, mlt_service_type type, const char *service, void *input )
116 {
117 void *( *symbol_ptr )( mlt_profile, mlt_service_type, const char *, void * ) = NULL;
118
119 // Get the entry point from the corresponding service list
120 switch ( type )
121 {
122 case consumer_type:
123 symbol_ptr = mlt_properties_get_data( this->consumers, service, NULL );
124 break;
125 case filter_type:
126 symbol_ptr = mlt_properties_get_data( this->filters, service, NULL );
127 break;
128 case producer_type:
129 symbol_ptr = mlt_properties_get_data( this->producers, service, NULL );
130 break;
131 case transition_type:
132 symbol_ptr = mlt_properties_get_data( this->transitions, service, NULL );
133 break;
134 default:
135 break;
136 }
137
138 // Construct the service
139 return ( symbol_ptr != NULL ) ? symbol_ptr( profile, type, service, input ) : NULL;
140 }
141
142 void mlt_repository_close( mlt_repository this )
143 {
144 mlt_properties_close( this->consumers );
145 mlt_properties_close( this->filters );
146 mlt_properties_close( this->producers );
147 mlt_properties_close( this->transitions );
148 mlt_properties_close( &this->parent );
149 free( this );
150 }