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