1 /*****************************************************************************
2 * vout_intf.c : video output interface
3 *****************************************************************************
4 * Copyright (C) 2000-2004 the VideoLAN team
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 CropCallback( vlc_object_t *, char const *,
45 vlc_value_t, vlc_value_t, void * );
46 static int OnTopCallback( vlc_object_t *, char const *,
47 vlc_value_t, vlc_value_t, void * );
48 static int FullscreenCallback( vlc_object_t *, char const *,
49 vlc_value_t, vlc_value_t, void * );
50 static int SnapshotCallback( vlc_object_t *, char const *,
51 vlc_value_t, vlc_value_t, void * );
53 /*****************************************************************************
54 * vout_RequestWindow: Create/Get a video window if possible.
55 *****************************************************************************
56 * This function looks for the main interface and tries to request
57 * a new video window. If it fails then the vout will still need to create the
59 *****************************************************************************/
60 void *vout_RequestWindow( vout_thread_t *p_vout,
61 int *pi_x_hint, int *pi_y_hint,
62 unsigned int *pi_width_hint,
63 unsigned int *pi_height_hint )
65 intf_thread_t *p_intf = NULL;
72 if( !var_Type( p_vout, "aspect-ratio" ) ) vout_IntfInit( p_vout );
74 /* Get requested coordinates */
75 var_Get( p_vout, "video-x", &val );
76 *pi_x_hint = val.i_int ;
77 var_Get( p_vout, "video-y", &val );
78 *pi_y_hint = val.i_int;
80 *pi_width_hint = p_vout->i_window_width;
81 *pi_height_hint = p_vout->i_window_height;
83 /* Check whether someone provided us with a window ID */
84 var_Get( p_vout->p_vlc, "drawable", &val );
85 if( val.i_int ) return (void *)val.i_int;
87 /* Find if the main interface supports embedding */
88 p_list = vlc_list_find( p_vout, VLC_OBJECT_INTF, FIND_ANYWHERE );
89 if( !p_list ) return NULL;
91 for( i = 0; i < p_list->i_count; i++ )
93 p_intf = (intf_thread_t *)p_list->p_values[i].p_object;
94 if( p_intf->b_block && p_intf->pf_request_window ) break;
100 vlc_list_release( p_list );
104 vlc_object_yield( p_intf );
105 vlc_list_release( p_list );
107 p_window = p_intf->pf_request_window( p_intf, p_vout, pi_x_hint, pi_y_hint,
108 pi_width_hint, pi_height_hint );
110 if( !p_window ) vlc_object_release( p_intf );
111 else p_vout->p_parent_intf = p_intf;
116 void vout_ReleaseWindow( vout_thread_t *p_vout, void *p_window )
118 intf_thread_t *p_intf = p_vout->p_parent_intf;
120 if( !p_intf ) return;
122 vlc_mutex_lock( &p_intf->object_lock );
125 vlc_mutex_unlock( &p_intf->object_lock );
129 if( !p_intf->pf_release_window )
131 msg_Err( p_vout, "no pf_release_window");
132 vlc_mutex_unlock( &p_intf->object_lock );
133 vlc_object_release( p_intf );
137 p_intf->pf_release_window( p_intf, p_window );
139 p_vout->p_parent_intf = NULL;
140 vlc_mutex_unlock( &p_intf->object_lock );
141 vlc_object_release( p_intf );
144 int vout_ControlWindow( vout_thread_t *p_vout, void *p_window,
145 int i_query, va_list args )
147 intf_thread_t *p_intf = p_vout->p_parent_intf;
150 if( !p_intf ) return VLC_EGENERIC;
152 vlc_mutex_lock( &p_intf->object_lock );
155 vlc_mutex_unlock( &p_intf->object_lock );
159 if( !p_intf->pf_control_window )
161 msg_Err( p_vout, "no pf_control_window");
162 vlc_mutex_unlock( &p_intf->object_lock );
166 i_ret = p_intf->pf_control_window( p_intf, p_window, i_query, args );
167 vlc_mutex_unlock( &p_intf->object_lock );
171 /*****************************************************************************
172 * vout_IntfInit: called during the vout creation to initialise misc things.
173 *****************************************************************************/
174 void vout_IntfInit( vout_thread_t *p_vout )
176 vlc_value_t val, text, old_val;
178 /* Create a few object variables we'll need later on */
179 var_Create( p_vout, "snapshot-path", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
180 var_Create( p_vout, "snapshot-format", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
181 var_Create( p_vout, "aspect-ratio", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
182 var_Create( p_vout, "monitor-aspect-ratio",
183 VLC_VAR_STRING | VLC_VAR_DOINHERIT );
184 var_Create( p_vout, "width", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
185 var_Create( p_vout, "height", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
186 var_Create( p_vout, "align", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
187 var_Create( p_vout, "video-x", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
188 var_Create( p_vout, "video-y", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
190 var_Create( p_vout, "zoom", VLC_VAR_FLOAT | VLC_VAR_ISCOMMAND |
191 VLC_VAR_HASCHOICE | VLC_VAR_DOINHERIT );
193 text.psz_string = _("Zoom");
194 var_Change( p_vout, "zoom", VLC_VAR_SETTEXT, &text, NULL );
196 var_Get( p_vout, "zoom", &old_val );
197 if( old_val.f_float == 0.25 ||
198 old_val.f_float == 0.5 ||
199 old_val.f_float == 1 ||
200 old_val.f_float == 2 )
202 var_Change( p_vout, "zoom", VLC_VAR_DELCHOICE, &old_val, NULL );
205 val.f_float = 0.25; text.psz_string = _("1:4 Quarter");
206 var_Change( p_vout, "zoom", VLC_VAR_ADDCHOICE, &val, &text );
207 val.f_float = 0.5; text.psz_string = _("1:2 Half");
208 var_Change( p_vout, "zoom", VLC_VAR_ADDCHOICE, &val, &text );
209 val.f_float = 1; text.psz_string = _("1:1 Original");
210 var_Change( p_vout, "zoom", VLC_VAR_ADDCHOICE, &val, &text );
211 val.f_float = 2; text.psz_string = _("2:1 Double");
212 var_Change( p_vout, "zoom", VLC_VAR_ADDCHOICE, &val, &text );
214 var_Set( p_vout, "zoom", old_val );
216 var_AddCallback( p_vout, "zoom", ZoomCallback, NULL );
218 var_Create( p_vout, "crop", VLC_VAR_STRING | VLC_VAR_ISCOMMAND |
219 VLC_VAR_HASCHOICE | VLC_VAR_DOINHERIT );
221 text.psz_string = _("Crop");
222 var_Change( p_vout, "crop", VLC_VAR_SETTEXT, &text, NULL );
225 var_Change( p_vout, "crop", VLC_VAR_DELCHOICE, &val, 0 );
226 val.psz_string = ""; text.psz_string = _("Default");
227 var_Change( p_vout, "crop", VLC_VAR_ADDCHOICE, &val, &text );
228 val.psz_string = "001:1"; text.psz_string = _("1:1");
229 var_Change( p_vout, "crop", VLC_VAR_ADDCHOICE, &val, &text );
230 val.psz_string = "004:3"; text.psz_string = _("4:3");
231 var_Change( p_vout, "crop", VLC_VAR_ADDCHOICE, &val, &text );
232 val.psz_string = "16:9"; text.psz_string = _("16:9");
233 var_Change( p_vout, "crop", VLC_VAR_ADDCHOICE, &val, &text );
234 val.psz_string = "221:100"; text.psz_string = _("221:100");
235 var_Change( p_vout, "crop", VLC_VAR_ADDCHOICE, &val, &text );
237 var_AddCallback( p_vout, "crop", CropCallback, NULL );
239 /* Add a variable to indicate if the window should be on top of others */
240 var_Create( p_vout, "video-on-top", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
241 text.psz_string = _("Always on top");
242 var_Change( p_vout, "video-on-top", VLC_VAR_SETTEXT, &text, NULL );
243 var_AddCallback( p_vout, "video-on-top", OnTopCallback, NULL );
245 /* Add a variable to indicate whether we want window decoration or not */
246 var_Create( p_vout, "video-deco", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
248 /* Add a fullscreen variable */
249 var_Create( p_vout, "fullscreen", VLC_VAR_BOOL );
250 text.psz_string = _("Fullscreen");
251 var_Change( p_vout, "fullscreen", VLC_VAR_SETTEXT, &text, NULL );
252 var_Change( p_vout, "fullscreen", VLC_VAR_INHERITVALUE, &val, NULL );
255 /* user requested fullscreen */
256 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
258 var_AddCallback( p_vout, "fullscreen", FullscreenCallback, NULL );
260 /* Add a snapshot variable */
261 var_Create( p_vout, "video-snapshot", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
262 text.psz_string = _("Snapshot");
263 var_Change( p_vout, "video-snapshot", VLC_VAR_SETTEXT, &text, NULL );
264 var_AddCallback( p_vout, "video-snapshot", SnapshotCallback, NULL );
266 /* Mouse coordinates */
267 var_Create( p_vout, "mouse-x", VLC_VAR_INTEGER );
268 var_Create( p_vout, "mouse-y", VLC_VAR_INTEGER );
269 var_Create( p_vout, "mouse-button-down", VLC_VAR_INTEGER );
270 var_Create( p_vout, "mouse-moved", VLC_VAR_BOOL );
271 var_Create( p_vout, "mouse-clicked", VLC_VAR_INTEGER );
273 var_Create( p_vout, "intf-change", VLC_VAR_BOOL );
274 val.b_bool = VLC_TRUE;
275 var_Set( p_vout, "intf-change", val );
278 /*****************************************************************************
279 * vout_Snapshot: generates a snapshot.
280 *****************************************************************************/
281 int vout_Snapshot( vout_thread_t *p_vout, picture_t *p_pic )
283 image_handler_t *p_image = image_HandlerCreate( p_vout );
284 video_format_t fmt_in = {0}, fmt_out = {0};
286 subpicture_t *p_subpic;
288 vlc_value_t val, format;
291 var_Get( p_vout, "snapshot-path", &val );
292 if( val.psz_string && !*val.psz_string )
294 free( val.psz_string );
298 #if defined(SYS_DARWIN) || defined(SYS_BEOS)
299 if( !val.psz_string && p_vout->p_vlc->psz_homedir )
301 asprintf( &val.psz_string, "%s/Desktop",
302 p_vout->p_vlc->psz_homedir );
305 #elif defined(WIN32) && !defined(UNDER_CE)
306 if( !val.psz_string && p_vout->p_vlc->psz_homedir )
308 /* Get the My Pictures folder path */
310 char *p_mypicturesdir = NULL;
311 typedef HRESULT (WINAPI *SHGETFOLDERPATH)( HWND, int, HANDLE, DWORD,
313 #ifndef CSIDL_FLAG_CREATE
314 # define CSIDL_FLAG_CREATE 0x8000
316 #ifndef CSIDL_MYPICTURES
317 # define CSIDL_MYPICTURES 0x27
319 #ifndef SHGFP_TYPE_CURRENT
320 # define SHGFP_TYPE_CURRENT 0
323 HINSTANCE shfolder_dll;
324 SHGETFOLDERPATH SHGetFolderPath ;
326 /* load the shfolder dll to retrieve SHGetFolderPath */
327 if( ( shfolder_dll = LoadLibrary( _T("SHFolder.dll") ) ) != NULL )
329 SHGetFolderPath = (void *)GetProcAddress( shfolder_dll,
330 _T("SHGetFolderPathA") );
331 if( SHGetFolderPath != NULL )
333 p_mypicturesdir = (char *)malloc( MAX_PATH );
334 if( p_mypicturesdir )
337 if( S_OK != SHGetFolderPath( NULL,
338 CSIDL_MYPICTURES | CSIDL_FLAG_CREATE,
339 NULL, SHGFP_TYPE_CURRENT,
342 free( p_mypicturesdir );
343 p_mypicturesdir = NULL;
347 FreeLibrary( shfolder_dll );
350 if( p_mypicturesdir == NULL )
352 asprintf( &val.psz_string, "%s/" CONFIG_DIR,
353 p_vout->p_vlc->psz_homedir );
357 asprintf( &val.psz_string, p_mypicturesdir );
358 free( p_mypicturesdir );
363 if( !val.psz_string && p_vout->p_vlc->psz_homedir )
365 asprintf( &val.psz_string, "%s/" CONFIG_DIR,
366 p_vout->p_vlc->psz_homedir );
370 if( !val.psz_string )
372 msg_Err( p_vout, "no directory specified for snapshots" );
375 var_Get( p_vout, "snapshot-format", &format );
376 if( !format.psz_string || !*format.psz_string )
378 if( format.psz_string ) free( format.psz_string );
379 format.psz_string = strdup( "png" );
382 asprintf( &psz_filename, "%s/vlcsnap-%u.%s", val.psz_string,
383 (unsigned int)(p_pic->date / 100000) & 0xFFFFFF,
385 free( val.psz_string );
386 free( format.psz_string );
388 /* Save the snapshot */
389 fmt_in = p_vout->fmt_in;
390 fmt_out.i_sar_num = fmt_out.i_sar_den = 1;
391 i_ret = image_WriteUrl( p_image, p_pic, &fmt_in, &fmt_out, psz_filename );
392 if( i_ret != VLC_SUCCESS )
394 msg_Err( p_vout, "could not create snapshot %s", psz_filename );
395 free( psz_filename );
396 image_HandlerDelete( p_image );
400 msg_Dbg( p_vout, "snapshot taken (%s)", psz_filename );
401 free( psz_filename );
403 /* Inject a subpicture with the snapshot */
404 memset( &fmt_out, 0, sizeof(fmt_out) );
405 fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
406 p_pif = image_Convert( p_image, p_pic, &fmt_in, &fmt_out );
407 image_HandlerDelete( p_image );
408 if( !p_pif ) return VLC_EGENERIC;
410 p_subpic = spu_CreateSubpicture( p_vout->p_spu );
411 if( p_subpic == NULL )
413 p_pif->pf_release( p_pif );
417 p_subpic->i_channel = 0;
418 p_subpic->i_start = mdate();
419 p_subpic->i_stop = mdate() + 4000000;
420 p_subpic->b_ephemer = VLC_TRUE;
421 p_subpic->b_fade = VLC_TRUE;
422 p_subpic->i_original_picture_width = p_vout->render.i_width * 4;
423 p_subpic->i_original_picture_height = p_vout->render.i_height * 4;
425 p_subpic->p_region = spu_CreateRegion( p_vout->p_spu, &fmt_out );
426 vout_CopyPicture( p_image->p_parent, &p_subpic->p_region->picture, p_pif );
427 p_pif->pf_release( p_pif );
429 spu_DisplaySubpicture( p_vout->p_spu, p_subpic );
434 /*****************************************************************************
435 * vout_ControlDefault: default methods for video output control.
436 *****************************************************************************/
437 int vout_vaControlDefault( vout_thread_t *p_vout, int i_query, va_list args )
443 if( p_vout->p_parent_intf )
445 vlc_object_release( p_vout->p_parent_intf );
446 p_vout->p_parent_intf = NULL;
452 p_vout->b_snapshot = VLC_TRUE;
457 msg_Dbg( p_vout, "control query not supported" );
462 /*****************************************************************************
463 * Object variables callbacks
464 *****************************************************************************/
465 static int ZoomCallback( vlc_object_t *p_this, char const *psz_cmd,
466 vlc_value_t oldval, vlc_value_t newval, void *p_data )
468 vout_thread_t *p_vout = (vout_thread_t *)p_this;
469 vout_Control( p_vout, VOUT_SET_ZOOM, newval.f_float );
473 static int CropCallback( vlc_object_t *p_this, char const *psz_cmd,
474 vlc_value_t oldval, vlc_value_t newval, void *p_data )
476 vout_thread_t *p_vout = (vout_thread_t *)p_this;
477 int64_t i_aspect_num, i_aspect_den;
478 unsigned int i_width, i_height;
480 char *psz_end, *psz_parser = strchr( newval.psz_string, ':' );
482 /* Restore defaults */
483 p_vout->fmt_in.i_x_offset = p_vout->fmt_render.i_x_offset;
484 p_vout->fmt_in.i_visible_width = p_vout->fmt_render.i_visible_width;
485 p_vout->fmt_in.i_y_offset = p_vout->fmt_render.i_y_offset;
486 p_vout->fmt_in.i_visible_height = p_vout->fmt_render.i_visible_height;
488 if( !psz_parser ) goto crop_end;
490 i_aspect_num = strtol( newval.psz_string, &psz_end, 0 );
491 if( psz_end == newval.psz_string ) goto crop_end;
493 i_aspect_den = strtol( ++psz_parser, &psz_end, 0 );
494 if( psz_end == psz_parser ) goto crop_end;
496 i_width = p_vout->fmt_in.i_sar_den * p_vout->fmt_render.i_visible_height *
497 i_aspect_num / i_aspect_den / p_vout->fmt_in.i_sar_num;
498 i_height = p_vout->fmt_render.i_visible_width * p_vout->fmt_in.i_sar_num *
499 i_aspect_den / i_aspect_num / p_vout->fmt_in.i_sar_den;
501 if( i_width < p_vout->fmt_render.i_visible_width )
503 p_vout->fmt_in.i_x_offset = p_vout->fmt_render.i_x_offset +
504 (p_vout->fmt_render.i_visible_width - i_width) / 2;
505 p_vout->fmt_in.i_visible_width = i_width;
509 p_vout->fmt_in.i_y_offset = p_vout->fmt_render.i_y_offset +
510 (p_vout->fmt_render.i_visible_height - i_height) / 2;
511 p_vout->fmt_in.i_visible_height = i_height;
515 msg_Dbg( p_vout, "cropping picture %ix%i to %i,%i,%ix%i",
516 p_vout->fmt_in.i_width, p_vout->fmt_in.i_height,
517 p_vout->fmt_in.i_x_offset, p_vout->fmt_in.i_y_offset,
518 p_vout->fmt_in.i_visible_width,
519 p_vout->fmt_in.i_visible_height );
524 static int OnTopCallback( vlc_object_t *p_this, char const *psz_cmd,
525 vlc_value_t oldval, vlc_value_t newval, void *p_data )
527 vout_thread_t *p_vout = (vout_thread_t *)p_this;
528 playlist_t *p_playlist;
529 vout_Control( p_vout, VOUT_SET_STAY_ON_TOP, newval.b_bool );
531 p_playlist = (playlist_t *)vlc_object_find( p_this, VLC_OBJECT_PLAYLIST,
535 /* Modify playlist as well because the vout might have to be restarted */
536 var_Create( p_playlist, "video-on-top", VLC_VAR_BOOL );
537 var_Set( p_playlist, "video-on-top", newval );
539 vlc_object_release( p_playlist );
544 static int FullscreenCallback( vlc_object_t *p_this, char const *psz_cmd,
545 vlc_value_t oldval, vlc_value_t newval, void *p_data )
547 vout_thread_t *p_vout = (vout_thread_t *)p_this;
548 playlist_t *p_playlist;
551 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
553 p_playlist = (playlist_t *)vlc_object_find( p_this, VLC_OBJECT_PLAYLIST,
557 /* Modify playlist as well because the vout might have to be restarted */
558 var_Create( p_playlist, "fullscreen", VLC_VAR_BOOL );
559 var_Set( p_playlist, "fullscreen", newval );
561 vlc_object_release( p_playlist );
564 /* Disable "always on top" in fullscreen mode */
565 var_Get( p_vout, "video-on-top", &val );
566 if( newval.b_bool && val.b_bool )
568 val.b_bool = VLC_FALSE;
569 vout_Control( p_vout, VOUT_SET_STAY_ON_TOP, val.b_bool );
571 else if( !newval.b_bool && val.b_bool )
573 vout_Control( p_vout, VOUT_SET_STAY_ON_TOP, val.b_bool );
576 val.b_bool = VLC_TRUE;
577 var_Set( p_vout, "intf-change", val );
581 static int SnapshotCallback( vlc_object_t *p_this, char const *psz_cmd,
582 vlc_value_t oldval, vlc_value_t newval, void *p_data )
584 vout_thread_t *p_vout = (vout_thread_t *)p_this;
585 vout_Control( p_vout, VOUT_SNAPSHOT );