Simplify
[vlc/vlc-skelet.git] / src / control / core.c
blobed332e2d323015d93338cc7b3849a008abab579a
1 /*****************************************************************************
2 * core.c: Core libvlc new API functions : initialization, exceptions handling
3 *****************************************************************************
4 * Copyright (C) 2005 the VideoLAN team
5 * $Id$
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>
29 #include <stdarg.h>
30 #include <limits.h>
31 #include <assert.h>
33 static const char nomemstr[] = "Insufficient memory";
35 /*************************************************************************
36 * Exceptions handling
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( p_exception->psz_message != nomemstr )
47 free( p_exception->psz_message );
48 p_exception->psz_message = NULL;
49 p_exception->b_raised = 0;
52 int libvlc_exception_raised( const libvlc_exception_t *p_exception )
54 return (NULL != p_exception) && p_exception->b_raised;
57 const char *
58 libvlc_exception_get_message( const libvlc_exception_t *p_exception )
60 if( p_exception->b_raised == 1 && p_exception->psz_message )
62 return p_exception->psz_message;
64 return NULL;
67 static void libvlc_exception_not_handled( const char *psz )
69 fprintf( stderr, "*** LibVLC Exception not handled: %s\nSet a breakpoint in '%s' to debug.\n",
70 psz, __func__ );
73 void libvlc_exception_raise( libvlc_exception_t *p_exception,
74 const char *psz_format, ... )
76 va_list args;
77 char * psz;
79 /* Unformat-ize the message */
80 va_start( args, psz_format );
81 if( vasprintf( &psz, psz_format, args ) == -1)
82 psz = (char *)nomemstr;
83 va_end( args );
85 /* Does caller care about exceptions ? */
86 if( p_exception == NULL ) {
87 /* Print something, so lazy third-parties can easily
88 * notice that something may have gone unoticedly wrong */
89 libvlc_exception_not_handled( psz );
90 return;
93 /* Make sure that there is no unoticed previous exception */
94 if( p_exception->b_raised )
96 libvlc_exception_not_handled( p_exception->psz_message );
97 libvlc_exception_clear( p_exception );
99 p_exception->psz_message = psz;
100 p_exception->b_raised = 1;
103 libvlc_instance_t * libvlc_new( int argc, const char *const *argv,
104 libvlc_exception_t *p_e )
106 libvlc_instance_t *p_new;
108 libvlc_int_t *p_libvlc_int = libvlc_InternalCreate();
109 if( !p_libvlc_int ) RAISENULL( "VLC initialization failed" );
111 p_new = (libvlc_instance_t *)malloc( sizeof( libvlc_instance_t ) );
112 if( !p_new ) RAISENULL( "Out of memory" );
114 const char *my_argv[argc + 2];
116 my_argv[0] = "libvlc"; /* dummy arg0, skipped by getopt() et al */
117 for( int i = 0; i < argc; i++ )
118 my_argv[i + 1] = argv[i];
119 my_argv[argc + 1] = NULL; /* C calling conventions require a NULL */
121 /** \todo Look for interface settings. If we don't have any, add -I dummy */
122 /* Because we probably don't want a GUI by default */
124 if( libvlc_InternalInit( p_libvlc_int, argc + 1, my_argv ) )
125 RAISENULL( "VLC initialization failed" );
127 p_new->p_libvlc_int = p_libvlc_int;
128 p_new->p_vlm = NULL;
129 p_new->b_playlist_locked = 0;
130 p_new->ref_count = 1;
131 p_new->p_callback_list = NULL;
132 vlc_mutex_init(&p_new->instance_lock);
133 vlc_mutex_init(&p_new->event_callback_lock);
135 return p_new;
138 void libvlc_retain( libvlc_instance_t *p_instance )
140 assert( p_instance != NULL );
141 assert( p_instance->ref_count < UINT_MAX );
143 vlc_mutex_lock( &p_instance->instance_lock );
144 p_instance->ref_count++;
145 vlc_mutex_unlock( &p_instance->instance_lock );
148 void libvlc_release( libvlc_instance_t *p_instance )
150 vlc_mutex_t *lock = &p_instance->instance_lock;
151 int refs;
153 assert( p_instance->ref_count > 0 );
155 vlc_mutex_lock( lock );
156 refs = --p_instance->ref_count;
157 vlc_mutex_unlock( lock );
159 if( refs == 0 )
161 vlc_mutex_destroy( lock );
162 vlc_mutex_destroy( &p_instance->event_callback_lock );
163 libvlc_InternalCleanup( p_instance->p_libvlc_int );
164 libvlc_InternalDestroy( p_instance->p_libvlc_int, false );
165 free( p_instance );
169 void libvlc_add_intf( libvlc_instance_t *p_i, const char *name,
170 libvlc_exception_t *p_e )
172 if( libvlc_InternalAddIntf( p_i->p_libvlc_int, name, false, true, 0, NULL ) )
173 RAISEVOID( "Interface initialization failed" );
176 void libvlc_wait( libvlc_instance_t *p_i )
178 libvlc_int_t *p_libvlc = p_i->p_libvlc_int;
179 vlc_object_lock( p_libvlc );
180 while( vlc_object_alive( p_libvlc ) )
181 vlc_object_wait( p_libvlc );
182 vlc_object_unlock( p_libvlc );
185 int libvlc_get_vlc_id( libvlc_instance_t *p_instance )
187 return p_instance->p_libvlc_int->i_object_id;
190 const char * libvlc_get_version()
192 return VLC_Version();
195 const char * libvlc_get_compiler()
197 return VLC_Compiler();
200 const char * libvlc_get_changeset()
202 return VLC_Changeset();