1 /*****************************************************************************
2 * interaction.c: User interaction functions
3 *****************************************************************************
4 * Copyright (C) 2005-2006 VideoLAN
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 *****************************************************************************/
33 #include <stdlib.h> /* free(), strtol() */
34 #include <stdio.h> /* FILE */
35 #include <string.h> /* strerror() */
38 #include <vlc/input.h>
41 #include "vlc_interaction.h"
42 #include "vlc_interface.h"
43 #include "vlc_playlist.h"
45 /*****************************************************************************
47 *****************************************************************************/
48 static void InteractionInit( playlist_t *p_playlist );
49 static interaction_t * InteractionGet( vlc_object_t *p_this );
50 static void InteractionSearchInterface( interaction_t *
52 static interaction_dialog_t *DialogGetById( interaction_t* , int );
53 static void DialogDestroy( interaction_dialog_t *p_dialog );
54 static int DialogSend( vlc_object_t *p_this, interaction_dialog_t *p_dialog );
57 * Destroy the interaction system
58 * \param The interaction object to destroy
61 void intf_InteractionDestroy( interaction_t *p_interaction )
64 // Remove all dialogs - Interfaces must be able to clean up their data
65 for( i = p_interaction->i_dialogs -1 ; i >= 0; i-- )
67 interaction_dialog_t * p_dialog = p_interaction->pp_dialogs[i];
68 DialogDestroy( p_dialog );
69 REMOVE_ELEM( p_interaction->pp_dialogs, p_interaction->i_dialogs, i );
71 vlc_object_destroy( p_interaction );
75 * The main interaction processing loop
76 * This function is called from the playlist loop
78 * \param p_playlist the parent playlist
81 void intf_InteractionManage( playlist_t *p_playlist )
85 interaction_t *p_interaction = p_playlist->p_interaction;
88 if( p_interaction->i_dialogs == 0 ) return;
90 vlc_mutex_lock( &p_interaction->object_lock );
92 InteractionSearchInterface( p_interaction );
93 if( !p_interaction->p_intf )
95 // We mark all dialogs as answered with their "default" answer
96 for( i_index = 0 ; i_index < p_interaction->i_dialogs; i_index ++ )
98 interaction_dialog_t *p_dialog = p_interaction->pp_dialogs[i_index];
99 p_dialog->i_return = DIALOG_DEFAULT; // Give default answer
101 // Pretend we have hidden and destroyed it
102 if( p_dialog->i_status == HIDDEN_DIALOG )
104 p_dialog->i_status = DESTROYED_DIALOG;
108 p_dialog->i_status = HIDING_DIALOG;
114 vlc_object_yield( p_interaction->p_intf );
117 for( i_index = 0 ; i_index < p_interaction->i_dialogs; i_index ++ )
119 interaction_dialog_t *p_dialog = p_interaction->pp_dialogs[i_index];
120 switch( p_dialog->i_status )
122 case ANSWERED_DIALOG:
123 // Ask interface to hide it
124 p_dialog->i_action = INTERACT_HIDE;
125 val.p_address = p_dialog;
126 if( p_interaction->p_intf )
127 var_Set( p_interaction->p_intf, "interaction", val );
128 p_dialog->i_status = HIDING_DIALOG;
131 p_dialog->i_action = INTERACT_UPDATE;
132 val.p_address = p_dialog;
133 if( p_interaction->p_intf )
134 var_Set( p_interaction->p_intf, "interaction", val );
135 p_dialog->i_status = SENT_DIALOG;
138 if( !(p_dialog->i_flags & DIALOG_GOT_ANSWER) ) break;
139 p_dialog->i_action = INTERACT_DESTROY;
140 val.p_address = p_dialog;
141 if( p_interaction->p_intf )
142 var_Set( p_interaction->p_intf, "interaction", val );
144 case DESTROYED_DIALOG:
145 // Interface has now destroyed it, remove it
146 REMOVE_ELEM( p_interaction->pp_dialogs, p_interaction->i_dialogs,
149 DialogDestroy( p_dialog );
152 // This is truly a new dialog, send it.
153 p_dialog->i_action = INTERACT_NEW;
154 val.p_address = p_dialog;
155 if( p_interaction->p_intf )
156 var_Set( p_interaction->p_intf, "interaction", val );
157 p_dialog->i_status = SENT_DIALOG;
162 if( p_interaction->p_intf )
164 vlc_object_release( p_interaction->p_intf );
167 vlc_mutex_unlock( &p_playlist->p_interaction->object_lock );
170 #define DIALOG_INIT( type ) \
171 DECMALLOC_ERR( p_new, interaction_dialog_t ); \
172 memset( p_new, 0, sizeof( interaction_dialog_t ) ); \
173 p_new->b_cancelled = VLC_FALSE; \
174 p_new->i_status = NEW_DIALOG; \
175 p_new->i_flags = 0; \
176 p_new->i_type = INTERACT_DIALOG_##type; \
177 p_new->psz_returned[0] = NULL; \
178 p_new->psz_returned[1] = NULL;
180 #define FORMAT_DESC \
181 va_start( args, psz_format ); \
182 vasprintf( &p_new->psz_description, psz_format, args ); \
185 /** Send an error message, both in a blocking and non-blocking way
186 * \param p_this Parent vlc_object
187 * \param b_blocking Is this dialog blocking or not?
188 * \param psz_title Title for the dialog
189 * \param psz_format The message to display
191 int __intf_UserFatal( vlc_object_t *p_this, vlc_bool_t b_blocking,
192 const char *psz_title,
193 const char *psz_format, ... )
196 DIALOG_INIT( ONEWAY );
198 p_new->psz_title = strdup( psz_title );
202 p_new->i_flags = DIALOG_BLOCKING_ERROR;
204 p_new->i_flags = DIALOG_NONBLOCKING_ERROR;
206 return DialogSend( p_this, p_new );
209 /** Helper function to send an warning, which is always shown non-blocking
210 * \param p_this Parent vlc_object
211 * \param psz_title Title for the dialog
212 * \param psz_format The message to display
214 int __intf_UserWarn( vlc_object_t *p_this,
215 const char *psz_title,
216 const char *psz_format, ... )
219 DIALOG_INIT( ONEWAY );
221 p_new->psz_title = strdup( psz_title );
224 p_new->i_flags = DIALOG_WARNING;
226 return DialogSend( p_this, p_new );
229 /** Helper function to ask a yes-no-cancel question
230 * \param p_this Parent vlc_object
231 * \param psz_title Title for the dialog
232 * \param psz_description A description
233 * \param psz_default caption for the default button
234 * \param psz_alternate caption for the alternate button
235 * \param psz_other caption for the optional 3rd button (== cancel)
236 * \return Clicked button code
238 int __intf_UserYesNo( vlc_object_t *p_this,
239 const char *psz_title,
240 const char *psz_description,
241 const char *psz_default,
242 const char *psz_alternate,
243 const char *psz_other )
245 DIALOG_INIT( TWOWAY );
247 p_new->psz_title = strdup( psz_title );
248 p_new->psz_description = strdup( psz_description );
249 p_new->i_flags = DIALOG_YES_NO_CANCEL;
250 p_new->psz_default_button = strdup( psz_default );
251 p_new->psz_alternate_button = strdup( psz_alternate );
253 p_new->psz_other_button = strdup( psz_other );
255 return DialogSend( p_this, p_new );
258 /** Helper function to create a dialogue showing a progress-bar with some info
259 * \param p_this Parent vlc_object
260 * \param psz_title Title for the dialog (NULL implies main intf )
261 * \param psz_status Current status
262 * \param f_position Current position (0.0->100.0)
263 * \param i_timeToGo Time (in sec) to go until process is finished
264 * \return Dialog id, to give to UserProgressUpdate
266 int __intf_Progress( vlc_object_t *p_this, const char *psz_title,
267 const char *psz_status, float f_pos, int i_time )
269 DIALOG_INIT( ONEWAY );
270 p_new->psz_description = strdup( psz_status );
271 p_new->val.f_float = f_pos;
272 p_new->i_timeToGo = i_time;
276 p_new->psz_title = strdup( psz_title );
277 p_new->i_flags = DIALOG_USER_PROGRESS;
280 p_new->i_flags = DIALOG_INTF_PROGRESS;
282 DialogSend( p_this, p_new );
286 /** Update a progress bar in a dialogue
287 * \param p_this Parent vlc_object
288 * \param i_id Identifier of the dialog
289 * \param psz_status New status
290 * \param f_position New position (0.0->100.0)
291 * \param i_timeToGo Time (in sec) to go until process is finished
294 void __intf_ProgressUpdate( vlc_object_t *p_this, int i_id,
295 const char *psz_status, float f_pos, int i_time )
297 interaction_t *p_interaction = InteractionGet( p_this );
298 interaction_dialog_t *p_dialog;
300 if( !p_interaction ) return;
302 vlc_mutex_lock( &p_interaction->object_lock );
303 p_dialog = DialogGetById( p_interaction, i_id );
307 vlc_mutex_unlock( &p_interaction->object_lock ) ;
311 FREE( p_dialog->psz_description );
312 p_dialog->psz_description = strdup( psz_status );
314 p_dialog->val.f_float = f_pos;
315 p_dialog->i_timeToGo = i_time;
317 p_dialog->i_status = UPDATED_DIALOG;
318 vlc_mutex_unlock( &p_interaction->object_lock) ;
321 /** Helper function to communicate dialogue cancellations between the
322 * interface module and the caller
323 * \param p_this Parent vlc_object
324 * \param i_id Identifier of the dialogue
325 * \return Either true or false
327 vlc_bool_t __intf_UserProgressIsCancelled( vlc_object_t *p_this, int i_id )
329 interaction_t *p_interaction = InteractionGet( p_this );
330 interaction_dialog_t *p_dialog;
333 if( !p_interaction ) return VLC_TRUE;
335 vlc_mutex_lock( &p_interaction->object_lock );
336 p_dialog = DialogGetById( p_interaction, i_id );
339 vlc_mutex_unlock( &p_interaction->object_lock ) ;
343 b_cancel = p_dialog->b_cancelled;
344 vlc_mutex_unlock( &p_interaction->object_lock );
348 /** Helper function to make a login/password dialogue
349 * \param p_this Parent vlc_object
350 * \param psz_title Title for the dialog
351 * \param psz_description A description
352 * \param ppsz_login Returned login
353 * \param ppsz_password Returned password
354 * \return Clicked button code
356 int __intf_UserLoginPassword( vlc_object_t *p_this,
357 const char *psz_title,
358 const char *psz_description,
360 char **ppsz_password )
363 DIALOG_INIT( TWOWAY );
364 p_new->i_type = INTERACT_DIALOG_TWOWAY;
365 p_new->psz_title = strdup( psz_title );
366 p_new->psz_description = strdup( psz_description );
368 p_new->i_flags = DIALOG_LOGIN_PW_OK_CANCEL;
370 i_ret = DialogSend( p_this, p_new );
372 if( i_ret != DIALOG_CANCELLED )
374 assert( p_new->psz_returned[0] && p_new->psz_returned[1] );
375 *ppsz_login = strdup( p_new->psz_returned[0] );
376 *ppsz_password = strdup( p_new->psz_returned[1] );
381 /** Helper function to make a dialogue asking the user for !password string
382 * \param p_this Parent vlc_object
383 * \param psz_title Title for the dialog
384 * \param psz_description A description
385 * \param ppsz_usersString Returned login
386 * \return Clicked button code
388 int __intf_UserStringInput( vlc_object_t *p_this,
389 const char *psz_title,
390 const char *psz_description,
391 char **ppsz_usersString )
394 DIALOG_INIT( TWOWAY );
395 p_new->psz_title = strdup( psz_title );
396 p_new->psz_description = strdup( psz_description );
398 p_new->i_flags = DIALOG_PSZ_INPUT_OK_CANCEL;
400 i_ret = DialogSend( p_this, p_new );
402 if( i_ret != DIALOG_CANCELLED )
404 assert( p_new->psz_returned[0] );
405 *ppsz_usersString = strdup( p_new->psz_returned[0] );
410 /** Hide an interaction dialog
411 * \param p_this the parent vlc object
412 * \param i_id the id of the item to hide
415 void __intf_UserHide( vlc_object_t *p_this, int i_id )
417 interaction_t *p_interaction = InteractionGet( p_this );
418 interaction_dialog_t *p_dialog;
420 if( !p_interaction ) return;
422 vlc_mutex_lock( &p_interaction->object_lock );
423 p_dialog = DialogGetById( p_interaction, i_id );
427 vlc_mutex_unlock( &p_interaction->object_lock );
431 p_dialog->i_status = ANSWERED_DIALOG;
432 vlc_mutex_unlock( &p_interaction->object_lock );
435 /**********************************************************************
436 * The following functions are local
437 **********************************************************************/
439 /* Get the interaction object. Create it if needed */
440 static interaction_t * InteractionGet( vlc_object_t *p_this )
442 playlist_t *p_playlist;
443 interaction_t *p_interaction;
445 p_playlist = (playlist_t*) vlc_object_find( p_this, VLC_OBJECT_PLAYLIST,
450 if( p_playlist->p_interaction == NULL )
451 InteractionInit( p_playlist );
453 p_interaction = p_playlist->p_interaction;
455 vlc_object_release( p_playlist );
456 return p_interaction;
459 /* Create the interaction object in the given playlist object */
460 static void InteractionInit( playlist_t *p_playlist )
462 interaction_t *p_interaction = vlc_object_create( VLC_OBJECT( p_playlist ),
463 sizeof( interaction_t ) );
466 msg_Err( p_playlist,"out of memory" );
470 p_interaction->i_dialogs = 0;
471 p_interaction->pp_dialogs = NULL;
472 p_interaction->p_intf = NULL;
473 p_interaction->i_last_id = DIALOG_LAST_PREDEFINED + 1;
475 vlc_mutex_init( p_interaction , &p_interaction->object_lock );
477 p_playlist->p_interaction = p_interaction;
480 /* Look for an interface suitable for interaction */
481 static void InteractionSearchInterface( interaction_t *p_interaction )
486 p_interaction->p_intf = NULL;
488 p_list = vlc_list_find( p_interaction, VLC_OBJECT_INTF, FIND_ANYWHERE );
491 msg_Err( p_interaction, "unable to create module list" );
495 for( i_index = 0; i_index < p_list->i_count; i_index ++ )
497 intf_thread_t *p_intf = (intf_thread_t *)
498 p_list->p_values[i_index].p_object;
499 if( p_intf->b_interaction )
501 p_interaction->p_intf = p_intf;
505 vlc_list_release ( p_list );
508 /* Find an interaction dialog by its id */
509 static interaction_dialog_t *DialogGetById( interaction_t *p_interaction,
513 for( i = 0 ; i< p_interaction->i_dialogs; i++ )
515 if( p_interaction->pp_dialogs[i]->i_id == i_id )
516 return p_interaction->pp_dialogs[i];
521 /* Destroy a dialog */
522 static void DialogDestroy( interaction_dialog_t *p_dialog )
524 FREENULL( p_dialog->psz_title );
525 FREENULL( p_dialog->psz_description );
526 FREENULL( p_dialog->psz_default_button );
527 FREENULL( p_dialog->psz_alternate_button );
528 FREENULL( p_dialog->psz_other_button );
532 /* Ask for the dialog to be sent to the user. Wait for answer
534 static int DialogSend( vlc_object_t *p_this, interaction_dialog_t *p_dialog )
536 interaction_t *p_interaction = InteractionGet( p_this );
538 /* Get an id, if we don't already have one */
539 if( p_dialog->i_id == 0 )
540 p_dialog->i_id = ++p_interaction->i_last_id;
542 if( p_this->i_flags & OBJECT_FLAGS_NOINTERACT ) return VLC_EGENERIC;
544 if( config_GetInt(p_this, "interact") ||
545 p_dialog->i_flags & DIALOG_BLOCKING_ERROR ||
546 p_dialog->i_flags & DIALOG_NONBLOCKING_ERROR )
548 vlc_bool_t b_found = VLC_FALSE;
550 p_dialog->p_interaction = p_interaction;
551 p_dialog->p_parent = p_this;
553 /* Check if we have already added this dialog */
554 vlc_mutex_lock( &p_interaction->object_lock );
555 for( i = 0 ; i< p_interaction->i_dialogs; i++ )
557 if( p_interaction->pp_dialogs[i]->i_id == p_dialog->i_id )
560 /* Add it to the queue, the main loop will send the orders to the
564 INSERT_ELEM( p_interaction->pp_dialogs,
565 p_interaction->i_dialogs,
566 p_interaction->i_dialogs,
570 p_dialog->i_status = UPDATED_DIALOG;
571 vlc_mutex_unlock( &p_interaction->object_lock );
573 if( p_dialog->i_type == INTERACT_DIALOG_TWOWAY ) // Wait for answer
575 while( p_dialog->i_status != ANSWERED_DIALOG &&
576 p_dialog->i_status != HIDING_DIALOG &&
577 p_dialog->i_status != HIDDEN_DIALOG &&
578 !p_dialog->p_parent->b_die )
583 if( p_dialog->p_parent->b_die )
585 p_dialog->i_return = DIALOG_CANCELLED;
586 p_dialog->i_status = ANSWERED_DIALOG;
588 p_dialog->i_flags |= DIALOG_GOT_ANSWER;
589 return p_dialog->i_return;
593 // Pretend we already retrieved the "answer"
594 p_dialog->i_flags |= DIALOG_GOT_ANSWER;
595 vlc_mutex_unlock( &p_interaction->object_lock );