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