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