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;
278 p_new->psz_title = strdup( psz_title );
279 p_new->i_flags = DIALOG_USER_PROGRESS;
282 p_new->i_flags = DIALOG_INTF_PROGRESS;
284 DialogSend( p_this, p_new );
288 /** Update a progress bar in a dialogue
289 * \param p_this Parent vlc_object
290 * \param i_id Identifier of the dialog
291 * \param psz_status New status
292 * \param f_position New position (0.0->100.0)
293 * \param i_timeToGo Time (in sec) to go until process is finished
296 void __intf_ProgressUpdate( vlc_object_t *p_this, int i_id,
297 const char *psz_status, float f_pos, int i_time )
299 interaction_t *p_interaction = InteractionGet( p_this );
300 interaction_dialog_t *p_dialog;
302 if( !p_interaction ) return;
304 vlc_mutex_lock( &p_interaction->object_lock );
305 p_dialog = DialogGetById( p_interaction, i_id );
309 vlc_mutex_unlock( &p_interaction->object_lock ) ;
313 FREE( p_dialog->psz_description );
314 p_dialog->psz_description = strdup( psz_status );
316 p_dialog->val.f_float = f_pos;
317 p_dialog->i_timeToGo = i_time;
319 p_dialog->i_status = UPDATED_DIALOG;
320 vlc_mutex_unlock( &p_interaction->object_lock) ;
323 /** Helper function to communicate dialogue cancellations between the
324 * interface module and the caller
325 * \param p_this Parent vlc_object
326 * \param i_id Identifier of the dialogue
327 * \return Either true or false
329 vlc_bool_t __intf_UserProgressIsCancelled( vlc_object_t *p_this, int i_id )
331 interaction_t *p_interaction = InteractionGet( p_this );
332 interaction_dialog_t *p_dialog;
335 if( !p_interaction ) return VLC_TRUE;
337 vlc_mutex_lock( &p_interaction->object_lock );
338 p_dialog = DialogGetById( p_interaction, i_id );
341 vlc_mutex_unlock( &p_interaction->object_lock ) ;
345 b_cancel = p_dialog->b_cancelled;
346 vlc_mutex_unlock( &p_interaction->object_lock );
350 /** Helper function to make a login/password dialogue
351 * \param p_this Parent vlc_object
352 * \param psz_title Title for the dialog
353 * \param psz_description A description
354 * \param ppsz_login Returned login
355 * \param ppsz_password Returned password
356 * \return Clicked button code
358 int __intf_UserLoginPassword( vlc_object_t *p_this,
359 const char *psz_title,
360 const char *psz_description,
362 char **ppsz_password )
365 DIALOG_INIT( TWOWAY );
366 p_new->i_type = INTERACT_DIALOG_TWOWAY;
367 p_new->psz_title = strdup( psz_title );
368 p_new->psz_description = strdup( psz_description );
370 p_new->i_flags = DIALOG_LOGIN_PW_OK_CANCEL;
372 i_ret = DialogSend( p_this, p_new );
374 if( i_ret != DIALOG_CANCELLED )
376 assert( p_new->psz_returned[0] && p_new->psz_returned[1] );
377 *ppsz_login = strdup( p_new->psz_returned[0] );
378 *ppsz_password = strdup( p_new->psz_returned[1] );
383 /** Helper function to make a dialogue asking the user for !password string
384 * \param p_this Parent vlc_object
385 * \param psz_title Title for the dialog
386 * \param psz_description A description
387 * \param ppsz_usersString Returned login
388 * \return Clicked button code
390 int __intf_UserStringInput( vlc_object_t *p_this,
391 const char *psz_title,
392 const char *psz_description,
393 char **ppsz_usersString )
396 DIALOG_INIT( TWOWAY );
397 p_new->psz_title = strdup( psz_title );
398 p_new->psz_description = strdup( psz_description );
400 p_new->i_flags = DIALOG_PSZ_INPUT_OK_CANCEL;
402 i_ret = DialogSend( p_this, p_new );
404 if( i_ret != DIALOG_CANCELLED )
406 assert( p_new->psz_returned[0] );
407 *ppsz_usersString = strdup( p_new->psz_returned[0] );
412 /** Hide an interaction dialog
413 * \param p_this the parent vlc object
414 * \param i_id the id of the item to hide
417 void __intf_UserHide( vlc_object_t *p_this, int i_id )
419 interaction_t *p_interaction = InteractionGet( p_this );
420 interaction_dialog_t *p_dialog;
422 if( !p_interaction ) return;
424 vlc_mutex_lock( &p_interaction->object_lock );
425 p_dialog = DialogGetById( p_interaction, i_id );
429 vlc_mutex_unlock( &p_interaction->object_lock );
433 p_dialog->i_status = ANSWERED_DIALOG;
434 vlc_mutex_unlock( &p_interaction->object_lock );
437 /**********************************************************************
438 * The following functions are local
439 **********************************************************************/
441 /* Get the interaction object. Create it if needed */
442 static interaction_t * InteractionGet( vlc_object_t *p_this )
444 playlist_t *p_playlist;
445 interaction_t *p_interaction;
447 p_playlist = (playlist_t*) vlc_object_find( p_this, VLC_OBJECT_PLAYLIST,
452 if( p_playlist->p_interaction == NULL )
453 InteractionInit( p_playlist );
455 p_interaction = p_playlist->p_interaction;
457 vlc_object_release( p_playlist );
458 return p_interaction;
461 /* Create the interaction object in the given playlist object */
462 static void InteractionInit( playlist_t *p_playlist )
464 interaction_t *p_interaction = vlc_object_create( VLC_OBJECT( p_playlist ),
465 sizeof( interaction_t ) );
468 msg_Err( p_playlist,"out of memory" );
472 p_interaction->i_dialogs = 0;
473 p_interaction->pp_dialogs = NULL;
474 p_interaction->p_intf = NULL;
475 p_interaction->i_last_id = DIALOG_LAST_PREDEFINED + 1;
477 vlc_mutex_init( p_interaction , &p_interaction->object_lock );
479 p_playlist->p_interaction = p_interaction;
482 /* Look for an interface suitable for interaction */
483 static void InteractionSearchInterface( interaction_t *p_interaction )
488 p_interaction->p_intf = NULL;
490 p_list = vlc_list_find( p_interaction, VLC_OBJECT_INTF, FIND_ANYWHERE );
493 msg_Err( p_interaction, "unable to create module list" );
497 for( i_index = 0; i_index < p_list->i_count; i_index ++ )
499 intf_thread_t *p_intf = (intf_thread_t *)
500 p_list->p_values[i_index].p_object;
501 if( p_intf->b_interaction )
503 p_interaction->p_intf = p_intf;
507 vlc_list_release ( p_list );
510 /* Find an interaction dialog by its id */
511 static interaction_dialog_t *DialogGetById( interaction_t *p_interaction,
515 for( i = 0 ; i< p_interaction->i_dialogs; i++ )
517 if( p_interaction->pp_dialogs[i]->i_id == i_id )
518 return p_interaction->pp_dialogs[i];
523 /* Destroy a dialog */
524 static void DialogDestroy( interaction_dialog_t *p_dialog )
526 FREENULL( p_dialog->psz_title );
527 FREENULL( p_dialog->psz_description );
528 FREENULL( p_dialog->psz_default_button );
529 FREENULL( p_dialog->psz_alternate_button );
530 FREENULL( p_dialog->psz_other_button );
534 /* Ask for the dialog to be sent to the user. Wait for answer
536 static int DialogSend( vlc_object_t *p_this, interaction_dialog_t *p_dialog )
538 interaction_t *p_interaction = InteractionGet( p_this );
540 /* Get an id, if we don't already have one */
541 if( p_dialog->i_id == 0 )
542 p_dialog->i_id = ++p_interaction->i_last_id;
544 if( p_this->i_flags & OBJECT_FLAGS_NOINTERACT ) return VLC_EGENERIC;
546 if( config_GetInt(p_this, "interact") ||
547 p_dialog->i_flags & DIALOG_BLOCKING_ERROR ||
548 p_dialog->i_flags & DIALOG_NONBLOCKING_ERROR )
550 vlc_bool_t b_found = VLC_FALSE;
552 p_dialog->p_interaction = p_interaction;
553 p_dialog->p_parent = p_this;
555 /* Check if we have already added this dialog */
556 vlc_mutex_lock( &p_interaction->object_lock );
557 for( i = 0 ; i< p_interaction->i_dialogs; i++ )
559 if( p_interaction->pp_dialogs[i]->i_id == p_dialog->i_id )
562 /* Add it to the queue, the main loop will send the orders to the
566 INSERT_ELEM( p_interaction->pp_dialogs,
567 p_interaction->i_dialogs,
568 p_interaction->i_dialogs,
572 p_dialog->i_status = UPDATED_DIALOG;
574 if( p_dialog->i_type == INTERACT_DIALOG_TWOWAY ) // Wait for answer
576 while( p_dialog->i_status != ANSWERED_DIALOG &&
577 p_dialog->i_status != HIDING_DIALOG &&
578 p_dialog->i_status != HIDDEN_DIALOG &&
579 !p_dialog->p_parent->b_die )
581 vlc_mutex_unlock( &p_interaction->object_lock );
583 vlc_mutex_lock( &p_interaction->object_lock );
585 if( p_dialog->p_parent->b_die )
587 p_dialog->i_return = DIALOG_CANCELLED;
588 p_dialog->i_status = ANSWERED_DIALOG;
590 p_dialog->i_flags |= DIALOG_GOT_ANSWER;
591 vlc_mutex_unlock( &p_interaction->object_lock );
592 return p_dialog->i_return;
596 // Pretend we already retrieved the "answer"
597 p_dialog->i_flags |= DIALOG_GOT_ANSWER;
598 vlc_mutex_unlock( &p_interaction->object_lock );