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 int canc = vlc_savecancel();
181 vout_thread_t * p_vout = NULL;
183 if( InitThread( p_intf ) < 0 )
185 msg_Err( p_intf, "can't initialize CMML interface" );
188 #ifdef CMML_INTF_DEBUG
189 msg_Dbg( p_intf, "CMML intf initialized" );
193 while( vlc_object_alive (p_intf) )
195 /* if video output is dying, disassociate ourselves from it */
196 if( p_vout && !vlc_object_alive (p_vout) )
198 var_DelCallback( p_vout, "mouse-clicked", MouseEvent, p_intf );
199 vlc_object_release( p_vout );
203 /* find a video output if we currently don't have one */
206 p_vout = vlc_object_find( p_intf->p_sys->p_input,
207 VLC_OBJECT_VOUT, FIND_CHILD );
210 #ifdef CMML_INTF_DEBUG
211 msg_Dbg( p_intf, "found vout thread" );
213 var_AddCallback( p_vout, "mouse-clicked", MouseEvent, p_intf );
217 vlc_mutex_lock( &p_intf->change_lock );
222 switch( p_intf->p_sys->i_key_action )
224 case ACTIONID_NAV_ACTIVATE:
225 FollowAnchor( p_intf );
227 case ACTIONID_HISTORY_BACK:
230 case ACTIONID_HISTORY_FORWARD:
236 p_intf->p_sys->i_key_action = 0;
237 vlc_mutex_unlock( &p_intf->change_lock );
239 (void) DisplayPendingAnchor( p_intf, p_vout );
242 msleep( INTF_IDLE_SLEEP );
245 /* if we're here, the video output is dying: release the vout object */
249 var_DelCallback( p_vout, "mouse-clicked", MouseEvent, p_intf );
250 vlc_object_release( p_vout );
253 vlc_object_release( p_intf->p_sys->p_input );
254 vlc_restorecancel( canc );
257 /*****************************************************************************
258 * DisplayPendingAnchor: get a pending anchor description/URL from the CMML
259 * decoder and display it on screen
260 *****************************************************************************/
261 static int DisplayPendingAnchor( intf_thread_t *p_intf, vout_thread_t *p_vout )
263 decoder_t *p_cmml_decoder;
264 char *psz_description = NULL;
265 char *psz_url = NULL;
267 intf_thread_t *p_primary_intf;
270 p_cmml_decoder = p_intf->p_sys->p_cmml_decoder;
271 if( var_Get( p_cmml_decoder, "psz-current-anchor-description", &val )
280 psz_description = val.p_address;
282 if( var_Get( p_cmml_decoder, "psz-current-anchor-url", &val )
285 psz_url = val.p_address;
290 /* don't display anchor if main interface can display it */
291 p_primary_intf = vlc_object_find( p_intf->p_libvlc, VLC_OBJECT_INTF,
296 if( var_Get( p_primary_intf, "intf-displays-cmml-description", &val )
299 if( val.b_bool == true )
301 vlc_object_release( p_primary_intf );
306 vlc_object_release( p_primary_intf );
309 /* display anchor as subtitle on-screen */
310 if( DisplayAnchor( p_intf, p_vout, psz_description, psz_url )
313 /* text render unsuccessful: do nothing */
317 /* text render successful: clear description */
318 val.p_address = NULL;
319 if( var_Set( p_cmml_decoder, "psz-current-anchor-description", val )
323 "reset of psz-current-anchor-description failed" );
325 free( psz_description );
333 /*****************************************************************************
335 *****************************************************************************/
336 static int InitThread( intf_thread_t * p_intf )
338 /* We might need some locking here */
339 if( vlc_object_alive (p_intf) )
341 input_thread_t * p_input;
342 decoder_t *p_cmml_decoder;
344 p_cmml_decoder = vlc_object_find( p_intf, VLC_OBJECT_DECODER, FIND_PARENT );
345 p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT, FIND_PARENT );
347 #ifdef CMML_INTF_DEBUG
348 msg_Dbg( p_intf, "cmml decoder at %p, input thread at %p",
349 p_cmml_decoder, p_input );
352 /* Maybe the input just died */
353 if( p_input == NULL )
358 vlc_mutex_lock( &p_intf->change_lock );
360 p_intf->p_sys->p_input = p_input;
361 p_intf->p_sys->p_cmml_decoder = p_cmml_decoder;
363 p_intf->p_sys->i_key_action = 0;
365 vlc_mutex_unlock( &p_intf->change_lock );
375 /*****************************************************************************
376 * MouseEvent: callback for mouse events
377 *****************************************************************************/
378 static int MouseEvent( vlc_object_t *p_this, char const *psz_var,
379 vlc_value_t oldval, vlc_value_t newval, void *p_data )
381 VLC_UNUSED(p_this); VLC_UNUSED(psz_var);
382 VLC_UNUSED(oldval); VLC_UNUSED(newval);
384 /* TODO: handle mouse clicks on the anchor text */
389 /*****************************************************************************
390 * KeyEvent: callback for keyboard events
391 *****************************************************************************/
392 static int KeyEvent( vlc_object_t *p_this, char const *psz_var,
393 vlc_value_t oldval, vlc_value_t newval, void *p_data )
395 VLC_UNUSED(p_this); VLC_UNUSED(psz_var);
396 VLC_UNUSED(oldval); VLC_UNUSED(newval);
397 intf_thread_t *p_intf = (intf_thread_t *)p_data;
398 vlc_mutex_lock( &p_intf->change_lock );
400 p_intf->p_sys->i_key_action = newval.i_int;
402 vlc_mutex_unlock( &p_intf->change_lock );
407 /*****************************************************************************
408 * FollowAnchor: follow the current anchor being displayed to the user
409 *****************************************************************************/
410 static void FollowAnchor ( intf_thread_t *p_intf )
413 decoder_t *p_cmml_decoder;
414 char *psz_url = NULL;
417 msg_Dbg( p_intf, "User followed anchor" );
419 p_sys = p_intf->p_sys;
420 p_cmml_decoder = p_sys->p_cmml_decoder;
422 if( var_Get( p_cmml_decoder, "psz-current-anchor-url", &val ) ==
425 if( val.p_address ) psz_url = val.p_address;
428 #ifdef CMML_INTF_DEBUG
429 msg_Dbg( p_intf, "Current URL is \"%s\"", psz_url );
434 playlist_t *p_playlist;
435 playlist_item_t *p_current_item;
436 char *psz_uri_to_load;
440 p_playlist = pl_Yield( p_intf );
443 p_current_item = p_playlist->status.p_item;
444 char *psz_uri = input_item_GetURI( p_current_item->p_input );
445 #ifdef CMML_INTF_DEBUG
446 msg_Dbg( p_intf, "Current playlist item URL is \"%s\"", psz_uri );
449 psz_uri_to_load = XURL_Concat( psz_uri, psz_url );
452 #ifdef CMML_INTF_DEBUG
453 msg_Dbg( p_intf, "URL to load is \"%s\"", psz_uri_to_load );
456 if( var_Get( p_intf->p_sys->p_input, "time", &time ) )
458 msg_Dbg( p_intf, "couldn't get time from current clip" );
461 i_seconds = time.i_time / 1000000;
462 #ifdef CMML_INTF_DEBUG
463 msg_Dbg( p_intf, "Current time is \"%lld\"", i_seconds );
466 /* TODO: we need a (much) more robust way of detecting whether
467 * the file's a media file ... */
468 if( strstr( psz_uri_to_load, ".anx" ) != NULL )
470 history_t *p_history = NULL;
471 history_item_t *p_history_item = NULL;
474 p_history = GetHistory( p_playlist );
476 /* create history item */
477 psz_timed_url = GetTimedURLFromPlaylistItem( p_intf, p_current_item );
478 p_history_item = historyItem_New( psz_timed_url, psz_timed_url );
479 free( psz_timed_url );
481 if( !p_history_item )
483 msg_Warn( p_intf, "could not initialise history item" );
487 #ifdef CMML_INTF_DEBUG
488 msg_Dbg( p_intf, "history pre-index %d", p_history->i_index );
490 history_PruneAndInsert( p_history, p_history_item );
491 #ifdef CMML_INTF_DEBUG
492 msg_Dbg( p_intf, "new history item at %p, uri is \"%s\"",
493 p_history_item, p_history_item->psz_uri );
494 msg_Dbg( p_intf, "history index now %d", p_history->i_index );
498 /* free current-anchor-url */
500 val.p_address = NULL;
501 if( var_Set( p_cmml_decoder, "psz-current-anchor-url", val ) !=
504 msg_Dbg( p_intf, "couldn't reset psz-current-anchor-url" );
507 ReplacePlaylistItem( p_playlist, psz_uri_to_load );
511 #ifdef CMML_INTF_DEBUG
512 msg_Dbg( p_intf, "calling browser_Open with \"%s\"", psz_url );
514 (void) browser_Open( psz_url );
515 playlist_Control( p_playlist, PLAYLIST_PAUSE, pl_Unlocked, 0 );
518 free( psz_uri_to_load );
520 vlc_object_release( p_playlist );
525 char *GetTimedURLFromPlaylistItem( intf_thread_t *p_intf,
526 playlist_item_t *p_current_item )
528 #ifdef CMML_INTF_USE_TIMED_URIS
529 char *psz_url = NULL;
530 char *psz_return_value = NULL;
531 char *psz_seconds = NULL;
534 char *psz_uri = input_item_GetURI( p_current_item->p_input );
535 psz_url = XURL_GetWithoutFragment( psz_uri );
538 /* Get current time as a string */
539 if( XURL_IsFileURL( psz_url ) == true )
540 psz_url = xstrcat( psz_url, "#" );
542 psz_url = xstrcat( psz_url, "?" );
544 /* jump back to 2 seconds before where we are now */
545 i_seconds = GetCurrentTimeInSeconds( p_intf->p_sys->p_input ) - 2;
546 psz_seconds = GetTimedURIFragmentForTime( i_seconds < 0 ? 0 : i_seconds );
549 psz_url = xstrcat( psz_url, psz_seconds );
551 psz_return_value = psz_url;
554 return psz_return_value;
559 /* Suppress warning messages about unused functions */
560 p = GetTimedURIFragmentForTime; /* unused */
561 p = GetCurrentTimeInSeconds; /* unused */
563 return input_item_GetURI( p_current_item->p_input );
569 * Get the current time, rounded down to the nearest second
571 * http://www.ietf.org/internet-drafts/draft-pfeiffer-temporal-fragments-02.txt
574 int GetCurrentTimeInSeconds( input_thread_t *p_input )
579 var_Get( p_input, "time", &time );
580 i_seconds = time.i_time / 1000000;
585 char *GetTimedURIFragmentForTime( int seconds )
589 if( asprintf( &psz_time, "%d", seconds ) == -1 )
595 int GoBackCallback( vlc_object_t *p_this, char const *psz_var,
596 vlc_value_t oldval, vlc_value_t newval, void *p_data )
598 VLC_UNUSED(p_this); VLC_UNUSED(psz_var);
599 VLC_UNUSED(oldval); VLC_UNUSED(newval);
600 intf_thread_t *p_intf = (intf_thread_t *) p_data;
606 int GoForwardCallback( vlc_object_t *p_this, char const *psz_var,
607 vlc_value_t oldval, vlc_value_t newval, void *p_data )
609 VLC_UNUSED(p_this); VLC_UNUSED(psz_var);
610 VLC_UNUSED(oldval); VLC_UNUSED(newval);
611 intf_thread_t *p_intf = (intf_thread_t *) p_data;
617 int FollowAnchorCallback( vlc_object_t *p_this, char const *psz_var,
618 vlc_value_t oldval, vlc_value_t newval,
621 VLC_UNUSED(p_this); VLC_UNUSED(psz_var);
622 VLC_UNUSED(oldval); VLC_UNUSED(newval);
623 intf_thread_t *p_intf = (intf_thread_t *) p_data;
624 FollowAnchor( p_intf );
629 void GoBack( intf_thread_t *p_intf )
632 history_t *p_history = NULL;
633 history_item_t *p_history_item = NULL;
634 history_item_t *p_new_history_item = NULL;
635 playlist_t *p_playlist = NULL;
636 char *psz_timed_url = NULL;
637 playlist_item_t *p_current_item;
639 #ifdef CMML_INTF_DEBUG
640 msg_Dbg( p_intf, "Going back in navigation history" );
643 /* Find the playlist */
644 p_playlist = pl_Yield( p_intf );
646 /* Retrieve navigation history from playlist */
647 if( var_Get( p_playlist, "navigation-history", &history ) != VLC_SUCCESS ||
650 /* History doesn't exist yet: ignore user's request */
651 msg_Warn( p_intf, "can't go back: no history exists yet" );
652 vlc_object_release( p_playlist );
656 p_history = history.p_address;
657 #ifdef CMML_INTF_DEBUG
658 msg_Dbg( p_intf, "back: nav history retrieved from %p", p_history );
659 msg_Dbg( p_intf, "nav history index:%d, p_xarray:%p", p_history->i_index,
660 p_history->p_xarray );
663 /* Check whether we can go back in the history */
664 if( history_CanGoBack( p_history ) == false )
666 msg_Warn( p_intf, "can't go back: already at beginning of history" );
667 vlc_object_release( p_playlist );
671 p_current_item = p_playlist->status.p_item;
673 /* Save the currently-playing media in a new history item */
674 psz_timed_url = GetTimedURLFromPlaylistItem( p_intf, p_current_item );
675 p_new_history_item = historyItem_New( psz_timed_url, psz_timed_url );
676 free( psz_timed_url );
678 if( !p_new_history_item )
680 #ifdef CMML_INTF_DEBUG
681 msg_Dbg( p_intf, "back: could not initialise new history item" );
683 vlc_object_release( p_playlist );
687 /* Go back in the history, saving the currently-playing item */
688 (void) history_GoBackSavingCurrentItem( p_history, p_new_history_item );
689 p_history_item = history_Item( p_history );
691 #ifdef CMML_INTF_DEBUG
692 msg_Dbg( p_intf, "retrieving item from h index %d", p_history->i_index );
693 msg_Dbg( p_intf, "got previous history item: %p", p_history_item );
694 msg_Dbg( p_intf, "prev history item URL: \"%s\"", p_history_item->psz_uri );
697 ReplacePlaylistItem( p_playlist, p_history_item->psz_uri );
698 vlc_object_release( p_playlist );
702 void GoForward( intf_thread_t *p_intf )
705 history_t *p_history = NULL;
706 history_item_t *p_history_item = NULL;
707 history_item_t *p_new_history_item = NULL;
708 playlist_t *p_playlist = NULL;
709 playlist_item_t *p_current_item;
711 #ifdef CMML_INTF_DEBUG
712 msg_Dbg( p_intf, "Going forward in navigation history" );
715 /* Find the playlist */
716 p_playlist = pl_Yield( p_intf );
718 /* Retrieve navigation history from playlist */
719 if( var_Get( p_playlist, "navigation-history", &history ) != VLC_SUCCESS ||
722 /* History doesn't exist yet: ignore user's request */
723 msg_Warn( p_intf, "can't go back: no history exists yet" );
724 vlc_object_release( p_playlist );
728 p_history = history.p_address;
729 #ifdef CMML_INTF_DEBUG
730 msg_Dbg( p_intf, "forward: nav history retrieved from %p", p_history );
731 msg_Dbg( p_intf, "nav history index:%d, p_xarray:%p", p_history->i_index,
732 p_history->p_xarray );
735 /* Check whether we can go forward in the history */
736 if( history_CanGoForward( p_history ) == false )
738 msg_Warn( p_intf, "can't go forward: already at end of history" );
739 vlc_object_release( p_playlist );
743 /* Save the currently-playing media in a new history item */
744 p_new_history_item = malloc( sizeof(history_item_t) );
745 if( !p_new_history_item )
747 #ifdef CMML_INTF_DEBUG
748 msg_Dbg( p_intf, "forward: could not initialise new history item" );
750 vlc_object_release( p_playlist );
753 p_current_item = p_playlist->status.p_item;
754 p_new_history_item->psz_uri = GetTimedURLFromPlaylistItem( p_intf,
756 p_new_history_item->psz_name = p_new_history_item->psz_uri;
758 /* Go forward in the history, saving the currently-playing item */
759 (void) history_GoForwardSavingCurrentItem( p_history, p_new_history_item );
760 p_history_item = history_Item( p_history );
762 #ifdef CMML_INTF_DEBUG
763 msg_Dbg( p_intf, "retrieving item from h index %d", p_history->i_index );
764 msg_Dbg( p_intf, "got next history item: %p", p_history_item );
765 msg_Dbg( p_intf, "next history item URL: \"%s\"", p_history_item->psz_uri );
768 ReplacePlaylistItem( p_playlist, p_history_item->psz_uri );
769 vlc_object_release( p_playlist );
772 static void ReplacePlaylistItem( playlist_t *p_playlist, char *psz_uri )
774 playlist_Stop( p_playlist );
775 (void) playlist_Add( p_playlist, psz_uri, psz_uri,
776 PLAYLIST_INSERT /* FIXME: used to be PLAYLIST_REPLACE */, PLAYLIST_END|PLAYLIST_GO, true /* FIXME: p_playlist->status.i_index */,
780 /****************************************************************************
781 * DisplayAnchor: displays an anchor on the given video output
782 ****************************************************************************/
783 static int DisplayAnchor( intf_thread_t *p_intf,
784 vout_thread_t *p_vout,
785 char *psz_anchor_description,
786 char *psz_anchor_url )
788 int i_margin_h, i_margin_v;
800 /* Should display subtitle underlined and in blue, but it looks
801 * like VLC doesn't implement any text styles yet. D'oh! */
802 // p_style = &blue_with_underline;
806 /* TODO: p_subpicture doesn't have the proper i_x and i_y
807 * coordinates. Need to look at the subpicture display system to
809 if ( vout_ShowTextAbsolute( p_vout, DEFAULT_CHAN,
810 psz_anchor_description, NULL, OSD_ALIGN_BOTTOM,
811 i_margin_h, i_margin_v, i_now, 0 ) == VLC_SUCCESS )
813 /* Displayed successfully */
822 msg_Dbg( p_intf, "DisplayAnchor couldn't find a video output" );
829 static history_t * GetHistory( playlist_t *p_playlist )
832 history_t *p_history = NULL;
834 if( var_Get( p_playlist, "navigation-history", &val ) != VLC_SUCCESS )
836 /* history doesn't exist yet: need to create it */
837 history_t *new_history = history_New();
838 val.p_address = new_history;
839 var_Create( p_playlist, "navigation-history",
840 VLC_VAR_ADDRESS|VLC_VAR_DOINHERIT );
841 if( var_Set( p_playlist, "navigation-history", val ) != VLC_SUCCESS )
843 msg_Warn( p_playlist, "could not initialise history" );
847 p_history = new_history;
848 #ifdef CMML_INTF_HISTORY_DEBUG
849 msg_Dbg( p_playlist, "nav history created at %p", new_history );
850 msg_Dbg( p_playlist, "nav history index:%d, p_xarray:%p",
851 p_history->i_index, p_history->p_xarray );
857 p_history = val.p_address;
858 #ifdef CMML_INTF_HISTORY_DEBUG
859 msg_Dbg( p_playlist, "nav history retrieved from %p", p_history );