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