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 *****************************************************************************/
35 #include <stdlib.h> /* free(), strtol() */
36 #include <stdio.h> /* FILE */
37 #include <string.h> /* strerror() */
44 #include "vlc_interface.h"
45 #include "modules/modules.h" // Gruik!
48 # include <Cocoa/Cocoa.h>
51 /*****************************************************************************
53 *****************************************************************************/
54 static void RunInterface( intf_thread_t *p_intf );
56 static int SwitchIntfCallback( vlc_object_t *, char const *,
57 vlc_value_t , vlc_value_t , void * );
58 static int AddIntfCallback( vlc_object_t *, char const *,
59 vlc_value_t , vlc_value_t , void * );
62 static void Manager( intf_thread_t *p_intf );
63 /*****************************************************************************
64 * VLCApplication interface
65 *****************************************************************************/
66 @interface VLCApplication : NSApplication
68 libvlc_int_t *o_libvlc;
71 - (void)setVLC: (libvlc_int_t *)p_libvlc;
76 /*****************************************************************************
77 * intf_Create: prepare interface before main loop
78 *****************************************************************************
79 * This function opens output devices and creates specific interfaces. It sends
80 * its own error messages.
81 *****************************************************************************/
83 * Create the interface, and prepare it for main loop.
84 * You can give some additional options to be used for interface initialization
86 * \param p_this the calling vlc_object_t
87 * \param psz_module a preferred interface module
88 * \param i_options number additional options
89 * \param ppsz_options additional option strings
90 * \return a pointer to the created interface thread, NULL on error
92 intf_thread_t* __intf_Create( vlc_object_t *p_this, const char *psz_module,
93 int i_options, const char *const *ppsz_options )
95 intf_thread_t * p_intf;
98 /* Allocate structure */
99 p_intf = vlc_object_create( p_this, VLC_OBJECT_INTF );
102 msg_Err( p_this, "out of memory" );
105 p_intf->pf_request_window = NULL;
106 p_intf->pf_release_window = NULL;
107 p_intf->pf_control_window = NULL;
108 p_intf->b_play = VLC_FALSE;
109 p_intf->b_interaction = VLC_FALSE;
111 for( i = 0 ; i< i_options; i++ )
113 var_OptionParse( p_intf, ppsz_options[i] );
116 /* Choose the best module */
117 p_intf->p_module = module_Need( p_intf, "interface", psz_module, VLC_FALSE );
119 if( p_intf->p_module == NULL )
121 msg_Err( p_intf, "no suitable interface module" );
122 vlc_object_destroy( p_intf );
126 /* Initialize structure */
127 p_intf->b_menu = VLC_FALSE;
128 p_intf->b_menu_change = VLC_FALSE;
130 /* Initialize mutexes */
131 vlc_mutex_init( p_intf, &p_intf->change_lock );
133 /* Attach interface to its parent object */
134 vlc_object_attach( p_intf, p_this );
139 /*****************************************************************************
140 * intf_RunThread: launch the interface thread
141 *****************************************************************************
142 * This function either creates a new thread and runs the interface in it,
143 * or runs the interface in the current thread, depending on b_block.
144 *****************************************************************************/
146 * Run the interface thread.
148 * If b_block is not set, runs the interface in the thread, else,
149 * creates a new thread and runs the interface.
150 * \param p_intf the interface thread
151 * \return VLC_SUCCESS on success, an error number else
153 int intf_RunThread( intf_thread_t *p_intf )
156 NSAutoreleasePool * o_pool;
158 /* If !clivlc, then run as a OS X application */
159 if( p_intf->b_block && strncmp( p_intf->p_libvlc->psz_object_name,
162 o_pool = [[NSAutoreleasePool alloc] init];
163 [VLCApplication sharedApplication];
164 [NSApp setVLC: p_intf->p_libvlc];
167 RunInterface( p_intf );
171 while( !intf_ShouldDie( p_intf ) )
172 msleep( INTF_IDLE_SLEEP * 2);
174 vlc_object_kill( p_intf );
178 if( p_intf->b_block )
180 /* If we are clivlc+macosx, don't run the macosx GUI */
181 if( !strcmp( p_intf->p_module->psz_object_name, "macosx" ) )
183 msg_Err( p_intf, "You cannot run the MacOS X module as an "
184 "interface in clivlc mode. Please read the "
185 "README.MacOSX.rtf file.");
189 /* If the main interface does not have a run function,
190 * implement a waiting loop ourselves
193 RunInterface( p_intf );
196 while( !intf_ShouldDie( p_intf ) )
197 msleep( INTF_IDLE_SLEEP * 2);
199 vlc_object_kill( p_intf );
203 /* This interface doesn't need to be run */
204 if( !p_intf->pf_run )
207 /* Run the interface in a separate thread */
208 if( !strcmp( p_intf->p_module->psz_object_name, "macosx" ) )
210 msg_Err( p_intf, "You cannot run the MacOS X module as an "
211 "extra interface. Please read the "
212 "README.MacOSX.rtf file.");
216 /* Run the interface in a separate thread */
217 if( vlc_thread_create( p_intf, "interface", RunInterface,
218 VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
220 msg_Err( p_intf, "cannot spawn interface thread" );
229 * Stops the interface thread
231 * This function asks the interface thread to stop
232 * \param p_intf the interface thread
235 void intf_StopThread( intf_thread_t *p_intf )
237 /* Tell the interface to die */
238 if( !p_intf->b_block )
240 vlc_object_kill( p_intf );
243 vlc_cond_signal( &p_intf->object_wait );
244 vlc_thread_join( p_intf );
250 * \brief Destroy the interface after the main loop endeed.
252 * Destroys interfaces and closes output devices
253 * \param p_intf the interface thread
256 void intf_Destroy( intf_thread_t *p_intf )
258 /* Unlock module if present (a switch may have failed) */
259 if( p_intf->p_module )
261 module_Unneed( p_intf, p_intf->p_module );
264 vlc_mutex_destroy( &p_intf->change_lock );
267 vlc_object_destroy( p_intf );
271 /* Following functions are local */
273 /*****************************************************************************
274 * Manager: helper thread for blocking OS X
275 *****************************************************************************/
277 static void Manager( intf_thread_t *p_intf )
279 while( !p_intf->b_die )
281 msleep( INTF_IDLE_SLEEP );
283 if( p_intf->p_libvlc->b_die )
285 p_intf->b_die = VLC_TRUE;
286 if( strncmp( p_intf->p_libvlc->psz_object_name, "clivlc", 6 ) )
296 /*****************************************************************************
297 * RunInterface: setups necessary data and give control to the interface
298 *****************************************************************************/
299 static void RunInterface( intf_thread_t *p_intf )
301 static const char *ppsz_interfaces[] =
305 "wxwidgets", "wxWidgets",
309 const char **ppsz_parser;
313 vlc_value_t val, text;
316 /* Variable used for interface switching */
317 p_intf->psz_switch_intf = NULL;
318 var_Create( p_intf, "intf-switch", VLC_VAR_STRING |
319 VLC_VAR_HASCHOICE | VLC_VAR_ISCOMMAND );
320 text.psz_string = _("Switch interface");
321 var_Change( p_intf, "intf-switch", VLC_VAR_SETTEXT, &text, NULL );
323 /* Only fill the list with available modules */
324 p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
325 for( ppsz_parser = ppsz_interfaces; *ppsz_parser; ppsz_parser += 2 )
327 for( i = 0; i < p_list->i_count; i++ )
329 module_t *p_module = (module_t *)p_list->p_values[i].p_object;
330 if( !strcmp( p_module->psz_object_name, ppsz_parser[0] ) )
332 val.psz_string = (char *)ppsz_parser[0];
333 text.psz_string = (char *)_(ppsz_parser[1]);
334 var_Change( p_intf, "intf-switch", VLC_VAR_ADDCHOICE,
340 vlc_list_release( p_list );
342 var_AddCallback( p_intf, "intf-switch", SwitchIntfCallback, NULL );
344 /* Variable used for interface spawning */
345 var_Create( p_intf, "intf-add", VLC_VAR_STRING |
346 VLC_VAR_HASCHOICE | VLC_VAR_ISCOMMAND );
347 text.psz_string = _("Add Interface");
348 var_Change( p_intf, "intf-add", VLC_VAR_SETTEXT, &text, NULL );
350 val.psz_string = (char *)"rc"; text.psz_string = (char *)"Console";
351 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
352 val.psz_string = (char *)"telnet";
353 text.psz_string = (char *)_("Telnet Interface");
354 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
355 val.psz_string = (char *)"http";
356 text.psz_string = (char *)_("Web Interface");
357 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
358 val.psz_string = (char *)"logger";
359 text.psz_string = (char *)_("Debug logging");
360 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
361 val.psz_string = (char *)"gestures";
362 text.psz_string = (char *)_("Mouse Gestures");
363 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
365 var_AddCallback( p_intf, "intf-add", AddIntfCallback, NULL );
369 /* Give control to the interface */
370 p_intf->pf_run( p_intf );
372 /* Reset play on start status */
373 p_intf->b_play = VLC_FALSE;
375 if( !p_intf->psz_switch_intf )
380 /* Make sure the old interface is completely uninitialized */
381 module_Unneed( p_intf, p_intf->p_module );
383 /* Provide ability to switch the main interface on the fly */
384 psz_intf = p_intf->psz_switch_intf;
385 p_intf->psz_switch_intf = NULL;
387 vlc_mutex_lock( &p_intf->object_lock );
388 p_intf->b_die = VLC_FALSE; /* FIXME */
389 p_intf->b_dead = VLC_FALSE;
390 vlc_mutex_unlock( &p_intf->object_lock );
392 p_intf->p_module = module_Need( p_intf, "interface", psz_intf, 0 );
395 while( p_intf->p_module );
398 static int SwitchIntfCallback( vlc_object_t *p_this, char const *psz_cmd,
399 vlc_value_t oldval, vlc_value_t newval, void *p_data )
401 intf_thread_t *p_intf = (intf_thread_t *)p_this;
402 (void)psz_cmd; (void)oldval; (void)p_data;
404 p_intf->psz_switch_intf =
405 malloc( strlen(newval.psz_string) + sizeof(",none") );
406 sprintf( p_intf->psz_switch_intf, "%s,none", newval.psz_string );
407 vlc_object_kill( p_intf );
412 static int AddIntfCallback( vlc_object_t *p_this, char const *psz_cmd,
413 vlc_value_t oldval, vlc_value_t newval, void *p_data )
415 intf_thread_t *p_intf;
416 char *psz_intf = malloc( strlen(newval.psz_string) + sizeof(",none") );
418 (void)psz_cmd; (void)oldval; (void)p_data;
420 /* Try to create the interface */
421 sprintf( psz_intf, "%s,none", newval.psz_string );
422 p_intf = intf_Create( p_this->p_libvlc, psz_intf, 0, NULL );
426 msg_Err( p_this, "interface \"%s\" initialization failed",
431 /* Try to run the interface */
432 p_intf->b_block = VLC_FALSE;
433 if( intf_RunThread( p_intf ) != VLC_SUCCESS )
435 vlc_object_detach( p_intf );
436 intf_Destroy( p_intf );
444 /*****************************************************************************
445 * VLCApplication implementation
446 *****************************************************************************/
447 @implementation VLCApplication
449 - (void)setVLC: (libvlc_int_t *) p_libvlc
454 - (void)terminate: (id)sender
456 vlc_object_kill( o_libvlc );
457 [super terminate: sender];