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