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