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 );
57 * Send an interaction element to the user
59 * \param p_this the calling vlc_object_t
60 * \param p_interact the interaction element
61 * \return VLC_SUCCESS or an error code
63 int __intf_Interact( vlc_object_t *p_this, interaction_dialog_t *
66 interaction_t *p_interaction = intf_InteractionGet( p_this );
68 /* Get an id, if we don't already have one */
69 if( p_dialog->i_id == 0 )
71 p_dialog->i_id = ++p_interaction->i_last_id;
74 p_dialog->p_interaction = p_interaction;
75 p_dialog->p_parent = p_this;
77 if( p_dialog->i_type == INTERACT_DIALOG_TWOWAY )
79 return intf_WaitAnswer( p_interaction, p_dialog );
83 p_dialog->i_flags |= DIALOG_GOT_ANSWER;
84 return intf_Send( p_interaction, p_dialog );
89 * Destroy the interaction system
91 void intf_InteractionDestroy( interaction_t *p_interaction )
93 /// \todo Code this, and call it
97 * The main interaction processing loop
98 * This function is called from the playlist loop
100 * \param p_playlist the parent playlist
103 void intf_InteractionManage( playlist_t *p_playlist )
107 interaction_t *p_interaction;
109 p_interaction = p_playlist->p_interaction;
112 if( p_interaction->i_dialogs == 0 ) return;
114 vlc_mutex_lock( &p_interaction->object_lock );
116 intf_InteractionSearchInterface( p_interaction );
118 if( !p_interaction->p_intf )
120 // We mark all dialogs as answered with their "default" answer
121 for( i_index = 0 ; i_index < p_interaction->i_dialogs; i_index ++ )
123 interaction_dialog_t *p_dialog = p_interaction->pp_dialogs[i_index];
124 p_dialog->i_return = DIALOG_DEFAULT;
125 if( p_dialog->i_flags & DIALOG_OK_CANCEL )
126 p_dialog->i_return = DIALOG_CANCELLED;
127 if( p_dialog->i_flags & DIALOG_YES_NO_CANCEL )
128 p_dialog->i_return = DIALOG_CANCELLED;
129 p_dialog->i_status = ANSWERED_DIALOG;
134 vlc_object_yield( p_interaction->p_intf );
137 for( i_index = 0 ; i_index < p_interaction->i_dialogs; i_index ++ )
139 interaction_dialog_t *p_dialog = p_interaction->pp_dialogs[i_index];
140 switch( p_dialog->i_status )
142 case ANSWERED_DIALOG:
143 // Ask interface to hide it
144 msg_Dbg( p_interaction, "Hiding dialog %i", p_dialog->i_id );
145 p_dialog->i_action = INTERACT_HIDE;
146 val.p_address = p_dialog;
147 if( p_interaction->p_intf )
148 var_Set( p_interaction->p_intf, "interaction", val );
149 p_dialog->i_status = HIDING_DIALOG;
152 p_dialog->i_action = INTERACT_UPDATE;
153 val.p_address = p_dialog;
154 if( p_interaction->p_intf )
155 var_Set( p_interaction->p_intf, "interaction", val );
156 p_dialog->i_status = SENT_DIALOG;
157 msg_Dbg( p_interaction, "Updating dialog %i, %i widgets",
158 p_dialog->i_id, p_dialog->i_widgets );
161 if( !(p_dialog->i_flags & DIALOG_GOT_ANSWER) ) break;
162 if( !(p_dialog->i_flags & DIALOG_REUSABLE) )
164 msg_Dbg( p_interaction, "Destroying dialog %i",
166 p_dialog->i_action = INTERACT_DESTROY;
167 val.p_address = p_dialog;
168 if( p_interaction->p_intf )
169 var_Set( p_interaction->p_intf, "interaction", val );
172 case DESTROYED_DIALOG:
173 // Interface has now destroyed it, remove it
174 /// \todo Remove it from the list
175 /// \todo Free data fields
178 // This is truly a new dialog, send it.
179 p_dialog->i_action = INTERACT_NEW;
180 val.p_address = p_dialog;
181 if( p_interaction->p_intf )
182 var_Set( p_interaction->p_intf, "interaction", val );
183 p_dialog->i_status = SENT_DIALOG;
188 if( p_interaction->p_intf )
190 vlc_object_release( p_interaction->p_intf );
193 vlc_mutex_unlock( &p_playlist->p_interaction->object_lock );
198 #define INTERACT_INIT( new ) \
199 new = (interaction_dialog_t*)malloc( \
200 sizeof( interaction_dialog_t ) ); \
201 new->i_widgets = 0; \
202 new->pp_widgets = NULL; \
203 new->psz_title = NULL; \
204 new->psz_description = NULL; \
206 new->i_status = NEW_DIALOG;
208 #define INTERACT_FREE( new ) \
209 if( new->psz_title ) free( new->psz_title ); \
210 if( new->psz_description ) free( new->psz_description );
212 /** Helper function to send an error message
213 * \param p_this Parent vlc_object
214 * \param i_id A predefined ID, 0 if not applicable
215 * \param psz_title Title for the dialog
216 * \param psz_format The message to display
218 void __intf_UserFatal( vlc_object_t *p_this,
219 const char *psz_title,
220 const char *psz_format, ... )
223 interaction_dialog_t *p_new = NULL;
224 user_widget_t *p_widget = NULL;
225 int i_id = DIALOG_ERRORS;
229 p_new = intf_InteractionGetById( p_this, i_id );
233 INTERACT_INIT( p_new );
234 if( i_id > 0 ) p_new->i_id = i_id ;
238 p_new->i_status = UPDATED_DIALOG;
241 p_new->i_flags |= DIALOG_REUSABLE;
243 p_new->i_type = INTERACT_DIALOG_ONEWAY;
244 p_new->psz_title = strdup( psz_title );
246 p_widget = (user_widget_t* )malloc( sizeof( user_widget_t ) );
248 p_widget->i_type = WIDGET_TEXT;
250 va_start( args, psz_format );
251 vasprintf( &p_widget->psz_text, psz_format, args );
254 INSERT_ELEM ( p_new->pp_widgets,
259 intf_Interact( p_this, p_new );
262 /** Helper function to make a login/password box
263 * \param p_this Parent vlc_object
264 * \param psz_title Title for the dialog
265 * \param psz_description A description
266 * \param ppsz_login Returned login
267 * \param ppsz_password Returned password
268 * \return 1 if user clicked Cancel, 0 if OK
270 int __intf_UserLoginPassword( vlc_object_t *p_this,
271 const char *psz_title,
272 const char *psz_description,
274 char **ppsz_password )
277 interaction_dialog_t *p_new = NULL;
278 user_widget_t *p_widget = NULL;
280 INTERACT_INIT( p_new );
282 p_new->i_type = INTERACT_DIALOG_TWOWAY;
283 p_new->psz_title = strdup( psz_title );
286 p_widget = (user_widget_t* )malloc( sizeof( user_widget_t ) );
287 p_widget->i_type = WIDGET_TEXT;
288 p_widget->psz_text = strdup( psz_description );
289 INSERT_ELEM ( p_new->pp_widgets, p_new->i_widgets,
290 p_new->i_widgets, p_widget );
293 p_widget = (user_widget_t* )malloc( sizeof( user_widget_t ) );
294 p_widget->i_type = WIDGET_INPUT_TEXT;
295 p_widget->psz_text = strdup( _("Login") );
296 p_widget->val.psz_string = NULL;
297 INSERT_ELEM ( p_new->pp_widgets, p_new->i_widgets,
298 p_new->i_widgets, p_widget );
301 p_widget = (user_widget_t* )malloc( sizeof( user_widget_t ) );
302 p_widget->i_type = WIDGET_INPUT_TEXT;
303 p_widget->psz_text = strdup( _("Password") );
304 p_widget->val.psz_string = NULL;
305 INSERT_ELEM ( p_new->pp_widgets, p_new->i_widgets,
306 p_new->i_widgets, p_widget );
308 p_new->i_flags = DIALOG_OK_CANCEL;
310 i_ret = intf_Interact( p_this, p_new );
312 if( i_ret == DIALOG_OK_YES )
314 *ppsz_login = strdup( p_new->pp_widgets[1]->val.psz_string );
315 *ppsz_password = strdup( p_new->pp_widgets[2]->val.psz_string );
320 /**********************************************************************
321 * The following functions are local
322 **********************************************************************/
324 /* Get the interaction object. Create it if needed */
325 static interaction_t * intf_InteractionGet( vlc_object_t *p_this )
327 playlist_t *p_playlist;
328 interaction_t *p_interaction;
330 p_playlist = (playlist_t*) vlc_object_find( p_this, VLC_OBJECT_PLAYLIST,
338 if( p_playlist->p_interaction == NULL )
340 intf_InteractionInit( p_playlist );
343 p_interaction = p_playlist->p_interaction;
345 vlc_object_release( p_playlist );
347 return p_interaction;
350 /* Create the interaction object in the given playlist object */
351 static void intf_InteractionInit( playlist_t *p_playlist )
353 interaction_t *p_interaction;
355 msg_Dbg( p_playlist, "initializing interaction system" );
357 p_interaction = vlc_object_create( VLC_OBJECT( p_playlist ),
358 sizeof( interaction_t ) );
361 msg_Err( p_playlist,"out of memory" );
365 p_interaction->i_dialogs = 0;
366 p_interaction->pp_dialogs = NULL;
367 p_interaction->p_intf = NULL;
368 p_interaction->i_last_id = DIALOG_LAST_PREDEFINED + 1;
370 vlc_mutex_init( p_interaction , &p_interaction->object_lock );
372 p_playlist->p_interaction = p_interaction;
375 /* Look for an interface suitable for interaction */
376 static void intf_InteractionSearchInterface( interaction_t *p_interaction )
381 p_interaction->p_intf = NULL;
383 p_list = vlc_list_find( p_interaction, VLC_OBJECT_INTF, FIND_ANYWHERE );
386 msg_Err( p_interaction, "Unable to create module list" );
390 for( i_index = 0; i_index < p_list->i_count; i_index ++ )
392 intf_thread_t *p_intf = (intf_thread_t *)
393 p_list->p_values[i_index].p_object;
394 if( p_intf->b_interaction )
396 p_interaction->p_intf = p_intf;
400 vlc_list_release ( p_list );
403 /* Add a dialog to the queue and wait for answer */
404 static int intf_WaitAnswer( interaction_t *p_interact, interaction_dialog_t *p_dialog )
407 vlc_bool_t b_found = VLC_FALSE;
408 vlc_mutex_lock( &p_interact->object_lock );
409 for( i = 0 ; i< p_interact->i_dialogs; i++ )
411 if( p_interact->pp_dialogs[i]->i_id == p_dialog->i_id )
418 INSERT_ELEM( p_interact->pp_dialogs,
419 p_interact->i_dialogs,
420 p_interact->i_dialogs,
424 p_dialog->i_status = UPDATED_DIALOG;
425 vlc_mutex_unlock( &p_interact->object_lock );
427 /// \todo Check that the initiating object is not dying
428 while( p_dialog->i_status != ANSWERED_DIALOG &&
429 p_dialog->i_status != HIDING_DIALOG &&
430 !p_dialog->p_parent->b_die )
435 if( p_dialog->p_parent->b_die )
437 p_dialog->i_return = DIALOG_CANCELLED;
438 p_dialog->i_status = ANSWERED_DIALOG;
440 p_dialog->i_flags |= DIALOG_GOT_ANSWER;
441 return p_dialog->i_return;
444 /* Add a dialog to the queue and return */
445 static int intf_Send( interaction_t *p_interact, interaction_dialog_t *p_dialog )
448 vlc_bool_t b_found = VLC_FALSE;
449 vlc_mutex_lock( &p_interact->object_lock );
451 for( i = 0 ; i< p_interact->i_dialogs; i++ )
453 if( p_interact->pp_dialogs[i]->i_id == p_dialog->i_id )
460 INSERT_ELEM( p_interact->pp_dialogs,
461 p_interact->i_dialogs,
462 p_interact->i_dialogs,
466 p_dialog->i_status = UPDATED_DIALOG;
467 vlc_mutex_unlock( &p_interact->object_lock );
471 /* Find an interaction dialog by its id */
472 static interaction_dialog_t *intf_InteractionGetById( vlc_object_t* p_this,
475 interaction_t *p_interaction = intf_InteractionGet( p_this );
478 if( !p_interaction ) return NULL;
480 for( i = 0 ; i< p_interaction->i_dialogs; i++ )
482 if( p_interaction->pp_dialogs[i]->i_id == i_id )
484 return p_interaction->pp_dialogs[i];