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