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