]> git.sesse.net Git - vlc/blob - src/interface/interaction.c
Don't loop continuously the playlist thread, use cond.
[vlc] / src / interface / interaction.c
1 /*****************************************************************************
2  * interaction.c: User interaction functions
3  *****************************************************************************
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  * This program is free software; you can redistribute it and/or modify
11  * it under the terms of the GNU General Public License as published by
12  * the Free Software Foundation; either version 2 of the License, or
13  * (at your option) any later version.
14  *
15  * This program is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU General Public License for more details.
19  *
20  * You should have received a copy of the GNU General Public License
21  * along with this program; if not, write to the Free Software
22  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
23  *****************************************************************************/
24
25 /**
26  *   \file
27  *   This file contains functions related to user interaction management
28  */
29
30 /*****************************************************************************
31  * Preamble
32  *****************************************************************************/
33
34 #include <vlc/vlc.h>
35
36 #include <stdlib.h>                                      /* free(), strtol() */
37 #include <stdio.h>                                                   /* FILE */
38 #include <string.h>                                            /* strerror() */
39
40 #include <vlc/input.h>
41 #include <assert.h>
42
43 #include "vlc_interaction.h"
44 #include "vlc_interface.h"
45 #include "vlc_playlist.h"
46
47 /*****************************************************************************
48  * Local prototypes
49  *****************************************************************************/
50 static void                  InteractionInit( playlist_t *p_playlist );
51 static interaction_t *       InteractionGet( vlc_object_t *p_this );
52 static void                  InteractionSearchInterface( interaction_t *
53                                                           p_interaction );
54 static interaction_dialog_t *DialogGetById( interaction_t* , int );
55 static void                  DialogDestroy( interaction_dialog_t *p_dialog );
56 static int DialogSend( vlc_object_t *p_this, interaction_dialog_t *p_dialog );
57
58 /**
59  * Destroy the interaction system
60  * \param The interaction object to destroy
61  * \return nothing
62  */
63 void intf_InteractionDestroy( interaction_t *p_interaction )
64 {
65     int i;
66     // Remove all dialogs - Interfaces must be able to clean up their data
67     for( i = p_interaction->i_dialogs -1 ; i >= 0; i-- )
68     {
69         interaction_dialog_t * p_dialog = p_interaction->pp_dialogs[i];
70         DialogDestroy( p_dialog );
71         REMOVE_ELEM( p_interaction->pp_dialogs, p_interaction->i_dialogs, i );
72     }
73     vlc_object_destroy( p_interaction );
74 }
75
76 /**
77  * The main interaction processing loop
78  * This function is called from the playlist loop
79  *
80  * \param p_playlist the parent playlist
81  * \return nothing
82  */
83 void intf_InteractionManage( playlist_t *p_playlist )
84 {
85     vlc_value_t val;
86     int i_index;
87     interaction_t *p_interaction = p_playlist->p_interaction;
88
89     // Nothing to do
90     if( p_interaction->i_dialogs == 0 ) return;
91
92     vlc_mutex_lock( &p_interaction->object_lock );
93
94     InteractionSearchInterface( p_interaction );
95     if( !p_interaction->p_intf )
96     {
97         // We mark all dialogs as answered with their "default" answer
98         for( i_index = 0 ; i_index < p_interaction->i_dialogs; i_index ++ )
99         {
100             interaction_dialog_t *p_dialog = p_interaction->pp_dialogs[i_index];
101             p_dialog->i_return = DIALOG_DEFAULT; // Give default answer
102
103             // Pretend we have hidden and destroyed it
104             if( p_dialog->i_status == HIDDEN_DIALOG )
105                 p_dialog->i_status = DESTROYED_DIALOG;
106             else
107                 p_dialog->i_status = HIDING_DIALOG;
108         }
109     }
110     else
111         vlc_object_yield( p_interaction->p_intf );
112
113     for( i_index = 0 ; i_index < p_interaction->i_dialogs; i_index ++ )
114     {
115         interaction_dialog_t *p_dialog = p_interaction->pp_dialogs[i_index];
116         switch( p_dialog->i_status )
117         {
118         case ANSWERED_DIALOG:
119             // Ask interface to hide it
120             p_dialog->i_action = INTERACT_HIDE;
121             val.p_address = p_dialog;
122             if( p_interaction->p_intf )
123                 var_Set( p_interaction->p_intf, "interaction", val );
124             p_dialog->i_status = HIDING_DIALOG;
125             break;
126         case UPDATED_DIALOG:
127             p_dialog->i_action = INTERACT_UPDATE;
128             val.p_address = p_dialog;
129             if( p_interaction->p_intf )
130                 var_Set( p_interaction->p_intf, "interaction", val );
131             p_dialog->i_status = SENT_DIALOG;
132             break;
133         case HIDDEN_DIALOG:
134             if( !(p_dialog->i_flags & DIALOG_GOT_ANSWER) ) break;
135             p_dialog->i_action = INTERACT_DESTROY;
136             val.p_address = p_dialog;
137             if( p_interaction->p_intf )
138                 var_Set( p_interaction->p_intf, "interaction", val );
139             break;
140         case DESTROYED_DIALOG:
141             // Interface has now destroyed it, remove it
142             REMOVE_ELEM( p_interaction->pp_dialogs, p_interaction->i_dialogs,
143                          i_index);
144             i_index--;
145             DialogDestroy( p_dialog );
146             break;
147         case NEW_DIALOG:
148             // This is truly a new dialog, send it.
149             p_dialog->i_action = INTERACT_NEW;
150             val.p_address = p_dialog;
151             if( p_interaction->p_intf )
152                 var_Set( p_interaction->p_intf, "interaction", val );
153             p_dialog->i_status = SENT_DIALOG;
154             break;
155         }
156     }
157
158     if( p_interaction->p_intf )
159     {
160         vlc_object_release( p_interaction->p_intf );
161     }
162
163     vlc_mutex_unlock( &p_playlist->p_interaction->object_lock );
164 }
165
166 #define DIALOG_INIT( type ) \
167         DECMALLOC_ERR( p_new, interaction_dialog_t );                     \
168         memset( p_new, 0, sizeof( interaction_dialog_t ) );               \
169         p_new->b_cancelled = VLC_FALSE;                                   \
170         p_new->i_status = NEW_DIALOG;                                     \
171         p_new->i_flags = 0; \
172         p_new->i_type = INTERACT_DIALOG_##type;                           \
173         p_new->psz_returned[0] = NULL;                                    \
174         p_new->psz_returned[1] = NULL;
175
176 #define FORMAT_DESC \
177         va_start( args, psz_format ); \
178         vasprintf( &p_new->psz_description, psz_format, args ); \
179         va_end( args );
180
181 /** Send an error message, both in a blocking and non-blocking way
182  *  \param p_this     Parent vlc_object
183  *  \param b_blocking Is this dialog blocking or not?
184  *  \param psz_title  Title for the dialog
185  *  \param psz_format The message to display
186  *  */
187 int __intf_UserFatal( vlc_object_t *p_this, vlc_bool_t b_blocking,
188                        const char *psz_title,
189                        const char *psz_format, ... )
190 {
191     va_list args;
192     DIALOG_INIT( ONEWAY );
193
194     p_new->psz_title = strdup( psz_title );
195     FORMAT_DESC;
196
197     if( b_blocking )
198         p_new->i_flags = DIALOG_BLOCKING_ERROR;
199     else
200         p_new->i_flags = DIALOG_NONBLOCKING_ERROR;
201
202     return DialogSend( p_this, p_new );
203 }
204
205 /** Helper function to send an warning, which is always shown non-blocking
206  *  \param p_this     Parent vlc_object
207  *  \param psz_title  Title for the dialog
208  *  \param psz_format The message to display
209  *  */
210 int __intf_UserWarn( vlc_object_t *p_this,
211                      const char *psz_title,
212                      const char *psz_format, ... )
213 {
214     va_list args;
215     DIALOG_INIT( ONEWAY );
216
217     p_new->psz_title = strdup( psz_title );
218     FORMAT_DESC
219
220     p_new->i_flags = DIALOG_WARNING;
221
222     return DialogSend( p_this, p_new );
223 }
224
225 /** Helper function to ask a yes-no-cancel question
226  *  \param p_this           Parent vlc_object
227  *  \param psz_title        Title for the dialog
228  *  \param psz_description  A description
229  *  \param psz_default      caption for the default button
230  *  \param psz_alternate    caption for the alternate button
231  *  \param psz_other        caption for the optional 3rd button (== cancel)
232  *  \return                 Clicked button code
233  */
234 int __intf_UserYesNo( vlc_object_t *p_this,
235                       const char *psz_title,
236                       const char *psz_description,
237                       const char *psz_default,
238                       const char *psz_alternate,
239                       const char *psz_other )
240 {
241     DIALOG_INIT( TWOWAY );
242
243     p_new->psz_title = strdup( psz_title );
244     p_new->psz_description = strdup( psz_description );
245     p_new->i_flags = DIALOG_YES_NO_CANCEL;
246     p_new->psz_default_button = strdup( psz_default );
247     p_new->psz_alternate_button = strdup( psz_alternate );
248     if( psz_other )
249         p_new->psz_other_button = strdup( psz_other );
250
251     return DialogSend( p_this, p_new );
252 }
253
254 /** Helper function to create a dialogue showing a progress-bar with some info
255  *  \param p_this           Parent vlc_object
256  *  \param psz_title        Title for the dialog (NULL implies main intf )
257  *  \param psz_status       Current status
258  *  \param f_position       Current position (0.0->100.0)
259  *  \param i_timeToGo       Time (in sec) to go until process is finished
260  *  \return                 Dialog id, to give to UserProgressUpdate
261  */
262 int __intf_Progress( vlc_object_t *p_this, const char *psz_title,
263                      const char *psz_status, float f_pos, int i_time )
264 {
265     DIALOG_INIT( ONEWAY );
266     p_new->psz_description = strdup( psz_status );
267     p_new->val.f_float = f_pos;
268     p_new->i_timeToGo = i_time;
269     p_new->psz_alternate_button = strdup( _("Cancel" ) );
270
271     if( psz_title )
272     {
273         p_new->psz_title = strdup( psz_title );
274         p_new->i_flags = DIALOG_USER_PROGRESS;
275     }
276     else
277         p_new->i_flags = DIALOG_INTF_PROGRESS;
278
279     DialogSend( p_this, p_new );
280     return p_new->i_id;
281 }
282
283 /** Update a progress bar in a dialogue
284  *  \param p_this           Parent vlc_object
285  *  \param i_id             Identifier of the dialog
286  *  \param psz_status       New status
287  *  \param f_position       New position (0.0->100.0)
288  *  \param i_timeToGo       Time (in sec) to go until process is finished
289  *  \return                 nothing
290  */
291 void __intf_ProgressUpdate( vlc_object_t *p_this, int i_id,
292                             const char *psz_status, float f_pos, int i_time )
293 {
294     interaction_t *p_interaction = InteractionGet( p_this );
295     interaction_dialog_t *p_dialog;
296
297     if( !p_interaction ) return;
298
299     vlc_mutex_lock( &p_interaction->object_lock );
300     p_dialog  =  DialogGetById( p_interaction, i_id );
301
302     if( !p_dialog )
303     {
304         vlc_mutex_unlock( &p_interaction->object_lock ) ;
305         return;
306     }
307
308     FREE( p_dialog->psz_description );
309     p_dialog->psz_description = strdup( psz_status );
310
311     p_dialog->val.f_float = f_pos;
312     p_dialog->i_timeToGo = i_time;
313
314     p_dialog->i_status = UPDATED_DIALOG;
315     vlc_mutex_unlock( &p_interaction->object_lock) ;
316 }
317
318 /** Helper function to communicate dialogue cancellations between the
319  *  interface module and the caller
320  *  \param p_this           Parent vlc_object
321  *  \param i_id             Identifier of the dialogue
322  *  \return                 Either true or false
323  */
324 vlc_bool_t __intf_UserProgressIsCancelled( vlc_object_t *p_this, int i_id )
325 {
326     interaction_t *p_interaction = InteractionGet( p_this );
327     interaction_dialog_t *p_dialog;
328     vlc_bool_t b_cancel;
329
330     if( !p_interaction ) return VLC_TRUE;
331
332     vlc_mutex_lock( &p_interaction->object_lock );
333     p_dialog  =  DialogGetById( p_interaction, i_id );
334     if( !p_dialog )
335     {
336         vlc_mutex_unlock( &p_interaction->object_lock ) ;
337         return VLC_TRUE;
338     }
339
340     b_cancel = p_dialog->b_cancelled;
341     vlc_mutex_unlock( &p_interaction->object_lock );
342     return b_cancel;
343 }
344
345 /** Helper function to make a login/password dialogue
346  *  \param p_this           Parent vlc_object
347  *  \param psz_title        Title for the dialog
348  *  \param psz_description  A description
349  *  \param ppsz_login       Returned login
350  *  \param ppsz_password    Returned password
351  *  \return                 Clicked button code
352  */
353 int __intf_UserLoginPassword( vlc_object_t *p_this,
354                               const char *psz_title,
355                               const char *psz_description,
356                               char **ppsz_login,
357                               char **ppsz_password )
358 {
359     int i_ret;
360     DIALOG_INIT( TWOWAY );
361     p_new->i_type = INTERACT_DIALOG_TWOWAY;
362     p_new->psz_title = strdup( psz_title );
363     p_new->psz_description = strdup( psz_description );
364     p_new->psz_default_button = strdup( _("Ok" ) );
365     p_new->psz_alternate_button = strdup( _("Cancel" ) );
366
367     p_new->i_flags = DIALOG_LOGIN_PW_OK_CANCEL;
368
369     i_ret = DialogSend( p_this, p_new );
370
371     if( i_ret != DIALOG_CANCELLED && i_ret != VLC_EGENERIC )
372     {
373         *ppsz_login = p_new->psz_returned[0]?
374                                 strdup( p_new->psz_returned[0] ) : NULL;
375         *ppsz_password = p_new->psz_returned[1]?
376                                 strdup( p_new->psz_returned[1] ) : NULL;
377     }
378     return i_ret;
379 }
380
381 /** Helper function to make a dialogue asking the user for !password string
382  *  \param p_this           Parent vlc_object
383  *  \param psz_title        Title for the dialog
384  *  \param psz_description  A description
385  *  \param ppsz_usersString Returned login
386  *  \return                 Clicked button code
387  */
388 int __intf_UserStringInput( vlc_object_t *p_this,
389                               const char *psz_title,
390                               const char *psz_description,
391                               char **ppsz_usersString )
392 {
393     int i_ret;
394     DIALOG_INIT( TWOWAY );
395     p_new->i_type = INTERACT_DIALOG_TWOWAY;
396     p_new->psz_title = strdup( psz_title );
397     p_new->psz_description = strdup( psz_description );
398
399     p_new->i_flags = DIALOG_PSZ_INPUT_OK_CANCEL;
400
401     i_ret = DialogSend( p_this, p_new );
402
403     if( i_ret != DIALOG_CANCELLED )
404     {
405         *ppsz_usersString = p_new->psz_returned[0]?
406                                     strdup( p_new->psz_returned[0] ) : NULL;
407     }
408     return i_ret;
409 }
410
411 /** Hide an interaction dialog
412  * \param p_this the parent vlc object
413  * \param i_id the id of the item to hide
414  * \return nothing
415  */
416 void __intf_UserHide( vlc_object_t *p_this, int i_id )
417 {
418     interaction_t *p_interaction = InteractionGet( p_this );
419     interaction_dialog_t *p_dialog;
420
421     if( !p_interaction ) return;
422
423     vlc_mutex_lock( &p_interaction->object_lock );
424     p_dialog = DialogGetById( p_interaction, i_id );
425
426     if( !p_dialog )
427     {
428        vlc_mutex_unlock( &p_interaction->object_lock );
429        return;
430     }
431
432     p_dialog->i_status = ANSWERED_DIALOG;
433     vlc_mutex_unlock( &p_interaction->object_lock );
434 }
435
436 /**********************************************************************
437  * The following functions are local
438  **********************************************************************/
439
440 /* Get the interaction object. Create it if needed */
441 static interaction_t * InteractionGet( vlc_object_t *p_this )
442 {
443     interaction_t *p_interaction;
444     playlist_t *p_playlist = pl_Yield( p_this );
445
446     PL_LOCK;
447     if( p_playlist->p_interaction == NULL )
448        InteractionInit( p_playlist );
449
450     p_interaction = p_playlist->p_interaction;
451     PL_UNLOCK;
452
453     pl_Release( p_this );
454     return p_interaction;
455 }
456
457 /* Create the interaction object in the given playlist object */
458 static void InteractionInit( playlist_t *p_playlist )
459 {
460     interaction_t *p_interaction = vlc_object_create( VLC_OBJECT( p_playlist ),
461                                                       sizeof( interaction_t ) );
462     if( !p_interaction )
463     {
464         msg_Err( p_playlist,"out of memory" );
465         return;
466     }
467
468     p_interaction->i_dialogs = 0;
469     p_interaction->pp_dialogs = NULL;
470     p_interaction->p_intf = NULL;
471     p_interaction->i_last_id = DIALOG_LAST_PREDEFINED + 1;
472
473     vlc_mutex_init( p_interaction , &p_interaction->object_lock );
474
475     p_playlist->p_interaction  = p_interaction;
476 }
477
478 /* Look for an interface suitable for interaction */
479 static void InteractionSearchInterface( interaction_t *p_interaction )
480 {
481     vlc_list_t  *p_list;
482     int          i_index;
483
484     p_interaction->p_intf = NULL;
485
486     p_list = vlc_list_find( p_interaction, VLC_OBJECT_INTF, FIND_ANYWHERE );
487     if( !p_list )
488     {
489         msg_Err( p_interaction, "unable to create module list" );
490         return;
491     }
492
493     for( i_index = 0; i_index < p_list->i_count; i_index ++ )
494     {
495         intf_thread_t *p_intf = (intf_thread_t *)
496                                         p_list->p_values[i_index].p_object;
497         if( p_intf->b_interaction )
498         {
499             p_interaction->p_intf = p_intf;
500             break;
501         }
502     }
503     vlc_list_release ( p_list );
504 }
505
506 /* Find an interaction dialog by its id */
507 static interaction_dialog_t *DialogGetById( interaction_t *p_interaction,
508                                             int i_id )
509 {
510     int i;
511     for( i = 0 ; i< p_interaction->i_dialogs; i++ )
512     {
513         if( p_interaction->pp_dialogs[i]->i_id == i_id )
514             return p_interaction->pp_dialogs[i];
515     }
516     return NULL;
517 }
518
519 /* Destroy a dialog */
520 static void DialogDestroy( interaction_dialog_t *p_dialog )
521 {
522     free( p_dialog->psz_title );
523     free( p_dialog->psz_description );
524     free( p_dialog->psz_default_button );
525     free( p_dialog->psz_alternate_button );
526     free( p_dialog->psz_other_button );
527     free( p_dialog );
528 }
529
530 /* Ask for the dialog to be sent to the user. Wait for answer
531  * if required */
532 static int DialogSend( vlc_object_t *p_this, interaction_dialog_t *p_dialog )
533 {
534     interaction_t *p_interaction = InteractionGet( p_this );
535
536     /* Get an id, if we don't already have one */
537     if( p_dialog->i_id == 0 )
538         p_dialog->i_id = ++p_interaction->i_last_id;
539
540     if( p_this->i_flags & OBJECT_FLAGS_NOINTERACT ) return VLC_EGENERIC;
541
542     if( config_GetInt(p_this, "interact") ||
543         p_dialog->i_flags & DIALOG_BLOCKING_ERROR ||
544         p_dialog->i_flags & DIALOG_NONBLOCKING_ERROR )
545     {
546         vlc_bool_t b_found = VLC_FALSE;
547         int i;
548         p_dialog->p_interaction = p_interaction;
549         p_dialog->p_parent = p_this;
550
551         /* Check if we have already added this dialog */
552         vlc_mutex_lock( &p_interaction->object_lock );
553         for( i = 0 ; i< p_interaction->i_dialogs; i++ )
554         {
555             if( p_interaction->pp_dialogs[i]->i_id == p_dialog->i_id )
556                 b_found = VLC_TRUE;
557         }
558         /* Add it to the queue, the main loop will send the orders to the
559          * interface */
560         if( ! b_found )
561         {
562             INSERT_ELEM( p_interaction->pp_dialogs,
563                          p_interaction->i_dialogs,
564                          p_interaction->i_dialogs,
565                          p_dialog );
566         }
567         else
568             p_dialog->i_status = UPDATED_DIALOG;
569
570         if( p_dialog->i_type == INTERACT_DIALOG_TWOWAY ) // Wait for answer
571         {
572             playlist_Signal( pl_Get(p_this) );
573             while( p_dialog->i_status != ANSWERED_DIALOG &&
574                    p_dialog->i_status != HIDING_DIALOG &&
575                    p_dialog->i_status != HIDDEN_DIALOG &&
576                    !p_dialog->p_parent->b_die )
577             {
578                 vlc_mutex_unlock( &p_interaction->object_lock );
579                 msleep( 100000 );
580                 vlc_mutex_lock( &p_interaction->object_lock );
581             }
582             if( p_dialog->p_parent->b_die )
583             {
584                 p_dialog->i_return = DIALOG_CANCELLED;
585                 p_dialog->i_status = ANSWERED_DIALOG;
586             }
587             p_dialog->i_flags |= DIALOG_GOT_ANSWER;
588             vlc_mutex_unlock( &p_interaction->object_lock );
589             playlist_Signal( pl_Get(p_this) );
590             return p_dialog->i_return;
591         }
592         else
593         {
594             // Pretend we already retrieved the "answer"
595             p_dialog->i_flags |=  DIALOG_GOT_ANSWER;
596             vlc_mutex_unlock( &p_interaction->object_lock );
597             playlist_Signal( pl_Get(p_this) );
598             return VLC_SUCCESS;
599         }
600     }
601     else
602         return VLC_EGENERIC;
603 }