xosd.c 10.3 KB
Newer Older
1 2 3
/*****************************************************************************
 * xosd.c : X On Screen Display interface
 *****************************************************************************
4
 * Copyright (C) 2001 the VideoLAN team
5
 * $Id$
6
 *
7
 * Authors: Loïc Minier <lool@videolan.org>
8 9 10 11 12 13 14 15 16 17 18 19 20
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 * 
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
Antoine Cellerier's avatar
Antoine Cellerier committed
21
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43
 *****************************************************************************/

/*****************************************************************************
 * Preamble
 *****************************************************************************/
#include <stdlib.h>                                      /* malloc(), free() */
#include <string.h>

#include <xosd.h>

#include <vlc/intf.h>

#ifdef HAVE_UNISTD_H
#    include <unistd.h>
#endif

/*****************************************************************************
 * intf_sys_t: description and status of rc interface
 *****************************************************************************/
struct intf_sys_t
{
    xosd * p_osd;               /* libxosd handle */
Clément Stenac's avatar
Clément Stenac committed
44
    vlc_bool_t  b_need_update;   /* Update display ? */
45 46 47 48 49 50 51 52
};

#define MAX_LINE_LENGTH 256

/*****************************************************************************
 * Local prototypes.
 *****************************************************************************/
static int  Open         ( vlc_object_t * );
53
static void Close        ( vlc_object_t * );
54

55
static void Run          ( intf_thread_t * );
56

Clément Stenac's avatar
Clément Stenac committed
57 58 59
static int PlaylistNext( vlc_object_t *p_this, const char *psz_variable,
                vlc_value_t oval, vlc_value_t nval, void *param );

60 61 62
/*****************************************************************************
 * Module descriptor
 *****************************************************************************/
Christophe Massiot's avatar
Christophe Massiot committed
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

Christophe Massiot's avatar
Christophe Massiot committed
67
#define TXT_OFS_TEXT N_("Vertical offset")
68 69 70
#define TXT_OFS_LONGTEXT N_("Vertical offset between the border of the screen "\
                            "and the displayed text (in pixels, defaults to "\
                            "30 pixels)." )
71

Christophe Massiot's avatar
Christophe Massiot committed
72
#define SHD_OFS_TEXT N_("Shadow offset")
73 74
#define SHD_OFS_LONGTEXT N_("Offset between the text and the shadow (in " \
                            "pixels, defaults to 2 pixels)." )
75

Christophe Massiot's avatar
Christophe Massiot committed
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 83
    set_category( CAT_INTERFACE );
    set_subcategory( SUBCAT_INTERFACE_CONTROL );
Gildas Bazin's avatar
 
Gildas Bazin committed
84
    set_description( _("XOSD interface") );
85
    set_shortname( "XOSD" );
86
    add_bool( "xosd-position", 1, NULL, POSITION_TEXT, POSITION_LONGTEXT, VLC_TRUE );
Clément Stenac's avatar
Clément Stenac committed
87 88
    add_integer( "xosd-text-offset", 30, NULL, TXT_OFS_TEXT, TXT_OFS_LONGTEXT, VLC_TRUE );
    add_integer( "xosd-shadow-offset", 2, NULL,
89
                 SHD_OFS_TEXT, SHD_OFS_LONGTEXT, VLC_TRUE );
Clément Stenac's avatar
Clément Stenac committed
90 91
    add_string( "xosd-font",
                "-adobe-helvetica-bold-r-normal-*-*-160-*-*-p-*-iso8859-1",
92
                NULL, FONT_TEXT, FONT_LONGTEXT, VLC_TRUE );
Clément Stenac's avatar
Clément Stenac committed
93 94
    add_string( "xosd-colour", "LawnGreen",
                    NULL, COLOUR_TEXT, COLOUR_LONGTEXT, VLC_TRUE );
Gildas Bazin's avatar
 
Gildas Bazin committed
95
    set_capability( "interface", 10 );
96 97 98 99 100 101 102 103 104
    set_callbacks( Open, Close );
vlc_module_end();

/*****************************************************************************
 * Open: initialize and create stuff
 *****************************************************************************/
static int Open( vlc_object_t *p_this )
{
    intf_thread_t *p_intf = (intf_thread_t *)p_this;
105
    xosd *p_osd;
106 107 108 109 110 111

    /* Allocate instance and initialize some members */
    p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
    if( p_intf->p_sys == NULL )
    {
        msg_Err( p_intf, "out of memory" );
112 113 114 115 116 117 118
        return VLC_ENOMEM;
    }

    if( getenv( "DISPLAY" ) == NULL )
    {
        msg_Err( p_intf, "no display, please set the DISPLAY variable" );
        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" ),
Clément Stenac's avatar
Clément Stenac committed
125 126
                   config_GetPsz( p_intf,"xosd-colour" ), 3,
                   XOSD_top, 0, 1 );
127 128 129 130 131
    if( p_intf->p_sys->p_osd == NULL )
    {
        msg_Err( p_intf, "couldn't initialize libxosd" );
        return VLC_EGENERIC;
    }
132 133 134 135 136 137 138 139 140 141 142
#else
    p_osd = p_intf->p_sys->p_osd = xosd_create( 1 );
    if( p_osd == NULL )
    {
        msg_Err( p_intf, "couldn't initialize libxosd" );
        return VLC_EGENERIC;
    }
    xosd_set_colour( p_osd, config_GetPsz( p_intf,"xosd-colour" ) );
    xosd_set_timeout( p_osd, 3 );
#endif

143

144
    playlist_t *p_playlist = pl_Yield( p_intf );
Clément Stenac's avatar
Clément Stenac committed
145 146
    var_AddCallback( p_playlist, "playlist-current", PlaylistNext, p_this );
    var_AddCallback( p_playlist, "item-change", PlaylistNext, p_this );
147
    pl_Release( p_intf );
Clément Stenac's avatar
Clément Stenac committed
148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168

    /* Set user preferences */
    xosd_set_font( p_intf->p_sys->p_osd,
                    config_GetPsz( p_intf, "xosd-font" ) );
    xosd_set_outline_colour( p_intf->p_sys->p_osd,"black" );
#ifdef HAVE_XOSD_VERSION_2
    xosd_set_horizontal_offset( p_intf->p_sys->p_osd,
                    config_GetInt( p_intf, "xosd-text-offset" ) );
    xosd_set_vertical_offset( p_intf->p_sys->p_osd,
                    config_GetInt( p_intf, "xosd-text-offset" ) );
#else
    xosd_set_offset( p_intf->p_sys->p_osd,
                    config_GetInt( p_intf, "xosd-text-offset" ) );
#endif
    xosd_set_shadow_offset( p_intf->p_sys->p_osd,
                    config_GetInt( p_intf, "xosd-shadow-offset" ));
    xosd_set_pos( p_intf->p_sys->p_osd,
                    config_GetInt( p_intf, "xosd-position" ) ?
                                         XOSD_bottom: XOSD_top );

    /* Initialize to NULL */
169
    xosd_display( p_osd, 0, XOSD_string, "XOSD interface initialized" );
170 171 172

    p_intf->pf_run = Run;

Clément Stenac's avatar
Clément Stenac committed
173 174
    p_intf->p_sys->b_need_update = VLC_TRUE;

175
    return VLC_SUCCESS;
176 177 178 179 180 181
}

/*****************************************************************************
 * Close: destroy interface stuff
 *****************************************************************************/
static void Close( vlc_object_t *p_this )
182
{
183
    intf_thread_t *p_intf = (intf_thread_t *)p_this;
184 185 186 187
    playlist_t *p_playlist = pl_Yield( p_intf );
    var_DelCallback( p_playlist, "playlist-current", PlaylistNext, p_this );
    var_DelCallback( p_playlist, "item-change", PlaylistNext, p_this );
    pl_Release( p_intf );
188 189

    /* Uninitialize library */
Clément Stenac's avatar
Clément Stenac committed
190
    xosd_destroy( p_intf->p_sys->p_osd );
191 192 193 194 195 196 197 198 199 200 201 202

    /* Destroy structure */
    free( p_intf->p_sys );
}

/*****************************************************************************
 * Run: xosd thread
 *****************************************************************************
 * This part of the interface runs in a separate thread
 *****************************************************************************/
static void Run( intf_thread_t *p_intf )
{
Clément Stenac's avatar
Clément Stenac committed
203 204
    playlist_t *p_playlist;
    playlist_item_t *p_item = NULL;
205
    input_item_t *p_input;
Clément Stenac's avatar
Clément Stenac committed
206 207
    char psz_duration[MSTRTIME_MAX_SIZE+2];
    char *psz_display = NULL;
208 209 210

    while( !p_intf->b_die )
    {
Clément Stenac's avatar
Clément Stenac committed
211
        if( p_intf->p_sys->b_need_update == VLC_TRUE )
212
        {
Clément Stenac's avatar
Clément Stenac committed
213
            p_intf->p_sys->b_need_update = VLC_FALSE;
214
            p_playlist = pl_Yield( p_intf );
215

216
            if( !playlist_isEmpty( p_playlist ) )
Clément Stenac's avatar
Clément Stenac committed
217 218 219
            {
                vlc_object_release( p_playlist );
                continue;
220
            }
Clément Stenac's avatar
Clément Stenac committed
221 222 223 224 225
            if( psz_display )
            {
                free( psz_display );
                psz_display = NULL;
            }
Clément Stenac's avatar
Clément Stenac committed
226
            if( p_playlist->status.i_status == PLAYLIST_STOPPED )
Clément Stenac's avatar
Clément Stenac committed
227
            {
228
                psz_display = strdup(_("Stop"));
Clément Stenac's avatar
Clément Stenac committed
229 230
                vlc_object_release( p_playlist );
            }
Clément Stenac's avatar
Clément Stenac committed
231
            else if( p_playlist->status.i_status == PLAYLIST_PAUSED )
Clément Stenac's avatar
Clément Stenac committed
232
            {
233
                psz_display = strdup(_("Pause"));
Clément Stenac's avatar
Clément Stenac committed
234 235 236 237
                vlc_object_release( p_playlist );
            }
            else
            {
238 239
                p_item = p_playlist->status.p_item;
                p_input = p_item->p_input;
Clément Stenac's avatar
Clément Stenac committed
240 241 242 243 244 245 246 247
                if( !p_item )
                {
                    vlc_object_release( p_playlist );
                    continue;
                }

                vlc_object_release( p_playlist );

248
                if( p_input->i_duration != -1 )
Clément Stenac's avatar
Clément Stenac committed
249 250
                {
                    char psz_durationstr[MSTRTIME_MAX_SIZE];
251
                    secstotimestr( psz_durationstr, p_input->i_duration/1000000 );
Clément Stenac's avatar
Clément Stenac committed
252 253 254 255 256 257 258 259
                    sprintf( psz_duration, "(%s)", psz_durationstr );
                }
                else
                {
                    sprintf( psz_duration," " );
                }

                psz_display = (char *)malloc( sizeof(char )*
260
                                          (strlen( p_input->psz_name ) +
Clément Stenac's avatar
Clément Stenac committed
261
                                          MSTRTIME_MAX_SIZE + 2+6 + 10 +10 ));
262
                sprintf( psz_display,"%s %s",
263
                         p_input->psz_name, psz_duration);
Clément Stenac's avatar
Clément Stenac committed
264 265 266 267 268 269 270
            }

            /* Display */
            xosd_display( p_intf->p_sys->p_osd,
                            0,                               /* first line */
                            XOSD_string,
                            psz_display );
271 272 273 274
        }

        msleep( INTF_IDLE_SLEEP );
    }
Clément Stenac's avatar
Clément Stenac committed
275
}
276

Clément Stenac's avatar
Clément Stenac committed
277 278 279 280
static int PlaylistNext( vlc_object_t *p_this, const char *psz_variable,
                vlc_value_t oval, vlc_value_t nval, void *param )
{
    intf_thread_t *p_intf = (intf_thread_t *)param;
281

Clément Stenac's avatar
Clément Stenac committed
282 283
    p_intf->p_sys->b_need_update = VLC_TRUE;
    return VLC_SUCCESS;
284 285
}