]> git.sesse.net Git - vlc/blob - src/interface/interaction.c
Interaction: remove (buggy and useless) dialog IDs
[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_this           Parent vlc_object
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( vlc_object_t *p_this,
215                             interaction_dialog_t *p_dialog,
216                             const char *psz_status, float f_pos, int i_time )
217 {
218     interaction_t *p_interaction = InteractionGet( p_this );
219
220     if( !p_interaction ) return;
221
222     vlc_object_lock( p_interaction );
223     free( p_dialog->psz_description );
224     p_dialog->psz_description = strdup( psz_status );
225
226     p_dialog->val.f_float = f_pos;
227     p_dialog->i_timeToGo = i_time;
228
229     p_dialog->i_status = UPDATED_DIALOG;
230
231     vlc_cond_signal( &p_interaction->wait );
232     vlc_object_unlock( p_interaction );
233     vlc_object_release( p_interaction );
234 }
235
236 /**
237  * Helper function to communicate dialogue cancellations between the
238  * interface module and the caller
239  *
240  * \param p_this           Parent vlc_object
241  * \param p_dialog         Dialog
242  * \return                 Either true or false
243  */
244 bool __intf_UserProgressIsCancelled( vlc_object_t *p_this,
245                                      interaction_dialog_t *p_dialog )
246 {
247     interaction_t *p_interaction = InteractionGet( p_this );
248     bool b_cancel;
249
250     if( !p_interaction ) return true;
251
252     vlc_object_lock( p_interaction );
253     b_cancel = p_dialog->b_cancelled;
254     vlc_object_unlock( p_interaction );
255     vlc_object_release( p_interaction );
256     return b_cancel;
257 }
258
259 /**
260  * Helper function to make a login/password dialogue
261  *
262  * \param p_this           Parent vlc_object
263  * \param psz_title        Title for the dialog
264  * \param psz_description  A description
265  * \param ppsz_login       Returned login
266  * \param ppsz_password    Returned password
267  * \return                 Clicked button code
268  */
269 int __intf_UserLoginPassword( vlc_object_t *p_this,
270         const char *psz_title,
271         const char *psz_description,
272         char **ppsz_login,
273         char **ppsz_password )
274 {
275     int i_ret;
276     DIALOG_INIT( TWOWAY, VLC_EGENERIC );
277     p_new->i_type = INTERACT_DIALOG_TWOWAY;
278     p_new->psz_title = strdup( psz_title );
279     p_new->psz_description = strdup( psz_description );
280     p_new->psz_default_button = strdup( _("OK" ) );
281     p_new->psz_alternate_button = strdup( _("Cancel" ) );
282
283     p_new->i_flags = DIALOG_LOGIN_PW_OK_CANCEL;
284
285     i_ret = DialogSend( p_this, p_new );
286
287     if( i_ret != DIALOG_CANCELLED && i_ret != VLC_EGENERIC )
288     {
289         *ppsz_login = p_new->psz_returned[0]?
290             strdup( p_new->psz_returned[0] ) : NULL;
291         *ppsz_password = p_new->psz_returned[1]?
292             strdup( p_new->psz_returned[1] ) : NULL;
293     }
294     return i_ret;
295 }
296
297 /**
298  * Helper function to make a dialogue asking the user for !password string
299  *
300  * \param p_this           Parent vlc_object
301  * \param psz_title        Title for the dialog
302  * \param psz_description  A description
303  * \param ppsz_usersString Returned login
304  * \return                 Clicked button code
305  */
306 int __intf_UserStringInput( vlc_object_t *p_this,
307         const char *psz_title,
308         const char *psz_description,
309         char **ppsz_usersString )
310 {
311     int i_ret;
312     DIALOG_INIT( TWOWAY, VLC_EGENERIC );
313     p_new->i_type = INTERACT_DIALOG_TWOWAY;
314     p_new->psz_title = strdup( psz_title );
315     p_new->psz_description = strdup( psz_description );
316
317     p_new->i_flags = DIALOG_PSZ_INPUT_OK_CANCEL;
318
319     i_ret = DialogSend( p_this, p_new );
320
321     if( i_ret != DIALOG_CANCELLED )
322     {
323         *ppsz_usersString = p_new->psz_returned[0]?
324             strdup( p_new->psz_returned[0] ) : NULL;
325     }
326     return i_ret;
327 }
328
329 /**
330  * Hide an interaction dialog
331  *
332  * \param p_this the parent vlc object
333  * \param p_dialog the dialog to hide
334  * \return nothing
335  */
336 void __intf_UserHide( vlc_object_t *p_this, interaction_dialog_t *p_dialog )
337 {
338     interaction_t *p_interaction = InteractionGet( p_this );
339
340     if( !p_interaction ) return;
341
342     vlc_object_lock( p_interaction );
343     p_dialog->i_status = ANSWERED_DIALOG;
344     vlc_cond_signal( &p_interaction->wait );
345     vlc_object_unlock( p_interaction );
346     vlc_object_release( p_interaction );
347 }
348
349 /**
350  * Create the initial interaction object
351  * (should only be used in libvlc_InternalInit, LibVLC private)
352  *
353  * \return a vlc_object_t that should be freed when done.
354  */
355 interaction_t * interaction_Init( libvlc_int_t *p_libvlc )
356 {
357     interaction_t *p_interaction;
358
359     /* Make sure we haven't yet created an interaction object */
360     assert( libvlc_priv(p_libvlc)->p_interaction == NULL );
361
362     p_interaction = vlc_custom_create( VLC_OBJECT(p_libvlc),
363                                        sizeof( *p_interaction ),
364                                        VLC_OBJECT_GENERIC, "interaction" );
365     if( !p_interaction )
366         return NULL;
367
368     vlc_object_attach( p_interaction, p_libvlc );
369     p_interaction->i_dialogs = 0;
370     p_interaction->pp_dialogs = NULL;
371     p_interaction->p_intf = NULL;
372     p_interaction->i_last_id = 0;
373
374     vlc_cond_init( &p_interaction->wait );
375
376     if( vlc_clone( &p_interaction->thread, InteractionLoop, p_interaction,
377                    VLC_THREAD_PRIORITY_LOW ) )
378     {
379         msg_Err( p_interaction, "Interaction control thread creation failed, "
380                  "interaction will not be displayed" );
381         vlc_object_detach( p_interaction );
382         vlc_object_release( p_interaction );
383         return NULL;
384     }
385
386     return p_interaction;
387 }
388
389 void interaction_Destroy( interaction_t *p_interaction )
390 {
391     if( !p_interaction )
392         return;
393
394     vlc_cancel( p_interaction->thread );
395     vlc_join( p_interaction->thread, NULL );
396     vlc_cond_destroy( &p_interaction->wait );
397
398     /* Remove all dialogs - Interfaces must be able to clean up their data */
399     for( int i = p_interaction->i_dialogs -1 ; i >= 0; i-- )
400     {
401         interaction_dialog_t * p_dialog = p_interaction->pp_dialogs[i];
402         DialogDestroy( p_dialog );
403         REMOVE_ELEM( p_interaction->pp_dialogs, p_interaction->i_dialogs, i );
404     }
405     vlc_object_release( p_interaction );
406 }
407
408 static vlc_mutex_t intf_lock = VLC_STATIC_MUTEX;
409
410 int interaction_Register( intf_thread_t *intf )
411 {
412     libvlc_priv_t *priv = libvlc_priv( intf->p_libvlc );
413     int ret = VLC_EGENERIC;
414
415     vlc_mutex_lock( &intf_lock );
416     if( priv->p_interaction_intf == NULL )
417     {   /* Since the interface is responsible for unregistering itself before
418          * it terminates, an object reference is not needed. */
419         priv->p_interaction_intf = intf;
420         ret = VLC_SUCCESS;
421     }
422     vlc_mutex_unlock( &intf_lock );
423     return ret;
424 }
425
426 int interaction_Unregister( intf_thread_t *intf )
427 {
428     libvlc_priv_t *priv = libvlc_priv( intf->p_libvlc );
429     int ret = VLC_EGENERIC;
430
431     vlc_mutex_lock( &intf_lock );
432     if( priv->p_interaction_intf == intf )
433     {
434         priv->p_interaction_intf = NULL;
435         ret = VLC_SUCCESS;
436     }
437     vlc_mutex_unlock( &intf_lock );
438     return ret;
439 }
440
441 /**********************************************************************
442  * The following functions are local
443  **********************************************************************/
444
445 /* Get the interaction object. Create it if needed */
446 static interaction_t * InteractionGet( vlc_object_t *p_this )
447 {
448     interaction_t *obj = libvlc_priv(p_this->p_libvlc)->p_interaction;
449     if( obj )
450         vlc_object_hold( obj );
451     return obj;
452 }
453
454
455 /* Look for an interface suitable for interaction, and hold it. */
456 static intf_thread_t *SearchInterface( interaction_t *p_interaction )
457 {
458     libvlc_priv_t *priv = libvlc_priv( p_interaction->p_libvlc );
459     intf_thread_t *intf;
460
461     vlc_mutex_lock( &intf_lock );
462     intf = priv->p_interaction_intf;
463     if( intf != NULL )
464         vlc_object_hold( intf );
465     vlc_mutex_unlock( &intf_lock );
466
467     return intf;
468 }
469
470 /* Destroy a dialog */
471 static void DialogDestroy( interaction_dialog_t *p_dialog )
472 {
473     free( p_dialog->psz_title );
474     free( p_dialog->psz_description );
475     free( p_dialog->psz_default_button );
476     free( p_dialog->psz_alternate_button );
477     free( p_dialog->psz_other_button );
478     free( p_dialog );
479 }
480
481 /* Ask for the dialog to be sent to the user. Wait for answer
482  * if required */
483 static int DialogSend( vlc_object_t *p_this, interaction_dialog_t *p_dialog )
484 {
485     interaction_t *p_interaction = InteractionGet( p_this );
486
487     if( !p_interaction )
488         return VLC_EGENERIC;
489
490     if( p_this->i_flags & OBJECT_FLAGS_NOINTERACT )
491     {
492         vlc_object_release( p_interaction );
493         return VLC_EGENERIC;
494     }
495
496     if( config_GetInt( p_this, "interact" ) ||
497         p_dialog->i_flags & DIALOG_BLOCKING_ERROR ||
498         p_dialog->i_flags & DIALOG_NONBLOCKING_ERROR )
499     {
500         bool b_found = false;
501         int i;
502         p_dialog->p_interaction = p_interaction;
503         p_dialog->p_parent = p_this;
504
505         /* Check if we have already added this dialog */
506         vlc_object_lock( p_interaction );
507         for( i = 0 ; i< p_interaction->i_dialogs; i++ )
508         {
509             if( p_interaction->pp_dialogs[i] == p_dialog )
510                 b_found = true;
511         }
512         /* Add it to the queue, the main loop will send the orders to the
513          * interface */
514         if( ! b_found )
515         {
516             INSERT_ELEM( p_interaction->pp_dialogs,
517                          p_interaction->i_dialogs,
518                          p_interaction->i_dialogs,
519                          p_dialog );
520         }
521         else
522             p_dialog->i_status = UPDATED_DIALOG;
523
524         if( p_dialog->i_type == INTERACT_DIALOG_TWOWAY ) /* Wait for answer */
525         {
526             vlc_cond_signal( &p_interaction->wait );
527             while( p_dialog->i_status != ANSWERED_DIALOG &&
528                    p_dialog->i_status != HIDING_DIALOG &&
529                    p_dialog->i_status != HIDDEN_DIALOG &&
530                    !p_dialog->p_parent->b_die )
531             {
532                 vlc_object_unlock( p_interaction );
533                 msleep( 100000 );
534                 vlc_object_lock( p_interaction );
535             }
536             if( p_dialog->p_parent->b_die )
537             {
538                 p_dialog->i_return = DIALOG_CANCELLED;
539                 p_dialog->i_status = ANSWERED_DIALOG;
540             }
541             p_dialog->i_flags |= DIALOG_GOT_ANSWER;
542             vlc_cond_signal( &p_interaction->wait );
543             vlc_object_unlock( p_interaction );
544             vlc_object_release( p_interaction );
545             return p_dialog->i_return;
546         }
547         else
548         {
549             /* Pretend we already retrieved the "answer" */
550             p_dialog->i_flags |=  DIALOG_GOT_ANSWER;
551             vlc_cond_signal( &p_interaction->wait );
552             vlc_object_unlock( p_interaction );
553             vlc_object_release( p_interaction );
554             return VLC_SUCCESS;
555         }
556     }
557     else
558     {
559         vlc_object_release( p_interaction );
560         return VLC_EGENERIC;
561     }
562 }
563
564 static void* InteractionLoop( void *p_this )
565 {
566     interaction_t *p_interaction = p_this;
567
568     vlc_object_lock( p_interaction );
569     mutex_cleanup_push( &(vlc_internals(p_interaction)->lock) );
570     for( ;; )
571     {
572         int canc = vlc_savecancel();
573         InteractionManage( p_interaction );
574         vlc_restorecancel( canc );
575
576         vlc_cond_wait( &p_interaction->wait, &(vlc_internals(p_interaction)->lock) );
577     }
578     vlc_cleanup_pop( );
579     assert( 0 );
580 }
581
582 /**
583  * The main interaction processing loop
584  *
585  * \param p_interaction the interaction object
586  * \return nothing
587  */
588
589 static void InteractionManage( interaction_t *p_interaction )
590 {
591     vlc_value_t val;
592     int i_index;
593
594     /* Nothing to do */
595     if( p_interaction->i_dialogs == 0 ) return;
596
597     p_interaction->p_intf = SearchInterface( p_interaction );
598     if( !p_interaction->p_intf )
599     {
600         /* We mark all dialogs as answered with their "default" answer */
601         for( i_index = 0 ; i_index < p_interaction->i_dialogs; i_index ++ )
602         {
603             interaction_dialog_t *p_dialog = p_interaction->pp_dialogs[i_index];
604             p_dialog->i_return = DIALOG_DEFAULT; /* Give default answer */
605
606             /* Pretend we have hidden and destroyed it */
607             if( p_dialog->i_status == HIDDEN_DIALOG )
608                 p_dialog->i_status = DESTROYED_DIALOG;
609             else
610                 p_dialog->i_status = HIDING_DIALOG;
611         }
612     }
613
614     for( i_index = 0 ; i_index < p_interaction->i_dialogs; i_index ++ )
615     {
616         interaction_dialog_t *p_dialog = p_interaction->pp_dialogs[i_index];
617         switch( p_dialog->i_status )
618         {
619         case ANSWERED_DIALOG:
620             /* Ask interface to hide it */
621             p_dialog->i_action = INTERACT_HIDE;
622             val.p_address = p_dialog;
623             if( p_interaction->p_intf )
624                 var_Set( p_interaction->p_intf, "interaction", val );
625             p_dialog->i_status = HIDING_DIALOG;
626             break;
627         case UPDATED_DIALOG:
628             p_dialog->i_action = INTERACT_UPDATE;
629             val.p_address = p_dialog;
630             if( p_interaction->p_intf )
631                 var_Set( p_interaction->p_intf, "interaction", val );
632             p_dialog->i_status = SENT_DIALOG;
633             break;
634         case HIDDEN_DIALOG:
635             if( !(p_dialog->i_flags & DIALOG_GOT_ANSWER) ) break;
636             p_dialog->i_action = INTERACT_DESTROY;
637             val.p_address = p_dialog;
638             if( p_interaction->p_intf )
639                 var_Set( p_interaction->p_intf, "interaction", val );
640             break;
641         case DESTROYED_DIALOG:
642             /* Interface has now destroyed it, remove it */
643             REMOVE_ELEM( p_interaction->pp_dialogs, p_interaction->i_dialogs,
644                          i_index);
645             i_index--;
646             DialogDestroy( p_dialog );
647             break;
648         case NEW_DIALOG:
649             /* This is truly a new dialog, send it. */
650
651             p_dialog->i_action = INTERACT_NEW;
652             val.p_address = p_dialog;
653             if( p_interaction->p_intf )
654                 var_Set( p_interaction->p_intf, "interaction", val );
655             p_dialog->i_status = SENT_DIALOG;
656             break;
657         }
658     }
659
660     if( p_interaction->p_intf )
661         vlc_object_release( p_interaction->p_intf );
662 }