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