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