]> git.sesse.net Git - vlc/blob - src/video_output/vout_intf.c
b18a7dfa56b7fc16c9ae975ce2067d9b0ad2d1a6
[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, "snapshot-format", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
179     var_Create( p_vout, "aspect-ratio", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
180     var_Create( p_vout, "width", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
181     var_Create( p_vout, "height", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
182     var_Create( p_vout, "align", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
183     var_Create( p_vout, "video-x", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
184     var_Create( p_vout, "video-y", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
185
186     var_Create( p_vout, "zoom", VLC_VAR_FLOAT | VLC_VAR_ISCOMMAND |
187                 VLC_VAR_HASCHOICE | VLC_VAR_DOINHERIT );
188
189     text.psz_string = _("Zoom");
190     var_Change( p_vout, "zoom", VLC_VAR_SETTEXT, &text, NULL );
191
192     var_Get( p_vout, "zoom", &old_val );
193     if( old_val.f_float == 0.25 ||
194         old_val.f_float == 0.5 ||
195         old_val.f_float == 1 ||
196         old_val.f_float == 2 )
197     {
198         var_Change( p_vout, "zoom", VLC_VAR_DELCHOICE, &old_val, NULL );
199     }
200
201     val.f_float = 0.25; text.psz_string = _("1:4 Quarter");
202     var_Change( p_vout, "zoom", VLC_VAR_ADDCHOICE, &val, &text );
203     val.f_float = 0.5; text.psz_string = _("1:2 Half");
204     var_Change( p_vout, "zoom", VLC_VAR_ADDCHOICE, &val, &text );
205     val.f_float = 1; text.psz_string = _("1:1 Original");
206     var_Change( p_vout, "zoom", VLC_VAR_ADDCHOICE, &val, &text );
207     val.f_float = 2; text.psz_string = _("2:1 Double");
208     var_Change( p_vout, "zoom", VLC_VAR_ADDCHOICE, &val, &text );
209
210     var_Set( p_vout, "zoom", old_val );
211
212     var_AddCallback( p_vout, "zoom", ZoomCallback, NULL );
213
214     /* Add a variable to indicate if the window should be on top of others */
215     var_Create( p_vout, "video-on-top", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
216     text.psz_string = _("Always on top");
217     var_Change( p_vout, "video-on-top", VLC_VAR_SETTEXT, &text, NULL );
218     var_AddCallback( p_vout, "video-on-top", OnTopCallback, NULL );
219
220     /* Add a variable to indicate whether we want window decoration or not */
221     var_Create( p_vout, "video-deco", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
222
223     /* Add a fullscreen variable */
224     var_Create( p_vout, "fullscreen", VLC_VAR_BOOL );
225     text.psz_string = _("Fullscreen");
226     var_Change( p_vout, "fullscreen", VLC_VAR_SETTEXT, &text, NULL );
227     var_Change( p_vout, "fullscreen", VLC_VAR_INHERITVALUE, &val, NULL );
228     if( val.b_bool )
229     {
230         /* user requested fullscreen */
231         p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
232     }
233     var_AddCallback( p_vout, "fullscreen", FullscreenCallback, NULL );
234
235     /* Add a snapshot variable */
236     var_Create( p_vout, "video-snapshot", VLC_VAR_VOID | VLC_VAR_ISCOMMAND );
237     text.psz_string = _("Snapshot");
238     var_Change( p_vout, "video-snapshot", VLC_VAR_SETTEXT, &text, NULL );
239     var_AddCallback( p_vout, "video-snapshot", SnapshotCallback, NULL );
240
241     /* Mouse coordinates */
242     var_Create( p_vout, "mouse-x", VLC_VAR_INTEGER );
243     var_Create( p_vout, "mouse-y", VLC_VAR_INTEGER );
244     var_Create( p_vout, "mouse-button-down", VLC_VAR_INTEGER );
245     var_Create( p_vout, "mouse-moved", VLC_VAR_BOOL );
246     var_Create( p_vout, "mouse-clicked", VLC_VAR_INTEGER );
247
248     var_Create( p_vout, "intf-change", VLC_VAR_BOOL );
249     val.b_bool = VLC_TRUE;
250     var_Set( p_vout, "intf-change", val );
251 }
252
253 /*****************************************************************************
254  * vout_Snapshot: generates a snapshot.
255  *****************************************************************************/
256 int vout_Snapshot( vout_thread_t *p_vout, picture_t *p_pic )
257 {
258     image_handler_t *p_image = image_HandlerCreate( p_vout );
259     video_format_t fmt_in = {0}, fmt_out = {0};
260     char *psz_filename;
261     subpicture_t *p_subpic;
262     picture_t *p_pif;
263     vlc_value_t val, format;
264     int i_ret;
265
266     var_Get( p_vout, "snapshot-path", &val );
267     if( val.psz_string && !*val.psz_string )
268     {
269         free( val.psz_string );
270         val.psz_string = 0;
271     }
272
273 #ifdef SYS_DARWIN
274     if( !val.psz_string && p_vout->p_vlc->psz_homedir )
275     {
276         asprintf( &val.psz_string, "%s/Desktop",
277                   p_vout->p_vlc->psz_homedir );
278     }
279
280 #elif defined(WIN32) && !defined(UNDER_CE)
281     if( !val.psz_string && p_vout->p_vlc->psz_homedir )
282     {
283         /* Get the My Pictures folder path */
284
285         char *p_mypicturesdir = NULL;
286         typedef HRESULT (WINAPI *SHGETFOLDERPATH)( HWND, int, HANDLE, DWORD,
287                                                    LPSTR );
288         #ifndef CSIDL_FLAG_CREATE
289         #   define CSIDL_FLAG_CREATE 0x8000
290         #endif
291         #ifndef CSIDL_MYPICTURES
292         #   define CSIDL_MYPICTURES 0x27
293         #endif
294         #ifndef SHGFP_TYPE_CURRENT
295         #   define SHGFP_TYPE_CURRENT 0
296         #endif
297
298         HINSTANCE shfolder_dll;
299         SHGETFOLDERPATH SHGetFolderPath ;
300
301         /* load the shfolder dll to retrieve SHGetFolderPath */
302         if( ( shfolder_dll = LoadLibrary( _T("SHFolder.dll") ) ) != NULL )
303         {
304             SHGetFolderPath = (void *)GetProcAddress( shfolder_dll,
305                                                       _T("SHGetFolderPathA") );
306             if( SHGetFolderPath != NULL )
307             {
308                 p_mypicturesdir = (char *)malloc( MAX_PATH );
309                 if( p_mypicturesdir ) 
310                 {
311
312                     if( S_OK != SHGetFolderPath( NULL,
313                                         CSIDL_MYPICTURES | CSIDL_FLAG_CREATE,
314                                         NULL, SHGFP_TYPE_CURRENT,
315                                         p_mypicturesdir ) )
316                     {
317                         free( p_mypicturesdir );
318                         p_mypicturesdir = NULL;
319                     }
320                 }
321             }
322             FreeLibrary( shfolder_dll );
323         }
324
325         if( p_mypicturesdir == NULL )
326         {
327             asprintf( &val.psz_string, "%s/" CONFIG_DIR,
328                       p_vout->p_vlc->psz_homedir );
329         }
330         else
331         {
332             asprintf( &val.psz_string, p_mypicturesdir );
333             free( p_mypicturesdir );
334         }
335     }
336
337 #else
338     if( !val.psz_string && p_vout->p_vlc->psz_homedir )
339     {
340         asprintf( &val.psz_string, "%s/" CONFIG_DIR,
341                   p_vout->p_vlc->psz_homedir );
342     }
343 #endif
344
345     if( !val.psz_string )
346     {
347         msg_Err( p_vout, "no directory specified for snapshots" );
348         return VLC_EGENERIC;
349     }
350     var_Get( p_vout, "snapshot-format", &format );
351     if( !format.psz_string || !*format.psz_string )
352     {
353         if( format.psz_string ) free( format.psz_string );
354         format.psz_string = strdup( "png" );
355     }
356
357     asprintf( &psz_filename, "%s/vlcsnap-%u.%s", val.psz_string,
358               (unsigned int)(p_pic->date / 100000) & 0xFFFFFF,
359               format.psz_string );
360     free( val.psz_string );
361     free( format.psz_string );
362
363     /* Save the snapshot */
364     fmt_in.i_chroma = p_vout->render.i_chroma;
365     fmt_in.i_width = p_vout->render.i_width;
366     fmt_in.i_height = p_vout->render.i_height;
367     fmt_in.i_sar_num = p_vout->fmt_render.i_sar_num;
368     fmt_in.i_sar_den = p_vout->fmt_render.i_sar_den;
369     fmt_out.i_sar_num = fmt_out.i_sar_den = 1;
370     i_ret = image_WriteUrl( p_image, p_pic, &fmt_in, &fmt_out, psz_filename );
371     if( i_ret != VLC_SUCCESS )
372     {
373         msg_Err( p_vout, "could not create snapshot %s", psz_filename );
374         free( psz_filename );
375         image_HandlerDelete( p_image );
376         return VLC_EGENERIC;
377     }
378
379     msg_Dbg( p_vout, "snapshot taken (%s)", psz_filename );
380     free( psz_filename );
381
382     /* Inject a subpicture with the snapshot */
383     fmt_out.i_chroma = VLC_FOURCC('Y','U','V','A');
384     fmt_out.i_width = fmt_out.i_visible_width = p_vout->render.i_width;
385     fmt_out.i_height = fmt_out.i_visible_height = p_vout->render.i_height;
386     fmt_out.i_aspect = VOUT_ASPECT_FACTOR;
387     p_pif = image_Convert( p_image, p_pic, &fmt_in, &fmt_out );
388     image_HandlerDelete( p_image );
389     if( !p_pif ) return VLC_EGENERIC;
390
391     p_subpic = spu_CreateSubpicture( p_vout->p_spu );
392     if( p_subpic == NULL )
393     {
394          p_pif->pf_release( p_pif );
395          return VLC_EGENERIC;
396     }
397
398     p_subpic->i_channel = 0;
399     p_subpic->i_start = mdate();
400     p_subpic->i_stop = mdate() + 4000000;
401     p_subpic->b_ephemer = VLC_TRUE;
402     p_subpic->b_fade = VLC_TRUE;
403     p_subpic->i_original_picture_width = p_vout->render.i_width * 4;
404     p_subpic->i_original_picture_height = p_vout->render.i_height * 4;
405
406     p_subpic->p_region = spu_CreateRegion( p_vout->p_spu, &fmt_out );
407     vout_CopyPicture( p_image->p_parent, &p_subpic->p_region->picture, p_pif );
408     p_pif->pf_release( p_pif );
409
410     spu_DisplaySubpicture( p_vout->p_spu, p_subpic );
411
412     return VLC_SUCCESS;
413 }
414
415 /*****************************************************************************
416  * vout_ControlDefault: default methods for video output control.
417  *****************************************************************************/
418 int vout_vaControlDefault( vout_thread_t *p_vout, int i_query, va_list args )
419 {
420     switch( i_query )
421     {
422     case VOUT_REPARENT:
423     case VOUT_CLOSE:
424         if( p_vout->p_parent_intf )
425         {
426             vlc_object_release( p_vout->p_parent_intf );
427             p_vout->p_parent_intf = NULL;
428         }
429         return VLC_SUCCESS;
430         break;
431
432     case VOUT_SNAPSHOT:
433         p_vout->b_snapshot = VLC_TRUE;
434         return VLC_SUCCESS;
435         break;
436
437     default:
438         msg_Dbg( p_vout, "control query not supported" );
439         return VLC_EGENERIC;
440     }
441 }
442
443 /*****************************************************************************
444  * Object variables callbacks
445  *****************************************************************************/
446 static int ZoomCallback( vlc_object_t *p_this, char const *psz_cmd,
447                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
448 {
449     vout_thread_t *p_vout = (vout_thread_t *)p_this;
450     vout_Control( p_vout, VOUT_SET_ZOOM, newval.f_float );
451     return VLC_SUCCESS;
452 }
453
454 static int OnTopCallback( vlc_object_t *p_this, char const *psz_cmd,
455                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
456 {
457     vout_thread_t *p_vout = (vout_thread_t *)p_this;
458     playlist_t *p_playlist;
459     vout_Control( p_vout, VOUT_SET_STAY_ON_TOP, newval.b_bool );
460
461     p_playlist = (playlist_t *)vlc_object_find( p_this, VLC_OBJECT_PLAYLIST,
462                                                  FIND_PARENT );
463     if( p_playlist )
464     {
465         /* Modify playlist as well because the vout might have to be restarted */
466         var_Create( p_playlist, "video-on-top", VLC_VAR_BOOL );
467         var_Set( p_playlist, "video-on-top", newval );
468
469         vlc_object_release( p_playlist );
470     }
471     return VLC_SUCCESS;
472 }
473
474 static int FullscreenCallback( vlc_object_t *p_this, char const *psz_cmd,
475                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
476 {
477     vout_thread_t *p_vout = (vout_thread_t *)p_this;
478     playlist_t *p_playlist;
479     vlc_value_t val;
480
481     p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
482
483     p_playlist = (playlist_t *)vlc_object_find( p_this, VLC_OBJECT_PLAYLIST,
484                                                  FIND_PARENT );
485     if( p_playlist )
486     {
487         /* Modify playlist as well because the vout might have to be restarted */
488         var_Create( p_playlist, "fullscreen", VLC_VAR_BOOL );
489         var_Set( p_playlist, "fullscreen", newval );
490
491         vlc_object_release( p_playlist );
492     }
493
494     /* Disable "always on top" in fullscreen mode */
495     var_Get( p_vout, "video-on-top", &val );
496     if( newval.b_bool && val.b_bool )
497     {
498         val.b_bool = VLC_FALSE;
499         vout_Control( p_vout, VOUT_SET_STAY_ON_TOP, val.b_bool );
500     }
501     else if( !newval.b_bool && val.b_bool )
502     {
503         vout_Control( p_vout, VOUT_SET_STAY_ON_TOP, val.b_bool );
504     }
505
506     val.b_bool = VLC_TRUE;
507     var_Set( p_vout, "intf-change", val );
508     return VLC_SUCCESS;
509 }
510
511 static int SnapshotCallback( vlc_object_t *p_this, char const *psz_cmd,
512                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
513 {
514     vout_thread_t *p_vout = (vout_thread_t *)p_this;
515     vout_Control( p_vout, VOUT_SNAPSHOT );
516     return VLC_SUCCESS;
517 }