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 */
98 //vlc_bool_t b_need_update;
102 struct rss_feed_t *p_feeds;
109 #define MSG_TEXT N_("RSS feed URLs")
110 #define MSG_LONGTEXT N_("RSS feed comma seperated URLs")
111 #define SPEED_TEXT N_("RSS feed speed")
112 #define SPEED_LONGTEXT N_("RSS feed speed (bigger is slower)")
113 #define LENGTH_TEXT N_("RSS feed max number of chars displayed")
114 #define LENGTH_LONGTEXT N_("RSS feed max number of chars displayed")
116 #define POSX_TEXT N_("X offset, from left")
117 #define POSX_LONGTEXT N_("X offset, from the left screen edge" )
118 #define POSY_TEXT N_("Y offset, from the top")
119 #define POSY_LONGTEXT N_("Y offset, down from the top" )
120 #define OPACITY_TEXT N_("Opacity")
121 #define OPACITY_LONGTEXT N_("The opacity (inverse of transparency) of " \
122 "overlay text. 0 = transparent, 255 = totally opaque. " )
123 #define SIZE_TEXT N_("Font size, pixels")
124 #define SIZE_LONGTEXT N_("Specify the font size, in pixels, " \
125 "with -1 = use freetype-fontsize" )
127 #define COLOR_TEXT N_("Text Default Color")
128 #define COLOR_LONGTEXT N_("The color of overlay text. 1 byte for each color, hexadecimal. " \
129 "#000000 = all colors off, " \
130 "0xFF0000 = just Red, 0xFFFFFF = all color on [White]" )
132 #define POS_TEXT N_("Marquee position")
133 #define POS_LONGTEXT N_( \
134 "You can enforce the marquee position on the video " \
135 "(0=center, 1=left, 2=right, 4=top, 8=bottom, you can " \
136 "also use combinations of these values by adding them).")
138 static int pi_pos_values[] = { 0, 1, 2, 4, 8, 5, 6, 9, 10 };
139 static char *ppsz_pos_descriptions[] =
140 { N_("Center"), N_("Left"), N_("Right"), N_("Top"), N_("Bottom"),
141 N_("Top-Left"), N_("Top-Right"), N_("Bottom-Left"), N_("Bottom-Right") };
143 /*****************************************************************************
145 *****************************************************************************/
147 set_capability( "sub filter", 0 );
148 set_shortname( N_("RSS" ));
149 set_callbacks( CreateFilter, DestroyFilter );
150 set_category( CAT_VIDEO );
151 set_subcategory( SUBCAT_VIDEO_SUBPIC );
152 add_string( "rss-urls", "rss", NULL, MSG_TEXT, MSG_LONGTEXT, VLC_FALSE );
154 set_section( N_("Position"), NULL );
155 add_integer( "rss-x", -1, NULL, POSX_TEXT, POSX_LONGTEXT, VLC_FALSE );
156 add_integer( "rss-y", 0, NULL, POSY_TEXT, POSY_LONGTEXT, VLC_FALSE );
157 add_integer( "rss-position", 5, NULL, POS_TEXT, POS_LONGTEXT, VLC_TRUE );
159 set_section( N_("Font"), NULL );
160 /* 5 sets the default to top [1] left [4] */
161 change_integer_list( pi_pos_values, ppsz_pos_descriptions, 0 );
162 add_integer_with_range( "rss-opacity", 255, 0, 255, NULL,
163 OPACITY_TEXT, OPACITY_LONGTEXT, VLC_FALSE );
164 add_integer( "rss-color", 0xFFFFFF, NULL, COLOR_TEXT, COLOR_LONGTEXT, VLC_TRUE );
165 change_integer_list( pi_color_values, ppsz_color_descriptions, 0 );
166 add_integer( "rss-size", -1, NULL, SIZE_TEXT, SIZE_LONGTEXT, VLC_FALSE );
168 set_section( N_("Misc"), NULL );
169 add_integer( "rss-speed", 100000, NULL, SPEED_TEXT, SPEED_LONGTEXT,
171 add_integer( "rss-length", 60, NULL, LENGTH_TEXT, LENGTH_LONGTEXT,
174 set_description( _("RSS feed display sub filter") );
175 add_shortcut( "rss" );
178 /*****************************************************************************
179 * CreateFilter: allocates RSS video filter
180 *****************************************************************************/
181 static int CreateFilter( vlc_object_t *p_this )
183 filter_t *p_filter = (filter_t *)p_this;
187 /* Allocate structure */
188 p_sys = p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
191 msg_Err( p_filter, "out of memory" );
195 vlc_mutex_init( p_filter, &p_sys->lock );
196 vlc_mutex_lock( &p_sys->lock );
198 p_sys->psz_urls = var_CreateGetString( p_filter, "rss-urls" );
199 p_sys->i_cur_feed = 0;
200 p_sys->i_cur_item = 0;
201 p_sys->i_cur_char = 0;
203 p_sys->p_feeds = NULL;
204 p_sys->i_speed = var_CreateGetInteger( p_filter, "rss-speed" );
205 p_sys->i_length = var_CreateGetInteger( p_filter, "rss-length" );
206 p_sys->psz_marquee = (char *)malloc( p_sys->i_length );
208 p_sys->i_xoff = var_CreateGetInteger( p_filter, "rss-x" );
209 p_sys->i_yoff = var_CreateGetInteger( p_filter, "rss-y" );
210 p_sys->i_pos = var_CreateGetInteger( p_filter, "rss-position" );
211 var_Create( p_filter, "rss-opacity", VLC_VAR_INTEGER|VLC_VAR_DOINHERIT );
212 p_sys->i_font_opacity = var_CreateGetInteger( p_filter, "rss-opacity" );
213 p_sys->i_font_color = var_CreateGetInteger( p_filter, "rss-color" );
214 p_sys->i_font_size = var_CreateGetInteger( p_filter, "rss-size" );
216 if( FetchRSS( p_filter ) )
218 msg_Err( p_filter, "failed while fetching RSS ... too bad" );
219 vlc_mutex_unlock( &p_sys->lock );
223 if( p_sys->i_feeds == 0 )
225 vlc_mutex_unlock( &p_sys->lock );
228 for( i_feed=0; i_feed < p_sys->i_feeds; i_feed ++ )
229 if( p_sys->p_feeds[i_feed].i_items == 0 )
231 vlc_mutex_unlock( &p_sys->lock );
236 p_filter->pf_sub_filter = Filter;
237 p_sys->last_date = (mtime_t)0;
239 vlc_mutex_unlock( &p_sys->lock );
243 /*****************************************************************************
244 * DestroyFilter: destroy RSS video filter
245 *****************************************************************************/
246 static void DestroyFilter( vlc_object_t *p_this )
248 filter_t *p_filter = (filter_t *)p_this;
249 filter_sys_t *p_sys = p_filter->p_sys;
251 vlc_mutex_lock( &p_sys->lock );
253 if( p_sys->psz_marquee ) free( p_sys->psz_marquee );
254 free( p_sys->psz_urls );
256 vlc_mutex_unlock( &p_sys->lock );
257 vlc_mutex_destroy( &p_sys->lock );
260 /* Delete the RSS variables */
261 var_Destroy( p_filter, "rss-urls" );
262 var_Destroy( p_filter, "rss-speed" );
263 var_Destroy( p_filter, "rss-length" );
264 var_Destroy( p_filter, "rss-x" );
265 var_Destroy( p_filter, "rss-y" );
266 var_Destroy( p_filter, "rss-position" );
267 var_Destroy( p_filter, "rss-color");
268 var_Destroy( p_filter, "rss-opacity");
269 var_Destroy( p_filter, "rss-size");
272 /****************************************************************************
273 * Filter: the whole thing
274 ****************************************************************************
275 * This function outputs subpictures at regular time intervals.
276 ****************************************************************************/
277 static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
279 filter_sys_t *p_sys = p_filter->p_sys;
285 vlc_mutex_lock( &p_sys->lock );
287 /* wait more for the 1st char */
288 if( p_sys->last_date + ( p_sys->i_cur_char == 0 && p_sys->i_cur_item == 0 ? 5 : 1 ) * p_sys->i_speed > date )
290 vlc_mutex_unlock( &p_sys->lock );
294 p_sys->last_date = date;
296 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 )
298 p_sys->i_cur_char = 0;
300 if( p_sys->i_cur_item >= p_sys->p_feeds[p_sys->i_cur_feed].i_items )
302 p_sys->i_cur_item = 0;
303 p_sys->i_cur_feed = (p_sys->i_cur_feed + 1)%p_sys->i_feeds;
307 p_spu = p_filter->pf_sub_buffer_new( p_filter );
310 vlc_mutex_unlock( &p_sys->lock );
314 memset( &fmt, 0, sizeof(video_format_t) );
315 fmt.i_chroma = VLC_FOURCC('T','E','X','T');
321 p_spu->p_region = p_spu->pf_create_region( VLC_OBJECT(p_filter), &fmt );
322 if( !p_spu->p_region )
324 p_filter->pf_sub_buffer_del( p_filter, p_spu );
325 vlc_mutex_unlock( &p_sys->lock );
329 i_item = p_sys->i_cur_item;
330 i_feed = p_sys->i_cur_feed;
331 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 );
332 while( strlen( p_sys->psz_marquee ) < (unsigned int)p_sys->i_length )
335 if( i_item == p_sys->p_feeds[i_feed].i_items ) break;
336 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 );
339 p_spu->p_region->psz_text = strdup(p_sys->psz_marquee);
340 p_spu->i_start = date;
342 p_spu->b_ephemer = VLC_TRUE;
344 /* where to locate the string: */
345 if( p_sys->i_xoff < 0 || p_sys->i_yoff < 0 )
346 { /* set to one of the 9 relative locations */
347 p_spu->i_flags = p_sys->i_pos;
350 p_spu->b_absolute = VLC_FALSE;
353 { /* set to an absolute xy, referenced to upper left corner */
354 p_spu->i_flags = OSD_ALIGN_LEFT | OSD_ALIGN_TOP;
355 p_spu->i_x = p_sys->i_xoff;
356 p_spu->i_y = p_sys->i_yoff;
357 p_spu->b_absolute = VLC_TRUE;
360 p_spu->p_region->i_text_color = p_sys->i_font_color;
361 p_spu->p_region->i_text_alpha = 255 - p_sys->i_font_opacity;
362 p_spu->p_region->i_text_size = p_sys->i_font_size;
364 vlc_mutex_unlock( &p_sys->lock );
368 /****************************************************************************
369 * RSS related functions
370 ****************************************************************************
371 * You should always lock the p_filter mutex before using any of these
373 ***************************************************************************/
375 /****************************************************************************
377 ***************************************************************************/
378 static int FetchRSS( filter_t *p_filter)
380 filter_sys_t *p_sys = p_filter->p_sys;
382 stream_t *p_stream = NULL;
384 xml_reader_t *p_xml_reader = NULL;
386 char *psz_eltname = NULL;
387 char *psz_eltvalue = NULL;
388 char *psz_feed = NULL;
389 char *psz_buffer = NULL;
390 char *psz_buffer_2 = NULL;
400 while( p_sys->psz_urls[i_int] != 0 )
401 if( p_sys->psz_urls[i_int++] == ',' )
403 p_sys->p_feeds = (struct rss_feed_t *)malloc( p_sys->i_feeds
404 * sizeof( struct rss_feed_t ) );
406 p_xml = xml_Create( p_filter );
409 msg_Err( p_filter, "Failed to open XML parser" );
413 psz_buffer = strdup( p_sys->psz_urls );
414 psz_buffer_2 = psz_buffer; /* keep track so we can free it */
415 for( i_feed = 0; i_feed < p_sys->i_feeds; i_feed++ )
417 struct rss_feed_t *p_feed = p_sys->p_feeds+i_feed;
419 if( psz_buffer == NULL ) break;
420 if( psz_buffer[0] == 0 ) psz_buffer++;
421 psz_feed = psz_buffer;
422 psz_buffer = strchr( psz_buffer, ',' );
423 if( psz_buffer != NULL ) psz_buffer[0] = 0;
425 p_feed->psz_title = NULL;
426 p_feed->psz_description = NULL;
427 p_feed->psz_link = NULL;
429 p_feed->p_items = NULL;
431 msg_Dbg( p_filter, "Opening %s RSS feed ...", psz_feed );
433 p_stream = stream_UrlNew( p_filter, psz_feed );
436 msg_Err( p_filter, "Failed to open %s for reading", psz_feed );
440 p_xml_reader = xml_ReaderCreate( p_xml, p_stream );
443 msg_Err( p_filter, "Failed to open %s for parsing", psz_feed );
448 i_is_item = VLC_FALSE;
450 while( xml_ReaderRead( p_xml_reader ) == 1 )
452 switch( xml_ReaderNodeType( p_xml_reader ) )
458 case XML_READER_STARTELEM:
459 if( psz_eltname ) free( psz_eltname );
460 psz_eltname = xml_ReaderName( p_xml_reader );
465 msg_Dbg( p_filter, "element name : %s", psz_eltname );
466 if( !strcmp( psz_eltname, "item" ) )
468 i_is_item = VLC_TRUE;
470 p_feed->p_items = (struct rss_item_t *)realloc( p_feed->p_items, p_feed->i_items * sizeof( struct rss_item_t ) );
474 case XML_READER_ENDELEM:
475 if( psz_eltname ) free( psz_eltname );
476 psz_eltname = xml_ReaderName( p_xml_reader );
481 msg_Dbg( p_filter, "element end : %s", psz_eltname );
482 if( !strcmp( psz_eltname, "item" ) )
484 i_is_item = VLC_FALSE;
487 free( psz_eltname ); psz_eltname = NULL;
490 case XML_READER_TEXT:
491 psz_eltvalue = xml_ReaderValue( p_xml_reader );
496 msg_Dbg( p_filter, " text : %s", psz_eltvalue );
497 if( i_is_item == VLC_FALSE )
499 if( !strcmp( psz_eltname, "title" ) )
501 p_feed->psz_title = psz_eltvalue;
503 else if( !strcmp( psz_eltname, "link" ) )
505 p_feed->psz_link = psz_eltvalue;
507 else if( !strcmp( psz_eltname, "description" ) )
509 p_feed->psz_description = psz_eltvalue;
513 free( psz_eltvalue );
518 struct rss_item_t *p_item;
519 p_item = p_feed->p_items+i_item;
520 if( !strcmp( psz_eltname, "title" ) )
522 p_item->psz_title = psz_eltvalue;
524 else if( !strcmp( psz_eltname, "link" ) )
526 p_item->psz_link = psz_eltvalue;
528 else if( !strcmp( psz_eltname, "description" ) )
530 p_item->psz_description = psz_eltvalue;
534 free( psz_eltvalue );
541 if( p_xml_reader && p_xml ) xml_ReaderDelete( p_xml, p_xml_reader );
542 if( p_stream ) stream_Delete( p_stream );
544 free( psz_buffer_2 );
545 if( p_xml ) xml_Delete( p_xml );
550 /****************************************************************************
552 ***************************************************************************/
553 static void FreeRSS( filter_t *p_filter)
555 filter_sys_t *p_sys = p_filter->p_sys;
557 struct rss_item_t *p_item;
558 struct rss_feed_t *p_feed;
563 for( i_feed = 0; i_feed < p_sys->i_feeds; i_feed++ )
565 p_feed = p_sys->p_feeds+i_feed;
566 for( i_item = 0; i_item < p_feed->i_items; i_item++ )
568 p_item = p_feed->p_items+i_item;
569 free( p_item->psz_title );
570 free( p_item->psz_link );
571 free( p_item->psz_description );
573 free( p_feed->p_items );
574 free( p_feed->psz_title);
575 free( p_feed->psz_link );
576 free( p_feed->psz_description );
578 free( p_sys->p_feeds );