]> git.sesse.net Git - vlc/blob - modules/gui/wxwindows/video.cpp
* modules/gui/wxwindows/menus.cpp: release the wxMutexGui lock before triggering...
[vlc] / modules / gui / wxwindows / video.cpp
1 /*****************************************************************************
2  * video.cpp : wxWindows plugin for vlc
3  *****************************************************************************
4  * Copyright (C) 2000-2004, 2003 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 <vlc/vlc.h>
28 #include <vlc/vout.h>
29 #include <vlc/intf.h>
30 #include "stream_control.h"
31
32 #include "wxwindows.h"
33
34 static void *GetWindow( intf_thread_t *p_intf, vout_thread_t *,
35                         int *pi_x_hint, int *pi_y_hint,
36                         unsigned int *pi_width_hint,
37                         unsigned int *pi_height_hint );
38
39 static void ReleaseWindow( intf_thread_t *p_intf, void *p_window );
40
41 static int ControlWindow( intf_thread_t *p_intf, void *p_window,
42                           int i_query, va_list args );
43
44 /* IDs for the controls and the menu commands */
45 enum
46 {
47     UpdateSize_Event = wxID_HIGHEST + 1,
48     UpdateHide_Event,
49     SetStayOnTop_Event,
50 };
51
52 class VideoWindow: public wxWindow
53 {
54 public:
55     /* Constructor */
56     VideoWindow( intf_thread_t *_p_intf, wxWindow *p_parent );
57     virtual ~VideoWindow();
58
59     void *GetWindow( vout_thread_t *p_vout, int *, int *,
60                      unsigned int *, unsigned int * );
61     void ReleaseWindow( void * );
62     int  ControlWindow( void *, int, va_list );
63
64 private:
65     intf_thread_t *p_intf;
66     vout_thread_t *p_vout;
67     wxWindow *p_parent;
68     vlc_mutex_t lock;
69
70     wxWindow *p_child_window;
71
72     void UpdateSize( wxSizeEvent & );
73     void UpdateHide( wxSizeEvent & );
74     void OnControlEvent( wxCommandEvent & );
75
76     DECLARE_EVENT_TABLE();
77 };
78
79 DEFINE_LOCAL_EVENT_TYPE( wxEVT_VLC_VIDEO );
80
81 BEGIN_EVENT_TABLE(VideoWindow, wxWindow)
82     EVT_CUSTOM( wxEVT_SIZE, UpdateSize_Event, VideoWindow::UpdateSize )
83     EVT_CUSTOM( wxEVT_SIZE, UpdateHide_Event, VideoWindow::UpdateHide )
84     EVT_COMMAND( SetStayOnTop_Event, wxEVT_VLC_VIDEO,
85                  VideoWindow::OnControlEvent )
86 END_EVENT_TABLE()
87
88 /*****************************************************************************
89  * Public methods.
90  *****************************************************************************/
91 wxWindow *VideoWindow( intf_thread_t *p_intf, wxWindow *p_parent )
92 {
93     return new VideoWindow::VideoWindow( p_intf, p_parent );
94 }
95
96 /*****************************************************************************
97  * Constructor.
98  *****************************************************************************/
99 VideoWindow::VideoWindow( intf_thread_t *_p_intf, wxWindow *_p_parent ):
100     wxWindow( _p_parent, -1 )
101 {
102     /* Initializations */
103     p_intf = _p_intf;
104     p_parent = _p_parent;
105
106     vlc_mutex_init( p_intf, &lock );
107
108     p_vout = NULL;
109
110     p_intf->pf_request_window = ::GetWindow;
111     p_intf->pf_release_window = ::ReleaseWindow;
112     p_intf->pf_control_window = ::ControlWindow;
113
114     p_intf->p_sys->p_video_window = this;
115     p_child_window = new wxWindow( this, -1, wxDefaultPosition, wxSize(0,0) );
116     p_child_window->Show();
117     Show();
118
119     p_intf->p_sys->p_video_sizer = new wxBoxSizer( wxHORIZONTAL );
120     p_intf->p_sys->p_video_sizer->Add( this, 1, wxEXPAND );
121
122     ReleaseWindow( NULL );
123 }
124
125 VideoWindow::~VideoWindow()
126 {
127     vlc_mutex_lock( &lock );
128     if( p_vout )
129     {
130         if( vout_Control( p_vout, VOUT_REPARENT ) != VLC_SUCCESS )
131             vout_Control( p_vout, VOUT_CLOSE );
132     }
133
134     p_intf->pf_request_window = NULL;
135     p_intf->pf_release_window = NULL;
136     p_intf->pf_control_window = NULL;
137     vlc_mutex_unlock( &lock );
138
139     vlc_mutex_destroy( &lock );
140 }
141
142 /*****************************************************************************
143  * Private methods.
144  *****************************************************************************/
145 static void *GetWindow( intf_thread_t *p_intf, vout_thread_t *p_vout,
146                         int *pi_x_hint, int *pi_y_hint,
147                         unsigned int *pi_width_hint,
148                         unsigned int *pi_height_hint )
149 {
150     return p_intf->p_sys->p_video_window->GetWindow( p_vout,
151                                                      pi_x_hint, pi_y_hint,
152                                                      pi_width_hint,
153                                                      pi_height_hint );
154 }
155
156 /* Part of the hack to get the X11 window handle from the GtkWidget */
157 #ifdef __WXGTK__
158 extern "C" {
159 #ifdef __WXGTK20__
160     int gdk_x11_drawable_get_xid( void * );
161 #endif
162     void *gtk_widget_get_parent_window( void * );
163 }
164 #endif
165
166 void *VideoWindow::GetWindow( vout_thread_t *_p_vout,
167                               int *pi_x_hint, int *pi_y_hint,
168                               unsigned int *pi_width_hint,
169                               unsigned int *pi_height_hint )
170 {
171 #if defined(__WXGTK__) || defined(WIN32)
172     vlc_mutex_lock( &lock );
173
174     if( p_vout )
175     {
176         vlc_mutex_unlock( &lock );
177         msg_Dbg( p_intf, "Video window already in use" );
178         return NULL;
179     }
180
181     p_vout = _p_vout;
182
183     wxSizeEvent event( wxSize(*pi_width_hint, *pi_height_hint),
184                        UpdateSize_Event );
185     AddPendingEvent( event );
186     vlc_mutex_unlock( &lock );
187
188 #ifdef __WXGTK__
189     GtkWidget *p_widget = p_child_window->GetHandle();
190
191 #ifdef __WXGTK20__
192     return (void *)gdk_x11_drawable_get_xid(
193                gtk_widget_get_parent_window( p_widget ) );
194 #elif defined(__WXGTK__)
195     return (void *)*(int *)( (char *)gtk_widget_get_parent_window( p_widget )
196                + 2 * sizeof(void *) );
197 #endif
198
199 #elif defined(WIN32)
200     return (void*)GetHandle();
201
202 #endif
203
204 #else // defined(__WXGTK__) || defined(WIN32)
205     return NULL;
206
207 #endif
208 }
209
210 static void ReleaseWindow( intf_thread_t *p_intf, void *p_window )
211 {
212     return p_intf->p_sys->p_video_window->ReleaseWindow( p_window );
213 }
214
215 void VideoWindow::ReleaseWindow( void *p_window )
216 {
217     vlc_mutex_lock( &lock );
218
219     p_vout = NULL;
220
221 #if defined(__WXGTK__) || defined(WIN32)
222     wxSizeEvent event( wxSize(0, 0), UpdateHide_Event );
223     AddPendingEvent( event );
224 #endif
225
226     vlc_mutex_unlock( &lock );
227 }
228
229 void VideoWindow::UpdateSize( wxSizeEvent &event )
230 {
231     if( !IsShown() )
232     {
233         p_intf->p_sys->p_video_sizer->Show( this, TRUE );
234         p_intf->p_sys->p_video_sizer->Layout();
235         SetFocus();
236     }
237     p_intf->p_sys->p_video_sizer->SetMinSize( event.GetSize() );
238
239     wxCommandEvent intf_event( wxEVT_INTF, 0 );
240     p_parent->AddPendingEvent( intf_event );
241 }
242
243 void VideoWindow::UpdateHide( wxSizeEvent &event )
244 {
245     if( IsShown() )
246     {
247         p_intf->p_sys->p_video_sizer->Show( this, FALSE );
248         p_intf->p_sys->p_video_sizer->Layout();
249     }
250     p_intf->p_sys->p_video_sizer->SetMinSize( event.GetSize() );
251
252     wxCommandEvent intf_event( wxEVT_INTF, 0 );
253     p_parent->AddPendingEvent( intf_event );
254 }
255
256 void VideoWindow::OnControlEvent( wxCommandEvent &event )
257 {
258     switch( event.GetId() )
259     {
260     case SetStayOnTop_Event:
261         wxCommandEvent intf_event( wxEVT_INTF, 1 );
262         intf_event.SetInt( event.GetInt() );
263         p_parent->AddPendingEvent( intf_event );
264         break;
265     }
266 }
267
268 static int ControlWindow( intf_thread_t *p_intf, void *p_window,
269                           int i_query, va_list args )
270 {
271     return p_intf->p_sys->p_video_window->ControlWindow( p_window, i_query,
272                                                          args );
273 }
274
275 int VideoWindow::ControlWindow( void *p_window, int i_query, va_list args )
276 {
277     int i_ret = VLC_EGENERIC;
278
279     vlc_mutex_lock( &lock );
280
281     switch( i_query )
282     {
283         case VOUT_SET_ZOOM:
284         {
285             double f_arg = va_arg( args, double );
286
287             /* Update dimensions */
288             wxSizeEvent event( wxSize(p_vout->i_window_width * f_arg,
289                                       p_vout->i_window_height * f_arg),
290                                UpdateSize_Event );
291             AddPendingEvent( event );
292
293             i_ret = VLC_SUCCESS;
294         }
295         break;
296
297         case VOUT_SET_STAY_ON_TOP:
298         {
299             int i_arg = va_arg( args, int );
300             wxCommandEvent event( wxEVT_VLC_VIDEO, SetStayOnTop_Event );
301             event.SetInt( i_arg );
302             AddPendingEvent( event );
303
304             i_ret = VLC_SUCCESS;
305         }
306         break;
307
308         default:
309             msg_Dbg( p_intf, "control query not supported" );
310             break;
311     }
312
313     vlc_mutex_unlock( &lock );
314
315     return i_ret;
316 }