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