]> git.sesse.net Git - vlc/blob - modules/video_filter/time.c
Fix a bug with preferences
[vlc] / modules / video_filter / time.c
1 /*****************************************************************************
2  * time.c : time display video plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2003-2004 VideoLAN
5  * $Id$
6  *
7  * Authors: Sigmund Augdal <sigmunau@idi.ntnu.no>
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  * filter_sys_t: time filter descriptor
46  *****************************************************************************/
47 struct filter_sys_t
48 {
49     int i_xoff, i_yoff;  /* offsets for the display string in the video window */
50     char *psz_format;    /* time format string */
51
52     time_t last_time;
53 };
54
55 #define MSG_TEXT N_("Time format string (%Y%m%d %H%M%S)")
56 #define MSG_LONGTEXT N_("Time format string (%Y = year, %m = month, %d = day, %H = hour, %M = minute, %S = second")
57 #define POSX_TEXT N_("X offset, from left")
58 #define POSX_LONGTEXT N_("X offset, from the left screen edge" )
59 #define POSY_TEXT N_("Y offset, from the top")
60 #define POSY_LONGTEXT N_("Y offset, down from the top" )
61
62 /*****************************************************************************
63  * Module descriptor
64  *****************************************************************************/
65 vlc_module_begin();
66     set_capability( "sub filter", 0 );
67     set_shortname( N_("Time overlay"));
68     set_category( CAT_VIDEO );
69     set_subcategory( SUBCAT_VIDEO_SUBPIC );
70     set_callbacks( CreateFilter, DestroyFilter );
71     add_string( "time-format", "%Y-%m-%d   %H:%M:%S", NULL, MSG_TEXT, MSG_LONGTEXT, VLC_FALSE );
72     add_integer( "time-x", 0, NULL, POSX_TEXT, POSX_LONGTEXT, VLC_FALSE );
73     add_integer( "time-y", 0, NULL, POSY_TEXT, POSY_LONGTEXT, VLC_FALSE );
74     set_description( _("Time display sub filter") );
75     add_shortcut( "time" );
76 vlc_module_end();
77
78 /*****************************************************************************
79  * CreateFilter: allocates logo video filter
80  *****************************************************************************/
81 static int CreateFilter( vlc_object_t *p_this )
82 {
83     filter_t *p_filter = (filter_t *)p_this;
84     filter_sys_t *p_sys;
85     vlc_value_t val;
86
87     /* Allocate structure */
88     p_sys = p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
89     if( p_sys == NULL )
90     {
91         msg_Err( p_filter, "out of memory" );
92         return VLC_ENOMEM;
93     }
94
95     var_Create( p_this, "time-x", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
96     var_Get( p_filter, "time-x", &val );
97     p_sys->i_xoff = val.i_int;
98     var_Create( p_this, "time-y", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
99     var_Get( p_filter, "time-y", &val );
100     p_sys->i_yoff = val.i_int;
101     p_sys->psz_format = var_CreateGetString( p_this, "time-format" );
102
103     /* Misc init */
104     p_filter->pf_sub_filter = Filter;
105     p_sys->last_time = ((time_t)-1);
106
107     return VLC_SUCCESS;
108 }
109 /*****************************************************************************
110  * DestroyFilter: destroy logo video filter
111  *****************************************************************************/
112 static void DestroyFilter( vlc_object_t *p_this )
113 {
114     filter_t *p_filter = (filter_t *)p_this;
115     filter_sys_t *p_sys = p_filter->p_sys;
116
117     if( p_sys->psz_format ) free( p_sys->psz_format );
118     free( p_sys );
119 }
120
121
122 static char *FormatTime(char *tformat )
123 {
124   char buffer[255];
125   time_t curtime;
126 #if defined(HAVE_LOCALTIME_R)
127   struct tm loctime;
128 #else
129   struct tm *loctime;
130 #endif
131
132   /* Get the current time.  */
133   curtime = time( NULL );
134
135   /* Convert it to local time representation.  */
136 #if defined(HAVE_LOCALTIME_R)
137   localtime_r( &curtime, &loctime );
138   strftime( buffer, 255, tformat, &loctime );
139 #else
140   loctime = localtime( &curtime );
141   strftime( buffer, 255, tformat, loctime );
142 #endif
143   return strdup( buffer );
144 }
145
146 /****************************************************************************
147  * Filter: the whole thing
148  ****************************************************************************
149  * This function outputs subpictures at regular time intervals.
150  ****************************************************************************/
151 static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
152 {
153     filter_sys_t *p_sys = p_filter->p_sys;
154     subpicture_t *p_spu;
155     video_format_t fmt;
156
157     if( p_sys->last_time == time( NULL ) ) return NULL;
158
159     p_spu = p_filter->pf_sub_buffer_new( p_filter );
160     if( !p_spu ) return NULL;
161     
162     memset( &fmt, 0, sizeof(video_format_t) );
163     fmt.i_chroma = VLC_FOURCC('T','E','X','T');
164     fmt.i_aspect = 0;
165     fmt.i_width = fmt.i_height = 0;     
166     fmt.i_x_offset = 0;
167     fmt.i_y_offset = 0;
168     
169     p_spu->p_region = p_spu->pf_create_region( VLC_OBJECT(p_filter), &fmt );
170     if( !p_spu->p_region )
171     {
172         p_filter->pf_sub_buffer_del( p_filter, p_spu );
173         return NULL;
174     }
175
176     p_sys->last_time = time( NULL );
177
178     p_spu->p_region->psz_text = FormatTime( p_sys->psz_format );
179     p_spu->i_start = date;
180     p_spu->i_stop  = 0;
181     p_spu->b_ephemer = VLC_TRUE;
182     p_spu->b_absolute = VLC_FALSE;
183     p_spu->i_x = p_sys->i_xoff;
184     p_spu->i_y = p_sys->i_yoff;
185
186     p_spu->i_flags = OSD_ALIGN_LEFT|OSD_ALIGN_TOP ;
187     return p_spu;
188 }