equalizer.c: compile fix for old compilers (thx sam)
[vlc.git] / modules / visualization / xosd.c
1 /*****************************************************************************
2  * xosd.c : X On Screen Display interface
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
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., 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 <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 on 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 in pixels of the displayed text")
69
70 #define SHD_OFS_TEXT N_("Shadow offset")
71 #define SHD_OFS_LONGTEXT N_("Offset in pixels of the shadow")
72
73 #define FONT_TEXT N_("Font")
74 #define FONT_LONGTEXT N_("Font used to display text in the xosd output")
75 /* FIXME FIXME FIXME: Gettextize */
76 #define COLOUR_TEXT ("Colour")
77 #define COLOUR_LONGTEXT ("Colour used to display text in the xosd output")
78
79 vlc_module_begin();
80     set_description( _("XOSD interface") );
81     add_bool( "xosd-position", 1, NULL, POSITION_TEXT, POSITION_LONGTEXT, VLC_TRUE );
82     add_integer( "xosd-text-offset", 30, NULL, TXT_OFS_TEXT, TXT_OFS_LONGTEXT, VLC_TRUE );
83     add_integer( "xosd-shadow-offset", 2, NULL,
84                  SHD_OFS_TEXT, SHD_OFS_LONGTEXT, VLC_TRUE );
85     add_string( "xosd-font",
86                 "-adobe-helvetica-bold-r-normal-*-*-160-*-*-p-*-iso8859-1",
87                 NULL, FONT_TEXT, FONT_LONGTEXT, VLC_TRUE );
88     add_string( "xosd-colour", "LawnGreen",
89                     NULL, COLOUR_TEXT, COLOUR_LONGTEXT, VLC_TRUE );
90     set_capability( "interface", 10 );
91     set_callbacks( Open, Close );
92 vlc_module_end();
93
94 /*****************************************************************************
95  * Open: initialize and create stuff
96  *****************************************************************************/
97 static int Open( vlc_object_t *p_this )
98 {
99     intf_thread_t *p_intf = (intf_thread_t *)p_this;
100
101     /* Allocate instance and initialize some members */
102     p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
103     if( p_intf->p_sys == NULL )
104     {
105         msg_Err( p_intf, "out of memory" );
106         return VLC_ENOMEM;
107     }
108
109     if( getenv( "DISPLAY" ) == NULL )
110     {
111         msg_Err( p_intf, "no display, please set the DISPLAY variable" );
112         return VLC_EGENERIC;
113     }
114
115     /* Initialize library */
116     p_intf->p_sys->p_osd =
117 #if defined(HAVE_XOSD_VERSION_0) || defined(HAVE_XOSD_VERSION_1)
118         xosd_init( config_GetPsz( p_intf, "xosd-font" ),
119                    config_GetPsz( p_intf,"xosd-colour" ), 3,
120                    XOSD_top, 0, 1 );
121 #else
122         xosd_init( config_GetPsz( p_intf, "xosd-font" ),
123                    config_GetPsz( p_intf,"xosd-colour" ), 3,
124                     XOSD_top, 0, 0, 1 );
125 #endif
126
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
133     playlist_t *p_playlist =
134             (playlist_t *)vlc_object_find( p_intf, VLC_OBJECT_PLAYLIST,
135                                            FIND_ANYWHERE );
136     if( p_playlist == NULL )
137     {
138         return VLC_EGENERIC;
139     }
140
141     var_AddCallback( p_playlist, "playlist-current", PlaylistNext, p_this );
142     var_AddCallback( p_playlist, "item-change", PlaylistNext, p_this );
143
144     vlc_object_release( p_playlist );
145
146     /* Set user preferences */
147     xosd_set_font( p_intf->p_sys->p_osd,
148                     config_GetPsz( p_intf, "xosd-font" ) );
149     xosd_set_outline_colour( p_intf->p_sys->p_osd,"black" );
150 #ifdef HAVE_XOSD_VERSION_2
151     xosd_set_horizontal_offset( p_intf->p_sys->p_osd,
152                     config_GetInt( p_intf, "xosd-text-offset" ) );
153     xosd_set_vertical_offset( p_intf->p_sys->p_osd,
154                     config_GetInt( p_intf, "xosd-text-offset" ) );
155 #else
156     xosd_set_offset( p_intf->p_sys->p_osd,
157                     config_GetInt( p_intf, "xosd-text-offset" ) );
158 #endif
159     xosd_set_shadow_offset( p_intf->p_sys->p_osd,
160                     config_GetInt( p_intf, "xosd-shadow-offset" ));
161     xosd_set_pos( p_intf->p_sys->p_osd,
162                     config_GetInt( p_intf, "xosd-position" ) ?
163                                          XOSD_bottom: XOSD_top );
164
165     /* Initialize to NULL */
166     xosd_display( p_intf->p_sys->p_osd,
167                   0,
168                   XOSD_string,
169                   "xosd interface initialized" );
170
171     p_intf->pf_run = Run;
172
173     p_intf->p_sys->b_need_update = VLC_TRUE;
174
175     return VLC_SUCCESS;
176 }
177
178 /*****************************************************************************
179  * Close: destroy interface stuff
180  *****************************************************************************/
181 static void Close( vlc_object_t *p_this )
182 {
183     intf_thread_t *p_intf = (intf_thread_t *)p_this;
184
185     /* Uninitialize library */
186     xosd_destroy( p_intf->p_sys->p_osd );
187
188     /* Destroy structure */
189     free( p_intf->p_sys );
190 }
191
192 /*****************************************************************************
193  * Run: xosd thread
194  *****************************************************************************
195  * This part of the interface runs in a separate thread
196  *****************************************************************************/
197 static void Run( intf_thread_t *p_intf )
198 {
199     int i_size,i_index;
200     playlist_t *p_playlist;
201     playlist_item_t *p_item = NULL;
202     input_item_t item;
203     char psz_duration[MSTRTIME_MAX_SIZE+2];
204     char *psz_display = NULL;
205
206     while( !p_intf->b_die )
207     {
208         if( p_intf->p_sys->b_need_update == VLC_TRUE )
209         {
210             p_intf->p_sys->b_need_update = VLC_FALSE;
211             p_playlist = (playlist_t *)vlc_object_find( p_intf,
212                                       VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
213             if( !p_playlist )
214             {
215                 continue;
216             }
217
218             if( p_playlist->i_size < 0 || p_playlist->i_index < 0 )
219             {
220                 vlc_object_release( p_playlist );
221                 continue;
222             }
223             if( psz_display )
224             {
225                 free( psz_display );
226                 psz_display = NULL;
227             }
228             if( p_playlist->i_status == PLAYLIST_STOPPED )
229             {
230                 psz_display = (char *)malloc( sizeof(char )*strlen(_("Stop")));
231                 sprintf( psz_display,_("Stop") );
232                 vlc_object_release( p_playlist );
233             }
234             else if( p_playlist->i_status == PLAYLIST_PAUSED )
235             {
236                 psz_display = (char *)malloc( sizeof(char )*strlen(_("Pause")));
237                 sprintf( psz_display,_("Pause") );
238                 vlc_object_release( p_playlist );
239             }
240             else
241             {
242     //           vlc_mutex_lock(&p_playlist->object_lock );
243                  p_item = playlist_ItemGetByPos( p_playlist,
244                                  p_playlist->i_index );
245                 item = p_item->input;
246                 if( !p_item )
247                 {
248                     vlc_object_release( p_playlist );
249      //            vlc_mutex_unlock(&p_playlist->object_lock );
250                     continue;
251                 }
252                 i_size = p_playlist->i_size;
253                 i_index = p_playlist->i_index+1;
254     //            vlc_mutex_unlock(&p_playlist->object_lock );
255
256                 vlc_object_release( p_playlist );
257
258                 if( item.i_duration != -1 )
259                 {
260                     char psz_durationstr[MSTRTIME_MAX_SIZE];
261                     secstotimestr( psz_durationstr, item.i_duration/1000000 );
262                     sprintf( psz_duration, "(%s)", psz_durationstr );
263                 }
264                 else
265                 {
266                     sprintf( psz_duration," " );
267                 }
268
269                 psz_display = (char *)malloc( sizeof(char )*
270                                           (strlen( item.psz_name ) +
271                                           MSTRTIME_MAX_SIZE + 2+6 + 10 +10 ));
272                 sprintf( psz_display," %i/%i - %s %s",
273                          i_index,i_size, item.psz_name, psz_duration);
274             }
275
276             /* Display */
277             xosd_display( p_intf->p_sys->p_osd,
278                             0,                               /* first line */
279                             XOSD_string,
280                             psz_display );
281         }
282
283         msleep( INTF_IDLE_SLEEP );
284     }
285 }
286
287 static int PlaylistNext( vlc_object_t *p_this, const char *psz_variable,
288                 vlc_value_t oval, vlc_value_t nval, void *param )
289 {
290     intf_thread_t *p_intf = (intf_thread_t *)param;
291
292     p_intf->p_sys->b_need_update = VLC_TRUE;
293     return VLC_SUCCESS;
294 }
295