interaction.c 19.8 KB
Newer Older
1
2
3
/*****************************************************************************
 * interaction.c: User interaction functions
 *****************************************************************************
zorglub's avatar
zorglub committed
4
 * Copyright (C) 2005-2006 the VideoLAN team
5
 * $Id$
6
 *
7
 * Authors: Clément Stenac <zorglub@videolan.org>
8
 *          Felix Kühne <fkuehne@videolan.org>
9
10
11
12
13
14
15
16
17
18
19
20
21
 *
 * 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.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * 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
22
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
 *****************************************************************************/

/**
 *   \file
 *   This file contains functions related to user interaction management
 */

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <stdlib.h>                                      /* free(), strtol() */
#include <stdio.h>                                                   /* FILE */
#include <string.h>                                            /* strerror() */

#include <vlc/vlc.h>
#include <vlc/input.h>
39
#include <assert.h>
40
41
42
43
44

#include "vlc_interaction.h"
#include "vlc_interface.h"
#include "vlc_playlist.h"

zorglub's avatar
zorglub committed
45
46
47
/*****************************************************************************
 * Local prototypes
 *****************************************************************************/
48
49
50
static void                  InteractionInit( playlist_t *p_playlist );
static interaction_t *       InteractionGet( vlc_object_t *p_this );
static void                  InteractionSearchInterface( interaction_t *
zorglub's avatar
zorglub committed
51
                                                          p_interaction );
52
53
54
static interaction_dialog_t *DialogGetById( interaction_t* , int );
static void                  DialogDestroy( interaction_dialog_t *p_dialog );
static int DialogSend( vlc_object_t *p_this, interaction_dialog_t *p_dialog );
55

zorglub's avatar
zorglub committed
56
/**
zorglub's avatar
zorglub committed
57
 * Destroy the interaction system
zorglub's avatar
zorglub committed
58
59
 * \param The interaction object to destroy
 * \return nothing
zorglub's avatar
zorglub committed
60
61
62
 */
void intf_InteractionDestroy( interaction_t *p_interaction )
{
zorglub's avatar
zorglub committed
63
64
65
66
67
    int i;
    // Remove all dialogs - Interfaces must be able to clean up their data
    for( i = p_interaction->i_dialogs -1 ; i >= 0; i-- )
    {
        interaction_dialog_t * p_dialog = p_interaction->pp_dialogs[i];
68
        DialogDestroy( p_dialog );
zorglub's avatar
zorglub committed
69
70
71
        REMOVE_ELEM( p_interaction->pp_dialogs, p_interaction->i_dialogs, i );
    }
    vlc_object_destroy( p_interaction );
zorglub's avatar
zorglub committed
72
73
}

zorglub's avatar
zorglub committed
74
/**
zorglub's avatar
zorglub committed
75
76
 * The main interaction processing loop
 * This function is called from the playlist loop
zorglub's avatar
zorglub committed
77
 *
zorglub's avatar
zorglub committed
78
79
80
81
82
 * \param p_playlist the parent playlist
 * \return nothing
 */
void intf_InteractionManage( playlist_t *p_playlist )
{
83
    vlc_value_t val;
zorglub's avatar
zorglub committed
84
    int i_index;
85
    interaction_t *p_interaction = p_playlist->p_interaction;
zorglub's avatar
zorglub committed
86
87
88
89
90
91

    // Nothing to do
    if( p_interaction->i_dialogs == 0 ) return;

    vlc_mutex_lock( &p_interaction->object_lock );

92
    InteractionSearchInterface( p_interaction );
zorglub's avatar
zorglub committed
93
    if( !p_interaction->p_intf )
94
    {
zorglub's avatar
zorglub committed
95
96
97
98
        // We mark all dialogs as answered with their "default" answer
        for( i_index = 0 ; i_index < p_interaction->i_dialogs; i_index ++ )
        {
            interaction_dialog_t *p_dialog = p_interaction->pp_dialogs[i_index];
99
            p_dialog->i_return = DIALOG_DEFAULT; // Give default answer
zorglub's avatar
zorglub committed
100
101
102
103
104
105
106
107
108
109

            // Pretend we have hidden and destroyed it
            if( p_dialog->i_status == HIDDEN_DIALOG )
            {
                p_dialog->i_status = DESTROYED_DIALOG;
            }
            else
            {
                p_dialog->i_status = HIDING_DIALOG;
            }
zorglub's avatar
zorglub committed
110
111
112
113
114
        }
    }
    else
    {
        vlc_object_yield( p_interaction->p_intf );
115
    }
zorglub's avatar
zorglub committed
116
117

    for( i_index = 0 ; i_index < p_interaction->i_dialogs; i_index ++ )
118
    {
zorglub's avatar
zorglub committed
119
        interaction_dialog_t *p_dialog = p_interaction->pp_dialogs[i_index];
zorglub's avatar
zorglub committed
120
        switch( p_dialog->i_status )
zorglub's avatar
zorglub committed
121
        {
zorglub's avatar
zorglub committed
122
        case ANSWERED_DIALOG:
zorglub's avatar
zorglub committed
123
            // Ask interface to hide it
124
125
            p_dialog->i_action = INTERACT_HIDE;
            val.p_address = p_dialog;
zorglub's avatar
zorglub committed
126
127
            if( p_interaction->p_intf )
                var_Set( p_interaction->p_intf, "interaction", val );
zorglub's avatar
zorglub committed
128
129
130
            p_dialog->i_status = HIDING_DIALOG;
            break;
        case UPDATED_DIALOG:
131
132
            p_dialog->i_action = INTERACT_UPDATE;
            val.p_address = p_dialog;
zorglub's avatar
zorglub committed
133
134
            if( p_interaction->p_intf )
                var_Set( p_interaction->p_intf, "interaction", val );
zorglub's avatar
zorglub committed
135
136
137
            p_dialog->i_status = SENT_DIALOG;
            break;
        case HIDDEN_DIALOG:
zorglub's avatar
zorglub committed
138
            if( !(p_dialog->i_flags & DIALOG_GOT_ANSWER) ) break;
zorglub's avatar
zorglub committed
139
140
141
142
            p_dialog->i_action = INTERACT_DESTROY;
            val.p_address = p_dialog;
            if( p_interaction->p_intf )
                var_Set( p_interaction->p_intf, "interaction", val );
zorglub's avatar
zorglub committed
143
            break;
zorglub's avatar
zorglub committed
144
145
        case DESTROYED_DIALOG:
            // Interface has now destroyed it, remove it
zorglub's avatar
zorglub committed
146
147
148
            REMOVE_ELEM( p_interaction->pp_dialogs, p_interaction->i_dialogs,
                         i_index);
            i_index--;
149
            DialogDestroy( p_dialog );
zorglub's avatar
zorglub committed
150
            break;
zorglub's avatar
zorglub committed
151
152
        case NEW_DIALOG:
            // This is truly a new dialog, send it.
153
154
            p_dialog->i_action = INTERACT_NEW;
            val.p_address = p_dialog;
zorglub's avatar
zorglub committed
155
156
            if( p_interaction->p_intf )
                var_Set( p_interaction->p_intf, "interaction", val );
zorglub's avatar
zorglub committed
157
158
            p_dialog->i_status = SENT_DIALOG;
            break;
zorglub's avatar
zorglub committed
159
        }
160
    }
zorglub's avatar
zorglub committed
161

zorglub's avatar
zorglub committed
162
163
164
165
    if( p_interaction->p_intf )
    {
        vlc_object_release( p_interaction->p_intf );
    }
zorglub's avatar
zorglub committed
166
167

    vlc_mutex_unlock( &p_playlist->p_interaction->object_lock );
168
169
}

170
171
172
173
174
#define DIALOG_INIT( type ) \
        DECMALLOC_ERR( p_new, interaction_dialog_t );                     \
        memset( p_new, 0, sizeof( interaction_dialog_t ) );               \
        p_new->b_cancelled = VLC_FALSE;                                   \
        p_new->i_status = NEW_DIALOG;                                     \
zorglub's avatar
zorglub committed
175
        p_new->i_flags = 0; \
176
177
178
179
180
181
182
183
184
185
        p_new->i_type = INTERACT_DIALOG_##type;                           \
        p_new->psz_returned[0] = NULL;                                    \
        p_new->psz_returned[1] = NULL;

#define FORMAT_DESC \
        va_start( args, psz_format ); \
        vasprintf( &p_new->psz_description, psz_format, args ); \
        va_end( args );

/** Send an error message, both in a blocking and non-blocking way
zorglub's avatar
zorglub committed
186
 *  \param p_this     Parent vlc_object
187
 *  \param b_blocking Is this dialog blocking or not?
zorglub's avatar
zorglub committed
188
189
190
 *  \param psz_title  Title for the dialog
 *  \param psz_format The message to display
 *  */
191
int __intf_UserFatal( vlc_object_t *p_this, vlc_bool_t b_blocking,
zorglub's avatar
zorglub committed
192
193
                       const char *psz_title,
                       const char *psz_format, ... )
194
{
zorglub's avatar
zorglub committed
195
    va_list args;
196
    DIALOG_INIT( ONEWAY );
zorglub's avatar
zorglub committed
197

198
    p_new->psz_title = strdup( psz_title );
199
    FORMAT_DESC;
200
201
202

    if( b_blocking )
        p_new->i_flags = DIALOG_BLOCKING_ERROR;
zorglub's avatar
zorglub committed
203
    else
204
        p_new->i_flags = DIALOG_NONBLOCKING_ERROR;
zorglub's avatar
zorglub committed
205

206
    return DialogSend( p_this, p_new );
207
}
zorglub's avatar
zorglub committed
208

209
210
211
212
213
/** Helper function to send an warning, which is always shown non-blocking
 *  \param p_this     Parent vlc_object
 *  \param psz_title  Title for the dialog
 *  \param psz_format The message to display
 *  */
214
215
216
int __intf_UserWarn( vlc_object_t *p_this,
                     const char *psz_title,
                     const char *psz_format, ... )
217
218
{
    va_list args;
219
    DIALOG_INIT( ONEWAY );
zorglub's avatar
zorglub committed
220

zorglub's avatar
zorglub committed
221
    p_new->psz_title = strdup( psz_title );
222
    FORMAT_DESC
zorglub's avatar
zorglub committed
223

224
    p_new->i_flags = DIALOG_WARNING;
225

226
    return DialogSend( p_this, p_new );
227
228
}

229
/** Helper function to ask a yes-no-cancel question
zorglub's avatar
zorglub committed
230
231
232
 *  \param p_this           Parent vlc_object
 *  \param psz_title        Title for the dialog
 *  \param psz_description  A description
233
234
235
 *  \param psz_default      caption for the default button
 *  \param psz_alternate    caption for the alternate button
 *  \param psz_other        caption for the optional 3rd button (== cancel)
236
237
238
239
 *  \return                 Clicked button code
 */
int __intf_UserYesNo( vlc_object_t *p_this,
                      const char *psz_title,
240
241
242
243
                      const char *psz_description,
                      const char *psz_default,
                      const char *psz_alternate,
                      const char *psz_other )
244
{
245
    DIALOG_INIT( TWOWAY );
246
247

    p_new->psz_title = strdup( psz_title );
248
249
    p_new->psz_description = strdup( psz_description );
    p_new->i_flags = DIALOG_YES_NO_CANCEL;
250
251
    p_new->psz_default_button = strdup( psz_default );
    p_new->psz_alternate_button = strdup( psz_alternate );
252
    if( psz_other )
253
        p_new->psz_other_button = strdup( psz_other );
254

255
    return DialogSend( p_this, p_new );
256
257
}

258
/** Helper function to create a dialogue showing a progress-bar with some info
zorglub's avatar
zorglub committed
259
 *  \param p_this           Parent vlc_object
260
 *  \param psz_title        Title for the dialog (NULL implies main intf )
zorglub's avatar
zorglub committed
261
262
 *  \param psz_status       Current status
 *  \param f_position       Current position (0.0->100.0)
263
 *  \param i_timeToGo       Time (in sec) to go until process is finished
zorglub's avatar
zorglub committed
264
265
 *  \return                 Dialog id, to give to UserProgressUpdate
 */
266
267
int __intf_Progress( vlc_object_t *p_this, const char *psz_title,
                     const char *psz_status, float f_pos, int i_time )
zorglub's avatar
zorglub committed
268
{
269
    DIALOG_INIT( ONEWAY );
270
271
    p_new->psz_description = strdup( psz_status );
    p_new->val.f_float = f_pos;
272
    p_new->i_timeToGo = i_time;
zorglub's avatar
zorglub committed
273

274
275
276
277
278
279
280
    if( psz_title )
    {
        p_new->psz_title = strdup( psz_title );
        p_new->i_flags = DIALOG_USER_PROGRESS;
    }
    else
        p_new->i_flags = DIALOG_INTF_PROGRESS;
zorglub's avatar
zorglub committed
281

282
    DialogSend( p_this, p_new );
zorglub's avatar
zorglub committed
283
284
    return p_new->i_id;
}
dionoea's avatar
dionoea committed
285

286
/** Update a progress bar in a dialogue
zorglub's avatar
zorglub committed
287
288
289
290
 *  \param p_this           Parent vlc_object
 *  \param i_id             Identifier of the dialog
 *  \param psz_status       New status
 *  \param f_position       New position (0.0->100.0)
291
 *  \param i_timeToGo       Time (in sec) to go until process is finished
zorglub's avatar
zorglub committed
292
293
 *  \return                 nothing
 */
294
295
void __intf_ProgressUpdate( vlc_object_t *p_this, int i_id,
                            const char *psz_status, float f_pos, int i_time )
zorglub's avatar
zorglub committed
296
{
297
    interaction_t *p_interaction = InteractionGet( p_this );
zorglub's avatar
zorglub committed
298
299
300
301
302
    interaction_dialog_t *p_dialog;

    if( !p_interaction ) return;

    vlc_mutex_lock( &p_interaction->object_lock );
303
    p_dialog  =  DialogGetById( p_interaction, i_id );
zorglub's avatar
zorglub committed
304

zorglub's avatar
zorglub committed
305
    if( !p_dialog )
zorglub's avatar
Fix no    
zorglub committed
306
    {
Christophe Mutricy's avatar
Typo    
Christophe Mutricy committed
307
        vlc_mutex_unlock( &p_interaction->object_lock ) ;
zorglub's avatar
Fix no    
zorglub committed
308
309
        return;
    }
zorglub's avatar
zorglub committed
310

311
    FREE( p_dialog->psz_description );
312
    p_dialog->psz_description = strdup( psz_status );
zorglub's avatar
zorglub committed
313

314
    p_dialog->val.f_float = f_pos;
315
    p_dialog->i_timeToGo = i_time;
zorglub's avatar
zorglub committed
316
317
318
319
320

    p_dialog->i_status = UPDATED_DIALOG;
    vlc_mutex_unlock( &p_interaction->object_lock) ;
}

321
322
/** Helper function to communicate dialogue cancellations between the
 *  interface module and the caller
323
324
325
326
327
328
 *  \param p_this           Parent vlc_object
 *  \param i_id             Identifier of the dialogue
 *  \return                 Either true or false
 */
vlc_bool_t __intf_UserProgressIsCancelled( vlc_object_t *p_this, int i_id )
{
329
    interaction_t *p_interaction = InteractionGet( p_this );
330
    interaction_dialog_t *p_dialog;
331
    vlc_bool_t b_cancel;
332
333
334
335

    if( !p_interaction ) return VLC_TRUE;

    vlc_mutex_lock( &p_interaction->object_lock );
336
    p_dialog  =  DialogGetById( p_interaction, i_id );
337
338
339
340
341
342
    if( !p_dialog )
    {
        vlc_mutex_unlock( &p_interaction->object_lock ) ;
        return VLC_TRUE;
    }

343
344
345
    b_cancel = p_dialog->b_cancelled;
    vlc_mutex_unlock( &p_interaction->object_lock );
    return b_cancel;
346
347
}

348
/** Helper function to make a login/password dialogue
349
350
351
 *  \param p_this           Parent vlc_object
 *  \param psz_title        Title for the dialog
 *  \param psz_description  A description
zorglub's avatar
zorglub committed
352
353
 *  \param ppsz_login       Returned login
 *  \param ppsz_password    Returned password
354
 *  \return                 Clicked button code
zorglub's avatar
zorglub committed
355
 */
zorglub's avatar
zorglub committed
356
357
358
359
360
int __intf_UserLoginPassword( vlc_object_t *p_this,
                              const char *psz_title,
                              const char *psz_description,
                              char **ppsz_login,
                              char **ppsz_password )
361
{
zorglub's avatar
zorglub committed
362
    int i_ret;
363
    DIALOG_INIT( TWOWAY );
zorglub's avatar
zorglub committed
364
365
    p_new->i_type = INTERACT_DIALOG_TWOWAY;
    p_new->psz_title = strdup( psz_title );
366
    p_new->psz_description = strdup( psz_description );
zorglub's avatar
zorglub committed
367

368
    p_new->i_flags = DIALOG_LOGIN_PW_OK_CANCEL;
zorglub's avatar
zorglub committed
369

370
    i_ret = DialogSend( p_this, p_new );
zorglub's avatar
zorglub committed
371

zorglub's avatar
zorglub committed
372
    if( i_ret != DIALOG_CANCELLED )
zorglub's avatar
zorglub committed
373
    {
374
        assert( p_new->psz_returned[0] && p_new->psz_returned[1] );
375
376
        *ppsz_login = strdup( p_new->psz_returned[0] );
        *ppsz_password = strdup( p_new->psz_returned[1] );
zorglub's avatar
zorglub committed
377
378
379
    }
    return i_ret;
}
zorglub's avatar
zorglub committed
380

381
382
383
384
385
386
387
388
389
390
391
392
393
/** Helper function to make a dialogue asking the user for !password string
 *  \param p_this           Parent vlc_object
 *  \param psz_title        Title for the dialog
 *  \param psz_description  A description
 *  \param ppsz_usersString Returned login
 *  \return                 Clicked button code
 */
int __intf_UserStringInput( vlc_object_t *p_this,
                              const char *psz_title,
                              const char *psz_description,
                              char **ppsz_usersString )
{
    int i_ret;
394
    DIALOG_INIT( TWOWAY );
395
396
397
398
399
    p_new->psz_title = strdup( psz_title );
    p_new->psz_description = strdup( psz_description );

    p_new->i_flags = DIALOG_PSZ_INPUT_OK_CANCEL;

400
    i_ret = DialogSend( p_this, p_new );
401
402
403

    if( i_ret != DIALOG_CANCELLED )
    {
404
        assert( p_new->psz_returned[0] );
405
406
407
408
409
        *ppsz_usersString = strdup( p_new->psz_returned[0] );
    }
    return i_ret;
}

zorglub's avatar
zorglub committed
410
411
412
413
414
415
416
/** Hide an interaction dialog
 * \param p_this the parent vlc object
 * \param i_id the id of the item to hide
 * \return nothing
 */
void __intf_UserHide( vlc_object_t *p_this, int i_id )
{
417
    interaction_t *p_interaction = InteractionGet( p_this );
zorglub's avatar
zorglub committed
418
    interaction_dialog_t *p_dialog;
419

zorglub's avatar
zorglub committed
420
421
422
    if( !p_interaction ) return;

    vlc_mutex_lock( &p_interaction->object_lock );
423
    p_dialog = DialogGetById( p_interaction, i_id );
zorglub's avatar
zorglub committed
424

zorglub's avatar
zorglub committed
425
    if( !p_dialog )
426
427
428
429
    {
       vlc_mutex_unlock( &p_interaction->object_lock );
       return;
    }
zorglub's avatar
zorglub committed
430
431
432
433

    p_dialog->i_status = ANSWERED_DIALOG;
    vlc_mutex_unlock( &p_interaction->object_lock );
}
434

zorglub's avatar
zorglub committed
435
436
437
438
439
/**********************************************************************
 * The following functions are local
 **********************************************************************/

/* Get the interaction object. Create it if needed */
440
static interaction_t * InteractionGet( vlc_object_t *p_this )
441
{
zorglub's avatar
zorglub committed
442
443
444
445
446
447
448
449
    playlist_t *p_playlist;
    interaction_t *p_interaction;

    p_playlist = (playlist_t*) vlc_object_find( p_this, VLC_OBJECT_PLAYLIST,
                                                FIND_ANYWHERE );
    if( !p_playlist )
        return NULL;

450
    if( p_playlist->p_interaction == NULL )
451
       InteractionInit( p_playlist );
452

zorglub's avatar
zorglub committed
453
    p_interaction = p_playlist->p_interaction;
454

zorglub's avatar
zorglub committed
455
456
    vlc_object_release( p_playlist );
    return p_interaction;
457
458
}

zorglub's avatar
zorglub committed
459
/* Create the interaction object in the given playlist object */
460
static void InteractionInit( playlist_t *p_playlist )
461
{
462
463
    interaction_t *p_interaction = vlc_object_create( VLC_OBJECT( p_playlist ),
                                                      sizeof( interaction_t ) );
464
465
466
467
468
469
470
471
    if( !p_interaction )
    {
        msg_Err( p_playlist,"out of memory" );
        return;
    }

    p_interaction->i_dialogs = 0;
    p_interaction->pp_dialogs = NULL;
zorglub's avatar
zorglub committed
472
473
474
475
476
477
    p_interaction->p_intf = NULL;
    p_interaction->i_last_id = DIALOG_LAST_PREDEFINED + 1;

    vlc_mutex_init( p_interaction , &p_interaction->object_lock );

    p_playlist->p_interaction  = p_interaction;
478
479
}

zorglub's avatar
zorglub committed
480
/* Look for an interface suitable for interaction */
481
static void InteractionSearchInterface( interaction_t *p_interaction )
482
{
zorglub's avatar
zorglub committed
483
484
    vlc_list_t  *p_list;
    int          i_index;
485

zorglub's avatar
zorglub committed
486
    p_interaction->p_intf = NULL;
487

zorglub's avatar
zorglub committed
488
489
490
    p_list = vlc_list_find( p_interaction, VLC_OBJECT_INTF, FIND_ANYWHERE );
    if( !p_list )
    {
zorglub's avatar
zorglub committed
491
        msg_Err( p_interaction, "unable to create module list" );
zorglub's avatar
zorglub committed
492
493
        return;
    }
494

zorglub's avatar
zorglub committed
495
496
497
498
    for( i_index = 0; i_index < p_list->i_count; i_index ++ )
    {
        intf_thread_t *p_intf = (intf_thread_t *)
                                        p_list->p_values[i_index].p_object;
499
        if( p_intf->b_interaction )
zorglub's avatar
zorglub committed
500
501
502
503
504
505
506
        {
            p_interaction->p_intf = p_intf;
            break;
        }
    }
    vlc_list_release ( p_list );
}
507

508
509
510
/* Find an interaction dialog by its id */
static interaction_dialog_t *DialogGetById( interaction_t *p_interaction,
                                            int i_id )
511
{
zorglub's avatar
zorglub committed
512
    int i;
513
    for( i = 0 ; i< p_interaction->i_dialogs; i++ )
zorglub's avatar
zorglub committed
514
    {
515
516
        if( p_interaction->pp_dialogs[i]->i_id == i_id )
            return p_interaction->pp_dialogs[i];
zorglub's avatar
zorglub committed
517
    }
518
    return NULL;
zorglub's avatar
zorglub committed
519
}
520

521
522
/* Destroy a dialog */
static void DialogDestroy( interaction_dialog_t *p_dialog )
zorglub's avatar
zorglub committed
523
{
524
525
526
527
528
529
    FREENULL( p_dialog->psz_title );
    FREENULL( p_dialog->psz_description );
    FREENULL( p_dialog->psz_default_button );
    FREENULL( p_dialog->psz_alternate_button );
    FREENULL( p_dialog->psz_other_button );
    free( p_dialog );
zorglub's avatar
zorglub committed
530
}
531

532
533
534
/* Ask for the dialog to be sent to the user. Wait for answer
 * if required */
static int DialogSend( vlc_object_t *p_this, interaction_dialog_t *p_dialog )
zorglub's avatar
zorglub committed
535
{
536
    interaction_t *p_interaction = InteractionGet( p_this );
537

538
539
540
    /* Get an id, if we don't already have one */
    if( p_dialog->i_id == 0 )
        p_dialog->i_id = ++p_interaction->i_last_id;
zorglub's avatar
zorglub committed
541

542
543
    if( p_this->i_flags & OBJECT_FLAGS_NOINTERACT ) return VLC_EGENERIC;

zorglub's avatar
zorglub committed
544
    if( config_GetInt(p_this, "interact") ||
545
546
        p_dialog->i_flags & DIALOG_BLOCKING_ERROR ||
        p_dialog->i_flags & DIALOG_NONBLOCKING_ERROR )
zorglub's avatar
zorglub committed
547
    {
548
549
550
551
552
553
554
555
        vlc_bool_t b_found = VLC_FALSE;
        int i;
        p_dialog->p_interaction = p_interaction;
        p_dialog->p_parent = p_this;

        /* Check if we have already added this dialog */
        vlc_mutex_lock( &p_interaction->object_lock );
        for( i = 0 ; i< p_interaction->i_dialogs; i++ )
zorglub's avatar
zorglub committed
556
        {
557
558
            if( p_interaction->pp_dialogs[i]->i_id == p_dialog->i_id )
                b_found = VLC_TRUE;
zorglub's avatar
zorglub committed
559
        }
560
561
562
563
564
565
566
567
568
569
570
        /* Add it to the queue, the main loop will send the orders to the
         * interface */
        if( ! b_found )
        {
            INSERT_ELEM( p_interaction->pp_dialogs,
                         p_interaction->i_dialogs,
                         p_interaction->i_dialogs,
                         p_dialog );
        }
        else
            p_dialog->i_status = UPDATED_DIALOG;
zorglub's avatar
zorglub committed
571

572
573
574
575
576
577
578
        if( p_dialog->i_type == INTERACT_DIALOG_TWOWAY ) // Wait for answer
        {
            while( p_dialog->i_status != ANSWERED_DIALOG &&
                   p_dialog->i_status != HIDING_DIALOG &&
                   p_dialog->i_status != HIDDEN_DIALOG &&
                   !p_dialog->p_parent->b_die )
            {
zorglub's avatar
zorglub committed
579
                vlc_mutex_unlock( &p_interaction->object_lock );
580
                msleep( 100000 );
zorglub's avatar
zorglub committed
581
                vlc_mutex_lock( &p_interaction->object_lock );
582
583
584
585
586
587
588
            }
            if( p_dialog->p_parent->b_die )
            {
                p_dialog->i_return = DIALOG_CANCELLED;
                p_dialog->i_status = ANSWERED_DIALOG;
            }
            p_dialog->i_flags |= DIALOG_GOT_ANSWER;
zorglub's avatar
zorglub committed
589
            vlc_mutex_unlock( &p_interaction->object_lock );
590
591
592
593
594
595
596
597
598
599
600
601
            return p_dialog->i_return;
        }
        else
        {
            // Pretend we already retrieved the "answer"
            p_dialog->i_flags |=  DIALOG_GOT_ANSWER;
            vlc_mutex_unlock( &p_interaction->object_lock );
            return VLC_SUCCESS;
        }
    }
    else
        return VLC_EGENERIC;
zorglub's avatar
zorglub committed
602
}