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