]> git.sesse.net Git - vlc/blob - src/interface/interaction.c
* first implementation of a widget-free authentication-dialogue (core and OSX only...
[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  *          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 #include <stdlib.h>                                      /* free(), strtol() */
34 #include <stdio.h>                                                   /* FILE */
35 #include <string.h>                                            /* strerror() */
36
37 #include <vlc/vlc.h>
38 #include <vlc/input.h>
39
40 #include "vlc_interaction.h"
41 #include "vlc_interface.h"
42 #include "vlc_playlist.h"
43
44 /*****************************************************************************
45  * Local prototypes
46  *****************************************************************************/
47 static void                  intf_InteractionInit( playlist_t *p_playlist );
48 static interaction_t *       intf_InteractionGet( vlc_object_t *p_this );
49 static void                  intf_InteractionSearchInterface( interaction_t *
50                                                           p_interaction );
51 static int                   intf_WaitAnswer( interaction_t *p_interact,
52                              interaction_dialog_t *p_dialog );
53 static int                   intf_Send( interaction_t *p_interact,
54                              interaction_dialog_t *p_dialog );
55 static interaction_dialog_t *intf_InteractionGetById( vlc_object_t* , int );
56 static void                  intf_InteractionDialogDestroy(
57                                               interaction_dialog_t *p_dialog );
58
59 /**
60  * Send an interaction element to the user
61  *
62  * \param p_this the calling vlc_object_t
63  * \param p_interact the interaction element
64  * \return VLC_SUCCESS or an error code
65  */
66 int  __intf_Interact( vlc_object_t *p_this, interaction_dialog_t *
67                                     p_dialog )
68 {
69     interaction_t *p_interaction = intf_InteractionGet( p_this );
70
71     /* Get an id, if we don't already have one */
72     if( p_dialog->i_id == 0 )
73     {
74         p_dialog->i_id = ++p_interaction->i_last_id;
75     }
76
77     if( p_this->i_flags & OBJECT_FLAGS_NOINTERACT )
78     {
79        return VLC_EGENERIC;
80     }
81
82     p_dialog->p_interaction = p_interaction;
83     p_dialog->p_parent = p_this;
84
85     if( p_dialog->i_type == INTERACT_DIALOG_TWOWAY )
86     {
87         return intf_WaitAnswer( p_interaction, p_dialog );
88     }
89     else
90     {
91         p_dialog->i_flags |=  DIALOG_GOT_ANSWER;
92         return intf_Send( p_interaction, p_dialog );
93     }
94 }
95
96 /**
97  * Destroy the interaction system
98  * \param The interaction object to destroy
99  * \return nothing
100  */
101 void intf_InteractionDestroy( interaction_t *p_interaction )
102 {
103     int i;
104
105     // Remove all dialogs - Interfaces must be able to clean up their data
106
107     for( i = p_interaction->i_dialogs -1 ; i >= 0; i-- )
108     {
109         interaction_dialog_t * p_dialog = p_interaction->pp_dialogs[i];
110         intf_InteractionDialogDestroy( p_dialog );
111         REMOVE_ELEM( p_interaction->pp_dialogs, p_interaction->i_dialogs, i );
112     }
113
114     vlc_object_destroy( p_interaction );
115 }
116
117 /**
118  * The main interaction processing loop
119  * This function is called from the playlist loop
120  *
121  * \param p_playlist the parent playlist
122  * \return nothing
123  */
124 void intf_InteractionManage( playlist_t *p_playlist )
125 {
126     vlc_value_t val;
127     int i_index;
128     interaction_t *p_interaction;
129
130     p_interaction = p_playlist->p_interaction;
131
132     // Nothing to do
133     if( p_interaction->i_dialogs == 0 ) return;
134
135     vlc_mutex_lock( &p_interaction->object_lock );
136
137     intf_InteractionSearchInterface( p_interaction );
138
139     if( !p_interaction->p_intf )
140     {
141         // We mark all dialogs as answered with their "default" answer
142         for( i_index = 0 ; i_index < p_interaction->i_dialogs; i_index ++ )
143         {
144             interaction_dialog_t *p_dialog = p_interaction->pp_dialogs[i_index];
145
146             // Give default answer
147             p_dialog->i_return = DIALOG_DEFAULT;
148             if( p_dialog->i_flags & DIALOG_OK_CANCEL )
149                 p_dialog->i_return = DIALOG_CANCELLED;
150
151             // Pretend we have hidden and destroyed it
152             if( p_dialog->i_status == HIDDEN_DIALOG )
153             {
154                 p_dialog->i_status = DESTROYED_DIALOG;
155             }
156             else
157             {
158                 p_dialog->i_status = HIDING_DIALOG;
159             }
160         }
161     }
162     else
163     {
164         vlc_object_yield( p_interaction->p_intf );
165     }
166
167     for( i_index = 0 ; i_index < p_interaction->i_dialogs; i_index ++ )
168     {
169         interaction_dialog_t *p_dialog = p_interaction->pp_dialogs[i_index];
170         switch( p_dialog->i_status )
171         {
172         case ANSWERED_DIALOG:
173             // Ask interface to hide it
174             p_dialog->i_action = INTERACT_HIDE;
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 = HIDING_DIALOG;
179             break;
180         case UPDATED_DIALOG:
181             p_dialog->i_action = INTERACT_UPDATE;
182             val.p_address = p_dialog;
183             if( p_interaction->p_intf )
184                 var_Set( p_interaction->p_intf, "interaction", val );
185             p_dialog->i_status = SENT_DIALOG;
186             break;
187         case HIDDEN_DIALOG:
188             if( !(p_dialog->i_flags & DIALOG_GOT_ANSWER) ) break;
189             if( !(p_dialog->i_flags & DIALOG_REUSABLE) )
190             {
191                 p_dialog->i_action = INTERACT_DESTROY;
192                 val.p_address = p_dialog;
193                 if( p_interaction->p_intf )
194                     var_Set( p_interaction->p_intf, "interaction", val );
195             }
196             break;
197         case DESTROYED_DIALOG:
198             // Interface has now destroyed it, remove it
199             REMOVE_ELEM( p_interaction->pp_dialogs, p_interaction->i_dialogs,
200                          i_index);
201             i_index--;
202             intf_InteractionDialogDestroy( p_dialog );
203             break;
204         case NEW_DIALOG:
205             // This is truly a new dialog, send it.
206             p_dialog->i_action = INTERACT_NEW;
207             val.p_address = p_dialog;
208             if( p_interaction->p_intf )
209                 var_Set( p_interaction->p_intf, "interaction", val );
210             p_dialog->i_status = SENT_DIALOG;
211             break;
212         }
213     }
214
215     if( p_interaction->p_intf )
216     {
217         vlc_object_release( p_interaction->p_intf );
218     }
219
220     vlc_mutex_unlock( &p_playlist->p_interaction->object_lock );
221 }
222
223
224
225 #define INTERACT_INIT( new )                                            \
226         new = (interaction_dialog_t*)malloc(                            \
227                         sizeof( interaction_dialog_t ) );               \
228         new->i_widgets = 0;                                             \
229         new->pp_widgets = NULL;                                         \
230         new->psz_title = NULL;                                          \
231         new->psz_description = NULL;                                    \
232         new->p_private = NULL;                                          \
233         new->i_id = 0;                                                  \
234         new->i_flags = 0;                                               \
235         new->i_status = NEW_DIALOG;
236
237 #define INTERACT_FREE( new )                                            \
238         if( new->psz_title ) free( new->psz_title );                    \
239         if( new->psz_description ) free( new->psz_description );
240
241 /** Helper function to send an error message
242  *  \param p_this     Parent vlc_object
243  *  \param i_id       A predefined ID, 0 if not applicable
244  *  \param psz_title  Title for the dialog
245  *  \param psz_format The message to display
246  *  */
247 void __intf_UserFatal( vlc_object_t *p_this,
248                        const char *psz_title,
249                        const char *psz_format, ... )
250 {
251     va_list args;
252     interaction_dialog_t *p_new = NULL;
253     user_widget_t *p_widget = NULL;
254     int i_id = DIALOG_ERRORS;
255
256     if( i_id > 0 )
257     {
258         p_new = intf_InteractionGetById( p_this, i_id );
259     }
260     if( !p_new )
261     {
262         INTERACT_INIT( p_new );
263         if( i_id > 0 ) p_new->i_id = i_id ;
264     }
265     else
266     {
267         p_new->i_status = UPDATED_DIALOG;
268     }
269
270     p_new->i_flags |= DIALOG_REUSABLE;
271
272     p_new->i_type = INTERACT_DIALOG_ONEWAY;
273     p_new->psz_title = strdup( psz_title );
274
275     p_widget = (user_widget_t* )malloc( sizeof( user_widget_t ) );
276
277     p_widget->i_type = WIDGET_TEXT;
278     p_widget->val.psz_string = NULL;
279
280     va_start( args, psz_format );
281     vasprintf( &p_widget->psz_text, psz_format, args );
282     va_end( args );
283
284     INSERT_ELEM ( p_new->pp_widgets,
285                   p_new->i_widgets,
286                   p_new->i_widgets,
287                   p_widget );
288
289     p_new->i_flags |= DIALOG_CLEAR_NOSHOW;
290
291     intf_Interact( p_this, p_new );
292 }
293
294 /** Helper function to ask a yes-no question
295  *  \param p_this           Parent vlc_object
296  *  \param psz_title        Title for the dialog
297  *  \param psz_description  A description
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
362 /** Update a progress bar
363  *  \param p_this           Parent vlc_object
364  *  \param i_id             Identifier of the dialog
365  *  \param psz_status       New status
366  *  \param f_position       New position (0.0->100.0)
367  *  \return                 nothing
368  */
369 void __intf_UserProgressUpdate( vlc_object_t *p_this, int i_id,
370                                 const char *psz_status, float f_pos )
371 {
372     interaction_t *p_interaction = intf_InteractionGet( p_this );
373     interaction_dialog_t *p_dialog;
374
375     if( !p_interaction ) return;
376
377     vlc_mutex_lock( &p_interaction->object_lock );
378     p_dialog  =  intf_InteractionGetById( p_this, i_id );
379
380     if( !p_dialog )
381     {
382         vlc_mutex_unlock( &p_interaction->object_lock ) ;
383         return;
384     }
385
386     if( p_dialog->pp_widgets[0]->psz_text )
387         free( p_dialog->pp_widgets[0]->psz_text );
388     p_dialog->pp_widgets[0]->psz_text = strdup( psz_status );
389
390     p_dialog->pp_widgets[0]->val.f_float = f_pos;
391
392     p_dialog->i_status = UPDATED_DIALOG;
393     vlc_mutex_unlock( &p_interaction->object_lock) ;
394 }
395
396 /** Helper function to make a login/password box
397  *  \param p_this           Parent vlc_object
398  *  \param psz_title        Title for the dialog
399  *  \param psz_description  A description
400  *  \param ppsz_login       Returned login
401  *  \param ppsz_password    Returned password
402  *  \return                 Clicked button code
403  */
404 int __intf_UserLoginPassword( vlc_object_t *p_this,
405                               const char *psz_title,
406                               const char *psz_description,
407                               char **ppsz_login,
408                               char **ppsz_password )
409 {
410
411     int i_ret;
412     interaction_dialog_t *p_new = NULL;
413
414     INTERACT_INIT( p_new );
415
416     p_new->i_type = INTERACT_DIALOG_TWOWAY;
417     p_new->psz_title = strdup( psz_title );
418     p_new->psz_description = strdup( psz_description );
419
420     p_new->i_flags = DIALOG_LOGIN_PW_OK_CANCEL;
421
422     i_ret = intf_Interact( p_this, p_new );
423
424     if( i_ret != DIALOG_CANCELLED )
425     {
426         *ppsz_login = strdup( p_new->psz_returned[0] );
427         *ppsz_password = strdup( p_new->psz_returned[1] );
428     }
429     return i_ret;
430 }
431
432 /** Hide an interaction dialog
433  * \param p_this the parent vlc object
434  * \param i_id the id of the item to hide
435  * \return nothing
436  */
437 void __intf_UserHide( vlc_object_t *p_this, int i_id )
438 {
439     interaction_t *p_interaction = intf_InteractionGet( p_this );
440     interaction_dialog_t *p_dialog;
441
442     if( !p_interaction ) return;
443
444     vlc_mutex_lock( &p_interaction->object_lock );
445     p_dialog  =  intf_InteractionGetById( p_this, i_id );
446
447     if( !p_dialog )
448     {
449        vlc_mutex_unlock( &p_interaction->object_lock );
450        return;
451     }
452
453     p_dialog->i_status = ANSWERED_DIALOG;
454     vlc_mutex_unlock( &p_interaction->object_lock );
455 }
456
457
458
459 /**********************************************************************
460  * The following functions are local
461  **********************************************************************/
462
463 /* Get the interaction object. Create it if needed */
464 static interaction_t * intf_InteractionGet( vlc_object_t *p_this )
465 {
466     playlist_t *p_playlist;
467     interaction_t *p_interaction;
468
469     p_playlist = (playlist_t*) vlc_object_find( p_this, VLC_OBJECT_PLAYLIST,
470                                                 FIND_ANYWHERE );
471
472     if( !p_playlist )
473     {
474         return NULL;
475     }
476
477     if( p_playlist->p_interaction == NULL )
478     {
479         intf_InteractionInit( p_playlist );
480     }
481
482     p_interaction = p_playlist->p_interaction;
483
484     vlc_object_release( p_playlist );
485
486     return p_interaction;
487 }
488
489 /* Create the interaction object in the given playlist object */
490 static void intf_InteractionInit( playlist_t *p_playlist )
491 {
492     interaction_t *p_interaction;
493
494     p_interaction = vlc_object_create( VLC_OBJECT( p_playlist ),
495                                        sizeof( interaction_t ) );
496     if( !p_interaction )
497     {
498         msg_Err( p_playlist,"out of memory" );
499         return;
500     }
501
502     p_interaction->i_dialogs = 0;
503     p_interaction->pp_dialogs = NULL;
504     p_interaction->p_intf = NULL;
505     p_interaction->i_last_id = DIALOG_LAST_PREDEFINED + 1;
506
507     vlc_mutex_init( p_interaction , &p_interaction->object_lock );
508
509     p_playlist->p_interaction  = p_interaction;
510 }
511
512 /* Look for an interface suitable for interaction */
513 static void intf_InteractionSearchInterface( interaction_t *p_interaction )
514 {
515     vlc_list_t  *p_list;
516     int          i_index;
517
518     p_interaction->p_intf = NULL;
519
520     p_list = vlc_list_find( p_interaction, VLC_OBJECT_INTF, FIND_ANYWHERE );
521     if( !p_list )
522     {
523         msg_Err( p_interaction, "unable to create module list" );
524         return;
525     }
526
527     for( i_index = 0; i_index < p_list->i_count; i_index ++ )
528     {
529         intf_thread_t *p_intf = (intf_thread_t *)
530                                         p_list->p_values[i_index].p_object;
531         if( p_intf->b_interaction )
532         {
533             p_interaction->p_intf = p_intf;
534             break;
535         }
536     }
537     vlc_list_release ( p_list );
538 }
539
540 /* Add a dialog to the queue and wait for answer */
541 static int intf_WaitAnswer( interaction_t *p_interact,
542                             interaction_dialog_t *p_dialog )
543 {
544     int i;
545     vlc_bool_t b_found = VLC_FALSE;
546     vlc_mutex_lock( &p_interact->object_lock );
547     for( i = 0 ; i< p_interact->i_dialogs; i++ )
548     {
549         if( p_interact->pp_dialogs[i]->i_id == p_dialog->i_id )
550         {
551             b_found = VLC_TRUE;
552         }
553     }
554     if( ! b_found )
555     {
556         INSERT_ELEM( p_interact->pp_dialogs,
557                      p_interact->i_dialogs,
558                      p_interact->i_dialogs,
559                      p_dialog );
560     }
561     else
562         p_dialog->i_status = UPDATED_DIALOG;
563     vlc_mutex_unlock( &p_interact->object_lock );
564
565     /// \todo Check that the initiating object is not dying
566     while( p_dialog->i_status != ANSWERED_DIALOG &&
567            p_dialog->i_status != HIDING_DIALOG &&
568            p_dialog->i_status != HIDDEN_DIALOG &&
569            !p_dialog->p_parent->b_die )
570     {
571         msleep( 100000 );
572     }
573     /// \todo locking
574     if( p_dialog->p_parent->b_die )
575     {
576         p_dialog->i_return = DIALOG_CANCELLED;
577         p_dialog->i_status = ANSWERED_DIALOG;
578     }
579     p_dialog->i_flags |= DIALOG_GOT_ANSWER;
580     return p_dialog->i_return;
581 }
582
583 /* Add a dialog to the queue and return */
584 static int intf_Send( interaction_t *p_interact,
585                       interaction_dialog_t *p_dialog )
586 {
587     int i;
588     vlc_bool_t b_found = VLC_FALSE;
589     if( p_interact == NULL ) return VLC_ENOOBJ;
590     vlc_mutex_lock( &p_interact->object_lock );
591
592     for( i = 0 ; i< p_interact->i_dialogs; i++ )
593     {
594         if( p_interact->pp_dialogs[i]->i_id == p_dialog->i_id )
595         {
596             b_found = VLC_TRUE;
597         }
598     }
599     if( !b_found )
600     {
601         INSERT_ELEM( p_interact->pp_dialogs,
602                      p_interact->i_dialogs,
603                      p_interact->i_dialogs,
604                      p_dialog );
605     }
606     else
607         p_dialog->i_status = UPDATED_DIALOG;
608     // Pretend we already retrieved the "answer"
609     p_dialog->i_flags |= DIALOG_GOT_ANSWER;
610     vlc_mutex_unlock( &p_interact->object_lock );
611     return VLC_SUCCESS;
612 }
613
614 /* Find an interaction dialog by its id */
615 static interaction_dialog_t *intf_InteractionGetById( vlc_object_t* p_this,
616                                                        int i_id )
617 {
618     interaction_t *p_interaction = intf_InteractionGet( p_this );
619     int i;
620
621     if( !p_interaction ) return NULL;
622
623     for( i = 0 ; i< p_interaction->i_dialogs; i++ )
624     {
625         if( p_interaction->pp_dialogs[i]->i_id == i_id )
626         {
627             return p_interaction->pp_dialogs[i];
628         }
629     }
630     return NULL;
631 }
632
633 #define FREE( i ) { if( i ) free( i ); i = NULL; }
634
635 static void intf_InteractionDialogDestroy( interaction_dialog_t *p_dialog )
636 {
637     int i;
638     for( i = p_dialog->i_widgets - 1 ; i >= 0 ; i-- )
639     {
640         user_widget_t *p_widget = p_dialog->pp_widgets[i];
641         FREE( p_widget->psz_text );
642         if( p_widget->i_type == WIDGET_INPUT_TEXT )
643         {
644             FREE( p_widget->val.psz_string );
645         }
646
647         REMOVE_ELEM( p_dialog->pp_widgets, p_dialog->i_widgets, i );
648         free( p_widget );
649     }
650     FREE( p_dialog->psz_title );
651     FREE( p_dialog->psz_description );
652     
653     FREE( p_dialog->psz_returned[0] );
654     FREE( p_dialog->psz_returned[1] );
655
656     free( p_dialog );
657 }