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