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;
146 if( p_dialog->i_flags & DIALOG_OK_CANCEL )
147 p_dialog->i_return = DIALOG_CANCELLED;
149 // Pretend we have hidden and destroyed it
150 if( p_dialog->i_status == HIDDEN_DIALOG )
152 p_dialog->i_status = DESTROYED_DIALOG;
156 p_dialog->i_status = HIDING_DIALOG;
162 vlc_object_yield( p_interaction->p_intf );
165 for( i_index = 0 ; i_index < p_interaction->i_dialogs; i_index ++ )
167 interaction_dialog_t *p_dialog = p_interaction->pp_dialogs[i_index];
168 switch( p_dialog->i_status )
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;
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;
186 if( !(p_dialog->i_flags & DIALOG_GOT_ANSWER) ) break;
187 if( !(p_dialog->i_flags & DIALOG_REUSABLE) )
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 );
195 case DESTROYED_DIALOG:
196 // Interface has now destroyed it, remove it
197 REMOVE_ELEM( p_interaction->pp_dialogs, p_interaction->i_dialogs,
200 intf_InteractionDialogDestroy( p_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;
213 if( p_interaction->p_intf )
215 vlc_object_release( p_interaction->p_intf );
218 vlc_mutex_unlock( &p_playlist->p_interaction->object_lock );
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; \
230 new->i_status = NEW_DIALOG;
232 #define INTERACT_FREE( new ) \
233 if( new->psz_title ) free( new->psz_title ); \
234 if( new->psz_description ) free( new->psz_description );
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
242 void __intf_UserFatal( vlc_object_t *p_this,
243 const char *psz_title,
244 const char *psz_format, ... )
247 interaction_dialog_t *p_new = NULL;
248 int i_id = DIALOG_ERRORS;
252 p_new = intf_InteractionGetById( p_this, i_id );
256 INTERACT_INIT( p_new );
257 if( i_id > 0 ) p_new->i_id = i_id ;
261 p_new->i_status = UPDATED_DIALOG;
264 p_new->i_flags |= DIALOG_REUSABLE;
266 p_new->i_type = INTERACT_DIALOG_ONEWAY;
267 p_new->psz_title = strdup( psz_title );
269 va_start( args, psz_format );
270 vasprintf( &p_new->psz_description, psz_format, args );
273 p_new->i_flags |= DIALOG_CLEAR_NOSHOW;
275 intf_Interact( p_this, p_new );
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
284 int __intf_UserYesNo( vlc_object_t *p_this,
285 const char *psz_title,
286 const char *psz_description )
289 interaction_dialog_t *p_new = NULL;
291 INTERACT_INIT( p_new );
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;
298 i_ret = intf_Interact( p_this, p_new );
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
309 int __intf_UserOkayCancel( vlc_object_t *p_this,
310 const char *psz_title,
311 const char *psz_description )
314 interaction_dialog_t *p_new = NULL;
316 INTERACT_INIT( p_new );
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;
323 i_ret = intf_Interact( p_this, p_new );
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
335 int __intf_UserProgress( vlc_object_t *p_this,
336 const char *psz_title,
337 const char *psz_status,
341 interaction_dialog_t *p_new = NULL;
343 INTERACT_INIT( p_new );
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;
350 p_new->i_flags = DIALOG_USER_PROGRESS;
352 i_ret = intf_Interact( p_this, p_new );
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)
364 void __intf_UserProgressUpdate( vlc_object_t *p_this, int i_id,
365 const char *psz_status, float f_pos )
367 interaction_t *p_interaction = intf_InteractionGet( p_this );
368 interaction_dialog_t *p_dialog;
370 if( !p_interaction ) return;
372 vlc_mutex_lock( &p_interaction->object_lock );
373 p_dialog = intf_InteractionGetById( p_this, i_id );
377 vlc_mutex_unlock( &p_interaction->object_lock ) ;
381 if( p_dialog->psz_description )
382 free( p_dialog->psz_description );
383 p_dialog->psz_description = strdup( psz_status );
385 p_dialog->val.f_float = f_pos;
387 p_dialog->i_status = UPDATED_DIALOG;
388 vlc_mutex_unlock( &p_interaction->object_lock) ;
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
399 int __intf_UserLoginPassword( vlc_object_t *p_this,
400 const char *psz_title,
401 const char *psz_description,
403 char **ppsz_password )
407 interaction_dialog_t *p_new = NULL;
409 INTERACT_INIT( p_new );
411 p_new->i_type = INTERACT_DIALOG_TWOWAY;
412 p_new->psz_title = strdup( psz_title );
413 p_new->psz_description = strdup( psz_description );
415 p_new->i_flags = DIALOG_LOGIN_PW_OK_CANCEL;
417 i_ret = intf_Interact( p_this, p_new );
419 if( i_ret != DIALOG_CANCELLED )
421 *ppsz_login = strdup( p_new->psz_returned[0] );
422 *ppsz_password = strdup( p_new->psz_returned[1] );
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
434 int __intf_UserStringInput( vlc_object_t *p_this,
435 const char *psz_title,
436 const char *psz_description,
437 char **ppsz_usersString )
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_PSZ_INPUT_OK_CANCEL;
451 i_ret = intf_Interact( p_this, p_new );
453 if( i_ret != DIALOG_CANCELLED )
455 *ppsz_usersString = strdup( p_new->psz_returned[0] );
460 /** Hide an interaction dialog
461 * \param p_this the parent vlc object
462 * \param i_id the id of the item to hide
465 void __intf_UserHide( vlc_object_t *p_this, int i_id )
467 interaction_t *p_interaction = intf_InteractionGet( p_this );
468 interaction_dialog_t *p_dialog;
470 if( !p_interaction ) return;
472 vlc_mutex_lock( &p_interaction->object_lock );
473 p_dialog = intf_InteractionGetById( p_this, i_id );
477 vlc_mutex_unlock( &p_interaction->object_lock );
481 p_dialog->i_status = ANSWERED_DIALOG;
482 vlc_mutex_unlock( &p_interaction->object_lock );
487 /**********************************************************************
488 * The following functions are local
489 **********************************************************************/
491 /* Get the interaction object. Create it if needed */
492 static interaction_t * intf_InteractionGet( vlc_object_t *p_this )
494 playlist_t *p_playlist;
495 interaction_t *p_interaction;
497 p_playlist = (playlist_t*) vlc_object_find( p_this, VLC_OBJECT_PLAYLIST,
505 if( p_playlist->p_interaction == NULL )
507 intf_InteractionInit( p_playlist );
510 p_interaction = p_playlist->p_interaction;
512 vlc_object_release( p_playlist );
514 return p_interaction;
517 /* Create the interaction object in the given playlist object */
518 static void intf_InteractionInit( playlist_t *p_playlist )
520 interaction_t *p_interaction;
522 p_interaction = vlc_object_create( VLC_OBJECT( p_playlist ),
523 sizeof( interaction_t ) );
526 msg_Err( p_playlist,"out of memory" );
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;
535 vlc_mutex_init( p_interaction , &p_interaction->object_lock );
537 p_playlist->p_interaction = p_interaction;
540 /* Look for an interface suitable for interaction */
541 static void intf_InteractionSearchInterface( interaction_t *p_interaction )
546 p_interaction->p_intf = NULL;
548 p_list = vlc_list_find( p_interaction, VLC_OBJECT_INTF, FIND_ANYWHERE );
551 msg_Err( p_interaction, "unable to create module list" );
555 for( i_index = 0; i_index < p_list->i_count; i_index ++ )
557 intf_thread_t *p_intf = (intf_thread_t *)
558 p_list->p_values[i_index].p_object;
559 if( p_intf->b_interaction )
561 p_interaction->p_intf = p_intf;
565 vlc_list_release ( p_list );
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 )
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++ )
577 if( p_interact->pp_dialogs[i]->i_id == p_dialog->i_id )
584 INSERT_ELEM( p_interact->pp_dialogs,
585 p_interact->i_dialogs,
586 p_interact->i_dialogs,
590 p_dialog->i_status = UPDATED_DIALOG;
591 vlc_mutex_unlock( &p_interact->object_lock );
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 )
602 if( p_dialog->p_parent->b_die )
604 p_dialog->i_return = DIALOG_CANCELLED;
605 p_dialog->i_status = ANSWERED_DIALOG;
607 p_dialog->i_flags |= DIALOG_GOT_ANSWER;
608 return p_dialog->i_return;
611 /* Add a dialog to the queue and return */
612 static int intf_Send( interaction_t *p_interact,
613 interaction_dialog_t *p_dialog )
616 vlc_bool_t b_found = VLC_FALSE;
617 if( p_interact == NULL ) return VLC_ENOOBJ;
618 vlc_mutex_lock( &p_interact->object_lock );
620 for( i = 0 ; i< p_interact->i_dialogs; i++ )
622 if( p_interact->pp_dialogs[i]->i_id == p_dialog->i_id )
629 INSERT_ELEM( p_interact->pp_dialogs,
630 p_interact->i_dialogs,
631 p_interact->i_dialogs,
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 );
642 /* Find an interaction dialog by its id */
643 static interaction_dialog_t *intf_InteractionGetById( vlc_object_t* p_this,
646 interaction_t *p_interaction = intf_InteractionGet( p_this );
649 if( !p_interaction ) return NULL;
651 for( i = 0 ; i< p_interaction->i_dialogs; i++ )
653 if( p_interaction->pp_dialogs[i]->i_id == i_id )
655 return p_interaction->pp_dialogs[i];
661 #define FREE( i ) { if( i ) free( i ); i = NULL; }
663 static void intf_InteractionDialogDestroy( interaction_dialog_t *p_dialog )
665 /* FREE( p_dialog->val.psz_string );
667 FREE( p_dialog->psz_title );
668 FREE( p_dialog->psz_description );
670 FREE( p_dialog->psz_returned[0] );
671 FREE( p_dialog->psz_returned[1] ); */