]> git.sesse.net Git - vlc/blob - src/video_output/vout_intf.c
Segregate vout window types
[vlc] / src / video_output / vout_intf.c
1 /*****************************************************************************
2  * vout_intf.c : video output interface
3  *****************************************************************************
4  * Copyright (C) 2000-2007 the VideoLAN team
5  *
6  * Authors: Gildas Bazin <gbazin@videolan.org>
7  *
8  * This program is free software; you can redistribute it and/or modify
9  * it under the terms of the GNU General Public License as published by
10  * the Free Software Foundation; either version 2 of the License, or
11  * (at your option) any later version.
12  *
13  * This program is distributed in the hope that it will be useful,
14  * but WITHOUT ANY WARRANTY; without even the implied warranty of
15  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
16  * GNU General Public License for more details.
17  *
18  * You should have received a copy of the GNU General Public License
19  * along with this program; if not, write to the Free Software
20  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
21  *****************************************************************************/
22
23 /*****************************************************************************
24  * Preamble
25  *****************************************************************************/
26
27 #ifdef HAVE_CONFIG_H
28 # include "config.h"
29 #endif
30
31 #include <vlc_common.h>
32
33 #include <stdio.h>
34 #include <stdlib.h>                                                /* free() */
35 #include <sys/types.h>                                          /* opendir() */
36 #include <sys/stat.h>
37 #include <dirent.h>                                             /* opendir() */
38 #include <assert.h>
39 #include <time.h>                                           /* strftime */
40
41 #include <vlc_interface.h>
42 #include <vlc_block.h>
43 #include <vlc_playlist.h>
44
45 #include <vlc_vout.h>
46 #include <vlc_window.h>
47 #include <vlc_image.h>
48 #include <vlc_osd.h>
49 #include <vlc_charset.h>
50
51 #include <vlc_strings.h>
52 #include <vlc_charset.h>
53 #include "../libvlc.h"
54 #include "vout_internal.h"
55
56 /*****************************************************************************
57  * Local prototypes
58  *****************************************************************************/
59 static void InitWindowSize( vout_thread_t *, unsigned *, unsigned * );
60
61 /* Object variables callbacks */
62 static int ZoomCallback( vlc_object_t *, char const *,
63                          vlc_value_t, vlc_value_t, void * );
64 static int CropCallback( vlc_object_t *, char const *,
65                          vlc_value_t, vlc_value_t, void * );
66 static int AspectCallback( vlc_object_t *, char const *,
67                            vlc_value_t, vlc_value_t, void * );
68 static int ScalingCallback( vlc_object_t *, char const *,
69                             vlc_value_t, vlc_value_t, void * );
70 static int OnTopCallback( vlc_object_t *, char const *,
71                           vlc_value_t, vlc_value_t, void * );
72 static int FullscreenCallback( vlc_object_t *, char const *,
73                                vlc_value_t, vlc_value_t, void * );
74 static int SnapshotCallback( vlc_object_t *, char const *,
75                              vlc_value_t, vlc_value_t, void * );
76
77 static int TitleShowCallback( vlc_object_t *, char const *,
78                               vlc_value_t, vlc_value_t, void * );
79 static int TitleTimeoutCallback( vlc_object_t *, char const *,
80                                  vlc_value_t, vlc_value_t, void * );
81 static int TitlePositionCallback( vlc_object_t *, char const *,
82                                   vlc_value_t, vlc_value_t, void * );
83
84 /**
85  * Creates a video output window.
86  * On Unix systems, this is an X11 drawable (handle).
87  * On Windows, this is a Win32 window (handle).
88  * Video output plugins are supposed to called this function and display the
89  * video within the resulting window, while in windowed mode.
90  *
91  * @param p_vout video output thread to create a window for
92  * @param psz_cap VLC module capability (window system type)
93  * @param pi_x_hint pointer to store the recommended horizontal position [OUT]
94  * @param pi_y_hint pointer to store the recommended vertical position [OUT]
95  * @param pi_width_hint pointer to store the recommended width [OUT]
96  * @param pi_height_hint pointer to store the recommended height [OUT]
97  *
98  * @return a vout_window_t object, or NULL in case of failure.
99  * The window is released with vout_ReleaseWindow().
100  */
101 vout_window_t *vout_RequestWindow( vout_thread_t *p_vout, const char *psz_cap,
102                           int *pi_x_hint, int *pi_y_hint,
103                           unsigned int *pi_width_hint,
104                           unsigned int *pi_height_hint )
105 {
106     /* Get requested coordinates */
107     *pi_x_hint = var_GetInteger( p_vout, "video-x" );
108     *pi_y_hint = var_GetInteger( p_vout, "video-y" );
109
110     *pi_width_hint = p_vout->i_window_width;
111     *pi_height_hint = p_vout->i_window_height;
112
113     /* Check whether someone provided us with a window ID */
114     int drawable = var_CreateGetInteger( p_vout, "drawable" );
115     if( drawable ) return (void *)(intptr_t)drawable;
116
117     vout_window_t *wnd = vlc_custom_create (VLC_OBJECT(p_vout), sizeof (*wnd),
118                                             VLC_OBJECT_GENERIC, "window");
119     if (wnd == NULL)
120         return NULL;
121
122     wnd->vout = p_vout;
123     wnd->width = *pi_width_hint;
124     wnd->height = *pi_height_hint;
125     wnd->pos_x = *pi_x_hint;
126     wnd->pos_y = *pi_y_hint;
127     vlc_object_attach (wnd, p_vout);
128
129     wnd->module = module_need (wnd, psz_cap, NULL, false);
130     if (wnd->module == NULL)
131     {
132         msg_Dbg (wnd, "no \"%s\" window provider available", psz_cap);
133         vlc_object_release (wnd);
134         return NULL;
135     }
136     *pi_width_hint = wnd->width;
137     *pi_height_hint = wnd->height;
138     *pi_x_hint = wnd->pos_x;
139     *pi_y_hint = wnd->pos_y;
140     return wnd;
141 }
142
143 /**
144  * Releases a window handle obtained with vout_RequestWindow().
145  * @param p_vout video output thread that allocated the window
146  *               (if this is NULL; this fnction is a no-op).
147  */
148 void vout_ReleaseWindow( vout_window_t *wnd )
149 {
150     if (wnd == NULL)
151         return;
152
153     assert (wnd->module);
154     module_unneed (wnd, wnd->module);
155
156     vlc_object_release (wnd);
157 }
158
159 int vout_ControlWindow( vout_window_t *wnd, int i_query, va_list args )
160 {
161     if (wnd == NULL)
162         return VLC_EGENERIC;
163
164     assert (wnd->control);
165     return wnd->control (wnd, i_query, args);
166 }
167
168 /*****************************************************************************
169  * vout_IntfInit: called during the vout creation to initialise misc things.
170  *****************************************************************************/
171 static const struct
172 {
173     double f_value;
174     const char *psz_label;
175 } p_zoom_values[] = {
176     { 0.25, N_("1:4 Quarter") },
177     { 0.5, N_("1:2 Half") },
178     { 1, N_("1:1 Original") },
179     { 2, N_("2:1 Double") },
180     { 0, NULL } };
181
182 static const struct
183 {
184     const char *psz_value;
185     const char *psz_label;
186 } p_crop_values[] = {
187     { "", N_("Default") },
188     { "16:10", "16:10" },
189     { "16:9", "16:9" },
190     { "185:100", "1.85:1" },
191     { "221:100", "2.21:1" },
192     { "235:100", "2.35:1" },
193     { "239:100", "2.39:1" },
194     { "5:3", "5:3" },
195     { "4:3", "4:3" },
196     { "5:4", "5:4" },
197     { "1:1", "1:1" },
198     { NULL, NULL } };
199
200 static const struct
201 {
202     const char *psz_value;
203     const char *psz_label;
204 } p_aspect_ratio_values[] = {
205     { "", N_("Default") },
206     { "1:1", "1:1" },
207     { "4:3", "4:3" },
208     { "16:9", "16:9" },
209     { "16:10", "16:10" },
210     { "221:100", "2.21:1" },
211     { "5:4", "5:4" },
212     { NULL, NULL } };
213
214 static void AddCustomRatios( vout_thread_t *p_vout, const char *psz_var,
215                              char *psz_list )
216 {
217     if( psz_list && *psz_list )
218     {
219         char *psz_cur = psz_list;
220         char *psz_next;
221         while( psz_cur && *psz_cur )
222         {
223             vlc_value_t val, text;
224             psz_next = strchr( psz_cur, ',' );
225             if( psz_next )
226             {
227                 *psz_next = '\0';
228                 psz_next++;
229             }
230             val.psz_string = psz_cur;
231             text.psz_string = psz_cur;
232             var_Change( p_vout, psz_var, VLC_VAR_ADDCHOICE, &val, &text);
233             psz_cur = psz_next;
234         }
235     }
236 }
237
238 void vout_IntfInit( vout_thread_t *p_vout )
239 {
240     vlc_value_t val, text, old_val;
241     bool b_force_par = false;
242     char *psz_buf;
243     int i;
244
245     /* Create a few object variables we'll need later on */
246     var_Create( p_vout, "snapshot-path", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
247     var_Create( p_vout, "snapshot-prefix", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
248     var_Create( p_vout, "snapshot-format", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
249     var_Create( p_vout, "snapshot-preview", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
250     var_Create( p_vout, "snapshot-sequential",
251                 VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
252     var_Create( p_vout, "snapshot-num", VLC_VAR_INTEGER );
253     var_SetInteger( p_vout, "snapshot-num", 1 );
254     var_Create( p_vout, "snapshot-width", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
255     var_Create( p_vout, "snapshot-height", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
256
257     var_Create( p_vout, "width", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
258     var_Create( p_vout, "height", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
259     p_vout->i_alignment = var_CreateGetInteger( p_vout, "align" );
260
261     var_Create( p_vout, "video-x", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
262     var_Create( p_vout, "video-y", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
263
264     var_Create( p_vout, "mouse-hide-timeout",
265                 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
266
267     p_vout->p->b_title_show = var_CreateGetBool( p_vout, "video-title-show" );
268     p_vout->p->i_title_timeout =
269         (mtime_t)var_CreateGetInteger( p_vout, "video-title-timeout" );
270     p_vout->p->i_title_position =
271         var_CreateGetInteger( p_vout, "video-title-position" );
272     p_vout->p->psz_title =  NULL;
273
274     var_AddCallback( p_vout, "video-title-show", TitleShowCallback, NULL );
275     var_AddCallback( p_vout, "video-title-timeout", TitleTimeoutCallback, NULL );
276     var_AddCallback( p_vout, "video-title-position", TitlePositionCallback, NULL );
277
278     /* Zoom object var */
279     var_Create( p_vout, "zoom", VLC_VAR_FLOAT | VLC_VAR_ISCOMMAND |
280                 VLC_VAR_HASCHOICE | VLC_VAR_DOINHERIT );
281
282     text.psz_string = _("Zoom");
283     var_Change( p_vout, "zoom", VLC_VAR_SETTEXT, &text, NULL );
284
285     var_Get( p_vout, "zoom", &old_val );
286
287     for( i = 0; p_zoom_values[i].f_value; i++ )
288     {
289         if( old_val.f_float == p_zoom_values[i].f_value )
290             var_Change( p_vout, "zoom", VLC_VAR_DELCHOICE, &old_val, NULL );
291         val.f_float = p_zoom_values[i].f_value;
292         text.psz_string = _( p_zoom_values[i].psz_label );
293         var_Change( p_vout, "zoom", VLC_VAR_ADDCHOICE, &val, &text );
294     }
295
296     var_Set( p_vout, "zoom", old_val ); /* Is this really needed? */
297
298     var_AddCallback( p_vout, "zoom", ZoomCallback, NULL );
299
300     /* Crop offset vars */
301     var_Create( p_vout, "crop-left", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
302     var_Create( p_vout, "crop-top", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
303     var_Create( p_vout, "crop-right", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
304     var_Create( p_vout, "crop-bottom", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
305
306     var_AddCallback( p_vout, "crop-left", CropCallback, NULL );
307     var_AddCallback( p_vout, "crop-top", CropCallback, NULL );
308     var_AddCallback( p_vout, "crop-right", CropCallback, NULL );
309     var_AddCallback( p_vout, "crop-bottom", CropCallback, NULL );
310
311     /* Crop object var */
312     var_Create( p_vout, "crop", VLC_VAR_STRING | VLC_VAR_ISCOMMAND |
313                 VLC_VAR_HASCHOICE | VLC_VAR_DOINHERIT );
314
315     text.psz_string = _("Crop");
316     var_Change( p_vout, "crop", VLC_VAR_SETTEXT, &text, NULL );
317
318     val.psz_string = (char*)"";
319     var_Change( p_vout, "crop", VLC_VAR_DELCHOICE, &val, 0 );
320
321     for( i = 0; p_crop_values[i].psz_value; i++ )
322     {
323         val.psz_string = (char*)p_crop_values[i].psz_value;
324         text.psz_string = _( p_crop_values[i].psz_label );
325         var_Change( p_vout, "crop", VLC_VAR_ADDCHOICE, &val, &text );
326     }
327
328     /* update triggered every time the vout's crop parameters are changed */
329     var_Create( p_vout, "crop-update", VLC_VAR_VOID );
330
331     /* Add custom crop ratios */
332     psz_buf = config_GetPsz( p_vout, "custom-crop-ratios" );
333     AddCustomRatios( p_vout, "crop", psz_buf );
334     free( psz_buf );
335
336     var_AddCallback( p_vout, "crop", CropCallback, NULL );
337     var_Get( p_vout, "crop", &old_val );
338     if( old_val.psz_string && *old_val.psz_string )
339         var_TriggerCallback( p_vout, "crop" );
340     free( old_val.psz_string );
341
342     /* Monitor pixel aspect-ratio */
343     var_Create( p_vout, "monitor-par", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
344     var_Get( p_vout, "monitor-par", &val );
345     if( val.psz_string && *val.psz_string )
346     {
347         char *psz_parser = strchr( val.psz_string, ':' );
348         unsigned int i_aspect_num = 0, i_aspect_den = 0;
349         float i_aspect = 0;
350         if( psz_parser )
351         {
352             i_aspect_num = strtol( val.psz_string, 0, 10 );
353             i_aspect_den = strtol( ++psz_parser, 0, 10 );
354         }
355         else
356         {
357             i_aspect = atof( val.psz_string );
358             vlc_ureduce( &i_aspect_num, &i_aspect_den,
359                          i_aspect *VOUT_ASPECT_FACTOR, VOUT_ASPECT_FACTOR, 0 );
360         }
361         if( !i_aspect_num || !i_aspect_den ) i_aspect_num = i_aspect_den = 1;
362
363         p_vout->p->i_par_num = i_aspect_num;
364         p_vout->p->i_par_den = i_aspect_den;
365
366         vlc_ureduce( &p_vout->p->i_par_num, &p_vout->p->i_par_den,
367                      p_vout->p->i_par_num, p_vout->p->i_par_den, 0 );
368
369         msg_Dbg( p_vout, "overriding monitor pixel aspect-ratio: %i:%i",
370                  p_vout->p->i_par_num, p_vout->p->i_par_den );
371         b_force_par = true;
372     }
373     free( val.psz_string );
374
375     /* Aspect-ratio object var */
376     var_Create( p_vout, "aspect-ratio", VLC_VAR_STRING | VLC_VAR_ISCOMMAND |
377                 VLC_VAR_HASCHOICE | VLC_VAR_DOINHERIT );
378
379     text.psz_string = _("Aspect-ratio");
380     var_Change( p_vout, "aspect-ratio", VLC_VAR_SETTEXT, &text, NULL );
381
382     val.psz_string = (char*)"";
383     var_Change( p_vout, "aspect-ratio", VLC_VAR_DELCHOICE, &val, 0 );
384
385     for( i = 0; p_aspect_ratio_values[i].psz_value; i++ )
386     {
387         val.psz_string = (char*)p_aspect_ratio_values[i].psz_value;
388         text.psz_string = _( p_aspect_ratio_values[i].psz_label );
389         var_Change( p_vout, "aspect-ratio", VLC_VAR_ADDCHOICE, &val, &text );
390     }
391
392     /* Add custom aspect ratios */
393     psz_buf = config_GetPsz( p_vout, "custom-aspect-ratios" );
394     AddCustomRatios( p_vout, "aspect-ratio", psz_buf );
395     free( psz_buf );
396
397     var_AddCallback( p_vout, "aspect-ratio", AspectCallback, NULL );
398     var_Get( p_vout, "aspect-ratio", &old_val );
399     if( (old_val.psz_string && *old_val.psz_string) || b_force_par )
400         var_TriggerCallback( p_vout, "aspect-ratio" );
401     free( old_val.psz_string );
402
403     /* Add a variable to indicate if scaling video is activated or not */
404     var_Create( p_vout, "scaling", VLC_VAR_BOOL | VLC_VAR_DOINHERIT
405                 | VLC_VAR_ISCOMMAND );
406     text.psz_string = _("Scaling video");
407     var_Change( p_vout, "scaling", VLC_VAR_SETTEXT, &text, NULL );
408     var_AddCallback( p_vout, "scaling", ScalingCallback, NULL );
409     var_Get( p_vout, "scaling", &val );
410     p_vout->b_scale = val.b_bool;
411
412     /* Initialize the dimensions of the video window */
413     InitWindowSize( p_vout, &p_vout->i_window_width,
414                     &p_vout->i_window_height );
415
416     /* Add a variable to indicate if the window should be on top of others */
417     var_Create( p_vout, "video-on-top", VLC_VAR_BOOL | VLC_VAR_DOINHERIT
418                 | VLC_VAR_ISCOMMAND );
419     text.psz_string = _("Always on top");
420     var_Change( p_vout, "video-on-top", VLC_VAR_SETTEXT, &text, NULL );
421     var_AddCallback( p_vout, "video-on-top", OnTopCallback, NULL );
422
423     /* Add a variable to indicate whether we want window decoration or not */
424     var_Create( p_vout, "video-deco", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
425
426     /* Add a fullscreen variable */
427     if( var_CreateGetBoolCommand( p_vout, "fullscreen" ) )
428     {
429         /* user requested fullscreen */
430         p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
431     }
432     text.psz_string = _("Fullscreen");
433     var_Change( p_vout, "fullscreen", VLC_VAR_SETTEXT, &text, NULL );
434     var_AddCallback( p_vout, "fullscreen", FullscreenCallback, NULL );
435
436     /* Add a snapshot variable */
437     var_Create( p_vout, "video-snapshot", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
438     text.psz_string = _("Snapshot");
439     var_Change( p_vout, "video-snapshot", VLC_VAR_SETTEXT, &text, NULL );
440     var_AddCallback( p_vout, "video-snapshot", SnapshotCallback, NULL );
441
442     /* Mouse coordinates */
443     var_Create( p_vout, "mouse-x", VLC_VAR_INTEGER );
444     var_Create( p_vout, "mouse-y", VLC_VAR_INTEGER );
445     var_Create( p_vout, "mouse-button-down", VLC_VAR_INTEGER );
446     var_Create( p_vout, "mouse-moved", VLC_VAR_BOOL );
447     var_Create( p_vout, "mouse-clicked", VLC_VAR_BOOL );
448
449     var_Create( p_vout, "intf-change", VLC_VAR_BOOL );
450     var_SetBool( p_vout, "intf-change", true );
451 }
452
453 /*****************************************************************************
454  * vout_Snapshot: generates a snapshot.
455  *****************************************************************************/
456 /**
457  * This function will inject a subpicture into the vout with the provided
458  * picture
459  */
460 static int VoutSnapshotPip( vout_thread_t *p_vout, image_handler_t *p_image, picture_t *p_pic, const video_format_t *p_fmt_in )
461 {
462     video_format_t fmt_in = *p_fmt_in;
463     video_format_t fmt_out;
464     picture_t *p_pip;
465     subpicture_t *p_subpic;
466
467     /* */
468     memset( &fmt_out, 0, sizeof(fmt_out) );
469     fmt_out = fmt_in;
470     fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
471
472     /* */
473     p_pip = image_Convert( p_image, p_pic, &fmt_in, &fmt_out );
474     if( !p_pip )
475         return VLC_EGENERIC;
476
477     p_subpic = subpicture_New();
478     if( p_subpic == NULL )
479     {
480          picture_Release( p_pip );
481          return VLC_EGENERIC;
482     }
483
484     p_subpic->i_channel = 0;
485     p_subpic->i_start = mdate();
486     p_subpic->i_stop = mdate() + 4000000;
487     p_subpic->b_ephemer = true;
488     p_subpic->b_fade = true;
489     p_subpic->i_original_picture_width = fmt_out.i_width * 4;
490     p_subpic->i_original_picture_height = fmt_out.i_height * 4;
491     fmt_out.i_aspect = 0;
492     fmt_out.i_sar_num =
493     fmt_out.i_sar_den = 0;
494
495     p_subpic->p_region = subpicture_region_New( &fmt_out );
496     if( p_subpic->p_region )
497     {
498         picture_Release( p_subpic->p_region->p_picture );
499         p_subpic->p_region->p_picture = p_pip;
500     }
501     else
502     {
503         picture_Release( p_pip );
504     }
505
506     spu_DisplaySubpicture( p_vout->p_spu, p_subpic );
507     return VLC_SUCCESS;
508 }
509 /**
510  * This function will return the default directory used for snapshots
511  */
512 static char *VoutSnapshotGetDefaultDirectory( void )
513 {
514     char *psz_path = NULL;
515 #if defined(__APPLE__) || defined(SYS_BEOS)
516
517     if( asprintf( &psz_path, "%s/Desktop",
518                   config_GetHomeDir() ) == -1 )
519         psz_path = NULL;
520
521 #elif defined(WIN32) && !defined(UNDER_CE)
522
523     /* Get the My Pictures folder path */
524     char *p_mypicturesdir = NULL;
525     typedef HRESULT (WINAPI *SHGETFOLDERPATH)( HWND, int, HANDLE, DWORD,
526                                                LPWSTR );
527     #ifndef CSIDL_FLAG_CREATE
528     #   define CSIDL_FLAG_CREATE 0x8000
529     #endif
530     #ifndef CSIDL_MYPICTURES
531     #   define CSIDL_MYPICTURES 0x27
532     #endif
533     #ifndef SHGFP_TYPE_CURRENT
534     #   define SHGFP_TYPE_CURRENT 0
535     #endif
536
537     HINSTANCE shfolder_dll;
538     SHGETFOLDERPATH SHGetFolderPath ;
539
540     /* load the shfolder dll to retrieve SHGetFolderPath */
541     if( ( shfolder_dll = LoadLibrary( _T("SHFolder.dll") ) ) != NULL )
542     {
543        wchar_t wdir[PATH_MAX];
544        SHGetFolderPath = (void *)GetProcAddress( shfolder_dll,
545                                                   _T("SHGetFolderPathW") );
546         if ((SHGetFolderPath != NULL )
547          && SUCCEEDED (SHGetFolderPath (NULL,
548                                        CSIDL_MYPICTURES | CSIDL_FLAG_CREATE,
549                                        NULL, SHGFP_TYPE_CURRENT,
550                                        wdir)))
551             p_mypicturesdir = FromWide (wdir);
552
553         FreeLibrary( shfolder_dll );
554     }
555
556     if( p_mypicturesdir == NULL )
557         psz_path = strdup( config_GetHomeDir() );
558     else
559         psz_path = p_mypicturesdir;
560
561 #else
562
563     /* XXX: This saves in the data directory. Shouldn't we try saving
564      *      to psz_homedir/Desktop or something nicer ? */
565     char *psz_datadir = config_GetUserDataDir();
566     if( psz_datadir )
567     {
568         if( asprintf( &psz_path, "%s", psz_datadir ) == -1 )
569             psz_path = NULL;
570         free( psz_datadir );
571     }
572
573 #endif
574
575     return psz_path;
576 }
577
578 int vout_Snapshot( vout_thread_t *p_vout, picture_t *p_pic )
579 {
580     image_handler_t *p_image = image_HandlerCreate( p_vout );
581     video_format_t fmt_in, fmt_out;
582     char *psz_filename = NULL;
583     vlc_value_t val, format;
584     DIR *path;
585     int i_ret;
586     bool b_embedded_snapshot;
587     void *p_obj;
588
589     /* */
590     val.psz_string = var_GetNonEmptyString( p_vout, "snapshot-path" );
591
592     /* Embedded snapshot : if snapshot-path == object:object_ptr */
593     if( val.psz_string && sscanf( val.psz_string, "object:%p", &p_obj ) > 0 )
594         b_embedded_snapshot = true;
595     else
596         b_embedded_snapshot = false;
597
598     /* */
599     memset( &fmt_in, 0, sizeof(video_format_t) );
600     fmt_in = p_vout->fmt_out;
601     if( fmt_in.i_sar_num <= 0 || fmt_in.i_sar_den <= 0 )
602     {
603         fmt_in.i_sar_num =
604         fmt_in.i_sar_den = 1;
605     }
606
607     /* */
608     memset( &fmt_out, 0, sizeof(video_format_t) );
609     fmt_out.i_sar_num =
610     fmt_out.i_sar_den = 1;
611     fmt_out.i_chroma = b_embedded_snapshot ? VLC_FOURCC('p','n','g',' ') : 0;
612     fmt_out.i_width = var_GetInteger( p_vout, "snapshot-width" );
613     fmt_out.i_height = var_GetInteger( p_vout, "snapshot-height" );
614
615     if( b_embedded_snapshot &&
616         fmt_out.i_width == 0 && fmt_out.i_height == 0 )
617     {
618         /* If snapshot-width and/or snapshot height were not specified,
619            use a default snapshot width of 320 */
620         fmt_out.i_width = 320;
621     }
622
623     if( fmt_out.i_height == 0 && fmt_out.i_width > 0 )
624     {
625         fmt_out.i_height = fmt_in.i_height * fmt_out.i_width / fmt_in.i_width;
626         const int i_height = fmt_out.i_height * fmt_in.i_sar_den / fmt_in.i_sar_num;
627         if( i_height > 0 )
628             fmt_out.i_height = i_height;
629     }
630     else
631     {
632         if( fmt_out.i_width == 0 && fmt_out.i_height > 0 )
633         {
634             fmt_out.i_width = fmt_in.i_width * fmt_out.i_height / fmt_in.i_height;
635         }
636         else
637         {
638             fmt_out.i_width = fmt_in.i_width;
639             fmt_out.i_height = fmt_in.i_height;
640         }
641         const int i_width = fmt_out.i_width * fmt_in.i_sar_num / fmt_in.i_sar_den;
642         if( i_width > 0 )
643             fmt_out.i_width = i_width;
644     }
645
646     /* Embedded snapshot
647        create a snapshot_t* and store it in
648        object_ptr->p_private, then unlock and signal the
649        waiting object.
650      */
651     if( b_embedded_snapshot )
652     {
653         block_t *p_block;
654         snapshot_t *p_snapshot = p_obj;
655         size_t i_size;
656
657         vlc_mutex_lock( &p_snapshot->p_mutex );
658         p_snapshot->p_data = NULL;
659
660         /* Save the snapshot to a memory zone */
661         p_block = image_Write( p_image, p_pic, &fmt_in, &fmt_out );
662         if( !p_block )
663         {
664             msg_Err( p_vout, "Could not get snapshot" );
665             image_HandlerDelete( p_image );
666             vlc_cond_signal( &p_snapshot->p_condvar );
667             vlc_mutex_unlock( &p_snapshot->p_mutex );
668             return VLC_EGENERIC;
669         }
670
671         i_size = p_block->i_buffer;
672
673         p_snapshot->i_width = fmt_out.i_width;
674         p_snapshot->i_height = fmt_out.i_height;
675         p_snapshot->i_datasize = i_size;
676         p_snapshot->date = p_block->i_pts; /* FIXME ?? */
677         p_snapshot->p_data = malloc( i_size );
678         if( !p_snapshot->p_data )
679         {
680             block_Release( p_block );
681             image_HandlerDelete( p_image );
682             vlc_cond_signal( &p_snapshot->p_condvar );
683             vlc_mutex_unlock( &p_snapshot->p_mutex );
684             return VLC_ENOMEM;
685         }
686         memcpy( p_snapshot->p_data, p_block->p_buffer, p_block->i_buffer );
687
688         block_Release( p_block );
689
690         /* Unlock the object */
691         vlc_cond_signal( &p_snapshot->p_condvar );
692         vlc_mutex_unlock( &p_snapshot->p_mutex );
693
694         image_HandlerDelete( p_image );
695         return VLC_SUCCESS;
696     }
697
698     /* Get default directory if none provided */
699     if( !val.psz_string )
700         val.psz_string = VoutSnapshotGetDefaultDirectory( );
701     if( !val.psz_string )
702     {
703         msg_Err( p_vout, "no path specified for snapshots" );
704         image_HandlerDelete( p_image );
705         return VLC_EGENERIC;
706     }
707
708     /* Get snapshot format, default being "png" */
709     format.psz_string = var_GetNonEmptyString( p_vout, "snapshot-format" );
710     if( !format.psz_string )
711         format.psz_string = strdup( "png" );
712     if( !format.psz_string )
713     {
714         free( val.psz_string );
715         image_HandlerDelete( p_image );
716         return VLC_ENOMEM;
717     }
718
719     /*
720      * Did the user specify a directory? If not, path = NULL.
721      */
722     path = utf8_opendir ( (const char *)val.psz_string  );
723     if( path != NULL )
724     {
725         char *psz_prefix = var_GetNonEmptyString( p_vout, "snapshot-prefix" );
726         if( psz_prefix == NULL )
727             psz_prefix = strdup( "vlcsnap-" );
728         else
729         {
730             char *psz_tmp = str_format( p_vout, psz_prefix );
731             filename_sanitize( psz_tmp );
732             free( psz_prefix );
733             psz_prefix = psz_tmp;
734         }
735
736         closedir( path );
737         if( var_GetBool( p_vout, "snapshot-sequential" ) == true )
738         {
739             int i_num = var_GetInteger( p_vout, "snapshot-num" );
740             struct stat st;
741
742             do
743             {
744                 free( psz_filename );
745                 if( asprintf( &psz_filename, "%s" DIR_SEP "%s%05d.%s",
746                               val.psz_string, psz_prefix, i_num++,
747                               format.psz_string ) == -1 )
748                 {
749                     msg_Err( p_vout, "could not create snapshot" );
750                     image_HandlerDelete( p_image );
751                     return VLC_EGENERIC;
752                 }
753             }
754             while( utf8_stat( psz_filename, &st ) == 0 );
755
756             var_SetInteger( p_vout, "snapshot-num", i_num );
757         }
758         else
759         {
760             struct tm    curtime;
761             time_t        lcurtime ;
762             lcurtime = time( NULL ) ;
763             if ( ( localtime_r( &lcurtime, &curtime ) == NULL ) )
764             {
765                 msg_Warn( p_vout, "failed to get current time. Falling back to legacy snapshot naming" );
766                 /* failed to get current time. Fallback to old format */
767                 if( asprintf( &psz_filename, "%s" DIR_SEP "%s%u.%s",
768                           val.psz_string, psz_prefix,
769                           (unsigned int)(p_pic->date / 100000) & 0xFFFFFF,
770                           format.psz_string ) == -1 )
771                 {
772                     msg_Err( p_vout, "could not create snapshot" );
773                     image_HandlerDelete( p_image );
774                     return VLC_EGENERIC;
775                 }
776             }
777             else
778             {
779                 char psz_curtime[15] ;
780                 if( strftime( psz_curtime, 15, "%y%m%d-%H%M%S", &curtime ) == 0 )
781                 {
782                     msg_Warn( p_vout, "snapshot date string truncated" ) ;
783                 }
784                 if( asprintf( &psz_filename, "%s" DIR_SEP "%s%s%1u.%s",
785                       val.psz_string, psz_prefix, psz_curtime,
786                      /* suffix with the last decimal digit in 10s of seconds resolution */
787                      (unsigned int)(p_pic->date / (100*1000)) & 0xFF,
788                       format.psz_string ) == -1 )
789                 {
790                     msg_Err( p_vout, "could not create snapshot" );
791                     image_HandlerDelete( p_image );
792                     return VLC_EGENERIC;
793                 }
794             } //end if time() < 0
795         } //end snapshot sequential
796         free( psz_prefix );
797     }
798     else // The user specified a full path name (including file name)
799     {
800         psz_filename = str_format( p_vout, val.psz_string );
801         path_sanitize( psz_filename );
802     }
803
804     free( val.psz_string );
805     free( format.psz_string );
806
807     /* Save the snapshot */
808     i_ret = image_WriteUrl( p_image, p_pic, &fmt_in, &fmt_out, psz_filename );
809     if( i_ret != VLC_SUCCESS )
810     {
811         msg_Err( p_vout, "could not create snapshot %s", psz_filename );
812         free( psz_filename );
813         image_HandlerDelete( p_image );
814         return VLC_EGENERIC;
815     }
816
817     /* */
818     msg_Dbg( p_vout, "snapshot taken (%s)", psz_filename );
819     vout_OSDMessage( VLC_OBJECT( p_vout ), DEFAULT_CHAN,
820                      "%s", psz_filename );
821
822     /* Generate a media player event  - Right now just trigger a global libvlc var
823         CHECK: Could not find a more local object. The goal is to communicate
824         vout_thread with libvlc_media_player or its input_thread*/
825     val.psz_string =  psz_filename  ;
826
827     var_Set( p_vout->p_libvlc, "vout-snapshottaken", val );
828     /* var_Set duplicates data for transport so we can free*/
829     free( psz_filename );
830
831     /* */
832     if( var_GetBool( p_vout, "snapshot-preview" ) )
833     {
834         if( VoutSnapshotPip( p_vout, p_image, p_pic, &fmt_in ) )
835             msg_Warn( p_vout, "Failed to display snapshot" );
836     }
837     image_HandlerDelete( p_image );
838
839     return VLC_SUCCESS;
840 }
841
842 /*****************************************************************************
843  * Handle filters
844  *****************************************************************************/
845
846 void vout_EnableFilter( vout_thread_t *p_vout, char *psz_name,
847                         bool b_add, bool b_setconfig )
848 {
849     char *psz_parser;
850     char *psz_string = config_GetPsz( p_vout, "vout-filter" );
851
852     /* Todo : Use some generic chain manipulation functions */
853     if( !psz_string ) psz_string = strdup("");
854
855     psz_parser = strstr( psz_string, psz_name );
856     if( b_add )
857     {
858         if( !psz_parser )
859         {
860             psz_parser = psz_string;
861             if( asprintf( &psz_string, (*psz_string) ? "%s:%s" : "%s%s",
862                           psz_string, psz_name ) == -1 )
863             {
864                 free( psz_parser );
865                 return;
866             }
867             free( psz_parser );
868         }
869         else
870             return;
871     }
872     else
873     {
874         if( psz_parser )
875         {
876             memmove( psz_parser, psz_parser + strlen(psz_name) +
877                             (*(psz_parser + strlen(psz_name)) == ':' ? 1 : 0 ),
878                             strlen(psz_parser + strlen(psz_name)) + 1 );
879
880             /* Remove trailing : : */
881             if( *(psz_string+strlen(psz_string ) -1 ) == ':' )
882             {
883                 *(psz_string+strlen(psz_string ) -1 ) = '\0';
884             }
885          }
886          else
887          {
888              free( psz_string );
889              return;
890          }
891     }
892     if( b_setconfig )
893         config_PutPsz( p_vout, "vout-filter", psz_string );
894
895     var_SetString( p_vout, "vout-filter", psz_string );
896     free( psz_string );
897 }
898
899 /*****************************************************************************
900  * vout_ControlDefault: default methods for video output control.
901  *****************************************************************************/
902 int vout_vaControlDefault( vout_thread_t *p_vout, int i_query, va_list args )
903 {
904     (void)args;
905     switch( i_query )
906     {
907     case VOUT_SNAPSHOT:
908         p_vout->p->b_snapshot = true;
909         return VLC_SUCCESS;
910
911     default:
912         msg_Dbg( p_vout, "control query not supported" );
913     }
914     return VLC_EGENERIC;
915 }
916
917 /*****************************************************************************
918  * InitWindowSize: find the initial dimensions the video window should have.
919  *****************************************************************************
920  * This function will check the "width", "height" and "zoom" config options and
921  * will calculate the size that the video window should have.
922  *****************************************************************************/
923 static void InitWindowSize( vout_thread_t *p_vout, unsigned *pi_width,
924                             unsigned *pi_height )
925 {
926     vlc_value_t val;
927     int i_width, i_height;
928     uint64_t ll_zoom;
929
930 #define FP_FACTOR 1000                             /* our fixed point factor */
931
932     var_Get( p_vout, "width", &val );
933     i_width = val.i_int;
934     var_Get( p_vout, "height", &val );
935     i_height = val.i_int;
936     var_Get( p_vout, "zoom", &val );
937     ll_zoom = (uint64_t)( FP_FACTOR * val.f_float );
938
939     if( i_width > 0 && i_height > 0)
940     {
941         *pi_width = (int)( i_width * ll_zoom / FP_FACTOR );
942         *pi_height = (int)( i_height * ll_zoom / FP_FACTOR );
943         goto initwsize_end;
944     }
945     else if( i_width > 0 )
946     {
947         *pi_width = (int)( i_width * ll_zoom / FP_FACTOR );
948         *pi_height = (int)( p_vout->fmt_in.i_visible_height * ll_zoom *
949             p_vout->fmt_in.i_sar_den * i_width / p_vout->fmt_in.i_sar_num /
950             FP_FACTOR / p_vout->fmt_in.i_visible_width );
951         goto initwsize_end;
952     }
953     else if( i_height > 0 )
954     {
955         *pi_height = (int)( i_height * ll_zoom / FP_FACTOR );
956         *pi_width = (int)( p_vout->fmt_in.i_visible_width * ll_zoom *
957             p_vout->fmt_in.i_sar_num * i_height / p_vout->fmt_in.i_sar_den /
958             FP_FACTOR / p_vout->fmt_in.i_visible_height );
959         goto initwsize_end;
960     }
961
962     if( p_vout->fmt_in.i_sar_num == 0 || p_vout->fmt_in.i_sar_den == 0 ) {
963         msg_Warn( p_vout, "fucked up aspect" );
964         *pi_width = (int)( p_vout->fmt_in.i_visible_width * ll_zoom / FP_FACTOR );
965         *pi_height = (int)( p_vout->fmt_in.i_visible_height * ll_zoom /FP_FACTOR);
966     }
967     else if( p_vout->fmt_in.i_sar_num >= p_vout->fmt_in.i_sar_den )
968     {
969         *pi_width = (int)( p_vout->fmt_in.i_visible_width * ll_zoom *
970             p_vout->fmt_in.i_sar_num / p_vout->fmt_in.i_sar_den / FP_FACTOR );
971         *pi_height = (int)( p_vout->fmt_in.i_visible_height * ll_zoom
972             / FP_FACTOR );
973     }
974     else
975     {
976         *pi_width = (int)( p_vout->fmt_in.i_visible_width * ll_zoom
977             / FP_FACTOR );
978         *pi_height = (int)( p_vout->fmt_in.i_visible_height * ll_zoom *
979             p_vout->fmt_in.i_sar_den / p_vout->fmt_in.i_sar_num / FP_FACTOR );
980     }
981
982 initwsize_end:
983     msg_Dbg( p_vout, "window size: %dx%d", p_vout->i_window_width,
984              p_vout->i_window_height );
985
986 #undef FP_FACTOR
987 }
988
989 /*****************************************************************************
990  * Object variables callbacks
991  *****************************************************************************/
992 static int ZoomCallback( vlc_object_t *p_this, char const *psz_cmd,
993                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
994 {
995     vout_thread_t *p_vout = (vout_thread_t *)p_this;
996     (void)psz_cmd; (void)oldval; (void)newval; (void)p_data;
997     InitWindowSize( p_vout, &p_vout->i_window_width,
998                     &p_vout->i_window_height );
999     vout_Control( p_vout, VOUT_SET_SIZE, p_vout->i_window_width,
1000                   p_vout->i_window_height );
1001     return VLC_SUCCESS;
1002 }
1003
1004 static int CropCallback( vlc_object_t *p_this, char const *psz_cmd,
1005                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
1006 {
1007     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1008     int64_t i_aspect_num, i_aspect_den;
1009     unsigned int i_width, i_height;
1010
1011     (void)oldval; (void)p_data;
1012
1013     /* Restore defaults */
1014     p_vout->fmt_in.i_x_offset = p_vout->fmt_render.i_x_offset;
1015     p_vout->fmt_in.i_visible_width = p_vout->fmt_render.i_visible_width;
1016     p_vout->fmt_in.i_y_offset = p_vout->fmt_render.i_y_offset;
1017     p_vout->fmt_in.i_visible_height = p_vout->fmt_render.i_visible_height;
1018
1019     if( !strcmp( psz_cmd, "crop" ) )
1020     {
1021         char *psz_end = NULL, *psz_parser = strchr( newval.psz_string, ':' );
1022         if( psz_parser )
1023         {
1024             /* We're using the 3:4 syntax */
1025             i_aspect_num = strtol( newval.psz_string, &psz_end, 10 );
1026             if( psz_end == newval.psz_string || !i_aspect_num ) goto crop_end;
1027
1028             i_aspect_den = strtol( ++psz_parser, &psz_end, 10 );
1029             if( psz_end == psz_parser || !i_aspect_den ) goto crop_end;
1030
1031             i_width = p_vout->fmt_in.i_sar_den*p_vout->fmt_render.i_visible_height *
1032                 i_aspect_num / i_aspect_den / p_vout->fmt_in.i_sar_num;
1033             i_height = p_vout->fmt_render.i_visible_width*p_vout->fmt_in.i_sar_num *
1034                 i_aspect_den / i_aspect_num / p_vout->fmt_in.i_sar_den;
1035
1036             if( i_width < p_vout->fmt_render.i_visible_width )
1037             {
1038                 p_vout->fmt_in.i_x_offset = p_vout->fmt_render.i_x_offset +
1039                     (p_vout->fmt_render.i_visible_width - i_width) / 2;
1040                 p_vout->fmt_in.i_visible_width = i_width;
1041             }
1042             else
1043             {
1044                 p_vout->fmt_in.i_y_offset = p_vout->fmt_render.i_y_offset +
1045                     (p_vout->fmt_render.i_visible_height - i_height) / 2;
1046                 p_vout->fmt_in.i_visible_height = i_height;
1047             }
1048         }
1049         else
1050         {
1051             psz_parser = strchr( newval.psz_string, 'x' );
1052             if( psz_parser )
1053             {
1054                 /* Maybe we're using the <width>x<height>+<left>+<top> syntax */
1055                 unsigned int i_crop_width, i_crop_height, i_crop_top, i_crop_left;
1056
1057                 i_crop_width = strtol( newval.psz_string, &psz_end, 10 );
1058                 if( psz_end != psz_parser ) goto crop_end;
1059
1060                 psz_parser = strchr( ++psz_end, '+' );
1061                 i_crop_height = strtol( psz_end, &psz_end, 10 );
1062                 if( psz_end != psz_parser ) goto crop_end;
1063
1064                 psz_parser = strchr( ++psz_end, '+' );
1065                 i_crop_left = strtol( psz_end, &psz_end, 10 );
1066                 if( psz_end != psz_parser ) goto crop_end;
1067
1068                 psz_end++;
1069                 i_crop_top = strtol( psz_end, &psz_end, 10 );
1070                 if( *psz_end != '\0' ) goto crop_end;
1071
1072                 i_width = i_crop_width;
1073                 p_vout->fmt_in.i_visible_width = i_width;
1074
1075                 i_height = i_crop_height;
1076                 p_vout->fmt_in.i_visible_height = i_height;
1077
1078                 p_vout->fmt_in.i_x_offset = i_crop_left;
1079                 p_vout->fmt_in.i_y_offset = i_crop_top;
1080             }
1081             else
1082             {
1083                 /* Maybe we're using the <left>+<top>+<right>+<bottom> syntax */
1084                 unsigned int i_crop_top, i_crop_left, i_crop_bottom, i_crop_right;
1085
1086                 psz_parser = strchr( newval.psz_string, '+' );
1087                 i_crop_left = strtol( newval.psz_string, &psz_end, 10 );
1088                 if( psz_end != psz_parser ) goto crop_end;
1089
1090                 psz_parser = strchr( ++psz_end, '+' );
1091                 i_crop_top = strtol( psz_end, &psz_end, 10 );
1092                 if( psz_end != psz_parser ) goto crop_end;
1093
1094                 psz_parser = strchr( ++psz_end, '+' );
1095                 i_crop_right = strtol( psz_end, &psz_end, 10 );
1096                 if( psz_end != psz_parser ) goto crop_end;
1097
1098                 psz_end++;
1099                 i_crop_bottom = strtol( psz_end, &psz_end, 10 );
1100                 if( *psz_end != '\0' ) goto crop_end;
1101
1102                 i_width = p_vout->fmt_render.i_visible_width
1103                           - i_crop_left - i_crop_right;
1104                 p_vout->fmt_in.i_visible_width = i_width;
1105
1106                 i_height = p_vout->fmt_render.i_visible_height
1107                            - i_crop_top - i_crop_bottom;
1108                 p_vout->fmt_in.i_visible_height = i_height;
1109
1110                 p_vout->fmt_in.i_x_offset = i_crop_left;
1111                 p_vout->fmt_in.i_y_offset = i_crop_top;
1112             }
1113         }
1114     }
1115     else if( !strcmp( psz_cmd, "crop-top" )
1116           || !strcmp( psz_cmd, "crop-left" )
1117           || !strcmp( psz_cmd, "crop-bottom" )
1118           || !strcmp( psz_cmd, "crop-right" ) )
1119     {
1120         unsigned int i_crop_top, i_crop_left, i_crop_bottom, i_crop_right;
1121
1122         i_crop_top = var_GetInteger( p_vout, "crop-top" );
1123         i_crop_left = var_GetInteger( p_vout, "crop-left" );
1124         i_crop_right = var_GetInteger( p_vout, "crop-right" );
1125         i_crop_bottom = var_GetInteger( p_vout, "crop-bottom" );
1126
1127         i_width = p_vout->fmt_render.i_visible_width
1128                   - i_crop_left - i_crop_right;
1129         p_vout->fmt_in.i_visible_width = i_width;
1130
1131         i_height = p_vout->fmt_render.i_visible_height
1132                    - i_crop_top - i_crop_bottom;
1133         p_vout->fmt_in.i_visible_height = i_height;
1134
1135         p_vout->fmt_in.i_x_offset = i_crop_left;
1136         p_vout->fmt_in.i_y_offset = i_crop_top;
1137     }
1138
1139  crop_end:
1140     InitWindowSize( p_vout, &p_vout->i_window_width,
1141                     &p_vout->i_window_height );
1142
1143     p_vout->i_changes |= VOUT_CROP_CHANGE;
1144
1145     msg_Dbg( p_vout, "cropping picture %ix%i to %i,%i,%ix%i",
1146              p_vout->fmt_in.i_width, p_vout->fmt_in.i_height,
1147              p_vout->fmt_in.i_x_offset, p_vout->fmt_in.i_y_offset,
1148              p_vout->fmt_in.i_visible_width,
1149              p_vout->fmt_in.i_visible_height );
1150
1151     var_SetVoid( p_vout, "crop-update" );
1152
1153     return VLC_SUCCESS;
1154 }
1155
1156 static int AspectCallback( vlc_object_t *p_this, char const *psz_cmd,
1157                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
1158 {
1159     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1160     unsigned int i_aspect_num, i_aspect_den, i_sar_num, i_sar_den;
1161     vlc_value_t val;
1162
1163     char *psz_end, *psz_parser = strchr( newval.psz_string, ':' );
1164     (void)psz_cmd; (void)oldval; (void)p_data;
1165
1166     /* Restore defaults */
1167     p_vout->fmt_in.i_sar_num = p_vout->fmt_render.i_sar_num;
1168     p_vout->fmt_in.i_sar_den = p_vout->fmt_render.i_sar_den;
1169     p_vout->fmt_in.i_aspect = p_vout->fmt_render.i_aspect;
1170     p_vout->render.i_aspect = p_vout->fmt_render.i_aspect;
1171
1172     if( !psz_parser ) goto aspect_end;
1173
1174     i_aspect_num = strtol( newval.psz_string, &psz_end, 10 );
1175     if( psz_end == newval.psz_string || !i_aspect_num ) goto aspect_end;
1176
1177     i_aspect_den = strtol( ++psz_parser, &psz_end, 10 );
1178     if( psz_end == psz_parser || !i_aspect_den ) goto aspect_end;
1179
1180     i_sar_num = i_aspect_num * p_vout->fmt_render.i_visible_height;
1181     i_sar_den = i_aspect_den * p_vout->fmt_render.i_visible_width;
1182     vlc_ureduce( &i_sar_num, &i_sar_den, i_sar_num, i_sar_den, 0 );
1183     p_vout->fmt_in.i_sar_num = i_sar_num;
1184     p_vout->fmt_in.i_sar_den = i_sar_den;
1185     p_vout->fmt_in.i_aspect = i_aspect_num * VOUT_ASPECT_FACTOR / i_aspect_den;
1186     p_vout->render.i_aspect = p_vout->fmt_in.i_aspect;
1187
1188  aspect_end:
1189     if( p_vout->p->i_par_num && p_vout->p->i_par_den )
1190     {
1191         p_vout->fmt_in.i_sar_num *= p_vout->p->i_par_den;
1192         p_vout->fmt_in.i_sar_den *= p_vout->p->i_par_num;
1193         p_vout->fmt_in.i_aspect = p_vout->fmt_in.i_aspect *
1194             p_vout->p->i_par_den / p_vout->p->i_par_num;
1195         p_vout->render.i_aspect = p_vout->fmt_in.i_aspect;
1196     }
1197
1198     p_vout->i_changes |= VOUT_ASPECT_CHANGE;
1199
1200     vlc_ureduce( &i_aspect_num, &i_aspect_den,
1201                  p_vout->fmt_in.i_aspect, VOUT_ASPECT_FACTOR, 0 );
1202     msg_Dbg( p_vout, "new aspect-ratio %i:%i, sample aspect-ratio %i:%i",
1203              i_aspect_num, i_aspect_den,
1204              p_vout->fmt_in.i_sar_num, p_vout->fmt_in.i_sar_den );
1205
1206     if( var_Get( p_vout, "crop", &val ) )
1207         return VLC_EGENERIC;
1208
1209     int i_ret = CropCallback( p_this, "crop", val, val, 0 );
1210     free( val.psz_string );
1211     return i_ret;
1212 }
1213
1214 static int ScalingCallback( vlc_object_t *p_this, char const *psz_cmd,
1215                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
1216 {
1217     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1218
1219     vlc_mutex_lock( &p_vout->change_lock );
1220
1221     p_vout->b_scale = newval.b_bool;
1222     p_vout->i_changes |= VOUT_SIZE_CHANGE;
1223
1224     vlc_mutex_unlock( &p_vout->change_lock );
1225
1226     return VLC_SUCCESS;
1227 }
1228
1229 static int OnTopCallback( vlc_object_t *p_this, char const *psz_cmd,
1230                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
1231 {
1232     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1233     vout_Control( p_vout, VOUT_SET_STAY_ON_TOP, newval.b_bool );
1234     (void)psz_cmd; (void)oldval; (void)p_data;
1235
1236     /* Modify libvlc as well because the vout might have to be restarted */
1237     var_Create( p_vout->p_libvlc, "video-on-top", VLC_VAR_BOOL );
1238     var_Set( p_vout->p_libvlc, "video-on-top", newval );
1239
1240     return VLC_SUCCESS;
1241 }
1242
1243 static int FullscreenCallback( vlc_object_t *p_this, char const *psz_cmd,
1244                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1245 {
1246     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1247     vlc_value_t val;
1248     (void)psz_cmd; (void)oldval; (void)p_data;
1249
1250     p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
1251
1252     /* Modify libvlc as well because the vout might have to be restarted */
1253     var_Create( p_vout->p_libvlc, "fullscreen", VLC_VAR_BOOL );
1254     var_Set( p_vout->p_libvlc, "fullscreen", newval );
1255
1256     val.b_bool = true;
1257     var_Set( p_vout, "intf-change", val );
1258     return VLC_SUCCESS;
1259 }
1260
1261 static int SnapshotCallback( vlc_object_t *p_this, char const *psz_cmd,
1262                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1263 {
1264     VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
1265     VLC_UNUSED(newval); VLC_UNUSED(p_data);
1266     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1267     vout_Control( p_vout, VOUT_SNAPSHOT );
1268     return VLC_SUCCESS;
1269 }
1270
1271 static int TitleShowCallback( vlc_object_t *p_this, char const *psz_cmd,
1272                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1273 {
1274     VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
1275     VLC_UNUSED(p_data);
1276     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1277     p_vout->p->b_title_show = newval.b_bool;
1278     return VLC_SUCCESS;
1279 }
1280
1281 static int TitleTimeoutCallback( vlc_object_t *p_this, char const *psz_cmd,
1282                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1283 {
1284     VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(p_data);
1285     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1286     p_vout->p->i_title_timeout = (mtime_t) newval.i_int;
1287     return VLC_SUCCESS;
1288 }
1289
1290 static int TitlePositionCallback( vlc_object_t *p_this, char const *psz_cmd,
1291                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1292 {
1293     VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
1294     VLC_UNUSED(p_data);
1295     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1296     p_vout->p->i_title_position = newval.i_int;
1297     return VLC_SUCCESS;
1298 }