]> git.sesse.net Git - vlc/blob - src/interface/interaction.c
dbc7d337895ce1c6f18275099bd3e8c3cbe814dd
[vlc] / src / interface / interaction.c
1 /*****************************************************************************
2  * interaction.c: User interaction functions
3  *****************************************************************************
4  * Copyright © 2005-2008 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 #ifdef HAVE_CONFIG_H
35 # include "config.h"
36 #endif
37
38 #include <vlc_common.h>
39
40 #include <vlc_interface.h>
41 #include "interface.h"
42 #include "libvlc.h"
43
44 #include <assert.h>
45
46 /*****************************************************************************
47  * Local prototypes
48  *****************************************************************************/
49
50 /**
51  * This structure contains the active interaction dialogs, and is
52  * used by the manager
53  */
54 struct interaction_t
55 {
56     VLC_COMMON_MEMBERS
57
58     vlc_thread_t thread;
59     vlc_cond_t wait;
60
61     int                         i_dialogs;      ///< Number of dialogs
62     interaction_dialog_t      **pp_dialogs;     ///< Dialogs
63     intf_thread_t              *p_intf;         ///< Interface to use
64     int                         i_last_id;      ///< Last attributed ID
65 };
66
67 static interaction_t *          InteractionGet( vlc_object_t * );
68 static intf_thread_t *          SearchInterface( interaction_t * );
69 static void*                    InteractionLoop( void * );
70 static void                     InteractionManage( interaction_t * );
71
72 static void                     DialogDestroy( interaction_dialog_t * );
73 static int DialogSend( vlc_object_t *, interaction_dialog_t * );
74
75 #define DIALOG_INIT( type, err ) \
76         interaction_dialog_t* p_new = calloc( 1, sizeof( interaction_dialog_t ) ); \
77         if( !p_new ) return err;                        \
78         p_new->b_cancelled = false;                     \
79         p_new->i_status = NEW_DIALOG;                   \
80         p_new->i_flags = 0;                             \
81         p_new->i_type = INTERACT_DIALOG_##type;         \
82         p_new->psz_returned[0] = NULL;                  \
83         p_new->psz_returned[1] = NULL
84
85 #define FORMAT_DESC \
86         va_start( args, psz_format ); \
87         if( vasprintf( &p_new->psz_description, psz_format, args ) == -1 ) \
88             return VLC_EGENERIC; \
89         va_end( args )
90
91 /**
92  * Send an error message, both in a blocking and non-blocking way
93  *
94  * \param p_this     Parent vlc_object
95  * \param b_blocking Is this dialog blocking or not?
96  * \param psz_title  Title for the dialog
97  * \param psz_format The message to display
98  * \return           VLC_SUCCESS or VLC_EGENERIC
99  */
100 int __intf_UserFatal( vlc_object_t *p_this, bool b_blocking,
101                        const char *psz_title,
102                        const char *psz_format, ... )
103 {
104     va_list args;
105     DIALOG_INIT( ONEWAY, VLC_EGENERIC );
106
107     p_new->psz_title = strdup( psz_title );
108     FORMAT_DESC;
109
110     if( b_blocking )
111         p_new->i_flags = DIALOG_BLOCKING_ERROR;
112     else
113         p_new->i_flags = DIALOG_NONBLOCKING_ERROR;
114
115     return DialogSend( p_this, p_new );
116 }
117
118 /**
119  * Helper function to send a warning, which is always shown non-blocking
120  *
121  * \param p_this     Parent vlc_object
122  * \param psz_title  Title for the dialog
123  * \param psz_format The message to display
124  * \return           VLC_SUCCESS or VLC_EGENERIC
125  */
126 int __intf_UserWarn( vlc_object_t *p_this,
127                      const char *psz_title,
128                      const char *psz_format, ... )
129 {
130     va_list args;
131     DIALOG_INIT( ONEWAY, VLC_EGENERIC );
132
133     p_new->psz_title = strdup( psz_title );
134     FORMAT_DESC;
135
136     p_new->i_flags = DIALOG_WARNING;
137
138     return DialogSend( p_this, p_new );
139 }
140
141 /**
142  * Helper function to ask a yes-no-cancel question
143  *
144  * \param p_this           Parent vlc_object
145  * \param psz_title        Title for the dialog
146  * \param psz_description  A description
147  * \param psz_default      caption for the default button
148  * \param psz_alternate    caption for the alternate button
149  * \param psz_other        caption for the optional 3rd button (== cancel)
150  * \return                 Clicked button code
151  */
152 int __intf_UserYesNo( vlc_object_t *p_this,
153                       const char *psz_title,
154                       const char *psz_description,
155                       const char *psz_default,
156                       const char *psz_alternate,
157                       const char *psz_other )
158 {
159     DIALOG_INIT( TWOWAY, VLC_EGENERIC );
160
161     p_new->psz_title = strdup( psz_title );
162     p_new->psz_description = strdup( psz_description );
163     p_new->i_flags = DIALOG_YES_NO_CANCEL;
164     p_new->psz_default_button = strdup( psz_default );
165     p_new->psz_alternate_button = strdup( psz_alternate );
166     if( psz_other )
167         p_new->psz_other_button = strdup( psz_other );
168
169     return DialogSend( p_this, p_new );
170 }
171
172 /**
173  * Helper function to create a dialogue showing a progress-bar with some info
174  *
175  * \param p_this           Parent vlc_object
176  * \param psz_title        Title for the dialog (NULL implies main intf )
177  * \param psz_status       Current status
178  * \param f_position       Current position (0.0->100.0)
179  * \param i_timeToGo       Time (in sec) to go until process is finished
180  * \return                 Dialog, for use with UserProgressUpdate
181  */
182 interaction_dialog_t *
183 __intf_Progress( vlc_object_t *p_this, const char *psz_title,
184                      const char *psz_status, float f_pos, int i_time )
185 {
186     DIALOG_INIT( ONEWAY, NULL );
187     p_new->psz_description = strdup( psz_status );
188     p_new->val.f_float = f_pos;
189     p_new->i_timeToGo = i_time;
190     p_new->psz_alternate_button = strdup( _( "Cancel" ) );
191
192     if( psz_title )
193     {
194         p_new->psz_title = strdup( psz_title );
195         p_new->i_flags = DIALOG_USER_PROGRESS;
196     }
197     else
198         p_new->i_flags = DIALOG_INTF_PROGRESS;
199
200     DialogSend( p_this, p_new );
201     return p_new;
202 }
203
204 /**
205  * Update a progress bar in a dialogue
206  *
207  * \param p_dialog         Dialog
208  * \param psz_status       New status
209  * \param f_position       New position (0.0->100.0)
210  * \param i_timeToGo       Time (in sec) to go until process is finished
211  * \return                 nothing
212  */
213 void intf_ProgressUpdate( interaction_dialog_t *p_dialog,
214                             const char *psz_status, float f_pos, int i_time )
215 {
216     interaction_t *p_interaction = InteractionGet( p_dialog->p_parent );
217     assert( p_interaction );
218
219     vlc_object_lock( p_interaction );
220     free( p_dialog->psz_description );
221     p_dialog->psz_description = strdup( psz_status );
222
223     p_dialog->val.f_float = f_pos;
224     p_dialog->i_timeToGo = i_time;
225
226     p_dialog->i_status = UPDATED_DIALOG;
227
228     vlc_cond_signal( &p_interaction->wait );
229     vlc_object_unlock( p_interaction );
230     vlc_object_release( p_interaction );
231 }
232
233 /**
234  * Helper function to communicate dialogue cancellations between the
235  * interface module and the caller
236  *
237  * \param p_dialog         Dialog
238  * \return                 Either true or false
239  */
240 bool intf_ProgressIsCancelled( interaction_dialog_t *p_dialog )
241 {
242     interaction_t *p_interaction = InteractionGet( p_dialog->p_parent );
243     bool b_cancel;
244
245     assert( p_interaction );
246     vlc_object_lock( p_interaction );
247     b_cancel = p_dialog->b_cancelled;
248     vlc_object_unlock( p_interaction );
249     vlc_object_release( p_interaction );
250     return b_cancel;
251 }
252
253 /**
254  * Helper function to make a login/password dialogue
255  *
256  * \param p_this           Parent vlc_object
257  * \param psz_title        Title for the dialog
258  * \param psz_description  A description
259  * \param ppsz_login       Returned login
260  * \param ppsz_password    Returned password
261  * \return                 Clicked button code
262  */
263 int __intf_UserLoginPassword( vlc_object_t *p_this,
264         const char *psz_title,
265         const char *psz_description,
266         char **ppsz_login,
267         char **ppsz_password )
268 {
269     int i_ret;
270     DIALOG_INIT( TWOWAY, VLC_EGENERIC );
271     p_new->i_type = INTERACT_DIALOG_TWOWAY;
272     p_new->psz_title = strdup( psz_title );
273     p_new->psz_description = strdup( psz_description );
274     p_new->psz_default_button = strdup( _("OK" ) );
275     p_new->psz_alternate_button = strdup( _("Cancel" ) );
276
277     p_new->i_flags = DIALOG_LOGIN_PW_OK_CANCEL;
278
279     i_ret = DialogSend( p_this, p_new );
280
281     if( i_ret != DIALOG_CANCELLED && i_ret != VLC_EGENERIC )
282     {
283         *ppsz_login = p_new->psz_returned[0]?
284             strdup( p_new->psz_returned[0] ) : NULL;
285         *ppsz_password = p_new->psz_returned[1]?
286             strdup( p_new->psz_returned[1] ) : NULL;
287     }
288     return i_ret;
289 }
290
291 /**
292  * Helper function to make a dialogue asking the user for !password string
293  *
294  * \param p_this           Parent vlc_object
295  * \param psz_title        Title for the dialog
296  * \param psz_description  A description
297  * \param ppsz_usersString Returned login
298  * \return                 Clicked button code
299  */
300 int __intf_UserStringInput( vlc_object_t *p_this,
301         const char *psz_title,
302         const char *psz_description,
303         char **ppsz_usersString )
304 {
305     int i_ret;
306     DIALOG_INIT( TWOWAY, VLC_EGENERIC );
307     p_new->i_type = INTERACT_DIALOG_TWOWAY;
308     p_new->psz_title = strdup( psz_title );
309     p_new->psz_description = strdup( psz_description );
310
311     p_new->i_flags = DIALOG_PSZ_INPUT_OK_CANCEL;
312
313     i_ret = DialogSend( p_this, p_new );
314
315     if( i_ret != DIALOG_CANCELLED )
316     {
317         *ppsz_usersString = p_new->psz_returned[0]?
318             strdup( p_new->psz_returned[0] ) : NULL;
319     }
320     return i_ret;
321 }
322
323 /**
324  * Hide an interaction dialog
325  *
326  * \param p_dialog the dialog to hide
327  * \return nothing
328  */
329 void intf_UserHide( interaction_dialog_t *p_dialog )
330 {
331     interaction_t *p_interaction = InteractionGet( p_dialog->p_parent );
332     assert( p_interaction );
333
334     vlc_object_lock( p_interaction );
335     p_dialog->i_status = ANSWERED_DIALOG;
336     vlc_cond_signal( &p_interaction->wait );
337     vlc_object_unlock( p_interaction );
338     vlc_object_release( p_interaction );
339 }
340
341 /**
342  * Create the initial interaction object
343  * (should only be used in libvlc_InternalInit, LibVLC private)
344  *
345  * \return a vlc_object_t that should be freed when done.
346  */
347 interaction_t * interaction_Init( libvlc_int_t *p_libvlc )
348 {
349     interaction_t *p_interaction;
350
351     /* Make sure we haven't yet created an interaction object */
352     assert( libvlc_priv(p_libvlc)->p_interaction == NULL );
353
354     p_interaction = vlc_custom_create( VLC_OBJECT(p_libvlc),
355                                        sizeof( *p_interaction ),
356                                        VLC_OBJECT_GENERIC, "interaction" );
357     if( !p_interaction )
358         return NULL;
359
360     vlc_object_attach( p_interaction, p_libvlc );
361     p_interaction->i_dialogs = 0;
362     p_interaction->pp_dialogs = NULL;
363     p_interaction->p_intf = NULL;
364     p_interaction->i_last_id = 0;
365
366     vlc_cond_init( &p_interaction->wait );
367
368     if( vlc_clone( &p_interaction->thread, InteractionLoop, p_interaction,
369                    VLC_THREAD_PRIORITY_LOW ) )
370     {
371         msg_Err( p_interaction, "Interaction control thread creation failed, "
372                  "interaction will not be displayed" );
373         vlc_object_detach( p_interaction );
374         vlc_object_release( p_interaction );
375         return NULL;
376     }
377
378     return p_interaction;
379 }
380
381 void interaction_Destroy( interaction_t *p_interaction )
382 {
383     if( !p_interaction )
384         return;
385
386     vlc_cancel( p_interaction->thread );
387     vlc_join( p_interaction->thread, NULL );
388     vlc_cond_destroy( &p_interaction->wait );
389
390     /* Remove all dialogs - Interfaces must be able to clean up their data */
391     for( int i = p_interaction->i_dialogs -1 ; i >= 0; i-- )
392     {
393         interaction_dialog_t * p_dialog = p_interaction->pp_dialogs[i];
394         DialogDestroy( p_dialog );
395         REMOVE_ELEM( p_interaction->pp_dialogs, p_interaction->i_dialogs, i );
396     }
397     vlc_object_release( p_interaction );
398 }
399
400 static vlc_mutex_t intf_lock = VLC_STATIC_MUTEX;
401
402 int interaction_Register( intf_thread_t *intf )
403 {
404     libvlc_priv_t *priv = libvlc_priv( intf->p_libvlc );
405     int ret = VLC_EGENERIC;
406
407     vlc_mutex_lock( &intf_lock );
408     if( priv->p_interaction_intf == NULL )
409     {   /* Since the interface is responsible for unregistering itself before
410          * it terminates, an object reference is not needed. */
411         priv->p_interaction_intf = intf;
412         ret = VLC_SUCCESS;
413     }
414     vlc_mutex_unlock( &intf_lock );
415     return ret;
416 }
417
418 int interaction_Unregister( intf_thread_t *intf )
419 {
420     libvlc_priv_t *priv = libvlc_priv( intf->p_libvlc );
421     int ret = VLC_EGENERIC;
422
423     vlc_mutex_lock( &intf_lock );
424     if( priv->p_interaction_intf == intf )
425     {
426         priv->p_interaction_intf = NULL;
427         ret = VLC_SUCCESS;
428     }
429     vlc_mutex_unlock( &intf_lock );
430     return ret;
431 }
432
433 /**********************************************************************
434  * The following functions are local
435  **********************************************************************/
436
437 /* Get the interaction object */
438 static interaction_t * InteractionGet( vlc_object_t *p_this )
439 {
440     interaction_t *obj = libvlc_priv(p_this->p_libvlc)->p_interaction;
441     if( obj )
442         vlc_object_hold( obj );
443     return obj;
444 }
445
446
447 /* Look for an interface suitable for interaction, and hold it. */
448 static intf_thread_t *SearchInterface( interaction_t *p_interaction )
449 {
450     libvlc_priv_t *priv = libvlc_priv( p_interaction->p_libvlc );
451     intf_thread_t *intf;
452
453     vlc_mutex_lock( &intf_lock );
454     intf = priv->p_interaction_intf;
455     if( intf != NULL )
456         vlc_object_hold( intf );
457     vlc_mutex_unlock( &intf_lock );
458
459     return intf;
460 }
461
462 /* Destroy a dialog */
463 static void DialogDestroy( interaction_dialog_t *p_dialog )
464 {
465     free( p_dialog->psz_title );
466     free( p_dialog->psz_description );
467     free( p_dialog->psz_default_button );
468     free( p_dialog->psz_alternate_button );
469     free( p_dialog->psz_other_button );
470     free( p_dialog );
471 }
472
473 /* Ask for the dialog to be sent to the user. Wait for answer
474  * if required */
475 static int DialogSend( vlc_object_t *p_this, interaction_dialog_t *p_dialog )
476 {
477     interaction_t *p_interaction = InteractionGet( p_dialog->p_parent );
478
479     if( !p_interaction )
480         return VLC_EGENERIC;
481
482     if( p_this->i_flags & OBJECT_FLAGS_NOINTERACT )
483     {
484         vlc_object_release( p_interaction );
485         return VLC_EGENERIC;
486     }
487
488     if( config_GetInt( p_this, "interact" ) ||
489         p_dialog->i_flags & DIALOG_BLOCKING_ERROR ||
490         p_dialog->i_flags & DIALOG_NONBLOCKING_ERROR )
491     {
492         bool b_found = false;
493         int i;
494         p_dialog->p_interaction = p_interaction;
495         p_dialog->p_parent = p_this;
496
497         /* Check if we have already added this dialog */
498         vlc_object_lock( p_interaction );
499         for( i = 0 ; i< p_interaction->i_dialogs; i++ )
500         {
501             if( p_interaction->pp_dialogs[i] == p_dialog )
502                 b_found = true;
503         }
504         /* Add it to the queue, the main loop will send the orders to the
505          * interface */
506         if( ! b_found )
507         {
508             INSERT_ELEM( p_interaction->pp_dialogs,
509                          p_interaction->i_dialogs,
510                          p_interaction->i_dialogs,
511                          p_dialog );
512         }
513         else
514             p_dialog->i_status = UPDATED_DIALOG;
515
516         if( p_dialog->i_type == INTERACT_DIALOG_TWOWAY ) /* Wait for answer */
517         {
518             vlc_cond_signal( &p_interaction->wait );
519             while( p_dialog->i_status != ANSWERED_DIALOG &&
520                    p_dialog->i_status != HIDING_DIALOG &&
521                    p_dialog->i_status != HIDDEN_DIALOG &&
522                    !p_dialog->p_parent->b_die )
523             {
524                 vlc_object_unlock( p_interaction );
525                 msleep( 100000 );
526                 vlc_object_lock( p_interaction );
527             }
528             if( p_dialog->p_parent->b_die )
529             {
530                 p_dialog->i_return = DIALOG_CANCELLED;
531                 p_dialog->i_status = ANSWERED_DIALOG;
532             }
533             p_dialog->i_flags |= DIALOG_GOT_ANSWER;
534             vlc_cond_signal( &p_interaction->wait );
535             vlc_object_unlock( p_interaction );
536             vlc_object_release( p_interaction );
537             return p_dialog->i_return;
538         }
539         else
540         {
541             /* Pretend we already retrieved the "answer" */
542             p_dialog->i_flags |=  DIALOG_GOT_ANSWER;
543             vlc_cond_signal( &p_interaction->wait );
544             vlc_object_unlock( p_interaction );
545             vlc_object_release( p_interaction );
546             return VLC_SUCCESS;
547         }
548     }
549     else
550     {
551         vlc_object_release( p_interaction );
552         return VLC_EGENERIC;
553     }
554 }
555
556 static void* InteractionLoop( void *p_this )
557 {
558     interaction_t *p_interaction = p_this;
559
560     vlc_object_lock( p_interaction );
561     mutex_cleanup_push( &(vlc_internals(p_interaction)->lock) );
562     for( ;; )
563     {
564         int canc = vlc_savecancel();
565         InteractionManage( p_interaction );
566         vlc_restorecancel( canc );
567
568         vlc_cond_wait( &p_interaction->wait, &(vlc_internals(p_interaction)->lock) );
569     }
570     vlc_cleanup_pop( );
571     assert( 0 );
572 }
573
574 /**
575  * The main interaction processing loop
576  *
577  * \param p_interaction the interaction object
578  * \return nothing
579  */
580
581 static void InteractionManage( interaction_t *p_interaction )
582 {
583     vlc_value_t val;
584     int i_index;
585
586     /* Nothing to do */
587     if( p_interaction->i_dialogs == 0 ) return;
588
589     p_interaction->p_intf = SearchInterface( p_interaction );
590     if( !p_interaction->p_intf )
591     {
592         /* We mark all dialogs as answered with their "default" answer */
593         for( i_index = 0 ; i_index < p_interaction->i_dialogs; i_index ++ )
594         {
595             interaction_dialog_t *p_dialog = p_interaction->pp_dialogs[i_index];
596             p_dialog->i_return = DIALOG_DEFAULT; /* Give default answer */
597
598             /* Pretend we have hidden and destroyed it */
599             if( p_dialog->i_status == HIDDEN_DIALOG )
600                 p_dialog->i_status = DESTROYED_DIALOG;
601             else
602                 p_dialog->i_status = HIDING_DIALOG;
603         }
604     }
605
606     for( i_index = 0 ; i_index < p_interaction->i_dialogs; i_index ++ )
607     {
608         interaction_dialog_t *p_dialog = p_interaction->pp_dialogs[i_index];
609         switch( p_dialog->i_status )
610         {
611         case ANSWERED_DIALOG:
612             /* Ask interface to hide it */
613             p_dialog->i_action = INTERACT_HIDE;
614             val.p_address = p_dialog;
615             if( p_interaction->p_intf )
616                 var_Set( p_interaction->p_intf, "interaction", val );
617             p_dialog->i_status = HIDING_DIALOG;
618             break;
619         case UPDATED_DIALOG:
620             p_dialog->i_action = INTERACT_UPDATE;
621             val.p_address = p_dialog;
622             if( p_interaction->p_intf )
623                 var_Set( p_interaction->p_intf, "interaction", val );
624             p_dialog->i_status = SENT_DIALOG;
625             break;
626         case HIDDEN_DIALOG:
627             if( !(p_dialog->i_flags & DIALOG_GOT_ANSWER) ) break;
628             p_dialog->i_action = INTERACT_DESTROY;
629             val.p_address = p_dialog;
630             if( p_interaction->p_intf )
631                 var_Set( p_interaction->p_intf, "interaction", val );
632             break;
633         case DESTROYED_DIALOG:
634             /* Interface has now destroyed it, remove it */
635             REMOVE_ELEM( p_interaction->pp_dialogs, p_interaction->i_dialogs,
636                          i_index);
637             i_index--;
638             DialogDestroy( p_dialog );
639             break;
640         case NEW_DIALOG:
641             /* This is truly a new dialog, send it. */
642
643             p_dialog->i_action = INTERACT_NEW;
644             val.p_address = p_dialog;
645             if( p_interaction->p_intf )
646                 var_Set( p_interaction->p_intf, "interaction", val );
647             p_dialog->i_status = SENT_DIALOG;
648             break;
649         }
650     }
651
652     if( p_interaction->p_intf )
653         vlc_object_release( p_interaction->p_intf );
654 }