]> git.sesse.net Git - vlc/blob - modules/video_filter/marq.c
Fix a bug with preferences
[vlc] / modules / video_filter / marq.c
1 /*****************************************************************************
2  * marq.c : marquee display video plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2003-2004 VideoLAN
5  * $Id: time.c 8751 2004-09-20 21:51:41Z gbazin $
6  *
7  * Authors: Mark Moriarty
8  *
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.
13  *
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.
18  *
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  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <string.h>
29
30 #include <vlc/vlc.h>
31 #include <vlc/vout.h>
32
33 #include "vlc_filter.h"
34 #include "vlc_block.h"
35 #include "osd.h"
36
37 /*****************************************************************************
38  * Local prototypes
39  *****************************************************************************/
40 static int  CreateFilter ( vlc_object_t * );
41 static void DestroyFilter( vlc_object_t * );
42 static subpicture_t *Filter( filter_t *, mtime_t );
43
44
45 static int MarqueeCallback( vlc_object_t *p_this, char const *psz_var,
46                             vlc_value_t oldval, vlc_value_t newval,
47                             void *p_data );
48
49 /*****************************************************************************
50  * filter_sys_t: marquee filter descriptor
51  *****************************************************************************/
52 struct filter_sys_t
53 {
54     int i_xoff, i_yoff;  /* offsets for the display string in the video window */
55     int i_timeout;
56
57     char *psz_marquee;    /* marquee string */
58
59     time_t last_time;
60
61     vlc_bool_t b_need_update;
62 };
63
64 #define MSG_TEXT N_("Marquee text")
65 #define MSG_LONGTEXT N_("Marquee text to display")
66 #define POSX_TEXT N_("X offset, from left")
67 #define POSX_LONGTEXT N_("X offset, from the left screen edge" )
68 #define POSY_TEXT N_("Y offset, from the top")
69 #define POSY_LONGTEXT N_("Y offset, down from the top" )
70 #define TIMEOUT_TEXT N_("Marquee timeout")
71 #define TIMEOUT_LONGTEXT N_("Defines the time the marquee must remain " \
72                             "displayed, in milliseconds. Default value is " \
73                             "0 (remain forever).")
74
75 /*****************************************************************************
76  * Module descriptor
77  *****************************************************************************/
78 vlc_module_begin();
79     set_capability( "sub filter", 0 );
80     set_shortname( N_("Marquee" ));
81     set_callbacks( CreateFilter, DestroyFilter );
82     set_category( CAT_VIDEO );
83     set_subcategory( SUBCAT_VIDEO_SUBPIC );
84     add_string( "marq-marquee", "Marquee", NULL, MSG_TEXT, MSG_LONGTEXT, VLC_FALSE );
85     add_integer( "marq-x", 0, NULL, POSX_TEXT, POSX_LONGTEXT, VLC_FALSE );
86     add_integer( "marq-y", 0, NULL, POSY_TEXT, POSY_LONGTEXT, VLC_FALSE );
87     add_integer( "marq-timeout", 0, NULL, TIMEOUT_TEXT, TIMEOUT_LONGTEXT,
88                  VLC_FALSE );
89     set_description( _("Marquee display sub filter") );
90     add_shortcut( "marq" );
91 vlc_module_end();
92
93 /*****************************************************************************
94  * CreateFilter: allocates marquee video filter
95  *****************************************************************************/
96 static int CreateFilter( vlc_object_t *p_this )
97 {
98     filter_t *p_filter = (filter_t *)p_this;
99     filter_sys_t *p_sys;
100     vlc_object_t *p_input;
101
102     /* Allocate structure */
103     p_sys = p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
104     if( p_sys == NULL )
105     {
106         msg_Err( p_filter, "out of memory" );
107         return VLC_ENOMEM;
108     }
109
110     /* Hook used for callback variables */
111     p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_ANYWHERE );
112     if( !p_input )
113     {
114         return VLC_ENOOBJ;
115     }
116
117     p_sys->i_xoff = var_CreateGetInteger( p_input->p_libvlc , "marq-x" );
118     p_sys->i_yoff = var_CreateGetInteger( p_input->p_libvlc , "marq-y" );
119     p_sys->i_timeout = var_CreateGetInteger( p_input->p_libvlc , "marq-timeout" );
120     p_sys->psz_marquee =  var_CreateGetString( p_input->p_libvlc, "marq-marquee" );
121
122     var_AddCallback( p_input->p_libvlc, "marq-x", MarqueeCallback, p_sys );
123     var_AddCallback( p_input->p_libvlc, "marq-y", MarqueeCallback, p_sys );
124     var_AddCallback( p_input->p_libvlc, "marq-marquee", MarqueeCallback, p_sys );
125     var_AddCallback( p_input->p_libvlc, "marq-timeout", MarqueeCallback, p_sys );
126
127     vlc_object_release( p_input );
128
129     /* Misc init */
130     p_filter->pf_sub_filter = Filter;
131     p_sys->last_time = ((time_t)-1);
132     p_sys->b_need_update = VLC_TRUE;
133
134     return VLC_SUCCESS;
135 }
136 /*****************************************************************************
137  * DestroyFilter: destroy marquee video filter
138  *****************************************************************************/
139 static void DestroyFilter( vlc_object_t *p_this )
140 {
141     filter_t *p_filter = (filter_t *)p_this;
142     filter_sys_t *p_sys = p_filter->p_sys;
143     vlc_object_t *p_input;
144
145     if( p_sys->psz_marquee ) free( p_sys->psz_marquee );
146     free( p_sys );
147
148     /* Delete the marquee variables from playlist */
149     p_input = vlc_object_find( p_this, VLC_OBJECT_INPUT, FIND_ANYWHERE );
150     if( !p_input )
151     {
152         return;
153     }
154     var_Destroy( p_input->p_libvlc , "marq-marquee" );
155     var_Destroy( p_input->p_libvlc , "marq-x" );
156     var_Destroy( p_input->p_libvlc , "marq-y" );
157     var_Destroy( p_input->p_libvlc , "marq-timeout" );
158     vlc_object_release( p_input );
159 }
160
161 /****************************************************************************
162  * Filter: the whole thing
163  ****************************************************************************
164  * This function outputs subpictures at regular time intervals.
165  ****************************************************************************/
166 static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
167 {
168     filter_sys_t *p_sys = p_filter->p_sys;
169     subpicture_t *p_spu;
170     video_format_t fmt;
171     time_t t;
172
173     if( p_sys->last_time == time( NULL ) )
174     {
175         return NULL;
176     }
177
178     if( p_sys->b_need_update == VLC_FALSE )
179     {
180         return NULL;
181     }
182
183     p_spu = p_filter->pf_sub_buffer_new( p_filter );
184     if( !p_spu ) return NULL;
185
186     memset( &fmt, 0, sizeof(video_format_t) );
187     fmt.i_chroma = VLC_FOURCC('T','E','X','T');
188     fmt.i_aspect = 0;
189     fmt.i_width = fmt.i_height = 0;
190     fmt.i_x_offset = 0;
191     fmt.i_y_offset = 0;
192
193     p_spu->p_region = p_spu->pf_create_region( VLC_OBJECT(p_filter), &fmt );
194     if( !p_spu->p_region )
195     {
196         p_filter->pf_sub_buffer_del( p_filter, p_spu );
197         return NULL;
198     }
199
200     t = p_sys->last_time = time( NULL );
201
202     p_spu->p_region->psz_text = strdup(p_sys->psz_marquee);
203     p_spu->i_start = date;
204     p_spu->i_stop  = p_sys->i_timeout == 0 ? 0 : date + p_sys->i_timeout * 1000;
205     p_spu->b_ephemer = VLC_TRUE;
206     p_spu->b_absolute = VLC_FALSE;
207     p_spu->i_x = p_sys->i_xoff;
208     p_spu->i_y = p_sys->i_yoff;
209
210     p_spu->i_flags = OSD_ALIGN_LEFT|OSD_ALIGN_TOP ;
211
212     p_sys->b_need_update = VLC_FALSE;
213     return p_spu;
214 }
215
216 /**********************************************************************
217  * Callback to update params on the fly
218  **********************************************************************/
219 static int MarqueeCallback( vlc_object_t *p_this, char const *psz_var,
220                             vlc_value_t oldval, vlc_value_t newval,
221                             void *p_data )
222 {
223     filter_sys_t *p_sys = (filter_sys_t *) p_data;
224
225     if( !strncmp( psz_var, "marq-marquee", 7 ) )
226     {
227         if( p_sys->psz_marquee ) free( p_sys->psz_marquee );
228         p_sys->psz_marquee = strdup( newval.psz_string );
229     }
230     else if ( !strncmp( psz_var, "marq-x", 6 ) )
231     {
232         p_sys->i_xoff = newval.i_int;
233     }
234     else if ( !strncmp( psz_var, "marq-y", 6 ) )
235     {
236         p_sys->i_yoff = newval.i_int;
237     }
238     else if ( !strncmp( psz_var, "marq-timeout", 12 ) )
239     {
240         p_sys->i_timeout = newval.i_int;
241     }
242     p_sys->b_need_update = VLC_TRUE;
243     return VLC_SUCCESS;
244 }