]> git.sesse.net Git - vlc/blob - src/interface/interaction.c
Better fix
[vlc] / src / interface / interaction.c
1 /*****************************************************************************
2  * interaction.c: User interaction functions
3  *****************************************************************************
4  * Copyright (C) 2005-2006 VideoLAN
5  * $Id$
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, 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     if( p_this->i_flags & OBJECT_FLAGS_NOINTERACT )
77     {
78        return VLC_EGENERIC;
79     }
80
81     p_dialog->p_interaction = p_interaction;
82     p_dialog->p_parent = p_this;
83
84     if( p_dialog->i_type == INTERACT_DIALOG_TWOWAY )
85     {
86         return intf_WaitAnswer( p_interaction, p_dialog );
87     }
88     else
89     {
90         p_dialog->i_flags |=  DIALOG_GOT_ANSWER;
91         return intf_Send( p_interaction, p_dialog );
92     }
93 }
94
95 /**
96  * Destroy the interaction system
97  * \param The interaction object to destroy
98  * \return nothing
99  */
100 void intf_InteractionDestroy( interaction_t *p_interaction )
101 {
102     int i;
103
104     // Remove all dialogs - Interfaces must be able to clean up their data
105
106     for( i = p_interaction->i_dialogs -1 ; i >= 0; i-- )
107     {
108         interaction_dialog_t * p_dialog = p_interaction->pp_dialogs[i];
109         intf_InteractionDialogDestroy( p_dialog );
110         REMOVE_ELEM( p_interaction->pp_dialogs, p_interaction->i_dialogs, i );
111     }
112
113     vlc_object_destroy( p_interaction );
114 }
115
116 /**
117  * The main interaction processing loop
118  * This function is called from the playlist loop
119  *
120  * \param p_playlist the parent playlist
121  * \return nothing
122  */
123 void intf_InteractionManage( playlist_t *p_playlist )
124 {
125     vlc_value_t val;
126     int i_index;
127     interaction_t *p_interaction;
128
129     p_interaction = p_playlist->p_interaction;
130
131     // Nothing to do
132     if( p_interaction->i_dialogs == 0 ) return;
133
134     vlc_mutex_lock( &p_interaction->object_lock );
135
136     intf_InteractionSearchInterface( p_interaction );
137
138     if( !p_interaction->p_intf )
139     {
140         // We mark all dialogs as answered with their "default" answer
141         for( i_index = 0 ; i_index < p_interaction->i_dialogs; i_index ++ )
142         {
143             interaction_dialog_t *p_dialog = p_interaction->pp_dialogs[i_index];
144
145             // Give default answer
146             p_dialog->i_return = DIALOG_DEFAULT;
147             if( p_dialog->i_flags & DIALOG_OK_CANCEL )
148                 p_dialog->i_return = DIALOG_CANCELLED;
149
150             // Pretend we have hidden and destroyed it
151             if( p_dialog->i_status == HIDDEN_DIALOG )
152             {
153                 p_dialog->i_status = DESTROYED_DIALOG;
154             }
155             else
156             {
157                 p_dialog->i_status = HIDING_DIALOG;
158             }
159         }
160     }
161     else
162     {
163         vlc_object_yield( p_interaction->p_intf );
164     }
165
166     for( i_index = 0 ; i_index < p_interaction->i_dialogs; i_index ++ )
167     {
168         interaction_dialog_t *p_dialog = p_interaction->pp_dialogs[i_index];
169         switch( p_dialog->i_status )
170         {
171         case ANSWERED_DIALOG:
172             // Ask interface to hide it
173             p_dialog->i_action = INTERACT_HIDE;
174             val.p_address = p_dialog;
175             if( p_interaction->p_intf )
176                 var_Set( p_interaction->p_intf, "interaction", val );
177             p_dialog->i_status = HIDING_DIALOG;
178             break;
179         case UPDATED_DIALOG:
180             p_dialog->i_action = INTERACT_UPDATE;
181             val.p_address = p_dialog;
182             if( p_interaction->p_intf )
183                 var_Set( p_interaction->p_intf, "interaction", val );
184             p_dialog->i_status = SENT_DIALOG;
185             break;
186         case HIDDEN_DIALOG:
187             if( !(p_dialog->i_flags & DIALOG_GOT_ANSWER) ) break;
188             if( !(p_dialog->i_flags & DIALOG_REUSABLE) )
189             {
190                 p_dialog->i_action = INTERACT_DESTROY;
191                 val.p_address = p_dialog;
192                 if( p_interaction->p_intf )
193                     var_Set( p_interaction->p_intf, "interaction", val );
194             }
195             break;
196         case DESTROYED_DIALOG:
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->p_private = NULL;                                          \
232         new->i_id = 0;                                                  \
233         new->i_flags = 0;                                               \
234         new->i_status = NEW_DIALOG;
235
236 #define INTERACT_FREE( new )                                            \
237         if( new->psz_title ) free( new->psz_title );                    \
238         if( new->psz_description ) free( new->psz_description );
239
240 /** Helper function to send an error message
241  *  \param p_this     Parent vlc_object
242  *  \param i_id       A predefined ID, 0 if not applicable
243  *  \param psz_title  Title for the dialog
244  *  \param psz_format The message to display
245  *  */
246 void __intf_UserFatal( vlc_object_t *p_this,
247                        const char *psz_title,
248                        const char *psz_format, ... )
249 {
250     va_list args;
251     interaction_dialog_t *p_new = NULL;
252     user_widget_t *p_widget = NULL;
253     int i_id = DIALOG_ERRORS;
254
255     if( i_id > 0 )
256     {
257         p_new = intf_InteractionGetById( p_this, i_id );
258     }
259     if( !p_new )
260     {
261         INTERACT_INIT( p_new );
262         if( i_id > 0 ) p_new->i_id = i_id ;
263     }
264     else
265     {
266         p_new->i_status = UPDATED_DIALOG;
267     }
268
269     p_new->i_flags |= DIALOG_REUSABLE;
270
271     p_new->i_type = INTERACT_DIALOG_ONEWAY;
272     p_new->psz_title = strdup( psz_title );
273
274     p_widget = (user_widget_t* )malloc( sizeof( user_widget_t ) );
275
276     p_widget->i_type = WIDGET_TEXT;
277     p_widget->val.psz_string = NULL;
278
279     va_start( args, psz_format );
280     vasprintf( &p_widget->psz_text, psz_format, args );
281     va_end( args );
282
283     INSERT_ELEM ( p_new->pp_widgets,
284                   p_new->i_widgets,
285                   p_new->i_widgets,
286                   p_widget );
287
288     p_new->i_flags |= DIALOG_CLEAR_NOSHOW;
289
290     intf_Interact( p_this, p_new );
291 }
292
293 /** Helper function to ask a yes-no question
294  *  \param p_this           Parent vlc_object
295  *  \param psz_title        Title for the dialog
296  *  \param psz_description  A description
297  *  \return                 Clicked button code
298  */
299 int __intf_UserYesNo( vlc_object_t *p_this,
300                       const char *psz_title,
301                       const char *psz_description )
302 {
303     int i_ret;
304     interaction_dialog_t *p_new = NULL;
305     user_widget_t *p_widget = NULL;
306
307     INTERACT_INIT( p_new );
308
309     p_new->i_type = INTERACT_DIALOG_TWOWAY;
310     p_new->psz_title = strdup( psz_title );
311
312     /* Text */
313     p_widget = (user_widget_t* )malloc( sizeof( user_widget_t ) );
314     p_widget->i_type = WIDGET_TEXT;
315     p_widget->psz_text = strdup( psz_description );
316     p_widget->val.psz_string = NULL;
317     INSERT_ELEM ( p_new->pp_widgets, p_new->i_widgets,
318                   p_new->i_widgets,  p_widget );
319
320     p_new->i_flags = DIALOG_YES_NO_CANCEL;
321
322     i_ret = intf_Interact( p_this, p_new );
323
324     return i_ret;
325 }
326
327 /** Helper function to make a progressbar box
328  *  \param p_this           Parent vlc_object
329  *  \param psz_title        Title for the dialog
330  *  \param psz_status       Current status
331  *  \param f_position       Current position (0.0->100.0)
332  *  \return                 Dialog id, to give to UserProgressUpdate
333  */
334 int __intf_UserProgress( vlc_object_t *p_this,
335                          const char *psz_title,
336                          const char *psz_status,
337                          float f_pos )
338 {
339     int i_ret;
340     interaction_dialog_t *p_new = NULL;
341     user_widget_t *p_widget = NULL;
342
343     INTERACT_INIT( p_new );
344
345     p_new->i_type = INTERACT_DIALOG_ONEWAY;
346     p_new->psz_title = strdup( psz_title );
347
348     /* Progress bar */
349     p_widget = (user_widget_t* )malloc( sizeof( user_widget_t ) );
350     p_widget->i_type = WIDGET_PROGRESS;
351     p_widget->psz_text = strdup( psz_status );
352     p_widget->val.f_float = f_pos;
353     INSERT_ELEM ( p_new->pp_widgets, p_new->i_widgets,
354                   p_new->i_widgets,  p_widget );
355
356     i_ret = intf_Interact( p_this, p_new );
357
358     return p_new->i_id;
359 }
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_interaction->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 /** Helper function to make a login/password box
396  *  \param p_this           Parent vlc_object
397  *  \param psz_title        Title for the dialog
398  *  \param psz_description  A description
399  *  \param ppsz_login       Returned login
400  *  \param ppsz_password    Returned password
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 )
470     {
471        vlc_mutex_unlock( &p_interaction->object_lock );
472        return;
473     }
474
475     p_dialog->i_status = ANSWERED_DIALOG;
476     vlc_mutex_unlock( &p_interaction->object_lock );
477 }
478
479
480
481 /**********************************************************************
482  * The following functions are local
483  **********************************************************************/
484
485 /* Get the interaction object. Create it if needed */
486 static interaction_t * intf_InteractionGet( vlc_object_t *p_this )
487 {
488     playlist_t *p_playlist;
489     interaction_t *p_interaction;
490
491     p_playlist = (playlist_t*) vlc_object_find( p_this, VLC_OBJECT_PLAYLIST,
492                                                 FIND_ANYWHERE );
493
494     if( !p_playlist )
495     {
496         return NULL;
497     }
498
499     if( p_playlist->p_interaction == NULL )
500     {
501         intf_InteractionInit( p_playlist );
502     }
503
504     p_interaction = p_playlist->p_interaction;
505
506     vlc_object_release( p_playlist );
507
508     return p_interaction;
509 }
510
511 /* Create the interaction object in the given playlist object */
512 static void intf_InteractionInit( playlist_t *p_playlist )
513 {
514     interaction_t *p_interaction;
515
516     msg_Dbg( p_playlist, "initializing interaction system" );
517
518     p_interaction = vlc_object_create( VLC_OBJECT( p_playlist ),
519                                        sizeof( interaction_t ) );
520     if( !p_interaction )
521     {
522         msg_Err( p_playlist,"out of memory" );
523         return;
524     }
525
526     p_interaction->i_dialogs = 0;
527     p_interaction->pp_dialogs = NULL;
528     p_interaction->p_intf = NULL;
529     p_interaction->i_last_id = DIALOG_LAST_PREDEFINED + 1;
530
531     vlc_mutex_init( p_interaction , &p_interaction->object_lock );
532
533     p_playlist->p_interaction  = p_interaction;
534 }
535
536 /* Look for an interface suitable for interaction */
537 static void intf_InteractionSearchInterface( interaction_t *p_interaction )
538 {
539     vlc_list_t  *p_list;
540     int          i_index;
541
542     p_interaction->p_intf = NULL;
543
544     p_list = vlc_list_find( p_interaction, VLC_OBJECT_INTF, FIND_ANYWHERE );
545     if( !p_list )
546     {
547         msg_Err( p_interaction, "Unable to create module list" );
548         return;
549     }
550
551     for( i_index = 0; i_index < p_list->i_count; i_index ++ )
552     {
553         intf_thread_t *p_intf = (intf_thread_t *)
554                                         p_list->p_values[i_index].p_object;
555         if( p_intf->b_interaction )
556         {
557             p_interaction->p_intf = p_intf;
558             break;
559         }
560     }
561     vlc_list_release ( p_list );
562 }
563
564 /* Add a dialog to the queue and wait for answer */
565 static int intf_WaitAnswer( interaction_t *p_interact,
566                             interaction_dialog_t *p_dialog )
567 {
568     int i;
569     vlc_bool_t b_found = VLC_FALSE;
570     vlc_mutex_lock( &p_interact->object_lock );
571     for( i = 0 ; i< p_interact->i_dialogs; i++ )
572     {
573         if( p_interact->pp_dialogs[i]->i_id == p_dialog->i_id )
574         {
575             b_found = VLC_TRUE;
576         }
577     }
578     if( ! b_found )
579     {
580         INSERT_ELEM( p_interact->pp_dialogs,
581                      p_interact->i_dialogs,
582                      p_interact->i_dialogs,
583                      p_dialog );
584     }
585     else
586         p_dialog->i_status = UPDATED_DIALOG;
587     vlc_mutex_unlock( &p_interact->object_lock );
588
589     /// \todo Check that the initiating object is not dying
590     while( p_dialog->i_status != ANSWERED_DIALOG &&
591            p_dialog->i_status != HIDING_DIALOG &&
592            p_dialog->i_status != HIDDEN_DIALOG &&
593            !p_dialog->p_parent->b_die )
594     {
595         msleep( 100000 );
596     }
597     /// \todo locking
598     if( p_dialog->p_parent->b_die )
599     {
600         p_dialog->i_return = DIALOG_CANCELLED;
601         p_dialog->i_status = ANSWERED_DIALOG;
602     }
603     p_dialog->i_flags |= DIALOG_GOT_ANSWER;
604     return p_dialog->i_return;
605 }
606
607 /* Add a dialog to the queue and return */
608 static int intf_Send( interaction_t *p_interact,
609                       interaction_dialog_t *p_dialog )
610 {
611     int i;
612     vlc_bool_t b_found = VLC_FALSE;
613     if( p_interact == NULL ) return VLC_ENOOBJ;
614     vlc_mutex_lock( &p_interact->object_lock );
615
616     for( i = 0 ; i< p_interact->i_dialogs; i++ )
617     {
618         if( p_interact->pp_dialogs[i]->i_id == p_dialog->i_id )
619         {
620             b_found = VLC_TRUE;
621         }
622     }
623     if( !b_found )
624     {
625         INSERT_ELEM( p_interact->pp_dialogs,
626                      p_interact->i_dialogs,
627                      p_interact->i_dialogs,
628                      p_dialog );
629     }
630     else
631         p_dialog->i_status = UPDATED_DIALOG;
632     // Pretend we already retrieved the "answer"
633     p_dialog->i_flags |= DIALOG_GOT_ANSWER;
634     vlc_mutex_unlock( &p_interact->object_lock );
635     return VLC_SUCCESS;
636 }
637
638 /* Find an interaction dialog by its id */
639 static interaction_dialog_t *intf_InteractionGetById( vlc_object_t* p_this,
640                                                        int i_id )
641 {
642     interaction_t *p_interaction = intf_InteractionGet( p_this );
643     int i;
644
645     if( !p_interaction ) return NULL;
646
647     for( i = 0 ; i< p_interaction->i_dialogs; i++ )
648     {
649         if( p_interaction->pp_dialogs[i]->i_id == i_id )
650         {
651             return p_interaction->pp_dialogs[i];
652         }
653     }
654     return NULL;
655 }
656
657 #define FREE( i ) { if( i ) free( i ); i = NULL; }
658
659 static void intf_InteractionDialogDestroy( interaction_dialog_t *p_dialog )
660 {
661     int i;
662     for( i = p_dialog->i_widgets - 1 ; i >= 0 ; i-- )
663     {
664         user_widget_t *p_widget = p_dialog->pp_widgets[i];
665         FREE( p_widget->psz_text );
666         if( p_widget->i_type == WIDGET_INPUT_TEXT )
667         {
668             FREE( p_widget->val.psz_string );
669         }
670
671         REMOVE_ELEM( p_dialog->pp_widgets, p_dialog->i_widgets, i );
672         free( p_widget );
673     }
674     FREE( p_dialog->psz_title );
675     FREE( p_dialog->psz_description );
676
677     free( p_dialog );
678 }