1 /*****************************************************************************
2 * core.c: Core libvlc new API functions : initialization, exceptions handling
3 *****************************************************************************
4 * Copyright (C) 2005 the VideoLAN team
7 * Authors: Clément Stenac <zorglub@videolan.org>
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.
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.
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 *****************************************************************************/
24 #include "libvlc_internal.h"
25 #include <vlc/libvlc.h>
27 #include <vlc_interface.h>
33 static const char nomemstr[] = "Insufficient memory";
35 /*************************************************************************
37 *************************************************************************/
38 void libvlc_exception_init( libvlc_exception_t *p_exception )
40 p_exception->b_raised = 0;
41 p_exception->psz_message = NULL;
44 void libvlc_exception_clear( libvlc_exception_t *p_exception )
46 if( NULL == p_exception )
48 if( p_exception->psz_message != nomemstr )
49 free( p_exception->psz_message );
50 p_exception->psz_message = NULL;
51 p_exception->b_raised = 0;
54 int libvlc_exception_raised( const libvlc_exception_t *p_exception )
56 return (NULL != p_exception) && p_exception->b_raised;
60 libvlc_exception_get_message( const libvlc_exception_t *p_exception )
62 if( p_exception->b_raised == 1 && p_exception->psz_message )
64 return p_exception->psz_message;
69 static void libvlc_exception_not_handled( const char *psz )
71 fprintf( stderr, "*** LibVLC Exception not handled: %s\nSet a breakpoint in '%s' to debug.\n",
75 void libvlc_exception_raise( libvlc_exception_t *p_exception,
76 const char *psz_format, ... )
81 /* Unformat-ize the message */
82 va_start( args, psz_format );
83 if( vasprintf( &psz, psz_format, args ) == -1)
84 psz = (char *)nomemstr;
87 /* Does caller care about exceptions ? */
88 if( p_exception == NULL ) {
89 /* Print something, so that lazy third-parties can easily
90 * notice that something may have gone unnoticedly wrong */
91 libvlc_exception_not_handled( psz );
95 /* Make sure that there is no unnoticed previous exception */
96 if( p_exception->b_raised )
98 libvlc_exception_not_handled( p_exception->psz_message );
99 libvlc_exception_clear( p_exception );
101 p_exception->psz_message = psz;
102 p_exception->b_raised = 1;
105 libvlc_instance_t * libvlc_new( int argc, const char *const *argv,
106 libvlc_exception_t *p_e )
108 libvlc_instance_t *p_new;
110 libvlc_int_t *p_libvlc_int = libvlc_InternalCreate();
111 if( !p_libvlc_int ) RAISENULL( "VLC initialization failed" );
113 p_new = malloc( sizeof( libvlc_instance_t ) );
114 if( !p_new ) RAISENULL( "Out of memory" );
116 const char *my_argv[argc + 2];
118 my_argv[0] = "libvlc"; /* dummy arg0, skipped by getopt() et al */
119 for( int i = 0; i < argc; i++ )
120 my_argv[i + 1] = argv[i];
121 my_argv[argc + 1] = NULL; /* C calling conventions require a NULL */
123 /** \todo Look for interface settings. If we don't have any, add -I dummy */
124 /* Because we probably don't want a GUI by default */
126 i_ret = libvlc_InternalInit( p_libvlc_int, argc + 1, my_argv );
129 libvlc_InternalDestroy( p_libvlc_int );
131 if( i_ret == VLC_EEXITSUCCESS )
134 RAISENULL( "VLC initialization failed" );
137 p_new->p_libvlc_int = p_libvlc_int;
139 p_new->b_playlist_locked = 0;
140 p_new->ref_count = 1;
141 p_new->verbosity = 1;
142 p_new->p_callback_list = NULL;
143 vlc_mutex_init(&p_new->instance_lock);
144 vlc_mutex_init(&p_new->event_callback_lock);
149 void libvlc_retain( libvlc_instance_t *p_instance )
151 assert( p_instance != NULL );
152 assert( p_instance->ref_count < UINT_MAX );
154 vlc_mutex_lock( &p_instance->instance_lock );
155 p_instance->ref_count++;
156 vlc_mutex_unlock( &p_instance->instance_lock );
159 void libvlc_release( libvlc_instance_t *p_instance )
161 vlc_mutex_t *lock = &p_instance->instance_lock;
164 assert( p_instance->ref_count > 0 );
166 vlc_mutex_lock( lock );
167 refs = --p_instance->ref_count;
168 vlc_mutex_unlock( lock );
172 vlc_mutex_destroy( lock );
173 vlc_mutex_destroy( &p_instance->event_callback_lock );
174 libvlc_InternalCleanup( p_instance->p_libvlc_int );
175 libvlc_InternalDestroy( p_instance->p_libvlc_int );
180 void libvlc_add_intf( libvlc_instance_t *p_i, const char *name,
181 libvlc_exception_t *p_e )
183 if( libvlc_InternalAddIntf( p_i->p_libvlc_int, name ) )
184 RAISEVOID( "Interface initialization failed" );
187 void libvlc_wait( libvlc_instance_t *p_i )
189 libvlc_int_t *p_libvlc = p_i->p_libvlc_int;
190 libvlc_InternalWait( p_libvlc );
193 int libvlc_get_vlc_id( libvlc_instance_t *p_instance )
195 assert( p_instance );
199 const char * libvlc_get_version(void)
201 return VLC_Version();
204 const char * libvlc_get_compiler(void)
206 return VLC_Compiler();
209 const char * libvlc_get_changeset(void)
214 /* export internal libvlc_instance for ugly hacks with libvlccore */
215 vlc_object_t *libvlc_get_vlc_instance( libvlc_instance_t* p_instance )
217 vlc_object_hold( p_instance->p_libvlc_int ) ;
218 return (vlc_object_t*) p_instance->p_libvlc_int ;
221 void libvlc_free( void *ptr )