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