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 *****************************************************************************/
142 * Starts and runs the interface thread.
144 * \param p_intf the interface thread
145 * \return VLC_SUCCESS on success, an error number else
147 int intf_RunThread( intf_thread_t *p_intf )
150 NSAutoreleasePool * o_pool;
152 /* If !clivlc, then run as a OS X application */
153 if( p_intf->b_block && strncmp( p_intf->p_libvlc->psz_object_name,
156 o_pool = [[NSAutoreleasePool alloc] init];
157 [VLCApplication sharedApplication];
158 [NSApp setVLC: p_intf->p_libvlc];
161 RunInterface( p_intf );
165 while( !intf_ShouldDie( p_intf ) )
166 msleep( INTF_IDLE_SLEEP * 2);
168 vlc_object_kill( p_intf );
173 /* This interface doesn't need to be run */
174 if( p_intf->pf_run == NULL )
177 /* Run the interface in a separate thread */
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 "extra interface. Please read the "
182 "README.MacOSX.rtf file.");
186 /* Run the interface in a separate thread */
187 if( vlc_thread_create( p_intf, "interface", RunInterface,
188 VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
190 msg_Err( p_intf, "cannot spawn interface thread" );
198 * Stops the interface thread
200 * This function asks the interface thread to stop
201 * \param p_intf the interface thread
204 void intf_StopThread( intf_thread_t *p_intf )
206 /* Tell the interface to die */
207 vlc_object_kill( p_intf );
208 if( p_intf->pf_run != NULL )
210 vlc_cond_signal( &p_intf->object_wait );
211 vlc_thread_join( p_intf );
216 * \brief Destroy the interface after the main loop endeed.
218 * Destroys interfaces and closes output devices
219 * \param p_intf the interface thread
222 void intf_Destroy( intf_thread_t *p_intf )
224 /* Unlock module if present (a switch may have failed) */
225 if( p_intf->p_module )
227 module_Unneed( p_intf, p_intf->p_module );
230 vlc_mutex_destroy( &p_intf->change_lock );
233 vlc_object_destroy( p_intf );
237 /* Following functions are local */
239 /*****************************************************************************
240 * Manager: helper thread for blocking OS X
241 *****************************************************************************/
243 static void Manager( intf_thread_t *p_intf )
245 while( !p_intf->b_die )
247 msleep( INTF_IDLE_SLEEP );
249 if( p_intf->p_libvlc->b_die )
251 p_intf->b_die = VLC_TRUE;
252 if( strncmp( p_intf->p_libvlc->psz_object_name, "clivlc", 6 ) )
262 /*****************************************************************************
263 * RunInterface: setups necessary data and give control to the interface
264 *****************************************************************************/
265 static void RunInterface( intf_thread_t *p_intf )
267 static const char *ppsz_interfaces[] =
271 "wxwidgets", "wxWidgets",
275 const char **ppsz_parser;
279 vlc_value_t val, text;
282 /* Variable used for interface switching */
283 p_intf->psz_switch_intf = NULL;
284 var_Create( p_intf, "intf-switch", VLC_VAR_STRING |
285 VLC_VAR_HASCHOICE | VLC_VAR_ISCOMMAND );
286 text.psz_string = _("Switch interface");
287 var_Change( p_intf, "intf-switch", VLC_VAR_SETTEXT, &text, NULL );
289 /* Only fill the list with available modules */
290 p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
291 for( ppsz_parser = ppsz_interfaces; *ppsz_parser; ppsz_parser += 2 )
293 for( i = 0; i < p_list->i_count; i++ )
295 module_t *p_module = (module_t *)p_list->p_values[i].p_object;
296 if( !strcmp( p_module->psz_object_name, ppsz_parser[0] ) )
298 val.psz_string = (char *)ppsz_parser[0];
299 text.psz_string = (char *)_(ppsz_parser[1]);
300 var_Change( p_intf, "intf-switch", VLC_VAR_ADDCHOICE,
306 vlc_list_release( p_list );
308 var_AddCallback( p_intf, "intf-switch", SwitchIntfCallback, NULL );
310 /* Variable used for interface spawning */
311 var_Create( p_intf, "intf-add", VLC_VAR_STRING |
312 VLC_VAR_HASCHOICE | VLC_VAR_ISCOMMAND );
313 text.psz_string = _("Add Interface");
314 var_Change( p_intf, "intf-add", VLC_VAR_SETTEXT, &text, NULL );
316 val.psz_string = (char *)"rc"; text.psz_string = (char *)"Console";
317 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
318 val.psz_string = (char *)"telnet";
319 text.psz_string = (char *)_("Telnet Interface");
320 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
321 val.psz_string = (char *)"http";
322 text.psz_string = (char *)_("Web Interface");
323 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
324 val.psz_string = (char *)"logger";
325 text.psz_string = (char *)_("Debug logging");
326 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
327 val.psz_string = (char *)"gestures";
328 text.psz_string = (char *)_("Mouse Gestures");
329 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
331 var_AddCallback( p_intf, "intf-add", AddIntfCallback, NULL );
335 /* Give control to the interface */
336 p_intf->pf_run( p_intf );
338 /* Reset play on start status */
339 p_intf->b_play = VLC_FALSE;
341 if( !p_intf->psz_switch_intf )
346 /* Make sure the old interface is completely uninitialized */
347 module_Unneed( p_intf, p_intf->p_module );
349 /* Provide ability to switch the main interface on the fly */
350 psz_intf = p_intf->psz_switch_intf;
351 p_intf->psz_switch_intf = NULL;
353 vlc_mutex_lock( &p_intf->object_lock );
354 p_intf->b_die = VLC_FALSE; /* FIXME */
355 p_intf->b_dead = VLC_FALSE;
356 vlc_mutex_unlock( &p_intf->object_lock );
358 p_intf->p_module = module_Need( p_intf, "interface", psz_intf, 0 );
361 while( p_intf->p_module );
364 static int SwitchIntfCallback( vlc_object_t *p_this, char const *psz_cmd,
365 vlc_value_t oldval, vlc_value_t newval, void *p_data )
367 intf_thread_t *p_intf = (intf_thread_t *)p_this;
368 (void)psz_cmd; (void)oldval; (void)p_data;
370 p_intf->psz_switch_intf =
371 malloc( strlen(newval.psz_string) + sizeof(",none") );
372 sprintf( p_intf->psz_switch_intf, "%s,none", newval.psz_string );
373 vlc_object_kill( p_intf );
378 static int AddIntfCallback( vlc_object_t *p_this, char const *psz_cmd,
379 vlc_value_t oldval, vlc_value_t newval, void *p_data )
381 intf_thread_t *p_intf;
382 char *psz_intf = malloc( strlen(newval.psz_string) + sizeof(",none") );
384 (void)psz_cmd; (void)oldval; (void)p_data;
386 /* Try to create the interface */
387 sprintf( psz_intf, "%s,none", newval.psz_string );
388 p_intf = intf_Create( p_this->p_libvlc, psz_intf, 0, NULL );
392 msg_Err( p_this, "interface \"%s\" initialization failed",
397 /* Try to run the interface */
398 if( intf_RunThread( p_intf ) != VLC_SUCCESS )
400 vlc_object_detach( p_intf );
401 intf_Destroy( p_intf );
409 /*****************************************************************************
410 * VLCApplication implementation
411 *****************************************************************************/
412 @implementation VLCApplication
414 - (void)setVLC: (libvlc_int_t *) p_libvlc
419 - (void)terminate: (id)sender
421 vlc_object_kill( o_libvlc );
422 [super terminate: sender];