]> git.sesse.net Git - vlc/blob - src/interface/interaction.c
dialog_Question: simple thread-safe replacement for intf_UserYesNo
[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     if( DialogSend( p_new ) == VLC_EGENERIC )
159     {
160         DialogDestroy( p_new );
161         return NULL;
162     }
163     return p_new;
164 }
165
166 /**
167  * Update a progress bar in a dialogue
168  *
169  * \param p_dialog         Dialog
170  * \param psz_status       New status
171  * \param f_position       New position (0.0->100.0)
172  * \param i_timeToGo       Time (in sec) to go until process is finished
173  * \return                 nothing
174  */
175 void intf_ProgressUpdate( interaction_dialog_t *p_dialog,
176                             const char *psz_status, float f_pos, int i_time )
177 {
178     interaction_t *p_interaction = InteractionGet( p_dialog->p_parent );
179     assert( p_interaction );
180
181     vlc_mutex_lock( &p_interaction->lock );
182     free( p_dialog->psz_description );
183     p_dialog->psz_description = strdup( psz_status );
184
185     p_dialog->val.f_float = f_pos;
186     p_dialog->i_timeToGo = i_time;
187
188     p_dialog->i_status = UPDATED_DIALOG;
189
190     vlc_cond_signal( &p_interaction->wait );
191     vlc_mutex_unlock( &p_interaction->lock );
192     vlc_object_release( p_interaction );
193 }
194
195 /**
196  * Helper function to communicate dialogue cancellations between the
197  * interface module and the caller
198  *
199  * \param p_dialog         Dialog
200  * \return                 Either true or false
201  */
202 bool intf_ProgressIsCancelled( interaction_dialog_t *p_dialog )
203 {
204     interaction_t *p_interaction = InteractionGet( p_dialog->p_parent );
205     bool b_cancel;
206
207     assert( p_interaction );
208     vlc_mutex_lock( &p_interaction->lock );
209     b_cancel = p_dialog->b_cancelled;
210     vlc_mutex_unlock( &p_interaction->lock );
211     vlc_object_release( p_interaction );
212     return b_cancel;
213 }
214
215 /**
216  * Helper function to make a dialogue asking the user for !password string
217  *
218  * \param p_this           Parent vlc_object
219  * \param psz_title        Title for the dialog
220  * \param psz_description  A description
221  * \param ppsz_usersString Returned login
222  * \return                 Clicked button code
223  */
224 int __intf_UserStringInput( vlc_object_t *p_this,
225         const char *psz_title,
226         const char *psz_description,
227         char **ppsz_usersString )
228 {
229     int i_ret;
230     DIALOG_INIT( TWOWAY, VLC_EGENERIC );
231     p_new->i_type = INTERACT_DIALOG_TWOWAY;
232     p_new->psz_title = strdup( psz_title );
233     p_new->psz_description = strdup( psz_description );
234
235     p_new->i_flags = DIALOG_PSZ_INPUT_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_usersString = p_new->psz_returned[0]?
244             strdup( p_new->psz_returned[0] ) : NULL;
245     }
246     return i_ret;
247 }
248
249 /**
250  * Hide an interaction dialog
251  *
252  * \param p_dialog the dialog to hide
253  * \return nothing
254  */
255 void intf_UserHide( interaction_dialog_t *p_dialog )
256 {
257     interaction_t *p_interaction = InteractionGet( p_dialog->p_parent );
258     assert( p_interaction );
259
260     vlc_mutex_lock( &p_interaction->lock );
261     p_dialog->i_status = ANSWERED_DIALOG;
262     vlc_cond_signal( &p_interaction->wait );
263     vlc_mutex_unlock( &p_interaction->lock );
264     vlc_object_release( p_interaction );
265 }
266
267 /**
268  * Create the initial interaction object
269  * (should only be used in libvlc_InternalInit, LibVLC private)
270  *
271  * \return a vlc_object_t that should be freed when done.
272  */
273 interaction_t * interaction_Init( libvlc_int_t *p_libvlc )
274 {
275     interaction_t *p_interaction;
276
277     /* Make sure we haven't yet created an interaction object */
278     assert( libvlc_priv(p_libvlc)->p_interaction == NULL );
279
280     p_interaction = vlc_custom_create( VLC_OBJECT(p_libvlc),
281                                        sizeof( *p_interaction ),
282                                        VLC_OBJECT_GENERIC, "interaction" );
283     if( !p_interaction )
284         return NULL;
285
286     vlc_object_attach( p_interaction, p_libvlc );
287     p_interaction->i_dialogs = 0;
288     p_interaction->pp_dialogs = NULL;
289     p_interaction->p_intf = NULL;
290
291     vlc_mutex_init( &p_interaction->lock );
292     vlc_cond_init( &p_interaction->wait );
293
294     if( vlc_clone( &p_interaction->thread, InteractionLoop, p_interaction,
295                    VLC_THREAD_PRIORITY_LOW ) )
296     {
297         msg_Err( p_interaction, "Interaction control thread creation failed, "
298                  "interaction will not be displayed" );
299         vlc_object_detach( p_interaction );
300         vlc_object_release( p_interaction );
301         return NULL;
302     }
303
304     return p_interaction;
305 }
306
307 void interaction_Destroy( interaction_t *p_interaction )
308 {
309     if( !p_interaction )
310         return;
311
312     vlc_cancel( p_interaction->thread );
313     vlc_join( p_interaction->thread, NULL );
314     vlc_cond_destroy( &p_interaction->wait );
315     vlc_mutex_destroy( &p_interaction->lock );
316
317     /* Remove all dialogs - Interfaces must be able to clean up their data */
318     for( int i = p_interaction->i_dialogs -1 ; i >= 0; i-- )
319     {
320         interaction_dialog_t * p_dialog = p_interaction->pp_dialogs[i];
321         DialogDestroy( p_dialog );
322         REMOVE_ELEM( p_interaction->pp_dialogs, p_interaction->i_dialogs, i );
323     }
324     vlc_object_release( p_interaction );
325 }
326
327 static vlc_mutex_t intf_lock = VLC_STATIC_MUTEX;
328
329 int interaction_Register( intf_thread_t *intf )
330 {
331     libvlc_priv_t *priv = libvlc_priv( intf->p_libvlc );
332     int ret = VLC_EGENERIC;
333
334     vlc_mutex_lock( &intf_lock );
335     if( priv->p_interaction_intf == NULL )
336     {   /* Since the interface is responsible for unregistering itself before
337          * it terminates, an object reference is not needed. */
338         priv->p_interaction_intf = intf;
339         ret = VLC_SUCCESS;
340     }
341     vlc_mutex_unlock( &intf_lock );
342     return ret;
343 }
344
345 int interaction_Unregister( intf_thread_t *intf )
346 {
347     libvlc_priv_t *priv = libvlc_priv( intf->p_libvlc );
348     int ret = VLC_EGENERIC;
349
350     vlc_mutex_lock( &intf_lock );
351     if( priv->p_interaction_intf == intf )
352     {
353         priv->p_interaction_intf = NULL;
354         ret = VLC_SUCCESS;
355     }
356     vlc_mutex_unlock( &intf_lock );
357     return ret;
358 }
359
360 /**********************************************************************
361  * The following functions are local
362  **********************************************************************/
363
364 /* Get the interaction object */
365 static interaction_t * InteractionGet( vlc_object_t *p_this )
366 {
367     interaction_t *obj = libvlc_priv(p_this->p_libvlc)->p_interaction;
368     if( obj )
369         vlc_object_hold( obj );
370     return obj;
371 }
372
373
374 /* Look for an interface suitable for interaction, and hold it. */
375 static intf_thread_t *SearchInterface( interaction_t *p_interaction )
376 {
377     libvlc_priv_t *priv = libvlc_priv( p_interaction->p_libvlc );
378     intf_thread_t *intf;
379
380     vlc_mutex_lock( &intf_lock );
381     intf = priv->p_interaction_intf;
382     if( intf != NULL )
383         vlc_object_hold( intf );
384     vlc_mutex_unlock( &intf_lock );
385
386     return intf;
387 }
388
389 /* Destroy a dialog */
390 static void DialogDestroy( interaction_dialog_t *p_dialog )
391 {
392     free( p_dialog->psz_title );
393     free( p_dialog->psz_description );
394     free( p_dialog->psz_default_button );
395     free( p_dialog->psz_alternate_button );
396     free( p_dialog->psz_other_button );
397     vlc_object_release( p_dialog->p_parent );
398     free( p_dialog );
399 }
400
401 /* Ask for the dialog to be sent to the user. Wait for answer
402  * if required */
403 static int DialogSend( interaction_dialog_t *p_dialog )
404 {
405     interaction_t *p_interaction;
406     intf_thread_t *p_intf;
407
408     if( ( p_dialog->p_parent->i_flags & OBJECT_FLAGS_NOINTERACT )
409      || !config_GetInt( p_interaction, "interact" ) )
410         return VLC_EGENERIC;
411
412     p_interaction = InteractionGet( p_dialog->p_parent );
413     if( !p_interaction )
414         return VLC_EGENERIC;
415
416     p_dialog->p_lock = &p_interaction->lock;
417
418     p_intf = SearchInterface( p_interaction );
419     if( p_intf == NULL )
420     {
421         p_dialog->i_return = DIALOG_DEFAULT; /* Give default answer */
422
423         /* Pretend we have hidden and destroyed it */
424         p_dialog->i_status = HIDING_DIALOG;
425         vlc_object_release( p_interaction );
426         return VLC_SUCCESS;
427     }
428     p_dialog->p_interface = p_intf;
429
430     p_dialog->i_action = INTERACT_NEW;
431     var_SetAddress( p_dialog->p_interface, "interaction", p_dialog );
432
433     /* Check if we have already added this dialog */
434     vlc_mutex_lock( &p_interaction->lock );
435     /* Add it to the queue, the main loop will send the orders to the
436      * interface */
437     INSERT_ELEM( p_interaction->pp_dialogs, p_interaction->i_dialogs,
438                  p_interaction->i_dialogs,  p_dialog );
439
440     if( p_dialog->i_type == INTERACT_DIALOG_TWOWAY ) /* Wait for answer */
441     {
442         vlc_cond_signal( &p_interaction->wait );
443         while( p_dialog->i_status != ANSWERED_DIALOG &&
444                p_dialog->i_status != HIDING_DIALOG &&
445                p_dialog->i_status != HIDDEN_DIALOG &&
446                !p_dialog->p_parent->b_die )
447         {
448             vlc_mutex_unlock( &p_interaction->lock );
449             msleep( 100000 );
450             vlc_mutex_lock( &p_interaction->lock );
451         }
452         if( p_dialog->p_parent->b_die )
453         {
454             p_dialog->i_return = DIALOG_CANCELLED;
455             p_dialog->i_status = ANSWERED_DIALOG;
456         }
457     }
458     p_dialog->i_flags |= DIALOG_GOT_ANSWER;
459     vlc_cond_signal( &p_interaction->wait );
460     vlc_mutex_unlock( &p_interaction->lock );
461     vlc_object_release( p_interaction );
462     if( p_dialog->i_type == INTERACT_DIALOG_TWOWAY )
463         return p_dialog->i_return;
464     else
465         return VLC_SUCCESS;
466 }
467
468 static void* InteractionLoop( void *p_this )
469 {
470     interaction_t *p_interaction = p_this;
471
472     vlc_mutex_lock( &p_interaction->lock );
473     mutex_cleanup_push( &p_interaction->lock );
474     for( ;; )
475     {
476         int canc = vlc_savecancel();
477         InteractionManage( p_interaction );
478         vlc_restorecancel( canc );
479
480         vlc_cond_wait( &p_interaction->wait, &p_interaction->lock );
481     }
482     vlc_cleanup_pop( );
483     assert( 0 );
484 }
485
486 /**
487  * The main interaction processing loop
488  *
489  * \param p_interaction the interaction object
490  * \return nothing
491  */
492
493 static void InteractionManage( interaction_t *p_interaction )
494 {
495     vlc_value_t val;
496     int i_index;
497
498     for( i_index = 0 ; i_index < p_interaction->i_dialogs; i_index ++ )
499     {
500         interaction_dialog_t *p_dialog = p_interaction->pp_dialogs[i_index];
501         switch( p_dialog->i_status )
502         {
503         case ANSWERED_DIALOG:
504             /* Ask interface to hide it */
505             p_dialog->i_action = INTERACT_HIDE;
506             val.p_address = p_dialog;
507             var_Set( p_dialog->p_interface, "interaction", val );
508             p_dialog->i_status = HIDING_DIALOG;
509             break;
510         case UPDATED_DIALOG:
511             p_dialog->i_action = INTERACT_UPDATE;
512             val.p_address = p_dialog;
513             var_Set( p_dialog->p_interface, "interaction", val );
514             p_dialog->i_status = SENT_DIALOG;
515             break;
516         case HIDDEN_DIALOG:
517             if( !(p_dialog->i_flags & DIALOG_GOT_ANSWER) ) break;
518             p_dialog->i_action = INTERACT_DESTROY;
519             val.p_address = p_dialog;
520             var_Set( p_dialog->p_interface, "interaction", val );
521             break;
522         case DESTROYED_DIALOG:
523             /* Interface has now destroyed it, remove it */
524             REMOVE_ELEM( p_interaction->pp_dialogs, p_interaction->i_dialogs,
525                          i_index);
526             i_index--;
527             DialogDestroy( p_dialog );
528             break;
529         }
530     }
531 }