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