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
240 * \param p_this Parent vlc_object
241 * \param i_id A predefined ID, 0 if not applicable
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 const char *psz_title,
247 const char *psz_format, ... )
250 interaction_dialog_t *p_new = NULL;
251 int i_id = DIALOG_ERRORS;
255 p_new = intf_InteractionGetById( p_this, i_id );
259 INTERACT_INIT( p_new );
260 if( i_id > 0 ) p_new->i_id = i_id ;
264 p_new->i_status = UPDATED_DIALOG;
267 p_new->i_flags |= DIALOG_REUSABLE;
269 p_new->i_type = INTERACT_DIALOG_ONEWAY;
270 p_new->psz_title = strdup( psz_title );
272 va_start( args, psz_format );
273 vasprintf( &p_new->psz_description, psz_format, args );
276 p_new->i_flags |= DIALOG_CLEAR_NOSHOW;
278 intf_Interact( p_this, p_new );
281 /** Helper function to ask a yes-no-cancel question
282 * \param p_this Parent vlc_object
283 * \param psz_title Title for the dialog
284 * \param psz_description A description
285 * \param psz_default caption for the default button
286 * \param psz_alternate caption for the alternate button
287 * \param psz_other caption for the optional 3rd button (== cancel)
288 * \return Clicked button code
290 int __intf_UserYesNo( vlc_object_t *p_this,
291 const char *psz_title,
292 const char *psz_description,
293 const char *psz_default,
294 const char *psz_alternate,
295 const char *psz_other )
298 interaction_dialog_t *p_new = NULL;
300 INTERACT_INIT( p_new );
302 p_new->i_type = INTERACT_DIALOG_TWOWAY;
303 p_new->psz_title = strdup( psz_title );
304 p_new->psz_description = strdup( psz_description );
305 p_new->i_flags = DIALOG_YES_NO_CANCEL;
306 p_new->psz_defaultButton = strdup( psz_default );
307 p_new->psz_alternateButton = strdup( psz_alternate );
309 p_new->psz_otherButton = strdup( psz_other );
311 p_new->psz_otherButton = NULL;
313 i_ret = intf_Interact( p_this, p_new );
318 /** Helper function to create a dialogue showing a progress-bar with some info
319 * \param p_this Parent vlc_object
320 * \param psz_title Title for the dialog
321 * \param psz_status Current status
322 * \param f_position Current position (0.0->100.0)
323 * \param i_timeToGo Time (in sec) to go until process is finished
324 * \return Dialog id, to give to UserProgressUpdate
326 int __intf_UserProgress( vlc_object_t *p_this,
327 const char *psz_title,
328 const char *psz_status,
333 interaction_dialog_t *p_new = NULL;
335 INTERACT_INIT( p_new );
337 p_new->i_type = INTERACT_DIALOG_ONEWAY;
338 p_new->psz_title = strdup( psz_title );
339 p_new->psz_description = strdup( psz_status );
340 p_new->val.f_float = f_pos;
341 p_new->i_timeToGo = i_time;
343 p_new->i_flags = DIALOG_USER_PROGRESS;
345 i_ret = intf_Interact( p_this, p_new );
350 /** Update a progress bar in a dialogue
351 * \param p_this Parent vlc_object
352 * \param i_id Identifier of the dialog
353 * \param psz_status New status
354 * \param f_position New position (0.0->100.0)
355 * \param i_timeToGo Time (in sec) to go until process is finished
358 void __intf_UserProgressUpdate( vlc_object_t *p_this, int i_id,
359 const char *psz_status, float f_pos,
362 interaction_t *p_interaction = intf_InteractionGet( p_this );
363 interaction_dialog_t *p_dialog;
365 if( !p_interaction ) return;
367 vlc_mutex_lock( &p_interaction->object_lock );
368 p_dialog = intf_InteractionGetById( p_this, i_id );
372 vlc_mutex_unlock( &p_interaction->object_lock ) ;
376 if( p_dialog->psz_description )
377 free( p_dialog->psz_description );
378 p_dialog->psz_description = strdup( psz_status );
380 p_dialog->val.f_float = f_pos;
381 p_dialog->i_timeToGo = i_time;
383 p_dialog->i_status = UPDATED_DIALOG;
384 vlc_mutex_unlock( &p_interaction->object_lock) ;
387 /** Helper function to communicate dialogue cancellations between the intf-module and caller
388 * \param p_this Parent vlc_object
389 * \param i_id Identifier of the dialogue
390 * \return Either true or false
392 vlc_bool_t __intf_UserProgressIsCancelled( vlc_object_t *p_this, int i_id )
394 interaction_t *p_interaction = intf_InteractionGet( p_this );
395 interaction_dialog_t *p_dialog;
397 if( !p_interaction ) return VLC_TRUE;
399 vlc_mutex_lock( &p_interaction->object_lock );
400 p_dialog = intf_InteractionGetById( p_this, i_id );
404 vlc_mutex_unlock( &p_interaction->object_lock ) ;
408 vlc_mutex_unlock( &p_interaction->object_lock) ;
409 return p_dialog->b_cancelled;
412 /** Helper function to make a login/password dialogue
413 * \param p_this Parent vlc_object
414 * \param psz_title Title for the dialog
415 * \param psz_description A description
416 * \param ppsz_login Returned login
417 * \param ppsz_password Returned password
418 * \return Clicked button code
420 int __intf_UserLoginPassword( vlc_object_t *p_this,
421 const char *psz_title,
422 const char *psz_description,
424 char **ppsz_password )
428 interaction_dialog_t *p_new = NULL;
430 INTERACT_INIT( p_new );
432 p_new->i_type = INTERACT_DIALOG_TWOWAY;
433 p_new->psz_title = strdup( psz_title );
434 p_new->psz_description = strdup( psz_description );
436 p_new->i_flags = DIALOG_LOGIN_PW_OK_CANCEL;
438 i_ret = intf_Interact( p_this, p_new );
440 if( i_ret != DIALOG_CANCELLED )
442 *ppsz_login = strdup( p_new->psz_returned[0] );
443 *ppsz_password = strdup( p_new->psz_returned[1] );
448 /** Helper function to make a dialogue asking the user for !password string
449 * \param p_this Parent vlc_object
450 * \param psz_title Title for the dialog
451 * \param psz_description A description
452 * \param ppsz_usersString Returned login
453 * \return Clicked button code
455 int __intf_UserStringInput( vlc_object_t *p_this,
456 const char *psz_title,
457 const char *psz_description,
458 char **ppsz_usersString )
462 interaction_dialog_t *p_new = NULL;
464 INTERACT_INIT( p_new );
466 p_new->i_type = INTERACT_DIALOG_TWOWAY;
467 p_new->psz_title = strdup( psz_title );
468 p_new->psz_description = strdup( psz_description );
470 p_new->i_flags = DIALOG_PSZ_INPUT_OK_CANCEL;
472 i_ret = intf_Interact( p_this, p_new );
474 if( i_ret != DIALOG_CANCELLED )
476 *ppsz_usersString = strdup( p_new->psz_returned[0] );
481 /** Helper function to create a progress-bar in the main interface with a
482 * single-line description
483 * \param p_this Parent vlc_object
484 * \param psz_status Current status
485 * \param f_position Current position (0.0->100.0)
486 * \return Dialog id, to give to IntfProgressUpdate
488 int __intf_IntfProgress( vlc_object_t *p_this,
489 const char *psz_status,
493 interaction_dialog_t *p_new = NULL;
495 INTERACT_INIT( p_new );
497 p_new->i_type = INTERACT_DIALOG_ONEWAY;
498 p_new->psz_description = strdup( psz_status );
499 p_new->val.f_float = f_pos;
501 p_new->i_flags = DIALOG_INTF_PROGRESS;
503 i_ret = intf_Interact( p_this, p_new );
508 /** Update the progress bar in the main interface
509 * \param p_this Parent vlc_object
510 * \param i_id Identifier of the dialog
511 * \param psz_status New status
512 * \param f_position New position (0.0->100.0)
515 void __intf_IntfProgressUpdate( vlc_object_t *p_this, int i_id,
516 const char *psz_status, float f_pos )
518 interaction_t *p_interaction = intf_InteractionGet( p_this );
519 interaction_dialog_t *p_dialog;
521 if( !p_interaction ) return;
523 vlc_mutex_lock( &p_interaction->object_lock );
524 p_dialog = intf_InteractionGetById( p_this, i_id );
528 vlc_mutex_unlock( &p_interaction->object_lock ) ;
532 if( p_dialog->psz_description )
533 free( p_dialog->psz_description );
534 p_dialog->psz_description = strdup( psz_status );
536 p_dialog->val.f_float = f_pos;
538 p_dialog->i_status = UPDATED_DIALOG;
539 vlc_mutex_unlock( &p_interaction->object_lock) ;
542 /** Hide an interaction dialog
543 * \param p_this the parent vlc object
544 * \param i_id the id of the item to hide
547 void __intf_UserHide( vlc_object_t *p_this, int i_id )
549 interaction_t *p_interaction = intf_InteractionGet( p_this );
550 interaction_dialog_t *p_dialog;
552 if( !p_interaction ) return;
554 vlc_mutex_lock( &p_interaction->object_lock );
555 p_dialog = intf_InteractionGetById( p_this, i_id );
559 vlc_mutex_unlock( &p_interaction->object_lock );
563 p_dialog->i_status = ANSWERED_DIALOG;
564 vlc_mutex_unlock( &p_interaction->object_lock );
569 /**********************************************************************
570 * The following functions are local
571 **********************************************************************/
573 /* Get the interaction object. Create it if needed */
574 static interaction_t * intf_InteractionGet( vlc_object_t *p_this )
576 playlist_t *p_playlist;
577 interaction_t *p_interaction;
579 p_playlist = (playlist_t*) vlc_object_find( p_this, VLC_OBJECT_PLAYLIST,
587 if( p_playlist->p_interaction == NULL )
589 intf_InteractionInit( p_playlist );
592 p_interaction = p_playlist->p_interaction;
594 vlc_object_release( p_playlist );
596 return p_interaction;
599 /* Create the interaction object in the given playlist object */
600 static void intf_InteractionInit( playlist_t *p_playlist )
602 interaction_t *p_interaction;
604 p_interaction = vlc_object_create( VLC_OBJECT( p_playlist ),
605 sizeof( interaction_t ) );
608 msg_Err( p_playlist,"out of memory" );
612 p_interaction->i_dialogs = 0;
613 p_interaction->pp_dialogs = NULL;
614 p_interaction->p_intf = NULL;
615 p_interaction->i_last_id = DIALOG_LAST_PREDEFINED + 1;
617 vlc_mutex_init( p_interaction , &p_interaction->object_lock );
619 p_playlist->p_interaction = p_interaction;
622 /* Look for an interface suitable for interaction */
623 static void intf_InteractionSearchInterface( interaction_t *p_interaction )
628 p_interaction->p_intf = NULL;
630 p_list = vlc_list_find( p_interaction, VLC_OBJECT_INTF, FIND_ANYWHERE );
633 msg_Err( p_interaction, "unable to create module list" );
637 for( i_index = 0; i_index < p_list->i_count; i_index ++ )
639 intf_thread_t *p_intf = (intf_thread_t *)
640 p_list->p_values[i_index].p_object;
641 if( p_intf->b_interaction )
643 p_interaction->p_intf = p_intf;
647 vlc_list_release ( p_list );
650 /* Add a dialog to the queue and wait for answer */
651 static int intf_WaitAnswer( interaction_t *p_interact,
652 interaction_dialog_t *p_dialog )
655 vlc_bool_t b_found = VLC_FALSE;
656 vlc_mutex_lock( &p_interact->object_lock );
657 for( i = 0 ; i< p_interact->i_dialogs; i++ )
659 if( p_interact->pp_dialogs[i]->i_id == p_dialog->i_id )
666 INSERT_ELEM( p_interact->pp_dialogs,
667 p_interact->i_dialogs,
668 p_interact->i_dialogs,
672 p_dialog->i_status = UPDATED_DIALOG;
673 vlc_mutex_unlock( &p_interact->object_lock );
675 /// \todo Check that the initiating object is not dying
676 while( p_dialog->i_status != ANSWERED_DIALOG &&
677 p_dialog->i_status != HIDING_DIALOG &&
678 p_dialog->i_status != HIDDEN_DIALOG &&
679 !p_dialog->p_parent->b_die )
684 if( p_dialog->p_parent->b_die )
686 p_dialog->i_return = DIALOG_CANCELLED;
687 p_dialog->i_status = ANSWERED_DIALOG;
689 p_dialog->i_flags |= DIALOG_GOT_ANSWER;
690 return p_dialog->i_return;
693 /* Add a dialog to the queue and return */
694 static int intf_Send( interaction_t *p_interact,
695 interaction_dialog_t *p_dialog )
698 vlc_bool_t b_found = VLC_FALSE;
699 if( p_interact == NULL ) return VLC_ENOOBJ;
700 vlc_mutex_lock( &p_interact->object_lock );
702 for( i = 0 ; i< p_interact->i_dialogs; i++ )
704 if( p_interact->pp_dialogs[i]->i_id == p_dialog->i_id )
711 INSERT_ELEM( p_interact->pp_dialogs,
712 p_interact->i_dialogs,
713 p_interact->i_dialogs,
717 p_dialog->i_status = UPDATED_DIALOG;
718 // Pretend we already retrieved the "answer"
719 p_dialog->i_flags |= DIALOG_GOT_ANSWER;
720 vlc_mutex_unlock( &p_interact->object_lock );
724 /* Find an interaction dialog by its id */
725 static interaction_dialog_t *intf_InteractionGetById( vlc_object_t* p_this,
728 interaction_t *p_interaction = intf_InteractionGet( p_this );
731 if( !p_interaction ) return NULL;
733 for( i = 0 ; i< p_interaction->i_dialogs; i++ )
735 if( p_interaction->pp_dialogs[i]->i_id == i_id )
737 return p_interaction->pp_dialogs[i];
743 #define FREE( i ) { if( i ) free( i ); i = NULL; }
745 static void intf_InteractionDialogDestroy( interaction_dialog_t *p_dialog )
747 FREE( p_dialog->psz_title );
748 FREE( p_dialog->psz_description );
749 FREE( p_dialog->psz_defaultButton );
750 FREE( p_dialog->psz_alternateButton );
751 FREE( p_dialog->psz_otherButton );