1 /*****************************************************************************
\r
2 * glwin32.c: Windows OpenGL provider
\r
3 *****************************************************************************
\r
4 * Copyright (C) 2001-2004 VideoLAN
\r
7 * Authors: Gildas Bazin <gbazin@videolan.org>
\r
9 * This program is free software; you can redistribute it and/or modify
\r
10 * it under the terms of the GNU General Public License as published by
\r
11 * the Free Software Foundation; either version 2 of the License, or
\r
12 * (at your option) any later version.
\r
14 * This program is distributed in the hope that it will be useful,
\r
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
\r
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
\r
17 * GNU General Public License for more details.
\r
19 * You should have received a copy of the GNU General Public License
\r
20 * along with this program; if not, write to the Free Software
\r
21 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
\r
22 *****************************************************************************/
\r
24 #include <errno.h> /* ENOMEM */
\r
25 #include <stdlib.h> /* free() */
\r
26 #include <string.h> /* strerror() */
\r
28 #include <vlc/vlc.h>
\r
29 #include <vlc/intf.h>
\r
30 #include <vlc/vout.h>
\r
32 #include <windows.h>
\r
34 #include <commctrl.h>
\r
36 #include <multimon.h>
\r
37 #undef GetSystemMetrics
\r
39 #ifndef MONITOR_DEFAULTTONEAREST
\r
40 # define MONITOR_DEFAULTTONEAREST 2
\r
47 /*****************************************************************************
\r
49 *****************************************************************************/
\r
50 static int OpenVideo ( vlc_object_t * );
\r
51 static void CloseVideo ( vlc_object_t * );
\r
53 static int Init ( vout_thread_t * );
\r
54 static void End ( vout_thread_t * );
\r
55 static int Manage ( vout_thread_t * );
\r
56 static void GLSwapBuffers( vout_thread_t * );
\r
58 /*****************************************************************************
\r
60 *****************************************************************************/
\r
62 set_category( CAT_VIDEO );
\r
63 set_subcategory( SUBCAT_VIDEO_VOUT );
\r
64 set_description( _("Win32 OpenGL provider") );
\r
65 set_capability( "opengl provider", 100 );
\r
66 add_shortcut( "glwin32" );
\r
67 set_callbacks( OpenVideo, CloseVideo );
\r
69 /* FIXME: Hack to avoid unregistering our window class */
\r
70 linked_with_a_crap_library_which_uses_atexit( );
\r
74 /* check if we registered a window class because we need to
\r
77 if( GetClassInfo( GetModuleHandle(NULL), "VLC DirectX", &wndclass ) )
\r
78 UnregisterClass( "VLC DirectX", GetModuleHandle(NULL) );
\r
81 /*****************************************************************************
\r
82 * OpenVideo: allocate OpenGL provider
\r
83 *****************************************************************************
\r
84 * This function creates and initializes a video window.
\r
85 *****************************************************************************/
\r
86 static int OpenVideo( vlc_object_t *p_this )
\r
88 vout_thread_t * p_vout = (vout_thread_t *)p_this;
\r
91 /* Allocate structure */
\r
92 p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
\r
93 if( p_vout->p_sys == NULL )
\r
95 msg_Err( p_vout, "out of memory" );
\r
98 memset( p_vout->p_sys, 0, sizeof( vout_sys_t ) );
\r
100 /* Initialisations */
\r
101 p_vout->pf_init = Init;
\r
102 p_vout->pf_end = End;
\r
103 p_vout->pf_manage = Manage;
\r
104 p_vout->pf_swap = GLSwapBuffers;
\r
106 p_vout->p_sys->p_ddobject = NULL;
\r
107 p_vout->p_sys->p_display = NULL;
\r
108 p_vout->p_sys->p_current_surface = NULL;
\r
109 p_vout->p_sys->p_clipper = NULL;
\r
110 p_vout->p_sys->hwnd = p_vout->p_sys->hvideownd = NULL;
\r
111 p_vout->p_sys->hparent = p_vout->p_sys->hfswnd = NULL;
\r
112 p_vout->p_sys->i_changes = 0;
\r
113 p_vout->p_sys->b_wallpaper = 0;
\r
114 vlc_mutex_init( p_vout, &p_vout->p_sys->lock );
\r
115 SetRectEmpty( &p_vout->p_sys->rect_display );
\r
116 SetRectEmpty( &p_vout->p_sys->rect_parent );
\r
118 var_Create( p_vout, "video-title", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
\r
120 p_vout->p_sys->b_cursor_hidden = 0;
\r
121 p_vout->p_sys->i_lastmoved = mdate();
\r
123 /* Set main window's size */
\r
124 p_vout->p_sys->i_window_width = p_vout->i_window_width;
\r
125 p_vout->p_sys->i_window_height = p_vout->i_window_height;
\r
127 /* Create the DirectXEventThread, this thread is created by us to isolate
\r
128 * the Win32 PeekMessage function calls. We want to do this because
\r
129 * Windows can stay blocked inside this call for a long time, and when
\r
130 * this happens it thus blocks vlc's video_output thread.
\r
131 * DirectXEventThread will take care of the creation of the video
\r
132 * window (because PeekMessage has to be called from the same thread which
\r
133 * created the window). */
\r
134 msg_Dbg( p_vout, "creating DirectXEventThread" );
\r
135 p_vout->p_sys->p_event =
\r
136 vlc_object_create( p_vout, sizeof(event_thread_t) );
\r
137 p_vout->p_sys->p_event->p_vout = p_vout;
\r
138 if( vlc_thread_create( p_vout->p_sys->p_event, "DirectX Events Thread",
\r
139 E_(DirectXEventThread), 0, 1 ) )
\r
141 msg_Err( p_vout, "cannot create DirectXEventThread" );
\r
142 vlc_object_destroy( p_vout->p_sys->p_event );
\r
143 p_vout->p_sys->p_event = NULL;
\r
147 if( p_vout->p_sys->p_event->b_error )
\r
149 msg_Err( p_vout, "DirectXEventThread failed" );
\r
153 vlc_object_attach( p_vout->p_sys->p_event, p_vout );
\r
155 msg_Dbg( p_vout, "DirectXEventThread running" );
\r
157 /* Variable to indicate if the window should be on top of others */
\r
158 /* Trigger a callback right now */
\r
159 var_Get( p_vout, "video-on-top", &val );
\r
160 var_Set( p_vout, "video-on-top", val );
\r
162 return VLC_SUCCESS;
\r
165 CloseVideo( VLC_OBJECT(p_vout) );
\r
166 return VLC_EGENERIC;
\r
169 /*****************************************************************************
\r
170 * Init: initialize video thread output method
\r
171 *****************************************************************************/
\r
172 static int Init( vout_thread_t *p_vout )
\r
174 PIXELFORMATDESCRIPTOR pfd;
\r
177 /* Change the window title bar text */
\r
178 PostMessage( p_vout->p_sys->hwnd, WM_VLC_CHANGE_TEXT, 0, 0 );
\r
180 p_vout->p_sys->hGLDC = GetDC( p_vout->p_sys->hvideownd );
\r
182 /* Set the pixel format for the DC */
\r
183 memset( &pfd, 0, sizeof( pfd ) );
\r
184 pfd.nSize = sizeof( pfd );
\r
186 pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
\r
187 pfd.iPixelType = PFD_TYPE_RGBA;
\r
188 pfd.cColorBits = 24;
\r
189 pfd.cDepthBits = 16;
\r
190 pfd.iLayerType = PFD_MAIN_PLANE;
\r
191 iFormat = ChoosePixelFormat( p_vout->p_sys->hGLDC, &pfd );
\r
192 SetPixelFormat( p_vout->p_sys->hGLDC, iFormat, &pfd );
\r
194 /* Create and enable the render context */
\r
195 p_vout->p_sys->hGLRC = wglCreateContext( p_vout->p_sys->hGLDC );
\r
196 wglMakeCurrent( p_vout->p_sys->hGLDC, p_vout->p_sys->hGLRC );
\r
198 return VLC_SUCCESS;
\r
201 /*****************************************************************************
\r
202 * End: terminate Sys video thread output method
\r
203 *****************************************************************************
\r
204 * Terminate an output method created by Create.
\r
205 * It is called at the end of the thread.
\r
206 *****************************************************************************/
\r
207 static void End( vout_thread_t *p_vout )
\r
209 wglMakeCurrent( NULL, NULL );
\r
210 wglDeleteContext( p_vout->p_sys->hGLRC );
\r
211 ReleaseDC( p_vout->p_sys->hvideownd, p_vout->p_sys->hGLDC );
\r
215 /*****************************************************************************
\r
216 * CloseVideo: destroy Sys video thread output method
\r
217 *****************************************************************************
\r
218 * Terminate an output method created by Create
\r
219 *****************************************************************************/
\r
220 static void CloseVideo( vlc_object_t *p_this )
\r
222 vout_thread_t * p_vout = (vout_thread_t *)p_this;
\r
224 msg_Dbg( p_vout, "CloseVideo" );
\r
226 if( p_vout->p_sys->p_event )
\r
228 vlc_object_detach( p_vout->p_sys->p_event );
\r
230 /* Kill DirectXEventThread */
\r
231 p_vout->p_sys->p_event->b_die = VLC_TRUE;
\r
233 /* we need to be sure DirectXEventThread won't stay stuck in
\r
234 * GetMessage, so we send a fake message */
\r
235 if( p_vout->p_sys->hwnd )
\r
237 PostMessage( p_vout->p_sys->hwnd, WM_NULL, 0, 0);
\r
240 vlc_thread_join( p_vout->p_sys->p_event );
\r
241 vlc_object_destroy( p_vout->p_sys->p_event );
\r
244 vlc_mutex_destroy( &p_vout->p_sys->lock );
\r
246 if( p_vout->p_sys )
\r
248 free( p_vout->p_sys );
\r
249 p_vout->p_sys = NULL;
\r
253 /*****************************************************************************
\r
254 * Manage: handle Sys events
\r
255 *****************************************************************************
\r
256 * This function should be called regularly by the video output thread.
\r
257 * It returns a non null value if an error occurred.
\r
258 *****************************************************************************/
\r
259 static int Manage( vout_thread_t *p_vout )
\r
261 WINDOWPLACEMENT window_placement;
\r
263 int i_width = p_vout->p_sys->rect_dest.right -
\r
264 p_vout->p_sys->rect_dest.left;
\r
265 int i_height = p_vout->p_sys->rect_dest.bottom -
\r
266 p_vout->p_sys->rect_dest.top;
\r
267 glViewport( 0, 0, i_width, i_height );
\r
269 /* If we do not control our window, we check for geometry changes
\r
270 * ourselves because the parent might not send us its events. */
\r
271 vlc_mutex_lock( &p_vout->p_sys->lock );
\r
272 if( p_vout->p_sys->hparent && !p_vout->b_fullscreen )
\r
277 vlc_mutex_unlock( &p_vout->p_sys->lock );
\r
279 GetClientRect( p_vout->p_sys->hparent, &rect_parent );
\r
280 point.x = point.y = 0;
\r
281 ClientToScreen( p_vout->p_sys->hparent, &point );
\r
282 OffsetRect( &rect_parent, point.x, point.y );
\r
284 if( !EqualRect( &rect_parent, &p_vout->p_sys->rect_parent ) )
\r
286 p_vout->p_sys->rect_parent = rect_parent;
\r
288 /* This one is to force the update even if only
\r
289 * the position has changed */
\r
290 SetWindowPos( p_vout->p_sys->hwnd, 0, 1, 1,
\r
291 rect_parent.right - rect_parent.left,
\r
292 rect_parent.bottom - rect_parent.top, 0 );
\r
294 SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
\r
295 rect_parent.right - rect_parent.left,
\r
296 rect_parent.bottom - rect_parent.top, 0 );
\r
301 vlc_mutex_unlock( &p_vout->p_sys->lock );
\r
304 /* We used to call the Win32 PeekMessage function here to read the window
\r
305 * messages. But since window can stay blocked into this function for a
\r
306 * long time (for example when you move your window on the screen), I
\r
307 * decided to isolate PeekMessage in another thread. */
\r
310 * Fullscreen change
\r
312 if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE
\r
313 || p_vout->p_sys->i_changes & VOUT_FULLSCREEN_CHANGE )
\r
316 HWND hwnd = (p_vout->p_sys->hparent && p_vout->p_sys->hfswnd) ?
\r
317 p_vout->p_sys->hfswnd : p_vout->p_sys->hwnd;
\r
319 p_vout->b_fullscreen = ! p_vout->b_fullscreen;
\r
321 /* We need to switch between Maximized and Normal sized window */
\r
322 window_placement.length = sizeof(WINDOWPLACEMENT);
\r
323 GetWindowPlacement( hwnd, &window_placement );
\r
324 if( p_vout->b_fullscreen )
\r
326 /* Change window style, no borders and no title bar */
\r
327 int i_style = WS_CLIPCHILDREN | WS_VISIBLE;
\r
328 SetWindowLong( hwnd, GWL_STYLE, i_style );
\r
330 if( p_vout->p_sys->hparent )
\r
332 /* Retrieve current window position so fullscreen will happen
\r
333 * on the right screen */
\r
334 POINT point = {0,0};
\r
336 ClientToScreen( p_vout->p_sys->hwnd, &point );
\r
337 GetClientRect( p_vout->p_sys->hwnd, &rect );
\r
338 SetWindowPos( hwnd, 0, point.x, point.y,
\r
339 rect.right, rect.bottom,
\r
340 SWP_NOZORDER|SWP_FRAMECHANGED );
\r
341 GetWindowPlacement( hwnd, &window_placement );
\r
344 /* Maximize window */
\r
345 window_placement.showCmd = SW_SHOWMAXIMIZED;
\r
346 SetWindowPlacement( hwnd, &window_placement );
\r
347 SetWindowPos( hwnd, 0, 0, 0, 0, 0,
\r
348 SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER|SWP_FRAMECHANGED);
\r
350 if( p_vout->p_sys->hparent )
\r
353 GetClientRect( hwnd, &rect );
\r
354 SetParent( p_vout->p_sys->hwnd, hwnd );
\r
355 SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
\r
356 rect.right, rect.bottom,
\r
357 SWP_NOZORDER|SWP_FRAMECHANGED );
\r
360 SetForegroundWindow( hwnd );
\r
364 /* Change window style, no borders and no title bar */
\r
365 SetWindowLong( hwnd, GWL_STYLE, p_vout->p_sys->i_window_style );
\r
367 /* Normal window */
\r
368 window_placement.showCmd = SW_SHOWNORMAL;
\r
369 SetWindowPlacement( hwnd, &window_placement );
\r
370 SetWindowPos( hwnd, 0, 0, 0, 0, 0,
\r
371 SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER|SWP_FRAMECHANGED);
\r
373 if( p_vout->p_sys->hparent )
\r
376 GetClientRect( p_vout->p_sys->hparent, &rect );
\r
377 SetParent( p_vout->p_sys->hwnd, p_vout->p_sys->hparent );
\r
378 SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
\r
379 rect.right, rect.bottom,
\r
380 SWP_NOZORDER|SWP_FRAMECHANGED );
\r
382 ShowWindow( hwnd, SW_HIDE );
\r
383 SetForegroundWindow( p_vout->p_sys->hparent );
\r
386 /* Make sure the mouse cursor is displayed */
\r
387 PostMessage( p_vout->p_sys->hwnd, WM_VLC_SHOW_MOUSE, 0, 0 );
\r
390 /* Update the object variable and trigger callback */
\r
391 val.b_bool = p_vout->b_fullscreen;
\r
392 var_Set( p_vout, "fullscreen", val );
\r
394 p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
\r
395 p_vout->p_sys->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
\r
401 if( p_vout->b_fullscreen && !p_vout->p_sys->b_cursor_hidden &&
\r
402 (mdate() - p_vout->p_sys->i_lastmoved) > 5000000 )
\r
407 /* Hide the cursor only if it is inside our window */
\r
408 GetCursorPos( &point );
\r
409 hwnd = WindowFromPoint(point);
\r
410 if( hwnd == p_vout->p_sys->hwnd || hwnd == p_vout->p_sys->hvideownd )
\r
412 PostMessage( p_vout->p_sys->hwnd, WM_VLC_HIDE_MOUSE, 0, 0 );
\r
416 p_vout->p_sys->i_lastmoved = mdate();
\r
421 * "Always on top" status change
\r
423 if( p_vout->p_sys->b_on_top_change )
\r
426 HMENU hMenu = GetSystemMenu( p_vout->p_sys->hwnd, FALSE );
\r
428 var_Get( p_vout, "video-on-top", &val );
\r
430 /* Set the window on top if necessary */
\r
431 if( val.b_bool && !( GetWindowLong( p_vout->p_sys->hwnd, GWL_EXSTYLE )
\r
432 & WS_EX_TOPMOST ) )
\r
434 CheckMenuItem( hMenu, IDM_TOGGLE_ON_TOP,
\r
435 MF_BYCOMMAND | MFS_CHECKED );
\r
436 SetWindowPos( p_vout->p_sys->hwnd, HWND_TOPMOST, 0, 0, 0, 0,
\r
437 SWP_NOSIZE | SWP_NOMOVE );
\r
440 /* The window shouldn't be on top */
\r
441 if( !val.b_bool && ( GetWindowLong( p_vout->p_sys->hwnd, GWL_EXSTYLE )
\r
442 & WS_EX_TOPMOST ) )
\r
444 CheckMenuItem( hMenu, IDM_TOGGLE_ON_TOP,
\r
445 MF_BYCOMMAND | MFS_UNCHECKED );
\r
446 SetWindowPos( p_vout->p_sys->hwnd, HWND_NOTOPMOST, 0, 0, 0, 0,
\r
447 SWP_NOSIZE | SWP_NOMOVE );
\r
450 p_vout->p_sys->b_on_top_change = VLC_FALSE;
\r
453 /* Check if the event thread is still running */
\r
454 if( p_vout->p_sys->p_event->b_die )
\r
456 return VLC_EGENERIC; /* exit */
\r
459 return VLC_SUCCESS;
\r
462 /*****************************************************************************
\r
463 * GLSwapBuffers: swap front/back buffers
\r
464 *****************************************************************************/
\r
465 static void GLSwapBuffers( vout_thread_t *p_vout )
\r
467 SwapBuffers( p_vout->p_sys->hGLDC );
\r
470 int E_(DirectXUpdateOverlay)( vout_thread_t *p_vout )
\r