]> git.sesse.net Git - vlc/blob - src/interface/interface.c
* - This should fix the "crash upon quit" issue with VLC Mac OS X.
[vlc] / src / interface / interface.c
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
7  * $Id$
8  *
9  * Authors: Vincent Seguin <seguin@via.ecp.fr>
10  *
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.
15  *
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.
20  *
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., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
24  *****************************************************************************/
25
26 /**
27  *   \file
28  *   This file contains functions related to interface management
29  */
30
31
32 /*****************************************************************************
33  * Preamble
34  *****************************************************************************/
35 #include <stdlib.h>                                      /* free(), strtol() */
36 #include <stdio.h>                                                   /* FILE */
37 #include <string.h>                                            /* strerror() */
38
39 #include <vlc/vlc.h>
40 #include <vlc/input.h>
41
42 #include "audio_output.h"
43
44 #include "vlc_interface.h"
45 #include "vlc_video.h"
46 #include "video_output.h"
47
48 #ifdef SYS_DARWIN
49 #    include "Cocoa/Cocoa.h"
50 #endif /* SYS_DARWIN */
51
52 /*****************************************************************************
53  * Local prototypes
54  *****************************************************************************/
55 static void Manager( intf_thread_t *p_intf );
56 static void RunInterface( intf_thread_t *p_intf );
57
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 * );
62
63 #ifdef SYS_DARWIN
64 /*****************************************************************************
65  * VLCApplication interface
66  *****************************************************************************/
67 @interface VLCApplication : NSApplication
68 {
69    vlc_t *o_vlc;
70 }
71
72 - (void)setVLC: (vlc_t *)p_vlc;
73
74 @end
75 #endif
76
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  *****************************************************************************/
83 /**
84  * Create the interface, and prepare it for main loop.
85  *
86  * \param p_this the calling vlc_object_t
87  * \param psz_module a prefered interface module
88  * \return a pointer to the created interface thread, NULL on error
89  */
90 intf_thread_t* __intf_Create( vlc_object_t *p_this, const char *psz_module )
91 {
92     intf_thread_t * p_intf;
93
94     /* Allocate structure */
95     p_intf = vlc_object_create( p_this, VLC_OBJECT_INTF );
96     if( !p_intf )
97     {
98         msg_Err( p_this, "out of memory" );
99         return NULL;
100     }
101     p_intf->pf_request_window = NULL;
102     p_intf->pf_release_window = NULL;
103     p_intf->pf_control_window = NULL;
104     p_intf->b_play = VLC_FALSE;
105     p_intf->b_interaction = VLC_FALSE;
106
107     /* Choose the best module */
108     p_intf->p_module = module_Need( p_intf, "interface", psz_module, 0 );
109
110     if( p_intf->p_module == NULL )
111     {
112         msg_Err( p_intf, "no suitable intf module" );
113         vlc_object_destroy( p_intf );
114         return NULL;
115     }
116
117     /* Initialize structure */
118     p_intf->b_menu        = VLC_FALSE;
119     p_intf->b_menu_change = VLC_FALSE;
120
121     /* Initialize mutexes */
122     vlc_mutex_init( p_intf, &p_intf->change_lock );
123
124     msg_Dbg( p_intf, "interface initialized" );
125
126     /* Attach interface to its parent object */
127     vlc_object_attach( p_intf, p_this );
128
129     return p_intf;
130 }
131
132 /*****************************************************************************
133  * intf_RunThread: launch the interface thread
134  *****************************************************************************
135  * This function either creates a new thread and runs the interface in it,
136  * or runs the interface in the current thread, depending on b_block.
137  *****************************************************************************/
138 /**
139  * Run the interface thread.
140  *
141  * If b_block is not set, runs the interface in the thread, else,
142  * creates a new thread and runs the interface.
143  * \param p_intf the interface thread
144  * \return VLC_SUCCESS on success, an error number else
145  */
146 int intf_RunThread( intf_thread_t *p_intf )
147 {
148 #ifdef SYS_DARWIN
149     NSAutoreleasePool * o_pool;
150
151     if( p_intf->b_block )
152     {
153         /* This is the primary intf */
154         /* Run a manager thread, launch the interface, kill the manager */
155         if( vlc_thread_create( p_intf, "manager", Manager,
156                                VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
157         {
158             msg_Err( p_intf, "cannot spawn manager thread" );
159             return VLC_EGENERIC;
160         }
161     }
162
163     if( p_intf->b_block && strncmp( p_intf->p_vlc->psz_object_name,
164                                     "clivlc", 6) )
165     {
166         o_pool = [[NSAutoreleasePool alloc] init];
167         [VLCApplication sharedApplication];
168         [NSApp setVLC: p_intf->p_vlc];
169     }
170
171     if( p_intf->b_block &&
172         ( !strncmp( p_intf->p_module->psz_object_name, "macosx" , 6 ) ||
173           !strncmp( p_intf->p_vlc->psz_object_name, "clivlc", 6 ) ) )
174     {
175         /* VLC in normal primary interface mode */
176         RunInterface( p_intf );
177         p_intf->b_die = VLC_TRUE;
178     }
179     else
180     {
181         /* Run the interface in a separate thread */
182         if( !strcmp( p_intf->p_module->psz_object_name, "macosx" ) )
183         {
184             msg_Err( p_intf, "You cannot run the MacOS X module as an extrainterface. Please read the README.MacOSX.rtf file");
185             return VLC_EGENERIC;
186         }
187         if( vlc_thread_create( p_intf, "interface", RunInterface,
188                                VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
189         {
190             msg_Err( p_intf, "cannot spawn interface thread" );
191             return VLC_EGENERIC;
192         }
193
194         if( p_intf->b_block )
195         {
196             /* VLC in primary interface mode with a working macosx vout */
197             [NSApp run];
198             p_intf->b_die = VLC_TRUE;
199         }
200     }
201 #else
202     if( p_intf->b_block )
203     {
204         /* Run a manager thread, launch the interface, kill the manager */
205         if( vlc_thread_create( p_intf, "manager", Manager,
206                                VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
207         {
208             msg_Err( p_intf, "cannot spawn manager thread" );
209             return VLC_EGENERIC;
210         }
211
212         RunInterface( p_intf );
213
214         p_intf->b_die = VLC_TRUE;
215         /* Do not join the thread... intf_StopThread will do it for us */
216     }
217     else
218     {
219         /* Run the interface in a separate thread */
220         if( vlc_thread_create( p_intf, "interface", RunInterface,
221                                VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
222         {
223             msg_Err( p_intf, "cannot spawn interface thread" );
224             return VLC_EGENERIC;
225         }
226     }
227 #endif
228
229     return VLC_SUCCESS;
230 }
231
232 /**
233  * Stops the interface thread
234  *
235  * This function asks the interface thread to stop
236  * \param p_intf the interface thread
237  * \return nothing
238  */
239 void intf_StopThread( intf_thread_t *p_intf )
240 {
241     /* Tell the interface to die */
242     if( !p_intf->b_block )
243     {
244         p_intf->b_die = VLC_TRUE;
245     }
246
247     /* Wait for the thread to exit */
248     vlc_thread_join( p_intf );
249 }
250
251 /**
252  * \brief Destroy the interface after the main loop endeed.
253  *
254  * Destroys interfaces and closes output devices
255  * \param p_intf the interface thread
256  * \return nothing
257  */
258 void intf_Destroy( intf_thread_t *p_intf )
259 {
260     /* Unlock module if present (a switch may have failed) */
261     if( p_intf->p_module )
262     {
263         module_Unneed( p_intf, p_intf->p_module );
264     }
265
266     vlc_mutex_destroy( &p_intf->change_lock );
267
268     /* Free structure */
269     vlc_object_destroy( p_intf );
270 }
271
272
273 /* Following functions are local */
274
275 /*****************************************************************************
276  * Manager: helper thread for blocking interfaces
277  *****************************************************************************
278  * If the interface is launched in the main thread, it will not listen to
279  * p_vlc->b_die events because it is only supposed to listen to p_intf->b_die.
280  * This thread takes care of the matter.
281  *****************************************************************************/
282 /**
283  * \brief Helper thread for blocking interfaces.
284  * \ingroup vlc_interface
285  *
286  * This is a local function
287  * If the interface is launched in the main thread, it will not listen to
288  * p_vlc->b_die events because it is only supposed to listen to p_intf->b_die.
289  * This thread takes care of the matter.
290  * \see intf_RunThread
291  * \param p_intf an interface thread
292  * \return nothing
293  */
294 static void Manager( intf_thread_t *p_intf )
295 {
296     while( !p_intf->b_die )
297     {
298         msleep( INTF_IDLE_SLEEP );
299
300         if( p_intf->p_vlc->b_die )
301         {
302             p_intf->b_die = VLC_TRUE;
303 #ifdef SYS_DARWIN
304     if( strncmp( p_intf->p_vlc->psz_object_name, "clivlc", 6 ) )
305     {
306         [NSApp stop: NULL];
307     }
308 #endif
309             return;
310         }
311     }
312 }
313
314 /*****************************************************************************
315  * RunInterface: setups necessary data and give control to the interface
316  *****************************************************************************/
317 static void RunInterface( intf_thread_t *p_intf )
318 {
319     static char *ppsz_interfaces[] =
320     {
321         "skins2", "Skins 2",
322         "wxwidgets", "wxWidgets",
323         NULL, NULL
324     };
325     char **ppsz_parser;
326
327     vlc_list_t *p_list;
328     int i;
329     vlc_value_t val, text;
330     char *psz_intf;
331
332     /* Variable used for interface switching */
333     p_intf->psz_switch_intf = NULL;
334     var_Create( p_intf, "intf-switch", VLC_VAR_STRING |
335                 VLC_VAR_HASCHOICE | VLC_VAR_ISCOMMAND );
336     text.psz_string = _("Switch interface");
337     var_Change( p_intf, "intf-switch", VLC_VAR_SETTEXT, &text, NULL );
338
339     /* Only fill the list with available modules */
340     p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
341     for( ppsz_parser = ppsz_interfaces; *ppsz_parser; ppsz_parser += 2 )
342     {
343         for( i = 0; i < p_list->i_count; i++ )
344         {
345             module_t *p_module = (module_t *)p_list->p_values[i].p_object;
346             if( !strcmp( p_module->psz_object_name, ppsz_parser[0] ) )
347             {
348                 val.psz_string = ppsz_parser[0];
349                 text.psz_string = ppsz_parser[1];
350                 var_Change( p_intf, "intf-switch", VLC_VAR_ADDCHOICE,
351                             &val, &text );
352                 break;
353             }
354         }
355     }
356     vlc_list_release( p_list );
357
358     var_AddCallback( p_intf, "intf-switch", SwitchIntfCallback, NULL );
359
360     /* Variable used for interface spawning */
361     var_Create( p_intf, "intf-add", VLC_VAR_STRING |
362                 VLC_VAR_HASCHOICE | VLC_VAR_ISCOMMAND );
363     text.psz_string = _("Add Interface");
364     var_Change( p_intf, "intf-add", VLC_VAR_SETTEXT, &text, NULL );
365
366     val.psz_string = "rc"; text.psz_string = "Console";
367     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
368     val.psz_string = "telnet"; text.psz_string = "Telnet Interface";
369     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
370     val.psz_string = "http"; text.psz_string = "Web Interface";
371     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
372     val.psz_string = "logger"; text.psz_string = "Debug logging";
373     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
374     val.psz_string = "gestures"; text.psz_string = "Mouse Gestures";
375     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
376
377     var_AddCallback( p_intf, "intf-add", AddIntfCallback, NULL );
378
379     do
380     {
381         /* Give control to the interface */
382         p_intf->pf_run( p_intf );
383
384         /* Reset play on start status */
385         p_intf->b_play = VLC_FALSE;
386
387         if( !p_intf->psz_switch_intf )
388         {
389             break;
390         }
391
392         /* Make sure the old interface is completely uninitialized */
393         module_Unneed( p_intf, p_intf->p_module );
394
395         /* Provide ability to switch the main interface on the fly */
396         psz_intf = p_intf->psz_switch_intf;
397         p_intf->psz_switch_intf = NULL;
398
399         vlc_mutex_lock( &p_intf->object_lock );
400         p_intf->b_die = VLC_FALSE;
401         p_intf->b_dead = VLC_FALSE;
402         vlc_mutex_unlock( &p_intf->object_lock );
403
404         p_intf->p_module = module_Need( p_intf, "interface", psz_intf, 0 );
405         free( psz_intf );
406     }
407     while( p_intf->p_module );
408 }
409
410 static int SwitchIntfCallback( vlc_object_t *p_this, char const *psz_cmd,
411                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
412 {
413     intf_thread_t *p_intf = (intf_thread_t *)p_this;
414
415     p_intf->psz_switch_intf =
416         malloc( strlen(newval.psz_string) + sizeof(",none") );
417     sprintf( p_intf->psz_switch_intf, "%s,none", newval.psz_string );
418     p_intf->b_die = VLC_TRUE;
419
420     return VLC_SUCCESS;
421 }
422
423 static int AddIntfCallback( vlc_object_t *p_this, char const *psz_cmd,
424                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
425 {
426     intf_thread_t *p_intf;
427     char *psz_intf = malloc( strlen(newval.psz_string) + sizeof(",none") );
428
429     /* Try to create the interface */
430     sprintf( psz_intf, "%s,none", newval.psz_string );
431     p_intf = intf_Create( p_this->p_vlc, psz_intf );
432     free( psz_intf );
433     if( p_intf == NULL )
434     {
435         msg_Err( p_this, "interface \"%s\" initialization failed",
436                  newval.psz_string );
437         return VLC_EGENERIC;
438     }
439
440     /* Try to run the interface */
441     p_intf->b_block = VLC_FALSE;
442     if( intf_RunThread( p_intf ) != VLC_SUCCESS )
443     {
444         vlc_object_detach( p_intf );
445         intf_Destroy( p_intf );
446         return VLC_EGENERIC;
447     }
448
449     return VLC_SUCCESS;
450 }
451
452 #ifdef SYS_DARWIN
453 /*****************************************************************************
454  * VLCApplication implementation 
455  *****************************************************************************/
456 @implementation VLCApplication 
457
458 - (void)setVLC: (vlc_t *) p_vlc
459 {
460     o_vlc = p_vlc;
461 }
462
463 - (void)stop: (id)sender
464 {
465     NSEvent *o_event;
466     NSAutoreleasePool *o_pool;
467     [super stop:sender];
468
469     o_pool = [[NSAutoreleasePool alloc] init];
470     /* send a dummy event to break out of the event loop */
471     o_event = [NSEvent mouseEventWithType: NSLeftMouseDown
472                 location: NSMakePoint( 1, 1 ) modifierFlags: 0
473                 timestamp: 1 windowNumber: [[NSApp mainWindow] windowNumber]
474                 context: [NSGraphicsContext currentContext] eventNumber: 1
475                 clickCount: 1 pressure: 0.0];
476     [NSApp postEvent: o_event atStart: YES];
477     [o_pool release];
478 }
479
480 - (void)terminate: (id)sender
481 {
482     o_vlc->b_die = VLC_TRUE;
483 }
484
485 @end
486 #endif
487