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