]> git.sesse.net Git - vlc/blob - modules/video_output/msw/events.c
Partially cleanup the vout_window API
[vlc] / modules / video_output / msw / events.c
1 /*****************************************************************************
2  * events.c: Windows DirectX video output events handler
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
25 /*****************************************************************************
26  * Preamble: This file contains the functions related to the creation of
27  *             a window and the handling of its messages (events).
28  *****************************************************************************/
29 #ifdef HAVE_CONFIG_H
30 # include "config.h"
31 #endif
32
33 #include <errno.h>                                                 /* ENOMEM */
34 #include <ctype.h>                                              /* tolower() */
35
36 #ifndef _WIN32_WINNT
37 #   define _WIN32_WINNT 0x0500
38 #endif
39
40 #include <vlc_common.h>
41 #include <vlc_interface.h>
42 #include <vlc_playlist.h>
43 #include <vlc_vout.h>
44 #include <vlc_window.h>
45
46 #include <windows.h>
47 #include <windowsx.h>
48 #include <shellapi.h>
49
50 #ifdef MODULE_NAME_IS_vout_directx
51 #include <ddraw.h>
52 #endif
53 #ifdef MODULE_NAME_IS_direct3d
54 #include <d3d9.h>
55 #endif
56 #ifdef MODULE_NAME_IS_glwin32
57 #include <GL/gl.h>
58 #endif
59
60 #include "vlc_keys.h"
61 #include "vout.h"
62
63 #ifdef UNDER_CE
64 #include <aygshell.h>
65     //WINSHELLAPI BOOL WINAPI SHFullScreen(HWND hwndRequester, DWORD dwState);
66 #endif
67
68 /*#if defined(UNDER_CE) && !defined(__PLUGIN__) --FIXME*/
69 /*#   define SHFS_SHOWSIPBUTTON 0x0004
70 #   define SHFS_HIDESIPBUTTON 0x0008
71 #   define MENU_HEIGHT 26
72     BOOL SHFullScreen(HWND hwndRequester, DWORD dwState);
73 #endif*/
74
75
76 /*****************************************************************************
77  * Local prototypes.
78  *****************************************************************************/
79 static int  DirectXCreateWindow( vout_thread_t *p_vout );
80 static void DirectXCloseWindow ( vout_thread_t *p_vout );
81 static long FAR PASCAL DirectXEventProc( HWND, UINT, WPARAM, LPARAM );
82
83 static int Control( vout_thread_t *p_vout, int i_query, va_list args );
84 static int vaControlParentWindow( vout_thread_t *, int, va_list );
85
86 static void DirectXPopupMenu( event_thread_t *p_event, bool b_open )
87 {
88     vlc_value_t val;
89     val.b_bool = b_open;
90     var_Set( p_event->p_libvlc, "intf-popupmenu", val );
91 }
92
93 static int DirectXConvertKey( int i_key );
94
95 /*****************************************************************************
96  * EventThread: Create video window & handle its messages
97  *****************************************************************************
98  * This function creates a video window and then enters an infinite loop
99  * that handles the messages sent to that window.
100  * The main goal of this thread is to isolate the Win32 PeekMessage function
101  * because this one can block for a long time.
102  *****************************************************************************/
103 void* EventThread( vlc_object_t *p_this )
104 {
105     event_thread_t *p_event = (event_thread_t *)p_this;
106     MSG msg;
107     POINT old_mouse_pos = {0,0}, mouse_pos;
108     vlc_value_t val;
109     unsigned int i_width, i_height, i_x, i_y;
110     HMODULE hkernel32;
111     int canc = vlc_savecancel ();
112
113     /* Initialisation */
114     p_event->p_vout->pf_control = Control;
115
116     /* Create a window for the video */
117     /* Creating a window under Windows also initializes the thread's event
118      * message queue */
119     if( DirectXCreateWindow( p_event->p_vout ) )
120     {
121         vlc_restorecancel (canc);
122         return NULL;
123     }
124
125     /* Signal the creation of the window */
126     SetEvent( p_event->window_ready );
127
128 #ifndef UNDER_CE
129     /* Set power management stuff */
130     if( (hkernel32 = GetModuleHandle( _T("KERNEL32") ) ) )
131     {
132         ULONG (WINAPI* OurSetThreadExecutionState)( ULONG );
133
134         OurSetThreadExecutionState = (ULONG (WINAPI*)( ULONG ))
135             GetProcAddress( hkernel32, _T("SetThreadExecutionState") );
136
137         if( OurSetThreadExecutionState )
138             /* Prevent monitor from powering off */
139             OurSetThreadExecutionState( ES_DISPLAY_REQUIRED | ES_CONTINUOUS );
140         else
141             msg_Dbg( p_event, "no support for SetThreadExecutionState()" );
142     }
143 #endif
144
145     /* Main loop */
146     /* GetMessage will sleep if there's no message in the queue */
147     while( vlc_object_alive (p_event) && GetMessage( &msg, 0, 0, 0 ) )
148     {
149         /* Check if we are asked to exit */
150         if( !vlc_object_alive (p_event) )
151             break;
152
153         switch( msg.message )
154         {
155
156         case WM_MOUSEMOVE:
157             vout_PlacePicture( p_event->p_vout,
158                                p_event->p_vout->p_sys->i_window_width,
159                                p_event->p_vout->p_sys->i_window_height,
160                                &i_x, &i_y, &i_width, &i_height );
161
162             if( msg.hwnd == p_event->p_vout->p_sys->hvideownd )
163             {
164                 /* Child window */
165                 i_x = i_y = 0;
166             }
167
168             if( i_width && i_height )
169             {
170                 val.i_int = ( GET_X_LPARAM(msg.lParam) - i_x ) *
171                     p_event->p_vout->fmt_in.i_visible_width / i_width +
172                     p_event->p_vout->fmt_in.i_x_offset;
173                 var_Set( p_event->p_vout, "mouse-x", val );
174                 val.i_int = ( GET_Y_LPARAM(msg.lParam) - i_y ) *
175                     p_event->p_vout->fmt_in.i_visible_height / i_height +
176                     p_event->p_vout->fmt_in.i_y_offset;
177                 var_Set( p_event->p_vout, "mouse-y", val );
178
179                 val.b_bool = true;
180                 var_Set( p_event->p_vout, "mouse-moved", val );
181             }
182
183         case WM_NCMOUSEMOVE:
184             GetCursorPos( &mouse_pos );
185             if( (abs(mouse_pos.x - old_mouse_pos.x) > 2 ||
186                 (abs(mouse_pos.y - old_mouse_pos.y)) > 2 ) )
187             {
188                 GetCursorPos( &old_mouse_pos );
189                 p_event->p_vout->p_sys->i_lastmoved = mdate();
190
191                 if( p_event->p_vout->p_sys->b_cursor_hidden )
192                 {
193                     p_event->p_vout->p_sys->b_cursor_hidden = 0;
194                     ShowCursor( TRUE );
195                 }
196             }
197             break;
198
199         case WM_VLC_HIDE_MOUSE:
200             if( p_event->p_vout->p_sys->b_cursor_hidden ) break;
201             p_event->p_vout->p_sys->b_cursor_hidden = true;
202             GetCursorPos( &old_mouse_pos );
203             ShowCursor( FALSE );
204             break;
205
206         case WM_VLC_SHOW_MOUSE:
207             if( !p_event->p_vout->p_sys->b_cursor_hidden ) break;
208             p_event->p_vout->p_sys->b_cursor_hidden = false;
209             GetCursorPos( &old_mouse_pos );
210             ShowCursor( TRUE );
211             break;
212
213         case WM_LBUTTONDOWN:
214             var_Get( p_event->p_vout, "mouse-button-down", &val );
215             val.i_int |= 1;
216             var_Set( p_event->p_vout, "mouse-button-down", val );
217             DirectXPopupMenu( p_event, false );
218             break;
219
220         case WM_LBUTTONUP:
221             var_Get( p_event->p_vout, "mouse-button-down", &val );
222             val.i_int &= ~1;
223             var_Set( p_event->p_vout, "mouse-button-down", val );
224
225             val.b_bool = true;
226             var_Set( p_event->p_vout, "mouse-clicked", val );
227             break;
228
229         case WM_LBUTTONDBLCLK:
230             p_event->p_vout->p_sys->i_changes |= VOUT_FULLSCREEN_CHANGE;
231             break;
232
233         case WM_MBUTTONDOWN:
234             var_Get( p_event->p_vout, "mouse-button-down", &val );
235             val.i_int |= 2;
236             var_Set( p_event->p_vout, "mouse-button-down", val );
237             DirectXPopupMenu( p_event, false );
238             break;
239
240         case WM_MBUTTONUP:
241             var_Get( p_event->p_vout, "mouse-button-down", &val );
242             val.i_int &= ~2;
243             var_Set( p_event->p_vout, "mouse-button-down", val );
244             break;
245
246         case WM_RBUTTONDOWN:
247             var_Get( p_event->p_vout, "mouse-button-down", &val );
248             val.i_int |= 4;
249             var_Set( p_event->p_vout, "mouse-button-down", val );
250             DirectXPopupMenu( p_event, false );
251             break;
252
253         case WM_RBUTTONUP:
254             var_Get( p_event->p_vout, "mouse-button-down", &val );
255             val.i_int &= ~4;
256             var_Set( p_event->p_vout, "mouse-button-down", val );
257             DirectXPopupMenu( p_event, true );
258             break;
259
260         case WM_KEYDOWN:
261         case WM_SYSKEYDOWN:
262             /* The key events are first processed here and not translated
263              * into WM_CHAR events because we need to know the status of the
264              * modifier keys. */
265             val.i_int = DirectXConvertKey( msg.wParam );
266             if( !val.i_int )
267             {
268                 /* This appears to be a "normal" (ascii) key */
269                 val.i_int = tolower( MapVirtualKey( msg.wParam, 2 ) );
270             }
271
272             if( val.i_int )
273             {
274                 if( GetKeyState(VK_CONTROL) & 0x8000 )
275                 {
276                     val.i_int |= KEY_MODIFIER_CTRL;
277                 }
278                 if( GetKeyState(VK_SHIFT) & 0x8000 )
279                 {
280                     val.i_int |= KEY_MODIFIER_SHIFT;
281                 }
282                 if( GetKeyState(VK_MENU) & 0x8000 )
283                 {
284                     val.i_int |= KEY_MODIFIER_ALT;
285                 }
286
287                 var_Set( p_event->p_libvlc, "key-pressed", val );
288             }
289             break;
290
291         case WM_MOUSEWHEEL:
292             if( GET_WHEEL_DELTA_WPARAM( msg.wParam ) > 0 )
293             {
294                 val.i_int = KEY_MOUSEWHEELUP;
295             }
296             else
297             {
298                 val.i_int = KEY_MOUSEWHEELDOWN;
299             }
300             if( val.i_int )
301             {
302                 if( GetKeyState(VK_CONTROL) & 0x8000 )
303                 {
304                     val.i_int |= KEY_MODIFIER_CTRL;
305                 }
306                 if( GetKeyState(VK_SHIFT) & 0x8000 )
307                 {
308                     val.i_int |= KEY_MODIFIER_SHIFT;
309                 }
310                 if( GetKeyState(VK_MENU) & 0x8000 )
311                 {
312                     val.i_int |= KEY_MODIFIER_ALT;
313                 }
314
315                 var_Set( p_event->p_libvlc, "key-pressed", val );
316             }
317             break;
318
319         case WM_VLC_CHANGE_TEXT:
320             var_Get( p_event->p_vout, "video-title", &val );
321             if( !val.psz_string || !*val.psz_string ) /* Default video title */
322             {
323                 free( val.psz_string );
324
325 #ifdef MODULE_NAME_IS_wingdi
326                 val.psz_string = strdup( VOUT_TITLE " (WinGDI output)" );
327 #endif
328 #ifdef MODULE_NAME_IS_wingapi
329                 val.psz_string = strdup( VOUT_TITLE " (WinGAPI output)" );
330 #endif
331 #ifdef MODULE_NAME_IS_glwin32
332                 val.psz_string = strdup( VOUT_TITLE " (OpenGL output)" );
333 #endif
334 #ifdef MODULE_NAME_IS_direct3d
335                 val.psz_string = strdup( VOUT_TITLE " (Direct3D output)" );
336 #endif
337 #ifdef MODULE_NAME_IS_vout_directx
338                 if( p_event->p_vout->p_sys->b_using_overlay ) val.psz_string =
339                     strdup( VOUT_TITLE " (hardware YUV overlay DirectX output)" );
340                 else if( p_event->p_vout->p_sys->b_hw_yuv ) val.psz_string =
341                     strdup( VOUT_TITLE " (hardware YUV DirectX output)" );
342                 else val.psz_string =
343                     strdup( VOUT_TITLE " (software RGB DirectX output)" );
344 #endif
345             }
346
347 #ifdef UNICODE
348             {
349                 wchar_t *psz_title = malloc( strlen(val.psz_string) * 2 + 2 );
350                 if( psz_title )
351                 {
352                     mbstowcs( psz_title, val.psz_string, strlen(val.psz_string)*2);
353                     psz_title[strlen(val.psz_string)] = 0;
354                     free( val.psz_string ); val.psz_string = (char *)psz_title;
355                 }
356             }
357 #endif
358
359             SetWindowText( p_event->p_vout->p_sys->hwnd,
360                            (LPCTSTR)val.psz_string );
361             if( p_event->p_vout->p_sys->hfswnd )
362                 SetWindowText( p_event->p_vout->p_sys->hfswnd,
363                                (LPCTSTR)val.psz_string );
364             free( val.psz_string );
365             break;
366
367         default:
368             /* Messages we don't handle directly are dispatched to the
369              * window procedure */
370             TranslateMessage(&msg);
371             DispatchMessage(&msg);
372             break;
373
374         } /* End Switch */
375
376     } /* End Main loop */
377
378     /* Check for WM_QUIT if we created the window */
379     if( !p_event->p_vout->p_sys->hparent && msg.message == WM_QUIT )
380     {
381         msg_Warn( p_event, "WM_QUIT... should not happen!!" );
382         p_event->p_vout->p_sys->hwnd = NULL; /* Window already destroyed */
383     }
384
385     msg_Dbg( p_event, "DirectXEventThread terminating" );
386
387     /* clear the changes formerly signaled */
388     p_event->p_vout->p_sys->i_changes = 0;
389
390     DirectXCloseWindow( p_event->p_vout );
391     vlc_restorecancel (canc);
392     return NULL;
393 }
394
395
396 /* following functions are local */
397
398 /*****************************************************************************
399  * DirectXCreateWindow: create a window for the video.
400  *****************************************************************************
401  * Before creating a direct draw surface, we need to create a window in which
402  * the video will be displayed. This window will also allow us to capture the
403  * events.
404  *****************************************************************************/
405 static int DirectXCreateWindow( vout_thread_t *p_vout )
406 {
407     HINSTANCE  hInstance;
408     HMENU      hMenu;
409     RECT       rect_window;
410     WNDCLASS   wc;                            /* window class components */
411     HICON      vlc_icon = NULL;
412     char       vlc_path[MAX_PATH+1];
413     int        i_style, i_stylex;
414
415     msg_Dbg( p_vout, "DirectXCreateWindow" );
416
417     /* Get this module's instance */
418     hInstance = GetModuleHandle(NULL);
419
420     /* If an external window was specified, we'll draw in it. */
421     p_vout->p_sys->parent_window =
422         vout_RequestWindow( p_vout, &p_vout->p_sys->i_window_x,
423                             &p_vout->p_sys->i_window_y,
424                             &p_vout->p_sys->i_window_width,
425                             &p_vout->p_sys->i_window_height );
426     p_vout->p_sys->hparent = p_vout->p_sys->parent_window->handle;
427
428     /* We create the window ourself, there is no previous window proc. */
429     p_vout->p_sys->pf_wndproc = NULL;
430
431     /* Get the Icon from the main app */
432     vlc_icon = NULL;
433 #ifndef UNDER_CE
434     if( GetModuleFileName( NULL, vlc_path, MAX_PATH ) )
435     {
436         vlc_icon = ExtractIcon( hInstance, vlc_path, 0 );
437     }
438 #endif
439
440     /* Fill in the window class structure */
441     wc.style         = CS_OWNDC|CS_DBLCLKS;          /* style: dbl click */
442     wc.lpfnWndProc   = (WNDPROC)DirectXEventProc;       /* event handler */
443     wc.cbClsExtra    = 0;                         /* no extra class data */
444     wc.cbWndExtra    = 0;                        /* no extra window data */
445     wc.hInstance     = hInstance;                            /* instance */
446     wc.hIcon         = vlc_icon;                /* load the vlc big icon */
447     wc.hCursor       = LoadCursor(NULL, IDC_ARROW);    /* default cursor */
448     wc.hbrBackground = GetStockObject(BLACK_BRUSH);  /* background color */
449     wc.lpszMenuName  = NULL;                                  /* no menu */
450     wc.lpszClassName = _T("VLC DirectX");         /* use a special class */
451
452     /* Register the window class */
453     if( !RegisterClass(&wc) )
454     {
455         WNDCLASS wndclass;
456
457         if( vlc_icon ) DestroyIcon( vlc_icon );
458
459         /* Check why it failed. If it's because one already exists
460          * then fine, otherwise return with an error. */
461         if( !GetClassInfo( hInstance, _T("VLC DirectX"), &wndclass ) )
462         {
463             msg_Err( p_vout, "DirectXCreateWindow RegisterClass FAILED (err=%lu)", GetLastError() );
464             return VLC_EGENERIC;
465         }
466     }
467
468     /* Register the video sub-window class */
469     wc.lpszClassName = _T("VLC DirectX video"); wc.hIcon = 0;
470     wc.hbrBackground = NULL; /* no background color */
471     if( !RegisterClass(&wc) )
472     {
473         WNDCLASS wndclass;
474
475         /* Check why it failed. If it's because one already exists
476          * then fine, otherwise return with an error. */
477         if( !GetClassInfo( hInstance, _T("VLC DirectX video"), &wndclass ) )
478         {
479             msg_Err( p_vout, "DirectXCreateWindow RegisterClass FAILED (err=%lu)", GetLastError() );
480             return VLC_EGENERIC;
481         }
482     }
483
484     /* When you create a window you give the dimensions you wish it to
485      * have. Unfortunatly these dimensions will include the borders and
486      * titlebar. We use the following function to find out the size of
487      * the window corresponding to the useable surface we want */
488     rect_window.top    = 10;
489     rect_window.left   = 10;
490     rect_window.right  = rect_window.left + p_vout->p_sys->i_window_width;
491     rect_window.bottom = rect_window.top + p_vout->p_sys->i_window_height;
492
493     if( var_GetBool( p_vout, "video-deco" ) )
494     {
495         /* Open with window decoration */
496         AdjustWindowRect( &rect_window, WS_OVERLAPPEDWINDOW|WS_SIZEBOX, 0 );
497         i_style = WS_OVERLAPPEDWINDOW|WS_SIZEBOX|WS_VISIBLE|WS_CLIPCHILDREN;
498         i_stylex = 0;
499     }
500     else
501     {
502         /* No window decoration */
503         AdjustWindowRect( &rect_window, WS_POPUP, 0 );
504         i_style = WS_POPUP|WS_VISIBLE|WS_CLIPCHILDREN;
505         i_stylex = 0; // WS_EX_TOOLWINDOW; Is TOOLWINDOW really needed ?
506                       // It messes up the fullscreen window.
507     }
508
509     if( p_vout->p_sys->hparent )
510     {
511         i_style = WS_VISIBLE|WS_CLIPCHILDREN|WS_CHILD;
512         i_stylex = 0;
513     }
514
515     p_vout->p_sys->i_window_style = i_style;
516
517     /* Create the window */
518     p_vout->p_sys->hwnd =
519         CreateWindowEx( WS_EX_NOPARENTNOTIFY | i_stylex,
520                     _T("VLC DirectX"),               /* name of window class */
521                     _T(VOUT_TITLE) _T(" (DirectX Output)"),  /* window title */
522                     i_style,                                 /* window style */
523                     (p_vout->p_sys->i_window_x < 0) ? CW_USEDEFAULT :
524                         (UINT)p_vout->p_sys->i_window_x,   /* default X coordinate */
525                     (p_vout->p_sys->i_window_y < 0) ? CW_USEDEFAULT :
526                         (UINT)p_vout->p_sys->i_window_y,   /* default Y coordinate */
527                     rect_window.right - rect_window.left,    /* window width */
528                     rect_window.bottom - rect_window.top,   /* window height */
529                     p_vout->p_sys->hparent,                 /* parent window */
530                     NULL,                          /* no menu in this window */
531                     hInstance,            /* handle of this program instance */
532                     (LPVOID)p_vout );            /* send p_vout to WM_CREATE */
533
534     if( !p_vout->p_sys->hwnd )
535     {
536         msg_Warn( p_vout, "DirectXCreateWindow create window FAILED (err=%lu)", GetLastError() );
537         return VLC_EGENERIC;
538     }
539
540     if( p_vout->p_sys->hparent )
541     {
542         LONG i_style;
543
544         /* We don't want the window owner to overwrite our client area */
545         i_style = GetWindowLong( p_vout->p_sys->hparent, GWL_STYLE );
546
547         if( !(i_style & WS_CLIPCHILDREN) )
548             /* Hmmm, apparently this is a blocking call... */
549             SetWindowLong( p_vout->p_sys->hparent, GWL_STYLE,
550                            i_style | WS_CLIPCHILDREN );
551
552         /* Create our fullscreen window */
553         p_vout->p_sys->hfswnd =
554             CreateWindowEx( WS_EX_APPWINDOW, _T("VLC DirectX"),
555                             _T(VOUT_TITLE) _T(" (DirectX Output)"),
556                             WS_OVERLAPPEDWINDOW|WS_CLIPCHILDREN|WS_SIZEBOX,
557                             CW_USEDEFAULT, CW_USEDEFAULT,
558                             CW_USEDEFAULT, CW_USEDEFAULT,
559                             NULL, NULL, hInstance, NULL );
560     }
561
562     /* Append a "Always On Top" entry in the system menu */
563     hMenu = GetSystemMenu( p_vout->p_sys->hwnd, FALSE );
564     AppendMenu( hMenu, MF_SEPARATOR, 0, _T("") );
565     AppendMenu( hMenu, MF_STRING | MF_UNCHECKED,
566                        IDM_TOGGLE_ON_TOP, _T("Always on &Top") );
567
568     /* Create video sub-window. This sub window will always exactly match
569      * the size of the video, which allows us to use crazy overlay colorkeys
570      * without having them shown outside of the video area. */
571     p_vout->p_sys->hvideownd =
572     CreateWindow( _T("VLC DirectX video"), _T(""),   /* window class */
573         WS_CHILD,                   /* window style, not visible initially */
574         0, 0,
575         p_vout->render.i_width,         /* default width */
576         p_vout->render.i_height,        /* default height */
577         p_vout->p_sys->hwnd,                    /* parent window */
578         NULL, hInstance,
579         (LPVOID)p_vout );            /* send p_vout to WM_CREATE */
580
581     if( !p_vout->p_sys->hvideownd )
582         msg_Warn( p_vout, "can't create video sub-window" );
583     else
584         msg_Dbg( p_vout, "created video sub-window" );
585
586     /* Now display the window */
587     ShowWindow( p_vout->p_sys->hwnd, SW_SHOW );
588
589     return VLC_SUCCESS;
590 }
591
592 /*****************************************************************************
593  * DirectXCloseWindow: close the window created by DirectXCreateWindow
594  *****************************************************************************
595  * This function returns all resources allocated by DirectXCreateWindow.
596  *****************************************************************************/
597 static void DirectXCloseWindow( vout_thread_t *p_vout )
598 {
599     msg_Dbg( p_vout, "DirectXCloseWindow" );
600
601     DestroyWindow( p_vout->p_sys->hwnd );
602     if( p_vout->p_sys->hfswnd ) DestroyWindow( p_vout->p_sys->hfswnd );
603
604     vout_ReleaseWindow( p_vout->p_sys->parent_window );
605     p_vout->p_sys->hwnd = NULL;
606
607     /* We don't unregister the Window Class because it could lead to race
608      * conditions and it will be done anyway by the system when the app will
609      * exit */
610 }
611
612 /*****************************************************************************
613  * UpdateRects: update clipping rectangles
614  *****************************************************************************
615  * This function is called when the window position or size are changed, and
616  * its job is to update the source and destination RECTs used to display the
617  * picture.
618  *****************************************************************************/
619 void UpdateRects( vout_thread_t *p_vout, bool b_force )
620 {
621 #define rect_src p_vout->p_sys->rect_src
622 #define rect_src_clipped p_vout->p_sys->rect_src_clipped
623 #define rect_dest p_vout->p_sys->rect_dest
624 #define rect_dest_clipped p_vout->p_sys->rect_dest_clipped
625
626     int i_width, i_height, i_x, i_y;
627
628     RECT  rect;
629     POINT point;
630
631     /* Retrieve the window size */
632     GetClientRect( p_vout->p_sys->hwnd, &rect );
633
634     /* Retrieve the window position */
635     point.x = point.y = 0;
636     ClientToScreen( p_vout->p_sys->hwnd, &point );
637
638     /* If nothing changed, we can return */
639     if( !b_force
640          && p_vout->p_sys->i_window_width == rect.right
641          && p_vout->p_sys->i_window_height == rect.bottom
642          && p_vout->p_sys->i_window_x == point.x
643          && p_vout->p_sys->i_window_y == point.y )
644     {
645         return;
646     }
647
648     /* Update the window position and size */
649     p_vout->p_sys->i_window_x = point.x;
650     p_vout->p_sys->i_window_y = point.y;
651     p_vout->p_sys->i_window_width = rect.right;
652     p_vout->p_sys->i_window_height = rect.bottom;
653
654     vout_PlacePicture( p_vout, rect.right, rect.bottom,
655                        &i_x, &i_y, &i_width, &i_height );
656
657     if( p_vout->p_sys->hvideownd )
658         SetWindowPos( p_vout->p_sys->hvideownd, 0,
659                       i_x, i_y, i_width, i_height,
660                       SWP_NOCOPYBITS|SWP_NOZORDER|SWP_ASYNCWINDOWPOS );
661
662     /* Destination image position and dimensions */
663     rect_dest.left = point.x + i_x;
664     rect_dest.right = rect_dest.left + i_width;
665     rect_dest.top = point.y + i_y;
666     rect_dest.bottom = rect_dest.top + i_height;
667
668 #ifdef MODULE_NAME_IS_vout_directx
669     /* Apply overlay hardware constraints */
670     if( p_vout->p_sys->b_using_overlay )
671     {
672         if( p_vout->p_sys->i_align_dest_boundary )
673             rect_dest.left = ( rect_dest.left +
674                 p_vout->p_sys->i_align_dest_boundary / 2 ) &
675                 ~p_vout->p_sys->i_align_dest_boundary;
676
677         if( p_vout->p_sys->i_align_dest_size )
678             rect_dest.right = (( rect_dest.right - rect_dest.left +
679                 p_vout->p_sys->i_align_dest_size / 2 ) &
680                 ~p_vout->p_sys->i_align_dest_size) + rect_dest.left;
681     }
682
683     /* UpdateOverlay directdraw function doesn't automatically clip to the
684      * display size so we need to do it otherwise it will fail */
685
686     /* Clip the destination window */
687     if( !IntersectRect( &rect_dest_clipped, &rect_dest,
688                         &p_vout->p_sys->rect_display ) )
689     {
690         SetRectEmpty( &rect_src_clipped );
691         return;
692     }
693
694 #if 0
695     msg_Dbg( p_vout, "DirectXUpdateRects image_dst_clipped coords:"
696                      " %i,%i,%i,%i",
697                      rect_dest_clipped.left, rect_dest_clipped.top,
698                      rect_dest_clipped.right, rect_dest_clipped.bottom );
699 #endif
700
701 #else /* MODULE_NAME_IS_vout_directx */
702
703     /* AFAIK, there are no clipping constraints in Direct3D, OpenGL and GDI */
704     rect_dest_clipped = rect_dest;
705
706 #endif
707
708     /* the 2 following lines are to fix a bug when clicking on the desktop */
709     if( (rect_dest_clipped.right - rect_dest_clipped.left)==0 ||
710         (rect_dest_clipped.bottom - rect_dest_clipped.top)==0 )
711     {
712         SetRectEmpty( &rect_src_clipped );
713         return;
714     }
715
716     /* src image dimensions */
717     rect_src.left = 0;
718     rect_src.top = 0;
719     rect_src.right = p_vout->render.i_width;
720     rect_src.bottom = p_vout->render.i_height;
721
722     /* Clip the source image */
723     rect_src_clipped.left = p_vout->fmt_out.i_x_offset +
724       (rect_dest_clipped.left - rect_dest.left) *
725       p_vout->fmt_out.i_visible_width / (rect_dest.right - rect_dest.left);
726     rect_src_clipped.right = p_vout->fmt_out.i_x_offset +
727       p_vout->fmt_out.i_visible_width -
728       (rect_dest.right - rect_dest_clipped.right) *
729       p_vout->fmt_out.i_visible_width / (rect_dest.right - rect_dest.left);
730     rect_src_clipped.top = p_vout->fmt_out.i_y_offset +
731       (rect_dest_clipped.top - rect_dest.top) *
732       p_vout->fmt_out.i_visible_height / (rect_dest.bottom - rect_dest.top);
733     rect_src_clipped.bottom = p_vout->fmt_out.i_y_offset +
734       p_vout->fmt_out.i_visible_height -
735       (rect_dest.bottom - rect_dest_clipped.bottom) *
736       p_vout->fmt_out.i_visible_height / (rect_dest.bottom - rect_dest.top);
737
738 #ifdef MODULE_NAME_IS_vout_directx
739     /* Apply overlay hardware constraints */
740     if( p_vout->p_sys->b_using_overlay )
741     {
742         if( p_vout->p_sys->i_align_src_boundary )
743             rect_src_clipped.left = ( rect_src_clipped.left +
744                 p_vout->p_sys->i_align_src_boundary / 2 ) &
745                 ~p_vout->p_sys->i_align_src_boundary;
746
747         if( p_vout->p_sys->i_align_src_size )
748             rect_src_clipped.right = (( rect_src_clipped.right -
749                 rect_src_clipped.left +
750                 p_vout->p_sys->i_align_src_size / 2 ) &
751                 ~p_vout->p_sys->i_align_src_size) + rect_src_clipped.left;
752     }
753 #endif
754
755 #if 0
756     msg_Dbg( p_vout, "DirectXUpdateRects image_src_clipped"
757                      " coords: %i,%i,%i,%i",
758                      rect_src_clipped.left, rect_src_clipped.top,
759                      rect_src_clipped.right, rect_src_clipped.bottom );
760 #endif
761
762 #ifdef MODULE_NAME_IS_vout_directx
763     /* The destination coordinates need to be relative to the current
764      * directdraw primary surface (display) */
765     rect_dest_clipped.left -= p_vout->p_sys->rect_display.left;
766     rect_dest_clipped.right -= p_vout->p_sys->rect_display.left;
767     rect_dest_clipped.top -= p_vout->p_sys->rect_display.top;
768     rect_dest_clipped.bottom -= p_vout->p_sys->rect_display.top;
769
770     if( p_vout->p_sys->b_using_overlay )
771         DirectDrawUpdateOverlay( p_vout );
772 #endif
773
774     /* Signal the change in size/position */
775     p_vout->p_sys->i_changes |= DX_POSITION_CHANGE;
776
777 #undef rect_src
778 #undef rect_src_clipped
779 #undef rect_dest
780 #undef rect_dest_clipped
781 }
782
783 /*****************************************************************************
784  * DirectXEventProc: This is the window event processing function.
785  *****************************************************************************
786  * On Windows, when you create a window you have to attach an event processing
787  * function to it. The aim of this function is to manage "Queued Messages" and
788  * "Nonqueued Messages".
789  * Queued Messages are those picked up and retransmitted by vout_Manage
790  * (using the GetMessage and DispatchMessage functions).
791  * Nonqueued Messages are those that Windows will send directly to this
792  * procedure (like WM_DESTROY, WM_WINDOWPOSCHANGED...)
793  *****************************************************************************/
794 static long FAR PASCAL DirectXEventProc( HWND hwnd, UINT message,
795                                          WPARAM wParam, LPARAM lParam )
796 {
797     vout_thread_t *p_vout;
798
799     if( message == WM_CREATE )
800     {
801         /* Store p_vout for future use */
802         p_vout = (vout_thread_t *)((CREATESTRUCT *)lParam)->lpCreateParams;
803         SetWindowLongPtr( hwnd, GWLP_USERDATA, (LONG_PTR)p_vout );
804         return TRUE;
805     }
806     else
807     {
808         p_vout = (vout_thread_t *)GetWindowLongPtr( hwnd, GWLP_USERDATA );
809         if( !p_vout )
810         {
811             /* Hmmm mozilla does manage somehow to save the pointer to our
812              * windowproc and still calls it after the vout has been closed. */
813             return DefWindowProc(hwnd, message, wParam, lParam);
814         }
815     }
816
817 #ifndef UNDER_CE
818     /* Catch the screensaver and the monitor turn-off */
819     if( message == WM_SYSCOMMAND &&
820         ( (wParam & 0xFFF0) == SC_SCREENSAVE || (wParam & 0xFFF0) == SC_MONITORPOWER ) )
821     {
822         //if( p_vout ) msg_Dbg( p_vout, "WinProc WM_SYSCOMMAND screensaver" );
823         return 0; /* this stops them from happening */
824     }
825 #endif
826
827     if( hwnd == p_vout->p_sys->hvideownd )
828     {
829         switch( message )
830         {
831 #ifdef MODULE_NAME_IS_vout_directx
832         case WM_ERASEBKGND:
833         /* For overlay, we need to erase background */
834             return !p_vout->p_sys->b_using_overlay ?
835                 1 : DefWindowProc(hwnd, message, wParam, lParam);
836         case WM_PAINT:
837         /*
838         ** For overlay, DefWindowProc() will erase dirty regions
839         ** with colorkey.
840         ** For non-overlay, vout will paint the whole window at
841         ** regular interval, therefore dirty regions can be ignored
842         ** to minimize repaint.
843         */
844             if( !p_vout->p_sys->b_using_overlay )
845             {
846                 ValidateRect(hwnd, NULL);
847             }
848             // fall through to default
849 #else
850         /*
851         ** For OpenGL and Direct3D, vout will update the whole
852         ** window at regular interval, therefore dirty region
853         ** can be ignored to minimize repaint.
854         */
855         case WM_ERASEBKGND:
856             /* nothing to erase */
857             return 1;
858         case WM_PAINT:
859             /* nothing to repaint */
860             ValidateRect(hwnd, NULL);
861             // fall through
862 #endif
863         default:
864             return DefWindowProc(hwnd, message, wParam, lParam);
865         }
866     }
867
868     switch( message )
869     {
870
871     case WM_WINDOWPOSCHANGED:
872         UpdateRects( p_vout, true );
873         return 0;
874
875     /* the user wants to close the window */
876     case WM_CLOSE:
877     {
878         playlist_t * p_playlist = pl_Hold( p_vout );
879         if( p_playlist )
880         {
881             playlist_Stop( p_playlist );
882             pl_Release( p_vout );
883         }
884         return 0;
885     }
886
887     /* the window has been closed so shut down everything now */
888     case WM_DESTROY:
889         msg_Dbg( p_vout, "WinProc WM_DESTROY" );
890         /* just destroy the window */
891         PostQuitMessage( 0 );
892         return 0;
893
894     case WM_SYSCOMMAND:
895         switch (wParam)
896         {
897             case IDM_TOGGLE_ON_TOP:            /* toggle the "on top" status */
898             {
899                 vlc_value_t val;
900                 msg_Dbg( p_vout, "WinProc WM_SYSCOMMAND: IDM_TOGGLE_ON_TOP");
901
902                 /* Change the current value */
903                 var_Get( p_vout, "video-on-top", &val );
904                 val.b_bool = !val.b_bool;
905                 var_Set( p_vout, "video-on-top", val );
906                 return 0;
907             }
908         }
909         break;
910
911     case WM_PAINT:
912     case WM_NCPAINT:
913     case WM_ERASEBKGND:
914         return DefWindowProc(hwnd, message, wParam, lParam);
915
916     case WM_KILLFOCUS:
917 #ifdef MODULE_NAME_IS_wingapi
918         p_vout->p_sys->b_focus = false;
919         if( !p_vout->p_sys->b_parent_focus ) GXSuspend();
920 #endif
921 #ifdef UNDER_CE
922         if( hwnd == p_vout->p_sys->hfswnd )
923         {
924             HWND htbar = FindWindow( _T("HHTaskbar"), NULL );
925             ShowWindow( htbar, SW_SHOW );
926         }
927
928         if( !p_vout->p_sys->hparent ||
929             hwnd == p_vout->p_sys->hfswnd )
930         {
931             SHFullScreen( hwnd, SHFS_SHOWSIPBUTTON );
932         }
933 #endif
934         return 0;
935
936     case WM_SETFOCUS:
937 #ifdef MODULE_NAME_IS_wingapi
938         p_vout->p_sys->b_focus = true;
939         GXResume();
940 #endif
941 #ifdef UNDER_CE
942         if( p_vout->p_sys->hparent &&
943             hwnd != p_vout->p_sys->hfswnd && p_vout->b_fullscreen )
944             p_vout->p_sys->i_changes |= VOUT_FULLSCREEN_CHANGE;
945
946         if( hwnd == p_vout->p_sys->hfswnd )
947         {
948             HWND htbar = FindWindow( _T("HHTaskbar"), NULL );
949             ShowWindow( htbar, SW_HIDE );
950         }
951
952         if( !p_vout->p_sys->hparent ||
953             hwnd == p_vout->p_sys->hfswnd )
954         {
955             SHFullScreen( hwnd, SHFS_HIDESIPBUTTON );
956         }
957 #endif
958         return 0;
959
960     default:
961         //msg_Dbg( p_vout, "WinProc WM Default %i", message );
962         break;
963     }
964
965     /* Let windows handle the message */
966     return DefWindowProc(hwnd, message, wParam, lParam);
967 }
968
969 static struct
970 {
971     int i_dxkey;
972     int i_vlckey;
973
974 } dxkeys_to_vlckeys[] =
975 {
976     { VK_F1, KEY_F1 }, { VK_F2, KEY_F2 }, { VK_F3, KEY_F3 }, { VK_F4, KEY_F4 },
977     { VK_F5, KEY_F5 }, { VK_F6, KEY_F6 }, { VK_F7, KEY_F7 }, { VK_F8, KEY_F8 },
978     { VK_F9, KEY_F9 }, { VK_F10, KEY_F10 }, { VK_F11, KEY_F11 },
979     { VK_F12, KEY_F12 },
980
981     { VK_RETURN, KEY_ENTER },
982     { VK_SPACE, KEY_SPACE },
983     { VK_ESCAPE, KEY_ESC },
984
985     { VK_LEFT, KEY_LEFT },
986     { VK_RIGHT, KEY_RIGHT },
987     { VK_UP, KEY_UP },
988     { VK_DOWN, KEY_DOWN },
989
990     { VK_HOME, KEY_HOME },
991     { VK_END, KEY_END },
992     { VK_PRIOR, KEY_PAGEUP },
993     { VK_NEXT, KEY_PAGEDOWN },
994
995     { VK_INSERT, KEY_INSERT },
996     { VK_DELETE, KEY_DELETE },
997
998     { VK_CONTROL, 0 },
999     { VK_SHIFT, 0 },
1000     { VK_MENU, 0 },
1001
1002     { VK_BROWSER_BACK, KEY_BROWSER_BACK },
1003     { VK_BROWSER_FORWARD, KEY_BROWSER_FORWARD },
1004     { VK_BROWSER_REFRESH, KEY_BROWSER_REFRESH },
1005     { VK_BROWSER_STOP, KEY_BROWSER_STOP },
1006     { VK_BROWSER_SEARCH, KEY_BROWSER_SEARCH },
1007     { VK_BROWSER_FAVORITES, KEY_BROWSER_FAVORITES },
1008     { VK_BROWSER_HOME, KEY_BROWSER_HOME },
1009     { VK_VOLUME_MUTE, KEY_VOLUME_MUTE },
1010     { VK_VOLUME_DOWN, KEY_VOLUME_DOWN },
1011     { VK_VOLUME_UP, KEY_VOLUME_UP },
1012     { VK_MEDIA_NEXT_TRACK, KEY_MEDIA_NEXT_TRACK },
1013     { VK_MEDIA_PREV_TRACK, KEY_MEDIA_PREV_TRACK },
1014     { VK_MEDIA_STOP, KEY_MEDIA_STOP },
1015     { VK_MEDIA_PLAY_PAUSE, KEY_MEDIA_PLAY_PAUSE },
1016
1017     { 0, 0 }
1018 };
1019
1020 static int DirectXConvertKey( int i_key )
1021 {
1022     int i;
1023
1024     for( i = 0; dxkeys_to_vlckeys[i].i_dxkey != 0; i++ )
1025     {
1026         if( dxkeys_to_vlckeys[i].i_dxkey == i_key )
1027         {
1028             return dxkeys_to_vlckeys[i].i_vlckey;
1029         }
1030     }
1031
1032     return 0;
1033 }
1034
1035 /*****************************************************************************
1036  * Control: control facility for the vout
1037  *****************************************************************************/
1038 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
1039 {
1040     unsigned int *pi_width, *pi_height;
1041         bool b_bool;
1042     RECT rect_window;
1043     POINT point;
1044
1045     switch( i_query )
1046     {
1047     case VOUT_GET_SIZE:
1048         if( p_vout->p_sys->parent_window )
1049             return vaControlParentWindow( p_vout, i_query, args );
1050
1051         pi_width  = va_arg( args, unsigned int * );
1052         pi_height = va_arg( args, unsigned int * );
1053
1054         GetClientRect( p_vout->p_sys->hwnd, &rect_window );
1055
1056         *pi_width  = rect_window.right - rect_window.left;
1057         *pi_height = rect_window.bottom - rect_window.top;
1058         return VLC_SUCCESS;
1059
1060     case VOUT_SET_SIZE:
1061         if( p_vout->p_sys->parent_window )
1062             return vaControlParentWindow( p_vout, i_query, args );
1063
1064         /* Update dimensions */
1065         rect_window.top = rect_window.left = 0;
1066         rect_window.right  = va_arg( args, unsigned int );
1067         rect_window.bottom = va_arg( args, unsigned int );
1068         if( !rect_window.right ) rect_window.right = p_vout->i_window_width;
1069         if( !rect_window.bottom ) rect_window.bottom = p_vout->i_window_height;
1070         AdjustWindowRect( &rect_window, p_vout->p_sys->i_window_style, 0 );
1071
1072         SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
1073                       rect_window.right - rect_window.left,
1074                       rect_window.bottom - rect_window.top, SWP_NOMOVE );
1075
1076         return VLC_SUCCESS;
1077
1078     case VOUT_CLOSE:
1079         ShowWindow( p_vout->p_sys->hwnd, SW_HIDE );
1080     case VOUT_REPARENT:
1081         /* Retrieve the window position */
1082         point.x = point.y = 0;
1083         ClientToScreen( p_vout->p_sys->hwnd, &point );
1084
1085         HWND d = 0;
1086
1087         if( i_query == VOUT_REPARENT ) d = (HWND)va_arg( args, int );
1088         if( !d )
1089         {
1090             vlc_mutex_lock( &p_vout->p_sys->lock );
1091             p_vout->p_sys->hparent = 0;
1092             vlc_mutex_unlock( &p_vout->p_sys->lock );
1093             SetParent( p_vout->p_sys->hwnd, 0 );
1094             p_vout->p_sys->i_window_style =
1095                 WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW | WS_SIZEBOX;
1096             SetWindowLong( p_vout->p_sys->hwnd, GWL_STYLE,
1097                            p_vout->p_sys->i_window_style |
1098                            (i_query == VOUT_CLOSE ? 0 : WS_VISIBLE) );
1099             SetWindowLong( p_vout->p_sys->hwnd, GWL_EXSTYLE, WS_EX_APPWINDOW );
1100             SetWindowPos( p_vout->p_sys->hwnd, 0, point.x, point.y, 0, 0,
1101                           SWP_NOSIZE|SWP_NOZORDER|SWP_FRAMECHANGED );
1102         }
1103         else
1104         {
1105             vlc_mutex_lock( &p_vout->p_sys->lock );
1106             p_vout->p_sys->hparent = d;
1107             vlc_mutex_unlock( &p_vout->p_sys->lock );
1108
1109             SetParent( p_vout->p_sys->hwnd, d );
1110             p_vout->p_sys->i_window_style = WS_CLIPCHILDREN;
1111             SetWindowLong( p_vout->p_sys->hwnd, GWL_STYLE,
1112                            p_vout->p_sys->i_window_style |
1113                            (i_query == VOUT_CLOSE ? 0 : WS_VISIBLE) );
1114             SetWindowLong( p_vout->p_sys->hwnd, GWL_EXSTYLE, WS_EX_APPWINDOW );
1115
1116             /* Retrieve the parent size */
1117             RECT  rect;
1118             GetClientRect( d, &rect );
1119             SetWindowPos( p_vout->p_sys->hwnd, d, point.x, point.y, rect.right, rect.bottom,
1120                           SWP_FRAMECHANGED );
1121         }
1122
1123         vout_ReleaseWindow( p_vout->p_sys->parent_window );
1124         return VLC_SUCCESS;
1125
1126     case VOUT_SET_STAY_ON_TOP:
1127         if( p_vout->p_sys->hparent && !var_GetBool( p_vout, "fullscreen" ) )
1128             return vaControlParentWindow( p_vout, i_query, args );
1129
1130         p_vout->p_sys->b_on_top_change = true;
1131         return VLC_SUCCESS;
1132
1133 #ifdef MODULE_NAME_IS_wingapi
1134     case VOUT_SET_FOCUS:
1135                 b_bool = (bool) va_arg( args, int );
1136         p_vout->p_sys->b_parent_focus = b_bool;
1137         if( b_bool ) GXResume();
1138         else if( !p_vout->p_sys->b_focus ) GXSuspend();
1139         return VLC_SUCCESS;
1140 #endif
1141
1142     default:
1143         return vout_vaControlDefault( p_vout, i_query, args );
1144     }
1145 }
1146
1147
1148 /* Internal wrapper over GetWindowPlacement */
1149 static WINDOWPLACEMENT getWindowState(HWND hwnd)
1150 {
1151     WINDOWPLACEMENT window_placement;
1152     window_placement.length = sizeof(WINDOWPLACEMENT);
1153     GetWindowPlacement( hwnd, &window_placement );
1154     return window_placement;
1155 }
1156
1157 /* Internal wrapper over SetWindowPlacement */
1158 static void SetWindowState(HWND hwnd, int nShowCmd,WINDOWPLACEMENT window_placement)
1159 {
1160     window_placement.showCmd = nShowCmd;
1161     SetWindowPlacement( hwnd, &window_placement );
1162     SetWindowPos( hwnd, 0, 0, 0, 0, 0,
1163         SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER|SWP_FRAMECHANGED);
1164 }
1165
1166 /* Internal wrapper to call vout_ControlWindow for hparent */
1167 static int vaControlParentWindow( vout_thread_t *p_vout, int i_query,
1168                                    va_list args )
1169 {
1170     return vout_ControlWindow( p_vout->p_sys->parent_window, i_query, args );
1171 }
1172
1173 static int ControlParentWindow( vout_thread_t *p_vout, int i_query, ... )
1174 {
1175     va_list args;
1176     int ret;
1177
1178     va_start( args, i_query );
1179     ret = vaControlParentWindow( p_vout, i_query, args );
1180     va_end( args );
1181     return ret;
1182 }
1183
1184 void Win32ToggleFullscreen( vout_thread_t *p_vout )
1185 {
1186     HWND hwnd = (p_vout->p_sys->hparent && p_vout->p_sys->hfswnd) ?
1187         p_vout->p_sys->hfswnd : p_vout->p_sys->hwnd;
1188
1189     /* Save the current windows placement/placement to restore
1190        when fullscreen is over */
1191     WINDOWPLACEMENT window_placement = getWindowState( hwnd );
1192
1193     p_vout->b_fullscreen = ! p_vout->b_fullscreen;
1194
1195     /* We want to go to Fullscreen */
1196     if( p_vout->b_fullscreen )
1197     {
1198         msg_Dbg( p_vout, "entering fullscreen mode" );
1199
1200         /* Change window style, no borders and no title bar */
1201         int i_style = WS_CLIPCHILDREN | WS_VISIBLE;
1202         SetWindowLong( hwnd, GWL_STYLE, i_style );
1203
1204         if( p_vout->p_sys->hparent )
1205         {
1206             /* Retrieve current window position so fullscreen will happen
1207             *on the right screen */
1208             HMONITOR hmon = MonitorFromWindow(p_vout->p_sys->hparent,
1209                                             MONITOR_DEFAULTTONEAREST);
1210             MONITORINFO mi = {sizeof(mi)};
1211             if (GetMonitorInfo(hmon, &mi))
1212             SetWindowPos( hwnd, 0,
1213                             mi.rcMonitor.left,
1214                             mi.rcMonitor.top,
1215                             mi.rcMonitor.right - mi.rcMonitor.left,
1216                             mi.rcMonitor.bottom - mi.rcMonitor.top,
1217                             SWP_NOZORDER|SWP_FRAMECHANGED );
1218         }
1219
1220         /* Maximize window */
1221         SetWindowState( hwnd, SW_SHOWMAXIMIZED, window_placement );
1222
1223         if( p_vout->p_sys->hparent )
1224         {
1225             /* Hide the previous window */
1226             RECT rect;
1227             GetClientRect( hwnd, &rect );
1228             SetParent( p_vout->p_sys->hwnd, hwnd );
1229             SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
1230                           rect.right, rect.bottom,
1231                           SWP_NOZORDER|SWP_FRAMECHANGED );
1232
1233 #ifdef UNDER_CE
1234             HWND topLevelParent = GetParent( p_vout->p_sys->hparent );
1235 #else
1236             HWND topLevelParent = GetAncestor( p_vout->p_sys->hparent, GA_ROOT );
1237 #endif
1238             ShowWindow( topLevelParent, SW_HIDE );
1239         }
1240
1241         SetForegroundWindow( hwnd );
1242     }
1243     else
1244     {
1245         msg_Dbg( p_vout, "leaving fullscreen mode" );
1246         /* Change window style, no borders and no title bar */
1247         SetWindowLong( hwnd, GWL_STYLE, p_vout->p_sys->i_window_style );
1248
1249         /* Normal window */
1250         SetWindowState( hwnd, SW_SHOWNORMAL, window_placement );
1251
1252         if( p_vout->p_sys->hparent )
1253         {
1254             RECT rect;
1255             GetClientRect( p_vout->p_sys->hparent, &rect );
1256             SetParent( p_vout->p_sys->hwnd, p_vout->p_sys->hparent );
1257             SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
1258                           rect.right, rect.bottom,
1259                           SWP_NOZORDER|SWP_FRAMECHANGED );
1260
1261 #ifdef UNDER_CE
1262             HWND topLevelParent = GetParent( p_vout->p_sys->hparent );
1263 #else
1264             HWND topLevelParent = GetAncestor( p_vout->p_sys->hparent, GA_ROOT );
1265 #endif
1266             ShowWindow( topLevelParent, SW_SHOW );
1267             SetForegroundWindow( p_vout->p_sys->hparent );
1268             ShowWindow( hwnd, SW_HIDE );
1269         }
1270
1271         /* Make sure the mouse cursor is displayed */
1272         PostMessage( p_vout->p_sys->hwnd, WM_VLC_SHOW_MOUSE, 0, 0 );
1273     }
1274
1275     vlc_value_t val;
1276     /* Update the object variable and trigger callback */
1277     val.b_bool = p_vout->b_fullscreen;
1278     var_Set( p_vout, "fullscreen", val );
1279 }