]> git.sesse.net Git - vlc/blob - src/interface/interface.c
Remove non-sensical condition
[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 /*****************************************************************************
51  * Local prototypes
52  *****************************************************************************/
53 static void* RunInterface( void * );
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 #if defined( __APPLE__ )
108     p_intf->b_should_run_on_first_thread = false;
109 #endif
110
111     /* Choose the best module */
112     p_intf->p_cfg = NULL;
113     char *psz_parser = *chain == '$'
114                      ? var_CreateGetString(p_intf, chain+1)
115                      : strdup( chain );
116     char *module;
117     char *psz_tmp = config_ChainCreate( &module, &p_intf->p_cfg,
118                                         psz_parser );
119     free( psz_tmp );
120     free( psz_parser );
121     p_intf->p_module = module_need( p_intf, "interface", module, true );
122     free(module);
123     if( p_intf->p_module == NULL )
124     {
125         msg_Err( p_intf, "no suitable interface module" );
126         goto error;
127     }
128
129 #if defined( __APPLE__ )
130     /* Hack to get Mac OS X Cocoa runtime running
131      * (it needs access to the main thread) */
132     if( p_intf->b_should_run_on_first_thread )
133     {
134         libvlc_SetExitHandler( p_libvlc, vlc_object_kill, p_intf );
135         assert( p_intf->pf_run );
136         p_intf->pf_run( p_intf );
137         p_intf->pf_run = NULL;
138     }
139 #endif
140     /* Run the interface in a separate thread */
141     if( p_intf->pf_run
142      && vlc_clone( &p_intf->thread,
143                    RunInterface, p_intf, VLC_THREAD_PRIORITY_LOW ) )
144     {
145         msg_Err( p_intf, "cannot spawn interface thread" );
146         goto error;
147     }
148
149     vlc_mutex_lock( &lock );
150     p_intf->p_next = libvlc_priv( p_libvlc )->p_intf;
151     libvlc_priv( p_libvlc )->p_intf = p_intf;
152     vlc_mutex_unlock( &lock );
153
154     return VLC_SUCCESS;
155
156 error:
157     if( p_intf->p_module )
158         module_unneed( p_intf, p_intf->p_module );
159     config_ChainDestroy( p_intf->p_cfg );
160     vlc_object_release( p_intf );
161     return VLC_EGENERIC;
162 }
163
164
165 /**
166  * Stops and destroys all interfaces
167  * @param p_libvlc the LibVLC instance
168  */
169 void intf_DestroyAll( libvlc_int_t *p_libvlc )
170 {
171     intf_thread_t *p_first;
172
173     vlc_mutex_lock( &lock );
174     p_first = libvlc_priv( p_libvlc )->p_intf;
175 #ifndef NDEBUG
176     libvlc_priv( p_libvlc )->p_intf = NULL;
177 #endif
178     vlc_mutex_unlock( &lock );
179
180     /* Tell the interfaces to die */
181     for( intf_thread_t *p_intf = p_first; p_intf; p_intf = p_intf->p_next )
182         vlc_object_kill( p_intf );
183
184     /* Cleanup the interfaces */
185     for( intf_thread_t *p_intf = p_first; p_intf != NULL; )
186     {
187         intf_thread_t *p_next = p_intf->p_next;
188
189         if( p_intf->pf_run )
190         {
191             vlc_cancel( p_intf->thread );
192             vlc_join( p_intf->thread, NULL );
193         }
194         module_unneed( p_intf, p_intf->p_module );
195         config_ChainDestroy( p_intf->p_cfg );
196         vlc_object_release( p_intf );
197
198         p_intf = p_next;
199     }
200 }
201
202 /* Following functions are local */
203
204 /**
205  * RunInterface: setups necessary data and give control to the interface
206  *
207  * @param p_this: interface object
208  */
209 static void* RunInterface( void *p_this )
210 {
211     intf_thread_t *p_intf = p_this;
212
213     p_intf->pf_run( p_intf );
214     return NULL;
215 }
216
217 static int AddIntfCallback( vlc_object_t *p_this, char const *psz_cmd,
218                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
219 {
220     (void)psz_cmd; (void)oldval; (void)p_data;
221     char* psz_intf;
222
223     /* Try to create the interface */
224     if( asprintf( &psz_intf, "%s,none", newval.psz_string ) == -1 )
225         return VLC_ENOMEM;
226
227     int ret = intf_Create( VLC_OBJECT(p_this->p_libvlc), psz_intf );
228     free( psz_intf );
229     if( ret )
230         msg_Err( p_this, "interface \"%s\" initialization failed",
231                  newval.psz_string );
232     return ret;
233 }