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, "snapshot-format", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
179 var_Create( p_vout, "aspect-ratio", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
180 var_Create( p_vout, "width", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
181 var_Create( p_vout, "height", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
182 var_Create( p_vout, "align", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
183 var_Create( p_vout, "video-x", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
184 var_Create( p_vout, "video-y", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
186 var_Create( p_vout, "zoom", VLC_VAR_FLOAT | VLC_VAR_ISCOMMAND |
187 VLC_VAR_HASCHOICE | VLC_VAR_DOINHERIT );
189 text.psz_string = _("Zoom");
190 var_Change( p_vout, "zoom", VLC_VAR_SETTEXT, &text, NULL );
192 var_Get( p_vout, "zoom", &old_val );
193 if( old_val.f_float == 0.25 ||
194 old_val.f_float == 0.5 ||
195 old_val.f_float == 1 ||
196 old_val.f_float == 2 )
198 var_Change( p_vout, "zoom", VLC_VAR_DELCHOICE, &old_val, NULL );
201 val.f_float = 0.25; text.psz_string = _("1:4 Quarter");
202 var_Change( p_vout, "zoom", VLC_VAR_ADDCHOICE, &val, &text );
203 val.f_float = 0.5; text.psz_string = _("1:2 Half");
204 var_Change( p_vout, "zoom", VLC_VAR_ADDCHOICE, &val, &text );
205 val.f_float = 1; text.psz_string = _("1:1 Original");
206 var_Change( p_vout, "zoom", VLC_VAR_ADDCHOICE, &val, &text );
207 val.f_float = 2; text.psz_string = _("2:1 Double");
208 var_Change( p_vout, "zoom", VLC_VAR_ADDCHOICE, &val, &text );
210 var_Set( p_vout, "zoom", old_val );
212 var_AddCallback( p_vout, "zoom", ZoomCallback, NULL );
214 /* Add a variable to indicate if the window should be on top of others */
215 var_Create( p_vout, "video-on-top", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
216 text.psz_string = _("Always on top");
217 var_Change( p_vout, "video-on-top", VLC_VAR_SETTEXT, &text, NULL );
218 var_AddCallback( p_vout, "video-on-top", OnTopCallback, NULL );
220 /* Add a variable to indicate whether we want window decoration or not */
221 var_Create( p_vout, "video-deco", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
223 /* Add a fullscreen variable */
224 var_Create( p_vout, "fullscreen", VLC_VAR_BOOL );
225 text.psz_string = _("Fullscreen");
226 var_Change( p_vout, "fullscreen", VLC_VAR_SETTEXT, &text, NULL );
227 var_Change( p_vout, "fullscreen", VLC_VAR_INHERITVALUE, &val, NULL );
230 /* user requested fullscreen */
231 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
233 var_AddCallback( p_vout, "fullscreen", FullscreenCallback, NULL );
235 /* Add a snapshot variable */
236 var_Create( p_vout, "video-snapshot", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
237 text.psz_string = _("Snapshot");
238 var_Change( p_vout, "video-snapshot", VLC_VAR_SETTEXT, &text, NULL );
239 var_AddCallback( p_vout, "video-snapshot", SnapshotCallback, NULL );
241 /* Mouse coordinates */
242 var_Create( p_vout, "mouse-x", VLC_VAR_INTEGER );
243 var_Create( p_vout, "mouse-y", VLC_VAR_INTEGER );
244 var_Create( p_vout, "mouse-button-down", VLC_VAR_INTEGER );
245 var_Create( p_vout, "mouse-moved", VLC_VAR_BOOL );
246 var_Create( p_vout, "mouse-clicked", VLC_VAR_INTEGER );
248 var_Create( p_vout, "intf-change", VLC_VAR_BOOL );
249 val.b_bool = VLC_TRUE;
250 var_Set( p_vout, "intf-change", val );
253 /*****************************************************************************
254 * vout_Snapshot: generates a snapshot.
255 *****************************************************************************/
256 int vout_Snapshot( vout_thread_t *p_vout, picture_t *p_pic )
258 image_handler_t *p_image = image_HandlerCreate( p_vout );
259 video_format_t fmt_in = {0}, fmt_out = {0};
261 subpicture_t *p_subpic;
263 vlc_value_t val, format;
266 var_Get( p_vout, "snapshot-path", &val );
267 if( val.psz_string && !*val.psz_string )
269 free( val.psz_string );
273 #if defined(SYS_DARWIN) || defined(SYS_BEOS)
274 if( !val.psz_string && p_vout->p_vlc->psz_homedir )
276 asprintf( &val.psz_string, "%s/Desktop",
277 p_vout->p_vlc->psz_homedir );
280 #elif defined(WIN32) && !defined(UNDER_CE)
281 if( !val.psz_string && p_vout->p_vlc->psz_homedir )
283 /* Get the My Pictures folder path */
285 char *p_mypicturesdir = NULL;
286 typedef HRESULT (WINAPI *SHGETFOLDERPATH)( HWND, int, HANDLE, DWORD,
288 #ifndef CSIDL_FLAG_CREATE
289 # define CSIDL_FLAG_CREATE 0x8000
291 #ifndef CSIDL_MYPICTURES
292 # define CSIDL_MYPICTURES 0x27
294 #ifndef SHGFP_TYPE_CURRENT
295 # define SHGFP_TYPE_CURRENT 0
298 HINSTANCE shfolder_dll;
299 SHGETFOLDERPATH SHGetFolderPath ;
301 /* load the shfolder dll to retrieve SHGetFolderPath */
302 if( ( shfolder_dll = LoadLibrary( _T("SHFolder.dll") ) ) != NULL )
304 SHGetFolderPath = (void *)GetProcAddress( shfolder_dll,
305 _T("SHGetFolderPathA") );
306 if( SHGetFolderPath != NULL )
308 p_mypicturesdir = (char *)malloc( MAX_PATH );
309 if( p_mypicturesdir )
312 if( S_OK != SHGetFolderPath( NULL,
313 CSIDL_MYPICTURES | CSIDL_FLAG_CREATE,
314 NULL, SHGFP_TYPE_CURRENT,
317 free( p_mypicturesdir );
318 p_mypicturesdir = NULL;
322 FreeLibrary( shfolder_dll );
325 if( p_mypicturesdir == NULL )
327 asprintf( &val.psz_string, "%s/" CONFIG_DIR,
328 p_vout->p_vlc->psz_homedir );
332 asprintf( &val.psz_string, p_mypicturesdir );
333 free( p_mypicturesdir );
338 if( !val.psz_string && p_vout->p_vlc->psz_homedir )
340 asprintf( &val.psz_string, "%s/" CONFIG_DIR,
341 p_vout->p_vlc->psz_homedir );
345 if( !val.psz_string )
347 msg_Err( p_vout, "no directory specified for snapshots" );
350 var_Get( p_vout, "snapshot-format", &format );
351 if( !format.psz_string || !*format.psz_string )
353 if( format.psz_string ) free( format.psz_string );
354 format.psz_string = strdup( "png" );
357 asprintf( &psz_filename, "%s/vlcsnap-%u.%s", val.psz_string,
358 (unsigned int)(p_pic->date / 100000) & 0xFFFFFF,
360 free( val.psz_string );
361 free( format.psz_string );
363 /* Save the snapshot */
364 fmt_in = p_vout->fmt_in;
365 fmt_out.i_sar_num = fmt_out.i_sar_den = 1;
366 i_ret = image_WriteUrl( p_image, p_pic, &fmt_in, &fmt_out, psz_filename );
367 if( i_ret != VLC_SUCCESS )
369 msg_Err( p_vout, "could not create snapshot %s", psz_filename );
370 free( psz_filename );
371 image_HandlerDelete( p_image );
375 msg_Dbg( p_vout, "snapshot taken (%s)", psz_filename );
376 free( psz_filename );
378 /* Inject a subpicture with the snapshot */
379 memset( &fmt_out, 0, sizeof(fmt_out) );
380 fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
381 p_pif = image_Convert( p_image, p_pic, &fmt_in, &fmt_out );
382 image_HandlerDelete( p_image );
383 if( !p_pif ) return VLC_EGENERIC;
385 p_subpic = spu_CreateSubpicture( p_vout->p_spu );
386 if( p_subpic == NULL )
388 p_pif->pf_release( p_pif );
392 p_subpic->i_channel = 0;
393 p_subpic->i_start = mdate();
394 p_subpic->i_stop = mdate() + 4000000;
395 p_subpic->b_ephemer = VLC_TRUE;
396 p_subpic->b_fade = VLC_TRUE;
397 p_subpic->i_original_picture_width = p_vout->render.i_width * 4;
398 p_subpic->i_original_picture_height = p_vout->render.i_height * 4;
400 p_subpic->p_region = spu_CreateRegion( p_vout->p_spu, &fmt_out );
401 vout_CopyPicture( p_image->p_parent, &p_subpic->p_region->picture, p_pif );
402 p_pif->pf_release( p_pif );
404 spu_DisplaySubpicture( p_vout->p_spu, p_subpic );
409 /*****************************************************************************
410 * vout_ControlDefault: default methods for video output control.
411 *****************************************************************************/
412 int vout_vaControlDefault( vout_thread_t *p_vout, int i_query, va_list args )
418 if( p_vout->p_parent_intf )
420 vlc_object_release( p_vout->p_parent_intf );
421 p_vout->p_parent_intf = NULL;
427 p_vout->b_snapshot = VLC_TRUE;
432 msg_Dbg( p_vout, "control query not supported" );
437 /*****************************************************************************
438 * Object variables callbacks
439 *****************************************************************************/
440 static int ZoomCallback( vlc_object_t *p_this, char const *psz_cmd,
441 vlc_value_t oldval, vlc_value_t newval, void *p_data )
443 vout_thread_t *p_vout = (vout_thread_t *)p_this;
444 vout_Control( p_vout, VOUT_SET_ZOOM, newval.f_float );
448 static int OnTopCallback( vlc_object_t *p_this, char const *psz_cmd,
449 vlc_value_t oldval, vlc_value_t newval, void *p_data )
451 vout_thread_t *p_vout = (vout_thread_t *)p_this;
452 playlist_t *p_playlist;
453 vout_Control( p_vout, VOUT_SET_STAY_ON_TOP, newval.b_bool );
455 p_playlist = (playlist_t *)vlc_object_find( p_this, VLC_OBJECT_PLAYLIST,
459 /* Modify playlist as well because the vout might have to be restarted */
460 var_Create( p_playlist, "video-on-top", VLC_VAR_BOOL );
461 var_Set( p_playlist, "video-on-top", newval );
463 vlc_object_release( p_playlist );
468 static int FullscreenCallback( vlc_object_t *p_this, char const *psz_cmd,
469 vlc_value_t oldval, vlc_value_t newval, void *p_data )
471 vout_thread_t *p_vout = (vout_thread_t *)p_this;
472 playlist_t *p_playlist;
475 p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
477 p_playlist = (playlist_t *)vlc_object_find( p_this, VLC_OBJECT_PLAYLIST,
481 /* Modify playlist as well because the vout might have to be restarted */
482 var_Create( p_playlist, "fullscreen", VLC_VAR_BOOL );
483 var_Set( p_playlist, "fullscreen", newval );
485 vlc_object_release( p_playlist );
488 /* Disable "always on top" in fullscreen mode */
489 var_Get( p_vout, "video-on-top", &val );
490 if( newval.b_bool && val.b_bool )
492 val.b_bool = VLC_FALSE;
493 vout_Control( p_vout, VOUT_SET_STAY_ON_TOP, val.b_bool );
495 else if( !newval.b_bool && val.b_bool )
497 vout_Control( p_vout, VOUT_SET_STAY_ON_TOP, val.b_bool );
500 val.b_bool = VLC_TRUE;
501 var_Set( p_vout, "intf-change", val );
505 static int SnapshotCallback( vlc_object_t *p_this, char const *psz_cmd,
506 vlc_value_t oldval, vlc_value_t newval, void *p_data )
508 vout_thread_t *p_vout = (vout_thread_t *)p_this;
509 vout_Control( p_vout, VOUT_SNAPSHOT );