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
9 * Authors: Vincent Seguin <seguin@via.ecp.fr>
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.
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.
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 *****************************************************************************/
28 * This file contains functions related to interface management
32 /*****************************************************************************
34 *****************************************************************************/
35 #include <stdlib.h> /* free(), strtol() */
36 #include <stdio.h> /* FILE */
37 #include <string.h> /* strerror() */
44 #include "vlc_interface.h"
47 # include <Cocoa/Cocoa.h>
50 /*****************************************************************************
52 *****************************************************************************/
53 static void RunInterface( intf_thread_t *p_intf );
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 * );
61 static void Manager( intf_thread_t *p_intf );
62 /*****************************************************************************
63 * VLCApplication interface
64 *****************************************************************************/
65 @interface VLCApplication : NSApplication
67 libvlc_int_t *o_libvlc;
70 - (void)setVLC: (libvlc_int_t *)p_libvlc;
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 *****************************************************************************/
82 * Create the interface, and prepare it for main loop.
83 * You can give some additional options to be used for interface initialization
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
91 intf_thread_t* __intf_Create( vlc_object_t *p_this, const char *psz_module,
92 int i_options, const char *const *ppsz_options )
94 intf_thread_t * p_intf;
97 /* Allocate structure */
98 p_intf = vlc_object_create( p_this, VLC_OBJECT_INTF );
101 msg_Err( p_this, "out of memory" );
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;
110 for( i = 0 ; i< i_options; i++ )
112 var_OptionParse( p_intf, ppsz_options[i] );
115 /* Choose the best module */
116 p_intf->p_module = module_Need( p_intf, "interface", psz_module, VLC_FALSE );
118 if( p_intf->p_module == NULL )
120 msg_Err( p_intf, "no suitable interface module" );
121 vlc_object_destroy( p_intf );
125 /* Initialize structure */
126 p_intf->b_menu = VLC_FALSE;
127 p_intf->b_menu_change = VLC_FALSE;
129 /* Initialize mutexes */
130 vlc_mutex_init( p_intf, &p_intf->change_lock );
132 /* Attach interface to its parent object */
133 vlc_object_attach( p_intf, p_this );
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 *****************************************************************************/
145 * Run the interface thread.
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
152 int intf_RunThread( intf_thread_t *p_intf )
155 NSAutoreleasePool * o_pool;
157 if( p_intf->b_block )
159 /* This is the primary intf */
160 /* Run a manager thread, launch the interface, kill the manager */
161 if( vlc_thread_create( p_intf, "manage", Manager,
162 VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
164 msg_Err( p_intf, "cannot spawn manager thread" );
169 if( p_intf->b_block && strncmp( p_intf->p_libvlc->psz_object_name,
172 o_pool = [[NSAutoreleasePool alloc] init];
173 [VLCApplication sharedApplication];
174 [NSApp setVLC: p_intf->p_libvlc];
177 if( p_intf->b_block &&
178 ( !strncmp( p_intf->p_module->psz_object_name, "macosx" , 6 ) ||
179 !strncmp( p_intf->p_libvlc->psz_object_name, "clivlc", 6 ) ) )
181 /* VLC in normal primary interface mode */
182 RunInterface( p_intf );
183 p_intf->b_die = VLC_TRUE;
187 /* This interface doesn't need to be run */
188 if( !p_intf->pf_run )
191 /* Run the interface in a separate thread */
192 if( !strcmp( p_intf->p_module->psz_object_name, "macosx" ) )
194 msg_Err( p_intf, "You cannot run the MacOS X module as an "
195 "extra interface. Please read the "
196 "README.MacOSX.rtf file.");
199 if( vlc_thread_create( p_intf, "interface", RunInterface,
200 VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
202 msg_Err( p_intf, "cannot spawn interface thread" );
206 if( p_intf->b_block )
208 /* VLC in primary interface mode with a working macosx vout */
210 p_intf->b_die = VLC_TRUE;
214 if( p_intf->b_block )
216 /* If the main interface does not have a run function,
217 * implement a waiting loop ourselves
220 RunInterface( p_intf );
223 while( !intf_ShouldDie( p_intf ) )
224 msleep( INTF_IDLE_SLEEP * 2);
226 p_intf->b_die = VLC_TRUE;
227 /* Do not join the thread... intf_StopThread will do it for us */
231 /* This interface doesn't need to be run */
232 if( !p_intf->pf_run )
235 /* Run the interface in a separate thread */
236 if( vlc_thread_create( p_intf, "interface", RunInterface,
237 VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
239 msg_Err( p_intf, "cannot spawn interface thread" );
249 * Stops the interface thread
251 * This function asks the interface thread to stop
252 * \param p_intf the interface thread
255 void intf_StopThread( intf_thread_t *p_intf )
257 /* Tell the interface to die */
258 if( !p_intf->b_block )
260 p_intf->b_die = VLC_TRUE;
263 vlc_cond_signal( &p_intf->object_wait );
264 vlc_thread_join( p_intf );
270 * \brief Destroy the interface after the main loop endeed.
272 * Destroys interfaces and closes output devices
273 * \param p_intf the interface thread
276 void intf_Destroy( intf_thread_t *p_intf )
278 /* Unlock module if present (a switch may have failed) */
279 if( p_intf->p_module )
281 module_Unneed( p_intf, p_intf->p_module );
284 vlc_mutex_destroy( &p_intf->change_lock );
287 vlc_object_destroy( p_intf );
291 /* Following functions are local */
293 /*****************************************************************************
294 * Manager: helper thread for blocking OS X
295 *****************************************************************************/
297 static void Manager( intf_thread_t *p_intf )
299 while( !p_intf->b_die )
301 msleep( INTF_IDLE_SLEEP );
303 if( p_intf->p_libvlc->b_die )
305 p_intf->b_die = VLC_TRUE;
306 if( strncmp( p_intf->p_libvlc->psz_object_name, "clivlc", 6 ) )
316 /*****************************************************************************
317 * RunInterface: setups necessary data and give control to the interface
318 *****************************************************************************/
319 static void RunInterface( intf_thread_t *p_intf )
321 static const char *ppsz_interfaces[] =
325 "wxwidgets", "wxWidgets",
329 const char **ppsz_parser;
333 vlc_value_t val, text;
336 /* Variable used for interface switching */
337 p_intf->psz_switch_intf = NULL;
338 var_Create( p_intf, "intf-switch", VLC_VAR_STRING |
339 VLC_VAR_HASCHOICE | VLC_VAR_ISCOMMAND );
340 text.psz_string = _("Switch interface");
341 var_Change( p_intf, "intf-switch", VLC_VAR_SETTEXT, &text, NULL );
343 /* Only fill the list with available modules */
344 p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
345 for( ppsz_parser = ppsz_interfaces; *ppsz_parser; ppsz_parser += 2 )
347 for( i = 0; i < p_list->i_count; i++ )
349 module_t *p_module = (module_t *)p_list->p_values[i].p_object;
350 if( !strcmp( p_module->psz_object_name, ppsz_parser[0] ) )
352 val.psz_string = (char *)ppsz_parser[0];
353 text.psz_string = (char *)_(ppsz_parser[1]);
354 var_Change( p_intf, "intf-switch", VLC_VAR_ADDCHOICE,
360 vlc_list_release( p_list );
362 var_AddCallback( p_intf, "intf-switch", SwitchIntfCallback, NULL );
364 /* Variable used for interface spawning */
365 var_Create( p_intf, "intf-add", VLC_VAR_STRING |
366 VLC_VAR_HASCHOICE | VLC_VAR_ISCOMMAND );
367 text.psz_string = _("Add Interface");
368 var_Change( p_intf, "intf-add", VLC_VAR_SETTEXT, &text, NULL );
370 val.psz_string = (char *)"rc"; text.psz_string = (char *)"Console";
371 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
372 val.psz_string = (char *)"telnet";
373 text.psz_string = (char *)_("Telnet Interface");
374 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
375 val.psz_string = (char *)"http";
376 text.psz_string = (char *)_("Web Interface");
377 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
378 val.psz_string = (char *)"logger";
379 text.psz_string = (char *)_("Debug logging");
380 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
381 val.psz_string = (char *)"gestures";
382 text.psz_string = (char *)_("Mouse Gestures");
383 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
385 var_AddCallback( p_intf, "intf-add", AddIntfCallback, NULL );
389 /* Give control to the interface */
390 p_intf->pf_run( p_intf );
392 /* Reset play on start status */
393 p_intf->b_play = VLC_FALSE;
395 if( !p_intf->psz_switch_intf )
400 /* Make sure the old interface is completely uninitialized */
401 module_Unneed( p_intf, p_intf->p_module );
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;
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 );
412 p_intf->p_module = module_Need( p_intf, "interface", psz_intf, 0 );
415 while( p_intf->p_module );
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 )
421 intf_thread_t *p_intf = (intf_thread_t *)p_this;
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;
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 )
434 intf_thread_t *p_intf;
435 char *psz_intf = malloc( strlen(newval.psz_string) + sizeof(",none") );
437 /* Try to create the interface */
438 sprintf( psz_intf, "%s,none", newval.psz_string );
439 p_intf = intf_Create( p_this->p_libvlc, psz_intf, 0, NULL );
443 msg_Err( p_this, "interface \"%s\" initialization failed",
448 /* Try to run the interface */
449 p_intf->b_block = VLC_FALSE;
450 if( intf_RunThread( p_intf ) != VLC_SUCCESS )
452 vlc_object_detach( p_intf );
453 intf_Destroy( p_intf );
461 /*****************************************************************************
462 * VLCApplication implementation
463 *****************************************************************************/
464 @implementation VLCApplication
466 - (void)setVLC: (libvlc_int_t *) p_libvlc
471 - (void)stop: (id)sender
474 NSAutoreleasePool *o_pool;
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];
488 - (void)terminate: (id)sender
490 o_libvlc->b_die = VLC_TRUE;