]> git.sesse.net Git - vlc/blob - src/control/core.c
libvlc: revert [22749], useless, breaks plugins
[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 #include "libvlc_internal.h"
24 #include <vlc/libvlc.h>
25
26 #include <vlc_interface.h>
27
28 #include <stdarg.h>
29 #include <limits.h>
30 #include <assert.h>
31
32 static const char nomemstr[] = "Insufficient memory";
33
34 /*************************************************************************
35  * Exceptions handling
36  *************************************************************************/
37 void libvlc_exception_init( libvlc_exception_t *p_exception )
38 {
39     p_exception->b_raised = 0;
40     p_exception->psz_message = NULL;
41 }
42
43 void libvlc_exception_clear( libvlc_exception_t *p_exception )
44 {
45     if( p_exception->psz_message != nomemstr )
46         free( p_exception->psz_message );
47     p_exception->psz_message = NULL;
48     p_exception->b_raised = 0;
49 }
50
51 int libvlc_exception_raised( const libvlc_exception_t *p_exception )
52 {
53     return (NULL != p_exception) && p_exception->b_raised;
54 }
55
56 const char *
57 libvlc_exception_get_message( const libvlc_exception_t *p_exception )
58 {
59     if( p_exception->b_raised == 1 && p_exception->psz_message )
60     {
61         return p_exception->psz_message;
62     }
63     return NULL;
64 }
65
66 static void libvlc_exception_not_handled( const char *psz )
67 {
68     fprintf( stderr, "*** LibVLC Exception not handled: %s\nSet a breakpoint in '%s' to debug.\n",
69              psz, __func__ );
70 }
71
72 void libvlc_exception_raise( libvlc_exception_t *p_exception,
73                                            const char *psz_format, ... )
74 {
75     va_list args;
76     char * psz;
77
78     /* Unformat-ize the message */
79     va_start( args, psz_format );
80     if( vasprintf( &psz, psz_format, args ) == -1)
81         psz = (char *)nomemstr;
82     va_end( args );
83
84     /* Does caller care about exceptions ? */
85     if( p_exception == NULL ) {
86         /* Print something, so lazy third-parties can easily
87          * notice that something may have gone unoticedly wrong */
88         libvlc_exception_not_handled( psz );
89         return;
90     }
91
92     /* Make sure that there is no unoticed previous exception */
93     if( p_exception->b_raised )
94     {
95         libvlc_exception_not_handled( p_exception->psz_message );
96         libvlc_exception_clear( p_exception );
97     }
98     p_exception->psz_message = psz;
99     p_exception->b_raised = 1;
100 }
101
102 libvlc_instance_t * libvlc_new( int argc, const char *const *argv,
103                                 libvlc_exception_t *p_e )
104 {
105     libvlc_instance_t *p_new;
106
107     libvlc_int_t *p_libvlc_int = libvlc_InternalCreate();
108     if( !p_libvlc_int ) RAISENULL( "VLC initialization failed" );
109
110     p_new = (libvlc_instance_t *)malloc( sizeof( libvlc_instance_t ) );
111     if( !p_new ) RAISENULL( "Out of memory" );
112
113     /** \todo Look for interface settings. If we don't have any, add -I dummy */
114     /* Because we probably don't want a GUI by default */
115
116     /* WARNING: caller must pass a program path in argv[0], which can be a dummy path
117      *          this is used by libvlc to locate, this is leveraged by plugins */
118     if( libvlc_InternalInit( p_libvlc_int, argc, argv ) )
119         RAISENULL( "VLC initialization failed" );
120
121     p_new->p_libvlc_int = p_libvlc_int;
122     p_new->p_vlm = NULL;
123     p_new->b_playlist_locked = 0;
124     p_new->ref_count = 1;
125     p_new->p_callback_list = NULL;
126     vlc_mutex_init(p_libvlc_int, &p_new->instance_lock);
127     vlc_mutex_init(p_libvlc_int, &p_new->event_callback_lock);
128  
129     libvlc_event_init(p_new, p_e);
130
131     return p_new;
132 }
133
134 void libvlc_retain( libvlc_instance_t *p_instance )
135 {
136     assert( p_instance != NULL );
137     assert( p_instance->ref_count < UINT_MAX );
138
139     vlc_mutex_lock( &p_instance->instance_lock );
140     p_instance->ref_count++;
141     vlc_mutex_unlock( &p_instance->instance_lock );
142 }
143
144 void libvlc_release( libvlc_instance_t *p_instance )
145 {
146     vlc_mutex_t *lock = &p_instance->instance_lock;
147     int refs;
148
149     assert( p_instance->ref_count > 0 );
150
151     vlc_mutex_lock( lock );
152     refs = --p_instance->ref_count;
153     if( refs == 0 )
154         libvlc_event_fini( p_instance );
155     vlc_mutex_unlock( lock );
156
157     if( refs == 0 )
158     {
159         vlc_mutex_destroy( lock );
160         vlc_mutex_destroy( &p_instance->event_callback_lock );
161         libvlc_InternalCleanup( p_instance->p_libvlc_int );
162         libvlc_InternalDestroy( p_instance->p_libvlc_int, VLC_FALSE );
163         free( p_instance );
164     }
165 }
166
167 int libvlc_get_vlc_id( libvlc_instance_t *p_instance )
168 {
169     return p_instance->p_libvlc_int->i_object_id;
170 }