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