]> git.sesse.net Git - vlc/blob - src/control/core.c
Changing the order of parameters may be needed, but changing the parameters themselve...
[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 <stdarg.h>
24 #include "libvlc_internal.h"
25 #include <vlc/libvlc.h>
26
27 #include <vlc_interface.h>
28
29 static const char nomemstr[] = "Insufficient memory";
30
31 /*************************************************************************
32  * Exceptions handling
33  *************************************************************************/
34 void libvlc_exception_init( libvlc_exception_t *p_exception )
35 {
36     p_exception->b_raised = 0;
37     p_exception->psz_message = NULL;
38 }
39
40 void libvlc_exception_clear( libvlc_exception_t *p_exception )
41 {
42     if( p_exception->psz_message != nomemstr )
43         free( p_exception->psz_message );
44     p_exception->psz_message = NULL;
45     p_exception->b_raised = 0;
46 }
47
48 int libvlc_exception_raised( const libvlc_exception_t *p_exception )
49 {
50     return (NULL != p_exception) && p_exception->b_raised;
51 }
52
53 const char *
54 libvlc_exception_get_message( const libvlc_exception_t *p_exception )
55 {
56     if( p_exception->b_raised == 1 && p_exception->psz_message )
57     {
58         return p_exception->psz_message;
59     }
60     return NULL;
61 }
62
63 void libvlc_exception_raise( libvlc_exception_t *p_exception,
64                                            const char *psz_format, ... )
65 {
66     va_list args;
67
68     /* does caller care about exceptions ? */
69     if( p_exception == NULL ) return;
70
71     /* remove previous exception if it wasn't cleared */
72     libvlc_exception_clear( p_exception );
73
74     va_start( args, psz_format );
75     if( vasprintf( &p_exception->psz_message, psz_format, args ) == -1)
76         p_exception->psz_message = (char *)nomemstr;
77     va_end( args );
78
79     p_exception->b_raised = 1;
80 }
81
82 libvlc_instance_t * libvlc_new( int argc, const char *const *argv,
83                                 libvlc_exception_t *p_e )
84 {
85     libvlc_instance_t *p_new;
86
87     libvlc_int_t *p_libvlc_int = libvlc_InternalCreate();
88     if( !p_libvlc_int ) RAISENULL( "VLC initialization failed" );
89
90     p_new = (libvlc_instance_t *)malloc( sizeof( libvlc_instance_t ) );
91     if( !p_new ) RAISENULL( "Out of memory" );
92
93     const char *my_argv[argc + 2];
94
95     my_argv[0] = "libvlc"; /* dummy arg0, skipped by getopt() et al */
96     for( int i = 0; i < argc; i++ )
97          my_argv[i + 1] = argv[i];
98     my_argv[argc + 1] = NULL; /* C calling conventions require a NULL */
99
100     /** \todo Look for interface settings. If we don't have any, add -I dummy */
101     /* Because we probably don't want a GUI by default */
102
103     if( libvlc_InternalInit( p_libvlc_int, argc + 1, my_argv ) )
104         RAISENULL( "VLC initialization failed" );
105
106     p_new->p_libvlc_int = p_libvlc_int;
107     p_new->p_vlm = NULL;
108     p_new->b_playlist_locked = 0;
109     p_new->p_callback_list = NULL;
110     vlc_mutex_init(p_libvlc_int, &p_new->instance_lock);
111     vlc_mutex_init(p_libvlc_int, &p_new->event_callback_lock);
112  
113     libvlc_event_init(p_new, p_e);
114
115     return p_new;
116 }
117
118 void libvlc_destroy( libvlc_instance_t *p_instance, libvlc_exception_t *p_e )
119 {
120     libvlc_event_fini( p_instance, p_e );
121     vlc_mutex_destroy( &p_instance->instance_lock );
122     vlc_mutex_destroy( &p_instance->event_callback_lock);
123     libvlc_InternalCleanup( p_instance->p_libvlc_int );
124     libvlc_InternalDestroy( p_instance->p_libvlc_int, VLC_FALSE );
125     free( p_instance );
126 }
127
128 int libvlc_get_vlc_id( libvlc_instance_t *p_instance )
129 {
130     return p_instance->p_libvlc_int->i_object_id;
131 }