On OSes other than Windows, Mac OS X and BeOS (so it's Linux) comply with the XDG...
[vlc.git] / src / video_output / vout_intf.c
1 /*****************************************************************************
2  * vout_intf.c : video output interface
3  *****************************************************************************
4  * Copyright (C) 2000-2007 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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27
28 #include <vlc/vlc.h>
29
30 #include <stdio.h>
31 #include <stdlib.h>                                                /* free() */
32 #include <sys/types.h>                                          /* opendir() */
33 #include <dirent.h>                                             /* opendir() */
34
35 #include <vlc_interface.h>
36 #include <vlc_block.h>
37 #include <vlc_playlist.h>
38
39 #include <vlc_vout.h>
40 #include <vlc_image.h>
41 #include <vlc_osd.h>
42 #include <vlc_charset.h>
43
44 #include <vlc_strings.h>
45 #include <vlc_charset.h>
46 #include "../libvlc.h"
47
48 /*****************************************************************************
49  * Local prototypes
50  *****************************************************************************/
51 static void InitWindowSize( vout_thread_t *, unsigned *, unsigned * );
52
53 /* Object variables callbacks */
54 static int ZoomCallback( vlc_object_t *, char const *,
55                          vlc_value_t, vlc_value_t, void * );
56 static int CropCallback( vlc_object_t *, char const *,
57                          vlc_value_t, vlc_value_t, void * );
58 static int AspectCallback( vlc_object_t *, char const *,
59                            vlc_value_t, vlc_value_t, void * );
60 static int OnTopCallback( vlc_object_t *, char const *,
61                           vlc_value_t, vlc_value_t, void * );
62 static int FullscreenCallback( vlc_object_t *, char const *,
63                                vlc_value_t, vlc_value_t, void * );
64 static int SnapshotCallback( vlc_object_t *, char const *,
65                              vlc_value_t, vlc_value_t, void * );
66 static int TitleCallback( vlc_object_t *, char const *,
67                        vlc_value_t, vlc_value_t, void * );
68
69 /*****************************************************************************
70  * vout_RequestWindow: Create/Get a video window if possible.
71  *****************************************************************************
72  * This function looks for the main interface and tries to request
73  * a new video window. If it fails then the vout will still need to create the
74  * window by itself.
75  *****************************************************************************/
76 void *vout_RequestWindow( vout_thread_t *p_vout,
77                           int *pi_x_hint, int *pi_y_hint,
78                           unsigned int *pi_width_hint,
79                           unsigned int *pi_height_hint )
80 {
81     intf_thread_t *p_intf = NULL;
82     vlc_list_t *p_list;
83     void *p_window;
84     vlc_value_t val;
85     int i;
86
87     /* Small kludge */
88     if( !var_Type( p_vout, "aspect-ratio" ) ) vout_IntfInit( p_vout );
89
90     /* Get requested coordinates */
91     var_Get( p_vout, "video-x", &val );
92     *pi_x_hint = val.i_int ;
93     var_Get( p_vout, "video-y", &val );
94     *pi_y_hint = val.i_int;
95
96     *pi_width_hint = p_vout->i_window_width;
97     *pi_height_hint = p_vout->i_window_height;
98
99     /* Check whether someone provided us with a window ID */
100     var_Get( p_vout->p_libvlc, "drawable", &val );
101     if( val.i_int ) return (void *)(intptr_t)val.i_int;
102
103     /* Find if the main interface supports embedding */
104     p_list = vlc_list_find( p_vout, VLC_OBJECT_INTF, FIND_ANYWHERE );
105     if( !p_list ) return NULL;
106
107     for( i = 0; i < p_list->i_count; i++ )
108     {
109         p_intf = (intf_thread_t *)p_list->p_values[i].p_object;
110         if( p_intf->b_block && p_intf->pf_request_window ) break;
111         p_intf = NULL;
112     }
113
114     if( !p_intf )
115     {
116         vlc_list_release( p_list );
117         return NULL;
118     }
119
120     vlc_object_yield( p_intf );
121     vlc_list_release( p_list );
122
123     p_window = p_intf->pf_request_window( p_intf, p_vout, pi_x_hint, pi_y_hint,
124                                           pi_width_hint, pi_height_hint );
125
126     if( !p_window ) vlc_object_release( p_intf );
127     else p_vout->p_parent_intf = p_intf;
128
129     return p_window;
130 }
131
132 void vout_ReleaseWindow( vout_thread_t *p_vout, void *p_window )
133 {
134     intf_thread_t *p_intf = p_vout->p_parent_intf;
135
136     if( !p_intf ) return;
137
138     vlc_mutex_lock( &p_intf->object_lock );
139     if( p_intf->b_dead )
140     {
141         vlc_mutex_unlock( &p_intf->object_lock );
142         return;
143     }
144
145     if( !p_intf->pf_release_window )
146     {
147         msg_Err( p_vout, "no pf_release_window");
148         vlc_mutex_unlock( &p_intf->object_lock );
149         vlc_object_release( p_intf );
150         return;
151     }
152
153     p_intf->pf_release_window( p_intf, p_window );
154
155     p_vout->p_parent_intf = NULL;
156     vlc_mutex_unlock( &p_intf->object_lock );
157     vlc_object_release( p_intf );
158 }
159
160 int vout_ControlWindow( vout_thread_t *p_vout, void *p_window,
161                         int i_query, va_list args )
162 {
163     intf_thread_t *p_intf = p_vout->p_parent_intf;
164     int i_ret;
165
166     if( !p_intf ) return VLC_EGENERIC;
167
168     vlc_mutex_lock( &p_intf->object_lock );
169     if( p_intf->b_dead )
170     {
171         vlc_mutex_unlock( &p_intf->object_lock );
172         return VLC_EGENERIC;
173     }
174
175     if( !p_intf->pf_control_window )
176     {
177         msg_Err( p_vout, "no pf_control_window");
178         vlc_mutex_unlock( &p_intf->object_lock );
179         return VLC_EGENERIC;
180     }
181
182     i_ret = p_intf->pf_control_window( p_intf, p_window, i_query, args );
183     vlc_mutex_unlock( &p_intf->object_lock );
184     return i_ret;
185 }
186
187 /*****************************************************************************
188  * vout_IntfInit: called during the vout creation to initialise misc things.
189  *****************************************************************************/
190 void vout_IntfInit( vout_thread_t *p_vout )
191 {
192     vlc_value_t val, text, old_val;
193     vlc_bool_t b_force_par = VLC_FALSE;
194     char *psz_buf;
195
196     /* Create a few object variables we'll need later on */
197     var_Create( p_vout, "snapshot-path", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
198     var_Create( p_vout, "snapshot-prefix", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
199     var_Create( p_vout, "snapshot-format", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
200     var_Create( p_vout, "snapshot-preview", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
201     var_Create( p_vout, "snapshot-sequential",
202                 VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
203     var_Create( p_vout, "snapshot-num", VLC_VAR_INTEGER );
204     var_SetInteger( p_vout, "snapshot-num", 1 );
205
206     var_Create( p_vout, "width", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
207     var_Create( p_vout, "height", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
208     var_Create( p_vout, "align", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
209     var_Get( p_vout, "align", &val );
210     p_vout->i_alignment = val.i_int;
211
212     var_Create( p_vout, "video-x", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
213     var_Create( p_vout, "video-y", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
214
215     var_Create( p_vout, "video-title-show", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
216     var_Create( p_vout, "video-title-timeout", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
217     var_Create( p_vout, "video-title-position", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
218
219     p_vout->b_title_show = var_GetBool( p_vout, "video-title-show" );
220     p_vout->i_title_timeout = (mtime_t) var_GetInteger( p_vout, "video-title-timeout" );
221     p_vout->i_title_position = var_GetInteger( p_vout, "video-title-position" );
222
223     var_AddCallback( p_vout, "video-title-show", TitleCallback, NULL );
224     var_AddCallback( p_vout, "video-title-timeout", TitleCallback, NULL );
225     var_AddCallback( p_vout, "video-title-position", TitleCallback, NULL );
226
227     /* Zoom object var */
228     var_Create( p_vout, "zoom", VLC_VAR_FLOAT | VLC_VAR_ISCOMMAND |
229                 VLC_VAR_HASCHOICE | VLC_VAR_DOINHERIT );
230
231     text.psz_string = _("Zoom");
232     var_Change( p_vout, "zoom", VLC_VAR_SETTEXT, &text, NULL );
233
234     var_Get( p_vout, "zoom", &old_val );
235     if( old_val.f_float == 0.25 ||
236         old_val.f_float == 0.5 ||
237         old_val.f_float == 1 ||
238         old_val.f_float == 2 )
239     {
240         var_Change( p_vout, "zoom", VLC_VAR_DELCHOICE, &old_val, NULL );
241     }
242
243     val.f_float = 0.25; text.psz_string = _("1:4 Quarter");
244     var_Change( p_vout, "zoom", VLC_VAR_ADDCHOICE, &val, &text );
245     val.f_float = 0.5; text.psz_string = _("1:2 Half");
246     var_Change( p_vout, "zoom", VLC_VAR_ADDCHOICE, &val, &text );
247     val.f_float = 1; text.psz_string = _("1:1 Original");
248     var_Change( p_vout, "zoom", VLC_VAR_ADDCHOICE, &val, &text );
249     val.f_float = 2; text.psz_string = _("2:1 Double");
250     var_Change( p_vout, "zoom", VLC_VAR_ADDCHOICE, &val, &text );
251
252     var_Set( p_vout, "zoom", old_val );
253
254     var_AddCallback( p_vout, "zoom", ZoomCallback, NULL );
255
256     /* Crop offset vars */
257     var_Create( p_vout, "crop-left", VLC_VAR_INTEGER );
258     var_Create( p_vout, "crop-top", VLC_VAR_INTEGER );
259     var_Create( p_vout, "crop-right", VLC_VAR_INTEGER );
260     var_Create( p_vout, "crop-bottom", VLC_VAR_INTEGER );
261
262     var_SetInteger( p_vout, "crop-left", 0 );
263     var_SetInteger( p_vout, "crop-top", 0 );
264     var_SetInteger( p_vout, "crop-right", 0 );
265     var_SetInteger( p_vout, "crop-bottom", 0 );
266
267     var_AddCallback( p_vout, "crop-left", CropCallback, NULL );
268     var_AddCallback( p_vout, "crop-top", CropCallback, NULL );
269     var_AddCallback( p_vout, "crop-right", CropCallback, NULL );
270     var_AddCallback( p_vout, "crop-bottom", CropCallback, NULL );
271
272     /* Crop object var */
273     var_Create( p_vout, "crop", VLC_VAR_STRING |
274                 VLC_VAR_HASCHOICE | VLC_VAR_DOINHERIT );
275
276     text.psz_string = _("Crop");
277     var_Change( p_vout, "crop", VLC_VAR_SETTEXT, &text, NULL );
278
279     val.psz_string = "";
280     var_Change( p_vout, "crop", VLC_VAR_DELCHOICE, &val, 0 );
281     val.psz_string = ""; text.psz_string = _("Default");
282     var_Change( p_vout, "crop", VLC_VAR_ADDCHOICE, &val, &text );
283     val.psz_string = "1:1"; text.psz_string = "1:1";
284     var_Change( p_vout, "crop", VLC_VAR_ADDCHOICE, &val, &text );
285     val.psz_string = "4:3"; text.psz_string = "4:3";
286     var_Change( p_vout, "crop", VLC_VAR_ADDCHOICE, &val, &text );
287     val.psz_string = "16:9"; text.psz_string = "16:9";
288     var_Change( p_vout, "crop", VLC_VAR_ADDCHOICE, &val, &text );
289     val.psz_string = "16:10"; text.psz_string = "16:10";
290     var_Change( p_vout, "crop", VLC_VAR_ADDCHOICE, &val, &text );
291     val.psz_string = "5:4"; text.psz_string = "5:4";
292     var_Change( p_vout, "crop", VLC_VAR_ADDCHOICE, &val, &text );
293     val.psz_string = "5:3"; text.psz_string = "5:3";
294     var_Change( p_vout, "crop", VLC_VAR_ADDCHOICE, &val, &text );
295     val.psz_string = "1.85:1"; text.psz_string = "1.85:1";
296     var_Change( p_vout, "crop", VLC_VAR_ADDCHOICE, &val, &text );
297     val.psz_string = "221:100"; text.psz_string = "2.21:1";
298     var_Change( p_vout, "crop", VLC_VAR_ADDCHOICE, &val, &text );
299     val.psz_string = "235:100"; text.psz_string = "2.35:1";
300     var_Change( p_vout, "crop", VLC_VAR_ADDCHOICE, &val, &text );
301     val.psz_string = "239:100"; text.psz_string = "2.39:1";
302     var_Change( p_vout, "crop", VLC_VAR_ADDCHOICE, &val, &text );
303
304     /* Add custom crop ratios */
305     psz_buf = config_GetPsz( p_vout, "custom-crop-ratios" );
306     if( psz_buf && *psz_buf )
307     {
308         char *psz_cur = psz_buf;
309         char *psz_next;
310         while( psz_cur && *psz_cur )
311         {
312             psz_next = strchr( psz_cur, ',' );
313             if( psz_next )
314             {
315                 *psz_next = '\0';
316                 psz_next++;
317             }
318             val.psz_string = strdup( psz_cur );
319             text.psz_string = strdup( psz_cur );
320             var_Change( p_vout, "crop", VLC_VAR_ADDCHOICE, &val, &text);
321             free( val.psz_string );
322             free( text.psz_string );
323             psz_cur = psz_next;
324         }
325     }
326     if( psz_buf ) free( psz_buf );
327
328     var_AddCallback( p_vout, "crop", CropCallback, NULL );
329     var_Get( p_vout, "crop", &old_val );
330     if( old_val.psz_string && *old_val.psz_string )
331         var_Change( p_vout, "crop", VLC_VAR_TRIGGER_CALLBACKS, 0, 0 );
332     if( old_val.psz_string ) free( old_val.psz_string );
333
334     /* Monitor pixel aspect-ratio */
335     var_Create( p_vout, "monitor-par", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
336     var_Get( p_vout, "monitor-par", &val );
337     if( val.psz_string && *val.psz_string )
338     {
339         char *psz_parser = strchr( val.psz_string, ':' );
340         unsigned int i_aspect_num = 0, i_aspect_den = 0;
341         float i_aspect = 0;
342         if( psz_parser )
343         {
344             i_aspect_num = strtol( val.psz_string, 0, 10 );
345             i_aspect_den = strtol( ++psz_parser, 0, 10 );
346         }
347         else
348         {
349             i_aspect = atof( val.psz_string );
350             vlc_ureduce( &i_aspect_num, &i_aspect_den,
351                          i_aspect *VOUT_ASPECT_FACTOR, VOUT_ASPECT_FACTOR, 0 );
352         }
353         if( !i_aspect_num || !i_aspect_den ) i_aspect_num = i_aspect_den = 1;
354
355         p_vout->i_par_num = i_aspect_num;
356         p_vout->i_par_den = i_aspect_den;
357
358         vlc_ureduce( &p_vout->i_par_num, &p_vout->i_par_den,
359                      p_vout->i_par_num, p_vout->i_par_den, 0 );
360
361         msg_Dbg( p_vout, "overriding monitor pixel aspect-ratio: %i:%i",
362                  p_vout->i_par_num, p_vout->i_par_den );
363         b_force_par = VLC_TRUE;
364     }
365     if( val.psz_string ) free( val.psz_string );
366
367     /* Aspect-ratio object var */
368     var_Create( p_vout, "aspect-ratio", VLC_VAR_STRING |
369                 VLC_VAR_HASCHOICE | VLC_VAR_DOINHERIT );
370
371     text.psz_string = _("Aspect-ratio");
372     var_Change( p_vout, "aspect-ratio", VLC_VAR_SETTEXT, &text, NULL );
373
374     val.psz_string = "";
375     var_Change( p_vout, "aspect-ratio", VLC_VAR_DELCHOICE, &val, 0 );
376     val.psz_string = ""; text.psz_string = _("Default");
377     var_Change( p_vout, "aspect-ratio", VLC_VAR_ADDCHOICE, &val, &text );
378     val.psz_string = "1:1"; text.psz_string = "1:1";
379     var_Change( p_vout, "aspect-ratio", VLC_VAR_ADDCHOICE, &val, &text );
380     val.psz_string = "4:3"; text.psz_string = "4:3";
381     var_Change( p_vout, "aspect-ratio", VLC_VAR_ADDCHOICE, &val, &text );
382     val.psz_string = "16:9"; text.psz_string = "16:9";
383     var_Change( p_vout, "aspect-ratio", VLC_VAR_ADDCHOICE, &val, &text );
384     val.psz_string = "16:10"; text.psz_string = "16:10";
385     var_Change( p_vout, "aspect-ratio", VLC_VAR_ADDCHOICE, &val, &text );
386     val.psz_string = "221:100"; text.psz_string = "2.21:1";
387     var_Change( p_vout, "aspect-ratio", VLC_VAR_ADDCHOICE, &val, &text );
388     val.psz_string = "5:4"; text.psz_string = "5:4";
389     var_Change( p_vout, "aspect-ratio", VLC_VAR_ADDCHOICE, &val, &text );
390
391     /* Add custom aspect ratios */
392     psz_buf = config_GetPsz( p_vout, "custom-aspect-ratios" );
393     if( psz_buf && *psz_buf )
394     {
395         char *psz_cur = psz_buf;
396         char *psz_next;
397         while( psz_cur && *psz_cur )
398         {
399             psz_next = strchr( psz_cur, ',' );
400             if( psz_next )
401             {
402                 *psz_next = '\0';
403                 psz_next++;
404             }
405             val.psz_string = strdup( psz_cur );
406             text.psz_string = strdup( psz_cur );
407             var_Change( p_vout, "aspect-ratio", VLC_VAR_ADDCHOICE, &val, &text);
408             free( val.psz_string );
409             free( text.psz_string );
410             psz_cur = psz_next;
411         }
412     }
413     if( psz_buf ) free( psz_buf );
414
415     var_AddCallback( p_vout, "aspect-ratio", AspectCallback, NULL );
416     var_Get( p_vout, "aspect-ratio", &old_val );
417     if( (old_val.psz_string && *old_val.psz_string) || b_force_par )
418         var_Change( p_vout, "aspect-ratio", VLC_VAR_TRIGGER_CALLBACKS, 0, 0 );
419     if( old_val.psz_string ) free( old_val.psz_string );
420
421     /* Initialize the dimensions of the video window */
422     InitWindowSize( p_vout, &p_vout->i_window_width,
423                     &p_vout->i_window_height );
424
425     /* Add a variable to indicate if the window should be on top of others */
426     var_Create( p_vout, "video-on-top", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
427     text.psz_string = _("Always on top");
428     var_Change( p_vout, "video-on-top", VLC_VAR_SETTEXT, &text, NULL );
429     var_AddCallback( p_vout, "video-on-top", OnTopCallback, NULL );
430
431     /* Add a variable to indicate whether we want window decoration or not */
432     var_Create( p_vout, "video-deco", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
433
434     /* Add a fullscreen variable */
435     var_Create( p_vout, "fullscreen", VLC_VAR_BOOL );
436     text.psz_string = _("Fullscreen");
437     var_Change( p_vout, "fullscreen", VLC_VAR_SETTEXT, &text, NULL );
438     var_Change( p_vout, "fullscreen", VLC_VAR_INHERITVALUE, &val, NULL );
439     if( val.b_bool )
440     {
441         /* user requested fullscreen */
442         p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
443     }
444     var_AddCallback( p_vout, "fullscreen", FullscreenCallback, NULL );
445
446     /* Add a snapshot variable */
447     var_Create( p_vout, "video-snapshot", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
448     text.psz_string = _("Snapshot");
449     var_Change( p_vout, "video-snapshot", VLC_VAR_SETTEXT, &text, NULL );
450     var_AddCallback( p_vout, "video-snapshot", SnapshotCallback, NULL );
451
452     /* Mouse coordinates */
453     var_Create( p_vout, "mouse-x", VLC_VAR_INTEGER );
454     var_Create( p_vout, "mouse-y", VLC_VAR_INTEGER );
455     var_Create( p_vout, "mouse-button-down", VLC_VAR_INTEGER );
456     var_Create( p_vout, "mouse-moved", VLC_VAR_BOOL );
457     var_Create( p_vout, "mouse-clicked", VLC_VAR_INTEGER );
458
459     var_Create( p_vout, "intf-change", VLC_VAR_BOOL );
460     val.b_bool = VLC_TRUE;
461     var_Set( p_vout, "intf-change", val );
462 }
463
464 /*****************************************************************************
465  * vout_Snapshot: generates a snapshot.
466  *****************************************************************************/
467 int vout_Snapshot( vout_thread_t *p_vout, picture_t *p_pic )
468 {
469     image_handler_t *p_image = image_HandlerCreate( p_vout );
470     video_format_t fmt_in, fmt_out;
471     char *psz_filename = NULL;
472     subpicture_t *p_subpic;
473     picture_t *p_pif;
474     vlc_value_t val, format;
475     DIR *path;
476     int i_ret;
477
478     memset( &fmt_in, 0, sizeof(video_format_t));
479     memset( &fmt_out, 0, sizeof(video_format_t));
480
481     var_Get( p_vout, "snapshot-path", &val );
482     if( val.psz_string && !*val.psz_string )
483     {
484         free( val.psz_string );
485         val.psz_string = 0;
486     }
487
488     /* Embedded snapshot : if snapshot-path == object:object-id, then
489        create a snapshot_t* and store it in
490        object(object-id)->p_private, then unlock and signal the
491        waiting object.
492      */
493     if( val.psz_string && !strncmp( val.psz_string, "object:", 7 ) )
494     {
495         int i_id;
496         vlc_object_t* p_dest;
497         block_t *p_block;
498         snapshot_t *p_snapshot;
499         int i_size;
500
501         /* Destination object-id is following object: */
502         i_id = atoi( &val.psz_string[7] );
503         p_dest = ( vlc_object_t* )vlc_current_object( i_id );
504         if( !p_dest )
505         {
506             msg_Err( p_vout, "Cannot find calling object" );
507             image_HandlerDelete( p_image );
508             return VLC_EGENERIC;
509         }
510         /* Object must be locked. We will unlock it once we get the
511            snapshot and written it to p_private */
512         p_dest->p_private = NULL;
513
514         /* Save the snapshot to a memory zone */
515         fmt_in = p_vout->fmt_in;
516         fmt_out.i_sar_num = fmt_out.i_sar_den = 1;
517         /* FIXME: should not be hardcoded. We should be able to
518         specify the snapshot size (snapshot-width and snapshot-height). */
519         fmt_out.i_width = 320;
520         fmt_out.i_height = 200;
521         fmt_out.i_chroma = VLC_FOURCC( 'p','n','g',' ' );
522         p_block = ( block_t* ) image_Write( p_image, p_pic, &fmt_in, &fmt_out );
523         if( !p_block )
524         {
525             msg_Err( p_vout, "Could not get snapshot" );
526             image_HandlerDelete( p_image );
527             vlc_cond_signal( &p_dest->object_wait );
528             vlc_object_release( p_dest );
529             return VLC_EGENERIC;
530         }
531
532         /* Copy the p_block data to a snapshot structure */
533         /* FIXME: get the timestamp */
534         p_snapshot = ( snapshot_t* ) malloc( sizeof( snapshot_t ) );
535         if( !p_snapshot )
536         {
537             block_Release( p_block );
538             image_HandlerDelete( p_image );
539             vlc_cond_signal( &p_dest->object_wait );
540             vlc_object_release( p_dest );
541             return VLC_ENOMEM;
542         }
543
544         i_size = p_block->i_buffer;
545
546         p_snapshot->i_width = fmt_out.i_width;
547         p_snapshot->i_height = fmt_out.i_height;
548         p_snapshot->i_datasize = i_size;
549         p_snapshot->date = p_block->i_pts; /* FIXME ?? */
550         p_snapshot->p_data = ( char* ) malloc( i_size );
551         if( !p_snapshot->p_data )
552         {
553             block_Release( p_block );
554             free( p_snapshot );
555             image_HandlerDelete( p_image );
556             vlc_cond_signal( &p_dest->object_wait );
557             vlc_object_release( p_dest );
558             return VLC_ENOMEM;
559         }
560         memcpy( p_snapshot->p_data, p_block->p_buffer, p_block->i_buffer );
561
562         p_dest->p_private = p_snapshot;
563
564         block_Release( p_block );
565
566         /* Unlock the object */
567         vlc_cond_signal( &p_dest->object_wait );
568         vlc_object_release( p_dest );
569
570         image_HandlerDelete( p_image );
571         return VLC_SUCCESS;
572     }
573
574
575 #if defined(__APPLE__) || defined(SYS_BEOS)
576     if( !val.psz_string && p_vout->p_libvlc->psz_homedir )
577     {
578         if( asprintf( &val.psz_string, "%s/Desktop",
579                       p_vout->p_libvlc->psz_homedir ) == -1 )
580             val.psz_string = NULL;
581     }
582
583 #elif defined(WIN32) && !defined(UNDER_CE)
584     if( !val.psz_string && p_vout->p_libvlc->psz_homedir )
585     {
586         /* Get the My Pictures folder path */
587
588         char *p_mypicturesdir = NULL;
589         typedef HRESULT (WINAPI *SHGETFOLDERPATH)( HWND, int, HANDLE, DWORD,
590                                                    LPWSTR );
591         #ifndef CSIDL_FLAG_CREATE
592         #   define CSIDL_FLAG_CREATE 0x8000
593         #endif
594         #ifndef CSIDL_MYPICTURES
595         #   define CSIDL_MYPICTURES 0x27
596         #endif
597         #ifndef SHGFP_TYPE_CURRENT
598         #   define SHGFP_TYPE_CURRENT 0
599         #endif
600
601         HINSTANCE shfolder_dll;
602         SHGETFOLDERPATH SHGetFolderPath ;
603
604         /* load the shfolder dll to retrieve SHGetFolderPath */
605         if( ( shfolder_dll = LoadLibrary( _T("SHFolder.dll") ) ) != NULL )
606         {
607            wchar_t wdir[PATH_MAX];
608            SHGetFolderPath = (void *)GetProcAddress( shfolder_dll,
609                                                       _T("SHGetFolderPathW") );
610             if ((SHGetFolderPath != NULL )
611              && SUCCEEDED (SHGetFolderPath (NULL,
612                                            CSIDL_MYPICTURES | CSIDL_FLAG_CREATE,
613                                            NULL, SHGFP_TYPE_CURRENT,
614                                            wdir)))
615                 p_mypicturesdir = FromWide (wdir);
616
617             FreeLibrary( shfolder_dll );
618         }
619
620         if( p_mypicturesdir == NULL )
621         {
622             if( asprintf( &val.psz_string, "%s",
623                           p_vout->p_libvlc->psz_homedir ) == -1 )
624                 val.psz_string = NULL;
625         }
626         else
627         {
628             if( asprintf( &val.psz_string, p_mypicturesdir ) == -1 )
629                 val.psz_string = NULL;
630             free( p_mypicturesdir );
631         }
632     }
633
634 #else
635     /* XXX: This saves in the data directory. Shouldn't we try saving
636      *      to psz_homedir/Desktop or something nicer ? */
637     if( !val.psz_string && p_vout->p_libvlc->psz_datadir )
638     {
639         if( asprintf( &val.psz_string, "%s",
640                       p_vout->p_libvlc->psz_datadir ) == -1 )
641             val.psz_string = NULL;
642     }
643 #endif
644
645     if( !val.psz_string )
646     {
647         msg_Err( p_vout, "no path specified for snapshots" );
648         return VLC_EGENERIC;
649     }
650     var_Get( p_vout, "snapshot-format", &format );
651     if( !format.psz_string || !*format.psz_string )
652     {
653         if( format.psz_string ) free( format.psz_string );
654         format.psz_string = strdup( "png" );
655     }
656
657     /*
658      * Did the user specify a directory? If not, path = NULL.
659      */
660     path = utf8_opendir ( (const char *)val.psz_string  );
661
662     if ( path != NULL )
663     {
664         char *psz_prefix = var_GetString( p_vout, "snapshot-prefix" );
665         if( !psz_prefix ) psz_prefix = strdup( "vlcsnap-" );
666         else
667         {
668             char *psz_tmp = str_format( p_vout, psz_prefix );
669             filename_sanitize( psz_tmp );
670             free( psz_prefix );
671             psz_prefix = psz_tmp;
672         }
673
674         closedir( path );
675         if( var_GetBool( p_vout, "snapshot-sequential" ) == VLC_TRUE )
676         {
677             int i_num = var_GetInteger( p_vout, "snapshot-num" );
678             FILE *p_file;
679             do
680             {
681                 asprintf( &psz_filename, "%s" DIR_SEP "%s%05d.%s", val.psz_string,
682                           psz_prefix, i_num++, format.psz_string );
683             }
684             while( ( p_file = utf8_fopen( psz_filename, "r" ) ) && !fclose( p_file ) );
685             var_SetInteger( p_vout, "snapshot-num", i_num );
686         }
687         else
688         {
689             asprintf( &psz_filename, "%s" DIR_SEP "%s%u.%s", val.psz_string,
690                       psz_prefix,
691                       (unsigned int)(p_pic->date / 100000) & 0xFFFFFF,
692                       format.psz_string );
693         }
694
695         free( psz_prefix );
696     }
697     else // The user specified a full path name (including file name)
698     {
699         psz_filename = str_format( p_vout, val.psz_string );
700         path_sanitize( psz_filename );
701     }
702
703     free( val.psz_string );
704     free( format.psz_string );
705
706     /* Save the snapshot */
707     fmt_in = p_vout->fmt_in;
708     fmt_out.i_sar_num = fmt_out.i_sar_den = 1;
709     i_ret = image_WriteUrl( p_image, p_pic, &fmt_in, &fmt_out, psz_filename );
710     if( i_ret != VLC_SUCCESS )
711     {
712         msg_Err( p_vout, "could not create snapshot %s", psz_filename );
713         free( psz_filename );
714         image_HandlerDelete( p_image );
715         return VLC_EGENERIC;
716     }
717
718     msg_Dbg( p_vout, "snapshot taken (%s)", psz_filename );
719     vout_OSDMessage( VLC_OBJECT( p_vout ), DEFAULT_CHAN,
720                      "%s", psz_filename );
721     free( psz_filename );
722
723     if( var_GetBool( p_vout, "snapshot-preview" ) )
724     {
725         /* Inject a subpicture with the snapshot */
726         memset( &fmt_out, 0, sizeof(fmt_out) );
727         fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
728         p_pif = image_Convert( p_image, p_pic, &fmt_in, &fmt_out );
729         image_HandlerDelete( p_image );
730         if( !p_pif ) return VLC_EGENERIC;
731
732         p_subpic = spu_CreateSubpicture( p_vout->p_spu );
733         if( p_subpic == NULL )
734         {
735              p_pif->pf_release( p_pif );
736              return VLC_EGENERIC;
737         }
738
739         p_subpic->i_channel = 0;
740         p_subpic->i_start = mdate();
741         p_subpic->i_stop = mdate() + 4000000;
742         p_subpic->b_ephemer = VLC_TRUE;
743         p_subpic->b_fade = VLC_TRUE;
744         p_subpic->i_original_picture_width = p_vout->render.i_width * 4;
745         p_subpic->i_original_picture_height = p_vout->render.i_height * 4;
746
747         p_subpic->p_region = spu_CreateRegion( p_vout->p_spu, &fmt_out );
748         vout_CopyPicture( p_image->p_parent, &p_subpic->p_region->picture,
749                           p_pif );
750         p_pif->pf_release( p_pif );
751
752         spu_DisplaySubpicture( p_vout->p_spu, p_subpic );
753     }
754     else
755     {
756         image_HandlerDelete( p_image );
757     }
758
759     return VLC_SUCCESS;
760 }
761
762 /*****************************************************************************
763  * Handle filters
764  *****************************************************************************/
765
766 void vout_EnableFilter( vout_thread_t *p_vout, char *psz_name,
767                         vlc_bool_t b_add, vlc_bool_t b_setconfig )
768 {
769     char *psz_parser;
770     char *psz_string = config_GetPsz( p_vout, "vout-filter" );
771
772     /* Todo : Use some generic chain manipulation functions */
773     if( !psz_string ) psz_string = strdup("");
774
775     psz_parser = strstr( psz_string, psz_name );
776     if( b_add )
777     {
778         if( !psz_parser )
779         {
780             psz_parser = psz_string;
781             asprintf( &psz_string, (*psz_string) ? "%s:%s" : "%s%s",
782                             psz_string, psz_name );
783             free( psz_parser );
784         }
785         else
786             return;
787     }
788     else
789     {
790         if( psz_parser )
791         {
792             memmove( psz_parser, psz_parser + strlen(psz_name) +
793                             (*(psz_parser + strlen(psz_name)) == ':' ? 1 : 0 ),
794                             strlen(psz_parser + strlen(psz_name)) + 1 );
795
796             /* Remove trailing : : */
797             if( *(psz_string+strlen(psz_string ) -1 ) == ':' )
798             {
799                 *(psz_string+strlen(psz_string ) -1 ) = '\0';
800             }
801          }
802          else
803          {
804              free( psz_string );
805              return;
806          }
807     }
808     if( b_setconfig )
809         config_PutPsz( p_vout, "vout-filter", psz_string );
810
811     var_SetString( p_vout, "vout-filter", psz_string );
812     free( psz_string );
813 }
814
815 /*****************************************************************************
816  * vout_ControlDefault: default methods for video output control.
817  *****************************************************************************/
818 int vout_vaControlDefault( vout_thread_t *p_vout, int i_query, va_list args )
819 {
820     (void)args;
821     switch( i_query )
822     {
823     case VOUT_REPARENT:
824     case VOUT_CLOSE:
825         if( p_vout->p_parent_intf )
826         {
827             vlc_object_release( p_vout->p_parent_intf );
828             p_vout->p_parent_intf = NULL;
829         }
830         return VLC_SUCCESS;
831         break;
832
833     case VOUT_SNAPSHOT:
834         p_vout->b_snapshot = VLC_TRUE;
835         return VLC_SUCCESS;
836         break;
837
838     default:
839         msg_Dbg( p_vout, "control query not supported" );
840         return VLC_EGENERIC;
841     }
842 }
843
844 /*****************************************************************************
845  * InitWindowSize: find the initial dimensions the video window should have.
846  *****************************************************************************
847  * This function will check the "width", "height" and "zoom" config options and
848  * will calculate the size that the video window should have.
849  *****************************************************************************/
850 static void InitWindowSize( vout_thread_t *p_vout, unsigned *pi_width,
851                             unsigned *pi_height )
852 {
853     vlc_value_t val;
854     int i_width, i_height;
855     uint64_t ll_zoom;
856
857 #define FP_FACTOR 1000                             /* our fixed point factor */
858
859     var_Get( p_vout, "width", &val );
860     i_width = val.i_int;
861     var_Get( p_vout, "height", &val );
862     i_height = val.i_int;
863     var_Get( p_vout, "zoom", &val );
864     ll_zoom = (uint64_t)( FP_FACTOR * val.f_float );
865
866     if( i_width > 0 && i_height > 0)
867     {
868         *pi_width = (int)( i_width * ll_zoom / FP_FACTOR );
869         *pi_height = (int)( i_height * ll_zoom / FP_FACTOR );
870         goto initwsize_end;
871     }
872     else if( i_width > 0 )
873     {
874         *pi_width = (int)( i_width * ll_zoom / FP_FACTOR );
875         *pi_height = (int)( p_vout->fmt_in.i_visible_height * ll_zoom *
876             p_vout->fmt_in.i_sar_den * i_width / p_vout->fmt_in.i_sar_num /
877             FP_FACTOR / p_vout->fmt_in.i_visible_width );
878         goto initwsize_end;
879     }
880     else if( i_height > 0 )
881     {
882         *pi_height = (int)( i_height * ll_zoom / FP_FACTOR );
883         *pi_width = (int)( p_vout->fmt_in.i_visible_width * ll_zoom *
884             p_vout->fmt_in.i_sar_num * i_height / p_vout->fmt_in.i_sar_den /
885             FP_FACTOR / p_vout->fmt_in.i_visible_height );
886         goto initwsize_end;
887     }
888
889     if( p_vout->fmt_in.i_sar_num == 0 || p_vout->fmt_in.i_sar_den == 0 ) {
890         msg_Warn( p_vout, "fucked up aspect" );
891         *pi_width = (int)( p_vout->fmt_in.i_visible_width * ll_zoom / FP_FACTOR );
892         *pi_height = (int)( p_vout->fmt_in.i_visible_height * ll_zoom /FP_FACTOR);
893     }
894     else if( p_vout->fmt_in.i_sar_num >= p_vout->fmt_in.i_sar_den )
895     {
896         *pi_width = (int)( p_vout->fmt_in.i_visible_width * ll_zoom *
897             p_vout->fmt_in.i_sar_num / p_vout->fmt_in.i_sar_den / FP_FACTOR );
898         *pi_height = (int)( p_vout->fmt_in.i_visible_height * ll_zoom 
899             / FP_FACTOR );
900     }
901     else
902     {
903         *pi_width = (int)( p_vout->fmt_in.i_visible_width * ll_zoom 
904             / FP_FACTOR );
905         *pi_height = (int)( p_vout->fmt_in.i_visible_height * ll_zoom *
906             p_vout->fmt_in.i_sar_den / p_vout->fmt_in.i_sar_num / FP_FACTOR );
907     }
908
909 initwsize_end:
910     msg_Dbg( p_vout, "window size: %dx%d", p_vout->i_window_width, 
911              p_vout->i_window_height );
912
913 #undef FP_FACTOR
914 }
915
916 /*****************************************************************************
917  * Object variables callbacks
918  *****************************************************************************/
919 static int ZoomCallback( vlc_object_t *p_this, char const *psz_cmd,
920                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
921 {
922     vout_thread_t *p_vout = (vout_thread_t *)p_this;
923     (void)psz_cmd; (void)oldval; (void)newval; (void)p_data;
924     InitWindowSize( p_vout, &p_vout->i_window_width,
925                     &p_vout->i_window_height );
926     vout_Control( p_vout, VOUT_SET_SIZE, p_vout->i_window_width,
927                   p_vout->i_window_height );
928     return VLC_SUCCESS;
929 }
930
931 static int CropCallback( vlc_object_t *p_this, char const *psz_cmd,
932                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
933 {
934     vout_thread_t *p_vout = (vout_thread_t *)p_this;
935     int64_t i_aspect_num, i_aspect_den;
936     unsigned int i_width, i_height;
937
938     (void)oldval; (void)p_data;
939
940     /* Restore defaults */
941     p_vout->fmt_in.i_x_offset = p_vout->fmt_render.i_x_offset;
942     p_vout->fmt_in.i_visible_width = p_vout->fmt_render.i_visible_width;
943     p_vout->fmt_in.i_y_offset = p_vout->fmt_render.i_y_offset;
944     p_vout->fmt_in.i_visible_height = p_vout->fmt_render.i_visible_height;
945
946     if( !strcmp( psz_cmd, "crop" ) )
947     {
948         char *psz_end = NULL, *psz_parser = strchr( newval.psz_string, ':' );
949         if( psz_parser )
950         {
951             /* We're using the 3:4 syntax */
952             i_aspect_num = strtol( newval.psz_string, &psz_end, 10 );
953             if( psz_end == newval.psz_string || !i_aspect_num ) goto crop_end;
954
955             i_aspect_den = strtol( ++psz_parser, &psz_end, 10 );
956             if( psz_end == psz_parser || !i_aspect_den ) goto crop_end;
957
958             i_width = p_vout->fmt_in.i_sar_den*p_vout->fmt_render.i_visible_height *
959                 i_aspect_num / i_aspect_den / p_vout->fmt_in.i_sar_num;
960             i_height = p_vout->fmt_render.i_visible_width*p_vout->fmt_in.i_sar_num *
961                 i_aspect_den / i_aspect_num / p_vout->fmt_in.i_sar_den;
962
963             if( i_width < p_vout->fmt_render.i_visible_width )
964             {
965                 p_vout->fmt_in.i_x_offset = p_vout->fmt_render.i_x_offset +
966                     (p_vout->fmt_render.i_visible_width - i_width) / 2;
967                 p_vout->fmt_in.i_visible_width = i_width;
968             }
969             else
970             {
971                 p_vout->fmt_in.i_y_offset = p_vout->fmt_render.i_y_offset +
972                     (p_vout->fmt_render.i_visible_height - i_height) / 2;
973                 p_vout->fmt_in.i_visible_height = i_height;
974             }
975         }
976         else
977         {
978             psz_parser = strchr( newval.psz_string, 'x' );
979             if( psz_parser )
980             {
981                 /* Maybe we're using the <width>x<height>+<left>+<top> syntax */
982                 unsigned int i_crop_width, i_crop_height, i_crop_top, i_crop_left;
983
984                 i_crop_width = strtol( newval.psz_string, &psz_end, 10 );
985                 if( psz_end != psz_parser ) goto crop_end;
986
987                 psz_parser = strchr( ++psz_end, '+' );
988                 i_crop_height = strtol( psz_end, &psz_end, 10 );
989                 if( psz_end != psz_parser ) goto crop_end;
990
991                 psz_parser = strchr( ++psz_end, '+' );
992                 i_crop_left = strtol( psz_end, &psz_end, 10 );
993                 if( psz_end != psz_parser ) goto crop_end;
994
995                 psz_end++;
996                 i_crop_top = strtol( psz_end, &psz_end, 10 );
997                 if( *psz_end != '\0' ) goto crop_end;
998
999                 i_width = i_crop_width;
1000                 p_vout->fmt_in.i_visible_width = i_width;
1001
1002                 i_height = i_crop_height;
1003                 p_vout->fmt_in.i_visible_height = i_height;
1004
1005                 p_vout->fmt_in.i_x_offset = i_crop_left;
1006                 p_vout->fmt_in.i_y_offset = i_crop_top;
1007             }
1008             else
1009             {
1010                 /* Maybe we're using the <left>+<top>+<right>+<bottom> syntax */
1011                 unsigned int i_crop_top, i_crop_left, i_crop_bottom, i_crop_right;
1012
1013                 psz_parser = strchr( newval.psz_string, '+' );
1014                 i_crop_left = strtol( newval.psz_string, &psz_end, 10 );
1015                 if( psz_end != psz_parser ) goto crop_end;
1016
1017                 psz_parser = strchr( ++psz_end, '+' );
1018                 i_crop_top = strtol( psz_end, &psz_end, 10 );
1019                 if( psz_end != psz_parser ) goto crop_end;
1020
1021                 psz_parser = strchr( ++psz_end, '+' );
1022                 i_crop_right = strtol( psz_end, &psz_end, 10 );
1023                 if( psz_end != psz_parser ) goto crop_end;
1024
1025                 psz_end++;
1026                 i_crop_bottom = strtol( psz_end, &psz_end, 10 );
1027                 if( *psz_end != '\0' ) goto crop_end;
1028
1029                 i_width = p_vout->fmt_render.i_visible_width
1030                           - i_crop_left - i_crop_right;
1031                 p_vout->fmt_in.i_visible_width = i_width;
1032
1033                 i_height = p_vout->fmt_render.i_visible_height
1034                            - i_crop_top - i_crop_bottom;
1035                 p_vout->fmt_in.i_visible_height = i_height;
1036
1037                 p_vout->fmt_in.i_x_offset = i_crop_left;
1038                 p_vout->fmt_in.i_y_offset = i_crop_top;
1039             }
1040         }
1041     }
1042     else if( !strcmp( psz_cmd, "crop-top" )
1043           || !strcmp( psz_cmd, "crop-left" )
1044           || !strcmp( psz_cmd, "crop-bottom" )
1045           || !strcmp( psz_cmd, "crop-right" ) )
1046     {
1047         unsigned int i_crop_top, i_crop_left, i_crop_bottom, i_crop_right;
1048
1049         i_crop_top = var_GetInteger( p_vout, "crop-top" );
1050         i_crop_left = var_GetInteger( p_vout, "crop-left" );
1051         i_crop_right = var_GetInteger( p_vout, "crop-right" );
1052         i_crop_bottom = var_GetInteger( p_vout, "crop-bottom" );
1053
1054         i_width = p_vout->fmt_render.i_visible_width
1055                   - i_crop_left - i_crop_right;
1056         p_vout->fmt_in.i_visible_width = i_width;
1057
1058         i_height = p_vout->fmt_render.i_visible_height
1059                    - i_crop_top - i_crop_bottom;
1060         p_vout->fmt_in.i_visible_height = i_height;
1061
1062         p_vout->fmt_in.i_x_offset = i_crop_left;
1063         p_vout->fmt_in.i_y_offset = i_crop_top;
1064     }
1065
1066  crop_end:
1067     InitWindowSize( p_vout, &p_vout->i_window_width,
1068                     &p_vout->i_window_height );
1069
1070     p_vout->i_changes |= VOUT_CROP_CHANGE;
1071
1072     msg_Dbg( p_vout, "cropping picture %ix%i to %i,%i,%ix%i",
1073              p_vout->fmt_in.i_width, p_vout->fmt_in.i_height,
1074              p_vout->fmt_in.i_x_offset, p_vout->fmt_in.i_y_offset,
1075              p_vout->fmt_in.i_visible_width,
1076              p_vout->fmt_in.i_visible_height );
1077
1078     return VLC_SUCCESS;
1079 }
1080
1081 static int AspectCallback( vlc_object_t *p_this, char const *psz_cmd,
1082                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
1083 {
1084     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1085     unsigned int i_aspect_num, i_aspect_den, i_sar_num, i_sar_den;
1086     vlc_value_t val;
1087
1088     char *psz_end, *psz_parser = strchr( newval.psz_string, ':' );
1089     (void)psz_cmd; (void)oldval; (void)p_data;
1090
1091     /* Restore defaults */
1092     p_vout->fmt_in.i_sar_num = p_vout->fmt_render.i_sar_num;
1093     p_vout->fmt_in.i_sar_den = p_vout->fmt_render.i_sar_den;
1094     p_vout->fmt_in.i_aspect = p_vout->fmt_render.i_aspect;
1095     p_vout->render.i_aspect = p_vout->fmt_render.i_aspect;
1096
1097     if( !psz_parser ) goto aspect_end;
1098
1099     i_aspect_num = strtol( newval.psz_string, &psz_end, 10 );
1100     if( psz_end == newval.psz_string || !i_aspect_num ) goto aspect_end;
1101
1102     i_aspect_den = strtol( ++psz_parser, &psz_end, 10 );
1103     if( psz_end == psz_parser || !i_aspect_den ) goto aspect_end;
1104
1105     i_sar_num = i_aspect_num * p_vout->fmt_render.i_visible_height;
1106     i_sar_den = i_aspect_den * p_vout->fmt_render.i_visible_width;
1107     vlc_ureduce( &i_sar_num, &i_sar_den, i_sar_num, i_sar_den, 0 );
1108     p_vout->fmt_in.i_sar_num = i_sar_num;
1109     p_vout->fmt_in.i_sar_den = i_sar_den;
1110     p_vout->fmt_in.i_aspect = i_aspect_num * VOUT_ASPECT_FACTOR / i_aspect_den;
1111     p_vout->render.i_aspect = p_vout->fmt_in.i_aspect;
1112
1113  aspect_end:
1114     if( p_vout->i_par_num && p_vout->i_par_den )
1115     {
1116         p_vout->fmt_in.i_sar_num *= p_vout->i_par_den;
1117         p_vout->fmt_in.i_sar_den *= p_vout->i_par_num;
1118         p_vout->fmt_in.i_aspect = p_vout->fmt_in.i_aspect *
1119             p_vout->i_par_den / p_vout->i_par_num;
1120         p_vout->render.i_aspect = p_vout->fmt_in.i_aspect;
1121     }
1122
1123     p_vout->i_changes |= VOUT_ASPECT_CHANGE;
1124
1125     vlc_ureduce( &i_aspect_num, &i_aspect_den,
1126                  p_vout->fmt_in.i_aspect, VOUT_ASPECT_FACTOR, 0 );
1127     msg_Dbg( p_vout, "new aspect-ratio %i:%i, sample aspect-ratio %i:%i",
1128              i_aspect_num, i_aspect_den,
1129              p_vout->fmt_in.i_sar_num, p_vout->fmt_in.i_sar_den );
1130
1131     var_Get( p_vout, "crop", &val );
1132     return CropCallback( p_this, "crop", val, val, 0 );
1133
1134     return VLC_SUCCESS;
1135 }
1136
1137 static int OnTopCallback( vlc_object_t *p_this, char const *psz_cmd,
1138                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
1139 {
1140     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1141     playlist_t *p_playlist = pl_Yield( p_this );
1142     vout_Control( p_vout, VOUT_SET_STAY_ON_TOP, newval.b_bool );
1143     (void)psz_cmd; (void)oldval; (void)p_data;
1144
1145     /* Modify playlist as well because the vout might have to be restarted */
1146     var_Create( p_playlist, "video-on-top", VLC_VAR_BOOL );
1147     var_Set( p_playlist, "video-on-top", newval );
1148
1149     pl_Release( p_this );
1150     return VLC_SUCCESS;
1151 }
1152
1153 static int FullscreenCallback( vlc_object_t *p_this, char const *psz_cmd,
1154                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1155 {
1156     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1157     vlc_value_t val;
1158     playlist_t *p_playlist = pl_Yield( p_this );
1159     (void)psz_cmd; (void)oldval; (void)p_data;
1160
1161     p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1162
1163     /* Modify playlist as well because the vout might have to be restarted */
1164     var_Create( p_playlist, "fullscreen", VLC_VAR_BOOL );
1165     var_Set( p_playlist, "fullscreen", newval );
1166     pl_Release( p_playlist );
1167
1168     val.b_bool = VLC_TRUE;
1169     var_Set( p_vout, "intf-change", val );
1170     return VLC_SUCCESS;
1171 }
1172
1173 static int SnapshotCallback( vlc_object_t *p_this, char const *psz_cmd,
1174                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1175 {
1176     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1177     vout_Control( p_vout, VOUT_SNAPSHOT );
1178     (void)psz_cmd; (void)oldval; (void)newval; (void)p_data;
1179     return VLC_SUCCESS;
1180 }
1181
1182 static int TitleCallback( vlc_object_t *p_this, char const *psz_cmd,
1183                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1184 {
1185     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1186
1187     if( !strncmp( psz_cmd, "video-title-show", 16 ) )
1188         p_vout->b_title_show = newval.b_bool;
1189     else if( !strncmp( psz_cmd, "video-title-timeout", 19 ) )
1190         p_vout->i_title_timeout = (mtime_t) newval.i_int;
1191     else if( !strncmp( psz_cmd, "video-title-position", 20 ) )
1192         p_vout->i_title_position = newval.i_int;
1193     return VLC_SUCCESS;
1194 }
1195