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