1 /*****************************************************************************
2 * glwin32.c: Windows OpenGL provider
3 *****************************************************************************
4 * Copyright (C) 2001-2004 the VideoLAN team
7 * Authors: Gildas Bazin <gbazin@videolan.org>
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.
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.
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22 *****************************************************************************/
24 #include <errno.h> /* ENOMEM */
31 #include <vlc_interface.h>
39 #undef GetSystemMetrics
41 #ifndef MONITOR_DEFAULTTONEAREST
42 # define MONITOR_DEFAULTTONEAREST 2
49 /*****************************************************************************
51 *****************************************************************************/
52 static int OpenVideo ( vlc_object_t * );
53 static void CloseVideo ( vlc_object_t * );
55 static int Init ( vout_thread_t * );
56 static void End ( vout_thread_t * );
57 static int Manage ( vout_thread_t * );
58 static void GLSwapBuffers( vout_thread_t * );
59 static void FirstSwap( vout_thread_t * );
61 /*****************************************************************************
63 *****************************************************************************/
65 set_category( CAT_VIDEO );
66 set_subcategory( SUBCAT_VIDEO_VOUT );
67 set_shortname( "OpenGL" );
68 set_description( _("OpenGL video output") );
69 set_capability( "opengl provider", 100 );
70 add_shortcut( "glwin32" );
71 set_callbacks( OpenVideo, CloseVideo );
73 /* FIXME: Hack to avoid unregistering our window class */
74 linked_with_a_crap_library_which_uses_atexit( );
78 /* check if we registered a window class because we need to
81 if( GetClassInfo( GetModuleHandle(NULL), "VLC DirectX", &wndclass ) )
82 UnregisterClass( "VLC DirectX", GetModuleHandle(NULL) );
85 /*****************************************************************************
86 * OpenVideo: allocate OpenGL provider
87 *****************************************************************************
88 * This function creates and initializes a video window.
89 *****************************************************************************/
90 static int OpenVideo( vlc_object_t *p_this )
92 vout_thread_t * p_vout = (vout_thread_t *)p_this;
95 /* Allocate structure */
96 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
97 if( p_vout->p_sys == NULL )
99 msg_Err( p_vout, "out of memory" );
102 memset( p_vout->p_sys, 0, sizeof( vout_sys_t ) );
104 /* Initialisations */
105 p_vout->pf_init = Init;
106 p_vout->pf_end = End;
107 p_vout->pf_manage = Manage;
108 p_vout->pf_swap = FirstSwap;
110 p_vout->p_sys->hwnd = p_vout->p_sys->hvideownd = NULL;
111 p_vout->p_sys->hparent = p_vout->p_sys->hfswnd = NULL;
112 p_vout->p_sys->i_changes = 0;
113 vlc_mutex_init( p_vout, &p_vout->p_sys->lock );
114 SetRectEmpty( &p_vout->p_sys->rect_display );
115 SetRectEmpty( &p_vout->p_sys->rect_parent );
117 var_Create( p_vout, "video-title", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
119 p_vout->p_sys->b_cursor_hidden = 0;
120 p_vout->p_sys->i_lastmoved = mdate();
121 p_vout->p_sys->i_mouse_hide_timeout =
122 var_GetInteger(p_vout, "mouse-hide-timeout") * 1000;
124 /* Set main window's size */
125 p_vout->p_sys->i_window_width = p_vout->i_window_width;
126 p_vout->p_sys->i_window_height = p_vout->i_window_height;
128 /* Create the Vout EventThread, this thread is created by us to isolate
129 * the Win32 PeekMessage function calls. We want to do this because
130 * Windows can stay blocked inside this call for a long time, and when
131 * this happens it thus blocks vlc's video_output thread.
132 * Vout EventThread will take care of the creation of the video
133 * window (because PeekMessage has to be called from the same thread which
134 * created the window). */
135 msg_Dbg( p_vout, "creating Vout EventThread" );
136 p_vout->p_sys->p_event =
137 vlc_object_create( p_vout, sizeof(event_thread_t) );
138 p_vout->p_sys->p_event->p_vout = p_vout;
139 if( vlc_thread_create( p_vout->p_sys->p_event, "Vout Events Thread",
140 E_(EventThread), 0, 1 ) )
142 msg_Err( p_vout, "cannot create Vout EventThread" );
143 vlc_object_release( p_vout->p_sys->p_event );
144 p_vout->p_sys->p_event = NULL;
148 if( p_vout->p_sys->p_event->b_error )
150 msg_Err( p_vout, "Vout EventThread failed" );
154 vlc_object_attach( p_vout->p_sys->p_event, p_vout );
156 msg_Dbg( p_vout, "Vout EventThread running" );
158 /* Variable to indicate if the window should be on top of others */
159 /* Trigger a callback right now */
160 var_Get( p_vout, "video-on-top", &val );
161 var_Set( p_vout, "video-on-top", val );
166 CloseVideo( VLC_OBJECT(p_vout) );
170 /*****************************************************************************
171 * Init: initialize video thread output method
172 *****************************************************************************/
173 static int Init( vout_thread_t *p_vout )
175 PIXELFORMATDESCRIPTOR pfd;
178 /* Change the window title bar text */
179 PostMessage( p_vout->p_sys->hwnd, WM_VLC_CHANGE_TEXT, 0, 0 );
181 p_vout->p_sys->hGLDC = GetDC( p_vout->p_sys->hvideownd );
183 /* Set the pixel format for the DC */
184 memset( &pfd, 0, sizeof( pfd ) );
185 pfd.nSize = sizeof( pfd );
187 pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
188 pfd.iPixelType = PFD_TYPE_RGBA;
191 pfd.iLayerType = PFD_MAIN_PLANE;
192 iFormat = ChoosePixelFormat( p_vout->p_sys->hGLDC, &pfd );
193 SetPixelFormat( p_vout->p_sys->hGLDC, iFormat, &pfd );
195 /* Create and enable the render context */
196 p_vout->p_sys->hGLRC = wglCreateContext( p_vout->p_sys->hGLDC );
197 wglMakeCurrent( p_vout->p_sys->hGLDC, p_vout->p_sys->hGLRC );
202 /*****************************************************************************
203 * End: terminate Sys video thread output method
204 *****************************************************************************
205 * Terminate an output method created by Create.
206 * It is called at the end of the thread.
207 *****************************************************************************/
208 static void End( vout_thread_t *p_vout )
210 wglMakeCurrent( NULL, NULL );
211 wglDeleteContext( p_vout->p_sys->hGLRC );
212 ReleaseDC( p_vout->p_sys->hvideownd, p_vout->p_sys->hGLDC );
216 /*****************************************************************************
217 * CloseVideo: destroy Sys video thread output method
218 *****************************************************************************
219 * Terminate an output method created by Create
220 *****************************************************************************/
221 static void CloseVideo( vlc_object_t *p_this )
223 vout_thread_t * p_vout = (vout_thread_t *)p_this;
225 msg_Dbg( p_vout, "closing video" );
227 if( p_vout->p_sys->p_event )
229 vlc_object_detach( p_vout->p_sys->p_event );
231 /* Kill Vout EventThread */
232 vlc_object_kill( p_vout->p_sys->p_event );
234 /* we need to be sure Vout EventThread won't stay stuck in
235 * GetMessage, so we send a fake message */
236 if( p_vout->p_sys->hwnd )
238 PostMessage( p_vout->p_sys->hwnd, WM_NULL, 0, 0);
241 vlc_thread_join( p_vout->p_sys->p_event );
242 vlc_object_release( p_vout->p_sys->p_event );
245 vlc_mutex_destroy( &p_vout->p_sys->lock );
249 free( p_vout->p_sys );
250 p_vout->p_sys = NULL;
254 /*****************************************************************************
255 * Manage: handle Sys events
256 *****************************************************************************
257 * This function should be called regularly by the video output thread.
258 * It returns a non null value if an error occurred.
259 *****************************************************************************/
260 static int Manage( vout_thread_t *p_vout )
262 int i_width = p_vout->p_sys->rect_dest.right -
263 p_vout->p_sys->rect_dest.left;
264 int i_height = p_vout->p_sys->rect_dest.bottom -
265 p_vout->p_sys->rect_dest.top;
266 glViewport( 0, 0, i_width, i_height );
268 /* If we do not control our window, we check for geometry changes
269 * ourselves because the parent might not send us its events. */
270 vlc_mutex_lock( &p_vout->p_sys->lock );
271 if( p_vout->p_sys->hparent && !p_vout->b_fullscreen )
276 vlc_mutex_unlock( &p_vout->p_sys->lock );
278 GetClientRect( p_vout->p_sys->hparent, &rect_parent );
279 point.x = point.y = 0;
280 ClientToScreen( p_vout->p_sys->hparent, &point );
281 OffsetRect( &rect_parent, point.x, point.y );
283 if( !EqualRect( &rect_parent, &p_vout->p_sys->rect_parent ) )
285 p_vout->p_sys->rect_parent = rect_parent;
287 /* This one is to force the update even if only
288 * the position has changed */
289 SetWindowPos( p_vout->p_sys->hwnd, 0, 1, 1,
290 rect_parent.right - rect_parent.left,
291 rect_parent.bottom - rect_parent.top, 0 );
293 SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
294 rect_parent.right - rect_parent.left,
295 rect_parent.bottom - rect_parent.top, 0 );
300 vlc_mutex_unlock( &p_vout->p_sys->lock );
303 /* Check for cropping / aspect changes */
304 if( p_vout->i_changes & VOUT_CROP_CHANGE ||
305 p_vout->i_changes & VOUT_ASPECT_CHANGE )
307 p_vout->i_changes &= ~VOUT_CROP_CHANGE;
308 p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
310 p_vout->fmt_out.i_x_offset = p_vout->fmt_in.i_x_offset;
311 p_vout->fmt_out.i_y_offset = p_vout->fmt_in.i_y_offset;
312 p_vout->fmt_out.i_visible_width = p_vout->fmt_in.i_visible_width;
313 p_vout->fmt_out.i_visible_height = p_vout->fmt_in.i_visible_height;
314 p_vout->fmt_out.i_aspect = p_vout->fmt_in.i_aspect;
315 p_vout->fmt_out.i_sar_num = p_vout->fmt_in.i_sar_num;
316 p_vout->fmt_out.i_sar_den = p_vout->fmt_in.i_sar_den;
317 p_vout->output.i_aspect = p_vout->fmt_in.i_aspect;
318 E_(UpdateRects)( p_vout, VLC_TRUE );
321 /* We used to call the Win32 PeekMessage function here to read the window
322 * messages. But since window can stay blocked into this function for a
323 * long time (for example when you move your window on the screen), I
324 * decided to isolate PeekMessage in another thread. */
329 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE
330 || p_vout->p_sys->i_changes & VOUT_FULLSCREEN_CHANGE )
332 Win32ToggleFullscreen( p_vout );
334 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
335 p_vout->p_sys->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
341 if( p_vout->b_fullscreen && !p_vout->p_sys->b_cursor_hidden &&
342 (mdate() - p_vout->p_sys->i_lastmoved) >
343 p_vout->p_sys->i_mouse_hide_timeout )
348 /* Hide the cursor only if it is inside our window */
349 GetCursorPos( &point );
350 hwnd = WindowFromPoint(point);
351 if( hwnd == p_vout->p_sys->hwnd || hwnd == p_vout->p_sys->hvideownd )
353 PostMessage( p_vout->p_sys->hwnd, WM_VLC_HIDE_MOUSE, 0, 0 );
357 p_vout->p_sys->i_lastmoved = mdate();
362 * "Always on top" status change
364 if( p_vout->p_sys->b_on_top_change )
367 HMENU hMenu = GetSystemMenu( p_vout->p_sys->hwnd, FALSE );
369 var_Get( p_vout, "video-on-top", &val );
371 /* Set the window on top if necessary */
372 if( val.b_bool && !( GetWindowLong( p_vout->p_sys->hwnd, GWL_EXSTYLE )
375 CheckMenuItem( hMenu, IDM_TOGGLE_ON_TOP,
376 MF_BYCOMMAND | MFS_CHECKED );
377 SetWindowPos( p_vout->p_sys->hwnd, HWND_TOPMOST, 0, 0, 0, 0,
378 SWP_NOSIZE | SWP_NOMOVE );
381 /* The window shouldn't be on top */
382 if( !val.b_bool && ( GetWindowLong( p_vout->p_sys->hwnd, GWL_EXSTYLE )
385 CheckMenuItem( hMenu, IDM_TOGGLE_ON_TOP,
386 MF_BYCOMMAND | MFS_UNCHECKED );
387 SetWindowPos( p_vout->p_sys->hwnd, HWND_NOTOPMOST, 0, 0, 0, 0,
388 SWP_NOSIZE | SWP_NOMOVE );
391 p_vout->p_sys->b_on_top_change = VLC_FALSE;
394 /* Check if the event thread is still running */
395 if( p_vout->p_sys->p_event->b_die )
397 return VLC_EGENERIC; /* exit */
403 /*****************************************************************************
404 * GLSwapBuffers: swap front/back buffers
405 *****************************************************************************/
406 static void GLSwapBuffers( vout_thread_t *p_vout )
408 SwapBuffers( p_vout->p_sys->hGLDC );
412 ** this function is only used once when the first picture is received
413 ** this function will show the video window once a picture is ready
416 static void FirstSwap( vout_thread_t *p_vout )
418 /* get initial picture buffer swapped to front buffer */
419 GLSwapBuffers( p_vout );
422 ** Video window is initially hidden, show it now since we got a
425 SetWindowPos( p_vout->p_sys->hvideownd, NULL, 0, 0, 0, 0,
433 /* use and restores proper swap function for further pictures */
434 p_vout->pf_swap = GLSwapBuffers;