]> git.sesse.net Git - vlc/blob - src/control/core.c
3a67a82ba12b47294f9a82dd302a8c474c3b65cd
[vlc] / src / control / core.c
1 /*****************************************************************************
2  * core.c: Core libvlc new API functions : initialization, exceptions handling
3  *****************************************************************************
4  * Copyright (C) 2005 the VideoLAN team
5  * $Id$
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #include "libvlc_internal.h"
25 #include <vlc/libvlc.h>
26
27 #include <vlc_interface.h>
28 #include <vlc_vlm.h>
29
30 #include <stdarg.h>
31 #include <limits.h>
32 #include <assert.h>
33
34 static const char nomemstr[] = "Insufficient memory";
35
36 /*************************************************************************
37  * Exceptions handling
38  *************************************************************************/
39 void libvlc_exception_init( libvlc_exception_t *p_exception )
40 {
41     p_exception->b_raised = 0;
42     p_exception->psz_message = NULL;
43 }
44
45 void libvlc_exception_clear( libvlc_exception_t *p_exception )
46 {
47     if( NULL == p_exception )
48         return;
49     if( p_exception->psz_message != nomemstr )
50         free( p_exception->psz_message );
51     p_exception->psz_message = NULL;
52     p_exception->b_raised = 0;
53 }
54
55 int libvlc_exception_raised( const libvlc_exception_t *p_exception )
56 {
57     return (NULL != p_exception) && p_exception->b_raised;
58 }
59
60 const char *
61 libvlc_exception_get_message( const libvlc_exception_t *p_exception )
62 {
63     if( p_exception->b_raised == 1 && p_exception->psz_message )
64     {
65         return p_exception->psz_message;
66     }
67     return NULL;
68 }
69
70 static void libvlc_exception_not_handled( const char *psz )
71 {
72     fprintf( stderr, "*** LibVLC Exception not handled: %s\nSet a breakpoint in '%s' to debug.\n",
73              psz, __func__ );
74     abort();
75 }
76
77 void libvlc_exception_raise( libvlc_exception_t *p_exception,
78                              const char *psz_format, ... )
79 {
80     va_list args;
81     char * psz;
82
83     /* Unformat-ize the message */
84     va_start( args, psz_format );
85     if( vasprintf( &psz, psz_format, args ) == -1)
86         psz = (char *)nomemstr;
87     va_end( args );
88
89     /* Does caller care about exceptions ? */
90     if( p_exception == NULL ) {
91         /* Print something, so that lazy third-parties can easily
92          * notice that something may have gone unnoticedly wrong */
93         libvlc_exception_not_handled( psz );
94         if( psz != nomemstr )
95             free( psz );
96         return;
97     }
98
99     /* Make sure that there is no unnoticed previous exception */
100     if( p_exception->b_raised )
101     {
102         libvlc_exception_not_handled( p_exception->psz_message );
103         libvlc_exception_clear( p_exception );
104     }
105     p_exception->psz_message = psz;
106     p_exception->b_raised = 1;
107 }
108
109 libvlc_instance_t * libvlc_new( int argc, const char *const *argv,
110                                 libvlc_exception_t *p_e )
111 {
112     libvlc_instance_t *p_new;
113     int i_ret;
114
115     libvlc_init_threads ();
116
117     libvlc_int_t *p_libvlc_int = libvlc_InternalCreate();
118     if( !p_libvlc_int )
119     {
120         libvlc_deinit_threads ();
121         RAISENULL( "VLC initialization failed" );
122     }
123
124     p_new = malloc( sizeof( libvlc_instance_t ) );
125     if( !p_new )
126     {
127         libvlc_deinit_threads ();
128         RAISENULL( "Out of memory" );
129     }
130
131     const char *my_argv[argc + 2];
132
133     my_argv[0] = "libvlc"; /* dummy arg0, skipped by getopt() et al */
134     for( int i = 0; i < argc; i++ )
135          my_argv[i + 1] = argv[i];
136     my_argv[argc + 1] = NULL; /* C calling conventions require a NULL */
137
138     /** \todo Look for interface settings. If we don't have any, add -I dummy */
139     /* Because we probably don't want a GUI by default */
140
141     i_ret = libvlc_InternalInit( p_libvlc_int, argc + 1, my_argv );
142     if( i_ret )
143     {
144         libvlc_InternalDestroy( p_libvlc_int );
145         free( p_new );
146         libvlc_deinit_threads ();
147
148         if( i_ret == VLC_EEXITSUCCESS )
149             return NULL;
150         else
151             RAISENULL( "VLC initialization failed" );
152     }
153
154     p_new->p_libvlc_int = p_libvlc_int;
155     p_new->libvlc_vlm.p_vlm = NULL;
156     p_new->libvlc_vlm.p_event_manager = NULL;
157     p_new->libvlc_vlm.pf_release = NULL;
158     p_new->b_playlist_locked = 0;
159     p_new->ref_count = 1;
160     p_new->verbosity = 1;
161     p_new->p_callback_list = NULL;
162     vlc_mutex_init(&p_new->instance_lock);
163     vlc_mutex_init(&p_new->event_callback_lock);
164
165     return p_new;
166 }
167
168 void libvlc_retain( libvlc_instance_t *p_instance )
169 {
170     assert( p_instance != NULL );
171     assert( p_instance->ref_count < UINT_MAX );
172
173     vlc_mutex_lock( &p_instance->instance_lock );
174     p_instance->ref_count++;
175     vlc_mutex_unlock( &p_instance->instance_lock );
176 }
177
178 void libvlc_release( libvlc_instance_t *p_instance )
179 {
180     vlc_mutex_t *lock = &p_instance->instance_lock;
181     int refs;
182
183     vlc_mutex_lock( lock );
184     assert( p_instance->ref_count > 0 );
185     refs = --p_instance->ref_count;
186     vlc_mutex_unlock( lock );
187
188     if( refs == 0 )
189     {
190         vlc_mutex_destroy( lock );
191         vlc_mutex_destroy( &p_instance->event_callback_lock );
192         if( p_instance->libvlc_vlm.pf_release )
193             p_instance->libvlc_vlm.pf_release( p_instance );
194         libvlc_InternalCleanup( p_instance->p_libvlc_int );
195         libvlc_InternalDestroy( p_instance->p_libvlc_int );
196         free( p_instance );
197         libvlc_deinit_threads ();
198     }
199 }
200
201 int libvlc_add_intf( libvlc_instance_t *p_i, const char *name,
202                       libvlc_exception_t *p_e )
203 {
204     if( libvlc_InternalAddIntf( p_i->p_libvlc_int, name ) )
205     {
206         libvlc_exception_raise( p_e, "Interface initialization failed" );
207         return -1;
208     }
209     return 0;
210 }
211
212 void libvlc_wait( libvlc_instance_t *p_i )
213 {
214     libvlc_int_t *p_libvlc = p_i->p_libvlc_int;
215     libvlc_InternalWait( p_libvlc );
216 }
217
218 const char * libvlc_get_version(void)
219 {
220     return VLC_Version();
221 }
222
223 const char * libvlc_get_compiler(void)
224 {
225     return VLC_Compiler();
226 }
227
228 const char * libvlc_get_changeset(void)
229 {
230     extern const char psz_vlc_changeset[];
231     return psz_vlc_changeset;
232 }
233
234 /* export internal libvlc_instance for ugly hacks with libvlccore */
235 vlc_object_t *libvlc_get_vlc_instance( libvlc_instance_t* p_instance )
236 {
237     vlc_object_hold( p_instance->p_libvlc_int ) ;
238     return (vlc_object_t*) p_instance->p_libvlc_int ;
239 }
240
241 void libvlc_free( void *ptr )
242 {
243     free( ptr );
244 }