]> git.sesse.net Git - vlc/blob - modules/visualization/xosd.c
0d805e54330b1a273f6d34ce8cafe982f5cdf95f
[vlc] / modules / visualization / xosd.c
1 /*****************************************************************************
2  * xosd.c : X On Screen Display interface
3  *****************************************************************************
4  * Copyright (C) 2001 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Loïc Minier <lool@videolan.org>
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                      /* malloc(), free() */
28 #include <string.h>
29
30 #include <xosd.h>
31
32 #include <vlc/intf.h>
33
34 #ifdef HAVE_UNISTD_H
35 #    include <unistd.h>
36 #endif
37
38 /*****************************************************************************
39  * intf_sys_t: description and status of rc interface
40  *****************************************************************************/
41 struct intf_sys_t
42 {
43     xosd * p_osd;               /* libxosd handle */
44     vlc_bool_t  b_need_update;   /* Update display ? */
45 };
46
47 #define MAX_LINE_LENGTH 256
48
49 /*****************************************************************************
50  * Local prototypes.
51  *****************************************************************************/
52 static int  Open         ( vlc_object_t * );
53 static void Close        ( vlc_object_t * );
54
55 static void Run          ( intf_thread_t * );
56
57 static int PlaylistNext( vlc_object_t *p_this, const char *psz_variable,
58                 vlc_value_t oval, vlc_value_t nval, void *param );
59
60 /*****************************************************************************
61  * Module descriptor
62  *****************************************************************************/
63 #define POSITION_TEXT N_("Flip vertical position")
64 #define POSITION_LONGTEXT N_("Display XOSD output at the bottom of the " \
65                              "screen instead of the top.")
66
67 #define TXT_OFS_TEXT N_("Vertical offset")
68 #define TXT_OFS_LONGTEXT N_("Vertical offset between the border of the screen "\
69                             "and the displayed text (in pixels, defaults to "\
70                             "30 pixels)." )
71
72 #define SHD_OFS_TEXT N_("Shadow offset")
73 #define SHD_OFS_LONGTEXT N_("Offset between the text and the shadow (in " \
74                             "pixels, defaults to 2 pixels)." )
75
76 #define FONT_TEXT N_("Font")
77 #define FONT_LONGTEXT N_("Font used to display text in the XOSD output.")
78 #define COLOUR_TEXT N_("Color")
79 #define COLOUR_LONGTEXT N_("Color used to display text in the XOSD output.")
80
81 vlc_module_begin();
82     set_category( CAT_INTERFACE );
83     set_subcategory( SUBCAT_INTERFACE_CONTROL );
84     set_description( _("XOSD interface") );
85     set_shortname( "XOSD" );
86     add_bool( "xosd-position", 1, NULL, POSITION_TEXT, POSITION_LONGTEXT, VLC_TRUE );
87     add_integer( "xosd-text-offset", 30, NULL, TXT_OFS_TEXT, TXT_OFS_LONGTEXT, VLC_TRUE );
88     add_integer( "xosd-shadow-offset", 2, NULL,
89                  SHD_OFS_TEXT, SHD_OFS_LONGTEXT, VLC_TRUE );
90     add_string( "xosd-font",
91                 "-adobe-helvetica-bold-r-normal-*-*-160-*-*-p-*-iso8859-1",
92                 NULL, FONT_TEXT, FONT_LONGTEXT, VLC_TRUE );
93     add_string( "xosd-colour", "LawnGreen",
94                     NULL, COLOUR_TEXT, COLOUR_LONGTEXT, VLC_TRUE );
95     set_capability( "interface", 10 );
96     set_callbacks( Open, Close );
97 vlc_module_end();
98
99 /*****************************************************************************
100  * Open: initialize and create stuff
101  *****************************************************************************/
102 static int Open( vlc_object_t *p_this )
103 {
104     intf_thread_t *p_intf = (intf_thread_t *)p_this;
105     xosd *p_osd;
106
107     /* Allocate instance and initialize some members */
108     p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
109     if( p_intf->p_sys == NULL )
110     {
111         msg_Err( p_intf, "out of memory" );
112         return VLC_ENOMEM;
113     }
114
115     if( getenv( "DISPLAY" ) == NULL )
116     {
117         msg_Err( p_intf, "no display, please set the DISPLAY variable" );
118         return VLC_EGENERIC;
119     }
120
121     /* Initialize library */
122 #if defined(HAVE_XOSD_VERSION_0) || defined(HAVE_XOSD_VERSION_1)
123     p_osd  = p_intf->p_sys->p_osd =
124         xosd_init( config_GetPsz( p_intf, "xosd-font" ),
125                    config_GetPsz( p_intf,"xosd-colour" ), 3,
126                    XOSD_top, 0, 1 );
127     if( p_intf->p_sys->p_osd == NULL )
128     {
129         msg_Err( p_intf, "couldn't initialize libxosd" );
130         return VLC_EGENERIC;
131     }
132 #else
133     p_osd = p_intf->p_sys->p_osd = xosd_create( 1 );
134     if( p_osd == NULL )
135     {
136         msg_Err( p_intf, "couldn't initialize libxosd" );
137         return VLC_EGENERIC;
138     }
139     xosd_set_colour( p_osd, config_GetPsz( p_intf,"xosd-colour" ) );
140     xosd_set_timeout( p_osd, 3 );
141 #endif
142
143
144     playlist_t *p_playlist =
145             (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
146                                            FIND_ANYWHERE );
147     if( p_playlist == NULL )
148     {
149         return VLC_EGENERIC;
150     }
151
152     var_AddCallback( p_playlist, "playlist-current", PlaylistNext, p_this );
153     var_AddCallback( p_playlist, "item-change", PlaylistNext, p_this );
154
155     vlc_object_release( p_playlist );
156
157     /* Set user preferences */
158     xosd_set_font( p_intf->p_sys->p_osd,
159                     config_GetPsz( p_intf, "xosd-font" ) );
160     xosd_set_outline_colour( p_intf->p_sys->p_osd,"black" );
161 #ifdef HAVE_XOSD_VERSION_2
162     xosd_set_horizontal_offset( p_intf->p_sys->p_osd,
163                     config_GetInt( p_intf, "xosd-text-offset" ) );
164     xosd_set_vertical_offset( p_intf->p_sys->p_osd,
165                     config_GetInt( p_intf, "xosd-text-offset" ) );
166 #else
167     xosd_set_offset( p_intf->p_sys->p_osd,
168                     config_GetInt( p_intf, "xosd-text-offset" ) );
169 #endif
170     xosd_set_shadow_offset( p_intf->p_sys->p_osd,
171                     config_GetInt( p_intf, "xosd-shadow-offset" ));
172     xosd_set_pos( p_intf->p_sys->p_osd,
173                     config_GetInt( p_intf, "xosd-position" ) ?
174                                          XOSD_bottom: XOSD_top );
175
176     /* Initialize to NULL */
177     xosd_display( p_osd, 0, XOSD_string, "XOSD interface initialized" );
178
179     p_intf->pf_run = Run;
180
181     p_intf->p_sys->b_need_update = VLC_TRUE;
182
183     return VLC_SUCCESS;
184 }
185
186 /*****************************************************************************
187  * Close: destroy interface stuff
188  *****************************************************************************/
189 static void Close( vlc_object_t *p_this )
190 {
191     intf_thread_t *p_intf = (intf_thread_t *)p_this;
192
193     /* Uninitialize library */
194     xosd_destroy( p_intf->p_sys->p_osd );
195
196     /* Destroy structure */
197     free( p_intf->p_sys );
198 }
199
200 /*****************************************************************************
201  * Run: xosd thread
202  *****************************************************************************
203  * This part of the interface runs in a separate thread
204  *****************************************************************************/
205 static void Run( intf_thread_t *p_intf )
206 {
207     int i_size,i_index;
208     playlist_t *p_playlist;
209     playlist_item_t *p_item = NULL;
210     input_item_t item;
211     char psz_duration[MSTRTIME_MAX_SIZE+2];
212     char *psz_display = NULL;
213
214     while( !p_intf->b_die )
215     {
216         if( p_intf->p_sys->b_need_update == VLC_TRUE )
217         {
218             p_intf->p_sys->b_need_update = VLC_FALSE;
219             p_playlist = (playlist_t *)vlc_object_find( p_intf,
220                                       VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
221             if( !p_playlist )
222             {
223                 continue;
224             }
225
226             if( p_playlist->i_size < 0 || p_playlist->i_index < 0 )
227             {
228                 vlc_object_release( p_playlist );
229                 continue;
230             }
231             if( psz_display )
232             {
233                 free( psz_display );
234                 psz_display = NULL;
235             }
236             if( p_playlist->status.i_status == PLAYLIST_STOPPED )
237             {
238                 psz_display = strdup(_("Stop"));
239                 vlc_object_release( p_playlist );
240             }
241             else if( p_playlist->status.i_status == PLAYLIST_PAUSED )
242             {
243                 psz_display = strdup(_("Pause"));
244                 vlc_object_release( p_playlist );
245             }
246             else
247             {
248     //           vlc_mutex_lock(&p_playlist->object_lock );
249                  p_item = p_playlist->status.p_item;
250                 item = p_item->input;
251                 if( !p_item )
252                 {
253                     vlc_object_release( p_playlist );
254      //            vlc_mutex_unlock(&p_playlist->object_lock );
255                     continue;
256                 }
257                 i_size = p_playlist->i_size;
258                 i_index = p_playlist->i_index+1;
259     //            vlc_mutex_unlock(&p_playlist->object_lock );
260
261                 vlc_object_release( p_playlist );
262
263                 if( item.i_duration != -1 )
264                 {
265                     char psz_durationstr[MSTRTIME_MAX_SIZE];
266                     secstotimestr( psz_durationstr, item.i_duration/1000000 );
267                     sprintf( psz_duration, "(%s)", psz_durationstr );
268                 }
269                 else
270                 {
271                     sprintf( psz_duration," " );
272                 }
273
274                 psz_display = (char *)malloc( sizeof(char )*
275                                           (strlen( item.psz_name ) +
276                                           MSTRTIME_MAX_SIZE + 2+6 + 10 +10 ));
277                 sprintf( psz_display," %i/%i - %s %s",
278                          i_index,i_size, item.psz_name, psz_duration);
279             }
280
281             /* Display */
282             xosd_display( p_intf->p_sys->p_osd,
283                             0,                               /* first line */
284                             XOSD_string,
285                             psz_display );
286         }
287
288         msleep( INTF_IDLE_SLEEP );
289     }
290 }
291
292 static int PlaylistNext( vlc_object_t *p_this, const char *psz_variable,
293                 vlc_value_t oval, vlc_value_t nval, void *param )
294 {
295     intf_thread_t *p_intf = (intf_thread_t *)param;
296
297     p_intf->p_sys->b_need_update = VLC_TRUE;
298     return VLC_SUCCESS;
299 }
300