]> git.sesse.net Git - vlc/blob - modules/video_filter/time.c
time.c: customizable position and string prefix, mainly done by markfm
[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: logo.c 8721 2004-09-17 10:21:00Z gbazin $
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_head;  /* text to put before the day/time */
51 };
52
53 #define MSG_TEXT N_("Timestamp Prefix")
54 #define MSG_LONGTEXT N_("Text (name) to display before the date and time")
55 #define POSX_TEXT N_("X coordinate of the timestamp")
56 #define POSX_LONGTEXT N_("Positive offset, from the left" )
57 #define POSY_TEXT N_("Y coordinate of the timestamp")
58 #define POSY_LONGTEXT N_("Positive offset, down from the top" )
59 #define TRANS_TEXT N_("Transparency of the timestamp")
60
61 /*****************************************************************************
62  * Module descriptor
63  *****************************************************************************/
64 vlc_module_begin();
65     set_capability( "sub filter", 0 );
66     set_callbacks( CreateFilter, DestroyFilter );
67     add_string( "text-message", NULL, NULL, MSG_TEXT, MSG_LONGTEXT, VLC_FALSE );
68     add_integer( "timestamp-x", 0, NULL, POSX_TEXT, POSX_LONGTEXT, VLC_FALSE );
69     add_integer( "timestamp-y", 0, NULL, POSY_TEXT, POSY_LONGTEXT, VLC_FALSE );
70     set_description( _("Time display sub filter") );
71     add_shortcut( "time" );
72 vlc_module_end();
73
74 /*****************************************************************************
75  * CreateFilter: allocates logo video filter
76  *****************************************************************************/
77 static int CreateFilter( vlc_object_t *p_this )
78 {
79     filter_t *p_filter = (filter_t *)p_this;
80     filter_sys_t *p_sys;
81     vlc_value_t val;
82
83     /* Allocate structure */
84     p_sys = p_filter->p_sys = malloc( sizeof( filter_sys_t ) );
85     if( p_sys == NULL )
86     {
87         msg_Err( p_filter, "out of memory" );
88         return VLC_ENOMEM;
89     }
90
91     var_Create( p_this, "timestamp-x", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
92     var_Get( p_filter, "timestamp-x", &val );
93     p_sys->i_xoff = val.i_int;
94     var_Create( p_this, "timestamp-y", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
95     var_Get( p_filter, "timestamp-y", &val );
96     p_sys->i_yoff = val.i_int;
97     p_sys->psz_head = strdup(var_CreateGetString( p_this, "text-message" ));
98
99     
100     /* Misc init */
101     p_filter->pf_sub_filter = Filter;
102
103     return VLC_SUCCESS;
104 }
105 /*****************************************************************************
106  * DestroyFilter: destroy logo video filter
107  *****************************************************************************/
108 static void DestroyFilter( vlc_object_t *p_this )
109 {
110     filter_t *p_filter = (filter_t *)p_this;
111     filter_sys_t *p_sys = p_filter->p_sys;
112
113     free( p_sys );
114 }
115
116 char *myCtime( time_t *t )
117 {
118 #ifdef HAVE_CTIME_R
119     char tmp[27];
120     ctime_r( t, tmp );
121     return strdup( tmp );
122 #else
123     return strdup(ctime(t));
124 #endif
125 }
126
127 /****************************************************************************
128  * Filter: the whole thing
129  ****************************************************************************
130  * This function outputs subpictures at regular time intervals.
131  ****************************************************************************/
132 static subpicture_t *Filter( filter_t *p_filter, mtime_t date )
133 {
134     subpicture_t *p_spu;
135     video_format_t fmt;
136     time_t t;
137     char *psz_time, *psz_string;
138     filter_sys_t *p_sys;
139
140     p_sys = p_filter->p_sys;
141     p_spu = p_filter->pf_sub_buffer_new( p_filter );
142     if( !p_spu ) return NULL;
143     
144     memset( &fmt, 0, sizeof(video_format_t) );
145     fmt.i_chroma = VLC_FOURCC('T','E','X','T');
146     fmt.i_aspect = 0;
147     fmt.i_width = fmt.i_height = 0;     
148     fmt.i_x_offset = 0;
149     fmt.i_y_offset = 0;
150     
151     p_spu->p_region = p_spu->pf_create_region( VLC_OBJECT(p_filter), &fmt );
152     if( !p_spu->p_region )
153     {
154         p_filter->pf_sub_buffer_del( p_filter, p_spu );
155         return NULL;
156     }
157     t = time(NULL);
158     
159  
160     psz_time = myCtime( &t );
161     asprintf( &psz_string, "%s%s", p_sys->psz_head, psz_time );
162     
163     free( psz_time );
164     p_spu->p_region->psz_text = psz_string;
165     p_spu->i_start = date;
166     p_spu->i_stop  = date + 1000000;
167     p_spu->b_ephemer = VLC_TRUE;
168     p_spu->b_absolute = VLC_FALSE;
169     p_spu->i_x = p_sys->i_xoff;
170     p_spu->i_y = p_sys->i_yoff;
171            
172     p_spu->i_flags = OSD_ALIGN_LEFT|OSD_ALIGN_TOP ;
173     return p_spu;
174 }