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