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