]> git.sesse.net Git - vlc/blob - src/video_output/vout_intf.c
be3efabcb28400879011aae18c21c0d907041441
[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
35 /*****************************************************************************
36  * Local prototypes
37  *****************************************************************************/
38
39 /* Object variables callbacks */
40 static int ZoomCallback( vlc_object_t *, char const *,
41                          vlc_value_t, vlc_value_t, void * );
42 static int OnTopCallback( vlc_object_t *, char const *,
43                           vlc_value_t, vlc_value_t, void * );
44 static int FullscreenCallback( vlc_object_t *, char const *,
45                                vlc_value_t, vlc_value_t, void * );
46
47 /*****************************************************************************
48  * vout_RequestWindow: Create/Get a video window if possible.
49  *****************************************************************************
50  * This function looks for the main interface and tries to request
51  * a new video window. If it fails then the vout will still need to create the
52  * window by itself.
53  *****************************************************************************/
54 void *vout_RequestWindow( vout_thread_t *p_vout,
55                           int *pi_x_hint, int *pi_y_hint,
56                           unsigned int *pi_width_hint,
57                           unsigned int *pi_height_hint )
58 {
59     intf_thread_t *p_intf = NULL;
60     vlc_list_t *p_list;
61     void *p_window;
62     vlc_value_t val;
63     int i;
64
65     /* Small kludge */
66     if( !var_Type( p_vout, "aspect-ratio" ) ) vout_IntfInit( p_vout );
67
68     /* Get requested coordinates */
69     var_Get( p_vout, "video-x", &val );
70     *pi_x_hint = val.i_int ;
71     var_Get( p_vout, "video-y", &val );
72     *pi_y_hint = val.i_int;
73
74     *pi_width_hint = p_vout->i_window_width;
75     *pi_height_hint = p_vout->i_window_height;
76
77     /* Check whether someone provided us with a window ID */
78     var_Get( p_vout->p_vlc, "drawable", &val );
79     if( val.i_int ) return (void *)val.i_int;
80
81     /* Find if the main interface supports embedding */
82     p_list = vlc_list_find( p_vout, VLC_OBJECT_INTF, FIND_ANYWHERE );
83     if( !p_list ) return NULL;
84
85     for( i = 0; i < p_list->i_count; i++ )
86     {
87         p_intf = (intf_thread_t *)p_list->p_values[i].p_object;
88         if( p_intf->b_block && p_intf->pf_request_window ) break;
89         p_intf = NULL;
90     }
91
92     if( !p_intf )
93     {
94         vlc_list_release( p_list );
95         return NULL;
96     }
97
98     vlc_object_yield( p_intf );
99     vlc_list_release( p_list );
100
101     p_window = p_intf->pf_request_window( p_intf, p_vout, pi_x_hint, pi_y_hint,
102                                           pi_width_hint, pi_height_hint );
103
104     if( !p_window ) vlc_object_release( p_intf );
105     else p_vout->p_parent_intf = p_intf;
106
107     return p_window;
108 }
109
110 void vout_ReleaseWindow( vout_thread_t *p_vout, void *p_window )
111 {
112     intf_thread_t *p_intf = p_vout->p_parent_intf;
113
114     if( !p_intf ) return;
115
116     vlc_mutex_lock( &p_intf->object_lock );
117     if( p_intf->b_dead )
118     {
119         vlc_mutex_unlock( &p_intf->object_lock );
120         return;
121     }
122
123     if( !p_intf->pf_release_window )
124     {
125         msg_Err( p_vout, "no pf_release_window");
126         vlc_mutex_unlock( &p_intf->object_lock );
127         vlc_object_release( p_intf );
128         return;
129     }
130
131     p_intf->pf_release_window( p_intf, p_window );
132
133     p_vout->p_parent_intf = NULL;
134     vlc_mutex_unlock( &p_intf->object_lock );
135     vlc_object_release( p_intf );
136 }
137
138 int vout_ControlWindow( vout_thread_t *p_vout, void *p_window,
139                         int i_query, va_list args )
140 {
141     intf_thread_t *p_intf = p_vout->p_parent_intf;
142     int i_ret;
143
144     if( !p_intf ) return VLC_EGENERIC;
145
146     vlc_mutex_lock( &p_intf->object_lock );
147     if( p_intf->b_dead )
148     {
149         vlc_mutex_unlock( &p_intf->object_lock );
150         return VLC_EGENERIC;
151     }
152
153     if( !p_intf->pf_control_window )
154     {
155         msg_Err( p_vout, "no pf_control_window");
156         vlc_mutex_unlock( &p_intf->object_lock );
157         return VLC_EGENERIC;
158     }
159
160     i_ret = p_intf->pf_control_window( p_intf, p_window, i_query, args );
161     vlc_mutex_unlock( &p_intf->object_lock );
162     return i_ret;
163 }
164
165 /*****************************************************************************
166  * vout_IntfInit: called during the vout creation to initialise misc things.
167  *****************************************************************************/
168 void vout_IntfInit( vout_thread_t *p_vout )
169 {
170     vlc_value_t val, text, old_val;
171
172     /* Create a few object variables we'll need later on */
173     var_Create( p_vout, "aspect-ratio", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
174     var_Create( p_vout, "width", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
175     var_Create( p_vout, "height", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
176     var_Create( p_vout, "align", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
177     var_Create( p_vout, "video-x", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
178     var_Create( p_vout, "video-y", VLC_VAR_INTEGER | VLC_VAR_DOINHERIT );
179
180     var_Create( p_vout, "zoom", VLC_VAR_FLOAT | VLC_VAR_ISCOMMAND |
181                 VLC_VAR_HASCHOICE | VLC_VAR_DOINHERIT );
182
183     text.psz_string = _("Zoom");
184     var_Change( p_vout, "zoom", VLC_VAR_SETTEXT, &text, NULL );
185
186     var_Get( p_vout, "zoom", &old_val );
187     if( old_val.f_float == 0.25 ||
188         old_val.f_float == 0.5 ||
189         old_val.f_float == 1 ||
190         old_val.f_float == 2 )
191     {
192         var_Change( p_vout, "zoom", VLC_VAR_DELCHOICE, &old_val, NULL );
193     }
194
195     val.f_float = 0.25; text.psz_string = _("1:4 Quarter");
196     var_Change( p_vout, "zoom", VLC_VAR_ADDCHOICE, &val, &text );
197     val.f_float = 0.5; text.psz_string = _("1:2 Half");
198     var_Change( p_vout, "zoom", VLC_VAR_ADDCHOICE, &val, &text );
199     val.f_float = 1; text.psz_string = _("1:1 Original");
200     var_Change( p_vout, "zoom", VLC_VAR_ADDCHOICE, &val, &text );
201     val.f_float = 2; text.psz_string = _("2:1 Double");
202     var_Change( p_vout, "zoom", VLC_VAR_ADDCHOICE, &val, &text );
203
204     var_Set( p_vout, "zoom", old_val );
205
206     var_AddCallback( p_vout, "zoom", ZoomCallback, NULL );
207
208     /* Add a variable to indicate if the window should be on top of others */
209     var_Create( p_vout, "video-on-top", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
210     text.psz_string = _("Always on top");
211     var_Change( p_vout, "video-on-top", VLC_VAR_SETTEXT, &text, NULL );
212     var_AddCallback( p_vout, "video-on-top", OnTopCallback, NULL );
213
214     /* Add a variable to indicate whether we want window decoration or not */
215     var_Create( p_vout, "video-deco", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
216
217     /* Add a fullscreen variable */
218     var_Create( p_vout, "fullscreen", VLC_VAR_BOOL );
219     text.psz_string = _("Fullscreen");
220     var_Change( p_vout, "fullscreen", VLC_VAR_SETTEXT, &text, NULL );
221     var_Change( p_vout, "fullscreen", VLC_VAR_INHERITVALUE, &val, NULL );
222     if( val.b_bool )
223     {
224         /* user requested fullscreen */
225         p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
226     }
227     var_AddCallback( p_vout, "fullscreen", FullscreenCallback, NULL );
228
229     /* Mouse coordinates */
230     var_Create( p_vout, "mouse-x", VLC_VAR_INTEGER );
231     var_Create( p_vout, "mouse-y", VLC_VAR_INTEGER );
232     var_Create( p_vout, "mouse-button-down", VLC_VAR_INTEGER );
233     var_Create( p_vout, "mouse-moved", VLC_VAR_BOOL );
234     var_Create( p_vout, "mouse-clicked", VLC_VAR_INTEGER );
235
236     var_Create( p_vout, "intf-change", VLC_VAR_BOOL );
237     val.b_bool = VLC_TRUE;
238     var_Set( p_vout, "intf-change", val );
239 }
240
241 /*****************************************************************************
242  * vout_ControlDefault: default methods for video output control.
243  *****************************************************************************/
244 int vout_vaControlDefault( vout_thread_t *p_vout, int i_query, va_list args )
245 {
246     switch( i_query )
247     {
248     case VOUT_REPARENT:
249     case VOUT_CLOSE:
250         if( p_vout->p_parent_intf )
251         {
252             vlc_object_release( p_vout->p_parent_intf );
253             p_vout->p_parent_intf = NULL;
254         }
255         return VLC_SUCCESS;
256         break;
257
258     default:
259         msg_Dbg( p_vout, "control query not supported" );
260         return VLC_EGENERIC;
261     }
262 }
263
264 /*****************************************************************************
265  * Object variables callbacks
266  *****************************************************************************/
267 static int ZoomCallback( vlc_object_t *p_this, char const *psz_cmd,
268                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
269 {
270     vout_thread_t *p_vout = (vout_thread_t *)p_this;
271     vout_Control( p_vout, VOUT_SET_ZOOM, newval.f_float );
272     return VLC_SUCCESS;
273 }
274
275 static int OnTopCallback( vlc_object_t *p_this, char const *psz_cmd,
276                          vlc_value_t oldval, vlc_value_t newval, void *p_data )
277 {
278     vout_thread_t *p_vout = (vout_thread_t *)p_this;
279     playlist_t *p_playlist;
280     vout_Control( p_vout, VOUT_SET_STAY_ON_TOP, newval.b_bool );
281
282     p_playlist = (playlist_t *)vlc_object_find( p_this, VLC_OBJECT_PLAYLIST,
283                                                  FIND_PARENT );
284     if( p_playlist )
285     {
286         /* Modify playlist as well because the vout might have to be restarted */
287         var_Create( p_playlist, "video-on-top", VLC_VAR_BOOL );
288         var_Set( p_playlist, "video-on-top", newval );
289
290         vlc_object_release( p_playlist );
291     }
292     return VLC_SUCCESS;
293 }
294
295 static int FullscreenCallback( vlc_object_t *p_this, char const *psz_cmd,
296                        vlc_value_t oldval, vlc_value_t newval, void *p_data )
297 {
298     vout_thread_t *p_vout = (vout_thread_t *)p_this;
299     playlist_t *p_playlist;
300     vlc_value_t val;
301
302     p_vout->i_changes |= VOUT_FULLSCREEN_CHANGE;
303
304     p_playlist = (playlist_t *)vlc_object_find( p_this, VLC_OBJECT_PLAYLIST,
305                                                  FIND_PARENT );
306     if( p_playlist )
307     {
308         /* Modify playlist as well because the vout might have to be restarted */
309         var_Create( p_playlist, "fullscreen", VLC_VAR_BOOL );
310         var_Set( p_playlist, "fullscreen", newval );
311
312         vlc_object_release( p_playlist );
313     }
314
315     /* Disable "always on top" in fullscreen mode */
316     var_Get( p_vout, "video-on-top", &val );
317     if( newval.b_bool && val.b_bool )
318     {
319         val.b_bool = VLC_FALSE;
320         vout_Control( p_vout, VOUT_SET_STAY_ON_TOP, val.b_bool );
321     }
322     else if( !newval.b_bool && val.b_bool )
323     {
324         vout_Control( p_vout, VOUT_SET_STAY_ON_TOP, val.b_bool );
325     }
326
327     val.b_bool = VLC_TRUE;
328     var_Set( p_vout, "intf-change", val );
329     return VLC_SUCCESS;
330 }