1 /*****************************************************************************
2 * intf.c: interface for CMML annotations/hyperlinks
3 *****************************************************************************
4 * Copyright (C) 2003-2004 Commonwealth Scientific and Industrial Research
5 * Organisation (CSIRO) Australia
6 * Copyright (C) 2004 the VideoLAN team
10 * Authors: Andre Pang <Andre.Pang@csiro.au>
12 * This program is free software; you can redistribute it and/or modify
13 * it under the terms of the GNU General Public License as published by
14 * the Free Software Foundation; either version 2 of the License, or
15 * (at your option) any later version.
17 * This program is distributed in the hope that it will be useful,
18 * but WITHOUT ANY WARRANTY; without even the implied warranty of
19 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
20 * GNU General Public License for more details.
22 * You should have received a copy of the GNU General Public License
23 * along with this program; if not, write to the Free Software
24 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
25 *****************************************************************************/
27 /*****************************************************************************
29 *****************************************************************************/
34 #include <vlc_common.h>
42 #include <vlc_codec.h>
43 #include <vlc_playlist.h>
48 #include "browser_open.h"
53 #undef CMML_INTF_USE_TIMED_URIS
55 #undef CMML_INTF_DEBUG
56 #undef CMML_INTF_HISTORY_DEBUG
58 /*****************************************************************************
59 * intf_sys_t: description and status of interface
60 *****************************************************************************/
61 typedef struct decoder_sys_t
66 decoder_t * p_cmml_decoder;
67 input_thread_t * p_input;
72 struct navigation_history_t
78 /*****************************************************************************
80 *****************************************************************************/
82 decoder_sys_t *OpenIntf ( vlc_object_t * );
83 void CloseIntf ( decoder_sys_t * );
85 static int InitThread ( intf_thread_t * );
86 static int MouseEvent ( vlc_object_t *, char const *,
87 vlc_value_t, vlc_value_t, void * );
88 static int KeyEvent ( vlc_object_t *, char const *,
89 vlc_value_t, vlc_value_t, void * );
91 static void FollowAnchor ( intf_thread_t * );
92 static void GoBack ( intf_thread_t * );
93 static void GoForward ( intf_thread_t * );
95 static int FollowAnchorCallback ( vlc_object_t *, char const *,
96 vlc_value_t, vlc_value_t, void * );
97 static int GoBackCallback ( vlc_object_t *, char const *,
98 vlc_value_t, vlc_value_t, void * );
99 static int GoForwardCallback ( vlc_object_t *, char const *,
100 vlc_value_t, vlc_value_t, void * );
102 static char *GetTimedURLFromPlaylistItem( intf_thread_t *, playlist_item_t * );
103 #ifdef CMML_INTF_USE_TIMED_URIS
104 static int GetCurrentTimeInSeconds ( input_thread_t * );
105 static char *GetTimedURIFragmentForTime ( int );
107 static int DisplayAnchor ( intf_thread_t *, vout_thread_t *,
109 static int DisplayPendingAnchor ( intf_thread_t *, vout_thread_t * );
110 static history_t * GetHistory ( playlist_t * );
111 static void ReplacePlaylistItem ( playlist_t *, char * );
113 static void *RunIntf ( vlc_object_t * );
115 /*****************************************************************************
116 * OpenIntf: initialize CMML interface
117 *****************************************************************************/
118 decoder_sys_t *OpenIntf ( vlc_object_t *p_this )
120 decoder_sys_t *p_intf = vlc_object_create( p_this, sizeof( *p_intf ) );
124 vlc_mutex_init( &p_intf->lock );
126 var_AddCallback( p_intf->p_libvlc, "key-action", KeyEvent, p_intf );
127 /* we also need to add the callback for "mouse-clicked", but do that later
128 * when we've found a p_vout */
130 var_Create( p_intf->p_libvlc, "browse-go-back", VLC_VAR_VOID );
131 var_AddCallback( p_intf->p_libvlc, "browse-go-back",
132 GoBackCallback, p_intf );
133 var_Create( p_intf->p_libvlc, "browse-go-forward", VLC_VAR_VOID );
134 var_AddCallback( p_intf->p_libvlc, "browse-go-forward",
135 GoForwardCallback, p_intf );
136 var_Create( p_intf->p_libvlc, "browse-follow-anchor", VLC_VAR_VOID );
137 var_AddCallback( p_intf->p_libvlc, "browse-follow-anchor",
138 FollowAnchorCallback, p_intf );
140 vlc_thread_create( p_intf, "cmml", RunIntf, VLC_THREAD_PRIORITY_LOW );
144 /*****************************************************************************
145 * CloseIntf: destroy dummy interface
146 *****************************************************************************/
147 void CloseIntf ( decoder_sys_t *p_intf )
149 vout_thread_t * p_vout;
151 #ifdef CMML_INTF_DEBUG
152 msg_Dbg( p_intf, "freeing CMML interface" );
155 /* erase the anchor text description from the video output if it exists */
156 p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
159 /* enable CMML as a subtitle track */
160 spu_Control( vout_GetSpu( p_vout ), SPU_CHANNEL_CLEAR, DEFAULT_CHAN );
161 vlc_object_release( p_vout );
164 var_DelCallback( p_intf->p_libvlc, "key-action", KeyEvent, p_intf );
165 vlc_object_kill( p_intf );
166 vlc_thread_join( p_intf );
168 vlc_object_release( p_intf->p_cmml_decoder );
170 vlc_mutex_destroy( &p_intf->lock );
171 vlc_object_release( p_intf );
175 /*****************************************************************************
177 *****************************************************************************/
178 static void *RunIntf( vlc_object_t *p_obj )
180 decoder_sys_t *p_intf = (decoder_sys_t *)p_obj;
181 int canc = vlc_savecancel();
182 vout_thread_t * p_vout = NULL;
184 if( InitThread( p_intf ) < 0 )
186 msg_Err( p_intf, "can't initialize CMML interface" );
189 #ifdef CMML_INTF_DEBUG
190 msg_Dbg( p_intf, "CMML intf initialized" );
194 while( vlc_object_alive (p_intf) )
196 /* if video output is dying, disassociate ourselves from it */
197 if( p_vout && !vlc_object_alive (p_vout) )
199 var_DelCallback( p_vout, "mouse-clicked", MouseEvent, p_intf );
200 vlc_object_release( p_vout );
204 /* find a video output if we currently don't have one */
207 p_vout = vlc_object_find( p_intf->p_input,
208 VLC_OBJECT_VOUT, FIND_CHILD );
211 #ifdef CMML_INTF_DEBUG
212 msg_Dbg( p_intf, "found vout thread" );
214 var_AddCallback( p_vout, "mouse-clicked", MouseEvent, p_intf );
218 vlc_mutex_lock( &p_intf->lock );
223 switch( p_intf->i_key_action )
225 case ACTIONID_NAV_ACTIVATE:
226 FollowAnchor( p_intf );
228 case ACTIONID_HISTORY_BACK:
231 case ACTIONID_HISTORY_FORWARD:
237 p_intf->i_key_action = 0;
238 vlc_mutex_unlock( &p_intf->lock );
240 (void) DisplayPendingAnchor( p_intf, p_vout );
243 msleep( INTF_IDLE_SLEEP );
246 /* if we're here, the video output is dying: release the vout object */
250 var_DelCallback( p_vout, "mouse-clicked", MouseEvent, p_intf );
251 vlc_object_release( p_vout );
254 vlc_object_release( p_intf->p_input );
255 vlc_restorecancel( canc );
259 /*****************************************************************************
260 * DisplayPendingAnchor: get a pending anchor description/URL from the CMML
261 * decoder and display it on screen
262 *****************************************************************************/
263 static int DisplayPendingAnchor( intf_thread_t *p_intf, vout_thread_t *p_vout )
265 decoder_t *p_cmml_decoder;
266 char *psz_description = NULL;
267 char *psz_url = NULL;
271 p_cmml_decoder = p_intf->p_cmml_decoder;
272 if( var_Get( p_cmml_decoder, "psz-current-anchor-description", &val )
281 psz_description = val.p_address;
283 if( var_Get( p_cmml_decoder, "psz-current-anchor-url", &val )
286 psz_url = val.p_address;
291 /* display anchor as subtitle on-screen */
292 if( DisplayAnchor( p_intf, p_vout, psz_description, psz_url )
295 /* text render unsuccessful: do nothing */
299 /* text render successful: clear description */
300 val.p_address = NULL;
301 if( var_Set( p_cmml_decoder, "psz-current-anchor-description", val )
305 "reset of psz-current-anchor-description failed" );
307 free( psz_description );
315 /*****************************************************************************
317 *****************************************************************************/
318 static int InitThread( intf_thread_t * p_intf )
320 /* We might need some locking here */
321 if( vlc_object_alive (p_intf) )
323 input_thread_t * p_input;
324 decoder_t *p_cmml_decoder;
326 p_cmml_decoder = vlc_object_find( p_intf, VLC_OBJECT_DECODER, FIND_PARENT );
327 p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT, FIND_PARENT );
329 #ifdef CMML_INTF_DEBUG
330 msg_Dbg( p_intf, "cmml decoder at %p, input thread at %p",
331 p_cmml_decoder, p_input );
334 /* Maybe the input just died */
335 if( p_input == NULL )
340 vlc_mutex_lock( &p_intf->lock );
342 p_intf->p_input = p_input;
343 p_intf->p_cmml_decoder = p_cmml_decoder;
345 p_intf->i_key_action = 0;
347 vlc_mutex_unlock( &p_intf->lock );
357 /*****************************************************************************
358 * MouseEvent: callback for mouse events
359 *****************************************************************************/
360 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
361 vlc_value_t oldval, vlc_value_t newval, void *p_data )
363 VLC_UNUSED(p_this); VLC_UNUSED(psz_var);
364 VLC_UNUSED(oldval); VLC_UNUSED(newval);
366 /* TODO: handle mouse clicks on the anchor text */
371 /*****************************************************************************
372 * KeyEvent: callback for keyboard events
373 *****************************************************************************/
374 static int KeyEvent( vlc_object_t *p_this, char const *psz_var,
375 vlc_value_t oldval, vlc_value_t newval, void *p_data )
377 VLC_UNUSED(p_this); VLC_UNUSED(psz_var);
378 VLC_UNUSED(oldval); VLC_UNUSED(newval);
379 intf_thread_t *p_intf = (intf_thread_t *)p_data;
382 vlc_mutex_lock( &p_intf->lock );
383 /* FIXME: key presses might get lost here... */
384 p_intf->i_key_action = newval.i_int;
386 vlc_mutex_unlock( &p_intf->lock );
391 /*****************************************************************************
392 * FollowAnchor: follow the current anchor being displayed to the user
393 *****************************************************************************/
394 static void FollowAnchor ( intf_thread_t *p_intf )
396 decoder_t *p_cmml_decoder;
397 char *psz_url = NULL;
400 msg_Dbg( p_intf, "User followed anchor" );
402 p_cmml_decoder = p_intf->p_cmml_decoder;
404 if( var_Get( p_cmml_decoder, "psz-current-anchor-url", &val ) ==
407 if( val.p_address ) psz_url = val.p_address;
410 #ifdef CMML_INTF_DEBUG
411 msg_Dbg( p_intf, "Current URL is \"%s\"", psz_url );
416 playlist_t *p_playlist;
417 playlist_item_t *p_current_item;
418 char *psz_uri_to_load;
422 p_playlist = pl_Hold( p_intf );
425 p_current_item = playlist_CurrentPlayingItem( p_playlist );
426 char *psz_uri = input_item_GetURI( p_current_item->p_input );
427 #ifdef CMML_INTF_DEBUG
428 msg_Dbg( p_intf, "Current playlist item URL is \"%s\"", psz_uri );
431 psz_uri_to_load = XURL_Concat( psz_uri, psz_url );
434 #ifdef CMML_INTF_DEBUG
435 msg_Dbg( p_intf, "URL to load is \"%s\"", psz_uri_to_load );
438 if( var_Get( p_intf->p_input, "time", &time ) )
440 msg_Dbg( p_intf, "couldn't get time from current clip" );
443 i_seconds = time.i_time / 1000000;
444 #ifdef CMML_INTF_DEBUG
445 msg_Dbg( p_intf, "Current time is \"%lld\"", i_seconds );
448 /* TODO: we need a (much) more robust way of detecting whether
449 * the file's a media file ... */
450 if( strstr( psz_uri_to_load, ".anx" ) != NULL )
452 history_t *p_history = NULL;
453 history_item_t *p_history_item = NULL;
456 p_history = GetHistory( p_playlist );
458 /* create history item */
459 psz_timed_url = GetTimedURLFromPlaylistItem( p_intf, p_current_item );
460 p_history_item = historyItem_New( psz_timed_url, psz_timed_url );
461 free( psz_timed_url );
463 if( !p_history_item )
465 msg_Warn( p_intf, "could not initialise history item" );
469 #ifdef CMML_INTF_DEBUG
470 msg_Dbg( p_intf, "history pre-index %d", p_history->i_index );
472 history_PruneAndInsert( p_history, p_history_item );
473 #ifdef CMML_INTF_DEBUG
474 msg_Dbg( p_intf, "new history item at %p, uri is \"%s\"",
475 p_history_item, p_history_item->psz_uri );
476 msg_Dbg( p_intf, "history index now %d", p_history->i_index );
480 /* free current-anchor-url */
482 val.p_address = NULL;
483 if( var_Set( p_cmml_decoder, "psz-current-anchor-url", val ) !=
486 msg_Dbg( p_intf, "couldn't reset psz-current-anchor-url" );
489 ReplacePlaylistItem( p_playlist, psz_uri_to_load );
493 #ifdef CMML_INTF_DEBUG
494 msg_Dbg( p_intf, "calling browser_Open with \"%s\"", psz_url );
496 (void) browser_Open( psz_url );
497 playlist_Control( p_playlist, PLAYLIST_PAUSE, pl_Unlocked, 0 );
500 free( psz_uri_to_load );
502 pl_Release( p_intf );
507 char *GetTimedURLFromPlaylistItem( intf_thread_t *p_intf,
508 playlist_item_t *p_current_item )
510 #ifdef CMML_INTF_USE_TIMED_URIS
511 char *psz_url = NULL;
512 char *psz_return_value = NULL;
513 char *psz_seconds = NULL;
516 char *psz_uri = input_item_GetURI( p_current_item->p_input );
517 psz_url = XURL_GetWithoutFragment( psz_uri );
520 /* Get current time as a string */
521 if( XURL_IsFileURL( psz_url ) == true )
522 psz_url = xstrcat( psz_url, "#" );
524 psz_url = xstrcat( psz_url, "?" );
526 /* jump back to 2 seconds before where we are now */
527 i_seconds = GetCurrentTimeInSeconds( p_intf->p_input ) - 2;
528 psz_seconds = GetTimedURIFragmentForTime( i_seconds < 0 ? 0 : i_seconds );
531 psz_url = xstrcat( psz_url, psz_seconds );
533 psz_return_value = psz_url;
536 return psz_return_value;
540 return input_item_GetURI( p_current_item->p_input );
546 #ifdef CMML_INTF_USE_TIMED_URIS
548 * Get the current time, rounded down to the nearest second
550 * http://www.ietf.org/internet-drafts/draft-pfeiffer-temporal-fragments-02.txt
553 int GetCurrentTimeInSeconds( input_thread_t *p_input )
558 var_Get( p_input, "time", &time );
559 i_seconds = time.i_time / 1000000;
564 char *GetTimedURIFragmentForTime( int seconds )
568 if( asprintf( &psz_time, "%d", seconds ) == -1 )
575 int GoBackCallback( vlc_object_t *p_this, char const *psz_var,
576 vlc_value_t oldval, vlc_value_t newval, void *p_data )
578 VLC_UNUSED(p_this); VLC_UNUSED(psz_var);
579 VLC_UNUSED(oldval); VLC_UNUSED(newval);
580 intf_thread_t *p_intf = (intf_thread_t *) p_data;
586 int GoForwardCallback( vlc_object_t *p_this, char const *psz_var,
587 vlc_value_t oldval, vlc_value_t newval, void *p_data )
589 VLC_UNUSED(p_this); VLC_UNUSED(psz_var);
590 VLC_UNUSED(oldval); VLC_UNUSED(newval);
591 intf_thread_t *p_intf = (intf_thread_t *) p_data;
597 int FollowAnchorCallback( vlc_object_t *p_this, char const *psz_var,
598 vlc_value_t oldval, vlc_value_t newval,
601 VLC_UNUSED(p_this); VLC_UNUSED(psz_var);
602 VLC_UNUSED(oldval); VLC_UNUSED(newval);
603 intf_thread_t *p_intf = (intf_thread_t *) p_data;
604 FollowAnchor( p_intf );
609 void GoBack( intf_thread_t *p_intf )
612 history_t *p_history = NULL;
613 history_item_t *p_history_item = NULL;
614 history_item_t *p_new_history_item = NULL;
615 playlist_t *p_playlist = NULL;
616 char *psz_timed_url = NULL;
617 playlist_item_t *p_current_item;
619 #ifdef CMML_INTF_DEBUG
620 msg_Dbg( p_intf, "Going back in navigation history" );
623 /* Find the playlist */
624 p_playlist = pl_Hold( p_intf );
626 /* Retrieve navigation history from playlist */
627 if( var_Get( p_playlist, "navigation-history", &history ) != VLC_SUCCESS ||
630 /* History doesn't exist yet: ignore user's request */
631 msg_Warn( p_intf, "can't go back: no history exists yet" );
632 pl_Release( p_intf );
636 p_history = history.p_address;
637 #ifdef CMML_INTF_DEBUG
638 msg_Dbg( p_intf, "back: nav history retrieved from %p", p_history );
639 msg_Dbg( p_intf, "nav history index:%d, p_xarray:%p", p_history->i_index,
640 p_history->p_xarray );
643 /* Check whether we can go back in the history */
644 if( history_CanGoBack( p_history ) == false )
646 msg_Warn( p_intf, "can't go back: already at beginning of history" );
647 pl_Release( p_intf );
651 p_current_item = playlist_CurrentPlayingItem( p_playlist );
653 /* Save the currently-playing media in a new history item */
654 psz_timed_url = GetTimedURLFromPlaylistItem( p_intf, p_current_item );
655 p_new_history_item = historyItem_New( psz_timed_url, psz_timed_url );
656 free( psz_timed_url );
658 if( !p_new_history_item )
660 #ifdef CMML_INTF_DEBUG
661 msg_Dbg( p_intf, "back: could not initialise new history item" );
663 pl_Release( p_intf );
667 /* Go back in the history, saving the currently-playing item */
668 (void) history_GoBackSavingCurrentItem( p_history, p_new_history_item );
669 p_history_item = history_Item( p_history );
671 #ifdef CMML_INTF_DEBUG
672 msg_Dbg( p_intf, "retrieving item from h index %d", p_history->i_index );
673 msg_Dbg( p_intf, "got previous history item: %p", p_history_item );
674 msg_Dbg( p_intf, "prev history item URL: \"%s\"", p_history_item->psz_uri );
677 ReplacePlaylistItem( p_playlist, p_history_item->psz_uri );
678 pl_Release( p_intf );
682 void GoForward( intf_thread_t *p_intf )
685 history_t *p_history = NULL;
686 history_item_t *p_history_item = NULL;
687 history_item_t *p_new_history_item = NULL;
688 playlist_t *p_playlist = NULL;
689 playlist_item_t *p_current_item;
691 #ifdef CMML_INTF_DEBUG
692 msg_Dbg( p_intf, "Going forward in navigation history" );
695 /* Find the playlist */
696 p_playlist = pl_Hold( p_intf );
698 /* Retrieve navigation history from playlist */
699 if( var_Get( p_playlist, "navigation-history", &history ) != VLC_SUCCESS ||
702 /* History doesn't exist yet: ignore user's request */
703 msg_Warn( p_intf, "can't go back: no history exists yet" );
704 pl_Release( p_intf );
708 p_history = history.p_address;
709 #ifdef CMML_INTF_DEBUG
710 msg_Dbg( p_intf, "forward: nav history retrieved from %p", p_history );
711 msg_Dbg( p_intf, "nav history index:%d, p_xarray:%p", p_history->i_index,
712 p_history->p_xarray );
715 /* Check whether we can go forward in the history */
716 if( history_CanGoForward( p_history ) == false )
718 msg_Warn( p_intf, "can't go forward: already at end of history" );
719 pl_Release( p_intf );
723 /* Save the currently-playing media in a new history item */
724 p_new_history_item = malloc( sizeof(history_item_t) );
725 if( !p_new_history_item )
727 #ifdef CMML_INTF_DEBUG
728 msg_Dbg( p_intf, "forward: could not initialise new history item" );
730 pl_Release( p_intf );
733 p_current_item = playlist_CurrentPlayingItem( p_playlist );
734 p_new_history_item->psz_uri = GetTimedURLFromPlaylistItem( p_intf,
736 p_new_history_item->psz_name = p_new_history_item->psz_uri;
738 /* Go forward in the history, saving the currently-playing item */
739 (void) history_GoForwardSavingCurrentItem( p_history, p_new_history_item );
740 p_history_item = history_Item( p_history );
742 #ifdef CMML_INTF_DEBUG
743 msg_Dbg( p_intf, "retrieving item from h index %d", p_history->i_index );
744 msg_Dbg( p_intf, "got next history item: %p", p_history_item );
745 msg_Dbg( p_intf, "next history item URL: \"%s\"", p_history_item->psz_uri );
748 ReplacePlaylistItem( p_playlist, p_history_item->psz_uri );
749 pl_Release( p_intf );
752 static void ReplacePlaylistItem( playlist_t *p_playlist, char *psz_uri )
754 playlist_Stop( p_playlist );
755 (void) playlist_Add( p_playlist, psz_uri, psz_uri,
756 PLAYLIST_INSERT /* FIXME: used to be PLAYLIST_REPLACE */, PLAYLIST_END|PLAYLIST_GO, true /* FIXME: p_playlist->status.i_index */,
760 /****************************************************************************
761 * DisplayAnchor: displays an anchor on the given video output
762 ****************************************************************************/
763 static int DisplayAnchor( intf_thread_t *p_intf,
764 vout_thread_t *p_vout,
765 char *psz_anchor_description,
766 char *psz_anchor_url )
768 int i_margin_h, i_margin_v;
780 /* Should display subtitle underlined and in blue, but it looks
781 * like VLC doesn't implement any text styles yet. D'oh! */
782 // p_style = &blue_with_underline;
786 /* TODO: p_subpicture doesn't have the proper i_x and i_y
787 * coordinates. Need to look at the subpicture display system to
789 if ( vout_ShowTextAbsolute( p_vout, DEFAULT_CHAN,
790 psz_anchor_description, NULL, OSD_ALIGN_BOTTOM,
791 i_margin_h, i_margin_v, i_now, 0 ) == VLC_SUCCESS )
793 /* Displayed successfully */
802 msg_Dbg( p_intf, "DisplayAnchor couldn't find a video output" );
809 static history_t * GetHistory( playlist_t *p_playlist )
812 history_t *p_history = NULL;
814 if( var_Get( p_playlist, "navigation-history", &val ) != VLC_SUCCESS )
816 /* history doesn't exist yet: need to create it */
817 history_t *new_history = history_New();
818 val.p_address = new_history;
819 var_Create( p_playlist, "navigation-history",
820 VLC_VAR_ADDRESS|VLC_VAR_DOINHERIT );
821 if( var_Set( p_playlist, "navigation-history", val ) != VLC_SUCCESS )
823 msg_Warn( p_playlist, "could not initialise history" );
827 p_history = new_history;
828 #ifdef CMML_INTF_HISTORY_DEBUG
829 msg_Dbg( p_playlist, "nav history created at %p", new_history );
830 msg_Dbg( p_playlist, "nav history index:%d, p_xarray:%p",
831 p_history->i_index, p_history->p_xarray );
837 p_history = val.p_address;
838 #ifdef CMML_INTF_HISTORY_DEBUG
839 msg_Dbg( p_playlist, "nav history retrieved from %p", p_history );