]> git.sesse.net Git - vlc/blob - modules/video_output/msw/glwin32.c
Fixed uninitialized variables (msw).
[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     p_vout->pf_control = Control;
106
107     if( CommonInit( p_vout ) )
108         goto error;
109
110     return VLC_SUCCESS;
111
112 error:
113     CloseVideo( VLC_OBJECT(p_vout) );
114     return VLC_EGENERIC;
115 }
116
117 /*****************************************************************************
118  * Init: initialize video thread output method
119  *****************************************************************************/
120 static int Init( vout_thread_t *p_vout )
121 {
122     PIXELFORMATDESCRIPTOR pfd;
123     int iFormat;
124
125     /* Change the window title bar text */
126     EventThreadUpdateTitle( p_vout->p_sys->p_event, VOUT_TITLE " (OpenGL output)" );
127
128     p_vout->p_sys->hGLDC = GetDC( p_vout->p_sys->hvideownd );
129
130     /* Set the pixel format for the DC */
131     memset( &pfd, 0, sizeof( pfd ) );
132     pfd.nSize = sizeof( pfd );
133     pfd.nVersion = 1;
134     pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
135     pfd.iPixelType = PFD_TYPE_RGBA;
136     pfd.cColorBits = 24;
137     pfd.cDepthBits = 16;
138     pfd.iLayerType = PFD_MAIN_PLANE;
139     iFormat = ChoosePixelFormat( p_vout->p_sys->hGLDC, &pfd );
140     SetPixelFormat( p_vout->p_sys->hGLDC, iFormat, &pfd );
141
142     /* Create and enable the render context */
143     p_vout->p_sys->hGLRC = wglCreateContext( p_vout->p_sys->hGLDC );
144     wglMakeCurrent( p_vout->p_sys->hGLDC, p_vout->p_sys->hGLRC );
145
146     return VLC_SUCCESS;
147 }
148
149 /*****************************************************************************
150  * End: terminate Sys video thread output method
151  *****************************************************************************
152  * Terminate an output method created by Create.
153  * It is called at the end of the thread.
154  *****************************************************************************/
155 static void End( vout_thread_t *p_vout )
156 {
157     wglMakeCurrent( NULL, NULL );
158     wglDeleteContext( p_vout->p_sys->hGLRC );
159     ReleaseDC( p_vout->p_sys->hvideownd, p_vout->p_sys->hGLDC );
160     return;
161 }
162
163 /*****************************************************************************
164  * CloseVideo: destroy Sys video thread output method
165  *****************************************************************************
166  * Terminate an output method created by Create
167  *****************************************************************************/
168 static void CloseVideo( vlc_object_t *p_this )
169 {
170     vout_thread_t * p_vout = (vout_thread_t *)p_this;
171
172     CommonClean( p_vout );
173
174     free( p_vout->p_sys );
175 }
176
177 /*****************************************************************************
178  * Manage: handle Sys events
179  *****************************************************************************
180  * This function should be called regularly by the video output thread.
181  * It returns a non null value if an error occurred.
182  *****************************************************************************/
183 static int Manage( vout_thread_t *p_vout )
184 {
185     vout_sys_t *p_sys = p_vout->p_sys;
186
187     const int i_width  = p_sys->rect_dest.right - p_sys->rect_dest.left;
188     const int i_height = p_sys->rect_dest.bottom - p_sys->rect_dest.top;
189     glViewport( 0, 0, i_width, i_height );
190
191     CommonManage( p_vout );
192     return VLC_SUCCESS;
193 }
194
195 /*****************************************************************************
196  * GLSwapBuffers: swap front/back buffers
197  *****************************************************************************/
198 static void GLSwapBuffers( vout_thread_t *p_vout )
199 {
200     SwapBuffers( p_vout->p_sys->hGLDC );
201 }
202
203 /*
204 ** this function is only used once when the first picture is received
205 ** this function will show the video window once a picture is ready
206 */
207
208 static void FirstSwap( vout_thread_t *p_vout )
209 {
210     /* get initial picture buffer swapped to front buffer */
211     GLSwapBuffers( p_vout );
212
213     /*
214     ** Video window is initially hidden, show it now since we got a
215     ** picture to show.
216     */
217     SetWindowPos( p_vout->p_sys->hvideownd, NULL, 0, 0, 0, 0,
218         SWP_ASYNCWINDOWPOS|
219         SWP_FRAMECHANGED|
220         SWP_SHOWWINDOW|
221         SWP_NOMOVE|
222         SWP_NOSIZE|
223         SWP_NOZORDER );
224
225     /* use and restores proper swap function for further pictures */
226     p_vout->pf_swap = GLSwapBuffers;
227 }