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