]> git.sesse.net Git - vlc/blob - src/video_output/vout_intf.c
backport [13049] and [13059]
[vlc] / src / video_output / vout_intf.c
1 /*****************************************************************************
2  * vout_intf.c : video output interface
3  *****************************************************************************
4  * Copyright (C) 2000-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@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>                                                /* free() */
28
29 #include <vlc/vlc.h>
30 #include <vlc/intf.h>
31
32 #include "vlc_video.h"
33 #include "video_output.h"
34 #include "vlc_image.h"
35 #include "vlc_spu.h"
36
37 /*****************************************************************************
38  * Local prototypes
39  *****************************************************************************/
40 static void InitWindowSize( vout_thread_t *, unsigned *, unsigned * );
41
42 /* Object variables callbacks */
43 static int ZoomCallback( vlc_object_t *, char const *,
44                          vlc_value_t, vlc_value_t, void * );
45 static int CropCallback( vlc_object_t *, char const *,
46                          vlc_value_t, vlc_value_t, void * );
47 static int AspectCallback( vlc_object_t *, char const *,
48                            vlc_value_t, vlc_value_t, void * );
49 static int OnTopCallback( vlc_object_t *, char const *,
50                           vlc_value_t, vlc_value_t, void * );
51 static int FullscreenCallback( vlc_object_t *, char const *,
52                                vlc_value_t, vlc_value_t, void * );
53 static int SnapshotCallback( vlc_object_t *, char const *,
54                              vlc_value_t, vlc_value_t, void * );
55
56 /*****************************************************************************
57  * vout_RequestWindow: Create/Get a video window if possible.
58  *****************************************************************************
59  * This function looks for the main interface and tries to request
60  * a new video window. If it fails then the vout will still need to create the
61  * window by itself.
62  *****************************************************************************/
63 void *vout_RequestWindow( vout_thread_t *p_vout,
64                           int *pi_x_hint, int *pi_y_hint,
65                           unsigned int *pi_width_hint,
66                           unsigned int *pi_height_hint )
67 {
68     intf_thread_t *p_intf = NULL;
69     vlc_list_t *p_list;
70     void *p_window;
71     vlc_value_t val;
72     int i;
73
74     /* Small kludge */
75     if( !var_Type( p_vout, "aspect-ratio" ) ) vout_IntfInit( p_vout );
76
77     /* Get requested coordinates */
78     var_Get( p_vout, "video-x", &val );
79     *pi_x_hint = val.i_int ;
80     var_Get( p_vout, "video-y", &val );
81     *pi_y_hint = val.i_int;
82
83     *pi_width_hint = p_vout->i_window_width;
84     *pi_height_hint = p_vout->i_window_height;
85
86     /* Check whether someone provided us with a window ID */
87     var_Get( p_vout->p_vlc, "drawable", &val );
88     if( val.i_int ) return (void *)val.i_int;
89
90     /* Find if the main interface supports embedding */
91     p_list = vlc_list_find( p_vout, VLC_OBJECT_INTF, FIND_ANYWHERE );
92     if( !p_list ) return NULL;
93
94     for( i = 0; i < p_list->i_count; i++ )
95     {
96         p_intf = (intf_thread_t *)p_list->p_values[i].p_object;
97         if( p_intf->b_block && p_intf->pf_request_window ) break;
98         p_intf = NULL;
99     }
100
101     if( !p_intf )
102     {
103         vlc_list_release( p_list );
104         return NULL;
105     }
106
107     vlc_object_yield( p_intf );
108     vlc_list_release( p_list );
109
110     p_window = p_intf->pf_request_window( p_intf, p_vout, pi_x_hint, pi_y_hint,
111                                           pi_width_hint, pi_height_hint );
112
113     if( !p_window ) vlc_object_release( p_intf );
114     else p_vout->p_parent_intf = p_intf;
115
116     return p_window;
117 }
118
119 void vout_ReleaseWindow( vout_thread_t *p_vout, void *p_window )
120 {
121     intf_thread_t *p_intf = p_vout->p_parent_intf;
122
123     if( !p_intf ) return;
124
125     vlc_mutex_lock( &p_intf->object_lock );
126     if( p_intf->b_dead )
127     {
128         vlc_mutex_unlock( &p_intf->object_lock );
129         return;
130     }
131
132     if( !p_intf->pf_release_window )
133     {
134         msg_Err( p_vout, "no pf_release_window");
135         vlc_mutex_unlock( &p_intf->object_lock );
136         vlc_object_release( p_intf );
137         return;
138     }
139
140     p_intf->pf_release_window( p_intf, p_window );
141
142     p_vout->p_parent_intf = NULL;
143     vlc_mutex_unlock( &p_intf->object_lock );
144     vlc_object_release( p_intf );
145 }
146
147 int vout_ControlWindow( vout_thread_t *p_vout, void *p_window,
148                         int i_query, va_list args )
149 {
150     intf_thread_t *p_intf = p_vout->p_parent_intf;
151     int i_ret;
152
153     if( !p_intf ) return VLC_EGENERIC;
154
155     vlc_mutex_lock( &p_intf->object_lock );
156     if( p_intf->b_dead )
157     {
158         vlc_mutex_unlock( &p_intf->object_lock );
159         return VLC_EGENERIC;
160     }
161
162     if( !p_intf->pf_control_window )
163     {
164         msg_Err( p_vout, "no pf_control_window");
165         vlc_mutex_unlock( &p_intf->object_lock );
166         return VLC_EGENERIC;
167     }
168
169     i_ret = p_intf->pf_control_window( p_intf, p_window, i_query, args );
170     vlc_mutex_unlock( &p_intf->object_lock );
171     return i_ret;
172 }
173
174 /*****************************************************************************
175  * vout_IntfInit: called during the vout creation to initialise misc things.
176  *****************************************************************************/
177 void vout_IntfInit( vout_thread_t *p_vout )
178 {
179     vlc_value_t val, text, old_val;
180     vlc_bool_t b_force_par = VLC_FALSE;
181
182     /* Create a few object variables we'll need later on */
183     var_Create( p_vout, "snapshot-path", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
184     var_Create( p_vout, "snapshot-format", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
185     var_Create( p_vout, "width", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
186     var_Create( p_vout, "height", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
187     var_Create( p_vout, "align", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
188     var_Get( p_vout, "align", &val );
189     p_vout->i_alignment = val.i_int;
190
191     var_Create( p_vout, "video-x", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
192     var_Create( p_vout, "video-y", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
193
194     /* Zoom object var */
195     var_Create( p_vout, "zoom", VLC_VAR_FLOAT | VLC_VAR_ISCOMMAND |
196                 VLC_VAR_HASCHOICE | VLC_VAR_DOINHERIT );
197
198     text.psz_string = _("Zoom");
199     var_Change( p_vout, "zoom", VLC_VAR_SETTEXT, &text, NULL );
200
201     var_Get( p_vout, "zoom", &old_val );
202     if( old_val.f_float == 0.25 ||
203         old_val.f_float == 0.5 ||
204         old_val.f_float == 1 ||
205         old_val.f_float == 2 )
206     {
207         var_Change( p_vout, "zoom", VLC_VAR_DELCHOICE, &old_val, NULL );
208     }
209
210     val.f_float = 0.25; text.psz_string = _("1:4 Quarter");
211     var_Change( p_vout, "zoom", VLC_VAR_ADDCHOICE, &val, &text );
212     val.f_float = 0.5; text.psz_string = _("1:2 Half");
213     var_Change( p_vout, "zoom", VLC_VAR_ADDCHOICE, &val, &text );
214     val.f_float = 1; text.psz_string = _("1:1 Original");
215     var_Change( p_vout, "zoom", VLC_VAR_ADDCHOICE, &val, &text );
216     val.f_float = 2; text.psz_string = _("2:1 Double");
217     var_Change( p_vout, "zoom", VLC_VAR_ADDCHOICE, &val, &text );
218
219     var_Set( p_vout, "zoom", old_val );
220
221     var_AddCallback( p_vout, "zoom", ZoomCallback, NULL );
222
223     /* Crop object var */
224     var_Create( p_vout, "crop", VLC_VAR_STRING |
225                 VLC_VAR_HASCHOICE | VLC_VAR_DOINHERIT );
226
227     text.psz_string = _("Crop");
228     var_Change( p_vout, "crop", VLC_VAR_SETTEXT, &text, NULL );
229
230     val.psz_string = "";
231     var_Change( p_vout, "crop", VLC_VAR_DELCHOICE, &val, 0 );
232     val.psz_string = ""; text.psz_string = _("Default");
233     var_Change( p_vout, "crop", VLC_VAR_ADDCHOICE, &val, &text );
234     val.psz_string = "001:1"; text.psz_string = _("1:1");
235     var_Change( p_vout, "crop", VLC_VAR_ADDCHOICE, &val, &text );
236     val.psz_string = "004:3"; text.psz_string = _("4:3");
237     var_Change( p_vout, "crop", VLC_VAR_ADDCHOICE, &val, &text );
238     val.psz_string = "16:9"; text.psz_string = _("16:9");
239     var_Change( p_vout, "crop", VLC_VAR_ADDCHOICE, &val, &text );
240     val.psz_string = "221:100"; text.psz_string = _("221:100");
241     var_Change( p_vout, "crop", VLC_VAR_ADDCHOICE, &val, &text );
242
243     var_AddCallback( p_vout, "crop", CropCallback, NULL );
244     var_Get( p_vout, "crop", &old_val );
245     if( old_val.psz_string && *old_val.psz_string )
246         var_Change( p_vout, "crop", VLC_VAR_TRIGGER_CALLBACKS, 0, 0 );
247     if( old_val.psz_string ) free( old_val.psz_string );
248
249     /* Monitor pixel aspect-ratio */
250     var_Create( p_vout, "monitor-par", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
251     var_Get( p_vout, "monitor-par", &val );
252     if( val.psz_string && *val.psz_string )
253     {
254         char *psz_parser = strchr( val.psz_string, ':' );
255         unsigned int i_aspect_num = 0, i_aspect_den = 0;
256         float i_aspect = 0;
257         if( psz_parser )
258         {
259             i_aspect_num = strtol( val.psz_string, 0, 0 );
260             i_aspect_den = strtol( ++psz_parser, 0, 0 );
261         }
262         else
263         {
264             i_aspect = atof( val.psz_string );
265             vlc_ureduce( &i_aspect_num, &i_aspect_den,
266                          i_aspect *VOUT_ASPECT_FACTOR, VOUT_ASPECT_FACTOR, 0 );
267         }
268         if( !i_aspect_num || !i_aspect_den ) i_aspect_num = i_aspect_den = 1;
269
270         p_vout->i_par_num = i_aspect_num;
271         p_vout->i_par_den = i_aspect_den;
272
273         vlc_ureduce( &p_vout->i_par_num, &p_vout->i_par_den,
274                      p_vout->i_par_num, p_vout->i_par_den, 0 );
275
276         msg_Dbg( p_vout, "monitor pixel aspect-ratio overriding: %i:%i",
277                  p_vout->i_par_num, p_vout->i_par_den );
278         b_force_par = VLC_TRUE;
279     }
280     if( val.psz_string ) free( val.psz_string );
281
282     /* Aspect-ratio object var */
283     var_Create( p_vout, "aspect-ratio", VLC_VAR_STRING |
284                 VLC_VAR_HASCHOICE | VLC_VAR_DOINHERIT );
285
286     text.psz_string = _("Aspect-ratio");
287     var_Change( p_vout, "aspect-ratio", VLC_VAR_SETTEXT, &text, NULL );
288
289     val.psz_string = "";
290     var_Change( p_vout, "aspect-ratio", VLC_VAR_DELCHOICE, &val, 0 );
291     val.psz_string = ""; text.psz_string = _("Default");
292     var_Change( p_vout, "aspect-ratio", VLC_VAR_ADDCHOICE, &val, &text );
293     val.psz_string = "001:1"; text.psz_string = _("1:1");
294     var_Change( p_vout, "aspect-ratio", VLC_VAR_ADDCHOICE, &val, &text );
295     val.psz_string = "004:3"; text.psz_string = _("4:3");
296     var_Change( p_vout, "aspect-ratio", VLC_VAR_ADDCHOICE, &val, &text );
297     val.psz_string = "16:9"; text.psz_string = _("16:9");
298     var_Change( p_vout, "aspect-ratio", VLC_VAR_ADDCHOICE, &val, &text );
299     val.psz_string = "221:100"; text.psz_string = _("221:100");
300     var_Change( p_vout, "aspect-ratio", VLC_VAR_ADDCHOICE, &val, &text );
301
302     var_AddCallback( p_vout, "aspect-ratio", AspectCallback, NULL );
303     var_Get( p_vout, "aspect-ratio", &old_val );
304     if( (old_val.psz_string && *old_val.psz_string) || b_force_par )
305         var_Change( p_vout, "aspect-ratio", VLC_VAR_TRIGGER_CALLBACKS, 0, 0 );
306     if( old_val.psz_string ) free( old_val.psz_string );
307
308     /* Initialize the dimensions of the video window */
309     InitWindowSize( p_vout, &p_vout->i_window_width,
310                     &p_vout->i_window_height );
311
312     /* Add a variable to indicate if the window should be on top of others */
313     var_Create( p_vout, "video-on-top", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
314     text.psz_string = _("Always on top");
315     var_Change( p_vout, "video-on-top", VLC_VAR_SETTEXT, &text, NULL );
316     var_AddCallback( p_vout, "video-on-top", OnTopCallback, NULL );
317
318     /* Add a variable to indicate whether we want window decoration or not */
319     var_Create( p_vout, "video-deco", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
320
321     /* Add a fullscreen variable */
322     var_Create( p_vout, "fullscreen", VLC_VAR_BOOL );
323     text.psz_string = _("Fullscreen");
324     var_Change( p_vout, "fullscreen", VLC_VAR_SETTEXT, &text, NULL );
325     var_Change( p_vout, "fullscreen", VLC_VAR_INHERITVALUE, &val, NULL );
326     if( val.b_bool )
327     {
328         /* user requested fullscreen */
329         p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
330     }
331     var_AddCallback( p_vout, "fullscreen", FullscreenCallback, NULL );
332
333     /* Add a snapshot variable */
334     var_Create( p_vout, "video-snapshot", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
335     text.psz_string = _("Snapshot");
336     var_Change( p_vout, "video-snapshot", VLC_VAR_SETTEXT, &text, NULL );
337     var_AddCallback( p_vout, "video-snapshot", SnapshotCallback, NULL );
338
339     /* Mouse coordinates */
340     var_Create( p_vout, "mouse-x", VLC_VAR_INTEGER );
341     var_Create( p_vout, "mouse-y", VLC_VAR_INTEGER );
342     var_Create( p_vout, "mouse-button-down", VLC_VAR_INTEGER );
343     var_Create( p_vout, "mouse-moved", VLC_VAR_BOOL );
344     var_Create( p_vout, "mouse-clicked", VLC_VAR_INTEGER );
345
346     var_Create( p_vout, "intf-change", VLC_VAR_BOOL );
347     val.b_bool = VLC_TRUE;
348     var_Set( p_vout, "intf-change", val );
349 }
350
351 /*****************************************************************************
352  * vout_Snapshot: generates a snapshot.
353  *****************************************************************************/
354 int vout_Snapshot( vout_thread_t *p_vout, picture_t *p_pic )
355 {
356     image_handler_t *p_image = image_HandlerCreate( p_vout );
357     video_format_t fmt_in = {0}, fmt_out = {0};
358     char *psz_filename;
359     subpicture_t *p_subpic;
360     picture_t *p_pif;
361     vlc_value_t val, format;
362     int i_ret;
363
364     var_Get( p_vout, "snapshot-path", &val );
365     if( val.psz_string && !*val.psz_string )
366     {
367         free( val.psz_string );
368         val.psz_string = 0;
369     }
370
371 #if defined(SYS_DARWIN) || defined(SYS_BEOS)
372     if( !val.psz_string && p_vout->p_vlc->psz_homedir )
373     {
374         asprintf( &val.psz_string, "%s/Desktop",
375                   p_vout->p_vlc->psz_homedir );
376     }
377
378 #elif defined(WIN32) && !defined(UNDER_CE)
379     if( !val.psz_string && p_vout->p_vlc->psz_homedir )
380     {
381         /* Get the My Pictures folder path */
382
383         char *p_mypicturesdir = NULL;
384         typedef HRESULT (WINAPI *SHGETFOLDERPATH)( HWND, int, HANDLE, DWORD,
385                                                    LPSTR );
386         #ifndef CSIDL_FLAG_CREATE
387         #   define CSIDL_FLAG_CREATE 0x8000
388         #endif
389         #ifndef CSIDL_MYPICTURES
390         #   define CSIDL_MYPICTURES 0x27
391         #endif
392         #ifndef SHGFP_TYPE_CURRENT
393         #   define SHGFP_TYPE_CURRENT 0
394         #endif
395
396         HINSTANCE shfolder_dll;
397         SHGETFOLDERPATH SHGetFolderPath ;
398
399         /* load the shfolder dll to retrieve SHGetFolderPath */
400         if( ( shfolder_dll = LoadLibrary( _T("SHFolder.dll") ) ) != NULL )
401         {
402             SHGetFolderPath = (void *)GetProcAddress( shfolder_dll,
403                                                       _T("SHGetFolderPathA") );
404             if( SHGetFolderPath != NULL )
405             {
406                 p_mypicturesdir = (char *)malloc( MAX_PATH );
407                 if( p_mypicturesdir ) 
408                 {
409
410                     if( S_OK != SHGetFolderPath( NULL,
411                                         CSIDL_MYPICTURES | CSIDL_FLAG_CREATE,
412                                         NULL, SHGFP_TYPE_CURRENT,
413                                         p_mypicturesdir ) )
414                     {
415                         free( p_mypicturesdir );
416                         p_mypicturesdir = NULL;
417                     }
418                 }
419             }
420             FreeLibrary( shfolder_dll );
421         }
422
423         if( p_mypicturesdir == NULL )
424         {
425             asprintf( &val.psz_string, "%s/" CONFIG_DIR,
426                       p_vout->p_vlc->psz_homedir );
427         }
428         else
429         {
430             asprintf( &val.psz_string, p_mypicturesdir );
431             free( p_mypicturesdir );
432         }
433     }
434
435 #else
436     if( !val.psz_string && p_vout->p_vlc->psz_homedir )
437     {
438         asprintf( &val.psz_string, "%s/" CONFIG_DIR,
439                   p_vout->p_vlc->psz_homedir );
440     }
441 #endif
442
443     if( !val.psz_string )
444     {
445         msg_Err( p_vout, "no directory specified for snapshots" );
446         return VLC_EGENERIC;
447     }
448     var_Get( p_vout, "snapshot-format", &format );
449     if( !format.psz_string || !*format.psz_string )
450     {
451         if( format.psz_string ) free( format.psz_string );
452         format.psz_string = strdup( "png" );
453     }
454
455     asprintf( &psz_filename, "%s/vlcsnap-%u.%s", val.psz_string,
456               (unsigned int)(p_pic->date / 100000) & 0xFFFFFF,
457               format.psz_string );
458     free( val.psz_string );
459     free( format.psz_string );
460
461     /* Save the snapshot */
462     fmt_in = p_vout->fmt_in;
463     fmt_out.i_sar_num = fmt_out.i_sar_den = 1;
464     i_ret = image_WriteUrl( p_image, p_pic, &fmt_in, &fmt_out, psz_filename );
465     if( i_ret != VLC_SUCCESS )
466     {
467         msg_Err( p_vout, "could not create snapshot %s", psz_filename );
468         free( psz_filename );
469         image_HandlerDelete( p_image );
470         return VLC_EGENERIC;
471     }
472
473     msg_Dbg( p_vout, "snapshot taken (%s)", psz_filename );
474     free( psz_filename );
475
476     /* Inject a subpicture with the snapshot */
477     memset( &fmt_out, 0, sizeof(fmt_out) );
478     fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
479     p_pif = image_Convert( p_image, p_pic, &fmt_in, &fmt_out );
480     image_HandlerDelete( p_image );
481     if( !p_pif ) return VLC_EGENERIC;
482
483     p_subpic = spu_CreateSubpicture( p_vout->p_spu );
484     if( p_subpic == NULL )
485     {
486          p_pif->pf_release( p_pif );
487          return VLC_EGENERIC;
488     }
489
490     p_subpic->i_channel = 0;
491     p_subpic->i_start = mdate();
492     p_subpic->i_stop = mdate() + 4000000;
493     p_subpic->b_ephemer = VLC_TRUE;
494     p_subpic->b_fade = VLC_TRUE;
495     p_subpic->i_original_picture_width = p_vout->render.i_width * 4;
496     p_subpic->i_original_picture_height = p_vout->render.i_height * 4;
497
498     p_subpic->p_region = spu_CreateRegion( p_vout->p_spu, &fmt_out );
499     vout_CopyPicture( p_image->p_parent, &p_subpic->p_region->picture, p_pif );
500     p_pif->pf_release( p_pif );
501
502     spu_DisplaySubpicture( p_vout->p_spu, p_subpic );
503
504     return VLC_SUCCESS;
505 }
506
507 /*****************************************************************************
508  * vout_ControlDefault: default methods for video output control.
509  *****************************************************************************/
510 int vout_vaControlDefault( vout_thread_t *p_vout, int i_query, va_list args )
511 {
512     switch( i_query )
513     {
514     case VOUT_REPARENT:
515     case VOUT_CLOSE:
516         if( p_vout->p_parent_intf )
517         {
518             vlc_object_release( p_vout->p_parent_intf );
519             p_vout->p_parent_intf = NULL;
520         }
521         return VLC_SUCCESS;
522         break;
523
524     case VOUT_SNAPSHOT:
525         p_vout->b_snapshot = VLC_TRUE;
526         return VLC_SUCCESS;
527         break;
528
529     default:
530         msg_Dbg( p_vout, "control query not supported" );
531         return VLC_EGENERIC;
532     }
533 }
534
535 /*****************************************************************************
536  * InitWindowSize: find the initial dimensions the video window should have.
537  *****************************************************************************
538  * This function will check the "width", "height" and "zoom" config options and
539  * will calculate the size that the video window should have.
540  *****************************************************************************/
541 static void InitWindowSize( vout_thread_t *p_vout, unsigned *pi_width,
542                             unsigned *pi_height )
543 {
544     vlc_value_t val;
545     int i_width, i_height;
546     uint64_t ll_zoom;
547
548 #define FP_FACTOR 1000                             /* our fixed point factor */
549
550     var_Get( p_vout, "width", &val );
551     i_width = val.i_int;
552     var_Get( p_vout, "height", &val );
553     i_height = val.i_int;
554     var_Get( p_vout, "zoom", &val );
555     ll_zoom = (uint64_t)( FP_FACTOR * val.f_float );
556
557     if( i_width > 0 && i_height > 0)
558     {
559         *pi_width = (int)( i_width * ll_zoom / FP_FACTOR );
560         *pi_height = (int)( i_height * ll_zoom / FP_FACTOR );
561         goto initwsize_end;
562     }
563     else if( i_width > 0 )
564     {
565         *pi_width = (int)( i_width * ll_zoom / FP_FACTOR );
566         *pi_height = (int)( p_vout->fmt_in.i_visible_height * ll_zoom *
567             p_vout->fmt_in.i_sar_den * i_width / p_vout->fmt_in.i_sar_num /
568             FP_FACTOR / p_vout->fmt_in.i_visible_width );
569         goto initwsize_end;
570     }
571     else if( i_height > 0 )
572     {
573         *pi_height = (int)( i_height * ll_zoom / FP_FACTOR );
574         *pi_width = (int)( p_vout->fmt_in.i_visible_width * ll_zoom *
575             p_vout->fmt_in.i_sar_num * i_height / p_vout->fmt_in.i_sar_den /
576             FP_FACTOR / p_vout->fmt_in.i_visible_height );
577         goto initwsize_end;
578     }
579
580     if( p_vout->fmt_in.i_sar_num >= p_vout->fmt_in.i_sar_den )
581     {
582         *pi_width = (int)( p_vout->fmt_in.i_visible_width * ll_zoom *
583             p_vout->fmt_in.i_sar_num / p_vout->fmt_in.i_sar_den / FP_FACTOR );
584         *pi_height = (int)( p_vout->fmt_in.i_visible_height * ll_zoom 
585             / FP_FACTOR );
586     }
587     else
588     {
589         *pi_width = (int)( p_vout->fmt_in.i_visible_width * ll_zoom 
590             / FP_FACTOR );
591         *pi_height = (int)( p_vout->fmt_in.i_visible_height * ll_zoom *
592             p_vout->fmt_in.i_sar_den / p_vout->fmt_in.i_sar_num / FP_FACTOR );
593     }
594
595 initwsize_end:
596     msg_Dbg( p_vout, "window size: %dx%d", p_vout->i_window_width, 
597              p_vout->i_window_height );
598
599 #undef FP_FACTOR
600 }
601
602 /*****************************************************************************
603  * Object variables callbacks
604  *****************************************************************************/
605 static int ZoomCallback( vlc_object_t *p_this, char const *psz_cmd,
606                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
607 {
608     vout_thread_t *p_vout = (vout_thread_t *)p_this;
609     InitWindowSize( p_vout, &p_vout->i_window_width,
610                     &p_vout->i_window_height );
611     vout_Control( p_vout, VOUT_SET_ZOOM );
612     return VLC_SUCCESS;
613 }
614
615 static int CropCallback( vlc_object_t *p_this, char const *psz_cmd,
616                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
617 {
618     vout_thread_t *p_vout = (vout_thread_t *)p_this;
619     int64_t i_aspect_num, i_aspect_den;
620     unsigned int i_width, i_height;
621
622     char *psz_end, *psz_parser = strchr( newval.psz_string, ':' );
623
624     /* Restore defaults */
625     p_vout->fmt_in.i_x_offset = p_vout->fmt_render.i_x_offset;
626     p_vout->fmt_in.i_visible_width = p_vout->fmt_render.i_visible_width;
627     p_vout->fmt_in.i_y_offset = p_vout->fmt_render.i_y_offset;
628     p_vout->fmt_in.i_visible_height = p_vout->fmt_render.i_visible_height;
629
630     if( !psz_parser ) goto crop_end;
631
632     i_aspect_num = strtol( newval.psz_string, &psz_end, 0 );
633     if( psz_end == newval.psz_string || !i_aspect_num ) goto crop_end;
634
635     i_aspect_den = strtol( ++psz_parser, &psz_end, 0 );
636     if( psz_end == psz_parser || !i_aspect_den ) goto crop_end;
637
638     i_width = p_vout->fmt_in.i_sar_den * p_vout->fmt_render.i_visible_height *
639         i_aspect_num / i_aspect_den / p_vout->fmt_in.i_sar_num;
640     i_height = p_vout->fmt_render.i_visible_width * p_vout->fmt_in.i_sar_num *
641         i_aspect_den / i_aspect_num / p_vout->fmt_in.i_sar_den;
642
643     if( i_width < p_vout->fmt_render.i_visible_width )
644     {
645         p_vout->fmt_in.i_x_offset = p_vout->fmt_render.i_x_offset +
646             (p_vout->fmt_render.i_visible_width - i_width) / 2;
647         p_vout->fmt_in.i_visible_width = i_width;
648     }
649     else
650     {
651         p_vout->fmt_in.i_y_offset = p_vout->fmt_render.i_y_offset +
652             (p_vout->fmt_render.i_visible_height - i_height) / 2;
653         p_vout->fmt_in.i_visible_height = i_height;
654     }
655
656  crop_end:
657     InitWindowSize( p_vout, &p_vout->i_window_width,
658                     &p_vout->i_window_height );
659
660     p_vout->i_changes |= VOUT_CROP_CHANGE;
661
662     msg_Dbg( p_vout, "cropping picture %ix%i to %i,%i,%ix%i",
663              p_vout->fmt_in.i_width, p_vout->fmt_in.i_height,
664              p_vout->fmt_in.i_x_offset, p_vout->fmt_in.i_y_offset,
665              p_vout->fmt_in.i_visible_width,
666              p_vout->fmt_in.i_visible_height );
667
668     return VLC_SUCCESS;
669 }
670
671 static int AspectCallback( vlc_object_t *p_this, char const *psz_cmd,
672                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
673 {
674     vout_thread_t *p_vout = (vout_thread_t *)p_this;
675     unsigned int i_aspect_num, i_aspect_den, i_sar_num, i_sar_den;
676     vlc_value_t val;
677
678     char *psz_end, *psz_parser = strchr( newval.psz_string, ':' );
679
680     /* Restore defaults */
681     p_vout->fmt_in.i_sar_num = p_vout->fmt_render.i_sar_num;
682     p_vout->fmt_in.i_sar_den = p_vout->fmt_render.i_sar_den;
683     p_vout->fmt_in.i_aspect = p_vout->fmt_render.i_aspect;
684     p_vout->render.i_aspect = p_vout->fmt_render.i_aspect;
685
686     if( !psz_parser ) goto aspect_end;
687
688     i_aspect_num = strtol( newval.psz_string, &psz_end, 0 );
689     if( psz_end == newval.psz_string || !i_aspect_num ) goto aspect_end;
690
691     i_aspect_den = strtol( ++psz_parser, &psz_end, 0 );
692     if( psz_end == psz_parser || !i_aspect_den ) goto aspect_end;
693
694     i_sar_num = i_aspect_num * p_vout->fmt_render.i_visible_height;
695     i_sar_den = i_aspect_den * p_vout->fmt_render.i_visible_width;
696     vlc_ureduce( &i_sar_num, &i_sar_den, i_sar_num, i_sar_den, 0 );
697     p_vout->fmt_in.i_sar_num = i_sar_num;
698     p_vout->fmt_in.i_sar_den = i_sar_den;
699     p_vout->fmt_in.i_aspect = i_aspect_num * VOUT_ASPECT_FACTOR / i_aspect_den;
700     p_vout->render.i_aspect = p_vout->fmt_in.i_aspect;
701
702  aspect_end:
703     if( p_vout->i_par_num && p_vout->i_par_den )
704     {
705         p_vout->fmt_in.i_sar_num *= p_vout->i_par_den;
706         p_vout->fmt_in.i_sar_den *= p_vout->i_par_num;
707         p_vout->fmt_in.i_aspect = p_vout->fmt_in.i_aspect *
708             p_vout->i_par_den / p_vout->i_par_num;
709         p_vout->render.i_aspect = p_vout->fmt_in.i_aspect;
710     }
711
712     p_vout->i_changes |= VOUT_ASPECT_CHANGE;
713
714     vlc_ureduce( &i_aspect_num, &i_aspect_den,
715                  p_vout->fmt_in.i_aspect, VOUT_ASPECT_FACTOR, 0 );
716     msg_Dbg( p_vout, "new aspect-ratio %i:%i, sample aspect-ratio %i:%i",
717              i_aspect_num, i_aspect_den,
718              p_vout->fmt_in.i_sar_num, p_vout->fmt_in.i_sar_den );
719
720     var_Get( p_vout, "crop", &val );
721     return CropCallback( p_this, 0, val, val, 0 );
722
723     return VLC_SUCCESS;
724 }
725
726 static int OnTopCallback( vlc_object_t *p_this, char const *psz_cmd,
727                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
728 {
729     vout_thread_t *p_vout = (vout_thread_t *)p_this;
730     playlist_t *p_playlist;
731     vout_Control( p_vout, VOUT_SET_STAY_ON_TOP, newval.b_bool );
732
733     p_playlist = (playlist_t *)vlc_object_find( p_this, VLC_OBJECT_PLAYLIST,
734                                                  FIND_PARENT );
735     if( p_playlist )
736     {
737         /* Modify playlist as well because the vout might have to be restarted */
738         var_Create( p_playlist, "video-on-top", VLC_VAR_BOOL );
739         var_Set( p_playlist, "video-on-top", newval );
740
741         vlc_object_release( p_playlist );
742     }
743     return VLC_SUCCESS;
744 }
745
746 static int FullscreenCallback( vlc_object_t *p_this, char const *psz_cmd,
747                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
748 {
749     vout_thread_t *p_vout = (vout_thread_t *)p_this;
750     playlist_t *p_playlist;
751     vlc_value_t val;
752
753     p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
754
755     p_playlist = (playlist_t *)vlc_object_find( p_this, VLC_OBJECT_PLAYLIST,
756                                                  FIND_PARENT );
757     if( p_playlist )
758     {
759         /* Modify playlist as well because the vout might have to be restarted */
760         var_Create( p_playlist, "fullscreen", VLC_VAR_BOOL );
761         var_Set( p_playlist, "fullscreen", newval );
762
763         vlc_object_release( p_playlist );
764     }
765
766     /* Disable "always on top" in fullscreen mode */
767     var_Get( p_vout, "video-on-top", &val );
768     if( newval.b_bool && val.b_bool )
769     {
770         val.b_bool = VLC_FALSE;
771         vout_Control( p_vout, VOUT_SET_STAY_ON_TOP, val.b_bool );
772     }
773     else if( !newval.b_bool && val.b_bool )
774     {
775         vout_Control( p_vout, VOUT_SET_STAY_ON_TOP, val.b_bool );
776     }
777
778     val.b_bool = VLC_TRUE;
779     var_Set( p_vout, "intf-change", val );
780     return VLC_SUCCESS;
781 }
782
783 static int SnapshotCallback( vlc_object_t *p_this, char const *psz_cmd,
784                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
785 {
786     vout_thread_t *p_vout = (vout_thread_t *)p_this;
787     vout_Control( p_vout, VOUT_SNAPSHOT );
788     return VLC_SUCCESS;
789 }