]> git.sesse.net Git - vlc/blob - modules/visualization/xosd/xosd.c
23297ed94a5173e27c07bcc6271e1ec53f8b8d51
[vlc] / modules / visualization / xosd / xosd.c
1 /*****************************************************************************
2  * xosd.c : X On Screen Display interface
3  *****************************************************************************
4  * Copyright (C) 2001 VideoLAN
5  * $Id: xosd.c,v 1.7 2003/02/03 01:32:37 sam Exp $
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     input_thread_t * p_input;   /* associated input thread */
44     xosd * p_osd;               /* libxosd handle */
45     char * psz_source;          /* current file || NULL */
46 };
47
48 #define MAX_LINE_LENGTH 256
49
50 /*****************************************************************************
51  * Local prototypes.
52  *****************************************************************************/
53 static int  Open         ( vlc_object_t * );
54 static void Close        ( vlc_object_t * );
55
56 static void Run          ( intf_thread_t * );
57
58 /*****************************************************************************
59  * Module descriptor
60  *****************************************************************************/
61 #define POSITION_TEXT N_("flip vertical position")
62 #define POSITION_LONGTEXT N_("Display xosd output on the bottom of the " \
63                              "screen instead of the top")
64
65 #define TXT_OFS_TEXT N_("vertical offset")
66 #define TXT_OFS_LONGTEXT N_("Vertical offset in pixels of the displayed text")
67
68 #define SHD_OFS_TEXT N_("shadow offset")
69 #define SHD_OFS_LONGTEXT N_("Offset in pixels of the shadow")
70
71 #define FONT_TEXT N_("font")
72 #define FONT_LONGTEXT N_("Font used to display text in the xosd output")
73
74 vlc_module_begin();
75     int i = getenv( "DISPLAY" ) == NULL ? 10 : 90;
76     add_category_hint( N_("XOSD module"), NULL );
77     add_bool( "xosd-position", 1, NULL, POSITION_TEXT, POSITION_LONGTEXT );
78     add_integer( "xosd-text-offset", 0, NULL, TXT_OFS_TEXT, TXT_OFS_LONGTEXT );
79     add_integer( "xosd-shadow-offset", 1, NULL,
80                  SHD_OFS_TEXT, SHD_OFS_LONGTEXT );
81     add_string( "xosd-font", "-misc-fixed-medium-r-*-*-*-300-*-*-*-*-*-*",
82                 NULL, FONT_TEXT, FONT_LONGTEXT );
83     set_description( _("xosd interface module") );
84     set_capability( "interface", i );
85     set_callbacks( Open, Close );
86 vlc_module_end();
87
88 /*****************************************************************************
89  * Open: initialize and create stuff
90  *****************************************************************************/
91 static int Open( vlc_object_t *p_this )
92 {
93     intf_thread_t *p_intf = (intf_thread_t *)p_this;
94
95     /* Allocate instance and initialize some members */
96     p_intf->p_sys = (intf_sys_t *)malloc( sizeof( intf_sys_t ) );
97     if( p_intf->p_sys == NULL )
98     {
99         msg_Err( p_intf, "out of memory" );
100         return VLC_ENOMEM;
101     }
102
103     if( getenv( "DISPLAY" ) == NULL )
104     {
105         msg_Err( p_intf, "no display, please set the DISPLAY variable" );
106         return VLC_EGENERIC;
107     }
108
109     /* Initialize library */
110     p_intf->p_sys->p_osd =
111 #if defined(HAVE_XOSD_VERSION_0) || defined(HAVE_XOSD_VERSION_1)
112         xosd_init( config_GetPsz( p_intf, "xosd-font" ),
113                    "LawnGreen", 3, XOSD_top, 0, 1 );
114 #else
115         xosd_init( config_GetPsz( p_intf, "xosd-font" ),
116                    "LawnGreen", 3, XOSD_top, 0, 0, 1 );
117 #endif
118
119     if( p_intf->p_sys->p_osd == NULL )
120     {
121         msg_Err( p_intf, "couldn't initialize libxosd" );
122         return VLC_EGENERIC;
123     }
124
125     /* Initialize to NULL */
126     p_intf->p_sys->psz_source = NULL;
127
128     xosd_display( p_intf->p_sys->p_osd,
129                   0,
130                   XOSD_string,
131                   "xosd interface initialized" );
132
133     p_intf->pf_run = Run;
134
135     return VLC_SUCCESS;
136 }
137
138 /*****************************************************************************
139  * Close: destroy interface stuff
140  *****************************************************************************/
141 static void Close( vlc_object_t *p_this )
142 {
143     intf_thread_t *p_intf = (intf_thread_t *)p_this;
144
145     if( p_intf->p_sys->psz_source ) free( p_intf->p_sys->psz_source );
146
147     /* Uninitialize library */
148     xosd_uninit( p_intf->p_sys->p_osd );
149
150     /* Destroy structure */
151     free( p_intf->p_sys );
152 }
153
154 /*****************************************************************************
155  * Run: xosd thread
156  *****************************************************************************
157  * This part of the interface runs in a separate thread
158  *****************************************************************************/
159 static void Run( intf_thread_t *p_intf )
160 {
161     p_intf->p_sys->p_input = NULL;
162
163     while( !p_intf->b_die )
164     {
165         /* Manage the input part */
166         if( p_intf->p_sys->p_input == NULL )
167         {
168             p_intf->p_sys->p_input = vlc_object_find( p_intf, VLC_OBJECT_INPUT,
169                                                               FIND_ANYWHERE );
170         }
171         else if( p_intf->p_sys->p_input->b_dead )
172         {
173             vlc_object_release( p_intf->p_sys->p_input );
174             p_intf->p_sys->p_input = NULL;
175         }
176         else /* We have a valid input */
177         {
178             /* Did source change? */
179             if ( (p_intf->p_sys->psz_source == NULL)
180                  || (strcmp( p_intf->p_sys->psz_source,
181                              p_intf->p_sys->p_input->psz_source ) != 0)
182                )
183             {
184                 if( p_intf->p_sys->psz_source )
185                     free( p_intf->p_sys->psz_source );
186
187                 p_intf->p_sys->psz_source =
188                     strdup( p_intf->p_sys->p_input->psz_source );
189
190                 /* Set user preferences */
191                 xosd_set_font( p_intf->p_sys->p_osd,
192                                config_GetPsz( p_intf, "xosd-font" ) );
193 #ifdef HAVE_XOSD_VERSION_2
194                 xosd_set_horizontal_offset( p_intf->p_sys->p_osd,
195                     config_GetInt( p_intf, "xosd-text-offset" ) );
196                 xosd_set_vertical_offset( p_intf->p_sys->p_osd,
197                     config_GetInt( p_intf, "xosd-text-offset" ) );
198 #else
199                 xosd_set_offset( p_intf->p_sys->p_osd,
200                     config_GetInt( p_intf, "xosd-text-offset" ) );
201 #endif
202                 xosd_set_shadow_offset( p_intf->p_sys->p_osd,
203                     config_GetInt( p_intf, "xosd-shadow-offset" ));
204                 xosd_set_pos( p_intf->p_sys->p_osd,
205                     config_GetInt( p_intf, "xosd-position" ) ? XOSD_bottom
206                                                              : XOSD_top );
207
208                 /* Display */
209                 xosd_display( p_intf->p_sys->p_osd,
210                               0,                               /* first line */
211                               XOSD_string,
212                               p_intf->p_sys->psz_source );
213             }
214         }
215
216         msleep( INTF_IDLE_SLEEP );
217     }
218
219     if( p_intf->p_sys->p_input )
220     {
221         vlc_object_release( p_intf->p_sys->p_input );
222         p_intf->p_sys->p_input = NULL;
223     }
224
225 }
226