]> git.sesse.net Git - vlc/blob - src/interface/interface.c
Remove interface b_block property.
[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 #include <vlc/vlc.h>
37
38 #include <vlc_aout.h>
39 #include <vlc_vout.h>
40
41 #include "vlc_interface.h"
42 #include "modules/modules.h" // Gruik!
43
44 #ifdef __APPLE__
45 #    include <Cocoa/Cocoa.h>
46 #endif
47
48 /*****************************************************************************
49  * Local prototypes
50  *****************************************************************************/
51 static void RunInterface( intf_thread_t *p_intf );
52
53 static int SwitchIntfCallback( vlc_object_t *, char const *,
54                                vlc_value_t , vlc_value_t , void * );
55 static int AddIntfCallback( vlc_object_t *, char const *,
56                             vlc_value_t , vlc_value_t , void * );
57
58 #ifdef __APPLE__
59 static void Manager( intf_thread_t *p_intf );
60 /*****************************************************************************
61  * VLCApplication interface
62  *****************************************************************************/
63 @interface VLCApplication : NSApplication
64 {
65    libvlc_int_t *o_libvlc;
66 }
67
68 - (void)setVLC: (libvlc_int_t *)p_libvlc;
69
70 @end
71 #endif
72
73 /*****************************************************************************
74  * intf_Create: prepare interface before main loop
75  *****************************************************************************
76  * This function opens output devices and creates specific interfaces. It sends
77  * its own error messages.
78  *****************************************************************************/
79 /**
80  * Create the interface, and prepare it for main loop.
81  * You can give some additional options to be used for interface initialization
82  *
83  * \param p_this the calling vlc_object_t
84  * \param psz_module a preferred interface module
85  * \param i_options number additional options
86  * \param ppsz_options additional option strings
87  * \return a pointer to the created interface thread, NULL on error
88  */
89 intf_thread_t* __intf_Create( vlc_object_t *p_this, const char *psz_module,
90                               int i_options, const char *const *ppsz_options  )
91 {
92     intf_thread_t * p_intf;
93     int i;
94
95     /* Allocate structure */
96     p_intf = vlc_object_create( p_this, VLC_OBJECT_INTF );
97     if( !p_intf )
98     {
99         msg_Err( p_this, "out of memory" );
100         return NULL;
101     }
102     p_intf->pf_request_window = NULL;
103     p_intf->pf_release_window = NULL;
104     p_intf->pf_control_window = NULL;
105     p_intf->b_play = VLC_FALSE;
106     p_intf->b_interaction = VLC_FALSE;
107
108     for( i = 0 ; i< i_options; i++ )
109     {
110         var_OptionParse( p_intf, ppsz_options[i] );
111     }
112
113     /* Choose the best module */
114     p_intf->p_module = module_Need( p_intf, "interface", psz_module, VLC_FALSE );
115
116     if( p_intf->p_module == NULL )
117     {
118         msg_Err( p_intf, "no suitable interface module" );
119         vlc_object_destroy( p_intf );
120         return NULL;
121     }
122
123     /* Initialize structure */
124     p_intf->b_menu        = VLC_FALSE;
125     p_intf->b_menu_change = VLC_FALSE;
126
127     /* Initialize mutexes */
128     vlc_mutex_init( p_intf, &p_intf->change_lock );
129
130     /* Attach interface to its parent object */
131     vlc_object_attach( p_intf, p_this );
132
133     return p_intf;
134 }
135
136 /*****************************************************************************
137  * intf_RunThread: launch the interface thread
138  *****************************************************************************
139  * This function either creates a new thread and runs the interface in it.
140  *****************************************************************************/
141 /**
142  * Starts and runs the interface thread.
143  *
144  * \param p_intf the interface thread
145  * \return VLC_SUCCESS on success, an error number else
146  */
147 int intf_RunThread( intf_thread_t *p_intf )
148 {
149 #ifdef __APPLE__
150     NSAutoreleasePool * o_pool;
151
152     /* If !clivlc, then run as a OS X application */
153     if( p_intf->b_block && strncmp( p_intf->p_libvlc->psz_object_name,
154                                     "clivlc", 6) )
155     {
156         o_pool = [[NSAutoreleasePool alloc] init];
157         [VLCApplication sharedApplication];
158         [NSApp setVLC: p_intf->p_libvlc];
159
160         if( p_intf->pf_run )
161             RunInterface( p_intf );
162         else
163         {
164             [NSApp run];
165             while( !intf_ShouldDie( p_intf ) )
166                 msleep( INTF_IDLE_SLEEP * 2);
167         }
168         vlc_object_kill( p_intf );
169     }
170     else
171 #endif
172
173     /* This interface doesn't need to be run */
174     if( p_intf->pf_run == NULL )
175         return VLC_SUCCESS;
176
177     /* Run the interface in a separate thread */
178     if( !strcmp( p_intf->p_module->psz_object_name, "macosx" ) )
179     {
180         msg_Err( p_intf, "You cannot run the MacOS X module as an "
181                          "extra interface. Please read the "
182                          "README.MacOSX.rtf file.");
183         return VLC_EGENERIC;
184     }
185
186     /* Run the interface in a separate thread */
187     if( vlc_thread_create( p_intf, "interface", RunInterface,
188                            VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
189     {
190         msg_Err( p_intf, "cannot spawn interface thread" );
191         return VLC_EGENERIC;
192     }
193
194     return VLC_SUCCESS;
195 }
196
197 /**
198  * Stops the interface thread
199  *
200  * This function asks the interface thread to stop
201  * \param p_intf the interface thread
202  * \return nothing
203  */
204 void intf_StopThread( intf_thread_t *p_intf )
205 {
206     /* Tell the interface to die */
207     vlc_object_kill( p_intf );
208     if( p_intf->pf_run != NULL )
209     {
210         vlc_cond_signal( &p_intf->object_wait );
211         vlc_thread_join( p_intf );
212     }
213 }
214
215 /**
216  * \brief Destroy the interface after the main loop endeed.
217  *
218  * Destroys interfaces and closes output devices
219  * \param p_intf the interface thread
220  * \return nothing
221  */
222 void intf_Destroy( intf_thread_t *p_intf )
223 {
224     /* Unlock module if present (a switch may have failed) */
225     if( p_intf->p_module )
226     {
227         module_Unneed( p_intf, p_intf->p_module );
228     }
229
230     vlc_mutex_destroy( &p_intf->change_lock );
231
232     /* Free structure */
233     vlc_object_destroy( p_intf );
234 }
235
236
237 /* Following functions are local */
238
239 /*****************************************************************************
240  * Manager: helper thread for blocking OS X
241  *****************************************************************************/
242 #ifdef __APPLE__
243 static void Manager( intf_thread_t *p_intf )
244 {
245     while( !p_intf->b_die )
246     {
247         msleep( INTF_IDLE_SLEEP );
248
249         if( p_intf->p_libvlc->b_die )
250         {
251             p_intf->b_die = VLC_TRUE;
252             if( strncmp( p_intf->p_libvlc->psz_object_name, "clivlc", 6 ) )
253             {
254                 [NSApp stop: NULL];
255             }
256             return;
257         }
258     }
259 }
260 #endif
261
262 /*****************************************************************************
263  * RunInterface: setups necessary data and give control to the interface
264  *****************************************************************************/
265 static void RunInterface( intf_thread_t *p_intf )
266 {
267     static const char *ppsz_interfaces[] =
268     {
269         "skins2", "Skins 2",
270 #ifndef WIN32
271         "wxwidgets", "wxWidgets",
272 #endif
273         NULL, NULL
274     };
275     const char **ppsz_parser;
276
277     vlc_list_t *p_list;
278     int i;
279     vlc_value_t val, text;
280     char *psz_intf;
281
282     /* Variable used for interface switching */
283     p_intf->psz_switch_intf = NULL;
284     var_Create( p_intf, "intf-switch", VLC_VAR_STRING |
285                 VLC_VAR_HASCHOICE | VLC_VAR_ISCOMMAND );
286     text.psz_string = _("Switch interface");
287     var_Change( p_intf, "intf-switch", VLC_VAR_SETTEXT, &text, NULL );
288
289     /* Only fill the list with available modules */
290     p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
291     for( ppsz_parser = ppsz_interfaces; *ppsz_parser; ppsz_parser += 2 )
292     {
293         for( i = 0; i < p_list->i_count; i++ )
294         {
295             module_t *p_module = (module_t *)p_list->p_values[i].p_object;
296             if( !strcmp( p_module->psz_object_name, ppsz_parser[0] ) )
297             {
298                 val.psz_string = (char *)ppsz_parser[0];
299                 text.psz_string = (char *)_(ppsz_parser[1]);
300                 var_Change( p_intf, "intf-switch", VLC_VAR_ADDCHOICE,
301                             &val, &text );
302                 break;
303             }
304         }
305     }
306     vlc_list_release( p_list );
307
308     var_AddCallback( p_intf, "intf-switch", SwitchIntfCallback, NULL );
309
310     /* Variable used for interface spawning */
311     var_Create( p_intf, "intf-add", VLC_VAR_STRING |
312                 VLC_VAR_HASCHOICE | VLC_VAR_ISCOMMAND );
313     text.psz_string = _("Add Interface");
314     var_Change( p_intf, "intf-add", VLC_VAR_SETTEXT, &text, NULL );
315
316     val.psz_string = (char *)"rc"; text.psz_string = (char *)"Console";
317     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
318     val.psz_string = (char *)"telnet";
319     text.psz_string = (char *)_("Telnet Interface");
320     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
321     val.psz_string = (char *)"http";
322     text.psz_string = (char *)_("Web Interface");
323     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
324     val.psz_string = (char *)"logger";
325     text.psz_string = (char *)_("Debug logging");
326     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
327     val.psz_string = (char *)"gestures";
328     text.psz_string = (char *)_("Mouse Gestures");
329     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
330
331     var_AddCallback( p_intf, "intf-add", AddIntfCallback, NULL );
332
333     do
334     {
335         /* Give control to the interface */
336         p_intf->pf_run( p_intf );
337
338         /* Reset play on start status */
339         p_intf->b_play = VLC_FALSE;
340
341         if( !p_intf->psz_switch_intf )
342         {
343             break;
344         }
345
346         /* Make sure the old interface is completely uninitialized */
347         module_Unneed( p_intf, p_intf->p_module );
348
349         /* Provide ability to switch the main interface on the fly */
350         psz_intf = p_intf->psz_switch_intf;
351         p_intf->psz_switch_intf = NULL;
352
353         vlc_mutex_lock( &p_intf->object_lock );
354         p_intf->b_die = VLC_FALSE; /* FIXME */
355         p_intf->b_dead = VLC_FALSE;
356         vlc_mutex_unlock( &p_intf->object_lock );
357
358         p_intf->p_module = module_Need( p_intf, "interface", psz_intf, 0 );
359         free( psz_intf );
360     }
361     while( p_intf->p_module );
362 }
363
364 static int SwitchIntfCallback( vlc_object_t *p_this, char const *psz_cmd,
365                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
366 {
367     intf_thread_t *p_intf = (intf_thread_t *)p_this;
368     (void)psz_cmd; (void)oldval; (void)p_data;
369
370     p_intf->psz_switch_intf =
371         malloc( strlen(newval.psz_string) + sizeof(",none") );
372     sprintf( p_intf->psz_switch_intf, "%s,none", newval.psz_string );
373     vlc_object_kill( p_intf );
374
375     return VLC_SUCCESS;
376 }
377
378 static int AddIntfCallback( vlc_object_t *p_this, char const *psz_cmd,
379                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
380 {
381     intf_thread_t *p_intf;
382     char *psz_intf = malloc( strlen(newval.psz_string) + sizeof(",none") );
383
384     (void)psz_cmd; (void)oldval; (void)p_data;
385
386     /* Try to create the interface */
387     sprintf( psz_intf, "%s,none", newval.psz_string );
388     p_intf = intf_Create( p_this->p_libvlc, psz_intf, 0, NULL );
389     free( psz_intf );
390     if( p_intf == NULL )
391     {
392         msg_Err( p_this, "interface \"%s\" initialization failed",
393                  newval.psz_string );
394         return VLC_EGENERIC;
395     }
396
397     /* Try to run the interface */
398     if( intf_RunThread( p_intf ) != VLC_SUCCESS )
399     {
400         vlc_object_detach( p_intf );
401         intf_Destroy( p_intf );
402         return VLC_EGENERIC;
403     }
404
405     return VLC_SUCCESS;
406 }
407
408 #ifdef __APPLE__
409 /*****************************************************************************
410  * VLCApplication implementation
411  *****************************************************************************/
412 @implementation VLCApplication
413
414 - (void)setVLC: (libvlc_int_t *) p_libvlc
415 {
416     o_libvlc = p_libvlc;
417 }
418
419 - (void)terminate: (id)sender
420 {
421     vlc_object_kill( o_libvlc );
422     [super terminate: sender];
423 }
424
425 @end
426 #endif
427