]> git.sesse.net Git - vlc/blob - src/control/core.c
* Beginning of VLM API
[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�ent 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/intf.h>
28
29 /*************************************************************************
30  * Exceptions handling
31  *************************************************************************/
32 inline void libvlc_exception_init( libvlc_exception_t *p_exception )
33 {
34     p_exception->b_raised = 0;
35     p_exception->psz_message = NULL;
36 }
37
38 void libvlc_exception_clear( libvlc_exception_t *p_exception )
39 {
40     if( p_exception->psz_message )
41         free( p_exception->psz_message );
42     p_exception->psz_message = NULL;
43     p_exception->b_raised = 0;
44 }
45
46 inline int libvlc_exception_raised( libvlc_exception_t *p_exception )
47 {
48     return p_exception->b_raised;
49 }
50
51 inline char* libvlc_exception_get_message( libvlc_exception_t *p_exception )
52 {
53     if( p_exception->b_raised == 1 && p_exception->psz_message )
54     {
55         return p_exception->psz_message;
56     }
57     return NULL;
58 }
59
60 inline void libvlc_exception_raise( libvlc_exception_t *p_exception,
61                                     char *psz_format, ... )
62 {
63     va_list args;
64     char *psz_message;
65     va_start( args, psz_message );
66     vasprintf( &psz_message, psz_format, args );
67     va_end( args );
68
69     if( p_exception == NULL ) return;
70     p_exception->b_raised = 1;
71     if( psz_message )
72         p_exception->psz_message = strdup( psz_message );
73 }
74
75 libvlc_instance_t * libvlc_new( int argc, char **argv,
76                                 libvlc_exception_t *p_exception )
77 {
78     int i_vlc_id;
79     libvlc_instance_t *p_new;
80     vlc_t *p_vlc;
81
82     i_vlc_id = VLC_Create();
83     p_vlc = (vlc_t* ) vlc_current_object( i_vlc_id );
84
85     if( !p_vlc )
86     {
87         libvlc_exception_raise( p_exception, "VLC initialization failed" );
88         return NULL;
89     }
90     p_new = (libvlc_instance_t *)malloc( sizeof( libvlc_instance_t ) );
91
92     /** \todo Look for interface settings. If we don't have any, add -I dummy */
93     /* Because we probably don't want a GUI by default */
94
95     if( !p_new )
96     {
97         libvlc_exception_raise( p_exception, "Out of memory" );
98         return NULL;
99     }
100
101     VLC_Init( i_vlc_id, argc, argv );
102
103     p_new->p_vlc = p_vlc;
104     p_new->p_playlist = (playlist_t *)vlc_object_find( p_new->p_vlc,
105                                 VLC_OBJECT_PLAYLIST, FIND_CHILD );
106     p_new->p_vlm = NULL;
107
108     if( !p_new->p_playlist )
109     {
110         libvlc_exception_raise( p_exception, "Playlist creation failed" );
111         return NULL;
112     }
113     p_new->i_vlc_id = i_vlc_id;
114
115     return p_new;
116 }
117
118 void libvlc_destroy( libvlc_instance_t *p_instance )
119 {
120     if( p_instance->p_playlist )
121         vlc_object_release( p_instance->p_playlist );
122     vlc_object_release( p_instance->p_vlc );
123     VLC_CleanUp( p_instance->i_vlc_id );
124     VLC_Destroy( p_instance->i_vlc_id );
125 }