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_input.h>
44 #include <vlc_interface.h>
46 #include <vlc_playlist.h>
51 #include "browser_open.h"
56 #undef CMML_INTF_USE_TIMED_URIS
58 #undef CMML_INTF_DEBUG
59 #undef CMML_INTF_HISTORY_DEBUG
61 /*****************************************************************************
62 * intf_sys_t: description and status of interface
63 *****************************************************************************/
66 decoder_t * p_cmml_decoder;
67 input_thread_t * p_input;
72 struct navigation_history_t
78 /*****************************************************************************
80 *****************************************************************************/
82 int OpenIntf ( vlc_object_t * );
83 void CloseIntf ( vlc_object_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 static char *GetTimedURIFragmentForTime ( int );
104 static int GetCurrentTimeInSeconds ( input_thread_t * );
105 static int DisplayAnchor ( intf_thread_t *, vout_thread_t *,
107 static int DisplayPendingAnchor ( intf_thread_t *, vout_thread_t * );
108 static history_t * GetHistory ( playlist_t * );
109 static void ReplacePlaylistItem ( playlist_t *, char * );
111 /* Exported functions */
112 static void RunIntf ( intf_thread_t *p_intf );
114 /*****************************************************************************
115 * OpenIntf: initialize CMML interface
116 *****************************************************************************/
117 int OpenIntf ( vlc_object_t *p_this )
119 intf_thread_t *p_intf = (intf_thread_t *)p_this;
121 p_intf->p_sys = malloc( sizeof( intf_sys_t ) );
122 if( p_intf->p_sys == NULL )
127 p_intf->pf_run = RunIntf;
129 var_AddCallback( p_intf->p_libvlc, "key-action", KeyEvent, p_intf );
130 /* we also need to add the callback for "mouse-clicked", but do that later
131 * when we've found a p_vout */
133 var_Create( p_intf->p_libvlc, "browse-go-back", VLC_VAR_VOID );
134 var_AddCallback( p_intf->p_libvlc, "browse-go-back",
135 GoBackCallback, p_intf );
136 var_Create( p_intf->p_libvlc, "browse-go-forward", VLC_VAR_VOID );
137 var_AddCallback( p_intf->p_libvlc, "browse-go-forward",
138 GoForwardCallback, p_intf );
139 var_Create( p_intf->p_libvlc, "browse-follow-anchor", VLC_VAR_VOID );
140 var_AddCallback( p_intf->p_libvlc, "browse-follow-anchor",
141 FollowAnchorCallback, p_intf );
146 /*****************************************************************************
147 * CloseIntf: destroy dummy interface
148 *****************************************************************************/
149 void CloseIntf ( vlc_object_t *p_this )
151 intf_thread_t * p_intf = (intf_thread_t *)p_this;
152 vout_thread_t * p_vout;
154 #ifdef CMML_INTF_DEBUG
155 msg_Dbg( p_intf, "freeing CMML interface" );
158 /* erase the anchor text description from the video output if it exists */
159 p_vout = vlc_object_find( p_intf, VLC_OBJECT_VOUT, FIND_ANYWHERE );
162 /* enable CMML as a subtitle track */
163 spu_Control( p_vout->p_spu, SPU_CHANNEL_CLEAR, DEFAULT_CHAN );
164 vlc_object_release( p_vout );
167 var_DelCallback( p_intf->p_libvlc, "key-action", KeyEvent, p_intf );
169 vlc_object_release( p_intf->p_sys->p_cmml_decoder );
171 free( p_intf->p_sys );
175 /*****************************************************************************
177 *****************************************************************************/
178 static void RunIntf( intf_thread_t *p_intf )
180 vout_thread_t * p_vout = NULL;
182 if( InitThread( p_intf ) < 0 )
184 msg_Err( p_intf, "can't initialize CMML interface" );
187 #ifdef CMML_INTF_DEBUG
188 msg_Dbg( p_intf, "CMML intf initialized" );
192 while( vlc_object_alive (p_intf) )
194 /* if video output is dying, disassociate ourselves from it */
195 if( p_vout && !vlc_object_alive (p_vout) )
197 var_DelCallback( p_vout, "mouse-clicked", MouseEvent, p_intf );
198 vlc_object_release( p_vout );
202 /* find a video output if we currently don't have one */
205 p_vout = vlc_object_find( p_intf->p_sys->p_input,
206 VLC_OBJECT_VOUT, FIND_CHILD );
209 #ifdef CMML_INTF_DEBUG
210 msg_Dbg( p_intf, "found vout thread" );
212 var_AddCallback( p_vout, "mouse-clicked", MouseEvent, p_intf );
216 vlc_mutex_lock( &p_intf->change_lock );
221 switch( p_intf->p_sys->i_key_action )
223 case ACTIONID_NAV_ACTIVATE:
224 FollowAnchor( p_intf );
226 case ACTIONID_HISTORY_BACK:
229 case ACTIONID_HISTORY_FORWARD:
235 p_intf->p_sys->i_key_action = 0;
236 vlc_mutex_unlock( &p_intf->change_lock );
238 (void) DisplayPendingAnchor( p_intf, p_vout );
241 msleep( INTF_IDLE_SLEEP );
244 /* if we're here, the video output is dying: release the vout object */
248 var_DelCallback( p_vout, "mouse-clicked", MouseEvent, p_intf );
249 vlc_object_release( p_vout );
252 vlc_object_release( p_intf->p_sys->p_input );
255 /*****************************************************************************
256 * DisplayPendingAnchor: get a pending anchor description/URL from the CMML
257 * decoder and display it on screen
258 *****************************************************************************/
259 static int DisplayPendingAnchor( intf_thread_t *p_intf, vout_thread_t *p_vout )
261 decoder_t *p_cmml_decoder;
262 char *psz_description = NULL;
263 char *psz_url = NULL;
265 intf_thread_t *p_primary_intf;
268 p_cmml_decoder = p_intf->p_sys->p_cmml_decoder;
269 if( var_Get( p_cmml_decoder, "psz-current-anchor-description", &val )
278 psz_description = val.p_address;
280 if( var_Get( p_cmml_decoder, "psz-current-anchor-url", &val )
283 psz_url = val.p_address;
288 /* don't display anchor if main interface can display it */
289 p_primary_intf = vlc_object_find( p_intf->p_libvlc, VLC_OBJECT_INTF,
294 if( var_Get( p_primary_intf, "intf-displays-cmml-description", &val )
297 if( val.b_bool == true )
299 vlc_object_release( p_primary_intf );
304 vlc_object_release( p_primary_intf );
307 /* display anchor as subtitle on-screen */
308 if( DisplayAnchor( p_intf, p_vout, psz_description, psz_url )
311 /* text render unsuccessful: do nothing */
315 /* text render successful: clear description */
316 val.p_address = NULL;
317 if( var_Set( p_cmml_decoder, "psz-current-anchor-description", val )
321 "reset of psz-current-anchor-description failed" );
323 free( psz_description );
331 /*****************************************************************************
333 *****************************************************************************/
334 static int InitThread( intf_thread_t * p_intf )
336 /* We might need some locking here */
337 if( vlc_object_alive (p_intf) )
339 input_thread_t * p_input;
340 decoder_t *p_cmml_decoder;
342 p_cmml_decoder = vlc_object_find( p_intf, VLC_OBJECT_DECODER, FIND_PARENT );
343 p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT, FIND_PARENT );
345 #ifdef CMML_INTF_DEBUG
346 msg_Dbg( p_intf, "cmml decoder at %p, input thread at %p",
347 p_cmml_decoder, p_input );
350 /* Maybe the input just died */
351 if( p_input == NULL )
356 vlc_mutex_lock( &p_intf->change_lock );
358 p_intf->p_sys->p_input = p_input;
359 p_intf->p_sys->p_cmml_decoder = p_cmml_decoder;
361 p_intf->p_sys->i_key_action = 0;
363 vlc_mutex_unlock( &p_intf->change_lock );
373 /*****************************************************************************
374 * MouseEvent: callback for mouse events
375 *****************************************************************************/
376 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
377 vlc_value_t oldval, vlc_value_t newval, void *p_data )
379 VLC_UNUSED(p_this); VLC_UNUSED(psz_var);
380 VLC_UNUSED(oldval); VLC_UNUSED(newval);
382 /* TODO: handle mouse clicks on the anchor text */
387 /*****************************************************************************
388 * KeyEvent: callback for keyboard events
389 *****************************************************************************/
390 static int KeyEvent( vlc_object_t *p_this, char const *psz_var,
391 vlc_value_t oldval, vlc_value_t newval, void *p_data )
393 VLC_UNUSED(p_this); VLC_UNUSED(psz_var);
394 VLC_UNUSED(oldval); VLC_UNUSED(newval);
395 intf_thread_t *p_intf = (intf_thread_t *)p_data;
396 vlc_mutex_lock( &p_intf->change_lock );
398 p_intf->p_sys->i_key_action = newval.i_int;
400 vlc_mutex_unlock( &p_intf->change_lock );
405 /*****************************************************************************
406 * FollowAnchor: follow the current anchor being displayed to the user
407 *****************************************************************************/
408 static void FollowAnchor ( intf_thread_t *p_intf )
411 decoder_t *p_cmml_decoder;
412 char *psz_url = NULL;
415 msg_Dbg( p_intf, "User followed anchor" );
417 p_sys = p_intf->p_sys;
418 p_cmml_decoder = p_sys->p_cmml_decoder;
420 if( var_Get( p_cmml_decoder, "psz-current-anchor-url", &val ) ==
423 if( val.p_address ) psz_url = val.p_address;
426 #ifdef CMML_INTF_DEBUG
427 msg_Dbg( p_intf, "Current URL is \"%s\"", psz_url );
432 playlist_t *p_playlist;
433 playlist_item_t *p_current_item;
434 char *psz_uri_to_load;
438 p_playlist = pl_Yield( p_intf );
441 p_current_item = p_playlist->status.p_item;
442 char *psz_uri = input_item_GetURI( p_current_item->p_input );
443 #ifdef CMML_INTF_DEBUG
444 msg_Dbg( p_intf, "Current playlist item URL is \"%s\"", psz_uri );
447 psz_uri_to_load = XURL_Concat( psz_uri, psz_url );
450 #ifdef CMML_INTF_DEBUG
451 msg_Dbg( p_intf, "URL to load is \"%s\"", psz_uri_to_load );
454 if( var_Get( p_intf->p_sys->p_input, "time", &time ) )
456 msg_Dbg( p_intf, "couldn't get time from current clip" );
459 i_seconds = time.i_time / 1000000;
460 #ifdef CMML_INTF_DEBUG
461 msg_Dbg( p_intf, "Current time is \"%lld\"", i_seconds );
464 /* TODO: we need a (much) more robust way of detecting whether
465 * the file's a media file ... */
466 if( strstr( psz_uri_to_load, ".anx" ) != NULL )
468 history_t *p_history = NULL;
469 history_item_t *p_history_item = NULL;
472 p_history = GetHistory( p_playlist );
474 /* create history item */
475 psz_timed_url = GetTimedURLFromPlaylistItem( p_intf, p_current_item );
476 p_history_item = historyItem_New( psz_timed_url, psz_timed_url );
477 free( psz_timed_url );
479 if( !p_history_item )
481 msg_Warn( p_intf, "could not initialise history item" );
485 #ifdef CMML_INTF_DEBUG
486 msg_Dbg( p_intf, "history pre-index %d", p_history->i_index );
488 history_PruneAndInsert( p_history, p_history_item );
489 #ifdef CMML_INTF_DEBUG
490 msg_Dbg( p_intf, "new history item at %p, uri is \"%s\"",
491 p_history_item, p_history_item->psz_uri );
492 msg_Dbg( p_intf, "history index now %d", p_history->i_index );
496 /* free current-anchor-url */
498 val.p_address = NULL;
499 if( var_Set( p_cmml_decoder, "psz-current-anchor-url", val ) !=
502 msg_Dbg( p_intf, "couldn't reset psz-current-anchor-url" );
505 ReplacePlaylistItem( p_playlist, psz_uri_to_load );
509 #ifdef CMML_INTF_DEBUG
510 msg_Dbg( p_intf, "calling browser_Open with \"%s\"", psz_url );
512 (void) browser_Open( psz_url );
513 playlist_Control( p_playlist, PLAYLIST_PAUSE, pl_Unlocked, 0 );
516 free( psz_uri_to_load );
518 vlc_object_release( p_playlist );
523 char *GetTimedURLFromPlaylistItem( intf_thread_t *p_intf,
524 playlist_item_t *p_current_item )
526 #ifdef CMML_INTF_USE_TIMED_URIS
527 char *psz_url = NULL;
528 char *psz_return_value = NULL;
529 char *psz_seconds = NULL;
532 char *psz_uri = input_item_GetURI( p_current_item->p_input );
533 psz_url = XURL_GetWithoutFragment( psz_uri );
536 /* Get current time as a string */
537 if( XURL_IsFileURL( psz_url ) == true )
538 psz_url = xstrcat( psz_url, "#" );
540 psz_url = xstrcat( psz_url, "?" );
542 /* jump back to 2 seconds before where we are now */
543 i_seconds = GetCurrentTimeInSeconds( p_intf->p_sys->p_input ) - 2;
544 psz_seconds = GetTimedURIFragmentForTime( i_seconds < 0 ? 0 : i_seconds );
547 psz_url = xstrcat( psz_url, psz_seconds );
549 psz_return_value = psz_url;
552 return psz_return_value;
557 /* Suppress warning messages about unused functions */
558 p = GetTimedURIFragmentForTime; /* unused */
559 p = GetCurrentTimeInSeconds; /* unused */
561 return input_item_GetURI( p_current_item->p_input );
567 * Get the current time, rounded down to the nearest second
569 * http://www.ietf.org/internet-drafts/draft-pfeiffer-temporal-fragments-02.txt
572 int GetCurrentTimeInSeconds( input_thread_t *p_input )
577 var_Get( p_input, "time", &time );
578 i_seconds = time.i_time / 1000000;
583 char *GetTimedURIFragmentForTime( int seconds )
587 asprintf( &psz_time, "%d", seconds );
592 int GoBackCallback( vlc_object_t *p_this, char const *psz_var,
593 vlc_value_t oldval, vlc_value_t newval, void *p_data )
595 VLC_UNUSED(p_this); VLC_UNUSED(psz_var);
596 VLC_UNUSED(oldval); VLC_UNUSED(newval);
597 intf_thread_t *p_intf = (intf_thread_t *) p_data;
603 int GoForwardCallback( vlc_object_t *p_this, char const *psz_var,
604 vlc_value_t oldval, vlc_value_t newval, void *p_data )
606 VLC_UNUSED(p_this); VLC_UNUSED(psz_var);
607 VLC_UNUSED(oldval); VLC_UNUSED(newval);
608 intf_thread_t *p_intf = (intf_thread_t *) p_data;
614 int FollowAnchorCallback( vlc_object_t *p_this, char const *psz_var,
615 vlc_value_t oldval, vlc_value_t newval,
618 VLC_UNUSED(p_this); VLC_UNUSED(psz_var);
619 VLC_UNUSED(oldval); VLC_UNUSED(newval);
620 intf_thread_t *p_intf = (intf_thread_t *) p_data;
621 FollowAnchor( p_intf );
626 void GoBack( intf_thread_t *p_intf )
629 history_t *p_history = NULL;
630 history_item_t *p_history_item = NULL;
631 history_item_t *p_new_history_item = NULL;
632 playlist_t *p_playlist = NULL;
633 char *psz_timed_url = NULL;
634 playlist_item_t *p_current_item;
636 #ifdef CMML_INTF_DEBUG
637 msg_Dbg( p_intf, "Going back in navigation history" );
640 /* Find the playlist */
641 p_playlist = pl_Yield( p_intf );
643 /* Retrieve navigation history from playlist */
644 if( var_Get( p_playlist, "navigation-history", &history ) != VLC_SUCCESS ||
647 /* History doesn't exist yet: ignore user's request */
648 msg_Warn( p_intf, "can't go back: no history exists yet" );
649 vlc_object_release( p_playlist );
653 p_history = history.p_address;
654 #ifdef CMML_INTF_DEBUG
655 msg_Dbg( p_intf, "back: nav history retrieved from %p", p_history );
656 msg_Dbg( p_intf, "nav history index:%d, p_xarray:%p", p_history->i_index,
657 p_history->p_xarray );
660 /* Check whether we can go back in the history */
661 if( history_CanGoBack( p_history ) == false )
663 msg_Warn( p_intf, "can't go back: already at beginning of history" );
664 vlc_object_release( p_playlist );
668 p_current_item = p_playlist->status.p_item;
670 /* Save the currently-playing media in a new history item */
671 psz_timed_url = GetTimedURLFromPlaylistItem( p_intf, p_current_item );
672 p_new_history_item = historyItem_New( psz_timed_url, psz_timed_url );
673 free( psz_timed_url );
675 if( !p_new_history_item )
677 #ifdef CMML_INTF_DEBUG
678 msg_Dbg( p_intf, "back: could not initialise new history item" );
680 vlc_object_release( p_playlist );
684 /* Go back in the history, saving the currently-playing item */
685 (void) history_GoBackSavingCurrentItem( p_history, p_new_history_item );
686 p_history_item = history_Item( p_history );
688 #ifdef CMML_INTF_DEBUG
689 msg_Dbg( p_intf, "retrieving item from h index %d", p_history->i_index );
690 msg_Dbg( p_intf, "got previous history item: %p", p_history_item );
691 msg_Dbg( p_intf, "prev history item URL: \"%s\"", p_history_item->psz_uri );
694 ReplacePlaylistItem( p_playlist, p_history_item->psz_uri );
695 vlc_object_release( p_playlist );
699 void GoForward( intf_thread_t *p_intf )
702 history_t *p_history = NULL;
703 history_item_t *p_history_item = NULL;
704 history_item_t *p_new_history_item = NULL;
705 playlist_t *p_playlist = NULL;
706 playlist_item_t *p_current_item;
708 #ifdef CMML_INTF_DEBUG
709 msg_Dbg( p_intf, "Going forward in navigation history" );
712 /* Find the playlist */
713 p_playlist = pl_Yield( p_intf );
715 /* Retrieve navigation history from playlist */
716 if( var_Get( p_playlist, "navigation-history", &history ) != VLC_SUCCESS ||
719 /* History doesn't exist yet: ignore user's request */
720 msg_Warn( p_intf, "can't go back: no history exists yet" );
721 vlc_object_release( p_playlist );
725 p_history = history.p_address;
726 #ifdef CMML_INTF_DEBUG
727 msg_Dbg( p_intf, "forward: nav history retrieved from %p", p_history );
728 msg_Dbg( p_intf, "nav history index:%d, p_xarray:%p", p_history->i_index,
729 p_history->p_xarray );
732 /* Check whether we can go forward in the history */
733 if( history_CanGoForward( p_history ) == false )
735 msg_Warn( p_intf, "can't go forward: already at end of history" );
736 vlc_object_release( p_playlist );
740 /* Save the currently-playing media in a new history item */
741 p_new_history_item = malloc( sizeof(history_item_t) );
742 if( !p_new_history_item )
744 #ifdef CMML_INTF_DEBUG
745 msg_Dbg( p_intf, "forward: could not initialise new history item" );
747 vlc_object_release( p_playlist );
750 p_current_item = p_playlist->status.p_item;
751 p_new_history_item->psz_uri = GetTimedURLFromPlaylistItem( p_intf,
753 p_new_history_item->psz_name = p_new_history_item->psz_uri;
755 /* Go forward in the history, saving the currently-playing item */
756 (void) history_GoForwardSavingCurrentItem( p_history, p_new_history_item );
757 p_history_item = history_Item( p_history );
759 #ifdef CMML_INTF_DEBUG
760 msg_Dbg( p_intf, "retrieving item from h index %d", p_history->i_index );
761 msg_Dbg( p_intf, "got next history item: %p", p_history_item );
762 msg_Dbg( p_intf, "next history item URL: \"%s\"", p_history_item->psz_uri );
765 ReplacePlaylistItem( p_playlist, p_history_item->psz_uri );
766 vlc_object_release( p_playlist );
769 static void ReplacePlaylistItem( playlist_t *p_playlist, char *psz_uri )
771 playlist_Stop( p_playlist );
772 (void) playlist_Add( p_playlist, psz_uri, psz_uri,
773 PLAYLIST_INSERT /* FIXME: used to be PLAYLIST_REPLACE */, PLAYLIST_END|PLAYLIST_GO, true /* FIXME: p_playlist->status.i_index */,
777 /****************************************************************************
778 * DisplayAnchor: displays an anchor on the given video output
779 ****************************************************************************/
780 static int DisplayAnchor( intf_thread_t *p_intf,
781 vout_thread_t *p_vout,
782 char *psz_anchor_description,
783 char *psz_anchor_url )
785 int i_margin_h, i_margin_v;
797 /* Should display subtitle underlined and in blue, but it looks
798 * like VLC doesn't implement any text styles yet. D'oh! */
799 // p_style = &blue_with_underline;
803 /* TODO: p_subpicture doesn't have the proper i_x and i_y
804 * coordinates. Need to look at the subpicture display system to
806 if ( vout_ShowTextAbsolute( p_vout, DEFAULT_CHAN,
807 psz_anchor_description, NULL, OSD_ALIGN_BOTTOM,
808 i_margin_h, i_margin_v, i_now, 0 ) == VLC_SUCCESS )
810 /* Displayed successfully */
819 msg_Dbg( p_intf, "DisplayAnchor couldn't find a video output" );
826 static history_t * GetHistory( playlist_t *p_playlist )
829 history_t *p_history = NULL;
831 if( var_Get( p_playlist, "navigation-history", &val ) != VLC_SUCCESS )
833 /* history doesn't exist yet: need to create it */
834 history_t *new_history = history_New();
835 val.p_address = new_history;
836 var_Create( p_playlist, "navigation-history",
837 VLC_VAR_ADDRESS|VLC_VAR_DOINHERIT );
838 if( var_Set( p_playlist, "navigation-history", val ) != VLC_SUCCESS )
840 msg_Warn( p_playlist, "could not initialise history" );
844 p_history = new_history;
845 #ifdef CMML_INTF_HISTORY_DEBUG
846 msg_Dbg( p_playlist, "nav history created at %p", new_history );
847 msg_Dbg( p_playlist, "nav history index:%d, p_xarray:%p",
848 p_history->i_index, p_history->p_xarray );
854 p_history = val.p_address;
855 #ifdef CMML_INTF_HISTORY_DEBUG
856 msg_Dbg( p_playlist, "nav history retrieved from %p", p_history );