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