]> git.sesse.net Git - vlc/blob - modules/video_output/msw/glwin32.c
win32 vout: factorize code
[vlc] / modules / video_output / msw / glwin32.c
1 /*****************************************************************************
2  * glwin32.c: Windows OpenGL provider
3  *****************************************************************************
4  * Copyright (C) 2001-2009 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
95     /* Allocate structure */
96     p_vout->p_sys = calloc( 1, sizeof( vout_sys_t ) );
97     if( p_vout->p_sys == NULL )
98         return VLC_ENOMEM;
99
100     /* Initialisations */
101     p_vout->pf_init = Init;
102     p_vout->pf_end = End;
103     p_vout->pf_manage = Manage;
104     p_vout->pf_swap = FirstSwap;
105
106     p_vout->p_sys->hwnd = p_vout->p_sys->hvideownd = NULL;
107     p_vout->p_sys->hparent = p_vout->p_sys->hfswnd = NULL;
108     p_vout->p_sys->i_changes = 0;
109     vlc_mutex_init( &p_vout->p_sys->lock );
110     SetRectEmpty( &p_vout->p_sys->rect_display );
111     SetRectEmpty( &p_vout->p_sys->rect_parent );
112
113     var_Create( p_vout, "video-title", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
114
115     p_vout->p_sys->b_cursor_hidden = 0;
116     p_vout->p_sys->i_lastmoved = mdate();
117     p_vout->p_sys->i_mouse_hide_timeout =
118         var_GetInteger(p_vout, "mouse-hide-timeout") * 1000;
119
120     /* Set main window's size */
121     p_vout->p_sys->i_window_width = p_vout->i_window_width;
122     p_vout->p_sys->i_window_height = p_vout->i_window_height;
123
124     if ( CreateEventThread( p_vout ) )
125     {
126         /* Variable to indicate if the window should be on top of others */
127         /* Trigger a callback right now */
128         var_TriggerCallback( p_vout, "video-on-top" );
129
130         return VLC_SUCCESS;
131     }
132     else
133     {
134         CloseVideo( VLC_OBJECT(p_vout) );
135         return VLC_EGENERIC;
136     }
137 }
138
139 /*****************************************************************************
140  * Init: initialize video thread output method
141  *****************************************************************************/
142 static int Init( vout_thread_t *p_vout )
143 {
144     PIXELFORMATDESCRIPTOR pfd;
145     int iFormat;
146
147     /* Change the window title bar text */
148     PostMessage( p_vout->p_sys->hwnd, WM_VLC_CHANGE_TEXT, 0, 0 );
149
150     p_vout->p_sys->hGLDC = GetDC( p_vout->p_sys->hvideownd );
151
152     /* Set the pixel format for the DC */
153     memset( &pfd, 0, sizeof( pfd ) );
154     pfd.nSize = sizeof( pfd );
155     pfd.nVersion = 1;
156     pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
157     pfd.iPixelType = PFD_TYPE_RGBA;
158     pfd.cColorBits = 24;
159     pfd.cDepthBits = 16;
160     pfd.iLayerType = PFD_MAIN_PLANE;
161     iFormat = ChoosePixelFormat( p_vout->p_sys->hGLDC, &pfd );
162     SetPixelFormat( p_vout->p_sys->hGLDC, iFormat, &pfd );
163
164     /* Create and enable the render context */
165     p_vout->p_sys->hGLRC = wglCreateContext( p_vout->p_sys->hGLDC );
166     wglMakeCurrent( p_vout->p_sys->hGLDC, p_vout->p_sys->hGLRC );
167
168     return VLC_SUCCESS;
169 }
170
171 /*****************************************************************************
172  * End: terminate Sys video thread output method
173  *****************************************************************************
174  * Terminate an output method created by Create.
175  * It is called at the end of the thread.
176  *****************************************************************************/
177 static void End( vout_thread_t *p_vout )
178 {
179     wglMakeCurrent( NULL, NULL );
180     wglDeleteContext( p_vout->p_sys->hGLRC );
181     ReleaseDC( p_vout->p_sys->hvideownd, p_vout->p_sys->hGLDC );
182     return;
183 }
184
185 /*****************************************************************************
186  * CloseVideo: destroy Sys video thread output method
187  *****************************************************************************
188  * Terminate an output method created by Create
189  *****************************************************************************/
190 static void CloseVideo( vlc_object_t *p_this )
191 {
192     vout_thread_t * p_vout = (vout_thread_t *)p_this;
193
194     StopEventThread( p_vout );
195
196     free( p_vout->p_sys );
197     p_vout->p_sys = NULL;
198 }
199
200 /*****************************************************************************
201  * Manage: handle Sys events
202  *****************************************************************************
203  * This function should be called regularly by the video output thread.
204  * It returns a non null value if an error occurred.
205  *****************************************************************************/
206 static int Manage( vout_thread_t *p_vout )
207 {
208     int i_width = p_vout->p_sys->rect_dest.right -
209         p_vout->p_sys->rect_dest.left;
210     int i_height = p_vout->p_sys->rect_dest.bottom -
211         p_vout->p_sys->rect_dest.top;
212     glViewport( 0, 0, i_width, i_height );
213
214     /* If we do not control our window, we check for geometry changes
215      * ourselves because the parent might not send us its events. */
216     vlc_mutex_lock( &p_vout->p_sys->lock );
217     if( p_vout->p_sys->hparent && !p_vout->b_fullscreen )
218     {
219         RECT rect_parent;
220         POINT point;
221
222         vlc_mutex_unlock( &p_vout->p_sys->lock );
223
224         GetClientRect( p_vout->p_sys->hparent, &rect_parent );
225         point.x = point.y = 0;
226         ClientToScreen( p_vout->p_sys->hparent, &point );
227         OffsetRect( &rect_parent, point.x, point.y );
228
229         if( !EqualRect( &rect_parent, &p_vout->p_sys->rect_parent ) )
230         {
231             p_vout->p_sys->rect_parent = rect_parent;
232
233             /* This one is to force the update even if only
234              * the position has changed */
235             SetWindowPos( p_vout->p_sys->hwnd, 0, 1, 1,
236                           rect_parent.right - rect_parent.left,
237                           rect_parent.bottom - rect_parent.top, 0 );
238
239             SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
240                           rect_parent.right - rect_parent.left,
241                           rect_parent.bottom - rect_parent.top, 0 );
242         }
243     }
244     else
245     {
246         vlc_mutex_unlock( &p_vout->p_sys->lock );
247     }
248
249     /* autoscale toggle */
250     if( p_vout->i_changes & VOUT_SCALE_CHANGE )
251     {
252         p_vout->i_changes &= ~VOUT_SCALE_CHANGE;
253
254         p_vout->b_autoscale = var_GetBool( p_vout, "autoscale" );
255         p_vout->i_zoom = (int) ZOOM_FP_FACTOR;
256
257         UpdateRects( p_vout, true );
258     }
259
260     /* scaling factor */
261     if( p_vout->i_changes & VOUT_ZOOM_CHANGE )
262     {
263         p_vout->i_changes &= ~VOUT_ZOOM_CHANGE;
264
265         p_vout->b_autoscale = false;
266         p_vout->i_zoom =
267             (int)( ZOOM_FP_FACTOR * var_GetFloat( p_vout, "scale" ) );
268         UpdateRects( p_vout, true );
269     }
270
271     /* Check for cropping / aspect changes */
272     if( p_vout->i_changes & VOUT_CROP_CHANGE ||
273         p_vout->i_changes & VOUT_ASPECT_CHANGE )
274     {
275         p_vout->i_changes &= ~VOUT_CROP_CHANGE;
276         p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
277
278         p_vout->fmt_out.i_x_offset = p_vout->fmt_in.i_x_offset;
279         p_vout->fmt_out.i_y_offset = p_vout->fmt_in.i_y_offset;
280         p_vout->fmt_out.i_visible_width = p_vout->fmt_in.i_visible_width;
281         p_vout->fmt_out.i_visible_height = p_vout->fmt_in.i_visible_height;
282         p_vout->fmt_out.i_aspect = p_vout->fmt_in.i_aspect;
283         p_vout->fmt_out.i_sar_num = p_vout->fmt_in.i_sar_num;
284         p_vout->fmt_out.i_sar_den = p_vout->fmt_in.i_sar_den;
285         p_vout->output.i_aspect = p_vout->fmt_in.i_aspect;
286         UpdateRects( p_vout, true );
287     }
288
289     /* We used to call the Win32 PeekMessage function here to read the window
290      * messages. But since window can stay blocked into this function for a
291      * long time (for example when you move your window on the screen), I
292      * decided to isolate PeekMessage in another thread. */
293
294     /*
295      * Fullscreen change
296      */
297     if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE
298         || p_vout->p_sys->i_changes & VOUT_FULLSCREEN_CHANGE )
299     {
300         Win32ToggleFullscreen( p_vout );
301
302         p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
303         p_vout->p_sys->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
304     }
305
306     /*
307      * Pointer change
308      */
309     if( p_vout->b_fullscreen && !p_vout->p_sys->b_cursor_hidden &&
310         (mdate() - p_vout->p_sys->i_lastmoved) >
311             p_vout->p_sys->i_mouse_hide_timeout )
312     {
313         POINT point;
314         HWND hwnd;
315
316         /* Hide the cursor only if it is inside our window */
317         GetCursorPos( &point );
318         hwnd = WindowFromPoint(point);
319         if( hwnd == p_vout->p_sys->hwnd || hwnd == p_vout->p_sys->hvideownd )
320         {
321             PostMessage( p_vout->p_sys->hwnd, WM_VLC_HIDE_MOUSE, 0, 0 );
322         }
323         else
324         {
325             p_vout->p_sys->i_lastmoved = mdate();
326         }
327     }
328
329     /*
330      * "Always on top" status change
331      */
332     if( p_vout->p_sys->b_on_top_change )
333     {
334         vlc_value_t val;
335         HMENU hMenu = GetSystemMenu( p_vout->p_sys->hwnd, FALSE );
336
337         var_Get( p_vout, "video-on-top", &val );
338
339         /* Set the window on top if necessary */
340         if( val.b_bool && !( GetWindowLong( p_vout->p_sys->hwnd, GWL_EXSTYLE )
341                            & WS_EX_TOPMOST ) )
342         {
343             CheckMenuItem( hMenu, IDM_TOGGLE_ON_TOP,
344                            MF_BYCOMMAND | MFS_CHECKED );
345             SetWindowPos( p_vout->p_sys->hwnd, HWND_TOPMOST, 0, 0, 0, 0,
346                           SWP_NOSIZE | SWP_NOMOVE );
347         }
348         else
349         /* The window shouldn't be on top */
350         if( !val.b_bool && ( GetWindowLong( p_vout->p_sys->hwnd, GWL_EXSTYLE )
351                            & WS_EX_TOPMOST ) )
352         {
353             CheckMenuItem( hMenu, IDM_TOGGLE_ON_TOP,
354                            MF_BYCOMMAND | MFS_UNCHECKED );
355             SetWindowPos( p_vout->p_sys->hwnd, HWND_NOTOPMOST, 0, 0, 0, 0,
356                           SWP_NOSIZE | SWP_NOMOVE );
357         }
358
359         p_vout->p_sys->b_on_top_change = false;
360     }
361
362     /* Check if the event thread is still running */
363     if( !vlc_object_alive (p_vout->p_sys->p_event) )
364     {
365         return VLC_EGENERIC; /* exit */
366     }
367
368     return VLC_SUCCESS;
369 }
370
371 /*****************************************************************************
372  * GLSwapBuffers: swap front/back buffers
373  *****************************************************************************/
374 static void GLSwapBuffers( vout_thread_t *p_vout )
375 {
376     SwapBuffers( p_vout->p_sys->hGLDC );
377 }
378
379 /*
380 ** this function is only used once when the first picture is received
381 ** this function will show the video window once a picture is ready
382 */
383
384 static void FirstSwap( vout_thread_t *p_vout )
385 {
386     /* get initial picture buffer swapped to front buffer */
387     GLSwapBuffers( p_vout );
388
389     /*
390     ** Video window is initially hidden, show it now since we got a
391     ** picture to show.
392     */
393     SetWindowPos( p_vout->p_sys->hvideownd, NULL, 0, 0, 0, 0,
394         SWP_ASYNCWINDOWPOS|
395         SWP_FRAMECHANGED|
396         SWP_SHOWWINDOW|
397         SWP_NOMOVE|
398         SWP_NOSIZE|
399         SWP_NOZORDER );
400
401     /* use and restores proper swap function for further pictures */
402     p_vout->pf_swap = GLSwapBuffers;
403 }