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