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 */
40 #include <vlc_interface.h>
41 #include <vlc_playlist.h>
42 #include "interface.h"
44 /*****************************************************************************
46 *****************************************************************************/
47 static void InteractionInit( playlist_t *p_playlist );
48 static interaction_t * InteractionGet( vlc_object_t *p_this );
49 static void InteractionSearchInterface( interaction_t *
51 static interaction_dialog_t *DialogGetById( interaction_t* , int );
52 static void DialogDestroy( interaction_dialog_t *p_dialog );
53 static int DialogSend( vlc_object_t *p_this, interaction_dialog_t *p_dialog );
56 * Destroy the interaction system
57 * \param The interaction object to destroy
60 void intf_InteractionDestroy( interaction_t *p_interaction )
63 // Remove all dialogs - Interfaces must be able to clean up their data
64 for( i = p_interaction->i_dialogs -1 ; i >= 0; i-- )
66 interaction_dialog_t * p_dialog = p_interaction->pp_dialogs[i];
67 DialogDestroy( p_dialog );
68 REMOVE_ELEM( p_interaction->pp_dialogs, p_interaction->i_dialogs, i );
70 vlc_object_destroy( p_interaction );
74 * The main interaction processing loop
75 * This function is called from the playlist loop
77 * \param p_playlist the parent playlist
80 void intf_InteractionManage( playlist_t *p_playlist )
84 interaction_t *p_interaction = p_playlist->p_interaction;
87 if( p_interaction->i_dialogs == 0 ) return;
89 vlc_mutex_lock( &p_interaction->object_lock );
91 InteractionSearchInterface( p_interaction );
92 if( !p_interaction->p_intf )
94 // We mark all dialogs as answered with their "default" answer
95 for( i_index = 0 ; i_index < p_interaction->i_dialogs; i_index ++ )
97 interaction_dialog_t *p_dialog = p_interaction->pp_dialogs[i_index];
98 p_dialog->i_return = DIALOG_DEFAULT; // Give default answer
100 // Pretend we have hidden and destroyed it
101 if( p_dialog->i_status == HIDDEN_DIALOG )
102 p_dialog->i_status = DESTROYED_DIALOG;
104 p_dialog->i_status = HIDING_DIALOG;
108 vlc_object_yield( p_interaction->p_intf );
110 for( i_index = 0 ; i_index < p_interaction->i_dialogs; i_index ++ )
112 interaction_dialog_t *p_dialog = p_interaction->pp_dialogs[i_index];
113 switch( p_dialog->i_status )
115 case ANSWERED_DIALOG:
116 // Ask interface to hide it
117 p_dialog->i_action = INTERACT_HIDE;
118 val.p_address = p_dialog;
119 if( p_interaction->p_intf )
120 var_Set( p_interaction->p_intf, "interaction", val );
121 p_dialog->i_status = HIDING_DIALOG;
124 p_dialog->i_action = INTERACT_UPDATE;
125 val.p_address = p_dialog;
126 if( p_interaction->p_intf )
127 var_Set( p_interaction->p_intf, "interaction", val );
128 p_dialog->i_status = SENT_DIALOG;
131 if( !(p_dialog->i_flags & DIALOG_GOT_ANSWER) ) break;
132 p_dialog->i_action = INTERACT_DESTROY;
133 val.p_address = p_dialog;
134 if( p_interaction->p_intf )
135 var_Set( p_interaction->p_intf, "interaction", val );
137 case DESTROYED_DIALOG:
138 // Interface has now destroyed it, remove it
139 REMOVE_ELEM( p_interaction->pp_dialogs, p_interaction->i_dialogs,
142 DialogDestroy( p_dialog );
145 // This is truly a new dialog, send it.
146 p_dialog->i_action = INTERACT_NEW;
147 val.p_address = p_dialog;
148 if( p_interaction->p_intf )
149 var_Set( p_interaction->p_intf, "interaction", val );
150 p_dialog->i_status = SENT_DIALOG;
155 if( p_interaction->p_intf )
157 vlc_object_release( p_interaction->p_intf );
160 vlc_mutex_unlock( &p_playlist->p_interaction->object_lock );
163 #define DIALOG_INIT( type ) \
164 DECMALLOC_ERR( p_new, interaction_dialog_t ); \
165 memset( p_new, 0, sizeof( interaction_dialog_t ) ); \
166 p_new->b_cancelled = VLC_FALSE; \
167 p_new->i_status = NEW_DIALOG; \
168 p_new->i_flags = 0; \
169 p_new->i_type = INTERACT_DIALOG_##type; \
170 p_new->psz_returned[0] = NULL; \
171 p_new->psz_returned[1] = NULL;
173 #define FORMAT_DESC \
174 va_start( args, psz_format ); \
175 vasprintf( &p_new->psz_description, psz_format, args ); \
178 /** Send an error message, both in a blocking and non-blocking way
179 * \param p_this Parent vlc_object
180 * \param b_blocking Is this dialog blocking or not?
181 * \param psz_title Title for the dialog
182 * \param psz_format The message to display
184 int __intf_UserFatal( vlc_object_t *p_this, vlc_bool_t b_blocking,
185 const char *psz_title,
186 const char *psz_format, ... )
189 DIALOG_INIT( ONEWAY );
191 p_new->psz_title = strdup( psz_title );
195 p_new->i_flags = DIALOG_BLOCKING_ERROR;
197 p_new->i_flags = DIALOG_NONBLOCKING_ERROR;
199 return DialogSend( p_this, p_new );
202 /** Helper function to send an warning, which is always shown non-blocking
203 * \param p_this Parent vlc_object
204 * \param psz_title Title for the dialog
205 * \param psz_format The message to display
207 int __intf_UserWarn( vlc_object_t *p_this,
208 const char *psz_title,
209 const char *psz_format, ... )
212 DIALOG_INIT( ONEWAY );
214 p_new->psz_title = strdup( psz_title );
217 p_new->i_flags = DIALOG_WARNING;
219 return DialogSend( p_this, p_new );
222 /** Helper function to ask a yes-no-cancel question
223 * \param p_this Parent vlc_object
224 * \param psz_title Title for the dialog
225 * \param psz_description A description
226 * \param psz_default caption for the default button
227 * \param psz_alternate caption for the alternate button
228 * \param psz_other caption for the optional 3rd button (== cancel)
229 * \return Clicked button code
231 int __intf_UserYesNo( vlc_object_t *p_this,
232 const char *psz_title,
233 const char *psz_description,
234 const char *psz_default,
235 const char *psz_alternate,
236 const char *psz_other )
238 DIALOG_INIT( TWOWAY );
240 p_new->psz_title = strdup( psz_title );
241 p_new->psz_description = strdup( psz_description );
242 p_new->i_flags = DIALOG_YES_NO_CANCEL;
243 p_new->psz_default_button = strdup( psz_default );
244 p_new->psz_alternate_button = strdup( psz_alternate );
246 p_new->psz_other_button = strdup( psz_other );
248 return DialogSend( p_this, p_new );
251 /** Helper function to create a dialogue showing a progress-bar with some info
252 * \param p_this Parent vlc_object
253 * \param psz_title Title for the dialog (NULL implies main intf )
254 * \param psz_status Current status
255 * \param f_position Current position (0.0->100.0)
256 * \param i_timeToGo Time (in sec) to go until process is finished
257 * \return Dialog id, to give to UserProgressUpdate
259 int __intf_Progress( vlc_object_t *p_this, const char *psz_title,
260 const char *psz_status, float f_pos, int i_time )
262 DIALOG_INIT( ONEWAY );
263 p_new->psz_description = strdup( psz_status );
264 p_new->val.f_float = f_pos;
265 p_new->i_timeToGo = i_time;
266 p_new->psz_alternate_button = strdup( _("Cancel" ) );
270 p_new->psz_title = strdup( psz_title );
271 p_new->i_flags = DIALOG_USER_PROGRESS;
274 p_new->i_flags = DIALOG_INTF_PROGRESS;
276 DialogSend( p_this, p_new );
280 /** Update a progress bar in a dialogue
281 * \param p_this Parent vlc_object
282 * \param i_id Identifier of the dialog
283 * \param psz_status New status
284 * \param f_position New position (0.0->100.0)
285 * \param i_timeToGo Time (in sec) to go until process is finished
288 void __intf_ProgressUpdate( vlc_object_t *p_this, int i_id,
289 const char *psz_status, float f_pos, int i_time )
291 interaction_t *p_interaction = InteractionGet( p_this );
292 interaction_dialog_t *p_dialog;
294 if( !p_interaction ) return;
296 vlc_mutex_lock( &p_interaction->object_lock );
297 p_dialog = DialogGetById( p_interaction, i_id );
301 vlc_mutex_unlock( &p_interaction->object_lock ) ;
305 free( p_dialog->psz_description );
306 p_dialog->psz_description = strdup( psz_status );
308 p_dialog->val.f_float = f_pos;
309 p_dialog->i_timeToGo = i_time;
311 p_dialog->i_status = UPDATED_DIALOG;
312 vlc_mutex_unlock( &p_interaction->object_lock) ;
315 /** Helper function to communicate dialogue cancellations between the
316 * interface module and the caller
317 * \param p_this Parent vlc_object
318 * \param i_id Identifier of the dialogue
319 * \return Either true or false
321 vlc_bool_t __intf_UserProgressIsCancelled( vlc_object_t *p_this, int i_id )
323 interaction_t *p_interaction = InteractionGet( p_this );
324 interaction_dialog_t *p_dialog;
327 if( !p_interaction ) return VLC_TRUE;
329 vlc_mutex_lock( &p_interaction->object_lock );
330 p_dialog = DialogGetById( p_interaction, i_id );
333 vlc_mutex_unlock( &p_interaction->object_lock ) ;
337 b_cancel = p_dialog->b_cancelled;
338 vlc_mutex_unlock( &p_interaction->object_lock );
342 /** Helper function to make a login/password dialogue
343 * \param p_this Parent vlc_object
344 * \param psz_title Title for the dialog
345 * \param psz_description A description
346 * \param ppsz_login Returned login
347 * \param ppsz_password Returned password
348 * \return Clicked button code
350 int __intf_UserLoginPassword( vlc_object_t *p_this,
351 const char *psz_title,
352 const char *psz_description,
354 char **ppsz_password )
357 DIALOG_INIT( TWOWAY );
358 p_new->i_type = INTERACT_DIALOG_TWOWAY;
359 p_new->psz_title = strdup( psz_title );
360 p_new->psz_description = strdup( psz_description );
361 p_new->psz_default_button = strdup( _("Ok" ) );
362 p_new->psz_alternate_button = strdup( _("Cancel" ) );
364 p_new->i_flags = DIALOG_LOGIN_PW_OK_CANCEL;
366 i_ret = DialogSend( p_this, p_new );
368 if( i_ret != DIALOG_CANCELLED && i_ret != VLC_EGENERIC )
370 *ppsz_login = p_new->psz_returned[0]?
371 strdup( p_new->psz_returned[0] ) : NULL;
372 *ppsz_password = p_new->psz_returned[1]?
373 strdup( p_new->psz_returned[1] ) : NULL;
378 /** Helper function to make a dialogue asking the user for !password string
379 * \param p_this Parent vlc_object
380 * \param psz_title Title for the dialog
381 * \param psz_description A description
382 * \param ppsz_usersString Returned login
383 * \return Clicked button code
385 int __intf_UserStringInput( vlc_object_t *p_this,
386 const char *psz_title,
387 const char *psz_description,
388 char **ppsz_usersString )
391 DIALOG_INIT( TWOWAY );
392 p_new->i_type = INTERACT_DIALOG_TWOWAY;
393 p_new->psz_title = strdup( psz_title );
394 p_new->psz_description = strdup( psz_description );
396 p_new->i_flags = DIALOG_PSZ_INPUT_OK_CANCEL;
398 i_ret = DialogSend( p_this, p_new );
400 if( i_ret != DIALOG_CANCELLED )
402 *ppsz_usersString = p_new->psz_returned[0]?
403 strdup( p_new->psz_returned[0] ) : NULL;
408 /** Hide an interaction dialog
409 * \param p_this the parent vlc object
410 * \param i_id the id of the item to hide
413 void __intf_UserHide( vlc_object_t *p_this, int i_id )
415 interaction_t *p_interaction = InteractionGet( p_this );
416 interaction_dialog_t *p_dialog;
418 if( !p_interaction ) return;
420 vlc_mutex_lock( &p_interaction->object_lock );
421 p_dialog = DialogGetById( p_interaction, i_id );
425 vlc_mutex_unlock( &p_interaction->object_lock );
429 p_dialog->i_status = ANSWERED_DIALOG;
430 vlc_mutex_unlock( &p_interaction->object_lock );
433 /**********************************************************************
434 * The following functions are local
435 **********************************************************************/
437 /* Get the interaction object. Create it if needed */
438 static interaction_t * InteractionGet( vlc_object_t *p_this )
440 interaction_t *p_interaction;
441 playlist_t *p_playlist = pl_Yield( p_this );
444 if( p_playlist->p_interaction == NULL )
445 InteractionInit( p_playlist );
447 p_interaction = p_playlist->p_interaction;
450 pl_Release( p_this );
451 return p_interaction;
454 /* Create the interaction object in the given playlist object */
455 static void InteractionInit( playlist_t *p_playlist )
457 interaction_t *p_interaction = vlc_object_create( VLC_OBJECT( p_playlist ),
458 sizeof( interaction_t ) );
461 msg_Err( p_playlist,"out of memory" );
465 p_interaction->i_dialogs = 0;
466 p_interaction->pp_dialogs = NULL;
467 p_interaction->p_intf = NULL;
468 p_interaction->i_last_id = 0;
470 vlc_mutex_init( p_interaction , &p_interaction->object_lock );
471 p_playlist->p_interaction = p_interaction;
474 /* Look for an interface suitable for interaction */
475 static void InteractionSearchInterface( interaction_t *p_interaction )
480 p_interaction->p_intf = NULL;
482 p_list = vlc_list_find( p_interaction, VLC_OBJECT_INTF, FIND_ANYWHERE );
485 msg_Err( p_interaction, "unable to create module list" );
489 for( i_index = 0; i_index < p_list->i_count; i_index ++ )
491 intf_thread_t *p_intf = (intf_thread_t *)
492 p_list->p_values[i_index].p_object;
493 if( p_intf->b_interaction )
495 p_interaction->p_intf = p_intf;
499 vlc_list_release ( p_list );
502 /* Find an interaction dialog by its id */
503 static interaction_dialog_t *DialogGetById( interaction_t *p_interaction,
507 for( i = 0 ; i< p_interaction->i_dialogs; i++ )
509 if( p_interaction->pp_dialogs[i]->i_id == i_id )
510 return p_interaction->pp_dialogs[i];
515 /* Destroy a dialog */
516 static void DialogDestroy( interaction_dialog_t *p_dialog )
518 free( p_dialog->psz_title );
519 free( p_dialog->psz_description );
520 free( p_dialog->psz_default_button );
521 free( p_dialog->psz_alternate_button );
522 free( p_dialog->psz_other_button );
526 /* Ask for the dialog to be sent to the user. Wait for answer
528 static int DialogSend( vlc_object_t *p_this, interaction_dialog_t *p_dialog )
530 interaction_t *p_interaction = InteractionGet( p_this );
532 /* Get an id, if we don't already have one */
533 if( p_dialog->i_id == 0 )
534 p_dialog->i_id = ++p_interaction->i_last_id;
536 if( p_this->i_flags & OBJECT_FLAGS_NOINTERACT ) return VLC_EGENERIC;
538 if( config_GetInt(p_this, "interact") ||
539 p_dialog->i_flags & DIALOG_BLOCKING_ERROR ||
540 p_dialog->i_flags & DIALOG_NONBLOCKING_ERROR )
542 vlc_bool_t b_found = VLC_FALSE;
544 p_dialog->p_interaction = p_interaction;
545 p_dialog->p_parent = p_this;
547 /* Check if we have already added this dialog */
548 vlc_mutex_lock( &p_interaction->object_lock );
549 for( i = 0 ; i< p_interaction->i_dialogs; i++ )
551 if( p_interaction->pp_dialogs[i]->i_id == p_dialog->i_id )
554 /* Add it to the queue, the main loop will send the orders to the
558 INSERT_ELEM( p_interaction->pp_dialogs,
559 p_interaction->i_dialogs,
560 p_interaction->i_dialogs,
564 p_dialog->i_status = UPDATED_DIALOG;
566 if( p_dialog->i_type == INTERACT_DIALOG_TWOWAY ) // Wait for answer
568 playlist_Signal( pl_Get(p_this) );
569 while( p_dialog->i_status != ANSWERED_DIALOG &&
570 p_dialog->i_status != HIDING_DIALOG &&
571 p_dialog->i_status != HIDDEN_DIALOG &&
572 !p_dialog->p_parent->b_die )
574 vlc_mutex_unlock( &p_interaction->object_lock );
576 vlc_mutex_lock( &p_interaction->object_lock );
578 if( p_dialog->p_parent->b_die )
580 p_dialog->i_return = DIALOG_CANCELLED;
581 p_dialog->i_status = ANSWERED_DIALOG;
583 p_dialog->i_flags |= DIALOG_GOT_ANSWER;
584 vlc_mutex_unlock( &p_interaction->object_lock );
585 playlist_Signal( pl_Get(p_this) );
586 return p_dialog->i_return;
590 // Pretend we already retrieved the "answer"
591 p_dialog->i_flags |= DIALOG_GOT_ANSWER;
592 vlc_mutex_unlock( &p_interaction->object_lock );
593 playlist_Signal( pl_Get(p_this) );