1 /*****************************************************************************
2 * interaction.c: User interaction functions
3 *****************************************************************************
4 * Copyright (C) 1998-2004 VideoLAN
5 * $Id: interface.c 10147 2005-03-05 17:18:30Z gbazin $
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., 59 Temple Place - Suite 330, Boston, MA 02111, 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 p_dialog->p_interaction = p_interaction;
77 p_dialog->p_parent = p_this;
79 if( p_dialog->i_type == INTERACT_DIALOG_TWOWAY )
81 return intf_WaitAnswer( p_interaction, p_dialog );
85 p_dialog->i_flags |= DIALOG_GOT_ANSWER;
86 return intf_Send( p_interaction, p_dialog );
91 * Destroy the interaction system
93 void intf_InteractionDestroy( interaction_t *p_interaction )
97 // Remove all dialogs - Interfaces must be able to clean up their data
99 for( i = p_interaction->i_dialogs -1 ; i >= 0; i-- )
101 interaction_dialog_t * p_dialog = p_interaction->pp_dialogs[i];
102 intf_InteractionDialogDestroy( p_dialog );
103 REMOVE_ELEM( p_interaction->pp_dialogs, p_interaction->i_dialogs, i );
106 vlc_object_destroy( p_interaction );
110 * The main interaction processing loop
111 * This function is called from the playlist loop
113 * \param p_playlist the parent playlist
116 void intf_InteractionManage( playlist_t *p_playlist )
120 interaction_t *p_interaction;
122 p_interaction = p_playlist->p_interaction;
125 if( p_interaction->i_dialogs == 0 ) return;
127 vlc_mutex_lock( &p_interaction->object_lock );
129 intf_InteractionSearchInterface( p_interaction );
131 if( !p_interaction->p_intf )
133 // We mark all dialogs as answered with their "default" answer
134 for( i_index = 0 ; i_index < p_interaction->i_dialogs; i_index ++ )
136 interaction_dialog_t *p_dialog = p_interaction->pp_dialogs[i_index];
138 // Give default answer
139 p_dialog->i_return = DIALOG_DEFAULT;
140 if( p_dialog->i_flags & DIALOG_OK_CANCEL )
141 p_dialog->i_return = DIALOG_CANCELLED;
142 if( p_dialog->i_flags & DIALOG_YES_NO_CANCEL )
143 p_dialog->i_return = DIALOG_CANCELLED;
145 // Pretend we have hidden and destroyed it
146 if( p_dialog->i_status == HIDDEN_DIALOG )
148 p_dialog->i_status = DESTROYED_DIALOG;
152 p_dialog->i_status = HIDING_DIALOG;
158 vlc_object_yield( p_interaction->p_intf );
161 for( i_index = 0 ; i_index < p_interaction->i_dialogs; i_index ++ )
163 interaction_dialog_t *p_dialog = p_interaction->pp_dialogs[i_index];
164 switch( p_dialog->i_status )
166 case ANSWERED_DIALOG:
167 // Ask interface to hide it
168 msg_Dbg( p_interaction, "Hiding dialog %i", p_dialog->i_id );
169 p_dialog->i_action = INTERACT_HIDE;
170 val.p_address = p_dialog;
171 if( p_interaction->p_intf )
172 var_Set( p_interaction->p_intf, "interaction", val );
173 p_dialog->i_status = HIDING_DIALOG;
176 p_dialog->i_action = INTERACT_UPDATE;
177 val.p_address = p_dialog;
178 if( p_interaction->p_intf )
179 var_Set( p_interaction->p_intf, "interaction", val );
180 p_dialog->i_status = SENT_DIALOG;
181 msg_Dbg( p_interaction, "Updating dialog %i, %i widgets",
182 p_dialog->i_id, p_dialog->i_widgets );
185 if( !(p_dialog->i_flags & DIALOG_GOT_ANSWER) ) break;
186 if( !(p_dialog->i_flags & DIALOG_REUSABLE) )
188 msg_Dbg( p_interaction, "Destroying dialog %i",
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 msg_Dbg( p_interaction, "Removing dialog %i",
199 // Interface has now destroyed it, remove it
200 REMOVE_ELEM( p_interaction->pp_dialogs, p_interaction->i_dialogs,
203 intf_InteractionDialogDestroy( p_dialog );
206 // This is truly a new dialog, send it.
207 p_dialog->i_action = INTERACT_NEW;
208 val.p_address = p_dialog;
209 if( p_interaction->p_intf )
210 var_Set( p_interaction->p_intf, "interaction", val );
211 p_dialog->i_status = SENT_DIALOG;
216 if( p_interaction->p_intf )
218 vlc_object_release( p_interaction->p_intf );
221 vlc_mutex_unlock( &p_playlist->p_interaction->object_lock );
226 #define INTERACT_INIT( new ) \
227 new = (interaction_dialog_t*)malloc( \
228 sizeof( interaction_dialog_t ) ); \
229 new->i_widgets = 0; \
230 new->pp_widgets = NULL; \
231 new->psz_title = NULL; \
232 new->psz_description = NULL; \
235 new->i_status = NEW_DIALOG;
237 #define INTERACT_FREE( new ) \
238 if( new->psz_title ) free( new->psz_title ); \
239 if( new->psz_description ) free( new->psz_description );
241 /** Helper function to send an error message
242 * \param p_this Parent vlc_object
243 * \param i_id A predefined ID, 0 if not applicable
244 * \param psz_title Title for the dialog
245 * \param psz_format The message to display
247 void __intf_UserFatal( vlc_object_t *p_this,
248 const char *psz_title,
249 const char *psz_format, ... )
252 interaction_dialog_t *p_new = NULL;
253 user_widget_t *p_widget = NULL;
254 int i_id = DIALOG_ERRORS;
258 p_new = intf_InteractionGetById( p_this, i_id );
262 INTERACT_INIT( p_new );
263 if( i_id > 0 ) p_new->i_id = i_id ;
267 p_new->i_status = UPDATED_DIALOG;
270 p_new->i_flags |= DIALOG_REUSABLE;
272 p_new->i_type = INTERACT_DIALOG_ONEWAY;
273 p_new->psz_title = strdup( psz_title );
275 p_widget = (user_widget_t* )malloc( sizeof( user_widget_t ) );
277 p_widget->i_type = WIDGET_TEXT;
278 p_widget->val.psz_string = NULL;
280 va_start( args, psz_format );
281 vasprintf( &p_widget->psz_text, psz_format, args );
284 INSERT_ELEM ( p_new->pp_widgets,
289 intf_Interact( p_this, p_new );
292 /** Helper function to make a login/password box
293 * \param p_this Parent vlc_object
294 * \param psz_title Title for the dialog
295 * \param psz_description A description
296 * \param ppsz_login Returned login
297 * \param ppsz_password Returned password
298 * \return 1 if user clicked Cancel, 0 if OK
300 int __intf_UserLoginPassword( vlc_object_t *p_this,
301 const char *psz_title,
302 const char *psz_description,
304 char **ppsz_password )
307 interaction_dialog_t *p_new = NULL;
308 user_widget_t *p_widget = NULL;
310 INTERACT_INIT( p_new );
312 p_new->i_type = INTERACT_DIALOG_TWOWAY;
313 p_new->psz_title = strdup( psz_title );
316 p_widget = (user_widget_t* )malloc( sizeof( user_widget_t ) );
317 p_widget->i_type = WIDGET_TEXT;
318 p_widget->psz_text = strdup( psz_description );
319 p_widget->val.psz_string = NULL;
320 INSERT_ELEM ( p_new->pp_widgets, p_new->i_widgets,
321 p_new->i_widgets, p_widget );
324 p_widget = (user_widget_t* )malloc( sizeof( user_widget_t ) );
325 p_widget->i_type = WIDGET_INPUT_TEXT;
326 p_widget->psz_text = strdup( _("Login") );
327 p_widget->val.psz_string = NULL;
328 INSERT_ELEM ( p_new->pp_widgets, p_new->i_widgets,
329 p_new->i_widgets, p_widget );
332 p_widget = (user_widget_t* )malloc( sizeof( user_widget_t ) );
333 p_widget->i_type = WIDGET_INPUT_TEXT;
334 p_widget->psz_text = strdup( _("Password") );
335 p_widget->val.psz_string = NULL;
336 INSERT_ELEM ( p_new->pp_widgets, p_new->i_widgets,
337 p_new->i_widgets, p_widget );
339 p_new->i_flags = DIALOG_OK_CANCEL;
341 i_ret = intf_Interact( p_this, p_new );
343 if( i_ret != DIALOG_CANCELLED )
345 *ppsz_login = strdup( p_new->pp_widgets[1]->val.psz_string );
346 *ppsz_password = strdup( p_new->pp_widgets[2]->val.psz_string );
351 /**********************************************************************
352 * The following functions are local
353 **********************************************************************/
355 /* Get the interaction object. Create it if needed */
356 static interaction_t * intf_InteractionGet( vlc_object_t *p_this )
358 playlist_t *p_playlist;
359 interaction_t *p_interaction;
361 p_playlist = (playlist_t*) vlc_object_find( p_this, VLC_OBJECT_PLAYLIST,
369 if( p_playlist->p_interaction == NULL )
371 intf_InteractionInit( p_playlist );
374 p_interaction = p_playlist->p_interaction;
376 vlc_object_release( p_playlist );
378 return p_interaction;
381 /* Create the interaction object in the given playlist object */
382 static void intf_InteractionInit( playlist_t *p_playlist )
384 interaction_t *p_interaction;
386 msg_Dbg( p_playlist, "initializing interaction system" );
388 p_interaction = vlc_object_create( VLC_OBJECT( p_playlist ),
389 sizeof( interaction_t ) );
392 msg_Err( p_playlist,"out of memory" );
396 p_interaction->i_dialogs = 0;
397 p_interaction->pp_dialogs = NULL;
398 p_interaction->p_intf = NULL;
399 p_interaction->i_last_id = DIALOG_LAST_PREDEFINED + 1;
401 vlc_mutex_init( p_interaction , &p_interaction->object_lock );
403 p_playlist->p_interaction = p_interaction;
406 /* Look for an interface suitable for interaction */
407 static void intf_InteractionSearchInterface( interaction_t *p_interaction )
412 p_interaction->p_intf = NULL;
414 p_list = vlc_list_find( p_interaction, VLC_OBJECT_INTF, FIND_ANYWHERE );
417 msg_Err( p_interaction, "Unable to create module list" );
421 for( i_index = 0; i_index < p_list->i_count; i_index ++ )
423 intf_thread_t *p_intf = (intf_thread_t *)
424 p_list->p_values[i_index].p_object;
425 if( p_intf->b_interaction )
427 p_interaction->p_intf = p_intf;
431 vlc_list_release ( p_list );
434 /* Add a dialog to the queue and wait for answer */
435 static int intf_WaitAnswer( interaction_t *p_interact, interaction_dialog_t *p_dialog )
438 vlc_bool_t b_found = VLC_FALSE;
439 vlc_mutex_lock( &p_interact->object_lock );
440 for( i = 0 ; i< p_interact->i_dialogs; i++ )
442 if( p_interact->pp_dialogs[i]->i_id == p_dialog->i_id )
449 INSERT_ELEM( p_interact->pp_dialogs,
450 p_interact->i_dialogs,
451 p_interact->i_dialogs,
455 p_dialog->i_status = UPDATED_DIALOG;
456 vlc_mutex_unlock( &p_interact->object_lock );
458 /// \todo Check that the initiating object is not dying
459 while( p_dialog->i_status != ANSWERED_DIALOG &&
460 p_dialog->i_status != HIDING_DIALOG &&
461 p_dialog->i_status != HIDDEN_DIALOG &&
462 !p_dialog->p_parent->b_die )
467 if( p_dialog->p_parent->b_die )
469 p_dialog->i_return = DIALOG_CANCELLED;
470 p_dialog->i_status = ANSWERED_DIALOG;
472 p_dialog->i_flags |= DIALOG_GOT_ANSWER;
473 return p_dialog->i_return;
476 /* Add a dialog to the queue and return */
477 static int intf_Send( interaction_t *p_interact, interaction_dialog_t *p_dialog )
480 vlc_bool_t b_found = VLC_FALSE;
481 vlc_mutex_lock( &p_interact->object_lock );
483 for( i = 0 ; i< p_interact->i_dialogs; i++ )
485 if( p_interact->pp_dialogs[i]->i_id == p_dialog->i_id )
492 INSERT_ELEM( p_interact->pp_dialogs,
493 p_interact->i_dialogs,
494 p_interact->i_dialogs,
498 p_dialog->i_status = UPDATED_DIALOG;
499 // Pretend we already retrieved the "answer"
500 p_dialog->i_flags |= DIALOG_GOT_ANSWER;
501 vlc_mutex_unlock( &p_interact->object_lock );
505 /* Find an interaction dialog by its id */
506 static interaction_dialog_t *intf_InteractionGetById( vlc_object_t* p_this,
509 interaction_t *p_interaction = intf_InteractionGet( p_this );
512 if( !p_interaction ) return NULL;
514 for( i = 0 ; i< p_interaction->i_dialogs; i++ )
516 if( p_interaction->pp_dialogs[i]->i_id == i_id )
518 return p_interaction->pp_dialogs[i];
524 #define FREE( i ) { if( i ) free( i ); i = NULL; }
526 static void intf_InteractionDialogDestroy( interaction_dialog_t *p_dialog )
529 for( i = p_dialog->i_widgets - 1 ; i >= 0 ; i-- )
531 user_widget_t *p_widget = p_dialog->pp_widgets[i];
532 FREE( p_widget->psz_text );
533 FREE( p_widget->val.psz_string );
535 REMOVE_ELEM( p_dialog->pp_widgets, p_dialog->i_widgets, i );
538 FREE( p_dialog->psz_title );
539 FREE( p_dialog->psz_description );