1 /*****************************************************************************
2 * vout_intf.c : video output interface
3 *****************************************************************************
4 * Copyright (C) 2000-2004 VideoLAN
7 * Authors: Gildas Bazin <gbazin@videolan.org>
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.
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.
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 *****************************************************************************/
24 /*****************************************************************************
26 *****************************************************************************/
27 #include <stdlib.h> /* free() */
32 #include "vlc_video.h"
33 #include "video_output.h"
34 #include "vlc_image.h"
37 /*****************************************************************************
39 *****************************************************************************/
41 /* Object variables callbacks */
42 static int ZoomCallback( vlc_object_t *, char const *,
43 vlc_value_t, vlc_value_t, void * );
44 static int OnTopCallback( vlc_object_t *, char const *,
45 vlc_value_t, vlc_value_t, void * );
46 static int FullscreenCallback( vlc_object_t *, char const *,
47 vlc_value_t, vlc_value_t, void * );
49 /*****************************************************************************
50 * vout_RequestWindow: Create/Get a video window if possible.
51 *****************************************************************************
52 * This function looks for the main interface and tries to request
53 * a new video window. If it fails then the vout will still need to create the
55 *****************************************************************************/
56 void *vout_RequestWindow( vout_thread_t *p_vout,
57 int *pi_x_hint, int *pi_y_hint,
58 unsigned int *pi_width_hint,
59 unsigned int *pi_height_hint )
61 intf_thread_t *p_intf = NULL;
68 if( !var_Type( p_vout, "aspect-ratio" ) ) vout_IntfInit( p_vout );
70 /* Get requested coordinates */
71 var_Get( p_vout, "video-x", &val );
72 *pi_x_hint = val.i_int ;
73 var_Get( p_vout, "video-y", &val );
74 *pi_y_hint = val.i_int;
76 *pi_width_hint = p_vout->i_window_width;
77 *pi_height_hint = p_vout->i_window_height;
79 /* Check whether someone provided us with a window ID */
80 var_Get( p_vout->p_vlc, "drawable", &val );
81 if( val.i_int ) return (void *)val.i_int;
83 /* Find if the main interface supports embedding */
84 p_list = vlc_list_find( p_vout, VLC_OBJECT_INTF, FIND_ANYWHERE );
85 if( !p_list ) return NULL;
87 for( i = 0; i < p_list->i_count; i++ )
89 p_intf = (intf_thread_t *)p_list->p_values[i].p_object;
90 if( p_intf->b_block && p_intf->pf_request_window ) break;
96 vlc_list_release( p_list );
100 vlc_object_yield( p_intf );
101 vlc_list_release( p_list );
103 p_window = p_intf->pf_request_window( p_intf, p_vout, pi_x_hint, pi_y_hint,
104 pi_width_hint, pi_height_hint );
106 if( !p_window ) vlc_object_release( p_intf );
107 else p_vout->p_parent_intf = p_intf;
112 void vout_ReleaseWindow( vout_thread_t *p_vout, void *p_window )
114 intf_thread_t *p_intf = p_vout->p_parent_intf;
116 if( !p_intf ) return;
118 vlc_mutex_lock( &p_intf->object_lock );
121 vlc_mutex_unlock( &p_intf->object_lock );
125 if( !p_intf->pf_release_window )
127 msg_Err( p_vout, "no pf_release_window");
128 vlc_mutex_unlock( &p_intf->object_lock );
129 vlc_object_release( p_intf );
133 p_intf->pf_release_window( p_intf, p_window );
135 p_vout->p_parent_intf = NULL;
136 vlc_mutex_unlock( &p_intf->object_lock );
137 vlc_object_release( p_intf );
140 int vout_ControlWindow( vout_thread_t *p_vout, void *p_window,
141 int i_query, va_list args )
143 intf_thread_t *p_intf = p_vout->p_parent_intf;
146 if( !p_intf ) return VLC_EGENERIC;
148 vlc_mutex_lock( &p_intf->object_lock );
151 vlc_mutex_unlock( &p_intf->object_lock );
155 if( !p_intf->pf_control_window )
157 msg_Err( p_vout, "no pf_control_window");
158 vlc_mutex_unlock( &p_intf->object_lock );
162 i_ret = p_intf->pf_control_window( p_intf, p_window, i_query, args );
163 vlc_mutex_unlock( &p_intf->object_lock );
167 /*****************************************************************************
168 * vout_IntfInit: called during the vout creation to initialise misc things.
169 *****************************************************************************/
170 void vout_IntfInit( vout_thread_t *p_vout )
172 vlc_value_t val, text, old_val;
174 /* Create a few object variables we'll need later on */
175 var_Create( p_vout, "snapshot-path", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
176 var_Create( p_vout, "aspect-ratio", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
177 var_Create( p_vout, "width", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
178 var_Create( p_vout, "height", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
179 var_Create( p_vout, "align", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
180 var_Create( p_vout, "video-x", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
181 var_Create( p_vout, "video-y", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
183 var_Create( p_vout, "zoom", VLC_VAR_FLOAT | VLC_VAR_ISCOMMAND |
184 VLC_VAR_HASCHOICE | VLC_VAR_DOINHERIT );
186 text.psz_string = _("Zoom");
187 var_Change( p_vout, "zoom", VLC_VAR_SETTEXT, &text, NULL );
189 var_Get( p_vout, "zoom", &old_val );
190 if( old_val.f_float == 0.25 ||
191 old_val.f_float == 0.5 ||
192 old_val.f_float == 1 ||
193 old_val.f_float == 2 )
195 var_Change( p_vout, "zoom", VLC_VAR_DELCHOICE, &old_val, NULL );
198 val.f_float = 0.25; text.psz_string = _("1:4 Quarter");
199 var_Change( p_vout, "zoom", VLC_VAR_ADDCHOICE, &val, &text );
200 val.f_float = 0.5; text.psz_string = _("1:2 Half");
201 var_Change( p_vout, "zoom", VLC_VAR_ADDCHOICE, &val, &text );
202 val.f_float = 1; text.psz_string = _("1:1 Original");
203 var_Change( p_vout, "zoom", VLC_VAR_ADDCHOICE, &val, &text );
204 val.f_float = 2; text.psz_string = _("2:1 Double");
205 var_Change( p_vout, "zoom", VLC_VAR_ADDCHOICE, &val, &text );
207 var_Set( p_vout, "zoom", old_val );
209 var_AddCallback( p_vout, "zoom", ZoomCallback, NULL );
211 /* Add a variable to indicate if the window should be on top of others */
212 var_Create( p_vout, "video-on-top", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
213 text.psz_string = _("Always on top");
214 var_Change( p_vout, "video-on-top", VLC_VAR_SETTEXT, &text, NULL );
215 var_AddCallback( p_vout, "video-on-top", OnTopCallback, NULL );
217 /* Add a variable to indicate whether we want window decoration or not */
218 var_Create( p_vout, "video-deco", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
220 /* Add a fullscreen variable */
221 var_Create( p_vout, "fullscreen", VLC_VAR_BOOL );
222 text.psz_string = _("Fullscreen");
223 var_Change( p_vout, "fullscreen", VLC_VAR_SETTEXT, &text, NULL );
224 var_Change( p_vout, "fullscreen", VLC_VAR_INHERITVALUE, &val, NULL );
227 /* user requested fullscreen */
228 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
230 var_AddCallback( p_vout, "fullscreen", FullscreenCallback, NULL );
232 /* Mouse coordinates */
233 var_Create( p_vout, "mouse-x", VLC_VAR_INTEGER );
234 var_Create( p_vout, "mouse-y", VLC_VAR_INTEGER );
235 var_Create( p_vout, "mouse-button-down", VLC_VAR_INTEGER );
236 var_Create( p_vout, "mouse-moved", VLC_VAR_BOOL );
237 var_Create( p_vout, "mouse-clicked", VLC_VAR_INTEGER );
239 var_Create( p_vout, "intf-change", VLC_VAR_BOOL );
240 val.b_bool = VLC_TRUE;
241 var_Set( p_vout, "intf-change", val );
244 /*****************************************************************************
245 * vout_Snapshot: generates a snapshot.
246 *****************************************************************************/
247 int vout_Snapshot( vout_thread_t *p_vout, picture_t *p_pic )
249 image_handler_t *p_image = image_HandlerCreate( p_vout );
250 video_format_t fmt_in = {0}, fmt_out = {0};
252 subpicture_t *p_subpic;
257 var_Get( p_vout, "snapshot-path", &val );
258 if( val.psz_string && !*val.psz_string )
260 free( val.psz_string );
263 if( !val.psz_string && p_vout->p_vlc->psz_homedir )
265 asprintf( &val.psz_string, "%s/" CONFIG_DIR,
266 p_vout->p_vlc->psz_homedir );
268 if( !val.psz_string )
270 msg_Err( p_vout, "no directory specified for snapshots" );
274 asprintf( &psz_filename, "%s/vlcsnap-%u.png", val.psz_string,
275 (unsigned int)(p_pic->date / 100000) & 0xFFFFFF );
276 free( val.psz_string );
278 /* Save the snapshot */
279 fmt_in.i_chroma = p_vout->render.i_chroma;
280 fmt_in.i_width = p_vout->render.i_width;
281 fmt_in.i_height = p_vout->render.i_height;
282 i_ret = image_WriteUrl( p_image, p_pic, &fmt_in, &fmt_out, psz_filename );
283 if( i_ret != VLC_SUCCESS )
285 msg_Err( p_vout, "could not create snapshot %s", psz_filename );
286 free( psz_filename );
287 image_HandlerDelete( p_image );
291 msg_Dbg( p_vout, "snapshot taken (%s)", psz_filename );
292 free( psz_filename );
294 /* Inject a subpicture with the snapshot */
295 fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
296 fmt_out.i_width = fmt_out.i_visible_width = p_vout->render.i_width;
297 fmt_out.i_height = fmt_out.i_visible_height = p_vout->render.i_height;
298 fmt_out.i_aspect = VOUT_ASPECT_FACTOR;
299 p_pif = image_Convert( p_image, p_pic, &fmt_in, &fmt_out );
300 image_HandlerDelete( p_image );
301 if( !p_pif ) return VLC_EGENERIC;
303 p_subpic = spu_CreateSubpicture( p_vout->p_spu );
304 if( p_subpic == NULL )
306 p_pif->pf_release( p_pif );
310 p_subpic->i_channel = 0;
311 p_subpic->i_start = mdate();
312 p_subpic->i_stop = mdate() + 4000000;
313 p_subpic->b_ephemer = VLC_TRUE;
314 p_subpic->b_fade = VLC_TRUE;
315 p_subpic->i_original_picture_width = p_vout->render.i_width * 4;
316 p_subpic->i_original_picture_height = p_vout->render.i_height * 4;
318 p_subpic->p_region = spu_CreateRegion( p_vout->p_spu, &fmt_out );
319 vout_CopyPicture( p_image->p_parent, &p_subpic->p_region->picture, p_pif );
320 p_pif->pf_release( p_pif );
322 spu_DisplaySubpicture( p_vout->p_spu, p_subpic );
327 /*****************************************************************************
328 * vout_ControlDefault: default methods for video output control.
329 *****************************************************************************/
330 int vout_vaControlDefault( vout_thread_t *p_vout, int i_query, va_list args )
336 if( p_vout->p_parent_intf )
338 vlc_object_release( p_vout->p_parent_intf );
339 p_vout->p_parent_intf = NULL;
345 p_vout->b_snapshot = VLC_TRUE;
350 msg_Dbg( p_vout, "control query not supported" );
355 /*****************************************************************************
356 * Object variables callbacks
357 *****************************************************************************/
358 static int ZoomCallback( vlc_object_t *p_this, char const *psz_cmd,
359 vlc_value_t oldval, vlc_value_t newval, void *p_data )
361 vout_thread_t *p_vout = (vout_thread_t *)p_this;
362 vout_Control( p_vout, VOUT_SET_ZOOM, newval.f_float );
366 static int OnTopCallback( vlc_object_t *p_this, char const *psz_cmd,
367 vlc_value_t oldval, vlc_value_t newval, void *p_data )
369 vout_thread_t *p_vout = (vout_thread_t *)p_this;
370 playlist_t *p_playlist;
371 vout_Control( p_vout, VOUT_SET_STAY_ON_TOP, newval.b_bool );
373 p_playlist = (playlist_t *)vlc_object_find( p_this, VLC_OBJECT_PLAYLIST,
377 /* Modify playlist as well because the vout might have to be restarted */
378 var_Create( p_playlist, "video-on-top", VLC_VAR_BOOL );
379 var_Set( p_playlist, "video-on-top", newval );
381 vlc_object_release( p_playlist );
386 static int FullscreenCallback( vlc_object_t *p_this, char const *psz_cmd,
387 vlc_value_t oldval, vlc_value_t newval, void *p_data )
389 vout_thread_t *p_vout = (vout_thread_t *)p_this;
390 playlist_t *p_playlist;
393 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
395 p_playlist = (playlist_t *)vlc_object_find( p_this, VLC_OBJECT_PLAYLIST,
399 /* Modify playlist as well because the vout might have to be restarted */
400 var_Create( p_playlist, "fullscreen", VLC_VAR_BOOL );
401 var_Set( p_playlist, "fullscreen", newval );
403 vlc_object_release( p_playlist );
406 /* Disable "always on top" in fullscreen mode */
407 var_Get( p_vout, "video-on-top", &val );
408 if( newval.b_bool && val.b_bool )
410 val.b_bool = VLC_FALSE;
411 vout_Control( p_vout, VOUT_SET_STAY_ON_TOP, val.b_bool );
413 else if( !newval.b_bool && val.b_bool )
415 vout_Control( p_vout, VOUT_SET_STAY_ON_TOP, val.b_bool );
418 val.b_bool = VLC_TRUE;
419 var_Set( p_vout, "intf-change", val );