1 /*****************************************************************************
2 * interaction.c: User interaction functions
3 *****************************************************************************
4 * Copyright © 2005-2008 the VideoLAN team
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 *****************************************************************************/
38 #include <vlc_common.h>
40 #include <vlc_interface.h>
41 #include "interface.h"
46 /*****************************************************************************
48 *****************************************************************************/
51 * This structure contains the active interaction dialogs, and is
61 int i_dialogs; ///< Number of dialogs
62 interaction_dialog_t **pp_dialogs; ///< Dialogs
63 intf_thread_t *p_intf; ///< Interface to use
66 static interaction_t * InteractionGet( vlc_object_t * );
67 static intf_thread_t * SearchInterface( interaction_t * );
68 static void* InteractionLoop( void * );
69 static void InteractionManage( interaction_t * );
71 static void DialogDestroy( interaction_dialog_t * );
72 static int DialogSend( interaction_dialog_t * );
74 #define DIALOG_INIT( type, err ) \
75 interaction_dialog_t* p_new = calloc( 1, sizeof( interaction_dialog_t ) ); \
76 if( !p_new ) return err; \
77 p_new->p_parent = vlc_object_hold( p_this ); \
78 p_new->b_cancelled = false; \
79 p_new->i_status = SENT_DIALOG; \
81 p_new->i_type = INTERACT_DIALOG_##type; \
82 p_new->psz_returned[0] = NULL; \
83 p_new->psz_returned[1] = NULL
86 va_start( args, psz_format ); \
87 if( vasprintf( &p_new->psz_description, psz_format, args ) == -1 ) \
88 return VLC_EGENERIC; \
92 * Send an error message, both in a blocking and non-blocking way
94 * \param p_this Parent vlc_object
95 * \param b_blocking Is this dialog blocking or not?
96 * \param psz_title Title for the dialog
97 * \param psz_format The message to display
98 * \return VLC_SUCCESS or VLC_EGENERIC
100 int __intf_UserFatal( vlc_object_t *p_this, bool b_blocking,
101 const char *psz_title,
102 const char *psz_format, ... )
105 DIALOG_INIT( ONEWAY, VLC_EGENERIC );
107 p_new->psz_title = strdup( psz_title );
111 p_new->i_flags = DIALOG_BLOCKING_ERROR;
113 p_new->i_flags = DIALOG_NONBLOCKING_ERROR;
115 return DialogSend( p_new );
119 * Helper function to send a warning, which is always shown non-blocking
121 * \param p_this Parent vlc_object
122 * \param psz_title Title for the dialog
123 * \param psz_format The message to display
124 * \return VLC_SUCCESS or VLC_EGENERIC
126 int __intf_UserWarn( vlc_object_t *p_this,
127 const char *psz_title,
128 const char *psz_format, ... )
131 DIALOG_INIT( ONEWAY, VLC_EGENERIC );
133 p_new->psz_title = strdup( psz_title );
136 p_new->i_flags = DIALOG_WARNING;
138 return DialogSend( p_new );
142 * Helper function to ask a yes-no-cancel question
144 * \param p_this Parent vlc_object
145 * \param psz_title Title for the dialog
146 * \param psz_description A description
147 * \param psz_default caption for the default button
148 * \param psz_alternate caption for the alternate button
149 * \param psz_other caption for the optional 3rd button (== cancel)
150 * \return Clicked button code
152 int __intf_UserYesNo( vlc_object_t *p_this,
153 const char *psz_title,
154 const char *psz_description,
155 const char *psz_default,
156 const char *psz_alternate,
157 const char *psz_other )
159 DIALOG_INIT( TWOWAY, VLC_EGENERIC );
161 p_new->psz_title = strdup( psz_title );
162 p_new->psz_description = strdup( psz_description );
163 p_new->i_flags = DIALOG_YES_NO_CANCEL;
164 p_new->psz_default_button = strdup( psz_default );
165 p_new->psz_alternate_button = strdup( psz_alternate );
166 p_new->psz_other_button = psz_other ? strdup( psz_other ) : NULL;
168 return DialogSend( p_new );
172 * Helper function to create a dialogue showing a progress-bar with some info
174 * \param p_this Parent vlc_object
175 * \param psz_title Title for the dialog (NULL implies main intf )
176 * \param psz_status Current status
177 * \param f_position Current position (0.0->100.0)
178 * \param i_timeToGo Time (in sec) to go until process is finished
179 * \return Dialog, for use with UserProgressUpdate
181 interaction_dialog_t *
182 __intf_Progress( vlc_object_t *p_this, const char *psz_title,
183 const char *psz_status, float f_pos, int i_time )
185 DIALOG_INIT( ONEWAY, NULL );
186 p_new->psz_description = strdup( psz_status );
187 p_new->val.f_float = f_pos;
188 p_new->i_timeToGo = i_time;
189 p_new->psz_alternate_button = strdup( _( "Cancel" ) );
193 p_new->psz_title = strdup( psz_title );
194 p_new->i_flags = DIALOG_USER_PROGRESS;
197 p_new->i_flags = DIALOG_INTF_PROGRESS;
204 * Update a progress bar in a dialogue
206 * \param p_dialog Dialog
207 * \param psz_status New status
208 * \param f_position New position (0.0->100.0)
209 * \param i_timeToGo Time (in sec) to go until process is finished
212 void intf_ProgressUpdate( interaction_dialog_t *p_dialog,
213 const char *psz_status, float f_pos, int i_time )
215 interaction_t *p_interaction = InteractionGet( p_dialog->p_parent );
216 assert( p_interaction );
218 vlc_object_lock( p_interaction );
219 free( p_dialog->psz_description );
220 p_dialog->psz_description = strdup( psz_status );
222 p_dialog->val.f_float = f_pos;
223 p_dialog->i_timeToGo = i_time;
225 p_dialog->i_status = UPDATED_DIALOG;
227 vlc_cond_signal( &p_interaction->wait );
228 vlc_object_unlock( p_interaction );
229 vlc_object_release( p_interaction );
233 * Helper function to communicate dialogue cancellations between the
234 * interface module and the caller
236 * \param p_dialog Dialog
237 * \return Either true or false
239 bool intf_ProgressIsCancelled( interaction_dialog_t *p_dialog )
241 interaction_t *p_interaction = InteractionGet( p_dialog->p_parent );
244 assert( p_interaction );
245 vlc_object_lock( p_interaction );
246 b_cancel = p_dialog->b_cancelled;
247 vlc_object_unlock( p_interaction );
248 vlc_object_release( p_interaction );
253 * Helper function to make a login/password dialogue
255 * \param p_this Parent vlc_object
256 * \param psz_title Title for the dialog
257 * \param psz_description A description
258 * \param ppsz_login Returned login
259 * \param ppsz_password Returned password
260 * \return Clicked button code
262 int __intf_UserLoginPassword( vlc_object_t *p_this,
263 const char *psz_title,
264 const char *psz_description,
266 char **ppsz_password )
269 DIALOG_INIT( TWOWAY, VLC_EGENERIC );
270 p_new->i_type = INTERACT_DIALOG_TWOWAY;
271 p_new->psz_title = strdup( psz_title );
272 p_new->psz_description = strdup( psz_description );
273 p_new->psz_default_button = strdup( _("OK" ) );
274 p_new->psz_alternate_button = strdup( _("Cancel" ) );
276 p_new->i_flags = DIALOG_LOGIN_PW_OK_CANCEL;
278 i_ret = DialogSend( p_new );
280 if( i_ret != DIALOG_CANCELLED && i_ret != VLC_EGENERIC )
282 *ppsz_login = p_new->psz_returned[0]?
283 strdup( p_new->psz_returned[0] ) : NULL;
284 *ppsz_password = p_new->psz_returned[1]?
285 strdup( p_new->psz_returned[1] ) : NULL;
291 * Helper function to make a dialogue asking the user for !password string
293 * \param p_this Parent vlc_object
294 * \param psz_title Title for the dialog
295 * \param psz_description A description
296 * \param ppsz_usersString Returned login
297 * \return Clicked button code
299 int __intf_UserStringInput( vlc_object_t *p_this,
300 const char *psz_title,
301 const char *psz_description,
302 char **ppsz_usersString )
305 DIALOG_INIT( TWOWAY, VLC_EGENERIC );
306 p_new->i_type = INTERACT_DIALOG_TWOWAY;
307 p_new->psz_title = strdup( psz_title );
308 p_new->psz_description = strdup( psz_description );
310 p_new->i_flags = DIALOG_PSZ_INPUT_OK_CANCEL;
312 i_ret = DialogSend( p_new );
314 if( i_ret != DIALOG_CANCELLED )
316 *ppsz_usersString = p_new->psz_returned[0]?
317 strdup( p_new->psz_returned[0] ) : NULL;
323 * Hide an interaction dialog
325 * \param p_dialog the dialog to hide
328 void intf_UserHide( interaction_dialog_t *p_dialog )
330 interaction_t *p_interaction = InteractionGet( p_dialog->p_parent );
331 assert( p_interaction );
333 vlc_object_lock( p_interaction );
334 p_dialog->i_status = ANSWERED_DIALOG;
335 vlc_cond_signal( &p_interaction->wait );
336 vlc_object_unlock( p_interaction );
337 vlc_object_release( p_interaction );
341 * Create the initial interaction object
342 * (should only be used in libvlc_InternalInit, LibVLC private)
344 * \return a vlc_object_t that should be freed when done.
346 interaction_t * interaction_Init( libvlc_int_t *p_libvlc )
348 interaction_t *p_interaction;
350 /* Make sure we haven't yet created an interaction object */
351 assert( libvlc_priv(p_libvlc)->p_interaction == NULL );
353 p_interaction = vlc_custom_create( VLC_OBJECT(p_libvlc),
354 sizeof( *p_interaction ),
355 VLC_OBJECT_GENERIC, "interaction" );
359 vlc_object_attach( p_interaction, p_libvlc );
360 p_interaction->i_dialogs = 0;
361 p_interaction->pp_dialogs = NULL;
362 p_interaction->p_intf = NULL;
364 vlc_cond_init( &p_interaction->wait );
366 if( vlc_clone( &p_interaction->thread, InteractionLoop, p_interaction,
367 VLC_THREAD_PRIORITY_LOW ) )
369 msg_Err( p_interaction, "Interaction control thread creation failed, "
370 "interaction will not be displayed" );
371 vlc_object_detach( p_interaction );
372 vlc_object_release( p_interaction );
376 return p_interaction;
379 void interaction_Destroy( interaction_t *p_interaction )
384 vlc_cancel( p_interaction->thread );
385 vlc_join( p_interaction->thread, NULL );
386 vlc_cond_destroy( &p_interaction->wait );
388 /* Remove all dialogs - Interfaces must be able to clean up their data */
389 for( int i = p_interaction->i_dialogs -1 ; i >= 0; i-- )
391 interaction_dialog_t * p_dialog = p_interaction->pp_dialogs[i];
392 DialogDestroy( p_dialog );
393 REMOVE_ELEM( p_interaction->pp_dialogs, p_interaction->i_dialogs, i );
395 vlc_object_release( p_interaction );
398 static vlc_mutex_t intf_lock = VLC_STATIC_MUTEX;
400 int interaction_Register( intf_thread_t *intf )
402 libvlc_priv_t *priv = libvlc_priv( intf->p_libvlc );
403 int ret = VLC_EGENERIC;
405 vlc_mutex_lock( &intf_lock );
406 if( priv->p_interaction_intf == NULL )
407 { /* Since the interface is responsible for unregistering itself before
408 * it terminates, an object reference is not needed. */
409 priv->p_interaction_intf = intf;
412 vlc_mutex_unlock( &intf_lock );
416 int interaction_Unregister( intf_thread_t *intf )
418 libvlc_priv_t *priv = libvlc_priv( intf->p_libvlc );
419 int ret = VLC_EGENERIC;
421 vlc_mutex_lock( &intf_lock );
422 if( priv->p_interaction_intf == intf )
424 priv->p_interaction_intf = NULL;
427 vlc_mutex_unlock( &intf_lock );
431 /**********************************************************************
432 * The following functions are local
433 **********************************************************************/
435 /* Get the interaction object */
436 static interaction_t * InteractionGet( vlc_object_t *p_this )
438 interaction_t *obj = libvlc_priv(p_this->p_libvlc)->p_interaction;
440 vlc_object_hold( obj );
445 /* Look for an interface suitable for interaction, and hold it. */
446 static intf_thread_t *SearchInterface( interaction_t *p_interaction )
448 libvlc_priv_t *priv = libvlc_priv( p_interaction->p_libvlc );
451 vlc_mutex_lock( &intf_lock );
452 intf = priv->p_interaction_intf;
454 vlc_object_hold( intf );
455 vlc_mutex_unlock( &intf_lock );
460 /* Destroy a dialog */
461 static void DialogDestroy( interaction_dialog_t *p_dialog )
463 free( p_dialog->psz_title );
464 free( p_dialog->psz_description );
465 free( p_dialog->psz_default_button );
466 free( p_dialog->psz_alternate_button );
467 free( p_dialog->psz_other_button );
468 vlc_object_release( p_dialog->p_parent );
472 /* Ask for the dialog to be sent to the user. Wait for answer
474 static int DialogSend( interaction_dialog_t *p_dialog )
476 interaction_t *p_interaction;
477 intf_thread_t *p_intf;
479 if( p_dialog->p_parent->i_flags & OBJECT_FLAGS_NOINTERACT )
482 p_interaction = InteractionGet( p_dialog->p_parent );
486 p_intf = SearchInterface( p_interaction );
489 p_dialog->i_return = DIALOG_DEFAULT; /* Give default answer */
491 /* Pretend we have hidden and destroyed it */
492 p_dialog->i_status = HIDING_DIALOG;
493 vlc_object_release( p_interaction );
496 p_dialog->p_interface = p_intf;
498 if( config_GetInt( p_interaction, "interact" ) ||
499 p_dialog->i_flags & DIALOG_BLOCKING_ERROR ||
500 p_dialog->i_flags & DIALOG_NONBLOCKING_ERROR )
504 p_dialog->p_interaction = p_interaction;
505 p_dialog->i_action = INTERACT_NEW;
506 val.p_address = p_dialog;
507 var_Set( p_dialog->p_interface, "interaction", val );
509 /* Check if we have already added this dialog */
510 vlc_object_lock( p_interaction );
511 /* Add it to the queue, the main loop will send the orders to the
513 INSERT_ELEM( p_interaction->pp_dialogs, p_interaction->i_dialogs,
514 p_interaction->i_dialogs, p_dialog );
516 if( p_dialog->i_type == INTERACT_DIALOG_TWOWAY ) /* Wait for answer */
518 vlc_cond_signal( &p_interaction->wait );
519 while( p_dialog->i_status != ANSWERED_DIALOG &&
520 p_dialog->i_status != HIDING_DIALOG &&
521 p_dialog->i_status != HIDDEN_DIALOG &&
522 !p_dialog->p_parent->b_die )
524 vlc_object_unlock( p_interaction );
526 vlc_object_lock( p_interaction );
528 if( p_dialog->p_parent->b_die )
530 p_dialog->i_return = DIALOG_CANCELLED;
531 p_dialog->i_status = ANSWERED_DIALOG;
533 p_dialog->i_flags |= DIALOG_GOT_ANSWER;
534 vlc_cond_signal( &p_interaction->wait );
535 vlc_object_unlock( p_interaction );
536 vlc_object_release( p_interaction );
537 return p_dialog->i_return;
541 /* Pretend we already retrieved the "answer" */
542 p_dialog->i_flags |= DIALOG_GOT_ANSWER;
543 vlc_cond_signal( &p_interaction->wait );
544 vlc_object_unlock( p_interaction );
545 vlc_object_release( p_interaction );
551 vlc_object_release( p_interaction );
556 static void* InteractionLoop( void *p_this )
558 interaction_t *p_interaction = p_this;
560 vlc_object_lock( p_interaction );
561 mutex_cleanup_push( &(vlc_internals(p_interaction)->lock) );
564 int canc = vlc_savecancel();
565 InteractionManage( p_interaction );
566 vlc_restorecancel( canc );
568 vlc_cond_wait( &p_interaction->wait, &(vlc_internals(p_interaction)->lock) );
575 * The main interaction processing loop
577 * \param p_interaction the interaction object
581 static void InteractionManage( interaction_t *p_interaction )
586 for( i_index = 0 ; i_index < p_interaction->i_dialogs; i_index ++ )
588 interaction_dialog_t *p_dialog = p_interaction->pp_dialogs[i_index];
589 switch( p_dialog->i_status )
591 case ANSWERED_DIALOG:
592 /* Ask interface to hide it */
593 p_dialog->i_action = INTERACT_HIDE;
594 val.p_address = p_dialog;
595 var_Set( p_dialog->p_interface, "interaction", val );
596 p_dialog->i_status = HIDING_DIALOG;
599 p_dialog->i_action = INTERACT_UPDATE;
600 val.p_address = p_dialog;
601 var_Set( p_dialog->p_interface, "interaction", val );
602 p_dialog->i_status = SENT_DIALOG;
605 if( !(p_dialog->i_flags & DIALOG_GOT_ANSWER) ) break;
606 p_dialog->i_action = INTERACT_DESTROY;
607 val.p_address = p_dialog;
608 var_Set( p_dialog->p_interface, "interaction", val );
610 case DESTROYED_DIALOG:
611 /* Interface has now destroyed it, remove it */
612 REMOVE_ELEM( p_interaction->pp_dialogs, p_interaction->i_dialogs,
615 DialogDestroy( p_dialog );