1 /*****************************************************************************
2 * rss.c : rss feed display video plugin for vlc
3 *****************************************************************************
4 * Copyright (C) 2003-2005 the VideoLAN team
7 * Authors: Antoine Cellerier <dionoea@videolan.org>
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.
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.
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., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
22 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* malloc(), free() */
33 #include "vlc_filter.h"
34 #include "vlc_block.h"
37 #include "vlc_block.h"
38 #include "vlc_stream.h"
41 /*****************************************************************************
43 *****************************************************************************/
44 static int CreateFilter ( vlc_object_t * );
45 static void DestroyFilter( vlc_object_t * );
46 static subpicture_t *Filter( filter_t *, mtime_t );
48 static int FetchRSS( filter_t * );
49 static void FreeRSS( filter_t * );
51 static int pi_color_values[] = { 0xf0000000, 0x00000000, 0x00808080, 0x00C0C0C0,
52 0x00FFFFFF, 0x00800000, 0x00FF0000, 0x00FF00FF, 0x00FFFF00,
53 0x00808000, 0x00008000, 0x00008080, 0x0000FF00, 0x00800080,
54 0x00000080, 0x000000FF, 0x0000FFFF};
55 static char *ppsz_color_descriptions[] = { N_("Default"), N_("Black"),
56 N_("Gray"), N_("Silver"), N_("White"), N_("Maroon"), N_("Red"),
57 N_("Fuchsia"), N_("Yellow"), N_("Olive"), N_("Green"),
58 N_("Teal"), N_("Lime"), N_("Purple"), N_("Navy"), N_("Blue"),
61 /*****************************************************************************
62 * filter_sys_t: rss filter descriptor
63 *****************************************************************************/
68 char *psz_description;
75 char *psz_description;
79 struct rss_item_t *p_items;
87 int i_xoff, i_yoff; /* offsets for the display string in the video window */
88 int i_pos; /* permit relative positioning (top, bottom, left, right, center) */
92 char *psz_marquee; /* marquee string */
94 int i_font_color, i_font_opacity, i_font_size; /* font control */
100 struct rss_feed_t *p_feeds;
103 time_t t_last_update;
110 #define MSG_TEXT N_("RSS feed URLs")
111 #define MSG_LONGTEXT N_("RSS feed '|' (pipe) seperated URLs")
112 #define SPEED_TEXT N_("RSS feed speed")
113 #define SPEED_LONGTEXT N_("RSS feed speed (bigger is slower)")
114 #define LENGTH_TEXT N_("RSS feed max number of chars displayed")
115 #define LENGTH_LONGTEXT N_("RSS feed max number of chars displayed")
116 #define TTL_TEXT N_("Number of seconds between each forced refresh of the feeds")
117 #define TTL_LONGTEXT N_("Number of seconds between each forced refresh of the feeds. If 0, the feeds will never be updated.")
120 #define POSX_TEXT N_("X offset, from left")
121 #define POSX_LONGTEXT N_("X offset, from the left screen edge" )
122 #define POSY_TEXT N_("Y offset, from the top")
123 #define POSY_LONGTEXT N_("Y offset, down from the top" )
124 #define OPACITY_TEXT N_("Opacity")
125 #define OPACITY_LONGTEXT N_("The opacity (inverse of transparency) of " \
126 "overlay text. 0 = transparent, 255 = totally opaque. " )
127 #define SIZE_TEXT N_("Font size, pixels")
128 #define SIZE_LONGTEXT N_("Specify the font size, in pixels, " \
129 "with -1 = use freetype-fontsize" )
131 #define COLOR_TEXT N_("Text Default Color")
132 #define COLOR_LONGTEXT N_("The color of overlay text. 1 byte for each color, hexadecimal. " \
133 "#000000 = all colors off, " \
134 "0xFF0000 = just Red, 0xFFFFFF = all color on [White]" )
136 #define POS_TEXT N_("Marquee position")
137 #define POS_LONGTEXT N_( \
138 "You can enforce the marquee position on the video " \
139 "(0=center, 1=left, 2=right, 4=top, 8=bottom, you can " \
140 "also use combinations of these values by adding them).")
142 static int pi_pos_values[] = { 0, 1, 2, 4, 8, 5, 6, 9, 10 };
143 static char *ppsz_pos_descriptions[] =
144 { N_("Center"), N_("Left"), N_("Right"), N_("Top"), N_("Bottom"),
145 N_("Top-Left"), N_("Top-Right"), N_("Bottom-Left"), N_("Bottom-Right") };
147 /*****************************************************************************
149 *****************************************************************************/
151 set_capability( "sub filter", 0 );
152 set_shortname( "RSS" );
153 set_callbacks( CreateFilter, DestroyFilter );
154 set_category( CAT_VIDEO );
155 set_subcategory( SUBCAT_VIDEO_SUBPIC );
156 add_string( "rss-urls", "rss", NULL, MSG_TEXT, MSG_LONGTEXT, VLC_FALSE );
158 set_section( N_("Position"), NULL );
159 add_integer( "rss-x", -1, NULL, POSX_TEXT, POSX_LONGTEXT, VLC_TRUE );
160 add_integer( "rss-y", 0, NULL, POSY_TEXT, POSY_LONGTEXT, VLC_TRUE );
161 add_integer( "rss-position", 5, NULL, POS_TEXT, POS_LONGTEXT, VLC_FALSE );
162 change_integer_list( pi_pos_values, ppsz_pos_descriptions, 0 );
164 set_section( N_("Font"), NULL );
165 /* 5 sets the default to top [1] left [4] */
166 add_integer_with_range( "rss-opacity", 255, 0, 255, NULL,
167 OPACITY_TEXT, OPACITY_LONGTEXT, VLC_FALSE );
168 add_integer( "rss-color", 0xFFFFFF, NULL, COLOR_TEXT, COLOR_LONGTEXT,
170 change_integer_list( pi_color_values, ppsz_color_descriptions, 0 );
171 add_integer( "rss-size", -1, NULL, SIZE_TEXT, SIZE_LONGTEXT, VLC_FALSE );
173 set_section( N_("Misc"), NULL );
174 add_integer( "rss-speed", 100000, NULL, SPEED_TEXT, SPEED_LONGTEXT,
176 add_integer( "rss-length", 60, NULL, LENGTH_TEXT, LENGTH_LONGTEXT,
178 add_integer( "rss-ttl", 1800, NULL, TTL_TEXT, TTL_LONGTEXT, VLC_FALSE );
180 set_description( _("RSS feed display") );
181 add_shortcut( "rss" );
184 /*****************************************************************************
185 * CreateFilter: allocates RSS video filter
186 *****************************************************************************/
187 static int CreateFilter( vlc_object_t *p_this )
189 filter_t *p_filter = (filter_t *)p_this;
193 /* Allocate structure */
194 p_sys = p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
197 msg_Err( p_filter, "out of memory" );
201 vlc_mutex_init( p_filter, &p_sys->lock );
202 vlc_mutex_lock( &p_sys->lock );
204 p_sys->psz_urls = var_CreateGetString( p_filter, "rss-urls" );
205 p_sys->i_cur_feed = 0;
206 p_sys->i_cur_item = 0;
207 p_sys->i_cur_char = 0;
209 p_sys->p_feeds = NULL;
210 p_sys->i_speed = var_CreateGetInteger( p_filter, "rss-speed" );
211 p_sys->i_length = var_CreateGetInteger( p_filter, "rss-length" );
212 p_sys->i_ttl = __MAX( 0, var_CreateGetInteger( p_filter, "rss-ttl" ) );
213 p_sys->psz_marquee = (char *)malloc( p_sys->i_length );
215 p_sys->i_xoff = var_CreateGetInteger( p_filter, "rss-x" );
216 p_sys->i_yoff = var_CreateGetInteger( p_filter, "rss-y" );
217 p_sys->i_pos = var_CreateGetInteger( p_filter, "rss-position" );
218 var_Create( p_filter, "rss-opacity", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
219 p_sys->i_font_opacity = var_CreateGetInteger( p_filter, "rss-opacity" );
220 p_sys->i_font_color = var_CreateGetInteger( p_filter, "rss-color" );
221 p_sys->i_font_size = var_CreateGetInteger( p_filter, "rss-size" );
223 if( FetchRSS( p_filter ) )
225 msg_Err( p_filter, "failed while fetching RSS ... too bad" );
226 vlc_mutex_unlock( &p_sys->lock );
229 p_sys->t_last_update = time( NULL );
231 if( p_sys->i_feeds == 0 )
233 vlc_mutex_unlock( &p_sys->lock );
236 for( i_feed=0; i_feed < p_sys->i_feeds; i_feed ++ )
237 if( p_sys->p_feeds[i_feed].i_items == 0 )
239 vlc_mutex_unlock( &p_sys->lock );
244 p_filter->pf_sub_filter = Filter;
245 p_sys->last_date = (mtime_t)0;
247 vlc_mutex_unlock( &p_sys->lock );
251 /*****************************************************************************
252 * DestroyFilter: destroy RSS video filter
253 *****************************************************************************/
254 static void DestroyFilter( vlc_object_t *p_this )
256 filter_t *p_filter = (filter_t *)p_this;
257 filter_sys_t *p_sys = p_filter->p_sys;
259 vlc_mutex_lock( &p_sys->lock );
261 if( p_sys->psz_marquee ) free( p_sys->psz_marquee );
262 free( p_sys->psz_urls );
264 vlc_mutex_unlock( &p_sys->lock );
265 vlc_mutex_destroy( &p_sys->lock );
268 /* Delete the RSS variables */
269 var_Destroy( p_filter, "rss-urls" );
270 var_Destroy( p_filter, "rss-speed" );
271 var_Destroy( p_filter, "rss-length" );
272 var_Destroy( p_filter, "rss-ttl" );
273 var_Destroy( p_filter, "rss-x" );
274 var_Destroy( p_filter, "rss-y" );
275 var_Destroy( p_filter, "rss-position" );
276 var_Destroy( p_filter, "rss-color");
277 var_Destroy( p_filter, "rss-opacity");
278 var_Destroy( p_filter, "rss-size");
281 /****************************************************************************
282 * Filter: the whole thing
283 ****************************************************************************
284 * This function outputs subpictures at regular time intervals.
285 ****************************************************************************/
286 static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
288 filter_sys_t *p_sys = p_filter->p_sys;
294 vlc_mutex_lock( &p_sys->lock );
297 + ( p_sys->i_cur_char == 0 && p_sys->i_cur_item == 0 ? 5 : 1 )
298 /* ( ... ? 5 : 1 ) means "wait more for the 1st char" */
299 * p_sys->i_speed > date )
301 vlc_mutex_unlock( &p_sys->lock );
305 /* Do we need to update the feeds ? */
307 && time( NULL ) > p_sys->t_last_update + (time_t)p_sys->i_ttl )
309 msg_Dbg( p_filter, "Forcing update of all the RSS feeds" );
310 if( FetchRSS( p_filter ) )
312 msg_Err( p_filter, "failed while fetching RSS ... too bad" );
313 vlc_mutex_unlock( &p_sys->lock );
314 return NULL; /* FIXME : we most likely messed up all the data,
315 * so we might need to do something about it */
317 p_sys->t_last_update = time( NULL );
320 p_sys->last_date = date;
322 if( p_sys->p_feeds[p_sys->i_cur_feed].p_items[p_sys->i_cur_item].psz_title[p_sys->i_cur_char] == 0 )
324 p_sys->i_cur_char = 0;
326 if( p_sys->i_cur_item >= p_sys->p_feeds[p_sys->i_cur_feed].i_items )
328 p_sys->i_cur_item = 0;
329 p_sys->i_cur_feed = (p_sys->i_cur_feed + 1)%p_sys->i_feeds;
333 p_spu = p_filter->pf_sub_buffer_new( p_filter );
336 vlc_mutex_unlock( &p_sys->lock );
340 memset( &fmt, 0, sizeof(video_format_t) );
341 fmt.i_chroma = VLC_FOURCC('T','E','X','T');
347 p_spu->p_region = p_spu->pf_create_region( VLC_OBJECT(p_filter), &fmt );
348 if( !p_spu->p_region )
350 p_filter->pf_sub_buffer_del( p_filter, p_spu );
351 vlc_mutex_unlock( &p_sys->lock );
355 i_item = p_sys->i_cur_item;
356 i_feed = p_sys->i_cur_feed;
357 snprintf( p_sys->psz_marquee, p_sys->i_length, "%s : %s", p_sys->p_feeds[i_feed].psz_title, p_sys->p_feeds[i_feed].p_items[i_item].psz_title+p_sys->i_cur_char );
358 while( strlen( p_sys->psz_marquee ) < (unsigned int)p_sys->i_length )
361 if( i_item == p_sys->p_feeds[i_feed].i_items ) break;
362 snprintf( strchr( p_sys->psz_marquee, 0 ), p_sys->i_length - strlen( p_sys->psz_marquee ), " - %s", p_sys->p_feeds[i_feed].p_items[i_item].psz_title );
365 p_spu->p_region->psz_text = strdup(p_sys->psz_marquee);
366 p_spu->i_start = date;
368 p_spu->b_ephemer = VLC_TRUE;
370 /* where to locate the string: */
371 if( p_sys->i_xoff < 0 || p_sys->i_yoff < 0 )
372 { /* set to one of the 9 relative locations */
373 p_spu->i_flags = p_sys->i_pos;
376 p_spu->b_absolute = VLC_FALSE;
379 { /* set to an absolute xy, referenced to upper left corner */
380 p_spu->i_flags = OSD_ALIGN_LEFT | OSD_ALIGN_TOP;
381 p_spu->i_x = p_sys->i_xoff;
382 p_spu->i_y = p_sys->i_yoff;
383 p_spu->b_absolute = VLC_TRUE;
386 p_spu->p_region->i_text_color = p_sys->i_font_color;
387 p_spu->p_region->i_text_alpha = 255 - p_sys->i_font_opacity;
388 p_spu->p_region->i_text_size = p_sys->i_font_size;
390 vlc_mutex_unlock( &p_sys->lock );
394 /****************************************************************************
395 * RSS related functions
396 ****************************************************************************
397 * You should always lock the p_filter mutex before using any of these
399 ***************************************************************************/
401 /****************************************************************************
403 ***************************************************************************/
404 static int FetchRSS( filter_t *p_filter)
406 filter_sys_t *p_sys = p_filter->p_sys;
408 stream_t *p_stream = NULL;
410 xml_reader_t *p_xml_reader = NULL;
412 char *psz_eltname = NULL;
413 char *psz_eltvalue = NULL;
414 char *psz_feed = NULL;
415 char *psz_buffer = NULL;
416 char *psz_buffer_2 = NULL;
426 while( p_sys->psz_urls[i_int] != 0 )
427 if( p_sys->psz_urls[i_int++] == '|' )
429 p_sys->p_feeds = (struct rss_feed_t *)malloc( p_sys->i_feeds
430 * sizeof( struct rss_feed_t ) );
432 p_xml = xml_Create( p_filter );
435 msg_Err( p_filter, "Failed to open XML parser" );
439 psz_buffer = strdup( p_sys->psz_urls );
440 psz_buffer_2 = psz_buffer; /* keep track so we can free it */
441 for( i_feed = 0; i_feed < p_sys->i_feeds; i_feed++ )
443 struct rss_feed_t *p_feed = p_sys->p_feeds+i_feed;
445 if( psz_buffer == NULL ) break;
446 if( psz_buffer[0] == 0 ) psz_buffer++;
447 psz_feed = psz_buffer;
448 psz_buffer = strchr( psz_buffer, '|' );
449 if( psz_buffer != NULL ) psz_buffer[0] = 0;
451 p_feed->psz_title = NULL;
452 p_feed->psz_description = NULL;
453 p_feed->psz_link = NULL;
455 p_feed->p_items = NULL;
457 msg_Dbg( p_filter, "Opening %s RSS feed ...", psz_feed );
459 p_stream = stream_UrlNew( p_filter, psz_feed );
462 msg_Err( p_filter, "Failed to open %s for reading", psz_feed );
466 p_xml_reader = xml_ReaderCreate( p_xml, p_stream );
469 msg_Err( p_filter, "Failed to open %s for parsing", psz_feed );
474 i_is_item = VLC_FALSE;
476 while( xml_ReaderRead( p_xml_reader ) == 1 )
478 switch( xml_ReaderNodeType( p_xml_reader ) )
484 case XML_READER_STARTELEM:
490 psz_eltname = xml_ReaderName( p_xml_reader );
496 msg_Dbg( p_filter, "element name : %s", psz_eltname );
498 if( !strcmp( psz_eltname, "item" ) )
500 i_is_item = VLC_TRUE;
502 p_feed->p_items = (struct rss_item_t *)realloc( p_feed->p_items, p_feed->i_items * sizeof( struct rss_item_t ) );
503 p_feed->p_items[p_feed->i_items-1].psz_title = NULL;
504 p_feed->p_items[p_feed->i_items-1].psz_description
506 p_feed->p_items[p_feed->i_items-1].psz_link = NULL;
510 case XML_READER_ENDELEM:
516 psz_eltname = xml_ReaderName( p_xml_reader );
522 msg_Dbg( p_filter, "element end : %s", psz_eltname );
524 if( !strcmp( psz_eltname, "item" ) )
526 i_is_item = VLC_FALSE;
533 case XML_READER_TEXT:
534 psz_eltvalue = xml_ReaderValue( p_xml_reader );
540 msg_Dbg( p_filter, " text : %s", psz_eltvalue );
542 if( i_is_item == VLC_FALSE )
544 if( !strcmp( psz_eltname, "title" ) )
546 p_feed->psz_title = psz_eltvalue;
548 else if( !strcmp( psz_eltname, "link" ) )
550 p_feed->psz_link = psz_eltvalue;
552 else if( !strcmp( psz_eltname, "description" ) )
554 p_feed->psz_description = psz_eltvalue;
558 free( psz_eltvalue );
564 struct rss_item_t *p_item;
565 p_item = p_feed->p_items+i_item;
566 if( !strcmp( psz_eltname, "title" ) )
568 p_item->psz_title = psz_eltvalue;
570 else if( !strcmp( psz_eltname, "link" ) )
572 p_item->psz_link = psz_eltvalue;
574 else if( !strcmp( psz_eltname, "description" ) )
576 p_item->psz_description = psz_eltvalue;
580 free( psz_eltvalue );
588 if( p_xml_reader && p_xml ) xml_ReaderDelete( p_xml, p_xml_reader );
589 if( p_stream ) stream_Delete( p_stream );
590 msg_Dbg( p_filter, "Done with %s RSS feed.", psz_feed );
592 free( psz_buffer_2 );
593 if( p_xml ) xml_Delete( p_xml );
598 /****************************************************************************
600 ***************************************************************************/
601 static void FreeRSS( filter_t *p_filter)
603 filter_sys_t *p_sys = p_filter->p_sys;
605 struct rss_item_t *p_item;
606 struct rss_feed_t *p_feed;
611 for( i_feed = 0; i_feed < p_sys->i_feeds; i_feed++ )
613 p_feed = p_sys->p_feeds+i_feed;
614 for( i_item = 0; i_item < p_feed->i_items; i_item++ )
616 p_item = p_feed->p_items+i_item;
617 free( p_item->psz_title );
618 free( p_item->psz_link );
619 free( p_item->psz_description );
621 free( p_feed->p_items );
622 free( p_feed->psz_title);
623 free( p_feed->psz_link );
624 free( p_feed->psz_description );
626 free( p_sys->p_feeds );