]> git.sesse.net Git - vlc/blob - src/video_output/vout_intf.c
"fullscreen" callback: do nothing if value is unchanged
[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 <dirent.h>                                             /* opendir() */
37 #include <assert.h>
38 #include <time.h>                                           /* strftime */
39
40 #include <vlc_interface.h>
41 #include <vlc_block.h>
42 #include <vlc_playlist.h>
43
44 #include <vlc_vout.h>
45 #include <vlc_image.h>
46 #include <vlc_osd.h>
47 #include <vlc_strings.h>
48 #include <vlc_charset.h>
49 #include "../libvlc.h"
50 #include "vout_internal.h"
51
52 /*****************************************************************************
53  * Local prototypes
54  *****************************************************************************/
55 static void InitWindowSize( vout_thread_t *, unsigned *, unsigned * );
56
57 /* Object variables callbacks */
58 static int ZoomCallback( vlc_object_t *, char const *,
59                          vlc_value_t, vlc_value_t, void * );
60 static int CropCallback( vlc_object_t *, char const *,
61                          vlc_value_t, vlc_value_t, void * );
62 static int AspectCallback( vlc_object_t *, char const *,
63                            vlc_value_t, vlc_value_t, void * );
64 static int ScalingCallback( vlc_object_t *, char const *,
65                             vlc_value_t, vlc_value_t, void * );
66 static int OnTopCallback( vlc_object_t *, char const *,
67                           vlc_value_t, vlc_value_t, void * );
68 static int FullscreenCallback( vlc_object_t *, char const *,
69                                vlc_value_t, vlc_value_t, void * );
70 static int SnapshotCallback( vlc_object_t *, char const *,
71                              vlc_value_t, vlc_value_t, void * );
72
73 static int TitleShowCallback( vlc_object_t *, char const *,
74                               vlc_value_t, vlc_value_t, void * );
75 static int TitleTimeoutCallback( vlc_object_t *, char const *,
76                                  vlc_value_t, vlc_value_t, void * );
77 static int TitlePositionCallback( vlc_object_t *, char const *,
78                                   vlc_value_t, vlc_value_t, void * );
79
80 /*****************************************************************************
81  * vout_IntfInit: called during the vout creation to initialise misc things.
82  *****************************************************************************/
83 static const struct
84 {
85     double f_value;
86     const char *psz_label;
87 } p_zoom_values[] = {
88     { 0.25, N_("1:4 Quarter") },
89     { 0.5, N_("1:2 Half") },
90     { 1, N_("1:1 Original") },
91     { 2, N_("2:1 Double") },
92     { 0, NULL } };
93
94 static const struct
95 {
96     const char *psz_value;
97     const char *psz_label;
98 } p_crop_values[] = {
99     { "", N_("Default") },
100     { "16:10", "16:10" },
101     { "16:9", "16:9" },
102     { "185:100", "1.85:1" },
103     { "221:100", "2.21:1" },
104     { "235:100", "2.35:1" },
105     { "239:100", "2.39:1" },
106     { "5:3", "5:3" },
107     { "4:3", "4:3" },
108     { "5:4", "5:4" },
109     { "1:1", "1:1" },
110     { NULL, NULL } };
111
112 static const struct
113 {
114     const char *psz_value;
115     const char *psz_label;
116 } p_aspect_ratio_values[] = {
117     { "", N_("Default") },
118     { "1:1", "1:1" },
119     { "4:3", "4:3" },
120     { "16:9", "16:9" },
121     { "16:10", "16:10" },
122     { "221:100", "2.21:1" },
123     { "5:4", "5:4" },
124     { NULL, NULL } };
125
126 static void AddCustomRatios( vout_thread_t *p_vout, const char *psz_var,
127                              char *psz_list )
128 {
129     assert( psz_list );
130
131     char *psz_cur = psz_list;
132     char *psz_next;
133     while( psz_cur && *psz_cur )
134     {
135         vlc_value_t val, text;
136         psz_next = strchr( psz_cur, ',' );
137         if( psz_next )
138         {
139             *psz_next = '\0';
140             psz_next++;
141         }
142         val.psz_string = psz_cur;
143         text.psz_string = psz_cur;
144         var_Change( p_vout, psz_var, VLC_VAR_ADDCHOICE, &val, &text);
145         psz_cur = psz_next;
146     }
147 }
148
149 void vout_IntfInit( vout_thread_t *p_vout )
150 {
151     vlc_value_t val, text, old_val;
152     bool b_force_par = false;
153     char *psz_buf;
154     int i;
155
156     /* Create a few object variables we'll need later on */
157     var_Create( p_vout, "snapshot-path", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
158     var_Create( p_vout, "snapshot-prefix", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
159     var_Create( p_vout, "snapshot-format", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
160     var_Create( p_vout, "snapshot-preview", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
161     var_Create( p_vout, "snapshot-sequential",
162                 VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
163     var_Create( p_vout, "snapshot-num", VLC_VAR_INTEGER );
164     var_SetInteger( p_vout, "snapshot-num", 1 );
165     var_Create( p_vout, "snapshot-width", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
166     var_Create( p_vout, "snapshot-height", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
167
168     var_Create( p_vout, "width", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
169     var_Create( p_vout, "height", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
170     p_vout->i_alignment = var_CreateGetInteger( p_vout, "align" );
171
172     var_Create( p_vout, "video-x", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
173     var_Create( p_vout, "video-y", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
174
175     var_Create( p_vout, "mouse-hide-timeout",
176                 VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
177
178     p_vout->p->b_title_show = var_CreateGetBool( p_vout, "video-title-show" );
179     p_vout->p->i_title_timeout =
180         (mtime_t)var_CreateGetInteger( p_vout, "video-title-timeout" );
181     p_vout->p->i_title_position =
182         var_CreateGetInteger( p_vout, "video-title-position" );
183     p_vout->p->psz_title =  NULL;
184
185     var_AddCallback( p_vout, "video-title-show", TitleShowCallback, NULL );
186     var_AddCallback( p_vout, "video-title-timeout", TitleTimeoutCallback, NULL );
187     var_AddCallback( p_vout, "video-title-position", TitlePositionCallback, NULL );
188
189     /* Zoom object var */
190     var_Create( p_vout, "zoom", VLC_VAR_FLOAT | VLC_VAR_ISCOMMAND |
191                 VLC_VAR_HASCHOICE | VLC_VAR_DOINHERIT );
192
193     text.psz_string = _("Zoom");
194     var_Change( p_vout, "zoom", VLC_VAR_SETTEXT, &text, NULL );
195
196     var_Get( p_vout, "zoom", &old_val );
197
198     for( i = 0; p_zoom_values[i].f_value; i++ )
199     {
200         if( old_val.f_float == p_zoom_values[i].f_value )
201             var_Change( p_vout, "zoom", VLC_VAR_DELCHOICE, &old_val, NULL );
202         val.f_float = p_zoom_values[i].f_value;
203         text.psz_string = _( p_zoom_values[i].psz_label );
204         var_Change( p_vout, "zoom", VLC_VAR_ADDCHOICE, &val, &text );
205     }
206
207     var_Set( p_vout, "zoom", old_val ); /* Is this really needed? */
208
209     var_AddCallback( p_vout, "zoom", ZoomCallback, NULL );
210
211     /* Crop offset vars */
212     var_Create( p_vout, "crop-left", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
213     var_Create( p_vout, "crop-top", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
214     var_Create( p_vout, "crop-right", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
215     var_Create( p_vout, "crop-bottom", VLC_VAR_INTEGER | VLC_VAR_ISCOMMAND );
216
217     var_AddCallback( p_vout, "crop-left", CropCallback, NULL );
218     var_AddCallback( p_vout, "crop-top", CropCallback, NULL );
219     var_AddCallback( p_vout, "crop-right", CropCallback, NULL );
220     var_AddCallback( p_vout, "crop-bottom", CropCallback, NULL );
221
222     /* Crop object var */
223     var_Create( p_vout, "crop", VLC_VAR_STRING | VLC_VAR_ISCOMMAND |
224                 VLC_VAR_HASCHOICE | VLC_VAR_DOINHERIT );
225
226     text.psz_string = _("Crop");
227     var_Change( p_vout, "crop", VLC_VAR_SETTEXT, &text, NULL );
228
229     val.psz_string = (char*)"";
230     var_Change( p_vout, "crop", VLC_VAR_DELCHOICE, &val, 0 );
231
232     for( i = 0; p_crop_values[i].psz_value; i++ )
233     {
234         val.psz_string = (char*)p_crop_values[i].psz_value;
235         text.psz_string = _( p_crop_values[i].psz_label );
236         var_Change( p_vout, "crop", VLC_VAR_ADDCHOICE, &val, &text );
237     }
238
239     /* update triggered every time the vout's crop parameters are changed */
240     var_Create( p_vout, "crop-update", VLC_VAR_VOID );
241
242     /* Add custom crop ratios */
243     psz_buf = var_CreateGetNonEmptyString( p_vout, "custom-crop-ratios" );
244     if( psz_buf )
245     {
246         AddCustomRatios( p_vout, "crop", psz_buf );
247         free( psz_buf );
248     }
249
250     var_AddCallback( p_vout, "crop", CropCallback, NULL );
251     var_Get( p_vout, "crop", &old_val );
252     if( old_val.psz_string && *old_val.psz_string )
253         var_TriggerCallback( p_vout, "crop" );
254     free( old_val.psz_string );
255
256     /* Monitor pixel aspect-ratio */
257     var_Create( p_vout, "monitor-par", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
258     var_Get( p_vout, "monitor-par", &val );
259     if( val.psz_string && *val.psz_string )
260     {
261         char *psz_parser = strchr( val.psz_string, ':' );
262         unsigned int i_aspect_num = 0, i_aspect_den = 0;
263         float i_aspect = 0;
264         if( psz_parser )
265         {
266             i_aspect_num = strtol( val.psz_string, 0, 10 );
267             i_aspect_den = strtol( ++psz_parser, 0, 10 );
268         }
269         else
270         {
271             i_aspect = us_atof( val.psz_string );
272             vlc_ureduce( &i_aspect_num, &i_aspect_den,
273                          i_aspect *VOUT_ASPECT_FACTOR, VOUT_ASPECT_FACTOR, 0 );
274         }
275         if( !i_aspect_num || !i_aspect_den ) i_aspect_num = i_aspect_den = 1;
276
277         p_vout->p->i_par_num = i_aspect_num;
278         p_vout->p->i_par_den = i_aspect_den;
279
280         vlc_ureduce( &p_vout->p->i_par_num, &p_vout->p->i_par_den,
281                      p_vout->p->i_par_num, p_vout->p->i_par_den, 0 );
282
283         msg_Dbg( p_vout, "overriding monitor pixel aspect-ratio: %i:%i",
284                  p_vout->p->i_par_num, p_vout->p->i_par_den );
285         b_force_par = true;
286     }
287     free( val.psz_string );
288
289     /* Aspect-ratio object var */
290     var_Create( p_vout, "aspect-ratio", VLC_VAR_STRING | VLC_VAR_ISCOMMAND |
291                 VLC_VAR_HASCHOICE | VLC_VAR_DOINHERIT );
292
293     text.psz_string = _("Aspect-ratio");
294     var_Change( p_vout, "aspect-ratio", VLC_VAR_SETTEXT, &text, NULL );
295
296     val.psz_string = (char*)"";
297     var_Change( p_vout, "aspect-ratio", VLC_VAR_DELCHOICE, &val, 0 );
298
299     for( i = 0; p_aspect_ratio_values[i].psz_value; i++ )
300     {
301         val.psz_string = (char*)p_aspect_ratio_values[i].psz_value;
302         text.psz_string = _( p_aspect_ratio_values[i].psz_label );
303         var_Change( p_vout, "aspect-ratio", VLC_VAR_ADDCHOICE, &val, &text );
304     }
305
306     /* Add custom aspect ratios */
307     psz_buf = var_CreateGetNonEmptyString( p_vout, "custom-aspect-ratios" );
308     if( psz_buf )
309     {
310         AddCustomRatios( p_vout, "aspect-ratio", psz_buf );
311         free( psz_buf );
312     }
313
314     var_AddCallback( p_vout, "aspect-ratio", AspectCallback, NULL );
315     var_Get( p_vout, "aspect-ratio", &old_val );
316     if( (old_val.psz_string && *old_val.psz_string) || b_force_par )
317         var_TriggerCallback( p_vout, "aspect-ratio" );
318     free( old_val.psz_string );
319
320     /* Add variables to manage scaling video */
321     var_Create( p_vout, "autoscale", VLC_VAR_BOOL | VLC_VAR_DOINHERIT
322                 | VLC_VAR_ISCOMMAND );
323     text.psz_string = _("Autoscale video");
324     var_Change( p_vout, "autoscale", VLC_VAR_SETTEXT, &text, NULL );
325     var_AddCallback( p_vout, "autoscale", ScalingCallback, NULL );
326     p_vout->b_autoscale = var_GetBool( p_vout, "autoscale" );
327
328     var_Create( p_vout, "scale", VLC_VAR_FLOAT | VLC_VAR_DOINHERIT
329                 | VLC_VAR_ISCOMMAND );
330     text.psz_string = _("Scale factor");
331     var_Change( p_vout, "scale", VLC_VAR_SETTEXT, &text, NULL );
332     var_AddCallback( p_vout, "scale", ScalingCallback, NULL );
333     p_vout->i_zoom = (int)( ZOOM_FP_FACTOR * var_GetFloat( p_vout, "scale" ) );
334
335     /* Initialize the dimensions of the video window */
336     InitWindowSize( p_vout, &p_vout->i_window_width,
337                     &p_vout->i_window_height );
338
339     /* Add a variable to indicate if the window should be on top of others */
340     var_Create( p_vout, "video-on-top", VLC_VAR_BOOL | VLC_VAR_DOINHERIT
341                 | VLC_VAR_ISCOMMAND );
342     text.psz_string = _("Always on top");
343     var_Change( p_vout, "video-on-top", VLC_VAR_SETTEXT, &text, NULL );
344     var_AddCallback( p_vout, "video-on-top", OnTopCallback, NULL );
345
346     /* Add a variable to indicate whether we want window decoration or not */
347     var_Create( p_vout, "video-deco", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
348
349     /* Add a fullscreen variable */
350     if( var_CreateGetBoolCommand( p_vout, "fullscreen" ) )
351     {
352         /* user requested fullscreen */
353         p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
354     }
355     text.psz_string = _("Fullscreen");
356     var_Change( p_vout, "fullscreen", VLC_VAR_SETTEXT, &text, NULL );
357     var_AddCallback( p_vout, "fullscreen", FullscreenCallback, NULL );
358
359     /* Add a snapshot variable */
360     var_Create( p_vout, "video-snapshot", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
361     text.psz_string = _("Snapshot");
362     var_Change( p_vout, "video-snapshot", VLC_VAR_SETTEXT, &text, NULL );
363     var_AddCallback( p_vout, "video-snapshot", SnapshotCallback, NULL );
364
365     /* Mouse coordinates */
366     var_Create( p_vout, "mouse-x", VLC_VAR_INTEGER );
367     var_Create( p_vout, "mouse-y", VLC_VAR_INTEGER );
368     var_Create( p_vout, "mouse-button-down", VLC_VAR_INTEGER );
369     var_Create( p_vout, "mouse-moved", VLC_VAR_BOOL );
370     var_Create( p_vout, "mouse-clicked", VLC_VAR_BOOL );
371
372     var_Create( p_vout, "intf-change", VLC_VAR_BOOL );
373     var_SetBool( p_vout, "intf-change", true );
374 }
375
376 /*****************************************************************************
377  * vout_Snapshot: generates a snapshot.
378  *****************************************************************************/
379 /**
380  * This function will inject a subpicture into the vout with the provided
381  * picture
382  */
383 static int VoutSnapshotPip( vout_thread_t *p_vout, picture_t *p_pic )
384 {
385     subpicture_t *p_subpic = subpicture_NewFromPicture( VLC_OBJECT(p_vout),
386                                                         p_pic, VLC_CODEC_YUVA );
387     if( !p_subpic )
388         return VLC_EGENERIC;
389
390     /* FIXME DEFAULT_CHAN is not good (used by the text) but
391      * hardcoded 0 doesn't seem right */
392     p_subpic->i_channel = 0;
393     p_subpic->i_start = mdate();
394     p_subpic->i_stop  = p_subpic->i_start + 4000000;
395     p_subpic->b_ephemer = true;
396     p_subpic->b_fade = true;
397
398     /* Reduce the picture to 1/4^2 of the screen */
399     p_subpic->i_original_picture_width  *= 4;
400     p_subpic->i_original_picture_height *= 4;
401
402     spu_DisplaySubpicture( p_vout->p_spu, p_subpic );
403     return VLC_SUCCESS;
404 }
405
406 /**
407  * This function will display the name and a PIP of the provided snapshot
408  */
409 static void VoutOsdSnapshot( vout_thread_t *p_vout, picture_t *p_pic, const char *psz_filename )
410 {
411     msg_Dbg( p_vout, "snapshot taken (%s)", psz_filename );
412     vout_OSDMessage( VLC_OBJECT( p_vout ), DEFAULT_CHAN, "%s", psz_filename );
413
414     if( var_GetBool( p_vout, "snapshot-preview" ) )
415     {
416         if( VoutSnapshotPip( p_vout, p_pic ) )
417             msg_Warn( p_vout, "Failed to display snapshot" );
418     }
419 }
420
421 /* */
422 int vout_GetSnapshot( vout_thread_t *p_vout,
423                       block_t **pp_image, picture_t **pp_picture,
424                       video_format_t *p_fmt,
425                       const char *psz_format, mtime_t i_timeout )
426 {
427     picture_t *p_picture = vout_snapshot_Get( &p_vout->p->snapshot, i_timeout );
428     if( !p_picture )
429     {
430         msg_Err( p_vout, "Failed to grab a snapshot" );
431         return VLC_EGENERIC;
432     }
433
434     if( pp_image )
435     {
436         vlc_fourcc_t i_format = VLC_CODEC_PNG;
437         if( psz_format && image_Type2Fourcc( psz_format ) )
438             i_format = image_Type2Fourcc( psz_format );
439
440         const int i_override_width  = var_GetInteger( p_vout, "snapshot-width" );
441         const int i_override_height = var_GetInteger( p_vout, "snapshot-height" );
442
443         if( picture_Export( VLC_OBJECT(p_vout), pp_image, p_fmt,
444                             p_picture, i_format, i_override_width, i_override_height ) )
445         {
446             msg_Err( p_vout, "Failed to convert image for snapshot" );
447             picture_Release( p_picture );
448             return VLC_EGENERIC;
449         }
450     }
451     if( pp_picture )
452         *pp_picture = p_picture;
453     else
454         picture_Release( p_picture );
455     return VLC_SUCCESS;
456 }
457
458 /**
459  * This function will handle a snapshot request
460  */
461 static void VoutSaveSnapshot( vout_thread_t *p_vout )
462 {
463     char *psz_path = var_GetNonEmptyString( p_vout, "snapshot-path" );
464     char *psz_format = var_GetNonEmptyString( p_vout, "snapshot-format" );
465     char *psz_prefix = var_GetNonEmptyString( p_vout, "snapshot-prefix" );
466
467     /* */
468     picture_t *p_picture;
469     block_t *p_image;
470     video_format_t fmt;
471
472     /* 500ms timeout
473      * XXX it will cause trouble with low fps video (< 2fps) */
474     if( vout_GetSnapshot( p_vout, &p_image, &p_picture, &fmt, psz_format, 500*1000 ) )
475     {
476         p_picture = NULL;
477         p_image = NULL;
478         goto exit;
479     }
480
481     if( !psz_path )
482     {
483         psz_path = vout_snapshot_GetDirectory();
484         if( !psz_path )
485         {
486             msg_Err( p_vout, "no path specified for snapshots" );
487             goto exit;
488         }
489     }
490
491     vout_snapshot_save_cfg_t cfg;
492     memset( &cfg, 0, sizeof(cfg) );
493     cfg.is_sequential = var_GetBool( p_vout, "snapshot-sequential" );
494     cfg.sequence = var_GetInteger( p_vout, "snapshot-num" );
495     cfg.path = psz_path;
496     cfg.format = psz_format;
497     cfg.prefix_fmt = psz_prefix;
498
499     char *psz_filename;
500     int  i_sequence;
501     if (vout_snapshot_SaveImage( &psz_filename, &i_sequence,
502                                  p_image, VLC_OBJECT(p_vout), &cfg ) )
503         goto exit;
504     if( cfg.is_sequential )
505         var_SetInteger( p_vout, "snapshot-num", i_sequence + 1 );
506
507     VoutOsdSnapshot( p_vout, p_picture, psz_filename );
508
509     /* signal creation of a new snapshot file */
510     var_SetString( p_vout->p_libvlc, "snapshot-file", psz_filename );
511
512     free( psz_filename );
513
514 exit:
515     if( p_image )
516         block_Release( p_image );
517     if( p_picture )
518         picture_Release( p_picture );
519     free( psz_prefix );
520     free( psz_format );
521     free( psz_path );
522 }
523
524 /*****************************************************************************
525  * Handle filters
526  *****************************************************************************/
527
528 void vout_EnableFilter( vout_thread_t *p_vout, const char *psz_name,
529                         bool b_add, bool b_setconfig )
530 {
531     char *psz_parser;
532     char *psz_string;
533     const char *psz_filter_type;
534
535     /* FIXME temporary hack */
536     const char *psz_module_name = psz_name;
537     if( !strcmp( psz_name, "magnify" ) ||
538         !strcmp( psz_name, "puzzle" ) ||
539         !strcmp( psz_name, "logo" ) ||
540         !strcmp( psz_name, "wall" ) ||
541         !strcmp( psz_name, "clone" ) )
542         psz_module_name = "video_filter_wrapper";
543
544     module_t *p_obj = module_find( psz_module_name );
545     if( !p_obj )
546     {
547         msg_Err( p_vout, "Unable to find filter module \"%s\".", psz_name );
548         return;
549     }
550
551     if( module_provides( p_obj, "video filter" ) )
552     {
553         psz_filter_type = "vout-filter";
554     }
555     else if( module_provides( p_obj, "video filter2" ) )
556     {
557         psz_filter_type = "video-filter";
558     }
559     else if( module_provides( p_obj, "sub filter" ) )
560     {
561         psz_filter_type = "sub-filter";
562     }
563     else
564     {
565         module_release( p_obj );
566         msg_Err( p_vout, "Unknown video filter type." );
567         return;
568     }
569     module_release( p_obj );
570
571     if( !strcmp( psz_filter_type, "sub-filter") )
572         psz_string = var_GetString( vout_GetSpu( p_vout ), psz_filter_type );
573     else
574         psz_string = var_GetString( p_vout, psz_filter_type );
575
576     /* Todo : Use some generic chain manipulation functions */
577     if( !psz_string ) psz_string = strdup("");
578
579     psz_parser = strstr( psz_string, psz_name );
580     if( b_add )
581     {
582         if( !psz_parser )
583         {
584             psz_parser = psz_string;
585             if( asprintf( &psz_string, (*psz_string) ? "%s:%s" : "%s%s",
586                           psz_string, psz_name ) == -1 )
587             {
588                 free( psz_parser );
589                 return;
590             }
591             free( psz_parser );
592         }
593         else
594             return;
595     }
596     else
597     {
598         if( psz_parser )
599         {
600             memmove( psz_parser, psz_parser + strlen(psz_name) +
601                             (*(psz_parser + strlen(psz_name)) == ':' ? 1 : 0 ),
602                             strlen(psz_parser + strlen(psz_name)) + 1 );
603
604             /* Remove trailing : : */
605             if( *(psz_string+strlen(psz_string ) -1 ) == ':' )
606             {
607                 *(psz_string+strlen(psz_string ) -1 ) = '\0';
608             }
609          }
610          else
611          {
612              free( psz_string );
613              return;
614          }
615     }
616
617     if( b_setconfig )
618     {
619         if( !strcmp( psz_filter_type, "sub-filter") )
620             config_PutPsz( vout_GetSpu( p_vout ), psz_filter_type, psz_string );
621         else
622             config_PutPsz( p_vout, psz_filter_type, psz_string );
623     }
624
625     if( !strcmp( psz_filter_type, "sub-filter") )
626         var_SetString( vout_GetSpu( p_vout ), psz_filter_type, psz_string );
627     else
628         var_SetString( p_vout, psz_filter_type, psz_string );
629
630     free( psz_string );
631 }
632
633 /*****************************************************************************
634  * InitWindowSize: find the initial dimensions the video window should have.
635  *****************************************************************************
636  * This function will check the "width", "height" and "zoom" config options and
637  * will calculate the size that the video window should have.
638  *****************************************************************************/
639 static void InitWindowSize( vout_thread_t *p_vout, unsigned *pi_width,
640                             unsigned *pi_height )
641 {
642 #define FP_FACTOR 1000                             /* our fixed point factor */
643
644     int i_width = var_GetInteger( p_vout, "width" );
645     int i_height = var_GetInteger( p_vout, "height" );
646     float f_zoom = var_GetFloat( p_vout, "zoom" );
647     uint64_t ll_zoom = (uint64_t)( FP_FACTOR * f_zoom );
648
649     if( i_width > 0 && i_height > 0)
650     {
651         *pi_width = (int)( i_width * ll_zoom / FP_FACTOR );
652         *pi_height = (int)( i_height * ll_zoom / FP_FACTOR );
653     }
654     else if( i_width > 0 )
655     {
656         *pi_width = (int)( i_width * ll_zoom / FP_FACTOR );
657         *pi_height = (int)( p_vout->fmt_in.i_visible_height * ll_zoom *
658             p_vout->fmt_in.i_sar_den * i_width / p_vout->fmt_in.i_sar_num /
659             FP_FACTOR / p_vout->fmt_in.i_visible_width );
660     }
661     else if( i_height > 0 )
662     {
663         *pi_height = (int)( i_height * ll_zoom / FP_FACTOR );
664         *pi_width = (int)( p_vout->fmt_in.i_visible_width * ll_zoom *
665             p_vout->fmt_in.i_sar_num * i_height / p_vout->fmt_in.i_sar_den /
666             FP_FACTOR / p_vout->fmt_in.i_visible_height );
667     }
668     else if( p_vout->fmt_in.i_sar_num == 0 || p_vout->fmt_in.i_sar_den == 0 )
669     {
670         msg_Warn( p_vout, "aspect ratio screwed up" );
671         *pi_width = (int)( p_vout->fmt_in.i_visible_width * ll_zoom / FP_FACTOR );
672         *pi_height = (int)( p_vout->fmt_in.i_visible_height * ll_zoom /FP_FACTOR);
673     }
674     else if( p_vout->fmt_in.i_sar_num >= p_vout->fmt_in.i_sar_den )
675     {
676         *pi_width = (int)( p_vout->fmt_in.i_visible_width * ll_zoom *
677             p_vout->fmt_in.i_sar_num / p_vout->fmt_in.i_sar_den / FP_FACTOR );
678         *pi_height = (int)( p_vout->fmt_in.i_visible_height * ll_zoom
679             / FP_FACTOR );
680     }
681     else
682     {
683         *pi_width = (int)( p_vout->fmt_in.i_visible_width * ll_zoom
684             / FP_FACTOR );
685         *pi_height = (int)( p_vout->fmt_in.i_visible_height * ll_zoom *
686             p_vout->fmt_in.i_sar_den / p_vout->fmt_in.i_sar_num / FP_FACTOR );
687     }
688
689     msg_Dbg( p_vout, "window size: %ux%u", *pi_width, *pi_height );
690
691 #undef FP_FACTOR
692 }
693
694 /*****************************************************************************
695  * Object variables callbacks
696  *****************************************************************************/
697 static int ZoomCallback( vlc_object_t *p_this, char const *psz_cmd,
698                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
699 {
700     return var_SetFloat( p_this, "scale", newval.f_float );
701 }
702
703 static int CropCallback( vlc_object_t *p_this, char const *psz_cmd,
704                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
705 {
706     vout_thread_t *p_vout = (vout_thread_t *)p_this;
707     int64_t i_aspect_num, i_aspect_den;
708     unsigned int i_width, i_height;
709
710     (void)oldval; (void)p_data;
711
712     /* Restore defaults */
713     p_vout->fmt_in.i_x_offset = p_vout->fmt_render.i_x_offset;
714     p_vout->fmt_in.i_visible_width = p_vout->fmt_render.i_visible_width;
715     p_vout->fmt_in.i_y_offset = p_vout->fmt_render.i_y_offset;
716     p_vout->fmt_in.i_visible_height = p_vout->fmt_render.i_visible_height;
717
718     if( !strcmp( psz_cmd, "crop" ) )
719     {
720         char *psz_end = NULL, *psz_parser = strchr( newval.psz_string, ':' );
721         if( psz_parser )
722         {
723             /* We're using the 3:4 syntax */
724             i_aspect_num = strtol( newval.psz_string, &psz_end, 10 );
725             if( psz_end == newval.psz_string || !i_aspect_num ) goto crop_end;
726
727             i_aspect_den = strtol( ++psz_parser, &psz_end, 10 );
728             if( psz_end == psz_parser || !i_aspect_den ) goto crop_end;
729
730             i_width = p_vout->fmt_in.i_sar_den*p_vout->fmt_render.i_visible_height *
731                 i_aspect_num / i_aspect_den / p_vout->fmt_in.i_sar_num;
732             i_height = p_vout->fmt_render.i_visible_width*p_vout->fmt_in.i_sar_num *
733                 i_aspect_den / i_aspect_num / p_vout->fmt_in.i_sar_den;
734
735             if( i_width < p_vout->fmt_render.i_visible_width )
736             {
737                 p_vout->fmt_in.i_x_offset = p_vout->fmt_render.i_x_offset +
738                     (p_vout->fmt_render.i_visible_width - i_width) / 2;
739                 p_vout->fmt_in.i_visible_width = i_width;
740             }
741             else
742             {
743                 p_vout->fmt_in.i_y_offset = p_vout->fmt_render.i_y_offset +
744                     (p_vout->fmt_render.i_visible_height - i_height) / 2;
745                 p_vout->fmt_in.i_visible_height = i_height;
746             }
747         }
748         else
749         {
750             psz_parser = strchr( newval.psz_string, 'x' );
751             if( psz_parser )
752             {
753                 /* Maybe we're using the <width>x<height>+<left>+<top> syntax */
754                 unsigned int i_crop_width, i_crop_height, i_crop_top, i_crop_left;
755
756                 i_crop_width = strtol( newval.psz_string, &psz_end, 10 );
757                 if( psz_end != psz_parser ) goto crop_end;
758
759                 psz_parser = strchr( ++psz_end, '+' );
760                 i_crop_height = strtol( psz_end, &psz_end, 10 );
761                 if( psz_end != psz_parser ) goto crop_end;
762
763                 psz_parser = strchr( ++psz_end, '+' );
764                 i_crop_left = strtol( psz_end, &psz_end, 10 );
765                 if( psz_end != psz_parser ) goto crop_end;
766
767                 psz_end++;
768                 i_crop_top = strtol( psz_end, &psz_end, 10 );
769                 if( *psz_end != '\0' ) goto crop_end;
770
771                 if( i_crop_top + i_crop_height >= p_vout->fmt_render.i_visible_height ||
772                     i_crop_left + i_crop_width >= p_vout->fmt_render.i_visible_width )
773                 {
774                     msg_Err( p_vout, "Unable to crop over picture boundaries");
775                     return VLC_EGENERIC;
776                 }
777
778                 i_width = i_crop_width;
779                 p_vout->fmt_in.i_visible_width = i_width;
780
781                 i_height = i_crop_height;
782                 p_vout->fmt_in.i_visible_height = i_height;
783
784                 p_vout->fmt_in.i_x_offset = i_crop_left;
785                 p_vout->fmt_in.i_y_offset = i_crop_top;
786             }
787             else
788             {
789                 /* Maybe we're using the <left>+<top>+<right>+<bottom> syntax */
790                 unsigned int i_crop_top, i_crop_left, i_crop_bottom, i_crop_right;
791
792                 psz_parser = strchr( newval.psz_string, '+' );
793                 i_crop_left = strtol( newval.psz_string, &psz_end, 10 );
794                 if( psz_end != psz_parser ) goto crop_end;
795
796                 psz_parser = strchr( ++psz_end, '+' );
797                 i_crop_top = strtol( psz_end, &psz_end, 10 );
798                 if( psz_end != psz_parser ) goto crop_end;
799
800                 psz_parser = strchr( ++psz_end, '+' );
801                 i_crop_right = strtol( psz_end, &psz_end, 10 );
802                 if( psz_end != psz_parser ) goto crop_end;
803
804                 psz_end++;
805                 i_crop_bottom = strtol( psz_end, &psz_end, 10 );
806                 if( *psz_end != '\0' ) goto crop_end;
807
808                 if( i_crop_top + i_crop_bottom >= p_vout->fmt_render.i_visible_height ||
809                     i_crop_right + i_crop_left >= p_vout->fmt_render.i_visible_width )
810                 {
811                     msg_Err( p_vout, "Unable to crop over picture boundaries" );
812                     return VLC_EGENERIC;
813                 }
814
815                 i_width = p_vout->fmt_render.i_visible_width
816                           - i_crop_left - i_crop_right;
817                 p_vout->fmt_in.i_visible_width = i_width;
818
819                 i_height = p_vout->fmt_render.i_visible_height
820                            - i_crop_top - i_crop_bottom;
821                 p_vout->fmt_in.i_visible_height = i_height;
822
823                 p_vout->fmt_in.i_x_offset = i_crop_left;
824                 p_vout->fmt_in.i_y_offset = i_crop_top;
825             }
826         }
827     }
828     else if( !strcmp( psz_cmd, "crop-top" )
829           || !strcmp( psz_cmd, "crop-left" )
830           || !strcmp( psz_cmd, "crop-bottom" )
831           || !strcmp( psz_cmd, "crop-right" ) )
832     {
833         unsigned int i_crop_top, i_crop_left, i_crop_bottom, i_crop_right;
834
835         i_crop_top = var_GetInteger( p_vout, "crop-top" );
836         i_crop_left = var_GetInteger( p_vout, "crop-left" );
837         i_crop_right = var_GetInteger( p_vout, "crop-right" );
838         i_crop_bottom = var_GetInteger( p_vout, "crop-bottom" );
839
840         if( i_crop_top + i_crop_bottom >= p_vout->fmt_render.i_visible_height ||
841             i_crop_right + i_crop_left >= p_vout->fmt_render.i_visible_width )
842         {
843             msg_Err( p_vout, "Unable to crop over picture boundaries" );
844             return VLC_EGENERIC;
845         }
846
847         i_width = p_vout->fmt_render.i_visible_width
848                   - i_crop_left - i_crop_right;
849         p_vout->fmt_in.i_visible_width = i_width;
850
851         i_height = p_vout->fmt_render.i_visible_height
852                    - i_crop_top - i_crop_bottom;
853         p_vout->fmt_in.i_visible_height = i_height;
854
855         p_vout->fmt_in.i_x_offset = i_crop_left;
856         p_vout->fmt_in.i_y_offset = i_crop_top;
857     }
858
859  crop_end:
860     InitWindowSize( p_vout, &p_vout->i_window_width,
861                     &p_vout->i_window_height );
862
863     p_vout->i_changes |= VOUT_CROP_CHANGE;
864
865     msg_Dbg( p_vout, "cropping picture %ix%i to %i,%i,%ix%i",
866              p_vout->fmt_in.i_width, p_vout->fmt_in.i_height,
867              p_vout->fmt_in.i_x_offset, p_vout->fmt_in.i_y_offset,
868              p_vout->fmt_in.i_visible_width,
869              p_vout->fmt_in.i_visible_height );
870
871     var_TriggerCallback( p_vout, "crop-update" );
872
873     return VLC_SUCCESS;
874 }
875
876 static int AspectCallback( vlc_object_t *p_this, char const *psz_cmd,
877                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
878 {
879     vout_thread_t *p_vout = (vout_thread_t *)p_this;
880     unsigned int i_aspect_num, i_aspect_den, i_sar_num, i_sar_den;
881     vlc_value_t val;
882
883     char *psz_end, *psz_parser = strchr( newval.psz_string, ':' );
884     (void)psz_cmd; (void)oldval; (void)p_data;
885
886     /* Restore defaults */
887     p_vout->fmt_in.i_sar_num = p_vout->fmt_render.i_sar_num;
888     p_vout->fmt_in.i_sar_den = p_vout->fmt_render.i_sar_den;
889     p_vout->render.i_aspect = (int64_t)p_vout->fmt_render.i_sar_num *
890                                        p_vout->fmt_render.i_width *
891                                        VOUT_ASPECT_FACTOR /
892                                        p_vout->fmt_render.i_sar_den / p_vout->fmt_render.i_height;
893
894     if( !psz_parser ) goto aspect_end;
895
896     i_aspect_num = strtol( newval.psz_string, &psz_end, 10 );
897     if( psz_end == newval.psz_string || !i_aspect_num ) goto aspect_end;
898
899     i_aspect_den = strtol( ++psz_parser, &psz_end, 10 );
900     if( psz_end == psz_parser || !i_aspect_den ) goto aspect_end;
901
902     i_sar_num = i_aspect_num * p_vout->fmt_render.i_visible_height;
903     i_sar_den = i_aspect_den * p_vout->fmt_render.i_visible_width;
904     vlc_ureduce( &i_sar_num, &i_sar_den, i_sar_num, i_sar_den, 0 );
905     p_vout->fmt_in.i_sar_num = i_sar_num;
906     p_vout->fmt_in.i_sar_den = i_sar_den;
907     p_vout->render.i_aspect = i_aspect_num * VOUT_ASPECT_FACTOR / i_aspect_den;
908
909  aspect_end:
910     if( p_vout->p->i_par_num && p_vout->p->i_par_den )
911     {
912         p_vout->fmt_in.i_sar_num *= p_vout->p->i_par_den;
913         p_vout->fmt_in.i_sar_den *= p_vout->p->i_par_num;
914         p_vout->render.i_aspect = (int64_t)p_vout->fmt_in.i_sar_num *
915                                            p_vout->fmt_in.i_width *
916                                            VOUT_ASPECT_FACTOR /
917                                            p_vout->fmt_in.i_sar_den /
918                                            p_vout->fmt_in.i_height;
919     }
920
921     p_vout->i_changes |= VOUT_ASPECT_CHANGE;
922
923     msg_Dbg( p_vout, "new aspect-ratio %i:%i, sample aspect-ratio %i:%i",
924              p_vout->fmt_in.i_sar_num * p_vout->fmt_in.i_width,
925              p_vout->fmt_in.i_sar_den * p_vout->fmt_in.i_height,
926              p_vout->fmt_in.i_sar_num, p_vout->fmt_in.i_sar_den );
927
928     if( var_Get( p_vout, "crop", &val ) )
929         return VLC_EGENERIC;
930
931     int i_ret = CropCallback( p_this, "crop", val, val, 0 );
932     free( val.psz_string );
933     return i_ret;
934 }
935
936 static int ScalingCallback( vlc_object_t *p_this, char const *psz_cmd,
937                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
938 {
939     vout_thread_t *p_vout = (vout_thread_t *)p_this;
940     (void)oldval; (void)newval; (void)p_data;
941
942     vlc_mutex_lock( &p_vout->change_lock );
943
944     if( !strcmp( psz_cmd, "autoscale" ) )
945     {
946         p_vout->i_changes |= VOUT_SCALE_CHANGE;
947     }
948     else if( !strcmp( psz_cmd, "scale" ) )
949     {
950         p_vout->i_changes |= VOUT_ZOOM_CHANGE;
951     }
952
953     vlc_mutex_unlock( &p_vout->change_lock );
954
955     return VLC_SUCCESS;
956 }
957
958 static int OnTopCallback( vlc_object_t *p_this, char const *psz_cmd,
959                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
960 {
961     vout_thread_t *p_vout = (vout_thread_t *)p_this;
962
963     vlc_mutex_lock( &p_vout->change_lock );
964     p_vout->i_changes |= VOUT_ON_TOP_CHANGE;
965     p_vout->b_on_top = newval.b_bool;
966     vlc_mutex_unlock( &p_vout->change_lock );
967
968     /* Modify libvlc as well because the vout might have to be restarted */
969     var_Create( p_vout->p_libvlc, "video-on-top", VLC_VAR_BOOL );
970     var_Set( p_vout->p_libvlc, "video-on-top", newval );
971
972     (void)psz_cmd; (void)oldval; (void)p_data;
973     return VLC_SUCCESS;
974 }
975
976 static int FullscreenCallback( vlc_object_t *p_this, char const *psz_cmd,
977                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
978 {
979     vout_thread_t *p_vout = (vout_thread_t *)p_this;
980     vlc_value_t val;
981     (void)psz_cmd; (void)p_data;
982
983     if( oldval.b_bool == newval.b_bool )
984         return VLC_SUCCESS; /* no-op */
985     p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
986
987     /* Modify libvlc as well because the vout might have to be restarted */
988     var_Create( p_vout->p_libvlc, "fullscreen", VLC_VAR_BOOL );
989     var_Set( p_vout->p_libvlc, "fullscreen", newval );
990
991     val.b_bool = true;
992     var_Set( p_vout, "intf-change", val );
993     return VLC_SUCCESS;
994 }
995
996 static int SnapshotCallback( vlc_object_t *p_this, char const *psz_cmd,
997                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
998 {
999     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1000     VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
1001     VLC_UNUSED(newval); VLC_UNUSED(p_data);
1002
1003     VoutSaveSnapshot( p_vout );
1004     return VLC_SUCCESS;
1005 }
1006
1007 static int TitleShowCallback( vlc_object_t *p_this, char const *psz_cmd,
1008                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1009 {
1010     VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
1011     VLC_UNUSED(p_data);
1012     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1013     p_vout->p->b_title_show = newval.b_bool;
1014     return VLC_SUCCESS;
1015 }
1016
1017 static int TitleTimeoutCallback( vlc_object_t *p_this, char const *psz_cmd,
1018                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1019 {
1020     VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval); VLC_UNUSED(p_data);
1021     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1022     p_vout->p->i_title_timeout = (mtime_t) newval.i_int;
1023     return VLC_SUCCESS;
1024 }
1025
1026 static int TitlePositionCallback( vlc_object_t *p_this, char const *psz_cmd,
1027                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
1028 {
1029     VLC_UNUSED(psz_cmd); VLC_UNUSED(oldval);
1030     VLC_UNUSED(p_data);
1031     vout_thread_t *p_vout = (vout_thread_t *)p_this;
1032     p_vout->p->i_title_position = newval.i_int;
1033     return VLC_SUCCESS;
1034 }