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 * );
48 static int SnapshotCallback( vlc_object_t *, char const *,
49 vlc_value_t, vlc_value_t, void * );
51 /*****************************************************************************
52 * vout_RequestWindow: Create/Get a video window if possible.
53 *****************************************************************************
54 * This function looks for the main interface and tries to request
55 * a new video window. If it fails then the vout will still need to create the
57 *****************************************************************************/
58 void *vout_RequestWindow( vout_thread_t *p_vout,
59 int *pi_x_hint, int *pi_y_hint,
60 unsigned int *pi_width_hint,
61 unsigned int *pi_height_hint )
63 intf_thread_t *p_intf = NULL;
70 if( !var_Type( p_vout, "aspect-ratio" ) ) vout_IntfInit( p_vout );
72 /* Get requested coordinates */
73 var_Get( p_vout, "video-x", &val );
74 *pi_x_hint = val.i_int ;
75 var_Get( p_vout, "video-y", &val );
76 *pi_y_hint = val.i_int;
78 *pi_width_hint = p_vout->i_window_width;
79 *pi_height_hint = p_vout->i_window_height;
81 /* Check whether someone provided us with a window ID */
82 var_Get( p_vout->p_vlc, "drawable", &val );
83 if( val.i_int ) return (void *)val.i_int;
85 /* Find if the main interface supports embedding */
86 p_list = vlc_list_find( p_vout, VLC_OBJECT_INTF, FIND_ANYWHERE );
87 if( !p_list ) return NULL;
89 for( i = 0; i < p_list->i_count; i++ )
91 p_intf = (intf_thread_t *)p_list->p_values[i].p_object;
92 if( p_intf->b_block && p_intf->pf_request_window ) break;
98 vlc_list_release( p_list );
102 vlc_object_yield( p_intf );
103 vlc_list_release( p_list );
105 p_window = p_intf->pf_request_window( p_intf, p_vout, pi_x_hint, pi_y_hint,
106 pi_width_hint, pi_height_hint );
108 if( !p_window ) vlc_object_release( p_intf );
109 else p_vout->p_parent_intf = p_intf;
114 void vout_ReleaseWindow( vout_thread_t *p_vout, void *p_window )
116 intf_thread_t *p_intf = p_vout->p_parent_intf;
118 if( !p_intf ) return;
120 vlc_mutex_lock( &p_intf->object_lock );
123 vlc_mutex_unlock( &p_intf->object_lock );
127 if( !p_intf->pf_release_window )
129 msg_Err( p_vout, "no pf_release_window");
130 vlc_mutex_unlock( &p_intf->object_lock );
131 vlc_object_release( p_intf );
135 p_intf->pf_release_window( p_intf, p_window );
137 p_vout->p_parent_intf = NULL;
138 vlc_mutex_unlock( &p_intf->object_lock );
139 vlc_object_release( p_intf );
142 int vout_ControlWindow( vout_thread_t *p_vout, void *p_window,
143 int i_query, va_list args )
145 intf_thread_t *p_intf = p_vout->p_parent_intf;
148 if( !p_intf ) return VLC_EGENERIC;
150 vlc_mutex_lock( &p_intf->object_lock );
153 vlc_mutex_unlock( &p_intf->object_lock );
157 if( !p_intf->pf_control_window )
159 msg_Err( p_vout, "no pf_control_window");
160 vlc_mutex_unlock( &p_intf->object_lock );
164 i_ret = p_intf->pf_control_window( p_intf, p_window, i_query, args );
165 vlc_mutex_unlock( &p_intf->object_lock );
169 /*****************************************************************************
170 * vout_IntfInit: called during the vout creation to initialise misc things.
171 *****************************************************************************/
172 void vout_IntfInit( vout_thread_t *p_vout )
174 vlc_value_t val, text, old_val;
176 /* Create a few object variables we'll need later on */
177 var_Create( p_vout, "snapshot-path", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
178 var_Create( p_vout, "aspect-ratio", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
179 var_Create( p_vout, "width", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
180 var_Create( p_vout, "height", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
181 var_Create( p_vout, "align", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
182 var_Create( p_vout, "video-x", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
183 var_Create( p_vout, "video-y", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
185 var_Create( p_vout, "zoom", VLC_VAR_FLOAT | VLC_VAR_ISCOMMAND |
186 VLC_VAR_HASCHOICE | VLC_VAR_DOINHERIT );
188 text.psz_string = _("Zoom");
189 var_Change( p_vout, "zoom", VLC_VAR_SETTEXT, &text, NULL );
191 var_Get( p_vout, "zoom", &old_val );
192 if( old_val.f_float == 0.25 ||
193 old_val.f_float == 0.5 ||
194 old_val.f_float == 1 ||
195 old_val.f_float == 2 )
197 var_Change( p_vout, "zoom", VLC_VAR_DELCHOICE, &old_val, NULL );
200 val.f_float = 0.25; text.psz_string = _("1:4 Quarter");
201 var_Change( p_vout, "zoom", VLC_VAR_ADDCHOICE, &val, &text );
202 val.f_float = 0.5; text.psz_string = _("1:2 Half");
203 var_Change( p_vout, "zoom", VLC_VAR_ADDCHOICE, &val, &text );
204 val.f_float = 1; text.psz_string = _("1:1 Original");
205 var_Change( p_vout, "zoom", VLC_VAR_ADDCHOICE, &val, &text );
206 val.f_float = 2; text.psz_string = _("2:1 Double");
207 var_Change( p_vout, "zoom", VLC_VAR_ADDCHOICE, &val, &text );
209 var_Set( p_vout, "zoom", old_val );
211 var_AddCallback( p_vout, "zoom", ZoomCallback, NULL );
213 /* Add a variable to indicate if the window should be on top of others */
214 var_Create( p_vout, "video-on-top", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
215 text.psz_string = _("Always on top");
216 var_Change( p_vout, "video-on-top", VLC_VAR_SETTEXT, &text, NULL );
217 var_AddCallback( p_vout, "video-on-top", OnTopCallback, NULL );
219 /* Add a variable to indicate whether we want window decoration or not */
220 var_Create( p_vout, "video-deco", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
222 /* Add a fullscreen variable */
223 var_Create( p_vout, "fullscreen", VLC_VAR_BOOL );
224 text.psz_string = _("Fullscreen");
225 var_Change( p_vout, "fullscreen", VLC_VAR_SETTEXT, &text, NULL );
226 var_Change( p_vout, "fullscreen", VLC_VAR_INHERITVALUE, &val, NULL );
229 /* user requested fullscreen */
230 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
232 var_AddCallback( p_vout, "fullscreen", FullscreenCallback, NULL );
234 /* Add a snapshot variable */
235 var_Create( p_vout, "video-snapshot", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
236 text.psz_string = _("Snapshot");
237 var_Change( p_vout, "video-snapshot", VLC_VAR_SETTEXT, &text, NULL );
238 var_AddCallback( p_vout, "video-snapshot", SnapshotCallback, NULL );
240 /* Mouse coordinates */
241 var_Create( p_vout, "mouse-x", VLC_VAR_INTEGER );
242 var_Create( p_vout, "mouse-y", VLC_VAR_INTEGER );
243 var_Create( p_vout, "mouse-button-down", VLC_VAR_INTEGER );
244 var_Create( p_vout, "mouse-moved", VLC_VAR_BOOL );
245 var_Create( p_vout, "mouse-clicked", VLC_VAR_INTEGER );
247 var_Create( p_vout, "intf-change", VLC_VAR_BOOL );
248 val.b_bool = VLC_TRUE;
249 var_Set( p_vout, "intf-change", val );
252 /*****************************************************************************
253 * vout_Snapshot: generates a snapshot.
254 *****************************************************************************/
255 int vout_Snapshot( vout_thread_t *p_vout, picture_t *p_pic )
257 image_handler_t *p_image = image_HandlerCreate( p_vout );
258 video_format_t fmt_in = {0}, fmt_out = {0};
260 subpicture_t *p_subpic;
265 var_Get( p_vout, "snapshot-path", &val );
266 if( val.psz_string && !*val.psz_string )
268 free( val.psz_string );
271 if( !val.psz_string && p_vout->p_vlc->psz_homedir )
273 asprintf( &val.psz_string, "%s/" CONFIG_DIR,
274 p_vout->p_vlc->psz_homedir );
276 if( !val.psz_string )
278 msg_Err( p_vout, "no directory specified for snapshots" );
282 asprintf( &psz_filename, "%s/vlcsnap-%u.png", val.psz_string,
283 (unsigned int)(p_pic->date / 100000) & 0xFFFFFF );
284 free( val.psz_string );
286 /* Save the snapshot */
287 fmt_in.i_chroma = p_vout->render.i_chroma;
288 fmt_in.i_width = p_vout->render.i_width;
289 fmt_in.i_height = p_vout->render.i_height;
290 i_ret = image_WriteUrl( p_image, p_pic, &fmt_in, &fmt_out, psz_filename );
291 if( i_ret != VLC_SUCCESS )
293 msg_Err( p_vout, "could not create snapshot %s", psz_filename );
294 free( psz_filename );
295 image_HandlerDelete( p_image );
299 msg_Dbg( p_vout, "snapshot taken (%s)", psz_filename );
300 free( psz_filename );
302 /* Inject a subpicture with the snapshot */
303 fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
304 fmt_out.i_width = fmt_out.i_visible_width = p_vout->render.i_width;
305 fmt_out.i_height = fmt_out.i_visible_height = p_vout->render.i_height;
306 fmt_out.i_aspect = VOUT_ASPECT_FACTOR;
307 p_pif = image_Convert( p_image, p_pic, &fmt_in, &fmt_out );
308 image_HandlerDelete( p_image );
309 if( !p_pif ) return VLC_EGENERIC;
311 p_subpic = spu_CreateSubpicture( p_vout->p_spu );
312 if( p_subpic == NULL )
314 p_pif->pf_release( p_pif );
318 p_subpic->i_channel = 0;
319 p_subpic->i_start = mdate();
320 p_subpic->i_stop = mdate() + 4000000;
321 p_subpic->b_ephemer = VLC_TRUE;
322 p_subpic->b_fade = VLC_TRUE;
323 p_subpic->i_original_picture_width = p_vout->render.i_width * 4;
324 p_subpic->i_original_picture_height = p_vout->render.i_height * 4;
326 p_subpic->p_region = spu_CreateRegion( p_vout->p_spu, &fmt_out );
327 vout_CopyPicture( p_image->p_parent, &p_subpic->p_region->picture, p_pif );
328 p_pif->pf_release( p_pif );
330 spu_DisplaySubpicture( p_vout->p_spu, p_subpic );
335 /*****************************************************************************
336 * vout_ControlDefault: default methods for video output control.
337 *****************************************************************************/
338 int vout_vaControlDefault( vout_thread_t *p_vout, int i_query, va_list args )
344 if( p_vout->p_parent_intf )
346 vlc_object_release( p_vout->p_parent_intf );
347 p_vout->p_parent_intf = NULL;
353 p_vout->b_snapshot = VLC_TRUE;
358 msg_Dbg( p_vout, "control query not supported" );
363 /*****************************************************************************
364 * Object variables callbacks
365 *****************************************************************************/
366 static int ZoomCallback( 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 vout_Control( p_vout, VOUT_SET_ZOOM, newval.f_float );
374 static int OnTopCallback( vlc_object_t *p_this, char const *psz_cmd,
375 vlc_value_t oldval, vlc_value_t newval, void *p_data )
377 vout_thread_t *p_vout = (vout_thread_t *)p_this;
378 playlist_t *p_playlist;
379 vout_Control( p_vout, VOUT_SET_STAY_ON_TOP, newval.b_bool );
381 p_playlist = (playlist_t *)vlc_object_find( p_this, VLC_OBJECT_PLAYLIST,
385 /* Modify playlist as well because the vout might have to be restarted */
386 var_Create( p_playlist, "video-on-top", VLC_VAR_BOOL );
387 var_Set( p_playlist, "video-on-top", newval );
389 vlc_object_release( p_playlist );
394 static int FullscreenCallback( vlc_object_t *p_this, char const *psz_cmd,
395 vlc_value_t oldval, vlc_value_t newval, void *p_data )
397 vout_thread_t *p_vout = (vout_thread_t *)p_this;
398 playlist_t *p_playlist;
401 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
403 p_playlist = (playlist_t *)vlc_object_find( p_this, VLC_OBJECT_PLAYLIST,
407 /* Modify playlist as well because the vout might have to be restarted */
408 var_Create( p_playlist, "fullscreen", VLC_VAR_BOOL );
409 var_Set( p_playlist, "fullscreen", newval );
411 vlc_object_release( p_playlist );
414 /* Disable "always on top" in fullscreen mode */
415 var_Get( p_vout, "video-on-top", &val );
416 if( newval.b_bool && val.b_bool )
418 val.b_bool = VLC_FALSE;
419 vout_Control( p_vout, VOUT_SET_STAY_ON_TOP, val.b_bool );
421 else if( !newval.b_bool && val.b_bool )
423 vout_Control( p_vout, VOUT_SET_STAY_ON_TOP, val.b_bool );
426 val.b_bool = VLC_TRUE;
427 var_Set( p_vout, "intf-change", val );
431 static int SnapshotCallback( vlc_object_t *p_this, char const *psz_cmd,
432 vlc_value_t oldval, vlc_value_t newval, void *p_data )
434 vout_thread_t *p_vout = (vout_thread_t *)p_this;
435 vout_Control( p_vout, VOUT_SNAPSHOT );