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