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
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 *****************************************************************************/
41 #include "vlc_interface.h"
42 #include "modules/modules.h" // Gruik!
45 # include <Cocoa/Cocoa.h>
48 /*****************************************************************************
50 *****************************************************************************/
51 static void RunInterface( intf_thread_t *p_intf );
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 * );
59 static void Manager( intf_thread_t *p_intf );
60 /*****************************************************************************
61 * VLCApplication interface
62 *****************************************************************************/
63 @interface VLCApplication : NSApplication
65 libvlc_int_t *o_libvlc;
68 - (void)setVLC: (libvlc_int_t *)p_libvlc;
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 *****************************************************************************/
80 * Create the interface, and prepare it for main loop.
81 * You can give some additional options to be used for interface initialization
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
89 intf_thread_t* __intf_Create( vlc_object_t *p_this, const char *psz_module,
90 int i_options, const char *const *ppsz_options )
92 intf_thread_t * p_intf;
95 /* Allocate structure */
96 p_intf = vlc_object_create( p_this, VLC_OBJECT_INTF );
99 msg_Err( p_this, "out of memory" );
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;
108 for( i = 0 ; i< i_options; i++ )
110 var_OptionParse( p_intf, ppsz_options[i] );
113 /* Choose the best module */
114 p_intf->p_module = module_Need( p_intf, "interface", psz_module, VLC_FALSE );
116 if( p_intf->p_module == NULL )
118 msg_Err( p_intf, "no suitable interface module" );
119 vlc_object_destroy( p_intf );
123 /* Initialize structure */
124 p_intf->b_menu = VLC_FALSE;
125 p_intf->b_menu_change = VLC_FALSE;
127 /* Initialize mutexes */
128 vlc_mutex_init( p_intf, &p_intf->change_lock );
130 /* Attach interface to its parent object */
131 vlc_object_attach( p_intf, p_this );
136 /*****************************************************************************
137 * intf_RunThread: launch the interface thread
138 *****************************************************************************
139 * This function either creates a new thread and runs the interface in it,
140 * or runs the interface in the current thread, depending on b_block.
141 *****************************************************************************/
143 * Run the interface thread.
145 * If b_block is not set, runs the interface in the thread, else,
146 * creates a new thread and runs the interface.
147 * \param p_intf the interface thread
148 * \return VLC_SUCCESS on success, an error number else
150 int intf_RunThread( intf_thread_t *p_intf )
153 NSAutoreleasePool * o_pool;
155 /* If !clivlc, then run as a OS X application */
156 if( p_intf->b_block && strncmp( p_intf->p_libvlc->psz_object_name,
159 o_pool = [[NSAutoreleasePool alloc] init];
160 [VLCApplication sharedApplication];
161 [NSApp setVLC: p_intf->p_libvlc];
164 RunInterface( p_intf );
168 while( !intf_ShouldDie( p_intf ) )
169 msleep( INTF_IDLE_SLEEP * 2);
171 vlc_object_kill( p_intf );
175 if( p_intf->b_block )
177 /* If we are clivlc+macosx, don't run the macosx GUI */
178 if( !strcmp( p_intf->p_module->psz_object_name, "macosx" ) )
180 msg_Err( p_intf, "You cannot run the MacOS X module as an "
181 "interface in clivlc mode. Please read the "
182 "README.MacOSX.rtf file.");
186 /* If the main interface does not have a run function,
187 * implement a waiting loop ourselves
190 RunInterface( p_intf );
193 while( !intf_ShouldDie( p_intf ) )
194 msleep( INTF_IDLE_SLEEP * 2);
196 vlc_object_kill( p_intf );
200 /* This interface doesn't need to be run */
201 if( !p_intf->pf_run )
204 /* Run the interface in a separate thread */
205 if( !strcmp( p_intf->p_module->psz_object_name, "macosx" ) )
207 msg_Err( p_intf, "You cannot run the MacOS X module as an "
208 "extra interface. Please read the "
209 "README.MacOSX.rtf file.");
213 /* Run the interface in a separate thread */
214 if( vlc_thread_create( p_intf, "interface", RunInterface,
215 VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
217 msg_Err( p_intf, "cannot spawn interface thread" );
226 * Stops the interface thread
228 * This function asks the interface thread to stop
229 * \param p_intf the interface thread
232 void intf_StopThread( intf_thread_t *p_intf )
234 /* Tell the interface to die */
235 if( !p_intf->b_block )
237 vlc_object_kill( p_intf );
240 vlc_cond_signal( &p_intf->object_wait );
241 vlc_thread_join( p_intf );
247 * \brief Destroy the interface after the main loop endeed.
249 * Destroys interfaces and closes output devices
250 * \param p_intf the interface thread
253 void intf_Destroy( intf_thread_t *p_intf )
255 /* Unlock module if present (a switch may have failed) */
256 if( p_intf->p_module )
258 module_Unneed( p_intf, p_intf->p_module );
261 vlc_mutex_destroy( &p_intf->change_lock );
264 vlc_object_destroy( p_intf );
268 /* Following functions are local */
270 /*****************************************************************************
271 * Manager: helper thread for blocking OS X
272 *****************************************************************************/
274 static void Manager( intf_thread_t *p_intf )
276 while( !p_intf->b_die )
278 msleep( INTF_IDLE_SLEEP );
280 if( p_intf->p_libvlc->b_die )
282 p_intf->b_die = VLC_TRUE;
283 if( strncmp( p_intf->p_libvlc->psz_object_name, "clivlc", 6 ) )
293 /*****************************************************************************
294 * RunInterface: setups necessary data and give control to the interface
295 *****************************************************************************/
296 static void RunInterface( intf_thread_t *p_intf )
298 static const char *ppsz_interfaces[] =
302 "wxwidgets", "wxWidgets",
306 const char **ppsz_parser;
310 vlc_value_t val, text;
313 /* Variable used for interface switching */
314 p_intf->psz_switch_intf = NULL;
315 var_Create( p_intf, "intf-switch", VLC_VAR_STRING |
316 VLC_VAR_HASCHOICE | VLC_VAR_ISCOMMAND );
317 text.psz_string = _("Switch interface");
318 var_Change( p_intf, "intf-switch", VLC_VAR_SETTEXT, &text, NULL );
320 /* Only fill the list with available modules */
321 p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
322 for( ppsz_parser = ppsz_interfaces; *ppsz_parser; ppsz_parser += 2 )
324 for( i = 0; i < p_list->i_count; i++ )
326 module_t *p_module = (module_t *)p_list->p_values[i].p_object;
327 if( !strcmp( p_module->psz_object_name, ppsz_parser[0] ) )
329 val.psz_string = (char *)ppsz_parser[0];
330 text.psz_string = (char *)_(ppsz_parser[1]);
331 var_Change( p_intf, "intf-switch", VLC_VAR_ADDCHOICE,
337 vlc_list_release( p_list );
339 var_AddCallback( p_intf, "intf-switch", SwitchIntfCallback, NULL );
341 /* Variable used for interface spawning */
342 var_Create( p_intf, "intf-add", VLC_VAR_STRING |
343 VLC_VAR_HASCHOICE | VLC_VAR_ISCOMMAND );
344 text.psz_string = _("Add Interface");
345 var_Change( p_intf, "intf-add", VLC_VAR_SETTEXT, &text, NULL );
347 val.psz_string = (char *)"rc"; text.psz_string = (char *)"Console";
348 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
349 val.psz_string = (char *)"telnet";
350 text.psz_string = (char *)_("Telnet Interface");
351 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
352 val.psz_string = (char *)"http";
353 text.psz_string = (char *)_("Web Interface");
354 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
355 val.psz_string = (char *)"logger";
356 text.psz_string = (char *)_("Debug logging");
357 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
358 val.psz_string = (char *)"gestures";
359 text.psz_string = (char *)_("Mouse Gestures");
360 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
362 var_AddCallback( p_intf, "intf-add", AddIntfCallback, NULL );
366 /* Give control to the interface */
367 p_intf->pf_run( p_intf );
369 /* Reset play on start status */
370 p_intf->b_play = VLC_FALSE;
372 if( !p_intf->psz_switch_intf )
377 /* Make sure the old interface is completely uninitialized */
378 module_Unneed( p_intf, p_intf->p_module );
380 /* Provide ability to switch the main interface on the fly */
381 psz_intf = p_intf->psz_switch_intf;
382 p_intf->psz_switch_intf = NULL;
384 vlc_mutex_lock( &p_intf->object_lock );
385 p_intf->b_die = VLC_FALSE; /* FIXME */
386 p_intf->b_dead = VLC_FALSE;
387 vlc_mutex_unlock( &p_intf->object_lock );
389 p_intf->p_module = module_Need( p_intf, "interface", psz_intf, 0 );
392 while( p_intf->p_module );
395 static int SwitchIntfCallback( vlc_object_t *p_this, char const *psz_cmd,
396 vlc_value_t oldval, vlc_value_t newval, void *p_data )
398 intf_thread_t *p_intf = (intf_thread_t *)p_this;
399 (void)psz_cmd; (void)oldval; (void)p_data;
401 p_intf->psz_switch_intf =
402 malloc( strlen(newval.psz_string) + sizeof(",none") );
403 sprintf( p_intf->psz_switch_intf, "%s,none", newval.psz_string );
404 vlc_object_kill( p_intf );
409 static int AddIntfCallback( vlc_object_t *p_this, char const *psz_cmd,
410 vlc_value_t oldval, vlc_value_t newval, void *p_data )
412 intf_thread_t *p_intf;
413 char *psz_intf = malloc( strlen(newval.psz_string) + sizeof(",none") );
415 (void)psz_cmd; (void)oldval; (void)p_data;
417 /* Try to create the interface */
418 sprintf( psz_intf, "%s,none", newval.psz_string );
419 p_intf = intf_Create( p_this->p_libvlc, psz_intf, 0, NULL );
423 msg_Err( p_this, "interface \"%s\" initialization failed",
428 /* Try to run the interface */
429 p_intf->b_block = VLC_FALSE;
430 if( intf_RunThread( p_intf ) != VLC_SUCCESS )
432 vlc_object_detach( p_intf );
433 intf_Destroy( p_intf );
441 /*****************************************************************************
442 * VLCApplication implementation
443 *****************************************************************************/
444 @implementation VLCApplication
446 - (void)setVLC: (libvlc_int_t *) p_libvlc
451 - (void)terminate: (id)sender
453 vlc_object_kill( o_libvlc );
454 [super terminate: sender];