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
62 int i_dialogs; ///< Number of dialogs
63 interaction_dialog_t **pp_dialogs; ///< Dialogs
64 intf_thread_t *p_intf; ///< Interface to use
67 static interaction_t * InteractionGet( vlc_object_t * );
68 static intf_thread_t * SearchInterface( interaction_t * );
69 static void* InteractionLoop( void * );
70 static void InteractionManage( interaction_t * );
72 static void DialogDestroy( interaction_dialog_t * );
73 static int DialogSend( interaction_dialog_t * );
75 #define DIALOG_INIT( type, err ) \
76 interaction_dialog_t* p_new = calloc( 1, sizeof( interaction_dialog_t ) ); \
77 if( !p_new ) return err; \
78 p_new->p_parent = vlc_object_hold( p_this ); \
79 p_new->b_cancelled = false; \
80 p_new->i_status = SENT_DIALOG; \
82 p_new->i_type = INTERACT_DIALOG_##type; \
83 p_new->psz_returned[0] = NULL; \
84 p_new->psz_returned[1] = NULL
87 va_start( args, psz_format ); \
88 if( vasprintf( &p_new->psz_description, psz_format, args ) == -1 ) \
89 return VLC_EGENERIC; \
92 static inline int DialogFireForget( interaction_dialog_t *d )
94 int ret = DialogSend( d );
95 if( ret == VLC_EGENERIC )
101 * Helper function to ask a yes-no-cancel question
103 * \param p_this Parent vlc_object
104 * \param psz_title Title for the dialog
105 * \param psz_description A description
106 * \param psz_default caption for the default button
107 * \param psz_alternate caption for the alternate button
108 * \param psz_other caption for the optional 3rd button (== cancel)
109 * \return Clicked button code
111 int __intf_UserYesNo( vlc_object_t *p_this,
112 const char *psz_title,
113 const char *psz_description,
114 const char *psz_default,
115 const char *psz_alternate,
116 const char *psz_other )
118 DIALOG_INIT( TWOWAY, VLC_EGENERIC );
120 p_new->psz_title = strdup( psz_title );
121 p_new->psz_description = strdup( psz_description );
122 p_new->i_flags = DIALOG_YES_NO_CANCEL;
123 p_new->psz_default_button = strdup( psz_default );
124 p_new->psz_alternate_button = strdup( psz_alternate );
125 p_new->psz_other_button = psz_other ? strdup( psz_other ) : NULL;
127 return DialogFireForget( p_new );
131 * Helper function to create a dialogue showing a progress-bar with some info
133 * \param p_this Parent vlc_object
134 * \param psz_title Title for the dialog (NULL implies main intf )
135 * \param psz_status Current status
136 * \param f_position Current position (0.0->100.0)
137 * \param i_timeToGo Time (in sec) to go until process is finished
138 * \return Dialog, for use with UserProgressUpdate
140 interaction_dialog_t *
141 __intf_Progress( vlc_object_t *p_this, const char *psz_title,
142 const char *psz_status, float f_pos, int i_time )
144 DIALOG_INIT( ONEWAY, NULL );
145 p_new->psz_description = strdup( psz_status );
146 p_new->val.f_float = f_pos;
147 p_new->i_timeToGo = i_time;
148 p_new->psz_alternate_button = strdup( _( "Cancel" ) );
152 p_new->psz_title = strdup( psz_title );
153 p_new->i_flags = DIALOG_USER_PROGRESS;
156 p_new->i_flags = DIALOG_INTF_PROGRESS;
158 if( DialogSend( p_new ) == VLC_EGENERIC )
160 DialogDestroy( p_new );
167 * Update a progress bar in a dialogue
169 * \param p_dialog Dialog
170 * \param psz_status New status
171 * \param f_position New position (0.0->100.0)
172 * \param i_timeToGo Time (in sec) to go until process is finished
175 void intf_ProgressUpdate( interaction_dialog_t *p_dialog,
176 const char *psz_status, float f_pos, int i_time )
178 interaction_t *p_interaction = InteractionGet( p_dialog->p_parent );
179 assert( p_interaction );
181 vlc_mutex_lock( &p_interaction->lock );
182 free( p_dialog->psz_description );
183 p_dialog->psz_description = strdup( psz_status );
185 p_dialog->val.f_float = f_pos;
186 p_dialog->i_timeToGo = i_time;
188 p_dialog->i_status = UPDATED_DIALOG;
190 vlc_cond_signal( &p_interaction->wait );
191 vlc_mutex_unlock( &p_interaction->lock );
192 vlc_object_release( p_interaction );
196 * Helper function to communicate dialogue cancellations between the
197 * interface module and the caller
199 * \param p_dialog Dialog
200 * \return Either true or false
202 bool intf_ProgressIsCancelled( interaction_dialog_t *p_dialog )
204 interaction_t *p_interaction = InteractionGet( p_dialog->p_parent );
207 assert( p_interaction );
208 vlc_mutex_lock( &p_interaction->lock );
209 b_cancel = p_dialog->b_cancelled;
210 vlc_mutex_unlock( &p_interaction->lock );
211 vlc_object_release( p_interaction );
216 * Helper function to make a login/password dialogue
218 * \param p_this Parent vlc_object
219 * \param psz_title Title for the dialog
220 * \param psz_description A description
221 * \param ppsz_login Returned login
222 * \param ppsz_password Returned password
223 * \return Clicked button code
225 int __intf_UserLoginPassword( vlc_object_t *p_this,
226 const char *psz_title,
227 const char *psz_description,
229 char **ppsz_password )
232 DIALOG_INIT( TWOWAY, VLC_EGENERIC );
233 p_new->i_type = INTERACT_DIALOG_TWOWAY;
234 p_new->psz_title = strdup( psz_title );
235 p_new->psz_description = strdup( psz_description );
236 p_new->psz_default_button = strdup( _("OK" ) );
237 p_new->psz_alternate_button = strdup( _("Cancel" ) );
239 p_new->i_flags = DIALOG_LOGIN_PW_OK_CANCEL;
241 i_ret = DialogSend( p_new );
243 if( i_ret == VLC_EGENERIC )
244 DialogDestroy( p_new );
245 else if( i_ret != DIALOG_CANCELLED )
247 *ppsz_login = p_new->psz_returned[0]?
248 strdup( p_new->psz_returned[0] ) : NULL;
249 *ppsz_password = p_new->psz_returned[1]?
250 strdup( p_new->psz_returned[1] ) : NULL;
256 * Helper function to make a dialogue asking the user for !password string
258 * \param p_this Parent vlc_object
259 * \param psz_title Title for the dialog
260 * \param psz_description A description
261 * \param ppsz_usersString Returned login
262 * \return Clicked button code
264 int __intf_UserStringInput( vlc_object_t *p_this,
265 const char *psz_title,
266 const char *psz_description,
267 char **ppsz_usersString )
270 DIALOG_INIT( TWOWAY, VLC_EGENERIC );
271 p_new->i_type = INTERACT_DIALOG_TWOWAY;
272 p_new->psz_title = strdup( psz_title );
273 p_new->psz_description = strdup( psz_description );
275 p_new->i_flags = DIALOG_PSZ_INPUT_OK_CANCEL;
277 i_ret = DialogSend( p_new );
279 if( i_ret == VLC_EGENERIC )
280 DialogDestroy( p_new );
281 else if( i_ret != DIALOG_CANCELLED )
283 *ppsz_usersString = p_new->psz_returned[0]?
284 strdup( p_new->psz_returned[0] ) : NULL;
290 * Hide an interaction dialog
292 * \param p_dialog the dialog to hide
295 void intf_UserHide( interaction_dialog_t *p_dialog )
297 interaction_t *p_interaction = InteractionGet( p_dialog->p_parent );
298 assert( p_interaction );
300 vlc_mutex_lock( &p_interaction->lock );
301 p_dialog->i_status = ANSWERED_DIALOG;
302 vlc_cond_signal( &p_interaction->wait );
303 vlc_mutex_unlock( &p_interaction->lock );
304 vlc_object_release( p_interaction );
308 * Create the initial interaction object
309 * (should only be used in libvlc_InternalInit, LibVLC private)
311 * \return a vlc_object_t that should be freed when done.
313 interaction_t * interaction_Init( libvlc_int_t *p_libvlc )
315 interaction_t *p_interaction;
317 /* Make sure we haven't yet created an interaction object */
318 assert( libvlc_priv(p_libvlc)->p_interaction == NULL );
320 p_interaction = vlc_custom_create( VLC_OBJECT(p_libvlc),
321 sizeof( *p_interaction ),
322 VLC_OBJECT_GENERIC, "interaction" );
326 vlc_object_attach( p_interaction, p_libvlc );
327 p_interaction->i_dialogs = 0;
328 p_interaction->pp_dialogs = NULL;
329 p_interaction->p_intf = NULL;
331 vlc_mutex_init( &p_interaction->lock );
332 vlc_cond_init( &p_interaction->wait );
334 if( vlc_clone( &p_interaction->thread, InteractionLoop, p_interaction,
335 VLC_THREAD_PRIORITY_LOW ) )
337 msg_Err( p_interaction, "Interaction control thread creation failed, "
338 "interaction will not be displayed" );
339 vlc_object_detach( p_interaction );
340 vlc_object_release( p_interaction );
344 return p_interaction;
347 void interaction_Destroy( interaction_t *p_interaction )
352 vlc_cancel( p_interaction->thread );
353 vlc_join( p_interaction->thread, NULL );
354 vlc_cond_destroy( &p_interaction->wait );
355 vlc_mutex_destroy( &p_interaction->lock );
357 /* Remove all dialogs - Interfaces must be able to clean up their data */
358 for( int i = p_interaction->i_dialogs -1 ; i >= 0; i-- )
360 interaction_dialog_t * p_dialog = p_interaction->pp_dialogs[i];
361 DialogDestroy( p_dialog );
362 REMOVE_ELEM( p_interaction->pp_dialogs, p_interaction->i_dialogs, i );
364 vlc_object_release( p_interaction );
367 static vlc_mutex_t intf_lock = VLC_STATIC_MUTEX;
369 int interaction_Register( intf_thread_t *intf )
371 libvlc_priv_t *priv = libvlc_priv( intf->p_libvlc );
372 int ret = VLC_EGENERIC;
374 vlc_mutex_lock( &intf_lock );
375 if( priv->p_interaction_intf == NULL )
376 { /* Since the interface is responsible for unregistering itself before
377 * it terminates, an object reference is not needed. */
378 priv->p_interaction_intf = intf;
381 vlc_mutex_unlock( &intf_lock );
385 int interaction_Unregister( intf_thread_t *intf )
387 libvlc_priv_t *priv = libvlc_priv( intf->p_libvlc );
388 int ret = VLC_EGENERIC;
390 vlc_mutex_lock( &intf_lock );
391 if( priv->p_interaction_intf == intf )
393 priv->p_interaction_intf = NULL;
396 vlc_mutex_unlock( &intf_lock );
400 /**********************************************************************
401 * The following functions are local
402 **********************************************************************/
404 /* Get the interaction object */
405 static interaction_t * InteractionGet( vlc_object_t *p_this )
407 interaction_t *obj = libvlc_priv(p_this->p_libvlc)->p_interaction;
409 vlc_object_hold( obj );
414 /* Look for an interface suitable for interaction, and hold it. */
415 static intf_thread_t *SearchInterface( interaction_t *p_interaction )
417 libvlc_priv_t *priv = libvlc_priv( p_interaction->p_libvlc );
420 vlc_mutex_lock( &intf_lock );
421 intf = priv->p_interaction_intf;
423 vlc_object_hold( intf );
424 vlc_mutex_unlock( &intf_lock );
429 /* Destroy a dialog */
430 static void DialogDestroy( interaction_dialog_t *p_dialog )
432 free( p_dialog->psz_title );
433 free( p_dialog->psz_description );
434 free( p_dialog->psz_default_button );
435 free( p_dialog->psz_alternate_button );
436 free( p_dialog->psz_other_button );
437 vlc_object_release( p_dialog->p_parent );
441 /* Ask for the dialog to be sent to the user. Wait for answer
443 static int DialogSend( interaction_dialog_t *p_dialog )
445 interaction_t *p_interaction;
446 intf_thread_t *p_intf;
448 if( ( p_dialog->p_parent->i_flags & OBJECT_FLAGS_NOINTERACT )
449 || !config_GetInt( p_interaction, "interact" ) )
452 p_interaction = InteractionGet( p_dialog->p_parent );
456 p_dialog->p_lock = &p_interaction->lock;
458 p_intf = SearchInterface( p_interaction );
461 p_dialog->i_return = DIALOG_DEFAULT; /* Give default answer */
463 /* Pretend we have hidden and destroyed it */
464 p_dialog->i_status = HIDING_DIALOG;
465 vlc_object_release( p_interaction );
468 p_dialog->p_interface = p_intf;
470 p_dialog->i_action = INTERACT_NEW;
471 var_SetAddress( p_dialog->p_interface, "interaction", p_dialog );
473 /* Check if we have already added this dialog */
474 vlc_mutex_lock( &p_interaction->lock );
475 /* Add it to the queue, the main loop will send the orders to the
477 INSERT_ELEM( p_interaction->pp_dialogs, p_interaction->i_dialogs,
478 p_interaction->i_dialogs, p_dialog );
480 if( p_dialog->i_type == INTERACT_DIALOG_TWOWAY ) /* Wait for answer */
482 vlc_cond_signal( &p_interaction->wait );
483 while( p_dialog->i_status != ANSWERED_DIALOG &&
484 p_dialog->i_status != HIDING_DIALOG &&
485 p_dialog->i_status != HIDDEN_DIALOG &&
486 !p_dialog->p_parent->b_die )
488 vlc_mutex_unlock( &p_interaction->lock );
490 vlc_mutex_lock( &p_interaction->lock );
492 if( p_dialog->p_parent->b_die )
494 p_dialog->i_return = DIALOG_CANCELLED;
495 p_dialog->i_status = ANSWERED_DIALOG;
498 p_dialog->i_flags |= DIALOG_GOT_ANSWER;
499 vlc_cond_signal( &p_interaction->wait );
500 vlc_mutex_unlock( &p_interaction->lock );
501 vlc_object_release( p_interaction );
502 if( p_dialog->i_type == INTERACT_DIALOG_TWOWAY )
503 return p_dialog->i_return;
508 static void* InteractionLoop( void *p_this )
510 interaction_t *p_interaction = p_this;
512 vlc_mutex_lock( &p_interaction->lock );
513 mutex_cleanup_push( &p_interaction->lock );
516 int canc = vlc_savecancel();
517 InteractionManage( p_interaction );
518 vlc_restorecancel( canc );
520 vlc_cond_wait( &p_interaction->wait, &p_interaction->lock );
527 * The main interaction processing loop
529 * \param p_interaction the interaction object
533 static void InteractionManage( interaction_t *p_interaction )
538 for( i_index = 0 ; i_index < p_interaction->i_dialogs; i_index ++ )
540 interaction_dialog_t *p_dialog = p_interaction->pp_dialogs[i_index];
541 switch( p_dialog->i_status )
543 case ANSWERED_DIALOG:
544 /* Ask interface to hide it */
545 p_dialog->i_action = INTERACT_HIDE;
546 val.p_address = p_dialog;
547 var_Set( p_dialog->p_interface, "interaction", val );
548 p_dialog->i_status = HIDING_DIALOG;
551 p_dialog->i_action = INTERACT_UPDATE;
552 val.p_address = p_dialog;
553 var_Set( p_dialog->p_interface, "interaction", val );
554 p_dialog->i_status = SENT_DIALOG;
557 if( !(p_dialog->i_flags & DIALOG_GOT_ANSWER) ) break;
558 p_dialog->i_action = INTERACT_DESTROY;
559 val.p_address = p_dialog;
560 var_Set( p_dialog->p_interface, "interaction", val );
562 case DESTROYED_DIALOG:
563 /* Interface has now destroyed it, remove it */
564 REMOVE_ELEM( p_interaction->pp_dialogs, p_interaction->i_dialogs,
567 DialogDestroy( p_dialog );