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