]> git.sesse.net Git - vlc/blob - modules/video_output/directx/glwin32.c
* backport of [13046] , refs #416
[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., 59 Temple Place - Suite 330, Boston, MA  02111, 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/intf.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
58 /*****************************************************************************
59  * Module descriptor
60  *****************************************************************************/
61 vlc_module_begin();
62     set_category( CAT_VIDEO );
63     set_subcategory( SUBCAT_VIDEO_VOUT );
64     set_shortname( "OpenGL" );
65     set_description( _("OpenGL video output") );
66     set_capability( "opengl provider", 100 );
67     add_shortcut( "glwin32" );
68     set_callbacks( OpenVideo, CloseVideo );
69
70     /* FIXME: Hack to avoid unregistering our window class */
71     linked_with_a_crap_library_which_uses_atexit( );
72 vlc_module_end();
73
74 #if 0 /* FIXME */
75     /* check if we registered a window class because we need to
76      * unregister it */
77     WNDCLASS wndclass;
78     if( GetClassInfo( GetModuleHandle(NULL), "VLC DirectX", &wndclass ) )
79         UnregisterClass( "VLC DirectX", GetModuleHandle(NULL) );
80 #endif
81
82 /*****************************************************************************
83  * OpenVideo: allocate OpenGL provider
84  *****************************************************************************
85  * This function creates and initializes a video window.
86  *****************************************************************************/
87 static int OpenVideo( vlc_object_t *p_this )
88 {
89     vout_thread_t * p_vout = (vout_thread_t *)p_this;
90     vlc_value_t val;
91
92     /* Allocate structure */
93     p_vout->p_sys = malloc( sizeof( vout_sys_t ) );
94     if( p_vout->p_sys == NULL )
95     {
96         msg_Err( p_vout, "out of memory" );
97         return VLC_ENOMEM;
98     }
99     memset( p_vout->p_sys, 0, sizeof( vout_sys_t ) );
100
101     /* Initialisations */
102     p_vout->pf_init = Init;
103     p_vout->pf_end = End;
104     p_vout->pf_manage = Manage;
105     p_vout->pf_swap = GLSwapBuffers;
106
107     p_vout->p_sys->p_ddobject = NULL;
108     p_vout->p_sys->p_display = NULL;
109     p_vout->p_sys->p_current_surface = NULL;
110     p_vout->p_sys->p_clipper = NULL;
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     p_vout->p_sys->b_wallpaper = 0;
115     vlc_mutex_init( p_vout, &p_vout->p_sys->lock );
116     SetRectEmpty( &p_vout->p_sys->rect_display );
117     SetRectEmpty( &p_vout->p_sys->rect_parent );
118
119     var_Create( p_vout, "video-title", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
120
121     p_vout->p_sys->b_cursor_hidden = 0;
122     p_vout->p_sys->i_lastmoved = mdate();
123
124     /* Set main window's size */
125     p_vout->p_sys->i_window_width = p_vout->i_window_width;
126     p_vout->p_sys->i_window_height = p_vout->i_window_height;
127
128     /* Create the DirectXEventThread, this thread is created by us to isolate
129      * the Win32 PeekMessage function calls. We want to do this because
130      * Windows can stay blocked inside this call for a long time, and when
131      * this happens it thus blocks vlc's video_output thread.
132      * DirectXEventThread will take care of the creation of the video
133      * window (because PeekMessage has to be called from the same thread which
134      * created the window). */
135     msg_Dbg( p_vout, "creating DirectXEventThread" );
136     p_vout->p_sys->p_event =
137         vlc_object_create( p_vout, sizeof(event_thread_t) );
138     p_vout->p_sys->p_event->p_vout = p_vout;
139     if( vlc_thread_create( p_vout->p_sys->p_event, "DirectX Events Thread",
140                            E_(DirectXEventThread), 0, 1 ) )
141     {
142         msg_Err( p_vout, "cannot create DirectXEventThread" );
143         vlc_object_destroy( p_vout->p_sys->p_event );
144         p_vout->p_sys->p_event = NULL;
145         goto error;
146     }
147
148     if( p_vout->p_sys->p_event->b_error )
149     {
150         msg_Err( p_vout, "DirectXEventThread failed" );
151         goto error;
152     }
153
154     vlc_object_attach( p_vout->p_sys->p_event, p_vout );
155
156     msg_Dbg( p_vout, "DirectXEventThread running" );
157
158     /* Variable to indicate if the window should be on top of others */
159     /* Trigger a callback right now */
160     var_Get( p_vout, "video-on-top", &val );
161     var_Set( p_vout, "video-on-top", val );
162
163     return VLC_SUCCESS;
164
165  error:
166     CloseVideo( VLC_OBJECT(p_vout) );
167     return VLC_EGENERIC;
168 }
169
170 /*****************************************************************************
171  * Init: initialize video thread output method
172  *****************************************************************************/
173 static int Init( vout_thread_t *p_vout )
174 {
175     PIXELFORMATDESCRIPTOR pfd;
176     int iFormat;
177
178     /* Change the window title bar text */
179     PostMessage( p_vout->p_sys->hwnd, WM_VLC_CHANGE_TEXT, 0, 0 );
180
181     p_vout->p_sys->hGLDC = GetDC( p_vout->p_sys->hvideownd );
182
183     /* Set the pixel format for the DC */
184     memset( &pfd, 0, sizeof( pfd ) );
185     pfd.nSize = sizeof( pfd );
186     pfd.nVersion = 1;
187     pfd.dwFlags = PFD_DRAW_TO_WINDOW | PFD_SUPPORT_OPENGL | PFD_DOUBLEBUFFER;
188     pfd.iPixelType = PFD_TYPE_RGBA;
189     pfd.cColorBits = 24;
190     pfd.cDepthBits = 16;
191     pfd.iLayerType = PFD_MAIN_PLANE;
192     iFormat = ChoosePixelFormat( p_vout->p_sys->hGLDC, &pfd );
193     SetPixelFormat( p_vout->p_sys->hGLDC, iFormat, &pfd );
194
195     /* Create and enable the render context */
196     p_vout->p_sys->hGLRC = wglCreateContext( p_vout->p_sys->hGLDC );
197     wglMakeCurrent( p_vout->p_sys->hGLDC, p_vout->p_sys->hGLRC );
198
199     return VLC_SUCCESS;
200 }
201
202 /*****************************************************************************
203  * End: terminate Sys video thread output method
204  *****************************************************************************
205  * Terminate an output method created by Create.
206  * It is called at the end of the thread.
207  *****************************************************************************/
208 static void End( vout_thread_t *p_vout )
209 {
210     wglMakeCurrent( NULL, NULL );
211     wglDeleteContext( p_vout->p_sys->hGLRC );
212     ReleaseDC( p_vout->p_sys->hvideownd, p_vout->p_sys->hGLDC );
213     return;
214 }
215
216 /*****************************************************************************
217  * CloseVideo: destroy Sys video thread output method
218  *****************************************************************************
219  * Terminate an output method created by Create
220  *****************************************************************************/
221 static void CloseVideo( vlc_object_t *p_this )
222 {
223     vout_thread_t * p_vout = (vout_thread_t *)p_this;
224
225     msg_Dbg( p_vout, "CloseVideo" );
226
227     if( p_vout->p_sys->p_event )
228     {
229         vlc_object_detach( p_vout->p_sys->p_event );
230
231         /* Kill DirectXEventThread */
232         p_vout->p_sys->p_event->b_die = VLC_TRUE;
233
234         /* we need to be sure DirectXEventThread won't stay stuck in
235          * GetMessage, so we send a fake message */
236         if( p_vout->p_sys->hwnd )
237         {
238             PostMessage( p_vout->p_sys->hwnd, WM_NULL, 0, 0);
239         }
240
241         vlc_thread_join( p_vout->p_sys->p_event );
242         vlc_object_destroy( p_vout->p_sys->p_event );
243     }
244
245     vlc_mutex_destroy( &p_vout->p_sys->lock );
246
247     if( p_vout->p_sys )
248     {
249         free( p_vout->p_sys );
250         p_vout->p_sys = NULL;
251     }
252 }
253
254 /*****************************************************************************
255  * Manage: handle Sys events
256  *****************************************************************************
257  * This function should be called regularly by the video output thread.
258  * It returns a non null value if an error occurred.
259  *****************************************************************************/
260 static int Manage( vout_thread_t *p_vout )
261 {
262     WINDOWPLACEMENT window_placement;
263
264     int i_width = p_vout->p_sys->rect_dest.right -
265         p_vout->p_sys->rect_dest.left;
266     int i_height = p_vout->p_sys->rect_dest.bottom -
267         p_vout->p_sys->rect_dest.top;
268     glViewport( 0, 0, i_width, i_height );
269
270     /* If we do not control our window, we check for geometry changes
271      * ourselves because the parent might not send us its events. */
272     vlc_mutex_lock( &p_vout->p_sys->lock );
273     if( p_vout->p_sys->hparent && !p_vout->b_fullscreen )
274     {
275         RECT rect_parent;
276         POINT point;
277
278         vlc_mutex_unlock( &p_vout->p_sys->lock );
279
280         GetClientRect( p_vout->p_sys->hparent, &rect_parent );
281         point.x = point.y = 0;
282         ClientToScreen( p_vout->p_sys->hparent, &point );
283         OffsetRect( &rect_parent, point.x, point.y );
284
285         if( !EqualRect( &rect_parent, &p_vout->p_sys->rect_parent ) )
286         {
287             p_vout->p_sys->rect_parent = rect_parent;
288
289             /* This one is to force the update even if only
290              * the position has changed */
291             SetWindowPos( p_vout->p_sys->hwnd, 0, 1, 1,
292                           rect_parent.right - rect_parent.left,
293                           rect_parent.bottom - rect_parent.top, 0 );
294
295             SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
296                           rect_parent.right - rect_parent.left,
297                           rect_parent.bottom - rect_parent.top, 0 );
298         }
299     }
300     else
301     {
302         vlc_mutex_unlock( &p_vout->p_sys->lock );
303     }
304
305     /* Check for cropping / aspect changes */
306     if( p_vout->i_changes & VOUT_CROP_CHANGE ||
307         p_vout->i_changes & VOUT_ASPECT_CHANGE )
308     {
309         p_vout->i_changes &= ~VOUT_CROP_CHANGE;
310         p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
311
312         p_vout->fmt_out.i_x_offset = p_vout->fmt_in.i_x_offset;
313         p_vout->fmt_out.i_y_offset = p_vout->fmt_in.i_y_offset;
314         p_vout->fmt_out.i_visible_width = p_vout->fmt_in.i_visible_width;
315         p_vout->fmt_out.i_visible_height = p_vout->fmt_in.i_visible_height;
316         p_vout->fmt_out.i_aspect = p_vout->fmt_in.i_aspect;
317         p_vout->fmt_out.i_sar_num = p_vout->fmt_in.i_sar_num;
318         p_vout->fmt_out.i_sar_den = p_vout->fmt_in.i_sar_den;
319         p_vout->output.i_aspect = p_vout->fmt_in.i_aspect;
320         E_(DirectXUpdateRects)( p_vout, VLC_TRUE );
321     }
322
323     /* We used to call the Win32 PeekMessage function here to read the window
324      * messages. But since window can stay blocked into this function for a
325      * long time (for example when you move your window on the screen), I
326      * decided to isolate PeekMessage in another thread. */
327
328     /*
329      * Fullscreen change
330      */
331     if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE
332         || p_vout->p_sys->i_changes & VOUT_FULLSCREEN_CHANGE )
333     {
334         vlc_value_t val;
335         HWND hwnd = (p_vout->p_sys->hparent && p_vout->p_sys->hfswnd) ?
336             p_vout->p_sys->hfswnd : p_vout->p_sys->hwnd;
337
338         p_vout->b_fullscreen = ! p_vout->b_fullscreen;
339
340         /* We need to switch between Maximized and Normal sized window */
341         window_placement.length = sizeof(WINDOWPLACEMENT);
342         GetWindowPlacement( hwnd, &window_placement );
343         if( p_vout->b_fullscreen )
344         {
345             /* Change window style, no borders and no title bar */
346             int i_style = WS_CLIPCHILDREN | WS_VISIBLE;
347             SetWindowLong( hwnd, GWL_STYLE, i_style );
348
349             if( p_vout->p_sys->hparent )
350             {
351                 /* Retrieve current window position so fullscreen will happen
352                  * on the right screen */
353                 POINT point = {0,0};
354                 RECT rect;
355                 ClientToScreen( p_vout->p_sys->hwnd, &point );
356                 GetClientRect( p_vout->p_sys->hwnd, &rect );
357                 SetWindowPos( hwnd, 0, point.x, point.y,
358                               rect.right, rect.bottom,
359                               SWP_NOZORDER|SWP_FRAMECHANGED );
360                 GetWindowPlacement( hwnd, &window_placement );
361             }
362
363             /* Maximize window */
364             window_placement.showCmd = SW_SHOWMAXIMIZED;
365             SetWindowPlacement( hwnd, &window_placement );
366             SetWindowPos( hwnd, 0, 0, 0, 0, 0,
367                           SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER|SWP_FRAMECHANGED);
368
369             if( p_vout->p_sys->hparent )
370             {
371                 RECT rect;
372                 GetClientRect( hwnd, &rect );
373                 SetParent( p_vout->p_sys->hwnd, hwnd );
374                 SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
375                               rect.right, rect.bottom,
376                               SWP_NOZORDER|SWP_FRAMECHANGED );
377             }
378
379             SetForegroundWindow( hwnd );
380         }
381         else
382         {
383             /* Change window style, no borders and no title bar */
384             SetWindowLong( hwnd, GWL_STYLE, p_vout->p_sys->i_window_style );
385
386             /* Normal window */
387             window_placement.showCmd = SW_SHOWNORMAL;
388             SetWindowPlacement( hwnd, &window_placement );
389             SetWindowPos( hwnd, 0, 0, 0, 0, 0,
390                           SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER|SWP_FRAMECHANGED);
391
392             if( p_vout->p_sys->hparent )
393             {
394                 RECT rect;
395                 GetClientRect( p_vout->p_sys->hparent, &rect );
396                 SetParent( p_vout->p_sys->hwnd, p_vout->p_sys->hparent );
397                 SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
398                               rect.right, rect.bottom,
399                               SWP_NOZORDER|SWP_FRAMECHANGED );
400
401                 ShowWindow( hwnd, SW_HIDE );
402                 SetForegroundWindow( p_vout->p_sys->hparent );
403             }
404
405             /* Make sure the mouse cursor is displayed */
406             PostMessage( p_vout->p_sys->hwnd, WM_VLC_SHOW_MOUSE, 0, 0 );
407         }
408
409         /* Update the object variable and trigger callback */
410         val.b_bool = p_vout->b_fullscreen;
411         var_Set( p_vout, "fullscreen", val );
412
413         p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
414         p_vout->p_sys->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
415     }
416
417     /*
418      * Pointer change
419      */
420     if( p_vout->b_fullscreen && !p_vout->p_sys->b_cursor_hidden &&
421         (mdate() - p_vout->p_sys->i_lastmoved) > 5000000 )
422     {
423         POINT point;
424         HWND hwnd;
425
426         /* Hide the cursor only if it is inside our window */
427         GetCursorPos( &point );
428         hwnd = WindowFromPoint(point);
429         if( hwnd == p_vout->p_sys->hwnd || hwnd == p_vout->p_sys->hvideownd )
430         {
431             PostMessage( p_vout->p_sys->hwnd, WM_VLC_HIDE_MOUSE, 0, 0 );
432         }
433         else
434         {
435             p_vout->p_sys->i_lastmoved = mdate();
436         }
437     }
438
439     /*
440      * "Always on top" status change
441      */
442     if( p_vout->p_sys->b_on_top_change )
443     {
444         vlc_value_t val;
445         HMENU hMenu = GetSystemMenu( p_vout->p_sys->hwnd, FALSE );
446
447         var_Get( p_vout, "video-on-top", &val );
448
449         /* Set the window on top if necessary */
450         if( val.b_bool && !( GetWindowLong( p_vout->p_sys->hwnd, GWL_EXSTYLE )
451                            & WS_EX_TOPMOST ) )
452         {
453             CheckMenuItem( hMenu, IDM_TOGGLE_ON_TOP,
454                            MF_BYCOMMAND | MFS_CHECKED );
455             SetWindowPos( p_vout->p_sys->hwnd, HWND_TOPMOST, 0, 0, 0, 0,
456                           SWP_NOSIZE | SWP_NOMOVE );
457         }
458         else
459         /* The window shouldn't be on top */
460         if( !val.b_bool && ( GetWindowLong( p_vout->p_sys->hwnd, GWL_EXSTYLE )
461                            & WS_EX_TOPMOST ) )
462         {
463             CheckMenuItem( hMenu, IDM_TOGGLE_ON_TOP,
464                            MF_BYCOMMAND | MFS_UNCHECKED );
465             SetWindowPos( p_vout->p_sys->hwnd, HWND_NOTOPMOST, 0, 0, 0, 0,
466                           SWP_NOSIZE | SWP_NOMOVE );
467         }
468
469         p_vout->p_sys->b_on_top_change = VLC_FALSE;
470     }
471
472     /* Check if the event thread is still running */
473     if( p_vout->p_sys->p_event->b_die )
474     {
475         return VLC_EGENERIC; /* exit */
476     }
477
478     return VLC_SUCCESS;
479 }
480
481 /*****************************************************************************
482  * GLSwapBuffers: swap front/back buffers
483  *****************************************************************************/
484 static void GLSwapBuffers( vout_thread_t *p_vout )
485 {
486     SwapBuffers( p_vout->p_sys->hGLDC );
487 }
488
489 int E_(DirectXUpdateOverlay)( vout_thread_t *p_vout )
490 {
491     return 1;
492 }