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() */
40 #include <vlc/input.h>
42 #include "audio_output.h"
44 #include "vlc_interface.h"
45 #include "vlc_video.h"
46 #include "video_output.h"
49 # include <Cocoa/Cocoa.h>
52 /*****************************************************************************
54 *****************************************************************************/
55 static void Manager( intf_thread_t *p_intf );
56 static void RunInterface( intf_thread_t *p_intf );
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 * );
64 /*****************************************************************************
65 * VLCApplication interface
66 *****************************************************************************/
67 @interface VLCApplication : NSApplication
72 - (void)setVLC: (vlc_t *)p_vlc;
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 *****************************************************************************/
84 * Create the interface, and prepare it for main loop.
85 * You can give some additional options to be used for interface initialization
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
93 intf_thread_t* __intf_Create( vlc_object_t *p_this, const char *psz_module,
94 int i_options, char **ppsz_options )
96 intf_thread_t * p_intf;
99 /* Allocate structure */
100 p_intf = vlc_object_create( p_this, VLC_OBJECT_INTF );
103 msg_Err( p_this, "out of memory" );
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;
112 for( i = 0 ; i< i_options; i++ )
114 var_OptionParse( p_intf, ppsz_options[i] );
117 /* Choose the best module */
118 p_intf->p_module = module_Need( p_intf, "interface", psz_module, 0 );
120 if( p_intf->p_module == NULL )
122 msg_Err( p_intf, "no suitable interface module" );
123 vlc_object_destroy( p_intf );
127 /* Initialize structure */
128 p_intf->b_menu = VLC_FALSE;
129 p_intf->b_menu_change = VLC_FALSE;
131 /* Initialize mutexes */
132 vlc_mutex_init( p_intf, &p_intf->change_lock );
134 /* Attach interface to its parent object */
135 vlc_object_attach( p_intf, p_this );
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 *****************************************************************************/
147 * Run the interface thread.
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
154 int intf_RunThread( intf_thread_t *p_intf )
157 NSAutoreleasePool * o_pool;
159 if( p_intf->b_block )
161 /* This is the primary intf */
162 /* Run a manager thread, launch the interface, kill the manager */
163 if( vlc_thread_create( p_intf, "manage", Manager,
164 VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
166 msg_Err( p_intf, "cannot spawn manager thread" );
171 if( p_intf->b_block && strncmp( p_intf->p_vlc->psz_object_name,
174 o_pool = [[NSAutoreleasePool alloc] init];
175 [VLCApplication sharedApplication];
176 [NSApp setVLC: p_intf->p_vlc];
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 ) ) )
183 /* VLC in normal primary interface mode */
184 RunInterface( p_intf );
185 p_intf->b_die = VLC_TRUE;
189 /* Run the interface in a separate thread */
190 if( !strcmp( p_intf->p_module->psz_object_name, "macosx" ) )
192 msg_Err( p_intf, "You cannot run the MacOS X module as an "
193 "extra interface. Please read the "
194 "README.MacOSX.rtf file");
197 if( vlc_thread_create( p_intf, "interface", RunInterface,
198 VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
200 msg_Err( p_intf, "cannot spawn interface thread" );
204 if( p_intf->b_block )
206 /* VLC in primary interface mode with a working macosx vout */
208 p_intf->b_die = VLC_TRUE;
212 if( p_intf->b_block )
214 /* Run a manager thread, launch the interface, kill the manager */
215 if( vlc_thread_create( p_intf, "manager", Manager,
216 VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
218 msg_Err( p_intf, "cannot spawn manager thread" );
222 RunInterface( p_intf );
224 p_intf->b_die = VLC_TRUE;
225 /* Do not join the thread... intf_StopThread will do it for us */
229 /* Run the interface in a separate thread */
230 if( vlc_thread_create( p_intf, "interface", RunInterface,
231 VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
233 msg_Err( p_intf, "cannot spawn interface thread" );
243 * Stops the interface thread
245 * This function asks the interface thread to stop
246 * \param p_intf the interface thread
249 void intf_StopThread( intf_thread_t *p_intf )
251 /* Tell the interface to die */
252 if( !p_intf->b_block )
254 p_intf->b_die = VLC_TRUE;
257 /* Wait for the thread to exit */
258 vlc_thread_join( p_intf );
262 * \brief Destroy the interface after the main loop endeed.
264 * Destroys interfaces and closes output devices
265 * \param p_intf the interface thread
268 void intf_Destroy( intf_thread_t *p_intf )
270 /* Unlock module if present (a switch may have failed) */
271 if( p_intf->p_module )
273 module_Unneed( p_intf, p_intf->p_module );
276 vlc_mutex_destroy( &p_intf->change_lock );
279 vlc_object_destroy( p_intf );
283 /* Following functions are local */
285 /*****************************************************************************
286 * Manager: helper thread for blocking interfaces
287 *****************************************************************************
288 * If the interface is launched in the main thread, it will not listen to
289 * p_vlc->b_die events because it is only supposed to listen to p_intf->b_die.
290 * This thread takes care of the matter.
291 *****************************************************************************/
293 * \brief Helper thread for blocking interfaces.
294 * \ingroup vlc_interface
296 * This is a local function
297 * If the interface is launched in the main thread, it will not listen to
298 * p_vlc->b_die events because it is only supposed to listen to p_intf->b_die.
299 * This thread takes care of the matter.
300 * \see intf_RunThread
301 * \param p_intf an interface thread
304 static void Manager( intf_thread_t *p_intf )
306 while( !p_intf->b_die )
308 msleep( INTF_IDLE_SLEEP );
310 if( p_intf->p_vlc->b_die )
312 p_intf->b_die = VLC_TRUE;
314 if( strncmp( p_intf->p_vlc->psz_object_name, "clivlc", 6 ) )
324 /*****************************************************************************
325 * RunInterface: setups necessary data and give control to the interface
326 *****************************************************************************/
327 static void RunInterface( intf_thread_t *p_intf )
329 static char *ppsz_interfaces[] =
332 "wxwidgets", "wxWidgets",
339 vlc_value_t val, text;
342 /* Variable used for interface switching */
343 p_intf->psz_switch_intf = NULL;
344 var_Create( p_intf, "intf-switch", VLC_VAR_STRING |
345 VLC_VAR_HASCHOICE | VLC_VAR_ISCOMMAND );
346 text.psz_string = _("Switch interface");
347 var_Change( p_intf, "intf-switch", VLC_VAR_SETTEXT, &text, NULL );
349 /* Only fill the list with available modules */
350 p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
351 for( ppsz_parser = ppsz_interfaces; *ppsz_parser; ppsz_parser += 2 )
353 for( i = 0; i < p_list->i_count; i++ )
355 module_t *p_module = (module_t *)p_list->p_values[i].p_object;
356 if( !strcmp( p_module->psz_object_name, ppsz_parser[0] ) )
358 val.psz_string = ppsz_parser[0];
359 text.psz_string = ppsz_parser[1];
360 var_Change( p_intf, "intf-switch", VLC_VAR_ADDCHOICE,
366 vlc_list_release( p_list );
368 var_AddCallback( p_intf, "intf-switch", SwitchIntfCallback, NULL );
370 /* Variable used for interface spawning */
371 var_Create( p_intf, "intf-add", VLC_VAR_STRING |
372 VLC_VAR_HASCHOICE | VLC_VAR_ISCOMMAND );
373 text.psz_string = _("Add Interface");
374 var_Change( p_intf, "intf-add", VLC_VAR_SETTEXT, &text, NULL );
376 val.psz_string = "rc"; text.psz_string = "Console";
377 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
378 val.psz_string = "telnet"; text.psz_string = "Telnet Interface";
379 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
380 val.psz_string = "http"; text.psz_string = "Web Interface";
381 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
382 val.psz_string = "logger"; text.psz_string = "Debug logging";
383 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
384 val.psz_string = "gestures"; text.psz_string = "Mouse Gestures";
385 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
387 var_AddCallback( p_intf, "intf-add", AddIntfCallback, NULL );
391 /* Give control to the interface */
392 p_intf->pf_run( p_intf );
394 /* Reset play on start status */
395 p_intf->b_play = VLC_FALSE;
397 if( !p_intf->psz_switch_intf )
402 /* Make sure the old interface is completely uninitialized */
403 module_Unneed( p_intf, p_intf->p_module );
405 /* Provide ability to switch the main interface on the fly */
406 psz_intf = p_intf->psz_switch_intf;
407 p_intf->psz_switch_intf = NULL;
409 vlc_mutex_lock( &p_intf->object_lock );
410 p_intf->b_die = VLC_FALSE;
411 p_intf->b_dead = VLC_FALSE;
412 vlc_mutex_unlock( &p_intf->object_lock );
414 p_intf->p_module = module_Need( p_intf, "interface", psz_intf, 0 );
417 while( p_intf->p_module );
420 static int SwitchIntfCallback( vlc_object_t *p_this, char const *psz_cmd,
421 vlc_value_t oldval, vlc_value_t newval, void *p_data )
423 intf_thread_t *p_intf = (intf_thread_t *)p_this;
425 p_intf->psz_switch_intf =
426 malloc( strlen(newval.psz_string) + sizeof(",none") );
427 sprintf( p_intf->psz_switch_intf, "%s,none", newval.psz_string );
428 p_intf->b_die = VLC_TRUE;
433 static int AddIntfCallback( vlc_object_t *p_this, char const *psz_cmd,
434 vlc_value_t oldval, vlc_value_t newval, void *p_data )
436 intf_thread_t *p_intf;
437 char *psz_intf = malloc( strlen(newval.psz_string) + sizeof(",none") );
439 /* Try to create the interface */
440 sprintf( psz_intf, "%s,none", newval.psz_string );
441 p_intf = intf_Create( p_this->p_vlc, psz_intf, 0, NULL );
445 msg_Err( p_this, "interface \"%s\" initialization failed",
450 /* Try to run the interface */
451 p_intf->b_block = VLC_FALSE;
452 if( intf_RunThread( p_intf ) != VLC_SUCCESS )
454 vlc_object_detach( p_intf );
455 intf_Destroy( p_intf );
463 /*****************************************************************************
464 * VLCApplication implementation
465 *****************************************************************************/
466 @implementation VLCApplication
468 - (void)setVLC: (vlc_t *) p_vlc
473 - (void)stop: (id)sender
476 NSAutoreleasePool *o_pool;
479 o_pool = [[NSAutoreleasePool alloc] init];
480 /* send a dummy event to break out of the event loop */
481 o_event = [NSEvent mouseEventWithType: NSLeftMouseDown
482 location: NSMakePoint( 1, 1 ) modifierFlags: 0
483 timestamp: 1 windowNumber: [[NSApp mainWindow] windowNumber]
484 context: [NSGraphicsContext currentContext] eventNumber: 1
485 clickCount: 1 pressure: 0.0];
486 [NSApp postEvent: o_event atStart: YES];
490 - (void)terminate: (id)sender
492 o_vlc->b_die = VLC_TRUE;