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