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