]> git.sesse.net Git - vlc/blob - modules/access/dvdnav.c
modules: use vlc_object_alive()
[vlc] / modules / access / dvdnav.c
1 /*****************************************************************************
2  * dvdnav.c: DVD module using the dvdnav library.
3  *****************************************************************************
4  * Copyright (C) 2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Laurent Aimar <fenrir@via.ecp.fr>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #ifdef HAVE_CONFIG_H
29 # include "config.h"
30 #endif
31
32 #include <vlc_common.h>
33 #include <vlc_plugin.h>
34 #include <vlc_input.h>
35 #include <vlc_access.h>
36 #include <vlc_demux.h>
37 #include <vlc_charset.h>
38
39 #include <vlc_interface.h>
40
41 #ifdef HAVE_UNISTD_H
42 #   include <unistd.h>
43 #endif
44 #ifdef HAVE_SYS_TYPES_H
45 #   include <sys/types.h>
46 #endif
47 #ifdef HAVE_SYS_STAT_H
48 #   include <sys/stat.h>
49 #endif
50 #ifdef HAVE_FCNTL_H
51 #   include <fcntl.h>
52 #endif
53
54 #include "vlc_keys.h"
55 #include "vlc_iso_lang.h"
56
57 /* FIXME we should find a better way than including that */
58 #include "../../src/text/iso-639_def.h"
59
60
61 #include <dvdnav/dvdnav.h>
62
63 #include "../demux/ps.h"
64
65 /*****************************************************************************
66  * Module descriptor
67  *****************************************************************************/
68 #define ANGLE_TEXT N_("DVD angle")
69 #define ANGLE_LONGTEXT N_( \
70      "Default DVD angle." )
71
72 #define CACHING_TEXT N_("Caching value in ms")
73 #define CACHING_LONGTEXT N_( \
74     "Caching value for DVDs. This "\
75     "value should be set in milliseconds." )
76 #define MENU_TEXT N_("Start directly in menu")
77 #define MENU_LONGTEXT N_( \
78     "Start the DVD directly in the main menu. This "\
79     "will try to skip all the useless warning introductions." )
80
81 #define LANGUAGE_DEFAULT ("en")
82
83 static int  Open ( vlc_object_t * );
84 static void Close( vlc_object_t * );
85
86 vlc_module_begin();
87     set_shortname( N_("DVD with menus") );
88     set_description( N_("DVDnav Input") );
89     set_category( CAT_INPUT );
90     set_subcategory( SUBCAT_INPUT_ACCESS );
91     add_integer( "dvdnav-angle", 1, NULL, ANGLE_TEXT,
92         ANGLE_LONGTEXT, false );
93     add_integer( "dvdnav-caching", DEFAULT_PTS_DELAY / 1000, NULL,
94         CACHING_TEXT, CACHING_LONGTEXT, true );
95     add_bool( "dvdnav-menu", true, NULL,
96         MENU_TEXT, MENU_LONGTEXT, false );
97     set_capability( "access_demux", 5 );
98     add_shortcut( "dvd" );
99     add_shortcut( "dvdnav" );
100     set_callbacks( Open, Close );
101 vlc_module_end();
102
103 /* Shall we use libdvdnav's read ahead cache? */
104 #define DVD_READ_CACHE 1
105
106 /*****************************************************************************
107  * Local prototypes
108  *****************************************************************************/
109 typedef struct
110 {
111     VLC_COMMON_MEMBERS
112
113     demux_t        *p_demux;
114     vlc_mutex_t     lock;
115
116     bool      b_moved;
117     bool      b_clicked;
118     int             i_key_action;
119
120     bool      b_still;
121     int64_t         i_still_end;
122
123 } event_thread_t;
124
125 static int EventThread( vlc_object_t * );
126
127 struct demux_sys_t
128 {
129     dvdnav_t    *dvdnav;
130
131     /* track */
132     ps_track_t  tk[PS_TK_COUNT];
133     int         i_mux_rate;
134
135     /* for spu variables */
136     input_thread_t *p_input;
137
138     /* event */
139     event_thread_t *p_ev;
140
141     /* palette for menus */
142     uint32_t clut[16];
143     uint8_t  palette[4][4];
144     bool b_spu_change;
145
146     /* */
147     int i_aspect;
148
149     int           i_title;
150     input_title_t **title;
151
152     /* lenght of program group chain */
153     mtime_t     i_pgc_length;
154 };
155
156 static int Control( demux_t *, int, va_list );
157 static int Demux( demux_t * );
158 static int DemuxBlock( demux_t *, uint8_t *, int );
159
160 static void DemuxTitles( demux_t * );
161 static void ESSubtitleUpdate( demux_t * );
162 static void ButtonUpdate( demux_t *, bool );
163
164 static void ESNew( demux_t *, int );
165 static int ProbeDVD( demux_t *, char * );
166
167 static char *DemuxGetLanguageCode( demux_t *p_demux, const char *psz_var );
168
169 /*****************************************************************************
170  * DemuxOpen:
171  *****************************************************************************/
172 static int Open( vlc_object_t *p_this )
173 {
174     demux_t     *p_demux = (demux_t*)p_this;
175     demux_sys_t *p_sys;
176     dvdnav_t    *p_dvdnav;
177     int         i_angle;
178     char        *psz_name;
179     char        *psz_code;
180     vlc_value_t val;
181
182     if( !p_demux->psz_path || !*p_demux->psz_path )
183     {
184         /* Only when selected */
185         if( !p_this->b_force ) return VLC_EGENERIC;
186
187         psz_name = var_CreateGetString( p_this, "dvd" );
188         if( !psz_name )
189         {
190             psz_name = strdup("");
191         }
192     }
193     else
194         psz_name = ToLocaleDup( p_demux->psz_path );
195
196 #ifdef WIN32
197     if( psz_name[0] && psz_name[1] == ':' &&
198         psz_name[2] == '\\' && psz_name[3] == '\0' ) psz_name[2] = '\0';
199 #endif
200
201     /* Try some simple probing to avoid going through dvdnav_open too often */
202     if( ProbeDVD( p_demux, psz_name ) != VLC_SUCCESS )
203     {
204         free( psz_name );
205         return VLC_EGENERIC;
206     }
207
208     /* Open dvdnav */
209     if( dvdnav_open( &p_dvdnav, psz_name ) != DVDNAV_STATUS_OK )
210     {
211         msg_Warn( p_demux, "cannot open dvdnav" );
212         free( psz_name );
213         return VLC_EGENERIC;
214     }
215     free( psz_name );
216
217     /* Fill p_demux field */
218     DEMUX_INIT_COMMON(); p_sys = p_demux->p_sys;
219     p_sys->dvdnav = p_dvdnav;
220
221     ps_track_init( p_sys->tk );
222     p_sys->i_aspect = -1;
223     p_sys->i_mux_rate = 0;
224     p_sys->i_pgc_length = 0;
225     p_sys->b_spu_change = false;
226
227     if( 1 )
228     {
229         // Hack for libdvdnav CVS.
230         // Without it dvdnav_get_number_of_titles() fails.
231         // Remove when fixed in libdvdnav CVS.
232         uint8_t buffer[DVD_VIDEO_LB_LEN];
233         int i_event, i_len;
234
235         if( dvdnav_get_next_block( p_sys->dvdnav, buffer, &i_event, &i_len )
236               == DVDNAV_STATUS_ERR )
237         {
238             msg_Warn( p_demux, "dvdnav_get_next_block failed" );
239         }
240
241         dvdnav_sector_search( p_sys->dvdnav, 0, SEEK_SET );
242     }
243
244     /* Configure dvdnav */
245     if( dvdnav_set_readahead_flag( p_sys->dvdnav, DVD_READ_CACHE ) !=
246           DVDNAV_STATUS_OK )
247     {
248         msg_Warn( p_demux, "cannot set read-a-head flag" );
249     }
250
251     if( dvdnav_set_PGC_positioning_flag( p_sys->dvdnav, 1 ) !=
252           DVDNAV_STATUS_OK )
253     {
254         msg_Warn( p_demux, "cannot set PGC positioning flag" );
255     }
256
257     /* Set menu language ("en")
258      * XXX: maybe it would be better to set it like audio/spu
259      * or to create a --menu-language option */
260     if( dvdnav_menu_language_select( p_sys->dvdnav, LANGUAGE_DEFAULT ) !=
261         DVDNAV_STATUS_OK )
262     {
263         msg_Warn( p_demux, "can't set menu language to '%s' (%s)",
264                   LANGUAGE_DEFAULT, dvdnav_err_to_string( p_sys->dvdnav ) );
265     }
266
267     /* Set audio language */
268     psz_code = DemuxGetLanguageCode( p_demux, "audio-language" );
269     if( dvdnav_audio_language_select( p_sys->dvdnav, psz_code ) !=
270         DVDNAV_STATUS_OK )
271     {
272         msg_Warn( p_demux, "can't set audio language to '%s' (%s)",
273                   psz_code, dvdnav_err_to_string( p_sys->dvdnav ) );
274         /* We try to fall back to 'en' */
275         if( strcmp( psz_code, LANGUAGE_DEFAULT ) )
276             dvdnav_audio_language_select( p_sys->dvdnav, LANGUAGE_DEFAULT );
277     }
278     free( psz_code );
279
280     /* Set spu language */
281     psz_code = DemuxGetLanguageCode( p_demux, "sub-language" );
282     if( dvdnav_spu_language_select( p_sys->dvdnav, psz_code ) !=
283         DVDNAV_STATUS_OK )
284     {
285         msg_Warn( p_demux, "can't set spu language to '%s' (%s)",
286                   psz_code, dvdnav_err_to_string( p_sys->dvdnav ) );
287         /* We try to fall back to 'en' */
288         if( strcmp( psz_code, LANGUAGE_DEFAULT ) )
289             dvdnav_spu_language_select(p_sys->dvdnav, LANGUAGE_DEFAULT );
290     }
291     free( psz_code );
292
293     DemuxTitles( p_demux );
294
295     var_Create( p_demux, "dvdnav-menu", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
296     var_Get( p_demux, "dvdnav-menu", &val );
297     if( val.b_bool )
298     {
299         msg_Dbg( p_demux, "trying to go to dvd menu" );
300
301         if( dvdnav_title_play( p_sys->dvdnav, 1 ) != DVDNAV_STATUS_OK )
302         {
303             msg_Err( p_demux, "cannot set title (can't decrypt DVD?)" );
304             intf_UserFatal( p_demux, false, _("Playback failure"),
305                             _("VLC cannot set the DVD's title. It possibly "
306                               "cannot decrypt the entire disk.") );
307             dvdnav_close( p_sys->dvdnav );
308             free( p_sys );
309             return VLC_EGENERIC;
310         }
311
312         if( dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Title ) !=
313             DVDNAV_STATUS_OK )
314         {
315             /* Try going to menu root */
316             if( dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root ) !=
317                 DVDNAV_STATUS_OK )
318                     msg_Warn( p_demux, "cannot go to dvd menu" );
319         }
320     }
321
322     var_Create( p_demux, "dvdnav-angle", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
323     var_Get( p_demux, "dvdnav-angle", &val );
324     i_angle = val.i_int > 0 ? val.i_int : 1;
325
326     /* Update default_pts to a suitable value for dvdnav access */
327     var_Create( p_demux, "dvdnav-caching", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
328
329     /* FIXME hack hack hack hack FIXME */
330     /* Get p_input and create variable */
331     p_sys->p_input = vlc_object_find( p_demux, VLC_OBJECT_INPUT, FIND_PARENT );
332     var_Create( p_sys->p_input, "x-start", VLC_VAR_INTEGER );
333     var_Create( p_sys->p_input, "y-start", VLC_VAR_INTEGER );
334     var_Create( p_sys->p_input, "x-end", VLC_VAR_INTEGER );
335     var_Create( p_sys->p_input, "y-end", VLC_VAR_INTEGER );
336     var_Create( p_sys->p_input, "color", VLC_VAR_ADDRESS );
337     var_Create( p_sys->p_input, "menu-palette", VLC_VAR_ADDRESS );
338     var_Create( p_sys->p_input, "highlight", VLC_VAR_BOOL );
339     var_Create( p_sys->p_input, "highlight-mutex", VLC_VAR_MUTEX );
340
341     /* Now create our event thread catcher */
342     p_sys->p_ev = vlc_object_create( p_demux, sizeof( event_thread_t ) );
343     p_sys->p_ev->p_demux = p_demux;
344     vlc_thread_create( p_sys->p_ev, "dvdnav event thread handler", EventThread,
345                        VLC_THREAD_PRIORITY_LOW, false );
346
347     return VLC_SUCCESS;
348 }
349
350 /*****************************************************************************
351  * Close:
352  *****************************************************************************/
353 static void Close( vlc_object_t *p_this )
354 {
355     demux_t     *p_demux = (demux_t*)p_this;
356     demux_sys_t *p_sys = p_demux->p_sys;
357     int i;
358
359     /* stop the event handler */
360     vlc_object_kill( p_sys->p_ev );
361     vlc_thread_join( p_sys->p_ev );
362     vlc_object_release( p_sys->p_ev );
363
364     var_Destroy( p_sys->p_input, "highlight-mutex" );
365     var_Destroy( p_sys->p_input, "highlight" );
366     var_Destroy( p_sys->p_input, "x-start" );
367     var_Destroy( p_sys->p_input, "x-end" );
368     var_Destroy( p_sys->p_input, "y-start" );
369     var_Destroy( p_sys->p_input, "y-end" );
370     var_Destroy( p_sys->p_input, "color" );
371     var_Destroy( p_sys->p_input, "menu-palette" );
372
373     vlc_object_release( p_sys->p_input );
374
375     for( i = 0; i < PS_TK_COUNT; i++ )
376     {
377         ps_track_t *tk = &p_sys->tk[i];
378         if( tk->b_seen )
379         {
380             es_format_Clean( &tk->fmt );
381             if( tk->es ) es_out_Del( p_demux->out, tk->es );
382         }
383     }
384
385     dvdnav_close( p_sys->dvdnav );
386     free( p_sys );
387 }
388
389 /*****************************************************************************
390  * Control:
391  *****************************************************************************/
392 static int Control( demux_t *p_demux, int i_query, va_list args )
393 {
394     demux_sys_t *p_sys = p_demux->p_sys;
395     double f, *pf;
396     bool *pb;
397     int64_t *pi64;
398     input_title_t ***ppp_title;
399     int          *pi_int;
400     int i;
401
402     switch( i_query )
403     {
404         case DEMUX_SET_POSITION:
405         case DEMUX_GET_POSITION:
406         case DEMUX_GET_TIME:
407         case DEMUX_GET_LENGTH:
408         {
409             uint32_t pos, len;
410             if( dvdnav_get_position( p_sys->dvdnav, &pos, &len ) !=
411                   DVDNAV_STATUS_OK || len == 0 )
412             {
413                 return VLC_EGENERIC;
414             }
415
416             if( i_query == DEMUX_GET_POSITION )
417             {
418                 pf = (double*)va_arg( args, double* );
419                 *pf = (double)pos / (double)len;
420                 return VLC_SUCCESS;
421             }
422             else if( i_query == DEMUX_SET_POSITION )
423             {
424                 f = (double)va_arg( args, double );
425                 pos = f * len;
426                 if( dvdnav_sector_search( p_sys->dvdnav, pos, SEEK_SET ) ==
427                       DVDNAV_STATUS_OK )
428                 {
429                     return VLC_SUCCESS;
430                 }
431             }
432             else if( i_query == DEMUX_GET_TIME )
433             {
434                 pi64 = (int64_t*)va_arg( args, int64_t * );
435                 if( p_sys->i_pgc_length > 0 )
436                 {
437                     *pi64 = p_sys->i_pgc_length * pos / len;
438                     return VLC_SUCCESS;
439                 }
440             }
441             else if( i_query == DEMUX_GET_LENGTH )
442             {
443                 pi64 = (int64_t*)va_arg( args, int64_t * );
444                 if( p_sys->i_pgc_length > 0 )
445                 {
446                     *pi64 = (int64_t)p_sys->i_pgc_length;
447                     return VLC_SUCCESS;
448                 }
449             }
450
451             return VLC_EGENERIC;
452         }
453
454         /* Special for access_demux */
455         case DEMUX_CAN_PAUSE:
456         case DEMUX_CAN_SEEK:
457         case DEMUX_CAN_CONTROL_PACE:
458             /* TODO */
459             pb = (bool*)va_arg( args, bool * );
460             *pb = true;
461             return VLC_SUCCESS;
462
463         case DEMUX_SET_PAUSE_STATE:
464             return VLC_SUCCESS;
465
466         case DEMUX_GET_TITLE_INFO:
467             ppp_title = (input_title_t***)va_arg( args, input_title_t*** );
468             pi_int    = (int*)va_arg( args, int* );
469             *((int*)va_arg( args, int* )) = 0; /* Title offset */
470             *((int*)va_arg( args, int* )) = 1; /* Chapter offset */
471
472             /* Duplicate title infos */
473             *pi_int = p_sys->i_title;
474             *ppp_title = malloc( sizeof( input_title_t ** ) * p_sys->i_title );
475             for( i = 0; i < p_sys->i_title; i++ )
476             {
477                 (*ppp_title)[i] = vlc_input_title_Duplicate( p_sys->title[i] );
478             }
479             return VLC_SUCCESS;
480
481         case DEMUX_SET_TITLE:
482             i = (int)va_arg( args, int );
483             if( ( i == 0 && dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root )
484                   != DVDNAV_STATUS_OK ) ||
485                 ( i != 0 && dvdnav_title_play( p_sys->dvdnav, i )
486                   != DVDNAV_STATUS_OK ) )
487             {
488                 msg_Warn( p_demux, "cannot set title/chapter" );
489                 return VLC_EGENERIC;
490             }
491             p_demux->info.i_update |=
492                 INPUT_UPDATE_TITLE | INPUT_UPDATE_SEEKPOINT;
493             p_demux->info.i_title = i;
494             p_demux->info.i_seekpoint = 0;
495             return VLC_SUCCESS;
496
497         case DEMUX_SET_SEEKPOINT:
498             i = (int)va_arg( args, int );
499             if( p_demux->info.i_title == 0 )
500             {
501                 int i_ret;
502                 /* Special case */
503                 switch( i )
504                 {
505                 case 0:
506                     i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Escape );
507                     break;
508                 case 1:
509                     i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Root );
510                     break;
511                 case 2:
512                     i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Title );
513                     break;
514                 case 3:
515                     i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Part );
516                     break;
517                 case 4:
518                     i_ret = dvdnav_menu_call( p_sys->dvdnav,
519                                               DVD_MENU_Subpicture );
520                     break;
521                 case 5:
522                     i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Audio );
523                     break;
524                 case 6:
525                     i_ret = dvdnav_menu_call( p_sys->dvdnav, DVD_MENU_Angle );
526                     break;
527                 default:
528                     return VLC_EGENERIC;
529                 }
530
531                 if( i_ret != DVDNAV_STATUS_OK )
532                     return VLC_EGENERIC;
533             }
534             else if( dvdnav_part_play( p_sys->dvdnav, p_demux->info.i_title,
535                                        i + 1 ) != DVDNAV_STATUS_OK )
536             {
537                 msg_Warn( p_demux, "cannot set title/chapter" );
538                 return VLC_EGENERIC;
539             }
540             p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
541             p_demux->info.i_seekpoint = i;
542             return VLC_SUCCESS;
543
544         case DEMUX_GET_PTS_DELAY:
545             pi64 = (int64_t*)va_arg( args, int64_t * );
546             *pi64 = (int64_t)var_GetInteger( p_demux, "dvdnav-caching" ) *1000;
547             return VLC_SUCCESS;
548
549         case DEMUX_GET_META:
550         {
551             const char *title_name = NULL;
552
553             dvdnav_get_title_string(p_sys->dvdnav, &title_name);
554             if( (NULL != title_name) && ('\0' != title_name[0]) )
555             {
556                 vlc_meta_t *p_meta = (vlc_meta_t*)va_arg( args, vlc_meta_t* );
557                 vlc_meta_Set( p_meta, vlc_meta_Title, title_name );
558                 return VLC_SUCCESS;
559             }
560             return VLC_EGENERIC;
561         }
562
563         /* TODO implement others */
564         default:
565             return VLC_EGENERIC;
566     }
567 }
568
569 /*****************************************************************************
570  * Demux:
571  *****************************************************************************/
572 static int Demux( demux_t *p_demux )
573 {
574     demux_sys_t *p_sys = p_demux->p_sys;
575
576     uint8_t buffer[DVD_VIDEO_LB_LEN];
577     uint8_t *packet = buffer;
578     int i_event;
579     int i_len;
580
581 #if DVD_READ_CACHE
582     if( dvdnav_get_next_cache_block( p_sys->dvdnav, &packet, &i_event, &i_len )
583         == DVDNAV_STATUS_ERR )
584 #else
585     if( dvdnav_get_next_block( p_sys->dvdnav, packet, &i_event, &i_len )
586         == DVDNAV_STATUS_ERR )
587 #endif
588     {
589         msg_Warn( p_demux, "cannot get next block (%s)",
590                   dvdnav_err_to_string( p_sys->dvdnav ) );
591         return -1;
592     }
593
594     switch( i_event )
595     {
596     case DVDNAV_BLOCK_OK:   /* mpeg block */
597         DemuxBlock( p_demux, packet, i_len );
598         break;
599
600     case DVDNAV_NOP:    /* Nothing */
601         msg_Dbg( p_demux, "DVDNAV_NOP" );
602         break;
603
604     case DVDNAV_STILL_FRAME:
605     {
606         dvdnav_still_event_t *event = (dvdnav_still_event_t*)packet;
607         vlc_mutex_lock( &p_sys->p_ev->lock );
608         if( !p_sys->p_ev->b_still )
609         {
610             msg_Dbg( p_demux, "DVDNAV_STILL_FRAME" );
611             msg_Dbg( p_demux, "     - length=0x%x", event->length );
612             p_sys->p_ev->b_still = true;
613             if( event->length == 0xff )
614             {
615                 p_sys->p_ev->i_still_end = 0;
616             }
617             else
618             {
619                 p_sys->p_ev->i_still_end = (int64_t)event->length *
620                     1000000 + mdate() + p_sys->p_input->i_pts_delay;
621             }
622         }
623         vlc_mutex_unlock( &p_sys->p_ev->lock );
624         msleep( 40000 );
625         break;
626     }
627
628     case DVDNAV_SPU_CLUT_CHANGE:
629     {
630         int i;
631
632         msg_Dbg( p_demux, "DVDNAV_SPU_CLUT_CHANGE" );
633         /* Update color lookup table (16 *uint32_t in packet) */
634         memcpy( p_sys->clut, packet, 16 * sizeof( uint32_t ) );
635
636         /* HACK to get the SPU tracks registered in the right order */
637         for( i = 0; i < 0x1f; i++ )
638         {
639             if( dvdnav_spu_stream_to_lang( p_sys->dvdnav, i ) != 0xffff )
640                 ESNew( p_demux, 0xbd20 + i );
641         }
642         /* END HACK */
643         break;
644     }
645
646     case DVDNAV_SPU_STREAM_CHANGE:
647     {
648         dvdnav_spu_stream_change_event_t *event =
649             (dvdnav_spu_stream_change_event_t*)packet;
650         int i;
651
652         msg_Dbg( p_demux, "DVDNAV_SPU_STREAM_CHANGE" );
653         msg_Dbg( p_demux, "     - physical_wide=%d",
654                  event->physical_wide );
655         msg_Dbg( p_demux, "     - physical_letterbox=%d",
656                  event->physical_letterbox);
657         msg_Dbg( p_demux, "     - physical_pan_scan=%d",
658                  event->physical_pan_scan );
659
660         ESSubtitleUpdate( p_demux );
661         p_sys->b_spu_change = true;
662
663         /* HACK to get the SPU tracks registered in the right order */
664         for( i = 0; i < 0x1f; i++ )
665         {
666             if( dvdnav_spu_stream_to_lang( p_sys->dvdnav, i ) != 0xffff )
667                 ESNew( p_demux, 0xbd20 + i );
668         }
669         /* END HACK */
670         break;
671     }
672
673     case DVDNAV_AUDIO_STREAM_CHANGE:
674     {
675         dvdnav_audio_stream_change_event_t *event =
676             (dvdnav_audio_stream_change_event_t*)packet;
677         msg_Dbg( p_demux, "DVDNAV_AUDIO_STREAM_CHANGE" );
678         msg_Dbg( p_demux, "     - physical=%d", event->physical );
679         /* TODO */
680         break;
681     }
682
683     case DVDNAV_VTS_CHANGE:
684     {
685         int32_t i_title = 0;
686         int32_t i_part  = 0;
687         int i;
688
689         dvdnav_vts_change_event_t *event = (dvdnav_vts_change_event_t*)packet;
690         msg_Dbg( p_demux, "DVDNAV_VTS_CHANGE" );
691         msg_Dbg( p_demux, "     - vtsN=%d", event->new_vtsN );
692         msg_Dbg( p_demux, "     - domain=%d", event->new_domain );
693
694         /* dvdnav_get_video_aspect / dvdnav_get_video_scale_permission */
695         /* TODO check if we always have VTS and CELL */
696         p_sys->i_aspect = dvdnav_get_video_aspect( p_sys->dvdnav );
697
698         /* reset PCR */
699         es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
700
701         for( i = 0; i < PS_TK_COUNT; i++ )
702         {
703             ps_track_t *tk = &p_sys->tk[i];
704             if( tk->b_seen )
705             {
706                 es_format_Clean( &tk->fmt );
707                 if( tk->es ) es_out_Del( p_demux->out, tk->es );
708             }
709             tk->b_seen = false;
710         }
711
712         if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
713                                        &i_part ) == DVDNAV_STATUS_OK )
714         {
715             if( i_title >= 0 && i_title < p_sys->i_title &&
716                 p_demux->info.i_title != i_title )
717             {
718                 p_demux->info.i_update |= INPUT_UPDATE_TITLE;
719                 p_demux->info.i_title = i_title;
720             }
721         }
722         break;
723     }
724
725     case DVDNAV_CELL_CHANGE:
726     {
727         int32_t i_title = 0;
728         int32_t i_part  = 0;
729
730         dvdnav_cell_change_event_t *event =
731             (dvdnav_cell_change_event_t*)packet;
732         msg_Dbg( p_demux, "DVDNAV_CELL_CHANGE" );
733         msg_Dbg( p_demux, "     - cellN=%d", event->cellN );
734         msg_Dbg( p_demux, "     - pgN=%d", event->pgN );
735         msg_Dbg( p_demux, "     - cell_length=%"PRId64, event->cell_length );
736         msg_Dbg( p_demux, "     - pg_length=%"PRId64, event->pg_length );
737         msg_Dbg( p_demux, "     - pgc_length=%"PRId64, event->pgc_length );
738         msg_Dbg( p_demux, "     - cell_start=%"PRId64, event->cell_start );
739         msg_Dbg( p_demux, "     - pg_start=%"PRId64, event->pg_start );
740
741         /* Store the lenght in time of the current PGC */
742         p_sys->i_pgc_length = event->pgc_length / 90 * 1000;
743
744         /* FIXME is it correct or there is better way to know chapter change */
745         if( dvdnav_current_title_info( p_sys->dvdnav, &i_title,
746                                        &i_part ) == DVDNAV_STATUS_OK )
747         {
748             if( i_title >= 0 && i_title < p_sys->i_title &&
749                 i_part >= 1 && i_part <= p_sys->title[i_title]->i_seekpoint &&
750                 p_demux->info.i_seekpoint != i_part - 1 )
751             {
752                 p_demux->info.i_update |= INPUT_UPDATE_SEEKPOINT;
753                 p_demux->info.i_seekpoint = i_part - 1;
754             }
755         }
756         break;
757     }
758
759     case DVDNAV_NAV_PACKET:
760     {
761 #ifdef DVDNAV_DEBUG
762         msg_Dbg( p_demux, "DVDNAV_NAV_PACKET" );
763 #endif
764         /* A lot of thing to do here :
765          *  - handle packet
766          *  - fetch pts (for time display)
767          *  - ...
768          */
769         DemuxBlock( p_demux, packet, i_len );
770         if( p_sys->b_spu_change )
771         {
772             ButtonUpdate( p_demux, false );
773             p_sys->b_spu_change = false;
774         }
775         break;
776     }
777
778     case DVDNAV_STOP:   /* EOF */
779         msg_Dbg( p_demux, "DVDNAV_STOP" );
780
781 #if DVD_READ_CACHE
782         dvdnav_free_cache_block( p_sys->dvdnav, packet );
783 #endif
784         return 0;
785
786     case DVDNAV_HIGHLIGHT:
787     {
788         dvdnav_highlight_event_t *event = (dvdnav_highlight_event_t*)packet;
789         msg_Dbg( p_demux, "DVDNAV_HIGHLIGHT" );
790         msg_Dbg( p_demux, "     - display=%d", event->display );
791         msg_Dbg( p_demux, "     - buttonN=%d", event->buttonN );
792         ButtonUpdate( p_demux, false );
793         break;
794     }
795
796     case DVDNAV_HOP_CHANNEL:
797         msg_Dbg( p_demux, "DVDNAV_HOP_CHANNEL" );
798         /* We should try to flush all our internal buffer */
799         break;
800
801     case DVDNAV_WAIT:
802         msg_Dbg( p_demux, "DVDNAV_WAIT" );
803
804         /* reset PCR */
805         es_out_Control( p_demux->out, ES_OUT_RESET_PCR );
806         dvdnav_wait_skip( p_sys->dvdnav );
807         break;
808
809     default:
810         msg_Warn( p_demux, "Unknown event (0x%x)", i_event );
811         break;
812     }
813
814 #if DVD_READ_CACHE
815     dvdnav_free_cache_block( p_sys->dvdnav, packet );
816 #endif
817
818     return 1;
819 }
820
821 /* Get a 2 char code
822  * FIXME: partiallyy duplicated from src/input/es_out.c
823  */
824 static char *DemuxGetLanguageCode( demux_t *p_demux, const char *psz_var )
825 {
826     const iso639_lang_t *pl;
827     char *psz_lang;
828     char *p;
829
830     psz_lang = var_CreateGetString( p_demux, psz_var );
831     /* XXX: we will use only the first value
832      * (and ignore other ones in case of a list) */
833     if( ( p = strchr( psz_lang, ',' ) ) ) *p = '\0';
834
835     for( pl = p_languages; pl->psz_iso639_1 != NULL; pl++ )
836     {
837         if( !strcasecmp( pl->psz_eng_name, psz_lang ) ||
838             !strcasecmp( pl->psz_native_name, psz_lang ) ||
839             !strcasecmp( pl->psz_iso639_1, psz_lang ) ||
840             !strcasecmp( pl->psz_iso639_2T, psz_lang ) ||
841             !strcasecmp( pl->psz_iso639_2B, psz_lang ) )
842             break;
843     }
844
845     free( psz_lang );
846
847     if( pl->psz_iso639_1 != NULL )
848         return strdup( pl->psz_iso639_1 );
849
850     return strdup(LANGUAGE_DEFAULT);
851 }
852
853 static void DemuxTitles( demux_t *p_demux )
854 {
855     demux_sys_t *p_sys = p_demux->p_sys;
856     input_title_t *t;
857     seekpoint_t *s;
858     int32_t i_titles;
859     int i;
860
861     /* Menu */
862     t = vlc_input_title_New();
863     t->b_menu = true;
864     t->psz_name = strdup( "DVD Menu" );
865
866     s = vlc_seekpoint_New();
867     s->psz_name = strdup( "Resume" );
868     TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
869
870     s = vlc_seekpoint_New();
871     s->psz_name = strdup( "Root" );
872     TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
873
874     s = vlc_seekpoint_New();
875     s->psz_name = strdup( "Title" );
876     TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
877
878     s = vlc_seekpoint_New();
879     s->psz_name = strdup( "Chapter" );
880     TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
881
882     s = vlc_seekpoint_New();
883     s->psz_name = strdup( "Subtitle" );
884     TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
885
886     s = vlc_seekpoint_New();
887     s->psz_name = strdup( "Audio" );
888     TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
889
890     s = vlc_seekpoint_New();
891     s->psz_name = strdup( "Angle" );
892     TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
893
894     TAB_APPEND( p_sys->i_title, p_sys->title, t );
895
896     /* Find out number of titles/chapters */
897     dvdnav_get_number_of_titles( p_sys->dvdnav, &i_titles );
898     for( i = 1; i <= i_titles; i++ )
899     {
900         int32_t i_chapters = 0;
901         int j;
902
903         dvdnav_get_number_of_parts( p_sys->dvdnav, i, &i_chapters );
904
905         t = vlc_input_title_New();
906         for( j = 0; j < __MAX( i_chapters, 1 ); j++ )
907         {
908             s = vlc_seekpoint_New();
909             TAB_APPEND( t->i_seekpoint, t->seekpoint, s );
910         }
911
912         TAB_APPEND( p_sys->i_title, p_sys->title, t );
913     }
914 }
915
916 /*****************************************************************************
917  * Update functions:
918  *****************************************************************************/
919 static void ButtonUpdate( demux_t *p_demux, bool b_mode )
920 {
921     demux_sys_t *p_sys = p_demux->p_sys;
922     vlc_value_t val;
923     int32_t i_title, i_part;
924
925     dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
926
927     if( var_Get( p_sys->p_input, "highlight-mutex", &val ) == VLC_SUCCESS )
928     {
929         vlc_mutex_t *p_mutex = val.p_address;
930         dvdnav_highlight_area_t hl;
931         int32_t i_button;
932
933         if( dvdnav_get_current_highlight( p_sys->dvdnav, &i_button )
934             != DVDNAV_STATUS_OK )
935         {
936             msg_Err( p_demux, "dvdnav_get_current_highlight failed" );
937             return;
938         }
939
940         if( i_button > 0 && i_title ==  0 )
941         {
942             int i;
943             pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
944
945             dvdnav_get_highlight_area( pci, i_button, b_mode, &hl );
946
947             for( i = 0; i < 4; i++ )
948             {
949                 uint32_t i_yuv = p_sys->clut[(hl.palette>>(16+i*4))&0x0f];
950                 uint8_t i_alpha = (hl.palette>>(i*4))&0x0f;
951                 i_alpha = i_alpha == 0xf ? 0xff : i_alpha << 4;
952
953                 p_sys->palette[i][0] = (i_yuv >> 16) & 0xff;
954                 p_sys->palette[i][1] = (i_yuv >> 0) & 0xff;
955                 p_sys->palette[i][2] = (i_yuv >> 8) & 0xff;
956                 p_sys->palette[i][3] = i_alpha;
957             }
958
959             vlc_mutex_lock( p_mutex );
960             val.i_int = hl.sx; var_Set( p_sys->p_input, "x-start", val );
961             val.i_int = hl.ex; var_Set( p_sys->p_input, "x-end", val );
962             val.i_int = hl.sy; var_Set( p_sys->p_input, "y-start", val );
963             val.i_int = hl.ey; var_Set( p_sys->p_input, "y-end", val );
964
965             val.p_address = (void *)p_sys->palette;
966             var_Set( p_sys->p_input, "menu-palette", val );
967
968             val.b_bool = true; var_Set( p_sys->p_input, "highlight", val );
969             vlc_mutex_unlock( p_mutex );
970
971             msg_Dbg( p_demux, "buttonUpdate %d", i_button );
972         }
973         else
974         {
975             msg_Dbg( p_demux, "buttonUpdate not done b=%d t=%d",
976                      i_button, i_title );
977
978             /* Show all */
979             vlc_mutex_lock( p_mutex );
980             val.b_bool = false;
981             var_Set( p_sys->p_input, "highlight", val );
982             vlc_mutex_unlock( p_mutex );
983         }
984     }
985 }
986
987 static void ESSubtitleUpdate( demux_t *p_demux )
988 {
989     demux_sys_t *p_sys = p_demux->p_sys;
990     int         i_spu = dvdnav_get_active_spu_stream( p_sys->dvdnav );
991     int32_t i_title, i_part;
992
993     ButtonUpdate( p_demux, false );
994
995     dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
996     if( i_title > 0 ) return;
997
998     if( i_spu >= 0 && i_spu <= 0x1f )
999     {
1000         ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
1001
1002         ESNew( p_demux, 0xbd20 + i_spu );
1003
1004         /* be sure to unselect it (reset) */
1005         es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
1006                         (bool)false );
1007
1008         /* now select it */
1009         es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
1010     }
1011     else
1012     {
1013         for( i_spu = 0; i_spu <= 0x1F; i_spu++ )
1014         {
1015             ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(0xbd20 + i_spu)];
1016             if( tk->b_seen )
1017             {
1018                 es_out_Control( p_demux->out, ES_OUT_SET_ES_STATE, tk->es,
1019                                 (bool)false );
1020             }
1021         }
1022     }
1023 }
1024
1025 /*****************************************************************************
1026  * DemuxBlock: demux a given block
1027  *****************************************************************************/
1028 static int DemuxBlock( demux_t *p_demux, uint8_t *pkt, int i_pkt )
1029 {
1030     demux_sys_t *p_sys = p_demux->p_sys;
1031     uint8_t     *p = pkt;
1032
1033     while( p < &pkt[i_pkt] )
1034     {
1035         int i_size = ps_pkt_size( p, &pkt[i_pkt] - p );
1036         block_t *p_pkt;
1037         if( i_size <= 0 )
1038         {
1039             break;
1040         }
1041
1042         /* Create a block */
1043         p_pkt = block_New( p_demux, i_size );
1044         memcpy( p_pkt->p_buffer, p, i_size);
1045
1046         /* Parse it and send it */
1047         switch( 0x100 | p[3] )
1048         {
1049         case 0x1b9:
1050         case 0x1bb:
1051         case 0x1bc:
1052 #ifdef DVDNAV_DEBUG
1053             if( p[3] == 0xbc )
1054             {
1055                 msg_Warn( p_demux, "received a PSM packet" );
1056             }
1057             else if( p[3] == 0xbb )
1058             {
1059                 msg_Warn( p_demux, "received a SYSTEM packet" );
1060             }
1061 #endif
1062             block_Release( p_pkt );
1063             break;
1064
1065         case 0x1ba:
1066         {
1067             int64_t i_scr;
1068             int i_mux_rate;
1069             if( !ps_pkt_parse_pack( p_pkt, &i_scr, &i_mux_rate ) )
1070             {
1071                 es_out_Control( p_demux->out, ES_OUT_SET_PCR, i_scr );
1072                 if( i_mux_rate > 0 ) p_sys->i_mux_rate = i_mux_rate;
1073             }
1074             block_Release( p_pkt );
1075             break;
1076         }
1077         default:
1078         {
1079             int i_id = ps_pkt_id( p_pkt );
1080             if( i_id >= 0xc0 )
1081             {
1082                 ps_track_t *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
1083
1084                 if( !tk->b_seen )
1085                 {
1086                     ESNew( p_demux, i_id );
1087                 }
1088                 if( tk->b_seen && tk->es &&
1089                     !ps_pkt_parse_pes( p_pkt, tk->i_skip ) )
1090                 {
1091                     es_out_Send( p_demux->out, tk->es, p_pkt );
1092                 }
1093                 else
1094                 {
1095                     block_Release( p_pkt );
1096                 }
1097             }
1098             else
1099             {
1100                 block_Release( p_pkt );
1101             }
1102             break;
1103         }
1104         }
1105
1106         p += i_size;
1107     }
1108
1109     return VLC_SUCCESS;
1110 }
1111
1112 /*****************************************************************************
1113  * ESNew: register a new elementary stream
1114  *****************************************************************************/
1115 static void ESNew( demux_t *p_demux, int i_id )
1116 {
1117     demux_sys_t *p_sys = p_demux->p_sys;
1118     ps_track_t  *tk = &p_sys->tk[PS_ID_TO_TK(i_id)];
1119     bool  b_select = false;
1120
1121     if( tk->b_seen ) return;
1122
1123     if( ps_track_fill( tk, 0, i_id ) )
1124     {
1125         msg_Warn( p_demux, "unknown codec for id=0x%x", i_id );
1126         return;
1127     }
1128
1129     /* Add a new ES */
1130     if( tk->fmt.i_cat == VIDEO_ES )
1131     {
1132         switch( p_sys->i_aspect )
1133         {
1134             /* TODO Any docs somewhere ? */
1135         default:
1136             tk->fmt.video.i_aspect = 0;
1137             break;
1138         }
1139         b_select = true;
1140     }
1141     else if( tk->fmt.i_cat == AUDIO_ES )
1142     {
1143         int i_audio = -1;
1144         /* find the audio number PLEASE find another way */
1145         if( (i_id&0xbdf8) == 0xbd88 )       /* dts */
1146         {
1147             i_audio = i_id&0x07;
1148         }
1149         else if( (i_id&0xbdf0) == 0xbd80 )  /* a52 */
1150         {
1151             i_audio = i_id&0xf;
1152         }
1153         else if( (i_id&0xbdf0) == 0xbda0 )  /* lpcm */
1154         {
1155             i_audio = i_id&0x1f;
1156         }
1157         else if( ( i_id&0xe0 ) == 0xc0 )    /* mpga */
1158         {
1159             i_audio = i_id&0x1f;
1160         }
1161         if( i_audio >= 0 )
1162         {
1163             int i_lang = dvdnav_audio_stream_to_lang( p_sys->dvdnav, i_audio );
1164             if( i_lang != 0xffff )
1165             {
1166                 tk->fmt.psz_language = malloc( 3 );
1167                 tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1168                 tk->fmt.psz_language[1] = (i_lang     )&0xff;
1169                 tk->fmt.psz_language[2] = 0;
1170             }
1171             if( dvdnav_get_active_audio_stream( p_sys->dvdnav ) == i_audio )
1172             {
1173                 b_select = true;
1174             }
1175         }
1176     }
1177     else if( tk->fmt.i_cat == SPU_ES )
1178     {
1179         int32_t i_title, i_part;
1180         int i_lang = dvdnav_spu_stream_to_lang( p_sys->dvdnav, i_id&0x1f );
1181         if( i_lang != 0xffff )
1182         {
1183             tk->fmt.psz_language = malloc( 3 );
1184             tk->fmt.psz_language[0] = (i_lang >> 8)&0xff;
1185             tk->fmt.psz_language[1] = (i_lang     )&0xff;
1186             tk->fmt.psz_language[2] = 0;
1187         }
1188
1189         /* Palette */
1190         tk->fmt.subs.spu.palette[0] = 0xBeef;
1191         memcpy( &tk->fmt.subs.spu.palette[1], p_sys->clut,
1192                 16 * sizeof( uint32_t ) );
1193
1194         /* We select only when we are not in the menu */
1195         dvdnav_current_title_info( p_sys->dvdnav, &i_title, &i_part );
1196         if( i_title > 0 &&
1197             dvdnav_get_active_spu_stream( p_sys->dvdnav ) == (i_id&0x1f) )
1198         {
1199             b_select = true;
1200         }
1201     }
1202
1203     tk->es = es_out_Add( p_demux->out, &tk->fmt );
1204     if( b_select )
1205     {
1206         es_out_Control( p_demux->out, ES_OUT_SET_ES, tk->es );
1207     }
1208     tk->b_seen = true;
1209
1210     if( tk->fmt.i_cat == VIDEO_ES ) ButtonUpdate( p_demux, false );
1211 }
1212
1213 /*****************************************************************************
1214  * Event handler code
1215  *****************************************************************************/
1216 static int  EventMouse( vlc_object_t *, char const *,
1217                         vlc_value_t, vlc_value_t, void * );
1218 static int  EventKey  ( vlc_object_t *, char const *,
1219                         vlc_value_t, vlc_value_t, void * );
1220
1221 static int EventThread( vlc_object_t *p_this )
1222 {
1223     event_thread_t *p_ev = (event_thread_t*)p_this;
1224     demux_sys_t    *p_sys = p_ev->p_demux->p_sys;
1225     vlc_object_t   *p_vout = NULL;
1226
1227     vlc_mutex_init( &p_ev->lock );
1228     p_ev->b_moved   = false;
1229     p_ev->b_clicked = false;
1230     p_ev->i_key_action = 0;
1231     p_ev->b_still   = false;
1232
1233     /* catch all key event */
1234     var_AddCallback( p_ev->p_libvlc, "key-action", EventKey, p_ev );
1235
1236     /* main loop */
1237     while( vlc_object_alive (p_ev) )
1238     {
1239         bool b_activated = false;
1240
1241         /* KEY part */
1242         if( p_ev->i_key_action != 0 )
1243         {
1244             pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1245
1246             vlc_mutex_lock( &p_ev->lock );
1247             switch( p_ev->i_key_action )
1248             {
1249             case ACTIONID_NAV_LEFT:
1250                 dvdnav_left_button_select( p_sys->dvdnav, pci );
1251                 break;
1252             case ACTIONID_NAV_RIGHT:
1253                 dvdnav_right_button_select( p_sys->dvdnav, pci );
1254                 break;
1255             case ACTIONID_NAV_UP:
1256                 dvdnav_upper_button_select( p_sys->dvdnav, pci );
1257                 break;
1258             case ACTIONID_NAV_DOWN:
1259                 dvdnav_lower_button_select( p_sys->dvdnav, pci );
1260                 break;
1261             case ACTIONID_NAV_ACTIVATE:
1262                 b_activated = true;
1263                 dvdnav_button_activate( p_sys->dvdnav, pci );
1264                 ButtonUpdate( p_ev->p_demux, true );
1265                 break;
1266             default:
1267                 break;
1268             }
1269             p_ev->i_key_action = 0;
1270             vlc_mutex_unlock( &p_ev->lock );
1271         }
1272
1273         /* VOUT part */
1274         if( p_vout && ( p_ev->b_moved || p_ev->b_clicked ) )
1275         {
1276             pci_t *pci = dvdnav_get_current_nav_pci( p_sys->dvdnav );
1277             vlc_value_t valx, valy;
1278
1279             vlc_mutex_lock( &p_ev->lock );
1280             var_Get( p_vout, "mouse-x", &valx );
1281             var_Get( p_vout, "mouse-y", &valy );
1282
1283             if( p_ev->b_moved )
1284             {
1285                 dvdnav_mouse_select( p_sys->dvdnav, pci, valx.i_int,
1286                                      valy.i_int );
1287             }
1288             if( p_ev->b_clicked )
1289             {
1290                 b_activated = true;
1291                 dvdnav_mouse_activate( p_sys->dvdnav, pci, valx.i_int,
1292                                        valy.i_int );
1293                 ButtonUpdate( p_ev->p_demux, true );
1294             }
1295
1296             p_ev->b_moved = false;
1297             p_ev->b_clicked = false;
1298             vlc_mutex_unlock( &p_ev->lock );
1299         }
1300         if( p_vout && !vlc_object_alive (p_vout) )
1301         {
1302             var_DelCallback( p_vout, "mouse-moved", EventMouse, p_ev );
1303             var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
1304             vlc_object_release( p_vout );
1305             p_vout = NULL;
1306         }
1307         if( p_vout == NULL )
1308         {
1309             p_vout = vlc_object_find( p_sys->p_input, VLC_OBJECT_VOUT,
1310                                       FIND_CHILD );
1311             if( p_vout)
1312             {
1313                 var_AddCallback( p_vout, "mouse-moved", EventMouse, p_ev );
1314                 var_AddCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
1315             }
1316         }
1317
1318         /* Still part */
1319         vlc_mutex_lock( &p_ev->lock );
1320         if( p_ev->b_still )
1321         {
1322             if( /* b_activated || // This breaks menus */
1323                 ( p_ev->i_still_end > 0 && p_ev->i_still_end < mdate() ))
1324             {
1325                 p_ev->b_still = false;
1326                 dvdnav_still_skip( p_sys->dvdnav );
1327             }
1328         }
1329         vlc_mutex_unlock( &p_ev->lock );
1330
1331         /* Wait a bit */
1332         msleep( 10000 );
1333     }
1334
1335     /* Release callback */
1336     if( p_vout )
1337     {
1338         var_DelCallback( p_vout, "mouse-moved", EventMouse, p_ev );
1339         var_DelCallback( p_vout, "mouse-clicked", EventMouse, p_ev );
1340         vlc_object_release( p_vout );
1341     }
1342     var_DelCallback( p_ev->p_libvlc, "key-action", EventKey, p_ev );
1343
1344     vlc_mutex_destroy( &p_ev->lock );
1345
1346     return VLC_SUCCESS;
1347 }
1348
1349 static int EventMouse( vlc_object_t *p_this, char const *psz_var,
1350                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1351 {
1352     event_thread_t *p_ev = p_data;
1353     vlc_mutex_lock( &p_ev->lock );
1354     if( psz_var[6] == 'c' )
1355         p_ev->b_clicked = true;
1356     else if( psz_var[6] == 'm' )
1357         p_ev->b_moved = true;
1358     vlc_mutex_unlock( &p_ev->lock );
1359
1360     return VLC_SUCCESS;
1361 }
1362
1363 static int EventKey( vlc_object_t *p_this, char const *psz_var,
1364                      vlc_value_t oldval, vlc_value_t newval, void *p_data )
1365 {
1366     event_thread_t *p_ev = p_data;
1367     vlc_mutex_lock( &p_ev->lock );
1368     p_ev->i_key_action = newval.i_int;
1369     vlc_mutex_unlock( &p_ev->lock );
1370
1371     return VLC_SUCCESS;
1372 }
1373
1374 /*****************************************************************************
1375  * ProbeDVD: very weak probing that avoids going too often into a dvdnav_open()
1376  *****************************************************************************/
1377 static int ProbeDVD( demux_t *p_demux, char *psz_name )
1378 {
1379 #ifdef HAVE_SYS_STAT_H
1380     struct stat stat_info;
1381     uint8_t pi_anchor[2];
1382     uint16_t i_tag_id = 0;
1383     int i_fd, i_ret;
1384
1385     if( !*psz_name )
1386     {
1387         /* Triggers libdvdcss autodetection */
1388         return VLC_SUCCESS;
1389     }
1390
1391     if( stat( psz_name, &stat_info ) || !S_ISREG( stat_info.st_mode ) )
1392     {
1393         /* Let dvdnav_open() do the probing */
1394         return VLC_SUCCESS;
1395     }
1396
1397     if( (i_fd = open( psz_name, O_RDONLY )) == -1 )
1398     {
1399         /* Let dvdnav_open() do the probing */
1400         return VLC_SUCCESS;
1401     }
1402
1403     /* Try to find the anchor (2 bytes at LBA 256) */
1404     i_ret = VLC_SUCCESS;
1405     if( lseek( i_fd, 256 * DVD_VIDEO_LB_LEN, SEEK_SET ) == -1 )
1406     {
1407         i_ret = VLC_EGENERIC;
1408     }
1409
1410     if( read( i_fd, pi_anchor, 2 ) == 2 )
1411     {
1412         i_tag_id = GetWLE(pi_anchor);
1413         if( i_tag_id != 2 ) i_ret = VLC_EGENERIC; /* Not an anchor */
1414     }
1415     else
1416     {
1417         i_ret = VLC_EGENERIC;
1418     }
1419
1420     close( i_fd );
1421
1422     return i_ret;
1423 #else
1424
1425     return VLC_SUCCESS;
1426 #endif
1427 }