]> git.sesse.net Git - vlc/blob - modules/video_output/msw/glwin32.c
Remove unused parameter
[vlc] / modules / video_output / msw / glwin32.c
1 /*****************************************************************************
2  * glwin32.c: Windows OpenGL provider
3  *****************************************************************************
4  * Copyright (C) 2001-2004 the VideoLAN team
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., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24 #include <errno.h>                                                 /* ENOMEM */
25
26 #ifdef HAVE_CONFIG_H
27 # include "config.h"
28 #endif
29
30 #include <vlc_common.h>
31 #include <vlc_plugin.h>
32 #include <vlc_interface.h>
33 #include <vlc_vout.h>
34
35 #include <windows.h>
36 #include <ddraw.h>
37 #include <commctrl.h>
38
39 #include <multimon.h>
40 #undef GetSystemMetrics
41
42 #ifndef MONITOR_DEFAULTTONEAREST
43 #   define MONITOR_DEFAULTTONEAREST 2
44 #endif
45
46 #include <GL/gl.h>
47
48 #include "vout.h"
49
50 /*****************************************************************************
51  * Local prototypes.
52  *****************************************************************************/
53 static int  OpenVideo  ( vlc_object_t * );
54 static void CloseVideo ( vlc_object_t * );
55
56 static int  Init      ( vout_thread_t * );
57 static void End       ( vout_thread_t * );
58 static int  Manage    ( vout_thread_t * );
59 static void GLSwapBuffers( vout_thread_t * );
60 static void FirstSwap( vout_thread_t * );
61
62 /*****************************************************************************
63  * Module descriptor
64  *****************************************************************************/
65 vlc_module_begin ()
66     set_category( CAT_VIDEO )
67     set_subcategory( SUBCAT_VIDEO_VOUT )
68     set_shortname( "OpenGL" )
69     set_description( N_("OpenGL video output") )
70     set_capability( "opengl provider", 100 )
71     add_shortcut( "glwin32" )
72     set_callbacks( OpenVideo, CloseVideo )
73
74     /* FIXME: Hack to avoid unregistering our window class */
75     linked_with_a_crap_library_which_uses_atexit ()
76 vlc_module_end ()
77
78 #if 0 /* FIXME */
79     /* check if we registered a window class because we need to
80      * unregister it */
81     WNDCLASS wndclass;
82     if( GetClassInfo( GetModuleHandle(NULL), "VLC DirectX", &wndclass ) )
83         UnregisterClass( "VLC DirectX", GetModuleHandle(NULL) );
84 #endif
85
86 /*****************************************************************************
87  * OpenVideo: allocate OpenGL provider
88  *****************************************************************************
89  * This function creates and initializes a video window.
90  *****************************************************************************/
91 static int OpenVideo( vlc_object_t *p_this )
92 {
93     vout_thread_t * p_vout = (vout_thread_t *)p_this;
94     vlc_value_t val;
95
96     /* Allocate structure */
97     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
98     if( p_vout->p_sys == NULL )
99         return VLC_ENOMEM;
100     memset( p_vout->p_sys, 0, sizeof( vout_sys_t ) );
101
102     /* Initialisations */
103     p_vout->pf_init = Init;
104     p_vout->pf_end = End;
105     p_vout->pf_manage = Manage;
106     p_vout->pf_swap = FirstSwap;
107
108     p_vout->p_sys->hwnd = p_vout->p_sys->hvideownd = NULL;
109     p_vout->p_sys->hparent = p_vout->p_sys->hfswnd = NULL;
110     p_vout->p_sys->i_changes = 0;
111     vlc_mutex_init( &p_vout->p_sys->lock );
112     SetRectEmpty( &p_vout->p_sys->rect_display );
113     SetRectEmpty( &p_vout->p_sys->rect_parent );
114
115     var_Create( p_vout, "video-title", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
116
117     p_vout->p_sys->b_cursor_hidden = 0;
118     p_vout->p_sys->i_lastmoved = mdate();
119     p_vout->p_sys->i_mouse_hide_timeout =
120         var_GetInteger(p_vout, "mouse-hide-timeout") * 1000;
121
122     /* Set main window's size */
123     p_vout->p_sys->i_window_width = p_vout->i_window_width;
124     p_vout->p_sys->i_window_height = p_vout->i_window_height;
125
126     /* Create the Vout EventThread, this thread is created by us to isolate
127      * the Win32 PeekMessage function calls. We want to do this because
128      * Windows can stay blocked inside this call for a long time, and when
129      * this happens it thus blocks vlc's video_output thread.
130      * Vout EventThread will take care of the creation of the video
131      * window (because PeekMessage has to be called from the same thread which
132      * created the window). */
133     msg_Dbg( p_vout, "creating Vout EventThread" );
134     p_vout->p_sys->p_event =
135         vlc_object_create( p_vout, sizeof(event_thread_t) );
136     p_vout->p_sys->p_event->p_vout = p_vout;
137     p_vout->p_sys->p_event->window_ready = CreateEvent( NULL, TRUE, FALSE, NULL );
138     if( vlc_thread_create( p_vout->p_sys->p_event, "Vout Events Thread",
139                            EventThread, 0 ) )
140     {
141         msg_Err( p_vout, "cannot create Vout EventThread" );
142         CloseHandle( p_vout->p_sys->p_event->window_ready );
143         vlc_object_release( p_vout->p_sys->p_event );
144         p_vout->p_sys->p_event = NULL;
145         goto error;
146     }
147     WaitForSingleObject( p_vout->p_sys->p_event->window_ready, INFINITE );
148     CloseHandle( p_vout->p_sys->p_event->window_ready );
149
150     if( p_vout->p_sys->p_event->b_error )
151     {
152         msg_Err( p_vout, "Vout EventThread failed" );
153         goto error;
154     }
155
156     vlc_object_attach( p_vout->p_sys->p_event, p_vout );
157
158     msg_Dbg( p_vout, "Vout EventThread running" );
159
160     /* Variable to indicate if the window should be on top of others */
161     /* Trigger a callback right now */
162     var_Get( p_vout, "video-on-top", &val );
163     var_Set( p_vout, "video-on-top", val );
164
165     return VLC_SUCCESS;
166
167  error:
168     CloseVideo( VLC_OBJECT(p_vout) );
169     return VLC_EGENERIC;
170 }
171
172 /*****************************************************************************
173  * Init: initialize video thread output method
174  *****************************************************************************/
175 static int Init( vout_thread_t *p_vout )
176 {
177     PIXELFORMATDESCRIPTOR pfd;
178     int iFormat;
179
180     /* Change the window title bar text */
181     PostMessage( p_vout->p_sys->hwnd, WM_VLC_CHANGE_TEXT, 0, 0 );
182
183     p_vout->p_sys->hGLDC = GetDC( p_vout->p_sys->hvideownd );
184
185     /* Set the pixel format for the DC */
186     memset( &pfd, 0, sizeof( pfd ) );
187     pfd.nSize = sizeof( pfd );
188     pfd.nVersion = 1;
189     pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
190     pfd.iPixelType = PFD_TYPE_RGBA;
191     pfd.cColorBits = 24;
192     pfd.cDepthBits = 16;
193     pfd.iLayerType = PFD_MAIN_PLANE;
194     iFormat = ChoosePixelFormat( p_vout->p_sys->hGLDC, &pfd );
195     SetPixelFormat( p_vout->p_sys->hGLDC, iFormat, &pfd );
196
197     /* Create and enable the render context */
198     p_vout->p_sys->hGLRC = wglCreateContext( p_vout->p_sys->hGLDC );
199     wglMakeCurrent( p_vout->p_sys->hGLDC, p_vout->p_sys->hGLRC );
200
201     return VLC_SUCCESS;
202 }
203
204 /*****************************************************************************
205  * End: terminate Sys video thread output method
206  *****************************************************************************
207  * Terminate an output method created by Create.
208  * It is called at the end of the thread.
209  *****************************************************************************/
210 static void End( vout_thread_t *p_vout )
211 {
212     wglMakeCurrent( NULL, NULL );
213     wglDeleteContext( p_vout->p_sys->hGLRC );
214     ReleaseDC( p_vout->p_sys->hvideownd, p_vout->p_sys->hGLDC );
215     return;
216 }
217
218 /*****************************************************************************
219  * CloseVideo: destroy Sys video thread output method
220  *****************************************************************************
221  * Terminate an output method created by Create
222  *****************************************************************************/
223 static void CloseVideo( vlc_object_t *p_this )
224 {
225     vout_thread_t * p_vout = (vout_thread_t *)p_this;
226
227     if( p_vout->b_fullscreen )
228     {
229         msg_Dbg( p_vout, "Quitting fullscreen" );
230         Win32ToggleFullscreen( p_vout );
231         /* Force fullscreen in the core for the next video */
232         var_SetBool( p_vout, "fullscreen", true );
233     }
234
235     if( p_vout->p_sys->p_event )
236     {
237         vlc_object_detach( p_vout->p_sys->p_event );
238
239         /* Kill Vout EventThread */
240         vlc_object_kill( p_vout->p_sys->p_event );
241
242         /* we need to be sure Vout EventThread won't stay stuck in
243          * GetMessage, so we send a fake message */
244         if( p_vout->p_sys->hwnd )
245         {
246             PostMessage( p_vout->p_sys->hwnd, WM_NULL, 0, 0);
247         }
248
249         vlc_thread_join( p_vout->p_sys->p_event );
250         vlc_object_release( p_vout->p_sys->p_event );
251     }
252
253     vlc_mutex_destroy( &p_vout->p_sys->lock );
254
255     free( p_vout->p_sys );
256     p_vout->p_sys = NULL;
257 }
258
259 /*****************************************************************************
260  * Manage: handle Sys events
261  *****************************************************************************
262  * This function should be called regularly by the video output thread.
263  * It returns a non null value if an error occurred.
264  *****************************************************************************/
265 static int Manage( vout_thread_t *p_vout )
266 {
267     int i_width = p_vout->p_sys->rect_dest.right -
268         p_vout->p_sys->rect_dest.left;
269     int i_height = p_vout->p_sys->rect_dest.bottom -
270         p_vout->p_sys->rect_dest.top;
271     glViewport( 0, 0, i_width, i_height );
272
273     /* If we do not control our window, we check for geometry changes
274      * ourselves because the parent might not send us its events. */
275     vlc_mutex_lock( &p_vout->p_sys->lock );
276     if( p_vout->p_sys->hparent && !p_vout->b_fullscreen )
277     {
278         RECT rect_parent;
279         POINT point;
280
281         vlc_mutex_unlock( &p_vout->p_sys->lock );
282
283         GetClientRect( p_vout->p_sys->hparent, &rect_parent );
284         point.x = point.y = 0;
285         ClientToScreen( p_vout->p_sys->hparent, &point );
286         OffsetRect( &rect_parent, point.x, point.y );
287
288         if( !EqualRect( &rect_parent, &p_vout->p_sys->rect_parent ) )
289         {
290             p_vout->p_sys->rect_parent = rect_parent;
291
292             /* This one is to force the update even if only
293              * the position has changed */
294             SetWindowPos( p_vout->p_sys->hwnd, 0, 1, 1,
295                           rect_parent.right - rect_parent.left,
296                           rect_parent.bottom - rect_parent.top, 0 );
297
298             SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
299                           rect_parent.right - rect_parent.left,
300                           rect_parent.bottom - rect_parent.top, 0 );
301         }
302     }
303     else
304     {
305         vlc_mutex_unlock( &p_vout->p_sys->lock );
306     }
307
308     /* Check for cropping / aspect changes */
309     if( p_vout->i_changes & VOUT_CROP_CHANGE ||
310         p_vout->i_changes & VOUT_ASPECT_CHANGE )
311     {
312         p_vout->i_changes &= ~VOUT_CROP_CHANGE;
313         p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
314
315         p_vout->fmt_out.i_x_offset = p_vout->fmt_in.i_x_offset;
316         p_vout->fmt_out.i_y_offset = p_vout->fmt_in.i_y_offset;
317         p_vout->fmt_out.i_visible_width = p_vout->fmt_in.i_visible_width;
318         p_vout->fmt_out.i_visible_height = p_vout->fmt_in.i_visible_height;
319         p_vout->fmt_out.i_aspect = p_vout->fmt_in.i_aspect;
320         p_vout->fmt_out.i_sar_num = p_vout->fmt_in.i_sar_num;
321         p_vout->fmt_out.i_sar_den = p_vout->fmt_in.i_sar_den;
322         p_vout->output.i_aspect = p_vout->fmt_in.i_aspect;
323         UpdateRects( p_vout, true );
324     }
325
326     /* We used to call the Win32 PeekMessage function here to read the window
327      * messages. But since window can stay blocked into this function for a
328      * long time (for example when you move your window on the screen), I
329      * decided to isolate PeekMessage in another thread. */
330
331     /*
332      * Fullscreen change
333      */
334     if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE
335         || p_vout->p_sys->i_changes & VOUT_FULLSCREEN_CHANGE )
336     {
337         Win32ToggleFullscreen( p_vout );
338
339         p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
340         p_vout->p_sys->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
341     }
342
343     /*
344      * Pointer change
345      */
346     if( p_vout->b_fullscreen && !p_vout->p_sys->b_cursor_hidden &&
347         (mdate() - p_vout->p_sys->i_lastmoved) >
348             p_vout->p_sys->i_mouse_hide_timeout )
349     {
350         POINT point;
351         HWND hwnd;
352
353         /* Hide the cursor only if it is inside our window */
354         GetCursorPos( &point );
355         hwnd = WindowFromPoint(point);
356         if( hwnd == p_vout->p_sys->hwnd || hwnd == p_vout->p_sys->hvideownd )
357         {
358             PostMessage( p_vout->p_sys->hwnd, WM_VLC_HIDE_MOUSE, 0, 0 );
359         }
360         else
361         {
362             p_vout->p_sys->i_lastmoved = mdate();
363         }
364     }
365
366     /*
367      * "Always on top" status change
368      */
369     if( p_vout->p_sys->b_on_top_change )
370     {
371         vlc_value_t val;
372         HMENU hMenu = GetSystemMenu( p_vout->p_sys->hwnd, FALSE );
373
374         var_Get( p_vout, "video-on-top", &val );
375
376         /* Set the window on top if necessary */
377         if( val.b_bool && !( GetWindowLong( p_vout->p_sys->hwnd, GWL_EXSTYLE )
378                            & WS_EX_TOPMOST ) )
379         {
380             CheckMenuItem( hMenu, IDM_TOGGLE_ON_TOP,
381                            MF_BYCOMMAND | MFS_CHECKED );
382             SetWindowPos( p_vout->p_sys->hwnd, HWND_TOPMOST, 0, 0, 0, 0,
383                           SWP_NOSIZE | SWP_NOMOVE );
384         }
385         else
386         /* The window shouldn't be on top */
387         if( !val.b_bool && ( GetWindowLong( p_vout->p_sys->hwnd, GWL_EXSTYLE )
388                            & WS_EX_TOPMOST ) )
389         {
390             CheckMenuItem( hMenu, IDM_TOGGLE_ON_TOP,
391                            MF_BYCOMMAND | MFS_UNCHECKED );
392             SetWindowPos( p_vout->p_sys->hwnd, HWND_NOTOPMOST, 0, 0, 0, 0,
393                           SWP_NOSIZE | SWP_NOMOVE );
394         }
395
396         p_vout->p_sys->b_on_top_change = false;
397     }
398
399     /* Check if the event thread is still running */
400     if( !vlc_object_alive (p_vout->p_sys->p_event) )
401     {
402         return VLC_EGENERIC; /* exit */
403     }
404
405     return VLC_SUCCESS;
406 }
407
408 /*****************************************************************************
409  * GLSwapBuffers: swap front/back buffers
410  *****************************************************************************/
411 static void GLSwapBuffers( vout_thread_t *p_vout )
412 {
413     SwapBuffers( p_vout->p_sys->hGLDC );
414 }
415
416 /*
417 ** this function is only used once when the first picture is received
418 ** this function will show the video window once a picture is ready
419 */
420
421 static void FirstSwap( vout_thread_t *p_vout )
422 {
423     /* get initial picture buffer swapped to front buffer */
424     GLSwapBuffers( p_vout );
425
426     /*
427     ** Video window is initially hidden, show it now since we got a
428     ** picture to show.
429     */
430     SetWindowPos( p_vout->p_sys->hvideownd, NULL, 0, 0, 0, 0,
431         SWP_ASYNCWINDOWPOS|
432         SWP_FRAMECHANGED|
433         SWP_SHOWWINDOW|
434         SWP_NOMOVE|
435         SWP_NOSIZE|
436         SWP_NOZORDER );
437
438     /* use and restores proper swap function for further pictures */
439     p_vout->pf_swap = GLSwapBuffers;
440 }