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 )
106 p_dialog->i_status = DESTROYED_DIALOG;
110 p_dialog->i_status = HIDING_DIALOG;
116 vlc_object_yield( p_interaction->p_intf );
119 for( i_index = 0 ; i_index < p_interaction->i_dialogs; i_index ++ )
121 interaction_dialog_t *p_dialog = p_interaction->pp_dialogs[i_index];
122 switch( p_dialog->i_status )
124 case ANSWERED_DIALOG:
125 // Ask interface to hide it
126 p_dialog->i_action = INTERACT_HIDE;
127 val.p_address = p_dialog;
128 if( p_interaction->p_intf )
129 var_Set( p_interaction->p_intf, "interaction", val );
130 p_dialog->i_status = HIDING_DIALOG;
133 p_dialog->i_action = INTERACT_UPDATE;
134 val.p_address = p_dialog;
135 if( p_interaction->p_intf )
136 var_Set( p_interaction->p_intf, "interaction", val );
137 p_dialog->i_status = SENT_DIALOG;
140 if( !(p_dialog->i_flags & DIALOG_GOT_ANSWER) ) break;
141 p_dialog->i_action = INTERACT_DESTROY;
142 val.p_address = p_dialog;
143 if( p_interaction->p_intf )
144 var_Set( p_interaction->p_intf, "interaction", val );
146 case DESTROYED_DIALOG:
147 // Interface has now destroyed it, remove it
148 REMOVE_ELEM( p_interaction->pp_dialogs, p_interaction->i_dialogs,
151 DialogDestroy( p_dialog );
154 // This is truly a new dialog, send it.
155 p_dialog->i_action = INTERACT_NEW;
156 val.p_address = p_dialog;
157 if( p_interaction->p_intf )
158 var_Set( p_interaction->p_intf, "interaction", val );
159 p_dialog->i_status = SENT_DIALOG;
164 if( p_interaction->p_intf )
166 vlc_object_release( p_interaction->p_intf );
169 vlc_mutex_unlock( &p_playlist->p_interaction->object_lock );
172 #define DIALOG_INIT( type ) \
173 DECMALLOC_ERR( p_new, interaction_dialog_t ); \
174 memset( p_new, 0, sizeof( interaction_dialog_t ) ); \
175 p_new->b_cancelled = VLC_FALSE; \
176 p_new->i_status = NEW_DIALOG; \
177 p_new->i_flags = 0; \
178 p_new->i_type = INTERACT_DIALOG_##type; \
179 p_new->psz_returned[0] = NULL; \
180 p_new->psz_returned[1] = NULL;
182 #define FORMAT_DESC \
183 va_start( args, psz_format ); \
184 vasprintf( &p_new->psz_description, psz_format, args ); \
187 /** Send an error message, both in a blocking and non-blocking way
188 * \param p_this Parent vlc_object
189 * \param b_blocking Is this dialog blocking or not?
190 * \param psz_title Title for the dialog
191 * \param psz_format The message to display
193 int __intf_UserFatal( vlc_object_t *p_this, vlc_bool_t b_blocking,
194 const char *psz_title,
195 const char *psz_format, ... )
198 DIALOG_INIT( ONEWAY );
200 p_new->psz_title = strdup( psz_title );
204 p_new->i_flags = DIALOG_BLOCKING_ERROR;
206 p_new->i_flags = DIALOG_NONBLOCKING_ERROR;
208 return DialogSend( p_this, p_new );
211 /** Helper function to send an warning, which is always shown non-blocking
212 * \param p_this Parent vlc_object
213 * \param psz_title Title for the dialog
214 * \param psz_format The message to display
216 int __intf_UserWarn( vlc_object_t *p_this,
217 const char *psz_title,
218 const char *psz_format, ... )
221 DIALOG_INIT( ONEWAY );
223 p_new->psz_title = strdup( psz_title );
226 p_new->i_flags = DIALOG_WARNING;
228 return DialogSend( p_this, p_new );
231 /** Helper function to ask a yes-no-cancel question
232 * \param p_this Parent vlc_object
233 * \param psz_title Title for the dialog
234 * \param psz_description A description
235 * \param psz_default caption for the default button
236 * \param psz_alternate caption for the alternate button
237 * \param psz_other caption for the optional 3rd button (== cancel)
238 * \return Clicked button code
240 int __intf_UserYesNo( vlc_object_t *p_this,
241 const char *psz_title,
242 const char *psz_description,
243 const char *psz_default,
244 const char *psz_alternate,
245 const char *psz_other )
247 DIALOG_INIT( TWOWAY );
249 p_new->psz_title = strdup( psz_title );
250 p_new->psz_description = strdup( psz_description );
251 p_new->i_flags = DIALOG_YES_NO_CANCEL;
252 p_new->psz_default_button = strdup( psz_default );
253 p_new->psz_alternate_button = strdup( psz_alternate );
255 p_new->psz_other_button = strdup( psz_other );
257 return DialogSend( p_this, p_new );
260 /** Helper function to create a dialogue showing a progress-bar with some info
261 * \param p_this Parent vlc_object
262 * \param psz_title Title for the dialog (NULL implies main intf )
263 * \param psz_status Current status
264 * \param f_position Current position (0.0->100.0)
265 * \param i_timeToGo Time (in sec) to go until process is finished
266 * \return Dialog id, to give to UserProgressUpdate
268 int __intf_Progress( vlc_object_t *p_this, const char *psz_title,
269 const char *psz_status, float f_pos, int i_time )
271 DIALOG_INIT( ONEWAY );
272 p_new->psz_description = strdup( psz_status );
273 p_new->val.f_float = f_pos;
274 p_new->i_timeToGo = i_time;
275 p_new->psz_alternate_button = strdup( _("Cancel" ) );
279 p_new->psz_title = strdup( psz_title );
280 p_new->i_flags = DIALOG_USER_PROGRESS;
283 p_new->i_flags = DIALOG_INTF_PROGRESS;
285 DialogSend( p_this, p_new );
289 /** Update a progress bar in a dialogue
290 * \param p_this Parent vlc_object
291 * \param i_id Identifier of the dialog
292 * \param psz_status New status
293 * \param f_position New position (0.0->100.0)
294 * \param i_timeToGo Time (in sec) to go until process is finished
297 void __intf_ProgressUpdate( vlc_object_t *p_this, int i_id,
298 const char *psz_status, float f_pos, int i_time )
300 interaction_t *p_interaction = InteractionGet( p_this );
301 interaction_dialog_t *p_dialog;
303 if( !p_interaction ) return;
305 vlc_mutex_lock( &p_interaction->object_lock );
306 p_dialog = DialogGetById( p_interaction, i_id );
310 vlc_mutex_unlock( &p_interaction->object_lock ) ;
314 FREE( p_dialog->psz_description );
315 p_dialog->psz_description = strdup( psz_status );
317 p_dialog->val.f_float = f_pos;
318 p_dialog->i_timeToGo = i_time;
320 p_dialog->i_status = UPDATED_DIALOG;
321 vlc_mutex_unlock( &p_interaction->object_lock) ;
324 /** Helper function to communicate dialogue cancellations between the
325 * interface module and the caller
326 * \param p_this Parent vlc_object
327 * \param i_id Identifier of the dialogue
328 * \return Either true or false
330 vlc_bool_t __intf_UserProgressIsCancelled( vlc_object_t *p_this, int i_id )
332 interaction_t *p_interaction = InteractionGet( p_this );
333 interaction_dialog_t *p_dialog;
336 if( !p_interaction ) return VLC_TRUE;
338 vlc_mutex_lock( &p_interaction->object_lock );
339 p_dialog = DialogGetById( p_interaction, i_id );
342 vlc_mutex_unlock( &p_interaction->object_lock ) ;
346 b_cancel = p_dialog->b_cancelled;
347 vlc_mutex_unlock( &p_interaction->object_lock );
351 /** Helper function to make a login/password dialogue
352 * \param p_this Parent vlc_object
353 * \param psz_title Title for the dialog
354 * \param psz_description A description
355 * \param ppsz_login Returned login
356 * \param ppsz_password Returned password
357 * \return Clicked button code
359 int __intf_UserLoginPassword( vlc_object_t *p_this,
360 const char *psz_title,
361 const char *psz_description,
363 char **ppsz_password )
366 DIALOG_INIT( TWOWAY );
367 p_new->i_type = INTERACT_DIALOG_TWOWAY;
368 p_new->psz_title = strdup( psz_title );
369 p_new->psz_description = strdup( psz_description );
371 p_new->i_flags = DIALOG_LOGIN_PW_OK_CANCEL;
373 i_ret = DialogSend( p_this, p_new );
375 if( i_ret != DIALOG_CANCELLED )
377 assert( p_new->psz_returned[0] && p_new->psz_returned[1] );
378 *ppsz_login = strdup( p_new->psz_returned[0] );
379 *ppsz_password = strdup( p_new->psz_returned[1] );
384 /** Helper function to make a dialogue asking the user for !password string
385 * \param p_this Parent vlc_object
386 * \param psz_title Title for the dialog
387 * \param psz_description A description
388 * \param ppsz_usersString Returned login
389 * \return Clicked button code
391 int __intf_UserStringInput( vlc_object_t *p_this,
392 const char *psz_title,
393 const char *psz_description,
394 char **ppsz_usersString )
397 DIALOG_INIT( TWOWAY );
398 p_new->psz_title = strdup( psz_title );
399 p_new->psz_description = strdup( psz_description );
401 p_new->i_flags = DIALOG_PSZ_INPUT_OK_CANCEL;
403 i_ret = DialogSend( p_this, p_new );
405 if( i_ret != DIALOG_CANCELLED )
407 assert( p_new->psz_returned[0] );
408 *ppsz_usersString = strdup( p_new->psz_returned[0] );
413 /** Hide an interaction dialog
414 * \param p_this the parent vlc object
415 * \param i_id the id of the item to hide
418 void __intf_UserHide( vlc_object_t *p_this, int i_id )
420 interaction_t *p_interaction = InteractionGet( p_this );
421 interaction_dialog_t *p_dialog;
423 if( !p_interaction ) return;
425 vlc_mutex_lock( &p_interaction->object_lock );
426 p_dialog = DialogGetById( p_interaction, i_id );
430 vlc_mutex_unlock( &p_interaction->object_lock );
434 p_dialog->i_status = ANSWERED_DIALOG;
435 vlc_mutex_unlock( &p_interaction->object_lock );
438 /**********************************************************************
439 * The following functions are local
440 **********************************************************************/
442 /* Get the interaction object. Create it if needed */
443 static interaction_t * InteractionGet( vlc_object_t *p_this )
445 playlist_t *p_playlist;
446 interaction_t *p_interaction;
448 p_playlist = (playlist_t*) vlc_object_find( p_this, VLC_OBJECT_PLAYLIST,
453 if( p_playlist->p_interaction == NULL )
454 InteractionInit( p_playlist );
456 p_interaction = p_playlist->p_interaction;
458 vlc_object_release( p_playlist );
459 return p_interaction;
462 /* Create the interaction object in the given playlist object */
463 static void InteractionInit( playlist_t *p_playlist )
465 interaction_t *p_interaction = vlc_object_create( VLC_OBJECT( p_playlist ),
466 sizeof( interaction_t ) );
469 msg_Err( p_playlist,"out of memory" );
473 p_interaction->i_dialogs = 0;
474 p_interaction->pp_dialogs = NULL;
475 p_interaction->p_intf = NULL;
476 p_interaction->i_last_id = DIALOG_LAST_PREDEFINED + 1;
478 vlc_mutex_init( p_interaction , &p_interaction->object_lock );
480 p_playlist->p_interaction = p_interaction;
483 /* Look for an interface suitable for interaction */
484 static void InteractionSearchInterface( interaction_t *p_interaction )
489 p_interaction->p_intf = NULL;
491 p_list = vlc_list_find( p_interaction, VLC_OBJECT_INTF, FIND_ANYWHERE );
494 msg_Err( p_interaction, "unable to create module list" );
498 for( i_index = 0; i_index < p_list->i_count; i_index ++ )
500 intf_thread_t *p_intf = (intf_thread_t *)
501 p_list->p_values[i_index].p_object;
502 if( p_intf->b_interaction )
504 p_interaction->p_intf = p_intf;
508 vlc_list_release ( p_list );
511 /* Find an interaction dialog by its id */
512 static interaction_dialog_t *DialogGetById( interaction_t *p_interaction,
516 for( i = 0 ; i< p_interaction->i_dialogs; i++ )
518 if( p_interaction->pp_dialogs[i]->i_id == i_id )
519 return p_interaction->pp_dialogs[i];
524 /* Destroy a dialog */
525 static void DialogDestroy( interaction_dialog_t *p_dialog )
527 FREENULL( p_dialog->psz_title );
528 FREENULL( p_dialog->psz_description );
529 FREENULL( p_dialog->psz_default_button );
530 FREENULL( p_dialog->psz_alternate_button );
531 FREENULL( p_dialog->psz_other_button );
535 /* Ask for the dialog to be sent to the user. Wait for answer
537 static int DialogSend( vlc_object_t *p_this, interaction_dialog_t *p_dialog )
539 interaction_t *p_interaction = InteractionGet( p_this );
541 /* Get an id, if we don't already have one */
542 if( p_dialog->i_id == 0 )
543 p_dialog->i_id = ++p_interaction->i_last_id;
545 if( p_this->i_flags & OBJECT_FLAGS_NOINTERACT ) return VLC_EGENERIC;
547 if( config_GetInt(p_this, "interact") ||
548 p_dialog->i_flags & DIALOG_BLOCKING_ERROR ||
549 p_dialog->i_flags & DIALOG_NONBLOCKING_ERROR )
551 vlc_bool_t b_found = VLC_FALSE;
553 p_dialog->p_interaction = p_interaction;
554 p_dialog->p_parent = p_this;
556 /* Check if we have already added this dialog */
557 vlc_mutex_lock( &p_interaction->object_lock );
558 for( i = 0 ; i< p_interaction->i_dialogs; i++ )
560 if( p_interaction->pp_dialogs[i]->i_id == p_dialog->i_id )
563 /* Add it to the queue, the main loop will send the orders to the
567 INSERT_ELEM( p_interaction->pp_dialogs,
568 p_interaction->i_dialogs,
569 p_interaction->i_dialogs,
573 p_dialog->i_status = UPDATED_DIALOG;
575 if( p_dialog->i_type == INTERACT_DIALOG_TWOWAY ) // Wait for answer
577 while( p_dialog->i_status != ANSWERED_DIALOG &&
578 p_dialog->i_status != HIDING_DIALOG &&
579 p_dialog->i_status != HIDDEN_DIALOG &&
580 !p_dialog->p_parent->b_die )
582 vlc_mutex_unlock( &p_interaction->object_lock );
584 vlc_mutex_lock( &p_interaction->object_lock );
586 if( p_dialog->p_parent->b_die )
588 p_dialog->i_return = DIALOG_CANCELLED;
589 p_dialog->i_status = ANSWERED_DIALOG;
591 p_dialog->i_flags |= DIALOG_GOT_ANSWER;
592 vlc_mutex_unlock( &p_interaction->object_lock );
593 return p_dialog->i_return;
597 // Pretend we already retrieved the "answer"
598 p_dialog->i_flags |= DIALOG_GOT_ANSWER;
599 vlc_mutex_unlock( &p_interaction->object_lock );