]> git.sesse.net Git - vlc/blob - src/interface/interface.c
Another MacOS compile fix
[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-2007 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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24  *****************************************************************************/
25
26 /**
27  *   \file
28  *   This file contains functions related to interface management
29  */
30
31
32 /*****************************************************************************
33  * Preamble
34  *****************************************************************************/
35
36 #include <vlc/vlc.h>
37
38 #include <vlc_aout.h>
39 #include <vlc_vout.h>
40
41 #include "vlc_interface.h"
42 #include "modules/modules.h" // Gruik!
43
44 #ifdef __APPLE__
45 #    include <Cocoa/Cocoa.h>
46 #endif
47
48 /*****************************************************************************
49  * Local prototypes
50  *****************************************************************************/
51 static void RunInterface( intf_thread_t *p_intf );
52
53 static int SwitchIntfCallback( vlc_object_t *, char const *,
54                                vlc_value_t , vlc_value_t , void * );
55 static int AddIntfCallback( vlc_object_t *, char const *,
56                             vlc_value_t , vlc_value_t , void * );
57
58 #ifdef __APPLE__
59 static void Manager( intf_thread_t *p_intf );
60 /*****************************************************************************
61  * VLCApplication interface
62  *****************************************************************************/
63 @interface VLCApplication : NSApplication
64 {
65    libvlc_int_t *o_libvlc;
66 }
67
68 - (void)setVLC: (libvlc_int_t *)p_libvlc;
69
70 @end
71 #endif
72
73 /*****************************************************************************
74  * intf_Create: prepare interface before main loop
75  *****************************************************************************
76  * This function opens output devices and creates specific interfaces. It sends
77  * its own error messages.
78  *****************************************************************************/
79 /**
80  * Create the interface, and prepare it for main loop.
81  * You can give some additional options to be used for interface initialization
82  *
83  * \param p_this the calling vlc_object_t
84  * \param psz_module a preferred interface module
85  * \param i_options number additional options
86  * \param ppsz_options additional option strings
87  * \return a pointer to the created interface thread, NULL on error
88  */
89 intf_thread_t* __intf_Create( vlc_object_t *p_this, const char *psz_module,
90                               int i_options, const char *const *ppsz_options  )
91 {
92     intf_thread_t * p_intf;
93     int i;
94
95     /* Allocate structure */
96     p_intf = vlc_object_create( p_this, VLC_OBJECT_INTF );
97     if( !p_intf )
98     {
99         msg_Err( p_this, "out of memory" );
100         return NULL;
101     }
102     p_intf->pf_request_window = NULL;
103     p_intf->pf_release_window = NULL;
104     p_intf->pf_control_window = NULL;
105     p_intf->b_play = VLC_FALSE;
106     p_intf->b_interaction = VLC_FALSE;
107
108     for( i = 0 ; i< i_options; i++ )
109     {
110         var_OptionParse( p_intf, ppsz_options[i] );
111     }
112
113     /* Choose the best module */
114     p_intf->p_module = module_Need( p_intf, "interface", psz_module, VLC_FALSE );
115
116     if( p_intf->p_module == NULL )
117     {
118         msg_Err( p_intf, "no suitable interface module" );
119         vlc_object_destroy( p_intf );
120         return NULL;
121     }
122
123     /* Initialize structure */
124     p_intf->b_menu        = VLC_FALSE;
125     p_intf->b_menu_change = VLC_FALSE;
126
127     /* Initialize mutexes */
128     vlc_mutex_init( p_intf, &p_intf->change_lock );
129
130     /* Attach interface to its parent object */
131     vlc_object_attach( p_intf, p_this );
132
133     return p_intf;
134 }
135
136 /*****************************************************************************
137  * intf_RunThread: launch the interface thread
138  *****************************************************************************
139  * This function either creates a new thread and runs the interface in it.
140  *****************************************************************************/
141 /**
142  * Starts and runs the interface thread.
143  *
144  * \param p_intf the interface thread
145  * \return VLC_SUCCESS on success, an error number else
146  */
147 int intf_RunThread( intf_thread_t *p_intf )
148 {
149 #ifdef __APPLE__
150 # warning FIXME
151 #if 0
152     NSAutoreleasePool * o_pool;
153
154     /* If !clivlc, then run as a OS X application */
155     if( strncmp( p_intf->p_libvlc->psz_object_name, "clivlc", 6) )
156     {
157         o_pool = [[NSAutoreleasePool alloc] init];
158         [VLCApplication sharedApplication];
159         [NSApp setVLC: p_intf->p_libvlc];
160
161         if( p_intf->pf_run )
162             RunInterface( p_intf );
163         else
164         {
165             [NSApp run];
166             while( !intf_ShouldDie( p_intf ) )
167                 msleep( INTF_IDLE_SLEEP * 2);
168         }
169         vlc_object_kill( p_intf );
170         return VLC_SUCCESS;
171     }
172 #endif
173 #endif
174
175     /* This interface doesn't need to be run */
176     if( p_intf->pf_run == NULL )
177         return VLC_SUCCESS;
178
179     /* Run the interface in a separate thread */
180     if( !strcmp( p_intf->p_module->psz_object_name, "macosx" ) )
181     {
182         msg_Err( p_intf, "You cannot run the MacOS X module as an "
183                          "extra interface. Please read the "
184                          "README.MacOSX.rtf file.");
185         return VLC_EGENERIC;
186     }
187
188     /* Run the interface in a separate thread */
189     if( vlc_thread_create( p_intf, "interface", RunInterface,
190                            VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
191     {
192         msg_Err( p_intf, "cannot spawn interface thread" );
193         return VLC_EGENERIC;
194     }
195
196     return VLC_SUCCESS;
197 }
198
199 /**
200  * Stops the interface thread
201  *
202  * This function asks the interface thread to stop
203  * \param p_intf the interface thread
204  * \return nothing
205  */
206 void intf_StopThread( intf_thread_t *p_intf )
207 {
208     /* Tell the interface to die */
209     vlc_object_kill( p_intf );
210     if( p_intf->pf_run != NULL )
211     {
212         vlc_cond_signal( &p_intf->object_wait );
213         vlc_thread_join( p_intf );
214     }
215 }
216
217 /**
218  * \brief Destroy the interface after the main loop endeed.
219  *
220  * Destroys interfaces and closes output devices
221  * \param p_intf the interface thread
222  * \return nothing
223  */
224 void intf_Destroy( intf_thread_t *p_intf )
225 {
226     /* Unlock module if present (a switch may have failed) */
227     if( p_intf->p_module )
228     {
229         module_Unneed( p_intf, p_intf->p_module );
230     }
231
232     vlc_mutex_destroy( &p_intf->change_lock );
233
234     /* Free structure */
235     vlc_object_destroy( p_intf );
236 }
237
238
239 /* Following functions are local */
240
241 /*****************************************************************************
242  * Manager: helper thread for blocking OS X
243  *****************************************************************************/
244 #ifdef __APPLE__
245 static void Manager( intf_thread_t *p_intf )
246 {
247     while( !p_intf->b_die )
248     {
249         msleep( INTF_IDLE_SLEEP );
250
251         if( p_intf->p_libvlc->b_die )
252         {
253             p_intf->b_die = VLC_TRUE;
254             if( strncmp( p_intf->p_libvlc->psz_object_name, "clivlc", 6 ) )
255             {
256                 [NSApp stop: NULL];
257             }
258             return;
259         }
260     }
261 }
262 #endif
263
264 /*****************************************************************************
265  * RunInterface: setups necessary data and give control to the interface
266  *****************************************************************************/
267 static void RunInterface( intf_thread_t *p_intf )
268 {
269     static const char *ppsz_interfaces[] =
270     {
271         "skins2", "Skins 2",
272 #ifndef WIN32
273         "wxwidgets", "wxWidgets",
274 #endif
275         NULL, NULL
276     };
277     const char **ppsz_parser;
278
279     vlc_list_t *p_list;
280     int i;
281     vlc_value_t val, text;
282     char *psz_intf;
283
284     /* Variable used for interface switching */
285     p_intf->psz_switch_intf = NULL;
286     var_Create( p_intf, "intf-switch", VLC_VAR_STRING |
287                 VLC_VAR_HASCHOICE | VLC_VAR_ISCOMMAND );
288     text.psz_string = _("Switch interface");
289     var_Change( p_intf, "intf-switch", VLC_VAR_SETTEXT, &text, NULL );
290
291     /* Only fill the list with available modules */
292     p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
293     for( ppsz_parser = ppsz_interfaces; *ppsz_parser; ppsz_parser += 2 )
294     {
295         for( i = 0; i < p_list->i_count; i++ )
296         {
297             module_t *p_module = (module_t *)p_list->p_values[i].p_object;
298             if( !strcmp( p_module->psz_object_name, ppsz_parser[0] ) )
299             {
300                 val.psz_string = (char *)ppsz_parser[0];
301                 text.psz_string = (char *)_(ppsz_parser[1]);
302                 var_Change( p_intf, "intf-switch", VLC_VAR_ADDCHOICE,
303                             &val, &text );
304                 break;
305             }
306         }
307     }
308     vlc_list_release( p_list );
309
310     var_AddCallback( p_intf, "intf-switch", SwitchIntfCallback, NULL );
311
312     /* Variable used for interface spawning */
313     var_Create( p_intf, "intf-add", VLC_VAR_STRING |
314                 VLC_VAR_HASCHOICE | VLC_VAR_ISCOMMAND );
315     text.psz_string = _("Add Interface");
316     var_Change( p_intf, "intf-add", VLC_VAR_SETTEXT, &text, NULL );
317
318     val.psz_string = (char *)"rc"; text.psz_string = (char *)"Console";
319     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
320     val.psz_string = (char *)"telnet";
321     text.psz_string = (char *)_("Telnet Interface");
322     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
323     val.psz_string = (char *)"http";
324     text.psz_string = (char *)_("Web Interface");
325     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
326     val.psz_string = (char *)"logger";
327     text.psz_string = (char *)_("Debug logging");
328     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
329     val.psz_string = (char *)"gestures";
330     text.psz_string = (char *)_("Mouse Gestures");
331     var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
332
333     var_AddCallback( p_intf, "intf-add", AddIntfCallback, NULL );
334
335     do
336     {
337         /* Give control to the interface */
338         p_intf->pf_run( p_intf );
339
340         /* Reset play on start status */
341         p_intf->b_play = VLC_FALSE;
342
343         if( !p_intf->psz_switch_intf )
344         {
345             break;
346         }
347
348         /* Make sure the old interface is completely uninitialized */
349         module_Unneed( p_intf, p_intf->p_module );
350
351         /* Provide ability to switch the main interface on the fly */
352         psz_intf = p_intf->psz_switch_intf;
353         p_intf->psz_switch_intf = NULL;
354
355         vlc_mutex_lock( &p_intf->object_lock );
356         p_intf->b_die = VLC_FALSE; /* FIXME */
357         p_intf->b_dead = VLC_FALSE;
358         vlc_mutex_unlock( &p_intf->object_lock );
359
360         p_intf->p_module = module_Need( p_intf, "interface", psz_intf, 0 );
361         free( psz_intf );
362     }
363     while( p_intf->p_module );
364 }
365
366 static int SwitchIntfCallback( vlc_object_t *p_this, char const *psz_cmd,
367                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
368 {
369     intf_thread_t *p_intf = (intf_thread_t *)p_this;
370     (void)psz_cmd; (void)oldval; (void)p_data;
371
372     p_intf->psz_switch_intf =
373         malloc( strlen(newval.psz_string) + sizeof(",none") );
374     sprintf( p_intf->psz_switch_intf, "%s,none", newval.psz_string );
375     vlc_object_kill( p_intf );
376
377     return VLC_SUCCESS;
378 }
379
380 static int AddIntfCallback( vlc_object_t *p_this, char const *psz_cmd,
381                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
382 {
383     intf_thread_t *p_intf;
384     char *psz_intf = malloc( strlen(newval.psz_string) + sizeof(",none") );
385
386     (void)psz_cmd; (void)oldval; (void)p_data;
387
388     /* Try to create the interface */
389     sprintf( psz_intf, "%s,none", newval.psz_string );
390     p_intf = intf_Create( p_this->p_libvlc, psz_intf, 0, NULL );
391     free( psz_intf );
392     if( p_intf == NULL )
393     {
394         msg_Err( p_this, "interface \"%s\" initialization failed",
395                  newval.psz_string );
396         return VLC_EGENERIC;
397     }
398
399     /* Try to run the interface */
400     if( intf_RunThread( p_intf ) != VLC_SUCCESS )
401     {
402         vlc_object_detach( p_intf );
403         intf_Destroy( p_intf );
404         return VLC_EGENERIC;
405     }
406
407     return VLC_SUCCESS;
408 }
409
410 #ifdef __APPLE__
411 /*****************************************************************************
412  * VLCApplication implementation
413  *****************************************************************************/
414 @implementation VLCApplication
415
416 - (void)setVLC: (libvlc_int_t *) p_libvlc
417 {
418     o_libvlc = p_libvlc;
419 }
420
421 - (void)terminate: (id)sender
422 {
423     vlc_object_kill( o_libvlc );
424     [super terminate: sender];
425 }
426
427 @end
428 #endif
429