Merge fixes to the python binding from my branch
[vlc.git] / src / control / init.c
1 #define __VLC__
2 #include <vlc/control.h>
3
4 mediacontrol_Instance* mediacontrol_new( char** args, mediacontrol_Exception *exception )
5 {
6     mediacontrol_Instance* retval;
7     vlc_object_t *p_vlc;
8     int p_vlc_id;
9     char **ppsz_argv;
10     int i_count = 0;
11     int i_index;
12     char **p_tmp;
13
14     if( args )
15     {
16         for ( p_tmp = args ; *p_tmp != NULL ; p_tmp++ )
17             i_count++;
18     }
19
20     ppsz_argv = malloc( i_count + 2 );
21     if( ! ppsz_argv )
22     {
23         exception->code = mediacontrol_InternalException;
24         exception->message = "Out of memory";
25         return NULL;
26     }
27     ppsz_argv[0] = "vlc";
28     for ( i_index = 0; i_index < i_count; i_index++ )
29     {
30         ppsz_argv[i_index + 1] = strdup( args[i_index] );
31         if( ! ppsz_argv[i_index + 1] )
32         {
33             exception->code = mediacontrol_InternalException;
34             exception->message = "Out of memory";
35             return NULL;
36         }
37     }
38
39     ppsz_argv[i_count + 1] = NULL;
40
41     p_vlc_id = VLC_Create();
42   
43     p_vlc = ( vlc_object_t* )vlc_current_object( p_vlc_id );
44   
45     if( ! p_vlc )
46     {
47         exception->code = mediacontrol_InternalException;
48         exception->message = strdup( "Unable to initialize VLC" );
49         return NULL;
50     }
51     retval = ( mediacontrol_Instance* )malloc( sizeof( mediacontrol_Instance ) );
52     if( ! retval )
53     {
54         exception->code = mediacontrol_InternalException;
55         exception->message = strdup( "Out of memory" );
56         return NULL;
57     }
58
59     VLC_Init( p_vlc_id, i_count + 1, ppsz_argv );
60
61     retval->p_vlc = p_vlc;
62     retval->vlc_object_id = p_vlc_id;
63
64     /* We can keep references on these, which should not change. Is it true ? */
65     retval->p_playlist = vlc_object_find( p_vlc,
66                                          VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
67     retval->p_intf = vlc_object_find( p_vlc, VLC_OBJECT_INTF, FIND_ANYWHERE );
68
69     if( ! retval->p_playlist || ! retval->p_intf )
70     {
71         exception->code = mediacontrol_InternalException;
72         exception->message = strdup( "No available interface" );
73         return NULL;
74     }
75
76     
77     return retval;  
78 };
79
80 void
81 mediacontrol_exit( mediacontrol_Instance *self )
82 {
83   
84     vlc_object_release( (vlc_object_t* )self->p_playlist );
85     vlc_object_release( (vlc_object_t* )self->p_intf );
86     vlc_object_release( (vlc_object_t*)self->p_vlc );
87   
88     VLC_CleanUp( self->vlc_object_id );
89     VLC_Destroy( self->vlc_object_id );
90 }