interface.c 11.2 KB
Newer Older
1
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
2
3
4
 * interface.c: interface access for other threads
 * This library provides basic functions for threads to interact with user
 * interface, such as command line.
5
 *****************************************************************************
6
 * Copyright (C) 1998-2007 the VideoLAN team
gbazin's avatar
gbazin committed
7
 * $Id$
8
 *
Sam Hocevar's avatar
   
Sam Hocevar committed
9
 * Authors: Vincent Seguin <seguin@via.ecp.fr>
10
11
12
13
14
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
zorglub's avatar
zorglub committed
15
 *
16
17
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
18
19
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
20
 *
21
22
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
dionoea's avatar
dionoea committed
23
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
24
 *****************************************************************************/
Michel Kaempf's avatar
Michel Kaempf committed
25

zorglub's avatar
zorglub committed
26
27
28
29
30
31
/**
 *   \file
 *   This file contains functions related to interface management
 */


32
/*****************************************************************************
Michel Kaempf's avatar
Michel Kaempf committed
33
 * Preamble
34
 *****************************************************************************/
Christophe Massiot's avatar
Christophe Massiot committed
35

36
37
38
39
#ifdef HAVE_CONFIG_H
# include "config.h"
#endif

40
#include <vlc/vlc.h>
41

zorglub's avatar
zorglub committed
42
43
#include <vlc_aout.h>
#include <vlc_vout.h>
Renaud Dartus's avatar
   
Renaud Dartus committed
44

45
#include "vlc_interface.h"
46
#include "modules/modules.h" // Gruik!
47
#include "libvlc.h"
Michel Kaempf's avatar
Michel Kaempf committed
48

49
/*****************************************************************************
50
 * Local prototypes
51
 *****************************************************************************/
gbazin's avatar
   
gbazin committed
52
53
54
55
56
57
static void RunInterface( intf_thread_t *p_intf );

static int SwitchIntfCallback( vlc_object_t *, char const *,
                               vlc_value_t , vlc_value_t , void * );
static int AddIntfCallback( vlc_object_t *, char const *,
                            vlc_value_t , vlc_value_t , void * );
58

59
/*****************************************************************************
Vincent Seguin's avatar
Vincent Seguin committed
60
 * intf_Create: prepare interface before main loop
61
 *****************************************************************************
62
63
 * This function opens output devices and creates specific interfaces. It sends
 * its own error messages.
64
 *****************************************************************************/
zorglub's avatar
zorglub committed
65
66
/**
 * Create the interface, and prepare it for main loop.
67
 * You can give some additional options to be used for interface initialization
zorglub's avatar
zorglub committed
68
 *
zorglub's avatar
zorglub committed
69
 * \param p_this the calling vlc_object_t
70
 * \param psz_module a preferred interface module
71
72
 * \param i_options number additional options
 * \param ppsz_options additional option strings
zorglub's avatar
zorglub committed
73
74
 * \return a pointer to the created interface thread, NULL on error
 */
75
intf_thread_t* __intf_Create( vlc_object_t *p_this, const char *psz_module,
76
                              int i_options, const char *const *ppsz_options  )
77
{
78
    intf_thread_t * p_intf;
79
    int i;
Michel Kaempf's avatar
Michel Kaempf committed
80

Vincent Seguin's avatar
Vincent Seguin committed
81
    /* Allocate structure */
82
    p_intf = vlc_object_create( p_this, VLC_OBJECT_INTF );
Vincent Seguin's avatar
Vincent Seguin committed
83
84
    if( !p_intf )
    {
85
86
        msg_Err( p_this, "out of memory" );
        return NULL;
87
    }
gbazin's avatar
gbazin committed
88
89
    p_intf->pf_request_window = NULL;
    p_intf->pf_release_window = NULL;
90
    p_intf->pf_control_window = NULL;
91
    p_intf->b_play = VLC_FALSE;
92
    p_intf->b_interaction = VLC_FALSE;
93
    p_intf->b_should_run_on_first_thread = VLC_FALSE;
94

95
    for( i = 0 ; i< i_options; i++ )
96
        var_OptionParse( p_this, ppsz_options[i] );
97

Sam Hocevar's avatar
   
Sam Hocevar committed
98
    /* Choose the best module */
99
    p_intf->psz_intf = strdup( psz_module );
100
    p_intf->p_module = module_Need( p_intf, "interface", psz_module, VLC_FALSE );
101

Sam Hocevar's avatar
   
Sam Hocevar committed
102
    if( p_intf->p_module == NULL )
103
    {
zorglub's avatar
zorglub committed
104
        msg_Err( p_intf, "no suitable interface module" );
105
        free( p_intf->psz_intf );
106
107
        vlc_object_destroy( p_intf );
        return NULL;
108
    }
109

110
    /* Initialize structure */
Sam Hocevar's avatar
Sam Hocevar committed
111
112
    p_intf->b_menu        = VLC_FALSE;
    p_intf->b_menu_change = VLC_FALSE;
113

Sam Hocevar's avatar
   
Sam Hocevar committed
114
    /* Initialize mutexes */
115
116
    vlc_mutex_init( p_intf, &p_intf->change_lock );

Sam Hocevar's avatar
Sam Hocevar committed
117
118
    /* Attach interface to its parent object */
    vlc_object_attach( p_intf, p_this );
119
120

    return p_intf;
121
122
123
}

/*****************************************************************************
124
 * intf_RunThread: launch the interface thread
125
 *****************************************************************************
126
 * This function either creates a new thread and runs the interface in it.
127
 *****************************************************************************/
zorglub's avatar
zorglub committed
128
/**
129
 * Starts and runs the interface thread.
zorglub's avatar
zorglub committed
130
131
132
133
 *
 * \param p_intf the interface thread
 * \return VLC_SUCCESS on success, an error number else
 */
Sam Hocevar's avatar
Sam Hocevar committed
134
int intf_RunThread( intf_thread_t *p_intf )
135
{
136
137
138
139
    /* This interface doesn't need to be run */
    if( p_intf->pf_run == NULL )
        return VLC_SUCCESS;

140
141
    /* Hack to get Mac OS X Cocoa runtime running
     * (it needs access to the main thread) */
142
    if( p_intf->b_should_run_on_first_thread )
hartman's avatar
hartman committed
143
    {
144
145
        RunInterface( p_intf );
        return VLC_SUCCESS;
Sam Hocevar's avatar
   
Sam Hocevar committed
146
    }
147
    
148
149
150
151
152
153
    /* Run the interface in a separate thread */
    if( vlc_thread_create( p_intf, "interface", RunInterface,
                           VLC_THREAD_PRIORITY_LOW, VLC_FALSE ) )
    {
        msg_Err( p_intf, "cannot spawn interface thread" );
        return VLC_EGENERIC;
154
    }
Stéphane Borel's avatar
Stéphane Borel committed
155

156
157
158
    return VLC_SUCCESS;
}

zorglub's avatar
zorglub committed
159
160
161
162
163
164
165
/**
 * Stops the interface thread
 *
 * This function asks the interface thread to stop
 * \param p_intf the interface thread
 * \return nothing
 */
166
167
168
void intf_StopThread( intf_thread_t *p_intf )
{
    /* Tell the interface to die */
169
170
    vlc_object_kill( p_intf );
    if( p_intf->pf_run != NULL )
171
    {
172
173
        vlc_cond_signal( &p_intf->object_wait );
        vlc_thread_join( p_intf );
174
    }
175
}
176

zorglub's avatar
zorglub committed
177
178
/**
 * \brief Destroy the interface after the main loop endeed.
zorglub's avatar
zorglub committed
179
180
 *
 * Destroys interfaces and closes output devices
zorglub's avatar
zorglub committed
181
182
183
 * \param p_intf the interface thread
 * \return nothing
 */
184
185
void intf_Destroy( intf_thread_t *p_intf )
{
Sam Hocevar's avatar
Sam Hocevar committed
186
187
188
189
190
    /* Unlock module if present (a switch may have failed) */
    if( p_intf->p_module )
    {
        module_Unneed( p_intf, p_intf->p_module );
    }
191
    free( p_intf->psz_intf );
192

Sam Hocevar's avatar
   
Sam Hocevar committed
193
    vlc_mutex_destroy( &p_intf->change_lock );
194

Sam Hocevar's avatar
   
Sam Hocevar committed
195
    /* Free structure */
196
197
198
    vlc_object_destroy( p_intf );
}

zorglub's avatar
zorglub committed
199

gbazin's avatar
   
gbazin committed
200
/* Following functions are local */
zorglub's avatar
zorglub committed
201

gbazin's avatar
   
gbazin committed
202
203
204
205
206
/*****************************************************************************
 * RunInterface: setups necessary data and give control to the interface
 *****************************************************************************/
static void RunInterface( intf_thread_t *p_intf )
{
207
    static const char *ppsz_interfaces[] =
Sam Hocevar's avatar
Sam Hocevar committed
208
209
    {
        "skins2", "Skins 2",
zorglub's avatar
zorglub committed
210
#ifndef WIN32
zorglub's avatar
zorglub committed
211
        "wxwidgets", "wxWidgets",
zorglub's avatar
zorglub committed
212
#endif
Sam Hocevar's avatar
Sam Hocevar committed
213
214
        NULL, NULL
    };
215
    const char **ppsz_parser;
Sam Hocevar's avatar
Sam Hocevar committed
216
217
218

    vlc_list_t *p_list;
    int i;
gbazin's avatar
   
gbazin committed
219
    vlc_value_t val, text;
Sam Hocevar's avatar
Sam Hocevar committed
220
    char *psz_intf;
gbazin's avatar
   
gbazin committed
221
222
223
224
225
226
227
228

    /* Variable used for interface switching */
    p_intf->psz_switch_intf = NULL;
    var_Create( p_intf, "intf-switch", VLC_VAR_STRING |
                VLC_VAR_HASCHOICE | VLC_VAR_ISCOMMAND );
    text.psz_string = _("Switch interface");
    var_Change( p_intf, "intf-switch", VLC_VAR_SETTEXT, &text, NULL );

Sam Hocevar's avatar
Sam Hocevar committed
229
230
231
232
233
234
235
236
237
    /* Only fill the list with available modules */
    p_list = vlc_list_find( p_intf, VLC_OBJECT_MODULE, FIND_ANYWHERE );
    for( ppsz_parser = ppsz_interfaces; *ppsz_parser; ppsz_parser += 2 )
    {
        for( i = 0; i < p_list->i_count; i++ )
        {
            module_t *p_module = (module_t *)p_list->p_values[i].p_object;
            if( !strcmp( p_module->psz_object_name, ppsz_parser[0] ) )
            {
238
239
                val.psz_string = (char *)ppsz_parser[0];
                text.psz_string = (char *)_(ppsz_parser[1]);
Sam Hocevar's avatar
Sam Hocevar committed
240
241
242
243
244
245
246
                var_Change( p_intf, "intf-switch", VLC_VAR_ADDCHOICE,
                            &val, &text );
                break;
            }
        }
    }
    vlc_list_release( p_list );
gbazin's avatar
   
gbazin committed
247
248
249
250
251
252

    var_AddCallback( p_intf, "intf-switch", SwitchIntfCallback, NULL );

    /* Variable used for interface spawning */
    var_Create( p_intf, "intf-add", VLC_VAR_STRING |
                VLC_VAR_HASCHOICE | VLC_VAR_ISCOMMAND );
hartman's avatar
hartman committed
253
    text.psz_string = _("Add Interface");
gbazin's avatar
   
gbazin committed
254
255
    var_Change( p_intf, "intf-add", VLC_VAR_SETTEXT, &text, NULL );

256
    val.psz_string = (char *)"rc"; text.psz_string = (char *)"Console";
gbazin's avatar
   
gbazin committed
257
    var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
258
259
    val.psz_string = (char *)"telnet";
    text.psz_string = (char *)_("Telnet Interface");
hartman's avatar
hartman committed
260
    var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
261
262
    val.psz_string = (char *)"http";
    text.psz_string = (char *)_("Web Interface");
gbazin's avatar
   
gbazin committed
263
    var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
264
265
    val.psz_string = (char *)"logger";
    text.psz_string = (char *)_("Debug logging");
gbazin's avatar
   
gbazin committed
266
    var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
267
268
    val.psz_string = (char *)"gestures";
    text.psz_string = (char *)_("Mouse Gestures");
gbazin's avatar
   
gbazin committed
269
    var_Change( p_intf, "intf-add", VLC_VAR_ADDCHOICE, &val, &text );
gbazin's avatar
   
gbazin committed
270
271
272

    var_AddCallback( p_intf, "intf-add", AddIntfCallback, NULL );

Sam Hocevar's avatar
Sam Hocevar committed
273
274
275
276
    do
    {
        /* Give control to the interface */
        p_intf->pf_run( p_intf );
gbazin's avatar
   
gbazin committed
277

Sam Hocevar's avatar
Sam Hocevar committed
278
279
        /* Reset play on start status */
        p_intf->b_play = VLC_FALSE;
280

Sam Hocevar's avatar
Sam Hocevar committed
281
282
283
284
285
        if( !p_intf->psz_switch_intf )
        {
            break;
        }

286
287
288
        /* Make sure the old interface is completely uninitialized */
        module_Unneed( p_intf, p_intf->p_module );

Sam Hocevar's avatar
Sam Hocevar committed
289
290
        /* Provide ability to switch the main interface on the fly */
        psz_intf = p_intf->psz_switch_intf;
gbazin's avatar
   
gbazin committed
291
292
        p_intf->psz_switch_intf = NULL;

293
        vlc_mutex_lock( &p_intf->object_lock );
294
        p_intf->b_die = VLC_FALSE; /* FIXME */
295
        p_intf->b_dead = VLC_FALSE;
296

297
298
        vlc_mutex_unlock( &p_intf->object_lock );

299
        p_intf->psz_intf = psz_intf;
gbazin's avatar
   
gbazin committed
300
        p_intf->p_module = module_Need( p_intf, "interface", psz_intf, 0 );
gbazin's avatar
   
gbazin committed
301
    }
Sam Hocevar's avatar
Sam Hocevar committed
302
    while( p_intf->p_module );
gbazin's avatar
   
gbazin committed
303
304
305
306
307
308
}

static int SwitchIntfCallback( vlc_object_t *p_this, char const *psz_cmd,
                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
{
    intf_thread_t *p_intf = (intf_thread_t *)p_this;
309
    (void)psz_cmd; (void)oldval; (void)p_data;
gbazin's avatar
   
gbazin committed
310
311
312
313

    p_intf->psz_switch_intf =
        malloc( strlen(newval.psz_string) + sizeof(",none") );
    sprintf( p_intf->psz_switch_intf, "%s,none", newval.psz_string );
314
    vlc_object_kill( p_intf );
gbazin's avatar
   
gbazin committed
315
316
317
318
319
320
321
322
323
324

    return VLC_SUCCESS;
}

static int AddIntfCallback( vlc_object_t *p_this, char const *psz_cmd,
                         vlc_value_t oldval, vlc_value_t newval, void *p_data )
{
    intf_thread_t *p_intf;
    char *psz_intf = malloc( strlen(newval.psz_string) + sizeof(",none") );

325
326
    (void)psz_cmd; (void)oldval; (void)p_data;

gbazin's avatar
   
gbazin committed
327
328
    /* Try to create the interface */
    sprintf( psz_intf, "%s,none", newval.psz_string );
329
    p_intf = intf_Create( p_this->p_libvlc, psz_intf, 0, NULL );
gbazin's avatar
   
gbazin committed
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
    free( psz_intf );
    if( p_intf == NULL )
    {
        msg_Err( p_this, "interface \"%s\" initialization failed",
                 newval.psz_string );
        return VLC_EGENERIC;
    }

    /* Try to run the interface */
    if( intf_RunThread( p_intf ) != VLC_SUCCESS )
    {
        vlc_object_detach( p_intf );
        intf_Destroy( p_intf );
        return VLC_EGENERIC;
    }

    return VLC_SUCCESS;
}
hartman's avatar
-    
hartman committed
348