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