1 /*****************************************************************************
2 * interaction.c: User interaction functions
3 *****************************************************************************
4 * Copyright (C) 2005-2006 VideoLAN
7 * Authors: Clément Stenac <zorglub@videolan.org>
8 * Felix Kühne <fkuehne@videolan.org>
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.
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.
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 *****************************************************************************/
27 * This file contains functions related to user interaction management
30 /*****************************************************************************
32 *****************************************************************************/
33 #include <stdlib.h> /* free(), strtol() */
34 #include <stdio.h> /* FILE */
35 #include <string.h> /* strerror() */
38 #include <vlc/input.h>
40 #include "vlc_interaction.h"
41 #include "vlc_interface.h"
42 #include "vlc_playlist.h"
44 /*****************************************************************************
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 *
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 );
60 * Send an interaction element to the user
62 * \param p_this the calling vlc_object_t
63 * \param p_interact the interaction element
64 * \return VLC_SUCCESS or an error code
66 int __intf_Interact( vlc_object_t *p_this, interaction_dialog_t *p_dialog )
68 interaction_t *p_interaction = intf_InteractionGet( p_this );
70 /* Get an id, if we don't already have one */
71 if( p_dialog->i_id == 0 )
73 p_dialog->i_id = ++p_interaction->i_last_id;
76 if( p_this->i_flags & OBJECT_FLAGS_NOINTERACT ) return VLC_EGENERIC;
78 if( !config_GetInt(p_this, "interact") ) return VLC_EGENERIC;
80 p_dialog->p_interaction = p_interaction;
81 p_dialog->p_parent = p_this;
83 if( p_dialog->i_type == INTERACT_DIALOG_TWOWAY )
85 return intf_WaitAnswer( p_interaction, p_dialog );
89 p_dialog->i_flags |= DIALOG_GOT_ANSWER;
90 return intf_Send( p_interaction, p_dialog );
95 * Destroy the interaction system
96 * \param The interaction object to destroy
99 void intf_InteractionDestroy( interaction_t *p_interaction )
103 // Remove all dialogs - Interfaces must be able to clean up their data
105 for( i = p_interaction->i_dialogs -1 ; i >= 0; i-- )
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 );
112 vlc_object_destroy( p_interaction );
116 * The main interaction processing loop
117 * This function is called from the playlist loop
119 * \param p_playlist the parent playlist
122 void intf_InteractionManage( playlist_t *p_playlist )
126 interaction_t *p_interaction;
128 p_interaction = p_playlist->p_interaction;
131 if( p_interaction->i_dialogs == 0 ) return;
133 vlc_mutex_lock( &p_interaction->object_lock );
135 intf_InteractionSearchInterface( p_interaction );
137 if( !p_interaction->p_intf )
139 // We mark all dialogs as answered with their "default" answer
140 for( i_index = 0 ; i_index < p_interaction->i_dialogs; i_index ++ )
142 interaction_dialog_t *p_dialog = p_interaction->pp_dialogs[i_index];
144 // Give default answer
145 p_dialog->i_return = DIALOG_DEFAULT;
147 // Pretend we have hidden and destroyed it
148 if( p_dialog->i_status == HIDDEN_DIALOG )
150 p_dialog->i_status = DESTROYED_DIALOG;
154 p_dialog->i_status = HIDING_DIALOG;
160 vlc_object_yield( p_interaction->p_intf );
163 for( i_index = 0 ; i_index < p_interaction->i_dialogs; i_index ++ )
165 interaction_dialog_t *p_dialog = p_interaction->pp_dialogs[i_index];
166 switch( p_dialog->i_status )
168 case ANSWERED_DIALOG:
169 // Ask interface to hide it
170 p_dialog->i_action = INTERACT_HIDE;
171 val.p_address = p_dialog;
172 if( p_interaction->p_intf )
173 var_Set( p_interaction->p_intf, "interaction", val );
174 p_dialog->i_status = HIDING_DIALOG;
177 p_dialog->i_action = INTERACT_UPDATE;
178 val.p_address = p_dialog;
179 if( p_interaction->p_intf )
180 var_Set( p_interaction->p_intf, "interaction", val );
181 p_dialog->i_status = SENT_DIALOG;
184 if( !(p_dialog->i_flags & DIALOG_GOT_ANSWER) ) break;
185 if( !(p_dialog->i_flags & DIALOG_REUSABLE) )
187 p_dialog->i_action = INTERACT_DESTROY;
188 val.p_address = p_dialog;
189 if( p_interaction->p_intf )
190 var_Set( p_interaction->p_intf, "interaction", val );
193 case DESTROYED_DIALOG:
194 // Interface has now destroyed it, remove it
195 REMOVE_ELEM( p_interaction->pp_dialogs, p_interaction->i_dialogs,
198 intf_InteractionDialogDestroy( p_dialog );
201 // This is truly a new dialog, send it.
202 p_dialog->i_action = INTERACT_NEW;
203 val.p_address = p_dialog;
204 if( p_interaction->p_intf )
205 var_Set( p_interaction->p_intf, "interaction", val );
206 p_dialog->i_status = SENT_DIALOG;
211 if( p_interaction->p_intf )
213 vlc_object_release( p_interaction->p_intf );
216 vlc_mutex_unlock( &p_playlist->p_interaction->object_lock );
220 #define INTERACT_INIT( new ) \
221 new = (interaction_dialog_t*)malloc( \
222 sizeof( interaction_dialog_t ) ); \
223 new->psz_title = NULL; \
224 new->psz_description = NULL; \
225 new->psz_defaultButton = NULL; \
226 new->psz_alternateButton = NULL; \
227 new->psz_otherButton = NULL; \
228 new->i_timeToGo = 0; \
229 new->b_cancelled = VLC_FALSE; \
230 new->p_private = NULL; \
233 new->i_status = NEW_DIALOG;
235 #define INTERACT_FREE( new ) \
236 if( new->psz_title ) free( new->psz_title ); \
237 if( new->psz_description ) free( new->psz_description );
239 /** Helper function to send an error message, both in a blocking and non-blocking way
240 * \param p_this Parent vlc_object
241 * \param b_blocking Is this dialog blocking or not?
242 * \param psz_title Title for the dialog
243 * \param psz_format The message to display
245 void __intf_UserFatal( vlc_object_t *p_this,
246 vlc_bool_t b_blocking,
247 const char *psz_title,
248 const char *psz_format, ... )
251 interaction_dialog_t *p_new = NULL;
253 INTERACT_INIT( p_new );
255 p_new->psz_title = strdup( psz_title );
257 va_start( args, psz_format );
258 vasprintf( &p_new->psz_description, psz_format, args );
262 p_new->i_flags = DIALOG_BLOCKING_ERROR;
264 p_new->i_flags = DIALOG_NONBLOCKING_ERROR;
266 intf_Interact( p_this, p_new );
269 /** Helper function to send an warning, which is always shown non-blocking
270 * \param p_this Parent vlc_object
271 * \param psz_title Title for the dialog
272 * \param psz_format The message to display
274 void __intf_UserWarn( vlc_object_t *p_this,
275 const char *psz_title,
276 const char *psz_format, ... )
279 interaction_dialog_t *p_new = NULL;
281 INTERACT_INIT( p_new );
283 p_new->psz_title = strdup( psz_title );
285 va_start( args, psz_format );
286 vasprintf( &p_new->psz_description, psz_format, args );
289 p_new->i_flags = DIALOG_WARNING;
291 intf_Interact( p_this, p_new );
294 /** Helper function to ask a yes-no-cancel question
295 * \param p_this Parent vlc_object
296 * \param psz_title Title for the dialog
297 * \param psz_description A description
298 * \param psz_default caption for the default button
299 * \param psz_alternate caption for the alternate button
300 * \param psz_other caption for the optional 3rd button (== cancel)
301 * \return Clicked button code
303 int __intf_UserYesNo( vlc_object_t *p_this,
304 const char *psz_title,
305 const char *psz_description,
306 const char *psz_default,
307 const char *psz_alternate,
308 const char *psz_other )
311 interaction_dialog_t *p_new = NULL;
313 INTERACT_INIT( p_new );
315 p_new->i_type = INTERACT_DIALOG_TWOWAY;
316 p_new->psz_title = strdup( psz_title );
317 p_new->psz_description = strdup( psz_description );
318 p_new->i_flags = DIALOG_YES_NO_CANCEL;
319 p_new->psz_defaultButton = strdup( psz_default );
320 p_new->psz_alternateButton = strdup( psz_alternate );
322 p_new->psz_otherButton = strdup( psz_other );
324 p_new->psz_otherButton = NULL;
326 i_ret = intf_Interact( p_this, p_new );
331 /** Helper function to create a dialogue showing a progress-bar with some info
332 * \param p_this Parent vlc_object
333 * \param psz_title Title for the dialog
334 * \param psz_status Current status
335 * \param f_position Current position (0.0->100.0)
336 * \param i_timeToGo Time (in sec) to go until process is finished
337 * \return Dialog id, to give to UserProgressUpdate
339 int __intf_UserProgress( vlc_object_t *p_this,
340 const char *psz_title,
341 const char *psz_status,
346 interaction_dialog_t *p_new = NULL;
348 INTERACT_INIT( p_new );
350 p_new->i_type = INTERACT_DIALOG_ONEWAY;
351 p_new->psz_title = strdup( psz_title );
352 p_new->psz_description = strdup( psz_status );
353 p_new->val.f_float = f_pos;
354 p_new->i_timeToGo = i_time;
356 p_new->i_flags = DIALOG_USER_PROGRESS;
358 i_ret = intf_Interact( p_this, p_new );
363 /** Update a progress bar in a dialogue
364 * \param p_this Parent vlc_object
365 * \param i_id Identifier of the dialog
366 * \param psz_status New status
367 * \param f_position New position (0.0->100.0)
368 * \param i_timeToGo Time (in sec) to go until process is finished
371 void __intf_UserProgressUpdate( vlc_object_t *p_this, int i_id,
372 const char *psz_status, float f_pos,
375 interaction_t *p_interaction = intf_InteractionGet( p_this );
376 interaction_dialog_t *p_dialog;
378 if( !p_interaction ) return;
380 vlc_mutex_lock( &p_interaction->object_lock );
381 p_dialog = intf_InteractionGetById( p_this, i_id );
385 vlc_mutex_unlock( &p_interaction->object_lock ) ;
389 if( p_dialog->psz_description )
390 free( p_dialog->psz_description );
391 p_dialog->psz_description = strdup( psz_status );
393 p_dialog->val.f_float = f_pos;
394 p_dialog->i_timeToGo = i_time;
396 p_dialog->i_status = UPDATED_DIALOG;
397 vlc_mutex_unlock( &p_interaction->object_lock) ;
400 /** Helper function to communicate dialogue cancellations between the intf-module and caller
401 * \param p_this Parent vlc_object
402 * \param i_id Identifier of the dialogue
403 * \return Either true or false
405 vlc_bool_t __intf_UserProgressIsCancelled( vlc_object_t *p_this, int i_id )
407 interaction_t *p_interaction = intf_InteractionGet( p_this );
408 interaction_dialog_t *p_dialog;
410 if( !p_interaction ) return VLC_TRUE;
412 vlc_mutex_lock( &p_interaction->object_lock );
413 p_dialog = intf_InteractionGetById( p_this, i_id );
417 vlc_mutex_unlock( &p_interaction->object_lock ) ;
421 vlc_mutex_unlock( &p_interaction->object_lock) ;
422 return p_dialog->b_cancelled;
425 /** Helper function to make a login/password dialogue
426 * \param p_this Parent vlc_object
427 * \param psz_title Title for the dialog
428 * \param psz_description A description
429 * \param ppsz_login Returned login
430 * \param ppsz_password Returned password
431 * \return Clicked button code
433 int __intf_UserLoginPassword( vlc_object_t *p_this,
434 const char *psz_title,
435 const char *psz_description,
437 char **ppsz_password )
441 interaction_dialog_t *p_new = NULL;
443 INTERACT_INIT( p_new );
445 p_new->i_type = INTERACT_DIALOG_TWOWAY;
446 p_new->psz_title = strdup( psz_title );
447 p_new->psz_description = strdup( psz_description );
449 p_new->i_flags = DIALOG_LOGIN_PW_OK_CANCEL;
451 i_ret = intf_Interact( p_this, p_new );
453 if( i_ret != DIALOG_CANCELLED )
455 *ppsz_login = strdup( p_new->psz_returned[0] );
456 *ppsz_password = strdup( p_new->psz_returned[1] );
461 /** Helper function to make a dialogue asking the user for !password string
462 * \param p_this Parent vlc_object
463 * \param psz_title Title for the dialog
464 * \param psz_description A description
465 * \param ppsz_usersString Returned login
466 * \return Clicked button code
468 int __intf_UserStringInput( vlc_object_t *p_this,
469 const char *psz_title,
470 const char *psz_description,
471 char **ppsz_usersString )
475 interaction_dialog_t *p_new = NULL;
477 INTERACT_INIT( p_new );
479 p_new->i_type = INTERACT_DIALOG_TWOWAY;
480 p_new->psz_title = strdup( psz_title );
481 p_new->psz_description = strdup( psz_description );
483 p_new->i_flags = DIALOG_PSZ_INPUT_OK_CANCEL;
485 i_ret = intf_Interact( p_this, p_new );
487 if( i_ret != DIALOG_CANCELLED )
489 *ppsz_usersString = strdup( p_new->psz_returned[0] );
494 /** Helper function to create a progress-bar in the main interface with a
495 * single-line description
496 * \param p_this Parent vlc_object
497 * \param psz_status Current status
498 * \param f_position Current position (0.0->100.0)
499 * \return Dialog id, to give to IntfProgressUpdate
501 int __intf_IntfProgress( vlc_object_t *p_this,
502 const char *psz_status,
506 interaction_dialog_t *p_new = NULL;
508 INTERACT_INIT( p_new );
510 p_new->i_type = INTERACT_DIALOG_ONEWAY;
511 p_new->psz_description = strdup( psz_status );
512 p_new->val.f_float = f_pos;
514 p_new->i_flags = DIALOG_INTF_PROGRESS;
516 i_ret = intf_Interact( p_this, p_new );
521 /** Update the progress bar in the main interface
522 * \param p_this Parent vlc_object
523 * \param i_id Identifier of the dialog
524 * \param psz_status New status
525 * \param f_position New position (0.0->100.0)
528 void __intf_IntfProgressUpdate( vlc_object_t *p_this, int i_id,
529 const char *psz_status, float f_pos )
531 interaction_t *p_interaction = intf_InteractionGet( p_this );
532 interaction_dialog_t *p_dialog;
534 if( !p_interaction ) return;
536 vlc_mutex_lock( &p_interaction->object_lock );
537 p_dialog = intf_InteractionGetById( p_this, i_id );
541 vlc_mutex_unlock( &p_interaction->object_lock ) ;
545 if( p_dialog->psz_description )
546 free( p_dialog->psz_description );
547 p_dialog->psz_description = strdup( psz_status );
549 p_dialog->val.f_float = f_pos;
551 p_dialog->i_status = UPDATED_DIALOG;
552 vlc_mutex_unlock( &p_interaction->object_lock) ;
555 /** Hide an interaction dialog
556 * \param p_this the parent vlc object
557 * \param i_id the id of the item to hide
560 void __intf_UserHide( vlc_object_t *p_this, int i_id )
562 interaction_t *p_interaction = intf_InteractionGet( p_this );
563 interaction_dialog_t *p_dialog;
565 if( !p_interaction ) return;
567 vlc_mutex_lock( &p_interaction->object_lock );
568 p_dialog = intf_InteractionGetById( p_this, i_id );
572 vlc_mutex_unlock( &p_interaction->object_lock );
576 p_dialog->i_status = ANSWERED_DIALOG;
577 vlc_mutex_unlock( &p_interaction->object_lock );
582 /**********************************************************************
583 * The following functions are local
584 **********************************************************************/
586 /* Get the interaction object. Create it if needed */
587 static interaction_t * intf_InteractionGet( vlc_object_t *p_this )
589 playlist_t *p_playlist;
590 interaction_t *p_interaction;
592 p_playlist = (playlist_t*) vlc_object_find( p_this, VLC_OBJECT_PLAYLIST,
600 if( p_playlist->p_interaction == NULL )
602 intf_InteractionInit( p_playlist );
605 p_interaction = p_playlist->p_interaction;
607 vlc_object_release( p_playlist );
609 return p_interaction;
612 /* Create the interaction object in the given playlist object */
613 static void intf_InteractionInit( playlist_t *p_playlist )
615 interaction_t *p_interaction;
617 p_interaction = vlc_object_create( VLC_OBJECT( p_playlist ),
618 sizeof( interaction_t ) );
621 msg_Err( p_playlist,"out of memory" );
625 p_interaction->i_dialogs = 0;
626 p_interaction->pp_dialogs = NULL;
627 p_interaction->p_intf = NULL;
628 p_interaction->i_last_id = DIALOG_LAST_PREDEFINED + 1;
630 vlc_mutex_init( p_interaction , &p_interaction->object_lock );
632 p_playlist->p_interaction = p_interaction;
635 /* Look for an interface suitable for interaction */
636 static void intf_InteractionSearchInterface( interaction_t *p_interaction )
641 p_interaction->p_intf = NULL;
643 p_list = vlc_list_find( p_interaction, VLC_OBJECT_INTF, FIND_ANYWHERE );
646 msg_Err( p_interaction, "unable to create module list" );
650 for( i_index = 0; i_index < p_list->i_count; i_index ++ )
652 intf_thread_t *p_intf = (intf_thread_t *)
653 p_list->p_values[i_index].p_object;
654 if( p_intf->b_interaction )
656 p_interaction->p_intf = p_intf;
660 vlc_list_release ( p_list );
663 /* Add a dialog to the queue and wait for answer */
664 static int intf_WaitAnswer( interaction_t *p_interact,
665 interaction_dialog_t *p_dialog )
668 vlc_bool_t b_found = VLC_FALSE;
669 vlc_mutex_lock( &p_interact->object_lock );
670 for( i = 0 ; i< p_interact->i_dialogs; i++ )
672 if( p_interact->pp_dialogs[i]->i_id == p_dialog->i_id )
679 INSERT_ELEM( p_interact->pp_dialogs,
680 p_interact->i_dialogs,
681 p_interact->i_dialogs,
685 p_dialog->i_status = UPDATED_DIALOG;
686 vlc_mutex_unlock( &p_interact->object_lock );
688 /// \todo Check that the initiating object is not dying
689 while( p_dialog->i_status != ANSWERED_DIALOG &&
690 p_dialog->i_status != HIDING_DIALOG &&
691 p_dialog->i_status != HIDDEN_DIALOG &&
692 !p_dialog->p_parent->b_die )
697 if( p_dialog->p_parent->b_die )
699 p_dialog->i_return = DIALOG_CANCELLED;
700 p_dialog->i_status = ANSWERED_DIALOG;
702 p_dialog->i_flags |= DIALOG_GOT_ANSWER;
703 return p_dialog->i_return;
706 /* Add a dialog to the queue and return */
707 static int intf_Send( interaction_t *p_interact,
708 interaction_dialog_t *p_dialog )
711 vlc_bool_t b_found = VLC_FALSE;
712 if( p_interact == NULL ) return VLC_ENOOBJ;
713 vlc_mutex_lock( &p_interact->object_lock );
715 for( i = 0 ; i< p_interact->i_dialogs; i++ )
717 if( p_interact->pp_dialogs[i]->i_id == p_dialog->i_id )
724 INSERT_ELEM( p_interact->pp_dialogs,
725 p_interact->i_dialogs,
726 p_interact->i_dialogs,
730 p_dialog->i_status = UPDATED_DIALOG;
731 // Pretend we already retrieved the "answer"
732 p_dialog->i_flags |= DIALOG_GOT_ANSWER;
733 vlc_mutex_unlock( &p_interact->object_lock );
737 /* Find an interaction dialog by its id */
738 static interaction_dialog_t *intf_InteractionGetById( vlc_object_t* p_this,
741 interaction_t *p_interaction = intf_InteractionGet( p_this );
744 if( !p_interaction ) return NULL;
746 for( i = 0 ; i< p_interaction->i_dialogs; i++ )
748 if( p_interaction->pp_dialogs[i]->i_id == i_id )
750 return p_interaction->pp_dialogs[i];
756 #define FREE( i ) { if( i ) free( i ); i = NULL; }
758 static void intf_InteractionDialogDestroy( interaction_dialog_t *p_dialog )
760 FREE( p_dialog->psz_title );
761 FREE( p_dialog->psz_description );
762 FREE( p_dialog->psz_defaultButton );
763 FREE( p_dialog->psz_alternateButton );
764 FREE( p_dialog->psz_otherButton );