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