]> git.sesse.net Git - vlc/blob - src/interface/interface.c
a6a9938f8d7cf9541ceeff73a16e6a49017639a4
[vlc] / src / interface / interface.c
1 /*****************************************************************************
2  * interface.c: interface access for other threads
3  * This library provides basic functions for threads to interact with user
4  * interface, such as command line.
5  *****************************************************************************
6  * Copyright (C) 1998-2007 VLC authors and VideoLAN
7  * $Id$
8  *
9  * Authors: Vincent Seguin <seguin@via.ecp.fr>
10  *
11  * This program is free software; you can redistribute it and/or modify it
12  * under the terms of the GNU Lesser General Public License as published by
13  * the Free Software Foundation; either version 2.1 of the License, or
14  * (at your option) any later version.
15  *
16  * This program is distributed in the hope that it will be useful,
17  * but WITHOUT ANY WARRANTY; without even the implied warranty of
18  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
19  * GNU Lesser General Public License for more details.
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * along with this program; if not, write to the Free Software Foundation,
23  * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 /**
27  *   \file
28  *   This file contains functions related to interface management
29  */
30
31
32 /*****************************************************************************
33  * Preamble
34  *****************************************************************************/
35
36 #ifdef HAVE_CONFIG_H
37 # include "config.h"
38 #endif
39
40 #include <assert.h>
41 #ifdef HAVE_UNISTD_H
42 # include <unistd.h>
43 #endif
44
45 #include <vlc_common.h>
46 #include <vlc_modules.h>
47 #include <vlc_interface.h>
48 #include "libvlc.h"
49
50 #ifdef __APPLE__
51 #include "../lib/libvlc_internal.h"
52 #endif
53
54 static int AddIntfCallback( vlc_object_t *, char const *,
55                             vlc_value_t , vlc_value_t , void * );
56
57 static vlc_mutex_t lock = VLC_STATIC_MUTEX;
58
59 #undef intf_Create
60 /**
61  * Create and start an interface.
62  *
63  * @param p_this the calling vlc_object_t
64  * @param chain configuration chain string
65  * @return VLC_SUCCESS or an error code
66  */
67 int intf_Create( vlc_object_t *p_this, const char *chain )
68 {
69     libvlc_int_t *p_libvlc = p_this->p_libvlc;
70     intf_thread_t * p_intf;
71
72     /* Allocate structure */
73     p_intf = vlc_custom_create( p_libvlc, sizeof( *p_intf ), "interface" );
74     if( !p_intf )
75         return VLC_ENOMEM;
76
77     /* Variable used for interface spawning */
78     vlc_value_t val, text;
79     var_Create( p_intf, "intf-add", VLC_VAR_STRING |
80                 VLC_VAR_HASCHOICE | VLC_VAR_ISCOMMAND );
81     text.psz_string = _("Add Interface");
82     var_Change( p_intf, "intf-add", VLC_VAR_SETTEXT, &text, NULL );
83 #if !defined(WIN32) && defined(HAVE_ISATTY)
84     if( isatty( 0 ) )
85 #endif
86     {
87         val.psz_string = (char *)"rc";
88         text.psz_string = (char *)_("Console");
89         var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
90     }
91     val.psz_string = (char *)"telnet";
92     text.psz_string = (char *)_("Telnet");
93     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
94     val.psz_string = (char *)"http";
95     text.psz_string = (char *)_("Web");
96     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
97     val.psz_string = (char *)"logger";
98     text.psz_string = (char *)_("Debug logging");
99     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
100     val.psz_string = (char *)"gestures";
101     text.psz_string = (char *)_("Mouse Gestures");
102     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
103
104     var_AddCallback( p_intf, "intf-add", AddIntfCallback, NULL );
105
106     /* Attach interface to LibVLC */
107
108     /* Choose the best module */
109     p_intf->p_cfg = NULL;
110     char *psz_parser = *chain == '$'
111                      ? var_CreateGetString(p_intf, chain+1)
112                      : strdup( chain );
113     char *module;
114     char *psz_tmp = config_ChainCreate( &module, &p_intf->p_cfg,
115                                         psz_parser );
116     free( psz_tmp );
117     free( psz_parser );
118     p_intf->p_module = module_need( p_intf, "interface", module, true );
119     free(module);
120     if( p_intf->p_module == NULL )
121     {
122         msg_Err( p_intf, "no suitable interface module" );
123         goto error;
124     }
125
126     vlc_mutex_lock( &lock );
127     p_intf->p_next = libvlc_priv( p_libvlc )->p_intf;
128     libvlc_priv( p_libvlc )->p_intf = p_intf;
129     vlc_mutex_unlock( &lock );
130
131     return VLC_SUCCESS;
132
133 error:
134     if( p_intf->p_module )
135         module_unneed( p_intf, p_intf->p_module );
136     config_ChainDestroy( p_intf->p_cfg );
137     vlc_object_release( p_intf );
138     return VLC_EGENERIC;
139 }
140
141
142 /**
143  * Stops and destroys all interfaces
144  * @param p_libvlc the LibVLC instance
145  */
146 void intf_DestroyAll( libvlc_int_t *p_libvlc )
147 {
148     intf_thread_t *p_intf;
149
150     vlc_mutex_lock( &lock );
151     p_intf = libvlc_priv( p_libvlc )->p_intf;
152 #ifndef NDEBUG
153     libvlc_priv( p_libvlc )->p_intf = NULL;
154 #endif
155     vlc_mutex_unlock( &lock );
156
157     /* Cleanup the interfaces */
158     while( p_intf != NULL )
159     {
160         intf_thread_t *p_next = p_intf->p_next;
161
162         module_unneed( p_intf, p_intf->p_module );
163         config_ChainDestroy( p_intf->p_cfg );
164         vlc_object_release( p_intf );
165         p_intf = p_next;
166     }
167 }
168
169 /* Following functions are local */
170
171 static int AddIntfCallback( vlc_object_t *p_this, char const *psz_cmd,
172                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
173 {
174     (void)psz_cmd; (void)oldval; (void)p_data;
175     char* psz_intf;
176
177     /* Try to create the interface */
178     if( asprintf( &psz_intf, "%s,none", newval.psz_string ) == -1 )
179         return VLC_ENOMEM;
180
181     int ret = intf_Create( VLC_OBJECT(p_this->p_libvlc), psz_intf );
182     free( psz_intf );
183     if( ret )
184         msg_Err( p_this, "interface \"%s\" initialization failed",
185                  newval.psz_string );
186     return ret;
187 }