]> git.sesse.net Git - vlc/blob - src/video_output/vout_intf.c
* src/video_output/vout_intf.c: implemented vout_Control( VOUT_SNAPSHOT ) ... most...
[vlc] / src / video_output / vout_intf.c
1 /*****************************************************************************
2  * vout_intf.c : video output interface
3  *****************************************************************************
4  * Copyright (C) 2000-2004 VideoLAN
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA  02111, USA.
22  *****************************************************************************/
23
24 /*****************************************************************************
25  * Preamble
26  *****************************************************************************/
27 #include <stdlib.h>                                                /* free() */
28
29 #include <vlc/vlc.h>
30 #include <vlc/intf.h>
31
32 #include "vlc_video.h"
33 #include "video_output.h"
34 #include "vlc_image.h"
35 #include "vlc_spu.h"
36
37 /*****************************************************************************
38  * Local prototypes
39  *****************************************************************************/
40
41 /* Object variables callbacks */
42 static int ZoomCallback( vlc_object_t *, char const *,
43                          vlc_value_t, vlc_value_t, void * );
44 static int OnTopCallback( vlc_object_t *, char const *,
45                           vlc_value_t, vlc_value_t, void * );
46 static int FullscreenCallback( vlc_object_t *, char const *,
47                                vlc_value_t, vlc_value_t, void * );
48
49 /*****************************************************************************
50  * vout_RequestWindow: Create/Get a video window if possible.
51  *****************************************************************************
52  * This function looks for the main interface and tries to request
53  * a new video window. If it fails then the vout will still need to create the
54  * window by itself.
55  *****************************************************************************/
56 void *vout_RequestWindow( vout_thread_t *p_vout,
57                           int *pi_x_hint, int *pi_y_hint,
58                           unsigned int *pi_width_hint,
59                           unsigned int *pi_height_hint )
60 {
61     intf_thread_t *p_intf = NULL;
62     vlc_list_t *p_list;
63     void *p_window;
64     vlc_value_t val;
65     int i;
66
67     /* Small kludge */
68     if( !var_Type( p_vout, "aspect-ratio" ) ) vout_IntfInit( p_vout );
69
70     /* Get requested coordinates */
71     var_Get( p_vout, "video-x", &val );
72     *pi_x_hint = val.i_int ;
73     var_Get( p_vout, "video-y", &val );
74     *pi_y_hint = val.i_int;
75
76     *pi_width_hint = p_vout->i_window_width;
77     *pi_height_hint = p_vout->i_window_height;
78
79     /* Check whether someone provided us with a window ID */
80     var_Get( p_vout->p_vlc, "drawable", &val );
81     if( val.i_int ) return (void *)val.i_int;
82
83     /* Find if the main interface supports embedding */
84     p_list = vlc_list_find( p_vout, VLC_OBJECT_INTF, FIND_ANYWHERE );
85     if( !p_list ) return NULL;
86
87     for( i = 0; i < p_list->i_count; i++ )
88     {
89         p_intf = (intf_thread_t *)p_list->p_values[i].p_object;
90         if( p_intf->b_block && p_intf->pf_request_window ) break;
91         p_intf = NULL;
92     }
93
94     if( !p_intf )
95     {
96         vlc_list_release( p_list );
97         return NULL;
98     }
99
100     vlc_object_yield( p_intf );
101     vlc_list_release( p_list );
102
103     p_window = p_intf->pf_request_window( p_intf, p_vout, pi_x_hint, pi_y_hint,
104                                           pi_width_hint, pi_height_hint );
105
106     if( !p_window ) vlc_object_release( p_intf );
107     else p_vout->p_parent_intf = p_intf;
108
109     return p_window;
110 }
111
112 void vout_ReleaseWindow( vout_thread_t *p_vout, void *p_window )
113 {
114     intf_thread_t *p_intf = p_vout->p_parent_intf;
115
116     if( !p_intf ) return;
117
118     vlc_mutex_lock( &p_intf->object_lock );
119     if( p_intf->b_dead )
120     {
121         vlc_mutex_unlock( &p_intf->object_lock );
122         return;
123     }
124
125     if( !p_intf->pf_release_window )
126     {
127         msg_Err( p_vout, "no pf_release_window");
128         vlc_mutex_unlock( &p_intf->object_lock );
129         vlc_object_release( p_intf );
130         return;
131     }
132
133     p_intf->pf_release_window( p_intf, p_window );
134
135     p_vout->p_parent_intf = NULL;
136     vlc_mutex_unlock( &p_intf->object_lock );
137     vlc_object_release( p_intf );
138 }
139
140 int vout_ControlWindow( vout_thread_t *p_vout, void *p_window,
141                         int i_query, va_list args )
142 {
143     intf_thread_t *p_intf = p_vout->p_parent_intf;
144     int i_ret;
145
146     if( !p_intf ) return VLC_EGENERIC;
147
148     vlc_mutex_lock( &p_intf->object_lock );
149     if( p_intf->b_dead )
150     {
151         vlc_mutex_unlock( &p_intf->object_lock );
152         return VLC_EGENERIC;
153     }
154
155     if( !p_intf->pf_control_window )
156     {
157         msg_Err( p_vout, "no pf_control_window");
158         vlc_mutex_unlock( &p_intf->object_lock );
159         return VLC_EGENERIC;
160     }
161
162     i_ret = p_intf->pf_control_window( p_intf, p_window, i_query, args );
163     vlc_mutex_unlock( &p_intf->object_lock );
164     return i_ret;
165 }
166
167 /*****************************************************************************
168  * vout_IntfInit: called during the vout creation to initialise misc things.
169  *****************************************************************************/
170 void vout_IntfInit( vout_thread_t *p_vout )
171 {
172     vlc_value_t val, text, old_val;
173
174     /* Create a few object variables we'll need later on */
175     var_Create( p_vout, "snapshot-path", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
176     var_Create( p_vout, "aspect-ratio", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
177     var_Create( p_vout, "width", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
178     var_Create( p_vout, "height", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
179     var_Create( p_vout, "align", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
180     var_Create( p_vout, "video-x", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
181     var_Create( p_vout, "video-y", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
182
183     var_Create( p_vout, "zoom", VLC_VAR_FLOAT | VLC_VAR_ISCOMMAND |
184                 VLC_VAR_HASCHOICE | VLC_VAR_DOINHERIT );
185
186     text.psz_string = _("Zoom");
187     var_Change( p_vout, "zoom", VLC_VAR_SETTEXT, &text, NULL );
188
189     var_Get( p_vout, "zoom", &old_val );
190     if( old_val.f_float == 0.25 ||
191         old_val.f_float == 0.5 ||
192         old_val.f_float == 1 ||
193         old_val.f_float == 2 )
194     {
195         var_Change( p_vout, "zoom", VLC_VAR_DELCHOICE, &old_val, NULL );
196     }
197
198     val.f_float = 0.25; text.psz_string = _("1:4 Quarter");
199     var_Change( p_vout, "zoom", VLC_VAR_ADDCHOICE, &val, &text );
200     val.f_float = 0.5; text.psz_string = _("1:2 Half");
201     var_Change( p_vout, "zoom", VLC_VAR_ADDCHOICE, &val, &text );
202     val.f_float = 1; text.psz_string = _("1:1 Original");
203     var_Change( p_vout, "zoom", VLC_VAR_ADDCHOICE, &val, &text );
204     val.f_float = 2; text.psz_string = _("2:1 Double");
205     var_Change( p_vout, "zoom", VLC_VAR_ADDCHOICE, &val, &text );
206
207     var_Set( p_vout, "zoom", old_val );
208
209     var_AddCallback( p_vout, "zoom", ZoomCallback, NULL );
210
211     /* Add a variable to indicate if the window should be on top of others */
212     var_Create( p_vout, "video-on-top", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
213     text.psz_string = _("Always on top");
214     var_Change( p_vout, "video-on-top", VLC_VAR_SETTEXT, &text, NULL );
215     var_AddCallback( p_vout, "video-on-top", OnTopCallback, NULL );
216
217     /* Add a variable to indicate whether we want window decoration or not */
218     var_Create( p_vout, "video-deco", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
219
220     /* Add a fullscreen variable */
221     var_Create( p_vout, "fullscreen", VLC_VAR_BOOL );
222     text.psz_string = _("Fullscreen");
223     var_Change( p_vout, "fullscreen", VLC_VAR_SETTEXT, &text, NULL );
224     var_Change( p_vout, "fullscreen", VLC_VAR_INHERITVALUE, &val, NULL );
225     if( val.b_bool )
226     {
227         /* user requested fullscreen */
228         p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
229     }
230     var_AddCallback( p_vout, "fullscreen", FullscreenCallback, NULL );
231
232     /* Mouse coordinates */
233     var_Create( p_vout, "mouse-x", VLC_VAR_INTEGER );
234     var_Create( p_vout, "mouse-y", VLC_VAR_INTEGER );
235     var_Create( p_vout, "mouse-button-down", VLC_VAR_INTEGER );
236     var_Create( p_vout, "mouse-moved", VLC_VAR_BOOL );
237     var_Create( p_vout, "mouse-clicked", VLC_VAR_INTEGER );
238
239     var_Create( p_vout, "intf-change", VLC_VAR_BOOL );
240     val.b_bool = VLC_TRUE;
241     var_Set( p_vout, "intf-change", val );
242 }
243
244 /*****************************************************************************
245  * vout_Snapshot: generates a snapshot.
246  *****************************************************************************/
247 int vout_Snapshot( vout_thread_t *p_vout, picture_t *p_pic )
248 {
249     image_handler_t *p_image = image_HandlerCreate( p_vout );
250     video_format_t fmt_in = {0}, fmt_out = {0};
251     char *psz_filename;
252     subpicture_t *p_subpic;
253     picture_t *p_pif;
254     vlc_value_t val;
255     int i_ret;
256
257     var_Get( p_vout, "snapshot-path", &val );
258     if( val.psz_string && !*val.psz_string )
259     {
260         free( val.psz_string );
261         val.psz_string = 0;
262     }
263     if( !val.psz_string && p_vout->p_vlc->psz_homedir )
264     {
265         asprintf( &val.psz_string, "%s/" CONFIG_DIR,
266                   p_vout->p_vlc->psz_homedir );
267     }
268     if( !val.psz_string )
269     {
270         msg_Err( p_vout, "no directory specified for snapshots" );
271         return VLC_EGENERIC;
272     }
273
274     asprintf( &psz_filename, "%s/vlcsnap-%u.png", val.psz_string,
275               (unsigned int)(p_pic->date / 100000) & 0xFFFFFF );
276     free( val.psz_string );
277
278     /* Save the snapshot */
279     fmt_in.i_chroma = p_vout->render.i_chroma;
280     fmt_in.i_width = p_vout->render.i_width;
281     fmt_in.i_height = p_vout->render.i_height;
282     i_ret = image_WriteUrl( p_image, p_pic, &fmt_in, &fmt_out, psz_filename );
283     if( i_ret != VLC_SUCCESS )
284     {
285         msg_Err( p_vout, "could not create snapshot %s", psz_filename );
286         free( psz_filename );
287         image_HandlerDelete( p_image );
288         return VLC_EGENERIC;
289     }
290
291     msg_Dbg( p_vout, "snapshot taken (%s)", psz_filename );
292     free( psz_filename );
293
294     /* Inject a subpicture with the snapshot */
295     fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
296     fmt_out.i_width = fmt_out.i_visible_width = p_vout->render.i_width;
297     fmt_out.i_height = fmt_out.i_visible_height = p_vout->render.i_height;
298     fmt_out.i_aspect = VOUT_ASPECT_FACTOR;
299     p_pif = image_Convert( p_image, p_pic, &fmt_in, &fmt_out );
300     image_HandlerDelete( p_image );
301     if( !p_pif ) return VLC_EGENERIC;
302
303     p_subpic = spu_CreateSubpicture( p_vout->p_spu );
304     if( p_subpic == NULL )
305     {
306          p_pif->pf_release( p_pif );
307          return VLC_EGENERIC;
308     }
309
310     p_subpic->i_channel = 0;
311     p_subpic->i_start = mdate();
312     p_subpic->i_stop = mdate() + 4000000;
313     p_subpic->b_ephemer = VLC_TRUE;
314     p_subpic->b_fade = VLC_TRUE;
315     p_subpic->i_original_picture_width = p_vout->render.i_width * 4;
316     p_subpic->i_original_picture_height = p_vout->render.i_height * 4;
317
318     p_subpic->p_region = spu_CreateRegion( p_vout->p_spu, &fmt_out );
319     vout_CopyPicture( p_image->p_parent, &p_subpic->p_region->picture, p_pif );
320     p_pif->pf_release( p_pif );
321
322     spu_DisplaySubpicture( p_vout->p_spu, p_subpic );
323
324     return VLC_SUCCESS;
325 }
326
327 /*****************************************************************************
328  * vout_ControlDefault: default methods for video output control.
329  *****************************************************************************/
330 int vout_vaControlDefault( vout_thread_t *p_vout, int i_query, va_list args )
331 {
332     switch( i_query )
333     {
334     case VOUT_REPARENT:
335     case VOUT_CLOSE:
336         if( p_vout->p_parent_intf )
337         {
338             vlc_object_release( p_vout->p_parent_intf );
339             p_vout->p_parent_intf = NULL;
340         }
341         return VLC_SUCCESS;
342         break;
343
344     case VOUT_SNAPSHOT:
345         p_vout->b_snapshot = VLC_TRUE;
346         return VLC_SUCCESS;
347         break;
348
349     default:
350         msg_Dbg( p_vout, "control query not supported" );
351         return VLC_EGENERIC;
352     }
353 }
354
355 /*****************************************************************************
356  * Object variables callbacks
357  *****************************************************************************/
358 static int ZoomCallback( vlc_object_t *p_this, char const *psz_cmd,
359                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
360 {
361     vout_thread_t *p_vout = (vout_thread_t *)p_this;
362     vout_Control( p_vout, VOUT_SET_ZOOM, newval.f_float );
363     return VLC_SUCCESS;
364 }
365
366 static int OnTopCallback( vlc_object_t *p_this, char const *psz_cmd,
367                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
368 {
369     vout_thread_t *p_vout = (vout_thread_t *)p_this;
370     playlist_t *p_playlist;
371     vout_Control( p_vout, VOUT_SET_STAY_ON_TOP, newval.b_bool );
372
373     p_playlist = (playlist_t *)vlc_object_find( p_this, VLC_OBJECT_PLAYLIST,
374                                                  FIND_PARENT );
375     if( p_playlist )
376     {
377         /* Modify playlist as well because the vout might have to be restarted */
378         var_Create( p_playlist, "video-on-top", VLC_VAR_BOOL );
379         var_Set( p_playlist, "video-on-top", newval );
380
381         vlc_object_release( p_playlist );
382     }
383     return VLC_SUCCESS;
384 }
385
386 static int FullscreenCallback( vlc_object_t *p_this, char const *psz_cmd,
387                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
388 {
389     vout_thread_t *p_vout = (vout_thread_t *)p_this;
390     playlist_t *p_playlist;
391     vlc_value_t val;
392
393     p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
394
395     p_playlist = (playlist_t *)vlc_object_find( p_this, VLC_OBJECT_PLAYLIST,
396                                                  FIND_PARENT );
397     if( p_playlist )
398     {
399         /* Modify playlist as well because the vout might have to be restarted */
400         var_Create( p_playlist, "fullscreen", VLC_VAR_BOOL );
401         var_Set( p_playlist, "fullscreen", newval );
402
403         vlc_object_release( p_playlist );
404     }
405
406     /* Disable "always on top" in fullscreen mode */
407     var_Get( p_vout, "video-on-top", &val );
408     if( newval.b_bool && val.b_bool )
409     {
410         val.b_bool = VLC_FALSE;
411         vout_Control( p_vout, VOUT_SET_STAY_ON_TOP, val.b_bool );
412     }
413     else if( !newval.b_bool && val.b_bool )
414     {
415         vout_Control( p_vout, VOUT_SET_STAY_ON_TOP, val.b_bool );
416     }
417
418     val.b_bool = VLC_TRUE;
419     var_Set( p_vout, "intf-change", val );
420     return VLC_SUCCESS;
421 }