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;
163 * Update a progress bar in a dialogue
165 * \param p_dialog Dialog
166 * \param psz_status New status
167 * \param f_position New position (0.0->100.0)
168 * \param i_timeToGo Time (in sec) to go until process is finished
171 void intf_ProgressUpdate( interaction_dialog_t *p_dialog,
172 const char *psz_status, float f_pos, int i_time )
174 interaction_t *p_interaction = InteractionGet( p_dialog->p_parent );
175 assert( p_interaction );
177 vlc_mutex_lock( &p_interaction->lock );
178 free( p_dialog->psz_description );
179 p_dialog->psz_description = strdup( psz_status );
181 p_dialog->val.f_float = f_pos;
182 p_dialog->i_timeToGo = i_time;
184 p_dialog->i_status = UPDATED_DIALOG;
186 vlc_cond_signal( &p_interaction->wait );
187 vlc_mutex_unlock( &p_interaction->lock );
188 vlc_object_release( p_interaction );
192 * Helper function to communicate dialogue cancellations between the
193 * interface module and the caller
195 * \param p_dialog Dialog
196 * \return Either true or false
198 bool intf_ProgressIsCancelled( interaction_dialog_t *p_dialog )
200 interaction_t *p_interaction = InteractionGet( p_dialog->p_parent );
203 assert( p_interaction );
204 vlc_mutex_lock( &p_interaction->lock );
205 b_cancel = p_dialog->b_cancelled;
206 vlc_mutex_unlock( &p_interaction->lock );
207 vlc_object_release( p_interaction );
212 * Helper function to make a login/password dialogue
214 * \param p_this Parent vlc_object
215 * \param psz_title Title for the dialog
216 * \param psz_description A description
217 * \param ppsz_login Returned login
218 * \param ppsz_password Returned password
219 * \return Clicked button code
221 int __intf_UserLoginPassword( vlc_object_t *p_this,
222 const char *psz_title,
223 const char *psz_description,
225 char **ppsz_password )
228 DIALOG_INIT( TWOWAY, VLC_EGENERIC );
229 p_new->i_type = INTERACT_DIALOG_TWOWAY;
230 p_new->psz_title = strdup( psz_title );
231 p_new->psz_description = strdup( psz_description );
232 p_new->psz_default_button = strdup( _("OK" ) );
233 p_new->psz_alternate_button = strdup( _("Cancel" ) );
235 p_new->i_flags = DIALOG_LOGIN_PW_OK_CANCEL;
237 i_ret = DialogSend( p_new );
239 if( i_ret == VLC_EGENERIC )
240 DialogDestroy( p_new );
241 else if( i_ret != DIALOG_CANCELLED )
243 *ppsz_login = p_new->psz_returned[0]?
244 strdup( p_new->psz_returned[0] ) : NULL;
245 *ppsz_password = p_new->psz_returned[1]?
246 strdup( p_new->psz_returned[1] ) : NULL;
252 * Helper function to make a dialogue asking the user for !password string
254 * \param p_this Parent vlc_object
255 * \param psz_title Title for the dialog
256 * \param psz_description A description
257 * \param ppsz_usersString Returned login
258 * \return Clicked button code
260 int __intf_UserStringInput( vlc_object_t *p_this,
261 const char *psz_title,
262 const char *psz_description,
263 char **ppsz_usersString )
266 DIALOG_INIT( TWOWAY, VLC_EGENERIC );
267 p_new->i_type = INTERACT_DIALOG_TWOWAY;
268 p_new->psz_title = strdup( psz_title );
269 p_new->psz_description = strdup( psz_description );
271 p_new->i_flags = DIALOG_PSZ_INPUT_OK_CANCEL;
273 i_ret = DialogSend( p_new );
275 if( i_ret == VLC_EGENERIC )
276 DialogDestroy( p_new );
277 else if( i_ret != DIALOG_CANCELLED )
279 *ppsz_usersString = p_new->psz_returned[0]?
280 strdup( p_new->psz_returned[0] ) : NULL;
286 * Hide an interaction dialog
288 * \param p_dialog the dialog to hide
291 void intf_UserHide( interaction_dialog_t *p_dialog )
293 interaction_t *p_interaction = InteractionGet( p_dialog->p_parent );
294 assert( p_interaction );
296 vlc_mutex_lock( &p_interaction->lock );
297 p_dialog->i_status = ANSWERED_DIALOG;
298 vlc_cond_signal( &p_interaction->wait );
299 vlc_mutex_unlock( &p_interaction->lock );
300 vlc_object_release( p_interaction );
304 * Create the initial interaction object
305 * (should only be used in libvlc_InternalInit, LibVLC private)
307 * \return a vlc_object_t that should be freed when done.
309 interaction_t * interaction_Init( libvlc_int_t *p_libvlc )
311 interaction_t *p_interaction;
313 /* Make sure we haven't yet created an interaction object */
314 assert( libvlc_priv(p_libvlc)->p_interaction == NULL );
316 p_interaction = vlc_custom_create( VLC_OBJECT(p_libvlc),
317 sizeof( *p_interaction ),
318 VLC_OBJECT_GENERIC, "interaction" );
322 vlc_object_attach( p_interaction, p_libvlc );
323 p_interaction->i_dialogs = 0;
324 p_interaction->pp_dialogs = NULL;
325 p_interaction->p_intf = NULL;
327 vlc_mutex_init( &p_interaction->lock );
328 vlc_cond_init( &p_interaction->wait );
330 if( vlc_clone( &p_interaction->thread, InteractionLoop, p_interaction,
331 VLC_THREAD_PRIORITY_LOW ) )
333 msg_Err( p_interaction, "Interaction control thread creation failed, "
334 "interaction will not be displayed" );
335 vlc_object_detach( p_interaction );
336 vlc_object_release( p_interaction );
340 return p_interaction;
343 void interaction_Destroy( interaction_t *p_interaction )
348 vlc_cancel( p_interaction->thread );
349 vlc_join( p_interaction->thread, NULL );
350 vlc_cond_destroy( &p_interaction->wait );
351 vlc_mutex_destroy( &p_interaction->lock );
353 /* Remove all dialogs - Interfaces must be able to clean up their data */
354 for( int i = p_interaction->i_dialogs -1 ; i >= 0; i-- )
356 interaction_dialog_t * p_dialog = p_interaction->pp_dialogs[i];
357 DialogDestroy( p_dialog );
358 REMOVE_ELEM( p_interaction->pp_dialogs, p_interaction->i_dialogs, i );
360 vlc_object_release( p_interaction );
363 static vlc_mutex_t intf_lock = VLC_STATIC_MUTEX;
365 int interaction_Register( intf_thread_t *intf )
367 libvlc_priv_t *priv = libvlc_priv( intf->p_libvlc );
368 int ret = VLC_EGENERIC;
370 vlc_mutex_lock( &intf_lock );
371 if( priv->p_interaction_intf == NULL )
372 { /* Since the interface is responsible for unregistering itself before
373 * it terminates, an object reference is not needed. */
374 priv->p_interaction_intf = intf;
377 vlc_mutex_unlock( &intf_lock );
381 int interaction_Unregister( intf_thread_t *intf )
383 libvlc_priv_t *priv = libvlc_priv( intf->p_libvlc );
384 int ret = VLC_EGENERIC;
386 vlc_mutex_lock( &intf_lock );
387 if( priv->p_interaction_intf == intf )
389 priv->p_interaction_intf = NULL;
392 vlc_mutex_unlock( &intf_lock );
396 /**********************************************************************
397 * The following functions are local
398 **********************************************************************/
400 /* Get the interaction object */
401 static interaction_t * InteractionGet( vlc_object_t *p_this )
403 interaction_t *obj = libvlc_priv(p_this->p_libvlc)->p_interaction;
405 vlc_object_hold( obj );
410 /* Look for an interface suitable for interaction, and hold it. */
411 static intf_thread_t *SearchInterface( interaction_t *p_interaction )
413 libvlc_priv_t *priv = libvlc_priv( p_interaction->p_libvlc );
416 vlc_mutex_lock( &intf_lock );
417 intf = priv->p_interaction_intf;
419 vlc_object_hold( intf );
420 vlc_mutex_unlock( &intf_lock );
425 /* Destroy a dialog */
426 static void DialogDestroy( interaction_dialog_t *p_dialog )
428 free( p_dialog->psz_title );
429 free( p_dialog->psz_description );
430 free( p_dialog->psz_default_button );
431 free( p_dialog->psz_alternate_button );
432 free( p_dialog->psz_other_button );
433 vlc_object_release( p_dialog->p_parent );
437 /* Ask for the dialog to be sent to the user. Wait for answer
439 static int DialogSend( interaction_dialog_t *p_dialog )
441 interaction_t *p_interaction;
442 intf_thread_t *p_intf;
444 if( ( p_dialog->p_parent->i_flags & OBJECT_FLAGS_NOINTERACT )
445 || !config_GetInt( p_interaction, "interact" ) )
448 p_interaction = InteractionGet( p_dialog->p_parent );
452 p_dialog->p_lock = &p_interaction->lock;
454 p_intf = SearchInterface( p_interaction );
457 p_dialog->i_return = DIALOG_DEFAULT; /* Give default answer */
459 /* Pretend we have hidden and destroyed it */
460 p_dialog->i_status = HIDING_DIALOG;
461 vlc_object_release( p_interaction );
464 p_dialog->p_interface = p_intf;
466 p_dialog->i_action = INTERACT_NEW;
467 var_SetAddress( p_dialog->p_interface, "interaction", p_dialog );
469 /* Check if we have already added this dialog */
470 vlc_mutex_lock( &p_interaction->lock );
471 /* Add it to the queue, the main loop will send the orders to the
473 INSERT_ELEM( p_interaction->pp_dialogs, p_interaction->i_dialogs,
474 p_interaction->i_dialogs, p_dialog );
476 if( p_dialog->i_type == INTERACT_DIALOG_TWOWAY ) /* Wait for answer */
478 vlc_cond_signal( &p_interaction->wait );
479 while( p_dialog->i_status != ANSWERED_DIALOG &&
480 p_dialog->i_status != HIDING_DIALOG &&
481 p_dialog->i_status != HIDDEN_DIALOG &&
482 !p_dialog->p_parent->b_die )
484 vlc_mutex_unlock( &p_interaction->lock );
486 vlc_mutex_lock( &p_interaction->lock );
488 if( p_dialog->p_parent->b_die )
490 p_dialog->i_return = DIALOG_CANCELLED;
491 p_dialog->i_status = ANSWERED_DIALOG;
494 p_dialog->i_flags |= DIALOG_GOT_ANSWER;
495 vlc_cond_signal( &p_interaction->wait );
496 vlc_mutex_unlock( &p_interaction->lock );
497 vlc_object_release( p_interaction );
498 if( p_dialog->i_type == INTERACT_DIALOG_TWOWAY )
499 return p_dialog->i_return;
504 static void* InteractionLoop( void *p_this )
506 interaction_t *p_interaction = p_this;
508 vlc_mutex_lock( &p_interaction->lock );
509 mutex_cleanup_push( &p_interaction->lock );
512 int canc = vlc_savecancel();
513 InteractionManage( p_interaction );
514 vlc_restorecancel( canc );
516 vlc_cond_wait( &p_interaction->wait, &p_interaction->lock );
523 * The main interaction processing loop
525 * \param p_interaction the interaction object
529 static void InteractionManage( interaction_t *p_interaction )
534 for( i_index = 0 ; i_index < p_interaction->i_dialogs; i_index ++ )
536 interaction_dialog_t *p_dialog = p_interaction->pp_dialogs[i_index];
537 switch( p_dialog->i_status )
539 case ANSWERED_DIALOG:
540 /* Ask interface to hide it */
541 p_dialog->i_action = INTERACT_HIDE;
542 val.p_address = p_dialog;
543 var_Set( p_dialog->p_interface, "interaction", val );
544 p_dialog->i_status = HIDING_DIALOG;
547 p_dialog->i_action = INTERACT_UPDATE;
548 val.p_address = p_dialog;
549 var_Set( p_dialog->p_interface, "interaction", val );
550 p_dialog->i_status = SENT_DIALOG;
553 if( !(p_dialog->i_flags & DIALOG_GOT_ANSWER) ) break;
554 p_dialog->i_action = INTERACT_DESTROY;
555 val.p_address = p_dialog;
556 var_Set( p_dialog->p_interface, "interaction", val );
558 case DESTROYED_DIALOG:
559 /* Interface has now destroyed it, remove it */
560 REMOVE_ELEM( p_interaction->pp_dialogs, p_interaction->i_dialogs,
563 DialogDestroy( p_dialog );