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