1 /*****************************************************************************
2 * interaction.c: User interaction functions
3 *****************************************************************************
4 * Copyright (C) 2005-2006 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 *****************************************************************************/
36 #include <stdlib.h> /* free(), strtol() */
37 #include <stdio.h> /* FILE */
38 #include <string.h> /* strerror() */
40 #include <vlc/input.h>
43 #include "vlc_interaction.h"
44 #include "vlc_interface.h"
45 #include "vlc_playlist.h"
47 /*****************************************************************************
49 *****************************************************************************/
50 static void InteractionInit( playlist_t *p_playlist );
51 static interaction_t * InteractionGet( vlc_object_t *p_this );
52 static void InteractionSearchInterface( interaction_t *
54 static interaction_dialog_t *DialogGetById( interaction_t* , int );
55 static void DialogDestroy( interaction_dialog_t *p_dialog );
56 static int DialogSend( vlc_object_t *p_this, interaction_dialog_t *p_dialog );
59 * Destroy the interaction system
60 * \param The interaction object to destroy
63 void intf_InteractionDestroy( interaction_t *p_interaction )
66 // Remove all dialogs - Interfaces must be able to clean up their data
67 for( i = p_interaction->i_dialogs -1 ; i >= 0; i-- )
69 interaction_dialog_t * p_dialog = p_interaction->pp_dialogs[i];
70 DialogDestroy( p_dialog );
71 REMOVE_ELEM( p_interaction->pp_dialogs, p_interaction->i_dialogs, i );
73 vlc_object_destroy( p_interaction );
77 * The main interaction processing loop
78 * This function is called from the playlist loop
80 * \param p_playlist the parent playlist
83 void intf_InteractionManage( playlist_t *p_playlist )
87 interaction_t *p_interaction = p_playlist->p_interaction;
90 if( p_interaction->i_dialogs == 0 ) return;
92 vlc_mutex_lock( &p_interaction->object_lock );
94 InteractionSearchInterface( p_interaction );
95 if( !p_interaction->p_intf )
97 // We mark all dialogs as answered with their "default" answer
98 for( i_index = 0 ; i_index < p_interaction->i_dialogs; i_index ++ )
100 interaction_dialog_t *p_dialog = p_interaction->pp_dialogs[i_index];
101 p_dialog->i_return = DIALOG_DEFAULT; // Give default answer
103 // Pretend we have hidden and destroyed it
104 if( p_dialog->i_status == HIDDEN_DIALOG )
105 p_dialog->i_status = DESTROYED_DIALOG;
107 p_dialog->i_status = HIDING_DIALOG;
111 vlc_object_yield( p_interaction->p_intf );
113 for( i_index = 0 ; i_index < p_interaction->i_dialogs; i_index ++ )
115 interaction_dialog_t *p_dialog = p_interaction->pp_dialogs[i_index];
116 switch( p_dialog->i_status )
118 case ANSWERED_DIALOG:
119 // Ask interface to hide it
120 p_dialog->i_action = INTERACT_HIDE;
121 val.p_address = p_dialog;
122 if( p_interaction->p_intf )
123 var_Set( p_interaction->p_intf, "interaction", val );
124 p_dialog->i_status = HIDING_DIALOG;
127 p_dialog->i_action = INTERACT_UPDATE;
128 val.p_address = p_dialog;
129 if( p_interaction->p_intf )
130 var_Set( p_interaction->p_intf, "interaction", val );
131 p_dialog->i_status = SENT_DIALOG;
134 if( !(p_dialog->i_flags & DIALOG_GOT_ANSWER) ) break;
135 p_dialog->i_action = INTERACT_DESTROY;
136 val.p_address = p_dialog;
137 if( p_interaction->p_intf )
138 var_Set( p_interaction->p_intf, "interaction", val );
140 case DESTROYED_DIALOG:
141 // Interface has now destroyed it, remove it
142 REMOVE_ELEM( p_interaction->pp_dialogs, p_interaction->i_dialogs,
145 DialogDestroy( p_dialog );
148 // This is truly a new dialog, send it.
149 p_dialog->i_action = INTERACT_NEW;
150 val.p_address = p_dialog;
151 if( p_interaction->p_intf )
152 var_Set( p_interaction->p_intf, "interaction", val );
153 p_dialog->i_status = SENT_DIALOG;
158 if( p_interaction->p_intf )
160 vlc_object_release( p_interaction->p_intf );
163 vlc_mutex_unlock( &p_playlist->p_interaction->object_lock );
166 #define DIALOG_INIT( type ) \
167 DECMALLOC_ERR( p_new, interaction_dialog_t ); \
168 memset( p_new, 0, sizeof( interaction_dialog_t ) ); \
169 p_new->b_cancelled = VLC_FALSE; \
170 p_new->i_status = NEW_DIALOG; \
171 p_new->i_flags = 0; \
172 p_new->i_type = INTERACT_DIALOG_##type; \
173 p_new->psz_returned[0] = NULL; \
174 p_new->psz_returned[1] = NULL;
176 #define FORMAT_DESC \
177 va_start( args, psz_format ); \
178 vasprintf( &p_new->psz_description, psz_format, args ); \
181 /** Send an error message, both in a blocking and non-blocking way
182 * \param p_this Parent vlc_object
183 * \param b_blocking Is this dialog blocking or not?
184 * \param psz_title Title for the dialog
185 * \param psz_format The message to display
187 int __intf_UserFatal( vlc_object_t *p_this, vlc_bool_t b_blocking,
188 const char *psz_title,
189 const char *psz_format, ... )
192 DIALOG_INIT( ONEWAY );
194 p_new->psz_title = strdup( psz_title );
198 p_new->i_flags = DIALOG_BLOCKING_ERROR;
200 p_new->i_flags = DIALOG_NONBLOCKING_ERROR;
202 return DialogSend( p_this, p_new );
205 /** Helper function to send an warning, which is always shown non-blocking
206 * \param p_this Parent vlc_object
207 * \param psz_title Title for the dialog
208 * \param psz_format The message to display
210 int __intf_UserWarn( vlc_object_t *p_this,
211 const char *psz_title,
212 const char *psz_format, ... )
215 DIALOG_INIT( ONEWAY );
217 p_new->psz_title = strdup( psz_title );
220 p_new->i_flags = DIALOG_WARNING;
222 return DialogSend( p_this, p_new );
225 /** Helper function to ask a yes-no-cancel question
226 * \param p_this Parent vlc_object
227 * \param psz_title Title for the dialog
228 * \param psz_description A description
229 * \param psz_default caption for the default button
230 * \param psz_alternate caption for the alternate button
231 * \param psz_other caption for the optional 3rd button (== cancel)
232 * \return Clicked button code
234 int __intf_UserYesNo( vlc_object_t *p_this,
235 const char *psz_title,
236 const char *psz_description,
237 const char *psz_default,
238 const char *psz_alternate,
239 const char *psz_other )
241 DIALOG_INIT( TWOWAY );
243 p_new->psz_title = strdup( psz_title );
244 p_new->psz_description = strdup( psz_description );
245 p_new->i_flags = DIALOG_YES_NO_CANCEL;
246 p_new->psz_default_button = strdup( psz_default );
247 p_new->psz_alternate_button = strdup( psz_alternate );
249 p_new->psz_other_button = strdup( psz_other );
251 return DialogSend( p_this, p_new );
254 /** Helper function to create a dialogue showing a progress-bar with some info
255 * \param p_this Parent vlc_object
256 * \param psz_title Title for the dialog (NULL implies main intf )
257 * \param psz_status Current status
258 * \param f_position Current position (0.0->100.0)
259 * \param i_timeToGo Time (in sec) to go until process is finished
260 * \return Dialog id, to give to UserProgressUpdate
262 int __intf_Progress( vlc_object_t *p_this, const char *psz_title,
263 const char *psz_status, float f_pos, int i_time )
265 DIALOG_INIT( ONEWAY );
266 p_new->psz_description = strdup( psz_status );
267 p_new->val.f_float = f_pos;
268 p_new->i_timeToGo = i_time;
269 p_new->psz_alternate_button = strdup( _("Cancel" ) );
273 p_new->psz_title = strdup( psz_title );
274 p_new->i_flags = DIALOG_USER_PROGRESS;
277 p_new->i_flags = DIALOG_INTF_PROGRESS;
279 DialogSend( p_this, p_new );
283 /** Update a progress bar in a dialogue
284 * \param p_this Parent vlc_object
285 * \param i_id Identifier of the dialog
286 * \param psz_status New status
287 * \param f_position New position (0.0->100.0)
288 * \param i_timeToGo Time (in sec) to go until process is finished
291 void __intf_ProgressUpdate( vlc_object_t *p_this, int i_id,
292 const char *psz_status, float f_pos, int i_time )
294 interaction_t *p_interaction = InteractionGet( p_this );
295 interaction_dialog_t *p_dialog;
297 if( !p_interaction ) return;
299 vlc_mutex_lock( &p_interaction->object_lock );
300 p_dialog = DialogGetById( p_interaction, i_id );
304 vlc_mutex_unlock( &p_interaction->object_lock ) ;
308 FREE( p_dialog->psz_description );
309 p_dialog->psz_description = strdup( psz_status );
311 p_dialog->val.f_float = f_pos;
312 p_dialog->i_timeToGo = i_time;
314 p_dialog->i_status = UPDATED_DIALOG;
315 vlc_mutex_unlock( &p_interaction->object_lock) ;
318 /** Helper function to communicate dialogue cancellations between the
319 * interface module and the caller
320 * \param p_this Parent vlc_object
321 * \param i_id Identifier of the dialogue
322 * \return Either true or false
324 vlc_bool_t __intf_UserProgressIsCancelled( vlc_object_t *p_this, int i_id )
326 interaction_t *p_interaction = InteractionGet( p_this );
327 interaction_dialog_t *p_dialog;
330 if( !p_interaction ) return VLC_TRUE;
332 vlc_mutex_lock( &p_interaction->object_lock );
333 p_dialog = DialogGetById( p_interaction, i_id );
336 vlc_mutex_unlock( &p_interaction->object_lock ) ;
340 b_cancel = p_dialog->b_cancelled;
341 vlc_mutex_unlock( &p_interaction->object_lock );
345 /** Helper function to make a login/password dialogue
346 * \param p_this Parent vlc_object
347 * \param psz_title Title for the dialog
348 * \param psz_description A description
349 * \param ppsz_login Returned login
350 * \param ppsz_password Returned password
351 * \return Clicked button code
353 int __intf_UserLoginPassword( vlc_object_t *p_this,
354 const char *psz_title,
355 const char *psz_description,
357 char **ppsz_password )
360 DIALOG_INIT( TWOWAY );
361 p_new->i_type = INTERACT_DIALOG_TWOWAY;
362 p_new->psz_title = strdup( psz_title );
363 p_new->psz_description = strdup( psz_description );
364 p_new->psz_default_button = strdup( _("Ok" ) );
365 p_new->psz_alternate_button = strdup( _("Cancel" ) );
367 p_new->i_flags = DIALOG_LOGIN_PW_OK_CANCEL;
369 i_ret = DialogSend( p_this, p_new );
371 if( i_ret != DIALOG_CANCELLED && i_ret != VLC_EGENERIC )
373 *ppsz_login = p_new->psz_returned[0]?
374 strdup( p_new->psz_returned[0] ) : NULL;
375 *ppsz_password = p_new->psz_returned[1]?
376 strdup( p_new->psz_returned[1] ) : NULL;
381 /** Helper function to make a dialogue asking the user for !password string
382 * \param p_this Parent vlc_object
383 * \param psz_title Title for the dialog
384 * \param psz_description A description
385 * \param ppsz_usersString Returned login
386 * \return Clicked button code
388 int __intf_UserStringInput( vlc_object_t *p_this,
389 const char *psz_title,
390 const char *psz_description,
391 char **ppsz_usersString )
394 DIALOG_INIT( TWOWAY );
395 p_new->i_type = INTERACT_DIALOG_TWOWAY;
396 p_new->psz_title = strdup( psz_title );
397 p_new->psz_description = strdup( psz_description );
399 p_new->i_flags = DIALOG_PSZ_INPUT_OK_CANCEL;
401 i_ret = DialogSend( p_this, p_new );
403 if( i_ret != DIALOG_CANCELLED )
405 *ppsz_usersString = p_new->psz_returned[0]?
406 strdup( p_new->psz_returned[0] ) : NULL;
411 /** Hide an interaction dialog
412 * \param p_this the parent vlc object
413 * \param i_id the id of the item to hide
416 void __intf_UserHide( vlc_object_t *p_this, int i_id )
418 interaction_t *p_interaction = InteractionGet( p_this );
419 interaction_dialog_t *p_dialog;
421 if( !p_interaction ) return;
423 vlc_mutex_lock( &p_interaction->object_lock );
424 p_dialog = DialogGetById( p_interaction, i_id );
428 vlc_mutex_unlock( &p_interaction->object_lock );
432 p_dialog->i_status = ANSWERED_DIALOG;
433 vlc_mutex_unlock( &p_interaction->object_lock );
436 /**********************************************************************
437 * The following functions are local
438 **********************************************************************/
440 /* Get the interaction object. Create it if needed */
441 static interaction_t * InteractionGet( vlc_object_t *p_this )
443 interaction_t *p_interaction;
444 playlist_t *p_playlist = pl_Yield( p_this );
447 if( p_playlist->p_interaction == NULL )
448 InteractionInit( p_playlist );
450 p_interaction = p_playlist->p_interaction;
453 pl_Release( p_this );
454 return p_interaction;
457 /* Create the interaction object in the given playlist object */
458 static void InteractionInit( playlist_t *p_playlist )
460 interaction_t *p_interaction = vlc_object_create( VLC_OBJECT( p_playlist ),
461 sizeof( interaction_t ) );
464 msg_Err( p_playlist,"out of memory" );
468 p_interaction->i_dialogs = 0;
469 p_interaction->pp_dialogs = NULL;
470 p_interaction->p_intf = NULL;
471 p_interaction->i_last_id = DIALOG_LAST_PREDEFINED + 1;
473 vlc_mutex_init( p_interaction , &p_interaction->object_lock );
475 p_playlist->p_interaction = p_interaction;
478 /* Look for an interface suitable for interaction */
479 static void InteractionSearchInterface( interaction_t *p_interaction )
484 p_interaction->p_intf = NULL;
486 p_list = vlc_list_find( p_interaction, VLC_OBJECT_INTF, FIND_ANYWHERE );
489 msg_Err( p_interaction, "unable to create module list" );
493 for( i_index = 0; i_index < p_list->i_count; i_index ++ )
495 intf_thread_t *p_intf = (intf_thread_t *)
496 p_list->p_values[i_index].p_object;
497 if( p_intf->b_interaction )
499 p_interaction->p_intf = p_intf;
503 vlc_list_release ( p_list );
506 /* Find an interaction dialog by its id */
507 static interaction_dialog_t *DialogGetById( interaction_t *p_interaction,
511 for( i = 0 ; i< p_interaction->i_dialogs; i++ )
513 if( p_interaction->pp_dialogs[i]->i_id == i_id )
514 return p_interaction->pp_dialogs[i];
519 /* Destroy a dialog */
520 static void DialogDestroy( interaction_dialog_t *p_dialog )
522 free( p_dialog->psz_title );
523 free( p_dialog->psz_description );
524 free( p_dialog->psz_default_button );
525 free( p_dialog->psz_alternate_button );
526 free( p_dialog->psz_other_button );
530 /* Ask for the dialog to be sent to the user. Wait for answer
532 static int DialogSend( vlc_object_t *p_this, interaction_dialog_t *p_dialog )
534 interaction_t *p_interaction = InteractionGet( p_this );
536 /* Get an id, if we don't already have one */
537 if( p_dialog->i_id == 0 )
538 p_dialog->i_id = ++p_interaction->i_last_id;
540 if( p_this->i_flags & OBJECT_FLAGS_NOINTERACT ) return VLC_EGENERIC;
542 if( config_GetInt(p_this, "interact") ||
543 p_dialog->i_flags & DIALOG_BLOCKING_ERROR ||
544 p_dialog->i_flags & DIALOG_NONBLOCKING_ERROR )
546 vlc_bool_t b_found = VLC_FALSE;
548 p_dialog->p_interaction = p_interaction;
549 p_dialog->p_parent = p_this;
551 /* Check if we have already added this dialog */
552 vlc_mutex_lock( &p_interaction->object_lock );
553 for( i = 0 ; i< p_interaction->i_dialogs; i++ )
555 if( p_interaction->pp_dialogs[i]->i_id == p_dialog->i_id )
558 /* Add it to the queue, the main loop will send the orders to the
562 INSERT_ELEM( p_interaction->pp_dialogs,
563 p_interaction->i_dialogs,
564 p_interaction->i_dialogs,
568 p_dialog->i_status = UPDATED_DIALOG;
570 if( p_dialog->i_type == INTERACT_DIALOG_TWOWAY ) // Wait for answer
572 playlist_Signal( pl_Get(p_this) );
573 while( p_dialog->i_status != ANSWERED_DIALOG &&
574 p_dialog->i_status != HIDING_DIALOG &&
575 p_dialog->i_status != HIDDEN_DIALOG &&
576 !p_dialog->p_parent->b_die )
578 vlc_mutex_unlock( &p_interaction->object_lock );
580 vlc_mutex_lock( &p_interaction->object_lock );
582 if( p_dialog->p_parent->b_die )
584 p_dialog->i_return = DIALOG_CANCELLED;
585 p_dialog->i_status = ANSWERED_DIALOG;
587 p_dialog->i_flags |= DIALOG_GOT_ANSWER;
588 vlc_mutex_unlock( &p_interaction->object_lock );
589 playlist_Signal( pl_Get(p_this) );
590 return p_dialog->i_return;
594 // Pretend we already retrieved the "answer"
595 p_dialog->i_flags |= DIALOG_GOT_ANSWER;
596 vlc_mutex_unlock( &p_interaction->object_lock );
597 playlist_Signal( pl_Get(p_this) );