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