1 /*****************************************************************************
2 * interaction.c: User interaction functions
3 *****************************************************************************
4 * Copyright (C) 2005-2006 VideoLAN
7 * Authors: Clément Stenac <zorglub@videolan.org>
9 * This program is free software; you can redistribute it and/or modify
10 * it under the terms of the GNU General Public License as published by
11 * the Free Software Foundation; either version 2 of the License, or
12 * (at your option) any later version.
14 * This program is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
17 * GNU General Public License for more details.
19 * You should have received a copy of the GNU General Public License
20 * along with this program; if not, write to the Free Software
21 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
26 * This file contains functions related to user interaction management
29 /*****************************************************************************
31 *****************************************************************************/
32 #include <stdlib.h> /* free(), strtol() */
33 #include <stdio.h> /* FILE */
34 #include <string.h> /* strerror() */
37 #include <vlc/input.h>
39 #include "vlc_interaction.h"
40 #include "vlc_interface.h"
41 #include "vlc_playlist.h"
43 /*****************************************************************************
45 *****************************************************************************/
46 static void intf_InteractionInit( playlist_t *p_playlist );
47 static interaction_t * intf_InteractionGet( vlc_object_t *p_this );
48 static void intf_InteractionSearchInterface( interaction_t *
50 static int intf_WaitAnswer( interaction_t *p_interact,
51 interaction_dialog_t *p_dialog );
52 static int intf_Send( interaction_t *p_interact,
53 interaction_dialog_t *p_dialog );
54 static interaction_dialog_t *intf_InteractionGetById( vlc_object_t* , int );
55 static void intf_InteractionDialogDestroy(
56 interaction_dialog_t *p_dialog );
59 * Send an interaction element to the user
61 * \param p_this the calling vlc_object_t
62 * \param p_interact the interaction element
63 * \return VLC_SUCCESS or an error code
65 int __intf_Interact( vlc_object_t *p_this, interaction_dialog_t *
68 interaction_t *p_interaction = intf_InteractionGet( p_this );
70 /* Get an id, if we don't already have one */
71 if( p_dialog->i_id == 0 )
73 p_dialog->i_id = ++p_interaction->i_last_id;
76 if( p_this->i_flags & OBJECT_FLAGS_NOINTERACT )
81 p_dialog->p_interaction = p_interaction;
82 p_dialog->p_parent = p_this;
84 if( p_dialog->i_type == INTERACT_DIALOG_TWOWAY )
86 return intf_WaitAnswer( p_interaction, p_dialog );
90 p_dialog->i_flags |= DIALOG_GOT_ANSWER;
91 return intf_Send( p_interaction, p_dialog );
96 * Destroy the interaction system
97 * \param The interaction object to destroy
100 void intf_InteractionDestroy( interaction_t *p_interaction )
104 // Remove all dialogs - Interfaces must be able to clean up their data
106 for( i = p_interaction->i_dialogs -1 ; i >= 0; i-- )
108 interaction_dialog_t * p_dialog = p_interaction->pp_dialogs[i];
109 intf_InteractionDialogDestroy( p_dialog );
110 REMOVE_ELEM( p_interaction->pp_dialogs, p_interaction->i_dialogs, i );
113 vlc_object_destroy( p_interaction );
117 * The main interaction processing loop
118 * This function is called from the playlist loop
120 * \param p_playlist the parent playlist
123 void intf_InteractionManage( playlist_t *p_playlist )
127 interaction_t *p_interaction;
129 p_interaction = p_playlist->p_interaction;
132 if( p_interaction->i_dialogs == 0 ) return;
134 vlc_mutex_lock( &p_interaction->object_lock );
136 intf_InteractionSearchInterface( p_interaction );
138 if( !p_interaction->p_intf )
140 // We mark all dialogs as answered with their "default" answer
141 for( i_index = 0 ; i_index < p_interaction->i_dialogs; i_index ++ )
143 interaction_dialog_t *p_dialog = p_interaction->pp_dialogs[i_index];
145 // Give default answer
146 p_dialog->i_return = DIALOG_DEFAULT;
147 if( p_dialog->i_flags & DIALOG_OK_CANCEL )
148 p_dialog->i_return = DIALOG_CANCELLED;
150 // Pretend we have hidden and destroyed it
151 if( p_dialog->i_status == HIDDEN_DIALOG )
153 p_dialog->i_status = DESTROYED_DIALOG;
157 p_dialog->i_status = HIDING_DIALOG;
163 vlc_object_yield( p_interaction->p_intf );
166 for( i_index = 0 ; i_index < p_interaction->i_dialogs; i_index ++ )
168 interaction_dialog_t *p_dialog = p_interaction->pp_dialogs[i_index];
169 switch( p_dialog->i_status )
171 case ANSWERED_DIALOG:
172 // Ask interface to hide it
173 p_dialog->i_action = INTERACT_HIDE;
174 val.p_address = p_dialog;
175 if( p_interaction->p_intf )
176 var_Set( p_interaction->p_intf, "interaction", val );
177 p_dialog->i_status = HIDING_DIALOG;
180 p_dialog->i_action = INTERACT_UPDATE;
181 val.p_address = p_dialog;
182 if( p_interaction->p_intf )
183 var_Set( p_interaction->p_intf, "interaction", val );
184 p_dialog->i_status = SENT_DIALOG;
187 if( !(p_dialog->i_flags & DIALOG_GOT_ANSWER) ) break;
188 if( !(p_dialog->i_flags & DIALOG_REUSABLE) )
190 p_dialog->i_action = INTERACT_DESTROY;
191 val.p_address = p_dialog;
192 if( p_interaction->p_intf )
193 var_Set( p_interaction->p_intf, "interaction", val );
196 case DESTROYED_DIALOG:
197 // Interface has now destroyed it, remove it
198 REMOVE_ELEM( p_interaction->pp_dialogs, p_interaction->i_dialogs,
201 intf_InteractionDialogDestroy( p_dialog );
204 // This is truly a new dialog, send it.
205 p_dialog->i_action = INTERACT_NEW;
206 val.p_address = p_dialog;
207 if( p_interaction->p_intf )
208 var_Set( p_interaction->p_intf, "interaction", val );
209 p_dialog->i_status = SENT_DIALOG;
214 if( p_interaction->p_intf )
216 vlc_object_release( p_interaction->p_intf );
219 vlc_mutex_unlock( &p_playlist->p_interaction->object_lock );
224 #define INTERACT_INIT( new ) \
225 new = (interaction_dialog_t*)malloc( \
226 sizeof( interaction_dialog_t ) ); \
227 new->i_widgets = 0; \
228 new->pp_widgets = NULL; \
229 new->psz_title = NULL; \
230 new->psz_description = NULL; \
233 new->i_status = NEW_DIALOG;
235 #define INTERACT_FREE( new ) \
236 if( new->psz_title ) free( new->psz_title ); \
237 if( new->psz_description ) free( new->psz_description );
239 /** Helper function to send an error message
240 * \param p_this Parent vlc_object
241 * \param i_id A predefined ID, 0 if not applicable
242 * \param psz_title Title for the dialog
243 * \param psz_format The message to display
245 void __intf_UserFatal( vlc_object_t *p_this,
246 const char *psz_title,
247 const char *psz_format, ... )
250 interaction_dialog_t *p_new = NULL;
251 user_widget_t *p_widget = NULL;
252 int i_id = DIALOG_ERRORS;
256 p_new = intf_InteractionGetById( p_this, i_id );
260 INTERACT_INIT( p_new );
261 if( i_id > 0 ) p_new->i_id = i_id ;
265 p_new->i_status = UPDATED_DIALOG;
268 p_new->i_flags |= DIALOG_REUSABLE;
270 p_new->i_type = INTERACT_DIALOG_ONEWAY;
271 p_new->psz_title = strdup( psz_title );
273 p_widget = (user_widget_t* )malloc( sizeof( user_widget_t ) );
275 p_widget->i_type = WIDGET_TEXT;
276 p_widget->val.psz_string = NULL;
278 va_start( args, psz_format );
279 vasprintf( &p_widget->psz_text, psz_format, args );
282 INSERT_ELEM ( p_new->pp_widgets,
287 p_new->i_flags |= DIALOG_CLEAR_NOSHOW;
289 intf_Interact( p_this, p_new );
292 /** Helper function to ask a yes-no question
293 * \param p_this Parent vlc_object
294 * \param psz_title Title for the dialog
295 * \param psz_description A description
296 * \return Clicked button code
298 int __intf_UserYesNo( vlc_object_t *p_this,
299 const char *psz_title,
300 const char *psz_description )
303 interaction_dialog_t *p_new = NULL;
304 user_widget_t *p_widget = NULL;
306 INTERACT_INIT( p_new );
308 p_new->i_type = INTERACT_DIALOG_TWOWAY;
309 p_new->psz_title = strdup( psz_title );
312 p_widget = (user_widget_t* )malloc( sizeof( user_widget_t ) );
313 p_widget->i_type = WIDGET_TEXT;
314 p_widget->psz_text = strdup( psz_description );
315 p_widget->val.psz_string = NULL;
316 INSERT_ELEM ( p_new->pp_widgets, p_new->i_widgets,
317 p_new->i_widgets, p_widget );
319 p_new->i_flags = DIALOG_YES_NO_CANCEL;
321 i_ret = intf_Interact( p_this, p_new );
326 /** Helper function to make a progressbar box
327 * \param p_this Parent vlc_object
328 * \param psz_title Title for the dialog
329 * \param psz_status Current status
330 * \param f_position Current position (0.0->100.0)
331 * \return Dialog id, to give to UserProgressUpdate
333 int __intf_UserProgress( vlc_object_t *p_this,
334 const char *psz_title,
335 const char *psz_status,
339 interaction_dialog_t *p_new = NULL;
340 user_widget_t *p_widget = NULL;
342 INTERACT_INIT( p_new );
344 p_new->i_type = INTERACT_DIALOG_ONEWAY;
345 p_new->psz_title = strdup( psz_title );
348 p_widget = (user_widget_t* )malloc( sizeof( user_widget_t ) );
349 p_widget->i_type = WIDGET_PROGRESS;
350 p_widget->psz_text = strdup( psz_status );
351 p_widget->val.f_float = f_pos;
352 INSERT_ELEM ( p_new->pp_widgets, p_new->i_widgets,
353 p_new->i_widgets, p_widget );
355 i_ret = intf_Interact( p_this, p_new );
360 /** Update a progress bar
361 * \param p_this Parent vlc_object
362 * \param i_id Identifier of the dialog
363 * \param psz_status New status
364 * \param f_position New position (0.0->100.0)
367 void __intf_UserProgressUpdate( vlc_object_t *p_this, int i_id,
368 const char *psz_status, float f_pos )
370 interaction_t *p_interaction = intf_InteractionGet( p_this );
371 interaction_dialog_t *p_dialog;
373 if( !p_interaction ) return;
375 vlc_mutex_lock( &p_interaction->object_lock );
376 p_dialog = intf_InteractionGetById( p_this, i_id );
380 vlc_mutex_unlock( &p_interaction->object_lock ) ;
384 if( p_dialog->pp_widgets[0]->psz_text )
385 free( p_dialog->pp_widgets[0]->psz_text );
386 p_dialog->pp_widgets[0]->psz_text = strdup( psz_status );
388 p_dialog->pp_widgets[0]->val.f_float = f_pos;
390 p_dialog->i_status = UPDATED_DIALOG;
391 vlc_mutex_unlock( &p_interaction->object_lock) ;
394 /** Helper function to make a login/password box
395 * \param p_this Parent vlc_object
396 * \param psz_title Title for the dialog
397 * \param psz_description A description
398 * \param ppsz_login Returned login
399 * \param ppsz_password Returned password
400 * \return Clicked button code
402 int __intf_UserLoginPassword( vlc_object_t *p_this,
403 const char *psz_title,
404 const char *psz_description,
406 char **ppsz_password )
409 interaction_dialog_t *p_new = NULL;
410 user_widget_t *p_widget = NULL;
412 INTERACT_INIT( p_new );
414 p_new->i_type = INTERACT_DIALOG_TWOWAY;
415 p_new->psz_title = strdup( psz_title );
418 p_widget = (user_widget_t* )malloc( sizeof( user_widget_t ) );
419 p_widget->i_type = WIDGET_TEXT;
420 p_widget->psz_text = strdup( psz_description );
421 p_widget->val.psz_string = NULL;
422 INSERT_ELEM ( p_new->pp_widgets, p_new->i_widgets,
423 p_new->i_widgets, p_widget );
426 p_widget = (user_widget_t* )malloc( sizeof( user_widget_t ) );
427 p_widget->i_type = WIDGET_INPUT_TEXT;
428 p_widget->psz_text = strdup( _("Login") );
429 p_widget->val.psz_string = NULL;
430 INSERT_ELEM ( p_new->pp_widgets, p_new->i_widgets,
431 p_new->i_widgets, p_widget );
434 p_widget = (user_widget_t* )malloc( sizeof( user_widget_t ) );
435 p_widget->i_type = WIDGET_INPUT_TEXT;
436 p_widget->psz_text = strdup( _("Password") );
437 p_widget->val.psz_string = NULL;
438 INSERT_ELEM ( p_new->pp_widgets, p_new->i_widgets,
439 p_new->i_widgets, p_widget );
441 p_new->i_flags = DIALOG_OK_CANCEL;
443 i_ret = intf_Interact( p_this, p_new );
445 if( i_ret != DIALOG_CANCELLED )
447 *ppsz_login = strdup( p_new->pp_widgets[1]->val.psz_string );
448 *ppsz_password = strdup( p_new->pp_widgets[2]->val.psz_string );
453 /** Hide an interaction dialog
454 * \param p_this the parent vlc object
455 * \param i_id the id of the item to hide
458 void __intf_UserHide( vlc_object_t *p_this, int i_id )
460 interaction_t *p_interaction = intf_InteractionGet( p_this );
461 interaction_dialog_t *p_dialog;
463 if( !p_interaction ) return;
465 vlc_mutex_lock( &p_interaction->object_lock );
466 p_dialog = intf_InteractionGetById( p_this, i_id );
470 vlc_mutex_unlock( &p_interaction->object_lock );
474 p_dialog->i_status = ANSWERED_DIALOG;
475 vlc_mutex_unlock( &p_interaction->object_lock );
480 /**********************************************************************
481 * The following functions are local
482 **********************************************************************/
484 /* Get the interaction object. Create it if needed */
485 static interaction_t * intf_InteractionGet( vlc_object_t *p_this )
487 playlist_t *p_playlist;
488 interaction_t *p_interaction;
490 p_playlist = (playlist_t*) vlc_object_find( p_this, VLC_OBJECT_PLAYLIST,
498 if( p_playlist->p_interaction == NULL )
500 intf_InteractionInit( p_playlist );
503 p_interaction = p_playlist->p_interaction;
505 vlc_object_release( p_playlist );
507 return p_interaction;
510 /* Create the interaction object in the given playlist object */
511 static void intf_InteractionInit( playlist_t *p_playlist )
513 interaction_t *p_interaction;
515 msg_Dbg( p_playlist, "initializing interaction system" );
517 p_interaction = vlc_object_create( VLC_OBJECT( p_playlist ),
518 sizeof( interaction_t ) );
521 msg_Err( p_playlist,"out of memory" );
525 p_interaction->i_dialogs = 0;
526 p_interaction->pp_dialogs = NULL;
527 p_interaction->p_intf = NULL;
528 p_interaction->i_last_id = DIALOG_LAST_PREDEFINED + 1;
530 vlc_mutex_init( p_interaction , &p_interaction->object_lock );
532 p_playlist->p_interaction = p_interaction;
535 /* Look for an interface suitable for interaction */
536 static void intf_InteractionSearchInterface( interaction_t *p_interaction )
541 p_interaction->p_intf = NULL;
543 p_list = vlc_list_find( p_interaction, VLC_OBJECT_INTF, FIND_ANYWHERE );
546 msg_Err( p_interaction, "Unable to create module list" );
550 for( i_index = 0; i_index < p_list->i_count; i_index ++ )
552 intf_thread_t *p_intf = (intf_thread_t *)
553 p_list->p_values[i_index].p_object;
554 if( p_intf->b_interaction )
556 p_interaction->p_intf = p_intf;
560 vlc_list_release ( p_list );
563 /* Add a dialog to the queue and wait for answer */
564 static int intf_WaitAnswer( interaction_t *p_interact,
565 interaction_dialog_t *p_dialog )
568 vlc_bool_t b_found = VLC_FALSE;
569 vlc_mutex_lock( &p_interact->object_lock );
570 for( i = 0 ; i< p_interact->i_dialogs; i++ )
572 if( p_interact->pp_dialogs[i]->i_id == p_dialog->i_id )
579 INSERT_ELEM( p_interact->pp_dialogs,
580 p_interact->i_dialogs,
581 p_interact->i_dialogs,
585 p_dialog->i_status = UPDATED_DIALOG;
586 vlc_mutex_unlock( &p_interact->object_lock );
588 /// \todo Check that the initiating object is not dying
589 while( p_dialog->i_status != ANSWERED_DIALOG &&
590 p_dialog->i_status != HIDING_DIALOG &&
591 p_dialog->i_status != HIDDEN_DIALOG &&
592 !p_dialog->p_parent->b_die )
597 if( p_dialog->p_parent->b_die )
599 p_dialog->i_return = DIALOG_CANCELLED;
600 p_dialog->i_status = ANSWERED_DIALOG;
602 p_dialog->i_flags |= DIALOG_GOT_ANSWER;
603 return p_dialog->i_return;
606 /* Add a dialog to the queue and return */
607 static int intf_Send( interaction_t *p_interact,
608 interaction_dialog_t *p_dialog )
611 vlc_bool_t b_found = VLC_FALSE;
612 if( p_interact == NULL ) return VLC_ENOOBJ;
613 vlc_mutex_lock( &p_interact->object_lock );
615 for( i = 0 ; i< p_interact->i_dialogs; i++ )
617 if( p_interact->pp_dialogs[i]->i_id == p_dialog->i_id )
624 INSERT_ELEM( p_interact->pp_dialogs,
625 p_interact->i_dialogs,
626 p_interact->i_dialogs,
630 p_dialog->i_status = UPDATED_DIALOG;
631 // Pretend we already retrieved the "answer"
632 p_dialog->i_flags |= DIALOG_GOT_ANSWER;
633 vlc_mutex_unlock( &p_interact->object_lock );
637 /* Find an interaction dialog by its id */
638 static interaction_dialog_t *intf_InteractionGetById( vlc_object_t* p_this,
641 interaction_t *p_interaction = intf_InteractionGet( p_this );
644 if( !p_interaction ) return NULL;
646 for( i = 0 ; i< p_interaction->i_dialogs; i++ )
648 if( p_interaction->pp_dialogs[i]->i_id == i_id )
650 return p_interaction->pp_dialogs[i];
656 #define FREE( i ) { if( i ) free( i ); i = NULL; }
658 static void intf_InteractionDialogDestroy( interaction_dialog_t *p_dialog )
661 for( i = p_dialog->i_widgets - 1 ; i >= 0 ; i-- )
663 user_widget_t *p_widget = p_dialog->pp_widgets[i];
664 FREE( p_widget->psz_text );
665 if( p_widget->i_type == WIDGET_INPUT_TEXT )
667 FREE( p_widget->val.psz_string );
670 REMOVE_ELEM( p_dialog->pp_widgets, p_dialog->i_widgets, i );
673 FREE( p_dialog->psz_title );
674 FREE( p_dialog->psz_description );