]> git.sesse.net Git - vlc/blob - src/interface/interaction.c
Cosmetic
[vlc] / src / interface / interaction.c
1 /*****************************************************************************
2  * interaction.c: User interaction functions
3  *****************************************************************************
4  * Copyright (C) 2005-2006 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Clément Stenac <zorglub@videolan.org>
8  *          Felix Kühne <fkuehne@videolan.org>
9  *
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.
14  *
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.
19  *
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  *****************************************************************************/
24
25 /**
26  *   \file
27  *   This file contains functions related to user interaction management
28  */
29
30 /*****************************************************************************
31  * Preamble
32  *****************************************************************************/
33
34 #include <vlc/vlc.h>
35
36 #include <stdlib.h>                                      /* free(), strtol() */
37 #include <stdio.h>                                                   /* FILE */
38 #include <string.h>
39
40 #include <vlc_interface.h>
41 #include <vlc_playlist.h>
42 #include "interface.h"
43
44 /*****************************************************************************
45  * Local prototypes
46  *****************************************************************************/
47 static void                  InteractionInit( playlist_t *p_playlist );
48 static interaction_t *       InteractionGet( vlc_object_t *p_this );
49 static void                  InteractionSearchInterface( interaction_t *
50                                                           p_interaction );
51 static interaction_dialog_t *DialogGetById( interaction_t* , int );
52 static void                  DialogDestroy( interaction_dialog_t *p_dialog );
53 static int DialogSend( vlc_object_t *p_this, interaction_dialog_t *p_dialog );
54
55 /**
56  * Destroy the interaction system
57  * \param The interaction object to destroy
58  * \return nothing
59  */
60 void intf_InteractionDestroy( interaction_t *p_interaction )
61 {
62     int i;
63     // Remove all dialogs - Interfaces must be able to clean up their data
64     for( i = p_interaction->i_dialogs -1 ; i >= 0; i-- )
65     {
66         interaction_dialog_t * p_dialog = p_interaction->pp_dialogs[i];
67         DialogDestroy( p_dialog );
68         REMOVE_ELEM( p_interaction->pp_dialogs, p_interaction->i_dialogs, i );
69     }
70     vlc_object_destroy( p_interaction );
71 }
72
73 /**
74  * The main interaction processing loop
75  * This function is called from the playlist loop
76  *
77  * \param p_playlist the parent playlist
78  * \return nothing
79  */
80 void intf_InteractionManage( playlist_t *p_playlist )
81 {
82     vlc_value_t val;
83     int i_index;
84     interaction_t *p_interaction = p_playlist->p_interaction;
85
86     // Nothing to do
87     if( p_interaction->i_dialogs == 0 ) return;
88
89     vlc_mutex_lock( &p_interaction->object_lock );
90
91     InteractionSearchInterface( p_interaction );
92     if( !p_interaction->p_intf )
93     {
94         // We mark all dialogs as answered with their "default" answer
95         for( i_index = 0 ; i_index < p_interaction->i_dialogs; i_index ++ )
96         {
97             interaction_dialog_t *p_dialog = p_interaction->pp_dialogs[i_index];
98             p_dialog->i_return = DIALOG_DEFAULT; // Give default answer
99
100             // Pretend we have hidden and destroyed it
101             if( p_dialog->i_status == HIDDEN_DIALOG )
102                 p_dialog->i_status = DESTROYED_DIALOG;
103             else
104                 p_dialog->i_status = HIDING_DIALOG;
105         }
106     }
107     else
108         vlc_object_yield( p_interaction->p_intf );
109
110     for( i_index = 0 ; i_index < p_interaction->i_dialogs; i_index ++ )
111     {
112         interaction_dialog_t *p_dialog = p_interaction->pp_dialogs[i_index];
113         switch( p_dialog->i_status )
114         {
115         case ANSWERED_DIALOG:
116             // Ask interface to hide it
117             p_dialog->i_action = INTERACT_HIDE;
118             val.p_address = p_dialog;
119             if( p_interaction->p_intf )
120                 var_Set( p_interaction->p_intf, "interaction", val );
121             p_dialog->i_status = HIDING_DIALOG;
122             break;
123         case UPDATED_DIALOG:
124             p_dialog->i_action = INTERACT_UPDATE;
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 = SENT_DIALOG;
129             break;
130         case HIDDEN_DIALOG:
131             if( !(p_dialog->i_flags & DIALOG_GOT_ANSWER) ) break;
132             p_dialog->i_action = INTERACT_DESTROY;
133             val.p_address = p_dialog;
134             if( p_interaction->p_intf )
135                 var_Set( p_interaction->p_intf, "interaction", val );
136             break;
137         case DESTROYED_DIALOG:
138             // Interface has now destroyed it, remove it
139             REMOVE_ELEM( p_interaction->pp_dialogs, p_interaction->i_dialogs,
140                          i_index);
141             i_index--;
142             DialogDestroy( p_dialog );
143             break;
144         case NEW_DIALOG:
145             // This is truly a new dialog, send it.
146             p_dialog->i_action = INTERACT_NEW;
147             val.p_address = p_dialog;
148             if( p_interaction->p_intf )
149                 var_Set( p_interaction->p_intf, "interaction", val );
150             p_dialog->i_status = SENT_DIALOG;
151             break;
152         }
153     }
154
155     if( p_interaction->p_intf )
156     {
157         vlc_object_release( p_interaction->p_intf );
158     }
159
160     vlc_mutex_unlock( &p_playlist->p_interaction->object_lock );
161 }
162
163 #define DIALOG_INIT( type ) \
164         DECMALLOC_ERR( p_new, interaction_dialog_t );                     \
165         memset( p_new, 0, sizeof( interaction_dialog_t ) );               \
166         p_new->b_cancelled = VLC_FALSE;                                   \
167         p_new->i_status = NEW_DIALOG;                                     \
168         p_new->i_flags = 0; \
169         p_new->i_type = INTERACT_DIALOG_##type;                           \
170         p_new->psz_returned[0] = NULL;                                    \
171         p_new->psz_returned[1] = NULL;
172
173 #define FORMAT_DESC \
174         va_start( args, psz_format ); \
175         vasprintf( &p_new->psz_description, psz_format, args ); \
176         va_end( args );
177
178 /** Send an error message, both in a blocking and non-blocking way
179  *  \param p_this     Parent vlc_object
180  *  \param b_blocking Is this dialog blocking or not?
181  *  \param psz_title  Title for the dialog
182  *  \param psz_format The message to display
183  *  */
184 int __intf_UserFatal( vlc_object_t *p_this, vlc_bool_t b_blocking,
185                        const char *psz_title,
186                        const char *psz_format, ... )
187 {
188     va_list args;
189     DIALOG_INIT( ONEWAY );
190
191     p_new->psz_title = strdup( psz_title );
192     FORMAT_DESC;
193
194     if( b_blocking )
195         p_new->i_flags = DIALOG_BLOCKING_ERROR;
196     else
197         p_new->i_flags = DIALOG_NONBLOCKING_ERROR;
198
199     return DialogSend( p_this, p_new );
200 }
201
202 /** Helper function to send an warning, which is always shown non-blocking
203  *  \param p_this     Parent vlc_object
204  *  \param psz_title  Title for the dialog
205  *  \param psz_format The message to display
206  *  */
207 int __intf_UserWarn( vlc_object_t *p_this,
208                      const char *psz_title,
209                      const char *psz_format, ... )
210 {
211     va_list args;
212     DIALOG_INIT( ONEWAY );
213
214     p_new->psz_title = strdup( psz_title );
215     FORMAT_DESC
216
217     p_new->i_flags = DIALOG_WARNING;
218
219     return DialogSend( p_this, p_new );
220 }
221
222 /** Helper function to ask a yes-no-cancel question
223  *  \param p_this           Parent vlc_object
224  *  \param psz_title        Title for the dialog
225  *  \param psz_description  A description
226  *  \param psz_default      caption for the default button
227  *  \param psz_alternate    caption for the alternate button
228  *  \param psz_other        caption for the optional 3rd button (== cancel)
229  *  \return                 Clicked button code
230  */
231 int __intf_UserYesNo( vlc_object_t *p_this,
232                       const char *psz_title,
233                       const char *psz_description,
234                       const char *psz_default,
235                       const char *psz_alternate,
236                       const char *psz_other )
237 {
238     DIALOG_INIT( TWOWAY );
239
240     p_new->psz_title = strdup( psz_title );
241     p_new->psz_description = strdup( psz_description );
242     p_new->i_flags = DIALOG_YES_NO_CANCEL;
243     p_new->psz_default_button = strdup( psz_default );
244     p_new->psz_alternate_button = strdup( psz_alternate );
245     if( psz_other )
246         p_new->psz_other_button = strdup( psz_other );
247
248     return DialogSend( p_this, p_new );
249 }
250
251 /** Helper function to create a dialogue showing a progress-bar with some info
252  *  \param p_this           Parent vlc_object
253  *  \param psz_title        Title for the dialog (NULL implies main intf )
254  *  \param psz_status       Current status
255  *  \param f_position       Current position (0.0->100.0)
256  *  \param i_timeToGo       Time (in sec) to go until process is finished
257  *  \return                 Dialog id, to give to UserProgressUpdate
258  */
259 int __intf_Progress( vlc_object_t *p_this, const char *psz_title,
260                      const char *psz_status, float f_pos, int i_time )
261 {
262     DIALOG_INIT( ONEWAY );
263     p_new->psz_description = strdup( psz_status );
264     p_new->val.f_float = f_pos;
265     p_new->i_timeToGo = i_time;
266     p_new->psz_alternate_button = strdup( _( "Cancel" ) );
267
268     if( psz_title )
269     {
270         p_new->psz_title = strdup( psz_title );
271         p_new->i_flags = DIALOG_USER_PROGRESS;
272     }
273     else
274         p_new->i_flags = DIALOG_INTF_PROGRESS;
275
276     DialogSend( p_this, p_new );
277     return p_new->i_id;
278 }
279
280 /** Update a progress bar in a dialogue
281  *  \param p_this           Parent vlc_object
282  *  \param i_id             Identifier of the dialog
283  *  \param psz_status       New status
284  *  \param f_position       New position (0.0->100.0)
285  *  \param i_timeToGo       Time (in sec) to go until process is finished
286  *  \return                 nothing
287  */
288 void __intf_ProgressUpdate( vlc_object_t *p_this, int i_id,
289                             const char *psz_status, float f_pos, int i_time )
290 {
291     interaction_t *p_interaction = InteractionGet( p_this );
292     interaction_dialog_t *p_dialog;
293
294     if( !p_interaction ) return;
295
296     vlc_mutex_lock( &p_interaction->object_lock );
297     p_dialog  =  DialogGetById( p_interaction, i_id );
298
299     if( !p_dialog )
300     {
301         vlc_mutex_unlock( &p_interaction->object_lock ) ;
302         return;
303     }
304
305     free( p_dialog->psz_description );
306     p_dialog->psz_description = strdup( psz_status );
307
308     p_dialog->val.f_float = f_pos;
309     p_dialog->i_timeToGo = i_time;
310
311     p_dialog->i_status = UPDATED_DIALOG;
312     vlc_mutex_unlock( &p_interaction->object_lock) ;
313
314     playlist_Signal( pl_Get( p_this ) );
315 }
316
317 /** Helper function to communicate dialogue cancellations between the
318  *  interface module and the caller
319  *  \param p_this           Parent vlc_object
320  *  \param i_id             Identifier of the dialogue
321  *  \return                 Either true or false
322  */
323 vlc_bool_t __intf_UserProgressIsCancelled( vlc_object_t *p_this, int i_id )
324 {
325     interaction_t *p_interaction = InteractionGet( p_this );
326     interaction_dialog_t *p_dialog;
327     vlc_bool_t b_cancel;
328
329     if( !p_interaction ) return VLC_TRUE;
330
331     vlc_mutex_lock( &p_interaction->object_lock );
332     p_dialog  =  DialogGetById( p_interaction, i_id );
333     if( !p_dialog )
334     {
335         vlc_mutex_unlock( &p_interaction->object_lock ) ;
336         return VLC_TRUE;
337     }
338
339     b_cancel = p_dialog->b_cancelled;
340     vlc_mutex_unlock( &p_interaction->object_lock );
341     return b_cancel;
342 }
343
344 /** Helper function to make a login/password dialogue
345  *  \param p_this           Parent vlc_object
346  *  \param psz_title        Title for the dialog
347  *  \param psz_description  A description
348  *  \param ppsz_login       Returned login
349  *  \param ppsz_password    Returned password
350  *  \return                 Clicked button code
351  */
352 int __intf_UserLoginPassword( vlc_object_t *p_this,
353                               const char *psz_title,
354                               const char *psz_description,
355                               char **ppsz_login,
356                               char **ppsz_password )
357 {
358     int i_ret;
359     DIALOG_INIT( TWOWAY );
360     p_new->i_type = INTERACT_DIALOG_TWOWAY;
361     p_new->psz_title = strdup( psz_title );
362     p_new->psz_description = strdup( psz_description );
363     p_new->psz_default_button = strdup( _("Ok" ) );
364     p_new->psz_alternate_button = strdup( _("Cancel" ) );
365
366     p_new->i_flags = DIALOG_LOGIN_PW_OK_CANCEL;
367
368     i_ret = DialogSend( p_this, p_new );
369
370     if( i_ret != DIALOG_CANCELLED && i_ret != VLC_EGENERIC )
371     {
372         *ppsz_login = p_new->psz_returned[0]?
373                                 strdup( p_new->psz_returned[0] ) : NULL;
374         *ppsz_password = p_new->psz_returned[1]?
375                                 strdup( p_new->psz_returned[1] ) : NULL;
376     }
377     return i_ret;
378 }
379
380 /** Helper function to make a dialogue asking the user for !password string
381  *  \param p_this           Parent vlc_object
382  *  \param psz_title        Title for the dialog
383  *  \param psz_description  A description
384  *  \param ppsz_usersString Returned login
385  *  \return                 Clicked button code
386  */
387 int __intf_UserStringInput( vlc_object_t *p_this,
388                               const char *psz_title,
389                               const char *psz_description,
390                               char **ppsz_usersString )
391 {
392     int i_ret;
393     DIALOG_INIT( TWOWAY );
394     p_new->i_type = INTERACT_DIALOG_TWOWAY;
395     p_new->psz_title = strdup( psz_title );
396     p_new->psz_description = strdup( psz_description );
397
398     p_new->i_flags = DIALOG_PSZ_INPUT_OK_CANCEL;
399
400     i_ret = DialogSend( p_this, p_new );
401
402     if( i_ret != DIALOG_CANCELLED )
403     {
404         *ppsz_usersString = p_new->psz_returned[0]?
405                                     strdup( p_new->psz_returned[0] ) : NULL;
406     }
407     return i_ret;
408 }
409
410 /** Hide an interaction dialog
411  * \param p_this the parent vlc object
412  * \param i_id the id of the item to hide
413  * \return nothing
414  */
415 void __intf_UserHide( vlc_object_t *p_this, int i_id )
416 {
417     interaction_t *p_interaction = InteractionGet( p_this );
418     interaction_dialog_t *p_dialog;
419
420     if( !p_interaction ) return;
421
422     vlc_mutex_lock( &p_interaction->object_lock );
423     p_dialog = DialogGetById( p_interaction, i_id );
424
425     if( !p_dialog )
426     {
427        vlc_mutex_unlock( &p_interaction->object_lock );
428        return;
429     }
430
431     p_dialog->i_status = ANSWERED_DIALOG;
432     vlc_mutex_unlock( &p_interaction->object_lock );
433 }
434
435 /**********************************************************************
436  * The following functions are local
437  **********************************************************************/
438
439 /* Get the interaction object. Create it if needed */
440 static interaction_t * InteractionGet( vlc_object_t *p_this )
441 {
442     interaction_t *p_interaction;
443     playlist_t *p_playlist = pl_Yield( p_this );
444
445     PL_LOCK;
446     if( p_playlist->p_interaction == NULL )
447        InteractionInit( p_playlist );
448
449     p_interaction = p_playlist->p_interaction;
450     PL_UNLOCK;
451
452     pl_Release( p_this );
453     return p_interaction;
454 }
455
456 /* Create the interaction object in the given playlist object */
457 static void InteractionInit( playlist_t *p_playlist )
458 {
459     interaction_t *p_interaction = vlc_object_create( VLC_OBJECT( p_playlist ),
460                                                       sizeof( interaction_t ) );
461     if( !p_interaction )
462     {
463         msg_Err( p_playlist,"out of memory" );
464         return;
465     }
466
467     p_interaction->i_dialogs = 0;
468     p_interaction->pp_dialogs = NULL;
469     p_interaction->p_intf = NULL;
470     p_interaction->i_last_id = 0;
471
472     vlc_mutex_init( p_interaction , &p_interaction->object_lock );
473     p_playlist->p_interaction  = p_interaction;
474 }
475
476 /* Look for an interface suitable for interaction */
477 static void InteractionSearchInterface( interaction_t *p_interaction )
478 {
479     vlc_list_t  *p_list;
480     int          i_index;
481
482     p_interaction->p_intf = NULL;
483
484     p_list = vlc_list_find( p_interaction, VLC_OBJECT_INTF, FIND_ANYWHERE );
485     if( !p_list )
486     {
487         msg_Err( p_interaction, "unable to create module list" );
488         return;
489     }
490
491     for( i_index = 0; i_index < p_list->i_count; i_index ++ )
492     {
493         intf_thread_t *p_intf = (intf_thread_t *)
494                                         p_list->p_values[i_index].p_object;
495         if( p_intf->b_interaction )
496         {
497             p_interaction->p_intf = p_intf;
498             break;
499         }
500     }
501     vlc_list_release ( p_list );
502 }
503
504 /* Find an interaction dialog by its id */
505 static interaction_dialog_t *DialogGetById( interaction_t *p_interaction,
506                                             int i_id )
507 {
508     int i;
509     for( i = 0 ; i< p_interaction->i_dialogs; i++ )
510     {
511         if( p_interaction->pp_dialogs[i]->i_id == i_id )
512             return p_interaction->pp_dialogs[i];
513     }
514     return NULL;
515 }
516
517 /* Destroy a dialog */
518 static void DialogDestroy( interaction_dialog_t *p_dialog )
519 {
520     free( p_dialog->psz_title );
521     free( p_dialog->psz_description );
522     free( p_dialog->psz_default_button );
523     free( p_dialog->psz_alternate_button );
524     free( p_dialog->psz_other_button );
525     free( p_dialog );
526 }
527
528 /* Ask for the dialog to be sent to the user. Wait for answer
529  * if required */
530 static int DialogSend( vlc_object_t *p_this, interaction_dialog_t *p_dialog )
531 {
532     interaction_t *p_interaction = InteractionGet( p_this );
533
534     /* Get an id, if we don't already have one */
535     if( p_dialog->i_id == 0 )
536         p_dialog->i_id = ++p_interaction->i_last_id;
537
538     if( p_this->i_flags & OBJECT_FLAGS_NOINTERACT ) return VLC_EGENERIC;
539
540     if( config_GetInt(p_this, "interact") ||
541         p_dialog->i_flags & DIALOG_BLOCKING_ERROR ||
542         p_dialog->i_flags & DIALOG_NONBLOCKING_ERROR )
543     {
544         vlc_bool_t b_found = VLC_FALSE;
545         int i;
546         p_dialog->p_interaction = p_interaction;
547         p_dialog->p_parent = p_this;
548
549         /* Check if we have already added this dialog */
550         vlc_mutex_lock( &p_interaction->object_lock );
551         for( i = 0 ; i< p_interaction->i_dialogs; i++ )
552         {
553             if( p_interaction->pp_dialogs[i]->i_id == p_dialog->i_id )
554                 b_found = VLC_TRUE;
555         }
556         /* Add it to the queue, the main loop will send the orders to the
557          * interface */
558         if( ! b_found )
559         {
560             INSERT_ELEM( p_interaction->pp_dialogs,
561                          p_interaction->i_dialogs,
562                          p_interaction->i_dialogs,
563                          p_dialog );
564         }
565         else
566             p_dialog->i_status = UPDATED_DIALOG;
567
568         if( p_dialog->i_type == INTERACT_DIALOG_TWOWAY ) // Wait for answer
569         {
570             playlist_Signal( pl_Get( p_this ) );
571             while( p_dialog->i_status != ANSWERED_DIALOG &&
572                    p_dialog->i_status != HIDING_DIALOG &&
573                    p_dialog->i_status != HIDDEN_DIALOG &&
574                    !p_dialog->p_parent->b_die )
575             {
576                 vlc_mutex_unlock( &p_interaction->object_lock );
577                 msleep( 100000 );
578                 vlc_mutex_lock( &p_interaction->object_lock );
579             }
580             if( p_dialog->p_parent->b_die )
581             {
582                 p_dialog->i_return = DIALOG_CANCELLED;
583                 p_dialog->i_status = ANSWERED_DIALOG;
584             }
585             p_dialog->i_flags |= DIALOG_GOT_ANSWER;
586             vlc_mutex_unlock( &p_interaction->object_lock );
587             playlist_Signal( pl_Get( p_this ) );
588             return p_dialog->i_return;
589         }
590         else
591         {
592             // Pretend we already retrieved the "answer"
593             p_dialog->i_flags |=  DIALOG_GOT_ANSWER;
594             vlc_mutex_unlock( &p_interaction->object_lock );
595             playlist_Signal( pl_Get( p_this ) );
596             return VLC_SUCCESS;
597         }
598     }
599     else
600         return VLC_EGENERIC;
601 }