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