]> git.sesse.net Git - vlc/blob - src/interface/interface.c
Clean up coments.
[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 the VideoLAN team
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
12  * it under the terms of the GNU General Public License as published by
13  * the Free Software Foundation; either version 2 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 General Public License for more details.
20  *
21  * You should have received a copy of the GNU General Public License
22  * along with this program; if not, write to the Free Software
23  * Foundation, 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 <vlc_common.h>
41
42 #include <vlc_aout.h>
43 #include <vlc_vout.h>
44
45 #include "vlc_interface.h"
46 #include "libvlc.h"
47
48 /*****************************************************************************
49  * Local prototypes
50  *****************************************************************************/
51 static void* RunInterface( vlc_object_t *p_this );
52 #if defined( __APPLE__ ) || defined( WIN32 )
53 static void * MonitorLibVLCDeath( vlc_object_t *p_this );
54 #endif
55 static int AddIntfCallback( vlc_object_t *, char const *,
56                             vlc_value_t , vlc_value_t , void * );
57
58 /**
59  * Destroy the interface after the main loop endeed.
60  *
61  * @param p_obj: the interface thread
62  */
63 static void intf_Destroy( vlc_object_t *obj )
64 {
65     intf_thread_t *p_intf = (intf_thread_t *)obj;
66
67     /* Unlock module if present (a switch may have failed) */
68     if( p_intf->p_module )
69         module_unneed( p_intf, p_intf->p_module );
70
71     free( p_intf->psz_intf );
72     vlc_mutex_destroy( &p_intf->change_lock );
73 }
74
75
76 /**
77  * Create the interface, and prepare it for main loop. It opens ouput device
78  * and creates specific interfaces. Sends its own error messages.
79  *
80  * @param p_this the calling vlc_object_t
81  * @param psz_module a preferred interface module
82  * @return a pointer to the created interface thread, NULL on error
83  */
84 intf_thread_t* __intf_Create( vlc_object_t *p_this, const char *psz_module )
85 {
86     intf_thread_t * p_intf;
87
88     /* Allocate structure */
89     p_intf = vlc_object_create( p_this, VLC_OBJECT_INTF );
90     if( !p_intf )
91         return NULL;
92     p_intf->b_interaction = false;
93 #if defined( __APPLE__ ) || defined( WIN32 )
94     p_intf->b_should_run_on_first_thread = false;
95 #endif
96
97     /* Choose the best module */
98     p_intf->psz_intf = strdup( psz_module );
99     p_intf->p_module = module_need( p_intf, "interface", psz_module, true );
100
101     if( p_intf->p_module == NULL )
102     {
103         msg_Err( p_intf, "no suitable interface module" );
104         free( p_intf->psz_intf );
105         vlc_object_release( p_intf );
106         return NULL;
107     }
108
109     /* Initialize structure */
110     p_intf->b_menu        = false;
111     p_intf->b_menu_change = false;
112
113     /* Initialize mutexes */
114     vlc_mutex_init( &p_intf->change_lock );
115
116     /* Attach interface to its parent object */
117     vlc_object_attach( p_intf, p_this );
118     vlc_object_set_destructor( p_intf, intf_Destroy );
119
120     return p_intf;
121 }
122
123
124 /**
125  * Starts and runs the interface thread.
126  *
127  * @param p_intf the interface thread
128  * @return VLC_SUCCESS on success, an error number else
129  */
130 int intf_RunThread( intf_thread_t *p_intf )
131 {
132 #if defined( __APPLE__ ) || defined( WIN32 )
133     /* Hack to get Mac OS X Cocoa runtime running
134      * (it needs access to the main thread) */
135     if( p_intf->b_should_run_on_first_thread )
136     {
137         if( vlc_thread_create( p_intf, "interface", MonitorLibVLCDeath,
138                                VLC_THREAD_PRIORITY_LOW, false ) )
139         {
140             msg_Err( p_intf, "cannot spawn libvlc death monitoring thread" );
141             return VLC_EGENERIC;
142         }
143         RunInterface( VLC_OBJECT(p_intf) );
144
145         /* Make sure our MonitorLibVLCDeath thread exit */
146         vlc_object_kill( p_intf );
147         /* It is monitoring libvlc, not the p_intf */
148         vlc_object_signal( p_intf->p_libvlc );
149         vlc_thread_join( p_intf );
150
151         vlc_object_detach( p_intf );
152         vlc_object_release( p_intf );
153         return VLC_SUCCESS;
154     }
155 #endif
156     /* Run the interface in a separate thread */
157     if( vlc_thread_create( p_intf, "interface", RunInterface,
158                            VLC_THREAD_PRIORITY_LOW, false ) )
159     {
160         msg_Err( p_intf, "cannot spawn interface thread" );
161         return VLC_EGENERIC;
162     }
163
164     return VLC_SUCCESS;
165 }
166
167
168 /**
169  * Stops the interface thread
170  *
171  * This function asks the interface thread to stop
172  * @param p_intf the interface thread
173  */
174 void intf_StopThread( intf_thread_t *p_intf )
175 {
176     /* Tell the interface to die */
177     vlc_object_kill( p_intf );
178     vlc_thread_join( p_intf );
179 }
180
181
182
183 /* Following functions are local */
184
185 /**
186  * RunInterface: setups necessary data and give control to the interface
187  *
188  * @param p_this: interface object
189  */
190 static void* RunInterface( vlc_object_t *p_this )
191 {
192     intf_thread_t *p_intf = (intf_thread_t *)p_this;
193     vlc_value_t val, text;
194     int canc = vlc_savecancel ();
195
196     /* Variable used for interface spawning */
197     var_Create( p_intf, "intf-add", VLC_VAR_STRING |
198                 VLC_VAR_HASCHOICE | VLC_VAR_ISCOMMAND );
199     text.psz_string = _("Add Interface");
200     var_Change( p_intf, "intf-add", VLC_VAR_SETTEXT, &text, NULL );
201
202     val.psz_string = (char *)"rc";
203     text.psz_string = (char *)_("Console");
204     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
205     val.psz_string = (char *)"telnet";
206     text.psz_string = (char *)_("Telnet Interface");
207     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
208     val.psz_string = (char *)"http";
209     text.psz_string = (char *)_("Web Interface");
210     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
211     val.psz_string = (char *)"logger";
212     text.psz_string = (char *)_("Debug logging");
213     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
214     val.psz_string = (char *)"gestures";
215     text.psz_string = (char *)_("Mouse Gestures");
216     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
217
218     var_AddCallback( p_intf, "intf-add", AddIntfCallback, NULL );
219     vlc_restorecancel (canc);
220
221     /* Give control to the interface */
222     if( p_intf->pf_run )
223         p_intf->pf_run( p_intf );
224
225     return NULL;
226 }
227
228 #if defined( __APPLE__ ) || defined( WIN32 )
229 /**
230  * MonitorLibVLCDeath: Used when b_should_run_on_first_thread is set.
231  *
232  * @param p_this: the interface object
233  */
234 static void * MonitorLibVLCDeath( vlc_object_t * p_this )
235 {
236     intf_thread_t *p_intf = (intf_thread_t *)p_this;
237     libvlc_int_t * p_libvlc = p_intf->p_libvlc;
238     int canc = vlc_savecancel ();
239
240     vlc_object_lock( p_libvlc );
241     while(vlc_object_alive( p_libvlc ) )
242     {
243         if(p_intf->b_die)
244         {
245             vlc_object_unlock( p_libvlc );
246             return NULL;
247         }
248         vlc_object_wait( p_libvlc );
249     }
250     vlc_object_unlock( p_libvlc );
251
252     /* Someone killed libvlc */
253
254     /* Make sure we kill all interface objects, especially
255      * those that are blocking libvlc (running on main thread) */
256     vlc_list_t * p_list = vlc_list_find( p_libvlc, VLC_OBJECT_INTF, FIND_CHILD );
257     for( int i = 0; i < p_list->i_count; i++ )
258     {
259         vlc_object_t * p_intf = p_list->p_values[i].p_object;
260         vlc_object_kill( p_intf );
261     }
262     vlc_list_release( p_list );
263     vlc_restorecancel (canc);
264     return NULL;
265 }
266 #endif
267
268 static int AddIntfCallback( vlc_object_t *p_this, char const *psz_cmd,
269                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
270 {
271     (void)psz_cmd; (void)oldval; (void)p_data;
272     intf_thread_t *p_intf;
273     char* psz_intf;
274
275     /* Try to create the interface */
276     if( asprintf( &psz_intf, "%s,none", newval.psz_string ) == -1 )
277         return VLC_ENOMEM;
278
279     p_intf = intf_Create( p_this->p_libvlc, psz_intf );
280     free( psz_intf );
281     if( p_intf == NULL )
282     {
283         msg_Err( p_this, "interface \"%s\" initialization failed",
284                  newval.psz_string );
285         return VLC_EGENERIC;
286     }
287
288     /* Try to run the interface */
289     if( intf_RunThread( p_intf ) != VLC_SUCCESS )
290     {
291         vlc_object_detach( p_intf );
292         vlc_object_release( p_intf );
293         return VLC_EGENERIC;
294     }
295
296     return VLC_SUCCESS;
297 }
298