Add encoder support for Dirac using the Schroedinger library.
[vlc/asuraparaju-public.git] / src / interface / interface.c
blob43b45f28d17af44730c25f12abea815042f5072b
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$
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 *****************************************************************************/
26 /**
27 * \file
28 * This file contains functions related to interface management
32 /*****************************************************************************
33 * Preamble
34 *****************************************************************************/
36 #ifdef HAVE_CONFIG_H
37 # include "config.h"
38 #endif
40 #include <assert.h>
41 #include <vlc_common.h>
42 #include <vlc_modules.h>
43 #include <vlc_interface.h>
45 #if defined( __APPLE__ ) || defined( WIN32 )
46 #include "../control/libvlc_internal.h"
47 #endif
48 #include "libvlc.h"
50 /*****************************************************************************
51 * Local prototypes
52 *****************************************************************************/
53 static void* RunInterface( vlc_object_t *p_this );
54 #if defined( __APPLE__ )
55 static void * MonitorLibVLCDeath( vlc_object_t *p_this );
56 #endif
57 static int AddIntfCallback( vlc_object_t *, char const *,
58 vlc_value_t , vlc_value_t , void * );
60 static vlc_mutex_t lock = VLC_STATIC_MUTEX;
62 #undef intf_Create
63 /**
64 * Create and start an interface.
66 * @param p_this the calling vlc_object_t
67 * @param psz_module a preferred interface module
68 * @return VLC_SUCCESS or an error code
70 int intf_Create( vlc_object_t *p_this, const char *psz_module )
72 libvlc_int_t *p_libvlc = p_this->p_libvlc;
73 intf_thread_t * p_intf;
74 static const char psz_type[] = "interface";
76 /* Allocate structure */
77 p_intf = vlc_custom_create( p_libvlc, sizeof( *p_intf ),
78 VLC_OBJECT_GENERIC, psz_type );
79 if( !p_intf )
80 return VLC_ENOMEM;
82 /* Variable used for interface spawning */
83 vlc_value_t val, text;
84 var_Create( p_intf, "intf-add", VLC_VAR_STRING |
85 VLC_VAR_HASCHOICE | VLC_VAR_ISCOMMAND );
86 text.psz_string = _("Add Interface");
87 var_Change( p_intf, "intf-add", VLC_VAR_SETTEXT, &text, NULL );
89 val.psz_string = (char *)"rc";
90 text.psz_string = (char *)_("Console");
91 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
92 val.psz_string = (char *)"telnet";
93 text.psz_string = (char *)_("Telnet Interface");
94 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
95 val.psz_string = (char *)"http";
96 text.psz_string = (char *)_("Web Interface");
97 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
98 val.psz_string = (char *)"logger";
99 text.psz_string = (char *)_("Debug logging");
100 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
101 val.psz_string = (char *)"gestures";
102 text.psz_string = (char *)_("Mouse Gestures");
103 var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
105 var_AddCallback( p_intf, "intf-add", AddIntfCallback, NULL );
107 /* Attach interface to LibVLC */
108 vlc_object_attach( p_intf, p_libvlc );
109 #if defined( __APPLE__ )
110 p_intf->b_should_run_on_first_thread = false;
111 #endif
113 /* Choose the best module */
114 p_intf->p_cfg = NULL;
115 char *psz_parser = *psz_module == '$'
116 ? var_CreateGetString(p_intf,psz_module+1)
117 : strdup( psz_module );
118 char *psz_tmp = config_ChainCreate( &p_intf->psz_intf, &p_intf->p_cfg,
119 psz_parser );
120 free( psz_tmp );
121 free( psz_parser );
122 p_intf->p_module = module_need( p_intf, "interface", p_intf->psz_intf, true );
123 if( p_intf->p_module == NULL )
125 msg_Err( p_intf, "no suitable interface module" );
126 goto error;
129 vlc_mutex_lock( &lock );
130 #if defined( __APPLE__ )
131 /* Hack to get Mac OS X Cocoa runtime running
132 * (it needs access to the main thread) */
133 if( p_intf->b_should_run_on_first_thread )
135 if( vlc_thread_create( p_intf, "interface", MonitorLibVLCDeath,
136 VLC_THREAD_PRIORITY_LOW ) )
138 msg_Err( p_intf, "cannot spawn libvlc death monitoring thread" );
139 vlc_mutex_unlock( &lock );
140 goto error;
142 assert( p_intf->pf_run );
143 p_intf->pf_run( p_intf );
145 /* It is monitoring libvlc, not the p_intf */
146 vlc_object_kill( p_intf->p_libvlc );
148 else
149 #endif
150 /* Run the interface in a separate thread */
151 if( p_intf->pf_run
152 && vlc_thread_create( p_intf, "interface", RunInterface,
153 VLC_THREAD_PRIORITY_LOW ) )
155 msg_Err( p_intf, "cannot spawn interface thread" );
156 vlc_mutex_unlock( &lock );
157 goto error;
160 p_intf->p_next = libvlc_priv( p_libvlc )->p_intf;
161 libvlc_priv( p_libvlc )->p_intf = p_intf;
162 vlc_mutex_unlock( &lock );
164 return VLC_SUCCESS;
166 error:
167 if( p_intf->p_module )
168 module_unneed( p_intf, p_intf->p_module );
169 config_ChainDestroy( p_intf->p_cfg );
170 free( p_intf->psz_intf );
171 vlc_object_release( p_intf );
172 return VLC_EGENERIC;
177 * Stops and destroys all interfaces
178 * @param p_libvlc the LibVLC instance
180 void intf_DestroyAll( libvlc_int_t *p_libvlc )
182 intf_thread_t *p_first;
184 vlc_mutex_lock( &lock );
185 p_first = libvlc_priv( p_libvlc )->p_intf;
186 #ifndef NDEBUG
187 libvlc_priv( p_libvlc )->p_intf = NULL;
188 #endif
189 vlc_mutex_unlock( &lock );
191 /* Tell the interfaces to die */
192 for( intf_thread_t *p_intf = p_first; p_intf; p_intf = p_intf->p_next )
193 vlc_object_kill( p_intf );
195 /* Cleanup the interfaces */
196 for( intf_thread_t *p_intf = p_first; p_intf != NULL; )
198 intf_thread_t *p_next = p_intf->p_next;
200 if( p_intf->pf_run )
201 vlc_thread_join( p_intf );
202 module_unneed( p_intf, p_intf->p_module );
203 free( p_intf->psz_intf );
204 config_ChainDestroy( p_intf->p_cfg );
205 vlc_object_release( p_intf );
207 p_intf = p_next;
211 /* Following functions are local */
214 * RunInterface: setups necessary data and give control to the interface
216 * @param p_this: interface object
218 static void* RunInterface( vlc_object_t *p_this )
220 intf_thread_t *p_intf = (intf_thread_t *)p_this;
222 p_intf->pf_run( p_intf );
223 return NULL;
226 #if defined( __APPLE__ )
227 #include "control/libvlc_internal.h" /* libvlc_InternalWait */
229 * MonitorLibVLCDeath: Used when b_should_run_on_first_thread is set.
231 * @param p_this: the interface object
233 static void * MonitorLibVLCDeath( vlc_object_t * p_this )
235 intf_thread_t *p_intf = (intf_thread_t *)p_this;
236 libvlc_int_t * p_libvlc = p_intf->p_libvlc;
237 int canc = vlc_savecancel ();
239 libvlc_InternalWait( p_libvlc );
241 vlc_object_kill( p_intf ); /* Kill the stupid first thread interface */
242 vlc_restorecancel (canc);
243 return NULL;
245 #endif
247 static int AddIntfCallback( vlc_object_t *p_this, char const *psz_cmd,
248 vlc_value_t oldval, vlc_value_t newval, void *p_data )
250 (void)psz_cmd; (void)oldval; (void)p_data;
251 char* psz_intf;
253 /* Try to create the interface */
254 if( asprintf( &psz_intf, "%s,none", newval.psz_string ) == -1 )
255 return VLC_ENOMEM;
257 int ret = intf_Create( VLC_OBJECT(p_this->p_libvlc), psz_intf );
258 free( psz_intf );
259 if( ret )
260 msg_Err( p_this, "interface \"%s\" initialization failed",
261 newval.psz_string );
262 return ret;