]> git.sesse.net Git - vlc/blob - src/interface/interaction.c
* new dialogue type to prompt the user for a string
[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 #define INTERACT_INIT( new )                                            \
225         new = (interaction_dialog_t*)malloc(                            \
226                         sizeof( interaction_dialog_t ) );               \
227         new->psz_title = NULL;                                          \
228         new->psz_description = NULL;                                    \
229         new->p_private = NULL;                                          \
230         new->i_id = 0;                                                  \
231         new->i_flags = 0;                                               \
232         new->i_status = NEW_DIALOG;
233
234 #define INTERACT_FREE( new )                                            \
235         if( new->psz_title ) free( new->psz_title );                    \
236         if( new->psz_description ) free( new->psz_description );
237
238 /** Helper function to send an error message
239  *  \param p_this     Parent vlc_object
240  *  \param i_id       A predefined ID, 0 if not applicable
241  *  \param psz_title  Title for the dialog
242  *  \param psz_format The message to display
243  *  */
244 void __intf_UserFatal( vlc_object_t *p_this,
245                        const char *psz_title,
246                        const char *psz_format, ... )
247 {
248     va_list args;
249     interaction_dialog_t *p_new = NULL;
250     int i_id = DIALOG_ERRORS;
251
252     if( i_id > 0 )
253     {
254         p_new = intf_InteractionGetById( p_this, i_id );
255     }
256     if( !p_new )
257     {
258         INTERACT_INIT( p_new );
259         if( i_id > 0 ) p_new->i_id = i_id ;
260     }
261     else
262     {
263         p_new->i_status = UPDATED_DIALOG;
264     }
265
266     p_new->i_flags |= DIALOG_REUSABLE;
267
268     p_new->i_type = INTERACT_DIALOG_ONEWAY;
269     p_new->psz_title = strdup( psz_title );
270
271     va_start( args, psz_format );
272     vasprintf( &p_new->psz_description, psz_format, args );
273     va_end( args );
274
275     p_new->i_flags |= DIALOG_CLEAR_NOSHOW;
276
277     intf_Interact( p_this, p_new );
278 }
279
280 /** Helper function to ask a yes-no question
281  *  \param p_this           Parent vlc_object
282  *  \param psz_title        Title for the dialog
283  *  \param psz_description  A description
284  *  \return                 Clicked button code
285  */
286 int __intf_UserYesNo( vlc_object_t *p_this,
287                       const char *psz_title,
288                       const char *psz_description )
289 {
290     int i_ret;
291     interaction_dialog_t *p_new = NULL;
292
293     INTERACT_INIT( p_new );
294
295     p_new->i_type = INTERACT_DIALOG_TWOWAY;
296     p_new->psz_title = strdup( psz_title );
297     p_new->psz_description = strdup( psz_description );
298     p_new->i_flags = DIALOG_YES_NO_CANCEL;
299
300     i_ret = intf_Interact( p_this, p_new );
301
302     return i_ret;
303 }
304
305 /** Helper function to trigger a okay-cancel dialogue
306  *  \param p_this           Parent vlc_object
307  *  \param psz_title        Title for the dialog
308  *  \param psz_description  A description
309  *  \return                 Clicked button code
310  */
311 int __intf_UserOkayCancel( vlc_object_t *p_this,
312                       const char *psz_title,
313                       const char *psz_description )
314 {
315     int i_ret;
316     interaction_dialog_t *p_new = NULL;
317
318     INTERACT_INIT( p_new );
319
320     p_new->i_type = INTERACT_DIALOG_TWOWAY;
321     p_new->psz_title = strdup( psz_title );
322     p_new->psz_description = strdup( psz_description );
323     p_new->i_flags = DIALOG_OK_CANCEL;
324
325     i_ret = intf_Interact( p_this, p_new );
326
327     return i_ret;
328 }
329
330 /** Helper function to create a dialogue showing a progress-bar with some info
331  *  \param p_this           Parent vlc_object
332  *  \param psz_title        Title for the dialog
333  *  \param psz_status       Current status
334  *  \param f_position       Current position (0.0->100.0)
335  *  \return                 Dialog id, to give to UserProgressUpdate
336  */
337 int __intf_UserProgress( vlc_object_t *p_this,
338                          const char *psz_title,
339                          const char *psz_status,
340                          float f_pos )
341 {
342     int i_ret;
343     interaction_dialog_t *p_new = NULL;
344
345     INTERACT_INIT( p_new );
346
347     p_new->i_type = INTERACT_DIALOG_ONEWAY;
348     p_new->psz_title = strdup( psz_title );
349     p_new->psz_description = strdup( psz_status );
350     p_new->val.f_float = f_pos;
351
352     p_new->i_flags = DIALOG_USER_PROGRESS;
353
354     i_ret = intf_Interact( p_this, p_new );
355
356     return p_new->i_id;
357 }
358
359 /** Update a progress bar in a dialogue
360  *  \param p_this           Parent vlc_object
361  *  \param i_id             Identifier of the dialog
362  *  \param psz_status       New status
363  *  \param f_position       New position (0.0->100.0)
364  *  \return                 nothing
365  */
366 void __intf_UserProgressUpdate( vlc_object_t *p_this, int i_id,
367                                 const char *psz_status, float f_pos )
368 {
369     interaction_t *p_interaction = intf_InteractionGet( p_this );
370     interaction_dialog_t *p_dialog;
371
372     if( !p_interaction ) return;
373
374     vlc_mutex_lock( &p_interaction->object_lock );
375     p_dialog  =  intf_InteractionGetById( p_this, i_id );
376
377     if( !p_dialog )
378     {
379         vlc_mutex_unlock( &p_interaction->object_lock ) ;
380         return;
381     }
382
383     if( p_dialog->psz_description )
384         free( p_dialog->psz_description );
385     p_dialog->psz_description = strdup( psz_status );
386
387     p_dialog->val.f_float = f_pos;
388
389     p_dialog->i_status = UPDATED_DIALOG;
390     vlc_mutex_unlock( &p_interaction->object_lock) ;
391 }
392
393 /** Helper function to make a login/password dialogue
394  *  \param p_this           Parent vlc_object
395  *  \param psz_title        Title for the dialog
396  *  \param psz_description  A description
397  *  \param ppsz_login       Returned login
398  *  \param ppsz_password    Returned password
399  *  \return                 Clicked button code
400  */
401 int __intf_UserLoginPassword( vlc_object_t *p_this,
402                               const char *psz_title,
403                               const char *psz_description,
404                               char **ppsz_login,
405                               char **ppsz_password )
406 {
407
408     int i_ret;
409     interaction_dialog_t *p_new = NULL;
410
411     INTERACT_INIT( p_new );
412
413     p_new->i_type = INTERACT_DIALOG_TWOWAY;
414     p_new->psz_title = strdup( psz_title );
415     p_new->psz_description = strdup( psz_description );
416
417     p_new->i_flags = DIALOG_LOGIN_PW_OK_CANCEL;
418
419     i_ret = intf_Interact( p_this, p_new );
420
421     if( i_ret != DIALOG_CANCELLED )
422     {
423         *ppsz_login = strdup( p_new->psz_returned[0] );
424         *ppsz_password = strdup( p_new->psz_returned[1] );
425     }
426     return i_ret;
427 }
428
429 /** Helper function to make a dialogue asking the user for !password string
430  *  \param p_this           Parent vlc_object
431  *  \param psz_title        Title for the dialog
432  *  \param psz_description  A description
433  *  \param ppsz_usersString Returned login
434  *  \return                 Clicked button code
435  */
436 int __intf_UserStringInput( vlc_object_t *p_this,
437                               const char *psz_title,
438                               const char *psz_description,
439                               char **ppsz_usersString )
440 {
441
442     int i_ret;
443     interaction_dialog_t *p_new = NULL;
444
445     INTERACT_INIT( p_new );
446
447     p_new->i_type = INTERACT_DIALOG_TWOWAY;
448     p_new->psz_title = strdup( psz_title );
449     p_new->psz_description = strdup( psz_description );
450
451     p_new->i_flags = DIALOG_PSZ_INPUT_OK_CANCEL;
452
453     i_ret = intf_Interact( p_this, p_new );
454
455     if( i_ret != DIALOG_CANCELLED )
456     {
457         *ppsz_usersString = strdup( p_new->psz_returned[0] );
458     }
459     return i_ret;
460 }
461
462 /** Hide an interaction dialog
463  * \param p_this the parent vlc object
464  * \param i_id the id of the item to hide
465  * \return nothing
466  */
467 void __intf_UserHide( vlc_object_t *p_this, int i_id )
468 {
469     interaction_t *p_interaction = intf_InteractionGet( p_this );
470     interaction_dialog_t *p_dialog;
471
472     if( !p_interaction ) return;
473
474     vlc_mutex_lock( &p_interaction->object_lock );
475     p_dialog  =  intf_InteractionGetById( p_this, i_id );
476
477     if( !p_dialog )
478     {
479        vlc_mutex_unlock( &p_interaction->object_lock );
480        return;
481     }
482
483     p_dialog->i_status = ANSWERED_DIALOG;
484     vlc_mutex_unlock( &p_interaction->object_lock );
485 }
486
487
488
489 /**********************************************************************
490  * The following functions are local
491  **********************************************************************/
492
493 /* Get the interaction object. Create it if needed */
494 static interaction_t * intf_InteractionGet( vlc_object_t *p_this )
495 {
496     playlist_t *p_playlist;
497     interaction_t *p_interaction;
498
499     p_playlist = (playlist_t*) vlc_object_find( p_this, VLC_OBJECT_PLAYLIST,
500                                                 FIND_ANYWHERE );
501
502     if( !p_playlist )
503     {
504         return NULL;
505     }
506
507     if( p_playlist->p_interaction == NULL )
508     {
509         intf_InteractionInit( p_playlist );
510     }
511
512     p_interaction = p_playlist->p_interaction;
513
514     vlc_object_release( p_playlist );
515
516     return p_interaction;
517 }
518
519 /* Create the interaction object in the given playlist object */
520 static void intf_InteractionInit( playlist_t *p_playlist )
521 {
522     interaction_t *p_interaction;
523
524     p_interaction = vlc_object_create( VLC_OBJECT( p_playlist ),
525                                        sizeof( interaction_t ) );
526     if( !p_interaction )
527     {
528         msg_Err( p_playlist,"out of memory" );
529         return;
530     }
531
532     p_interaction->i_dialogs = 0;
533     p_interaction->pp_dialogs = NULL;
534     p_interaction->p_intf = NULL;
535     p_interaction->i_last_id = DIALOG_LAST_PREDEFINED + 1;
536
537     vlc_mutex_init( p_interaction , &p_interaction->object_lock );
538
539     p_playlist->p_interaction  = p_interaction;
540 }
541
542 /* Look for an interface suitable for interaction */
543 static void intf_InteractionSearchInterface( interaction_t *p_interaction )
544 {
545     vlc_list_t  *p_list;
546     int          i_index;
547
548     p_interaction->p_intf = NULL;
549
550     p_list = vlc_list_find( p_interaction, VLC_OBJECT_INTF, FIND_ANYWHERE );
551     if( !p_list )
552     {
553         msg_Err( p_interaction, "unable to create module list" );
554         return;
555     }
556
557     for( i_index = 0; i_index < p_list->i_count; i_index ++ )
558     {
559         intf_thread_t *p_intf = (intf_thread_t *)
560                                         p_list->p_values[i_index].p_object;
561         if( p_intf->b_interaction )
562         {
563             p_interaction->p_intf = p_intf;
564             break;
565         }
566     }
567     vlc_list_release ( p_list );
568 }
569
570 /* Add a dialog to the queue and wait for answer */
571 static int intf_WaitAnswer( interaction_t *p_interact,
572                             interaction_dialog_t *p_dialog )
573 {
574     int i;
575     vlc_bool_t b_found = VLC_FALSE;
576     vlc_mutex_lock( &p_interact->object_lock );
577     for( i = 0 ; i< p_interact->i_dialogs; i++ )
578     {
579         if( p_interact->pp_dialogs[i]->i_id == p_dialog->i_id )
580         {
581             b_found = VLC_TRUE;
582         }
583     }
584     if( ! b_found )
585     {
586         INSERT_ELEM( p_interact->pp_dialogs,
587                      p_interact->i_dialogs,
588                      p_interact->i_dialogs,
589                      p_dialog );
590     }
591     else
592         p_dialog->i_status = UPDATED_DIALOG;
593     vlc_mutex_unlock( &p_interact->object_lock );
594
595     /// \todo Check that the initiating object is not dying
596     while( p_dialog->i_status != ANSWERED_DIALOG &&
597            p_dialog->i_status != HIDING_DIALOG &&
598            p_dialog->i_status != HIDDEN_DIALOG &&
599            !p_dialog->p_parent->b_die )
600     {
601         msleep( 100000 );
602     }
603     /// \todo locking
604     if( p_dialog->p_parent->b_die )
605     {
606         p_dialog->i_return = DIALOG_CANCELLED;
607         p_dialog->i_status = ANSWERED_DIALOG;
608     }
609     p_dialog->i_flags |= DIALOG_GOT_ANSWER;
610     return p_dialog->i_return;
611 }
612
613 /* Add a dialog to the queue and return */
614 static int intf_Send( interaction_t *p_interact,
615                       interaction_dialog_t *p_dialog )
616 {
617     int i;
618     vlc_bool_t b_found = VLC_FALSE;
619     if( p_interact == NULL ) return VLC_ENOOBJ;
620     vlc_mutex_lock( &p_interact->object_lock );
621
622     for( i = 0 ; i< p_interact->i_dialogs; i++ )
623     {
624         if( p_interact->pp_dialogs[i]->i_id == p_dialog->i_id )
625         {
626             b_found = VLC_TRUE;
627         }
628     }
629     if( !b_found )
630     {
631         INSERT_ELEM( p_interact->pp_dialogs,
632                      p_interact->i_dialogs,
633                      p_interact->i_dialogs,
634                      p_dialog );
635     }
636     else
637         p_dialog->i_status = UPDATED_DIALOG;
638     // Pretend we already retrieved the "answer"
639     p_dialog->i_flags |= DIALOG_GOT_ANSWER;
640     vlc_mutex_unlock( &p_interact->object_lock );
641     return VLC_SUCCESS;
642 }
643
644 /* Find an interaction dialog by its id */
645 static interaction_dialog_t *intf_InteractionGetById( vlc_object_t* p_this,
646                                                        int i_id )
647 {
648     interaction_t *p_interaction = intf_InteractionGet( p_this );
649     int i;
650
651     if( !p_interaction ) return NULL;
652
653     for( i = 0 ; i< p_interaction->i_dialogs; i++ )
654     {
655         if( p_interaction->pp_dialogs[i]->i_id == i_id )
656         {
657             return p_interaction->pp_dialogs[i];
658         }
659     }
660     return NULL;
661 }
662
663 #define FREE( i ) { if( i ) free( i ); i = NULL; }
664
665 static void intf_InteractionDialogDestroy( interaction_dialog_t *p_dialog )
666 {
667  /*   FREE( p_dialog->val.psz_string );
668
669     FREE( p_dialog->psz_title );
670     FREE( p_dialog->psz_description );
671     
672     FREE( p_dialog->psz_returned[0] );
673     FREE( p_dialog->psz_returned[1] ); */
674
675     free( p_dialog );
676 }