Fix no
[vlc.git] / src / interface / interaction.c
1 /*****************************************************************************
2  * interaction.c: User interaction functions
3  *****************************************************************************
4  * Copyright (C) 1998-2004 VideoLAN
5  * $Id: interface.c 10147 2005-03-05 17:18:30Z gbazin $
6  *
7  * Authors: ClĂ©ment Stenac <zorglub@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /**
25  *   \file
26  *   This file contains functions related to user interaction management
27  */
28
29 /*****************************************************************************
30  * Preamble
31  *****************************************************************************/
32 #include <stdlib.h>                                      /* free(), strtol() */
33 #include <stdio.h>                                                   /* FILE */
34 #include <string.h>                                            /* strerror() */
35
36 #include <vlc/vlc.h>
37 #include <vlc/input.h>
38
39 #include "vlc_interaction.h"
40 #include "vlc_interface.h"
41 #include "vlc_playlist.h"
42
43 /*****************************************************************************
44  * Local prototypes
45  *****************************************************************************/
46 static void                  intf_InteractionInit( playlist_t *p_playlist );
47 static interaction_t *       intf_InteractionGet( vlc_object_t *p_this );
48 static void                  intf_InteractionSearchInterface( interaction_t *
49                                                           p_interaction );
50 static int                   intf_WaitAnswer( interaction_t *p_interact,
51                              interaction_dialog_t *p_dialog );
52 static int                   intf_Send( interaction_t *p_interact,
53                              interaction_dialog_t *p_dialog );
54 static interaction_dialog_t *intf_InteractionGetById( vlc_object_t* , int );
55 static void                  intf_InteractionDialogDestroy(
56                                               interaction_dialog_t *p_dialog );
57
58 /**
59  * Send an interaction element to the user
60  *
61  * \param p_this the calling vlc_object_t
62  * \param p_interact the interaction element
63  * \return VLC_SUCCESS or an error code
64  */
65 int  __intf_Interact( vlc_object_t *p_this, interaction_dialog_t *
66                                     p_dialog )
67 {
68     interaction_t *p_interaction = intf_InteractionGet( p_this );
69
70     /* Get an id, if we don't already have one */
71     if( p_dialog->i_id == 0 )
72     {
73         p_dialog->i_id = ++p_interaction->i_last_id;
74     }
75
76     p_dialog->p_interaction = p_interaction;
77     p_dialog->p_parent = p_this;
78
79     if( p_dialog->i_type == INTERACT_DIALOG_TWOWAY )
80     {
81         return intf_WaitAnswer( p_interaction, p_dialog );
82     }
83     else
84     {
85         p_dialog->i_flags |=  DIALOG_GOT_ANSWER;
86         return intf_Send( p_interaction, p_dialog );
87     }
88 }
89
90 /**
91  * Destroy the interaction system
92  */
93 void intf_InteractionDestroy( interaction_t *p_interaction )
94 {
95     int i;
96
97     // Remove all dialogs - Interfaces must be able to clean up their data
98
99     for( i = p_interaction->i_dialogs -1 ; i >= 0; i-- )
100     {
101         interaction_dialog_t * p_dialog = p_interaction->pp_dialogs[i];
102         intf_InteractionDialogDestroy( p_dialog );
103         REMOVE_ELEM( p_interaction->pp_dialogs, p_interaction->i_dialogs, i );
104     }
105
106     vlc_object_destroy( p_interaction );
107 }
108
109 /**
110  * The main interaction processing loop
111  * This function is called from the playlist loop
112  *
113  * \param p_playlist the parent playlist
114  * \return nothing
115  */
116 void intf_InteractionManage( playlist_t *p_playlist )
117 {
118     vlc_value_t val;
119     int i_index;
120     interaction_t *p_interaction;
121
122     p_interaction = p_playlist->p_interaction;
123
124     // Nothing to do
125     if( p_interaction->i_dialogs == 0 ) return;
126
127     vlc_mutex_lock( &p_interaction->object_lock );
128
129     intf_InteractionSearchInterface( p_interaction );
130
131     if( !p_interaction->p_intf )
132     {
133         // We mark all dialogs as answered with their "default" answer
134         for( i_index = 0 ; i_index < p_interaction->i_dialogs; i_index ++ )
135         {
136             interaction_dialog_t *p_dialog = p_interaction->pp_dialogs[i_index];
137
138             // Give default answer
139             p_dialog->i_return = DIALOG_DEFAULT;
140             if( p_dialog->i_flags & DIALOG_OK_CANCEL )
141                 p_dialog->i_return = DIALOG_CANCELLED;
142
143             // Pretend we have hidden and destroyed it
144             if( p_dialog->i_status == HIDDEN_DIALOG )
145             {
146                 p_dialog->i_status = DESTROYED_DIALOG;
147             }
148             else
149             {
150                 p_dialog->i_status = HIDING_DIALOG;
151             }
152         }
153     }
154     else
155     {
156         vlc_object_yield( p_interaction->p_intf );
157     }
158
159     for( i_index = 0 ; i_index < p_interaction->i_dialogs; i_index ++ )
160     {
161         interaction_dialog_t *p_dialog = p_interaction->pp_dialogs[i_index];
162         switch( p_dialog->i_status )
163         {
164         case ANSWERED_DIALOG:
165             // Ask interface to hide it
166             msg_Dbg( p_interaction, "Hiding dialog %i", p_dialog->i_id );
167             p_dialog->i_action = INTERACT_HIDE;
168             val.p_address = p_dialog;
169             if( p_interaction->p_intf )
170                 var_Set( p_interaction->p_intf, "interaction", val );
171             p_dialog->i_status = HIDING_DIALOG;
172             break;
173         case UPDATED_DIALOG:
174             p_dialog->i_action = INTERACT_UPDATE;
175             val.p_address = p_dialog;
176             if( p_interaction->p_intf )
177                 var_Set( p_interaction->p_intf, "interaction", val );
178             p_dialog->i_status = SENT_DIALOG;
179             msg_Dbg( p_interaction, "Updating dialog %i, %i widgets",
180                                     p_dialog->i_id, p_dialog->i_widgets );
181             break;
182         case HIDDEN_DIALOG:
183             if( !(p_dialog->i_flags & DIALOG_GOT_ANSWER) ) break;
184             if( !(p_dialog->i_flags & DIALOG_REUSABLE) )
185             {
186                 msg_Dbg( p_interaction, "Destroying dialog %i",
187                                                 p_dialog->i_id );
188                 p_dialog->i_action = INTERACT_DESTROY;
189                 val.p_address = p_dialog;
190                 if( p_interaction->p_intf )
191                     var_Set( p_interaction->p_intf, "interaction", val );
192             }
193             break;
194         case DESTROYED_DIALOG:
195             msg_Dbg( p_interaction, "Removing dialog %i",
196                                     p_dialog->i_id );
197             // Interface has now destroyed it, remove it
198             REMOVE_ELEM( p_interaction->pp_dialogs, p_interaction->i_dialogs,
199                          i_index);
200             i_index--;
201             intf_InteractionDialogDestroy( p_dialog );
202             break;
203         case NEW_DIALOG:
204             // This is truly a new dialog, send it.
205             p_dialog->i_action = INTERACT_NEW;
206             val.p_address = p_dialog;
207             if( p_interaction->p_intf )
208                 var_Set( p_interaction->p_intf, "interaction", val );
209             p_dialog->i_status = SENT_DIALOG;
210             break;
211         }
212     }
213
214     if( p_interaction->p_intf )
215     {
216         vlc_object_release( p_interaction->p_intf );
217     }
218
219     vlc_mutex_unlock( &p_playlist->p_interaction->object_lock );
220 }
221
222
223
224 #define INTERACT_INIT( new )                                            \
225         new = (interaction_dialog_t*)malloc(                            \
226                         sizeof( interaction_dialog_t ) );               \
227         new->i_widgets = 0;                                             \
228         new->pp_widgets = NULL;                                         \
229         new->psz_title = NULL;                                          \
230         new->psz_description = NULL;                                    \
231         new->i_id = 0;                                                  \
232         new->i_flags = 0;                                               \
233         new->i_status = NEW_DIALOG;
234
235 #define INTERACT_FREE( new )                                            \
236         if( new->psz_title ) free( new->psz_title );                    \
237         if( new->psz_description ) free( new->psz_description );
238
239 /** Helper function to send an error message
240  *  \param p_this     Parent vlc_object
241  *  \param i_id       A predefined ID, 0 if not applicable
242  *  \param psz_title  Title for the dialog
243  *  \param psz_format The message to display
244  *  */
245 void __intf_UserFatal( vlc_object_t *p_this,
246                        const char *psz_title,
247                        const char *psz_format, ... )
248 {
249     va_list args;
250     interaction_dialog_t *p_new = NULL;
251     user_widget_t *p_widget = NULL;
252     int i_id = DIALOG_ERRORS;
253
254     if( i_id > 0 )
255     {
256         p_new = intf_InteractionGetById( p_this, i_id );
257     }
258     if( !p_new )
259     {
260         INTERACT_INIT( p_new );
261         if( i_id > 0 ) p_new->i_id = i_id ;
262     }
263     else
264     {
265         p_new->i_status = UPDATED_DIALOG;
266     }
267
268     p_new->i_flags |= DIALOG_REUSABLE;
269
270     p_new->i_type = INTERACT_DIALOG_ONEWAY;
271     p_new->psz_title = strdup( psz_title );
272
273     p_widget = (user_widget_t* )malloc( sizeof( user_widget_t ) );
274
275     p_widget->i_type = WIDGET_TEXT;
276     p_widget->val.psz_string = NULL;
277
278     va_start( args, psz_format );
279     vasprintf( &p_widget->psz_text, psz_format, args );
280     va_end( args );
281
282     INSERT_ELEM ( p_new->pp_widgets,
283                   p_new->i_widgets,
284                   p_new->i_widgets,
285                   p_widget );
286
287     p_new->i_flags |= DIALOG_CLEAR_NOSHOW;
288
289     intf_Interact( p_this, p_new );
290 }
291
292 /** Helper function to make a login/password box
293  *  \param p_this           Parent vlc_object
294  *  \param psz_title        Title for the dialog
295  *  \param psz_description  A description
296  *  \param ppsz_login       Returned login
297  *  \param ppsz_password    Returned password
298  *  \return                 Clicked button code
299  */
300 int __intf_UserYesNo( vlc_object_t *p_this,
301                       const char *psz_title,
302                       const char *psz_description )
303 {
304     int i_ret;
305     interaction_dialog_t *p_new = NULL;
306     user_widget_t *p_widget = NULL;
307
308     INTERACT_INIT( p_new );
309
310     p_new->i_type = INTERACT_DIALOG_TWOWAY;
311     p_new->psz_title = strdup( psz_title );
312
313     /* Text */
314     p_widget = (user_widget_t* )malloc( sizeof( user_widget_t ) );
315     p_widget->i_type = WIDGET_TEXT;
316     p_widget->psz_text = strdup( psz_description );
317     p_widget->val.psz_string = NULL;
318     INSERT_ELEM ( p_new->pp_widgets, p_new->i_widgets,
319                   p_new->i_widgets,  p_widget );
320
321     p_new->i_flags = DIALOG_YES_NO_CANCEL;
322
323     i_ret = intf_Interact( p_this, p_new );
324
325     return i_ret;
326 }
327
328 /** Helper function to make a progressbar box
329  *  \param p_this           Parent vlc_object
330  *  \param psz_title        Title for the dialog
331  *  \param psz_status       Current status
332  *  \param f_position       Current position (0.0->100.0)
333  *  \return                 Dialog id, to give to UserProgressUpdate
334  */
335 int __intf_UserProgress( vlc_object_t *p_this,
336                          const char *psz_title,
337                          const char *psz_status,
338                          float f_pos )
339 {
340     int i_ret;
341     interaction_dialog_t *p_new = NULL;
342     user_widget_t *p_widget = NULL;
343
344     INTERACT_INIT( p_new );
345
346     p_new->i_type = INTERACT_DIALOG_ONEWAY;
347     p_new->psz_title = strdup( psz_title );
348
349     /* Progress bar */
350     p_widget = (user_widget_t* )malloc( sizeof( user_widget_t ) );
351     p_widget->i_type = WIDGET_PROGRESS;
352     p_widget->psz_text = strdup( psz_status );
353     p_widget->val.f_float = f_pos;
354     INSERT_ELEM ( p_new->pp_widgets, p_new->i_widgets,
355                   p_new->i_widgets,  p_widget );
356
357     i_ret = intf_Interact( p_this, p_new );
358
359     return p_new->i_id;
360 }
361 /** Update a progress bar
362  *  \param p_this           Parent vlc_object
363  *  \param i_id             Identifier of the dialog
364  *  \param psz_status       New status
365  *  \param f_position       New position (0.0->100.0)
366  *  \return                 nothing
367  */
368 void __intf_UserProgressUpdate( vlc_object_t *p_this, int i_id,
369                                 const char *psz_status, float f_pos )
370 {
371     interaction_t *p_interaction = intf_InteractionGet( p_this );
372     interaction_dialog_t *p_dialog;
373
374     if( !p_interaction ) return;
375
376     vlc_mutex_lock( &p_interaction->object_lock );
377     p_dialog  =  intf_InteractionGetById( p_this, i_id );
378
379     if( !p_dialog )
380     {
381         vlc_mutex_unlock( &p_intearction->object_lock ) ;
382         return;
383     }
384
385     if( p_dialog->pp_widgets[0]->psz_text )
386         free( p_dialog->pp_widgets[0]->psz_text );
387     p_dialog->pp_widgets[0]->psz_text = strdup( psz_status );
388
389     p_dialog->pp_widgets[0]->val.f_float = f_pos;
390
391     p_dialog->i_status = UPDATED_DIALOG;
392     vlc_mutex_unlock( &p_interaction->object_lock) ;
393 }
394
395
396
397 /** Helper function to ask a yes-no question
398  *  \param p_this           Parent vlc_object
399  *  \param psz_title        Title for the dialog
400  *  \param psz_description  A description
401  *  \return                 Clicked button code
402  */
403 int __intf_UserLoginPassword( vlc_object_t *p_this,
404                               const char *psz_title,
405                               const char *psz_description,
406                               char **ppsz_login,
407                               char **ppsz_password )
408 {
409     int i_ret;
410     interaction_dialog_t *p_new = NULL;
411     user_widget_t *p_widget = NULL;
412
413     INTERACT_INIT( p_new );
414
415     p_new->i_type = INTERACT_DIALOG_TWOWAY;
416     p_new->psz_title = strdup( psz_title );
417
418     /* Text */
419     p_widget = (user_widget_t* )malloc( sizeof( user_widget_t ) );
420     p_widget->i_type = WIDGET_TEXT;
421     p_widget->psz_text = strdup( psz_description );
422     p_widget->val.psz_string = NULL;
423     INSERT_ELEM ( p_new->pp_widgets, p_new->i_widgets,
424                   p_new->i_widgets,  p_widget );
425
426     /* Login */
427     p_widget = (user_widget_t* )malloc( sizeof( user_widget_t ) );
428     p_widget->i_type = WIDGET_INPUT_TEXT;
429     p_widget->psz_text = strdup( _("Login") );
430     p_widget->val.psz_string = NULL;
431     INSERT_ELEM ( p_new->pp_widgets, p_new->i_widgets,
432                   p_new->i_widgets,  p_widget );
433
434     /* Password */
435     p_widget = (user_widget_t* )malloc( sizeof( user_widget_t ) );
436     p_widget->i_type = WIDGET_INPUT_TEXT;
437     p_widget->psz_text = strdup( _("Password") );
438     p_widget->val.psz_string = NULL;
439     INSERT_ELEM ( p_new->pp_widgets, p_new->i_widgets,
440                   p_new->i_widgets,  p_widget );
441
442     p_new->i_flags = DIALOG_OK_CANCEL;
443
444     i_ret = intf_Interact( p_this, p_new );
445
446     if( i_ret != DIALOG_CANCELLED )
447     {
448         *ppsz_login = strdup( p_new->pp_widgets[1]->val.psz_string );
449         *ppsz_password = strdup( p_new->pp_widgets[2]->val.psz_string );
450     }
451     return i_ret;
452 }
453
454 /** Hide an interaction dialog
455  * \param p_this the parent vlc object
456  * \param i_id the id of the item to hide
457  * \return nothing
458  */
459 void __intf_UserHide( vlc_object_t *p_this, int i_id )
460 {
461     interaction_t *p_interaction = intf_InteractionGet( p_this );
462     interaction_dialog_t *p_dialog;
463
464     if( !p_interaction ) return;
465
466     vlc_mutex_lock( &p_interaction->object_lock );
467     p_dialog  =  intf_InteractionGetById( p_this, i_id );
468
469     if( !p_dialog ) return;
470
471     p_dialog->i_status = ANSWERED_DIALOG;
472     vlc_mutex_unlock( &p_interaction->object_lock );
473 }
474
475
476
477 /**********************************************************************
478  * The following functions are local
479  **********************************************************************/
480
481 /* Get the interaction object. Create it if needed */
482 static interaction_t * intf_InteractionGet( vlc_object_t *p_this )
483 {
484     playlist_t *p_playlist;
485     interaction_t *p_interaction;
486
487     p_playlist = (playlist_t*) vlc_object_find( p_this, VLC_OBJECT_PLAYLIST,
488                                                 FIND_ANYWHERE );
489
490     if( !p_playlist )
491     {
492         return NULL;
493     }
494
495     if( p_playlist->p_interaction == NULL )
496     {
497         intf_InteractionInit( p_playlist );
498     }
499
500     p_interaction = p_playlist->p_interaction;
501
502     vlc_object_release( p_playlist );
503
504     return p_interaction;
505 }
506
507 /* Create the interaction object in the given playlist object */
508 static void intf_InteractionInit( playlist_t *p_playlist )
509 {
510     interaction_t *p_interaction;
511
512     msg_Dbg( p_playlist, "initializing interaction system" );
513
514     p_interaction = vlc_object_create( VLC_OBJECT( p_playlist ),
515                                        sizeof( interaction_t ) );
516     if( !p_interaction )
517     {
518         msg_Err( p_playlist,"out of memory" );
519         return;
520     }
521
522     p_interaction->i_dialogs = 0;
523     p_interaction->pp_dialogs = NULL;
524     p_interaction->p_intf = NULL;
525     p_interaction->i_last_id = DIALOG_LAST_PREDEFINED + 1;
526
527     vlc_mutex_init( p_interaction , &p_interaction->object_lock );
528
529     p_playlist->p_interaction  = p_interaction;
530 }
531
532 /* Look for an interface suitable for interaction */
533 static void intf_InteractionSearchInterface( interaction_t *p_interaction )
534 {
535     vlc_list_t  *p_list;
536     int          i_index;
537
538     p_interaction->p_intf = NULL;
539
540     p_list = vlc_list_find( p_interaction, VLC_OBJECT_INTF, FIND_ANYWHERE );
541     if( !p_list )
542     {
543         msg_Err( p_interaction, "Unable to create module list" );
544         return;
545     }
546
547     for( i_index = 0; i_index < p_list->i_count; i_index ++ )
548     {
549         intf_thread_t *p_intf = (intf_thread_t *)
550                                         p_list->p_values[i_index].p_object;
551         if( p_intf->b_interaction )
552         {
553             p_interaction->p_intf = p_intf;
554             break;
555         }
556     }
557     vlc_list_release ( p_list );
558 }
559
560 /* Add a dialog to the queue and wait for answer */
561 static int intf_WaitAnswer( interaction_t *p_interact, interaction_dialog_t *p_dialog )
562 {
563     int i;
564     vlc_bool_t b_found = VLC_FALSE;
565     vlc_mutex_lock( &p_interact->object_lock );
566     for( i = 0 ; i< p_interact->i_dialogs; i++ )
567     {
568         if( p_interact->pp_dialogs[i]->i_id == p_dialog->i_id )
569         {
570             b_found = VLC_TRUE;
571         }
572     }
573     if( ! b_found )
574     {
575         INSERT_ELEM( p_interact->pp_dialogs,
576                      p_interact->i_dialogs,
577                      p_interact->i_dialogs,
578                      p_dialog );
579     }
580     else
581         p_dialog->i_status = UPDATED_DIALOG;
582     vlc_mutex_unlock( &p_interact->object_lock );
583
584     /// \todo Check that the initiating object is not dying
585     while( p_dialog->i_status != ANSWERED_DIALOG &&
586            p_dialog->i_status != HIDING_DIALOG &&
587            p_dialog->i_status != HIDDEN_DIALOG &&
588            !p_dialog->p_parent->b_die )
589     {
590         msleep( 100000 );
591     }
592     /// \todo locking
593     if( p_dialog->p_parent->b_die )
594     {
595         p_dialog->i_return = DIALOG_CANCELLED;
596         p_dialog->i_status = ANSWERED_DIALOG;
597     }
598     p_dialog->i_flags |= DIALOG_GOT_ANSWER;
599     return p_dialog->i_return;
600 }
601
602 /* Add a dialog to the queue and return */
603 static int intf_Send( interaction_t *p_interact, interaction_dialog_t *p_dialog )
604 {
605     int i;
606     vlc_bool_t b_found = VLC_FALSE;
607     vlc_mutex_lock( &p_interact->object_lock );
608
609     for( i = 0 ; i< p_interact->i_dialogs; i++ )
610     {
611         if( p_interact->pp_dialogs[i]->i_id == p_dialog->i_id )
612         {
613             b_found = VLC_TRUE;
614         }
615     }
616     if( !b_found )
617     {
618         INSERT_ELEM( p_interact->pp_dialogs,
619                      p_interact->i_dialogs,
620                      p_interact->i_dialogs,
621                      p_dialog );
622     }
623     else
624         p_dialog->i_status = UPDATED_DIALOG;
625     // Pretend we already retrieved the "answer"
626     p_dialog->i_flags |= DIALOG_GOT_ANSWER;
627     vlc_mutex_unlock( &p_interact->object_lock );
628     return VLC_SUCCESS;
629 }
630
631 /* Find an interaction dialog by its id */
632 static interaction_dialog_t *intf_InteractionGetById( vlc_object_t* p_this,
633                                                        int i_id )
634 {
635     interaction_t *p_interaction = intf_InteractionGet( p_this );
636     int i;
637
638     if( !p_interaction ) return NULL;
639
640     for( i = 0 ; i< p_interaction->i_dialogs; i++ )
641     {
642         if( p_interaction->pp_dialogs[i]->i_id == i_id )
643         {
644             return p_interaction->pp_dialogs[i];
645         }
646     }
647     return NULL;
648 }
649
650 #define FREE( i ) { if( i ) free( i ); i = NULL; }
651
652 static void intf_InteractionDialogDestroy( interaction_dialog_t *p_dialog )
653 {
654     int i;
655     for( i = p_dialog->i_widgets - 1 ; i >= 0 ; i-- )
656     {
657         user_widget_t *p_widget = p_dialog->pp_widgets[i];
658         FREE( p_widget->psz_text );
659         if( p_widget->i_type == WIDGET_INPUT_TEXT )
660         {
661             FREE( p_widget->val.psz_string );
662         }
663
664         REMOVE_ELEM( p_dialog->pp_widgets, p_dialog->i_widgets, i );
665         free( p_widget );
666     }
667     FREE( p_dialog->psz_title );
668     FREE( p_dialog->psz_description );
669
670     free( p_dialog );
671 }