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