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