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