]> git.sesse.net Git - vlc/blob - modules/video_output/msw/events.c
Replaced a certain amount of vlc_object_find by pl_Yield
[vlc] / modules / video_output / msw / events.c
1 /*****************************************************************************
2  * events.c: Windows DirectX video output events handler
3  *****************************************************************************
4  * Copyright (C) 2001-2004 the VideoLAN team
5  * $Id$
6  *
7  * Authors: Gildas Bazin <gbazin@videolan.org>
8  *
9  * This program is free software; you can redistribute it and/or modify
10  * it under the terms of the GNU General Public License as published by
11  * the Free Software Foundation; either version 2 of the License, or
12  * (at your option) any later version.
13  *
14  * This program is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17  * GNU General Public License for more details.
18  *
19  * You should have received a copy of the GNU General Public License
20  * along with this program; if not, write to the Free Software
21  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
22  *****************************************************************************/
23
24
25 /*****************************************************************************
26  * Preamble: This file contains the functions related to the creation of
27  *             a window and the handling of its messages (events).
28  *****************************************************************************/
29 #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     playlist_t *p_playlist = pl_Yield( p_event );
81     vlc_value_t val;
82     val.b_bool = b_open;
83     var_Set( p_playlist, "intf-popupmenu", val );
84     vlc_object_release( p_playlist );
85 }
86
87 static int DirectXConvertKey( int i_key );
88
89 /*****************************************************************************
90  * EventThread: Create video window & handle its messages
91  *****************************************************************************
92  * This function creates a video window and then enters an infinite loop
93  * that handles the messages sent to that window.
94  * The main goal of this thread is to isolate the Win32 PeekMessage function
95  * because this one can block for a long time.
96  *****************************************************************************/
97 void EventThread( event_thread_t *p_event )
98 {
99     MSG msg;
100     POINT old_mouse_pos = {0,0}, mouse_pos;
101     vlc_value_t val;
102     int i_width, i_height, i_x, i_y;
103     HMODULE hkernel32;
104
105     /* Initialisation */
106     p_event->p_vout->pf_control = Control;
107
108     /* Create a window for the video */
109     /* Creating a window under Windows also initializes the thread's event
110      * message queue */
111     if( DirectXCreateWindow( p_event->p_vout ) )
112     {
113         msg_Err( p_event, "out of memory" );
114         p_event->b_dead = true;
115     }
116
117     /* Signal the creation of the window */
118     vlc_thread_ready( p_event );
119
120 #ifndef UNDER_CE
121     /* Set power management stuff */
122     if( (hkernel32 = GetModuleHandle( _T("KERNEL32") ) ) )
123     {
124         ULONG (WINAPI* OurSetThreadExecutionState)( ULONG );
125
126         OurSetThreadExecutionState = (ULONG (WINAPI*)( ULONG ))
127             GetProcAddress( hkernel32, _T("SetThreadExecutionState") );
128
129         if( OurSetThreadExecutionState )
130             /* Prevent monitor from powering off */
131             OurSetThreadExecutionState( ES_DISPLAY_REQUIRED | ES_CONTINUOUS );
132         else
133             msg_Dbg( p_event, "no support for SetThreadExecutionState()" );
134     }
135 #endif
136
137     /* Main loop */
138     /* GetMessage will sleep if there's no message in the queue */
139     while( !p_event->b_die && GetMessage( &msg, 0, 0, 0 ) )
140     {
141         /* Check if we are asked to exit */
142         if( p_event->b_die )
143             break;
144
145         switch( msg.message )
146         {
147
148         case WM_MOUSEMOVE:
149             vout_PlacePicture( p_event->p_vout,
150                                p_event->p_vout->p_sys->i_window_width,
151                                p_event->p_vout->p_sys->i_window_height,
152                                &i_x, &i_y, &i_width, &i_height );
153
154             if( msg.hwnd == p_event->p_vout->p_sys->hvideownd )
155             {
156                 /* Child window */
157                 i_x = i_y = 0;
158             }
159
160             if( i_width && i_height )
161             {
162                 val.i_int = ( GET_X_LPARAM(msg.lParam) - i_x ) *
163                     p_event->p_vout->fmt_in.i_visible_width / i_width +
164                     p_event->p_vout->fmt_in.i_x_offset;
165                 var_Set( p_event->p_vout, "mouse-x", val );
166                 val.i_int = ( GET_Y_LPARAM(msg.lParam) - i_y ) *
167                     p_event->p_vout->fmt_in.i_visible_height / i_height +
168                     p_event->p_vout->fmt_in.i_y_offset;
169                 var_Set( p_event->p_vout, "mouse-y", val );
170
171                 val.b_bool = true;
172                 var_Set( p_event->p_vout, "mouse-moved", val );
173             }
174
175         case WM_NCMOUSEMOVE:
176             GetCursorPos( &mouse_pos );
177             if( (abs(mouse_pos.x - old_mouse_pos.x) > 2 ||
178                 (abs(mouse_pos.y - old_mouse_pos.y)) > 2 ) )
179             {
180                 GetCursorPos( &old_mouse_pos );
181                 p_event->p_vout->p_sys->i_lastmoved = mdate();
182
183                 if( p_event->p_vout->p_sys->b_cursor_hidden )
184                 {
185                     p_event->p_vout->p_sys->b_cursor_hidden = 0;
186                     ShowCursor( TRUE );
187                 }
188             }
189             break;
190
191         case WM_VLC_HIDE_MOUSE:
192             if( p_event->p_vout->p_sys->b_cursor_hidden ) break;
193             p_event->p_vout->p_sys->b_cursor_hidden = true;
194             GetCursorPos( &old_mouse_pos );
195             ShowCursor( FALSE );
196             break;
197
198         case WM_VLC_SHOW_MOUSE:
199             if( !p_event->p_vout->p_sys->b_cursor_hidden ) break;
200             p_event->p_vout->p_sys->b_cursor_hidden = false;
201             GetCursorPos( &old_mouse_pos );
202             ShowCursor( TRUE );
203             break;
204
205         case WM_LBUTTONDOWN:
206             var_Get( p_event->p_vout, "mouse-button-down", &val );
207             val.i_int |= 1;
208             var_Set( p_event->p_vout, "mouse-button-down", val );
209             DirectXPopupMenu( p_event, false );
210             break;
211
212         case WM_LBUTTONUP:
213             var_Get( p_event->p_vout, "mouse-button-down", &val );
214             val.i_int &= ~1;
215             var_Set( p_event->p_vout, "mouse-button-down", val );
216
217             val.b_bool = true;
218             var_Set( p_event->p_vout, "mouse-clicked", val );
219             break;
220
221         case WM_LBUTTONDBLCLK:
222             p_event->p_vout->p_sys->i_changes |= VOUT_FULLSCREEN_CHANGE;
223             break;
224
225         case WM_MBUTTONDOWN:
226             var_Get( p_event->p_vout, "mouse-button-down", &val );
227             val.i_int |= 2;
228             var_Set( p_event->p_vout, "mouse-button-down", val );
229             DirectXPopupMenu( p_event, false );
230             break;
231
232         case WM_MBUTTONUP:
233             var_Get( p_event->p_vout, "mouse-button-down", &val );
234             val.i_int &= ~2;
235             var_Set( p_event->p_vout, "mouse-button-down", val );
236             break;
237
238         case WM_RBUTTONDOWN:
239             var_Get( p_event->p_vout, "mouse-button-down", &val );
240             val.i_int |= 4;
241             var_Set( p_event->p_vout, "mouse-button-down", val );
242             DirectXPopupMenu( p_event, false );
243             break;
244
245         case WM_RBUTTONUP:
246             var_Get( p_event->p_vout, "mouse-button-down", &val );
247             val.i_int &= ~4;
248             var_Set( p_event->p_vout, "mouse-button-down", val );
249             DirectXPopupMenu( p_event, true );
250             break;
251
252         case WM_KEYDOWN:
253         case WM_SYSKEYDOWN:
254             /* The key events are first processed here and not translated
255              * into WM_CHAR events because we need to know the status of the
256              * modifier keys. */
257             val.i_int = DirectXConvertKey( msg.wParam );
258             if( !val.i_int )
259             {
260                 /* This appears to be a "normal" (ascii) key */
261                 val.i_int = tolower( MapVirtualKey( msg.wParam, 2 ) );
262             }
263
264             if( val.i_int )
265             {
266                 if( GetKeyState(VK_CONTROL) & 0x8000 )
267                 {
268                     val.i_int |= KEY_MODIFIER_CTRL;
269                 }
270                 if( GetKeyState(VK_SHIFT) & 0x8000 )
271                 {
272                     val.i_int |= KEY_MODIFIER_SHIFT;
273                 }
274                 if( GetKeyState(VK_MENU) & 0x8000 )
275                 {
276                     val.i_int |= KEY_MODIFIER_ALT;
277                 }
278
279                 var_Set( p_event->p_libvlc, "key-pressed", val );
280             }
281             break;
282
283         case WM_MOUSEWHEEL:
284             if( GET_WHEEL_DELTA_WPARAM( msg.wParam ) > 0 )
285             {
286                 val.i_int = KEY_MOUSEWHEELUP;
287             }
288             else
289             {
290                 val.i_int = KEY_MOUSEWHEELDOWN;
291             }
292             if( val.i_int )
293             {
294                 if( GetKeyState(VK_CONTROL) & 0x8000 )
295                 {
296                     val.i_int |= KEY_MODIFIER_CTRL;
297                 }
298                 if( GetKeyState(VK_SHIFT) & 0x8000 )
299                 {
300                     val.i_int |= KEY_MODIFIER_SHIFT;
301                 }
302                 if( GetKeyState(VK_MENU) & 0x8000 )
303                 {
304                     val.i_int |= KEY_MODIFIER_ALT;
305                 }
306
307                 var_Set( p_event->p_libvlc, "key-pressed", val );
308             }
309             break;
310
311         case WM_VLC_CHANGE_TEXT:
312             var_Get( p_event->p_vout, "video-title", &val );
313             if( !val.psz_string || !*val.psz_string ) /* Default video title */
314             {
315                 free( val.psz_string );
316
317 #ifdef MODULE_NAME_IS_wingdi
318                 val.psz_string = strdup( VOUT_TITLE " (WinGDI output)" );
319 #endif
320 #ifdef MODULE_NAME_IS_wingapi
321                 val.psz_string = strdup( VOUT_TITLE " (WinGAPI output)" );
322 #endif
323 #ifdef MODULE_NAME_IS_glwin32
324                 val.psz_string = strdup( VOUT_TITLE " (OpenGL output)" );
325 #endif
326 #ifdef MODULE_NAME_IS_direct3d
327                 val.psz_string = strdup( VOUT_TITLE " (Direct3D output)" );
328 #endif
329 #ifdef MODULE_NAME_IS_vout_directx
330                 if( p_event->p_vout->p_sys->b_using_overlay ) val.psz_string =
331                     strdup( VOUT_TITLE " (hardware YUV overlay DirectX output)" );
332                 else if( p_event->p_vout->p_sys->b_hw_yuv ) val.psz_string =
333                     strdup( VOUT_TITLE " (hardware YUV DirectX output)" );
334                 else val.psz_string =
335                     strdup( VOUT_TITLE " (software RGB DirectX output)" );
336 #endif
337             }
338
339 #ifdef UNICODE
340             {
341                 wchar_t *psz_title = malloc( strlen(val.psz_string) * 2 + 2 );
342                 if( psz_title )
343                 {
344                     mbstowcs( psz_title, val.psz_string, strlen(val.psz_string)*2);
345                     psz_title[strlen(val.psz_string)] = 0;
346                     free( val.psz_string ); val.psz_string = (char *)psz_title;
347                 }
348             }
349 #endif
350
351             SetWindowText( p_event->p_vout->p_sys->hwnd,
352                            (LPCTSTR)val.psz_string );
353             if( p_event->p_vout->p_sys->hfswnd )
354                 SetWindowText( p_event->p_vout->p_sys->hfswnd,
355                                (LPCTSTR)val.psz_string );
356             free( val.psz_string );
357             break;
358
359         default:
360             /* Messages we don't handle directly are dispatched to the
361              * window procedure */
362             TranslateMessage(&msg);
363             DispatchMessage(&msg);
364             break;
365
366         } /* End Switch */
367
368     } /* End Main loop */
369
370     /* Check for WM_QUIT if we created the window */
371     if( !p_event->p_vout->p_sys->hparent && msg.message == WM_QUIT )
372     {
373         msg_Warn( p_event, "WM_QUIT... should not happen!!" );
374         p_event->p_vout->p_sys->hwnd = NULL; /* Window already destroyed */
375     }
376
377     msg_Dbg( p_event, "DirectXEventThread terminating" );
378
379     /* clear the changes formerly signaled */
380     p_event->p_vout->p_sys->i_changes = 0;
381
382     DirectXCloseWindow( p_event->p_vout );
383 }
384
385
386 /* following functions are local */
387
388 /*****************************************************************************
389  * DirectXCreateWindow: create a window for the video.
390  *****************************************************************************
391  * Before creating a direct draw surface, we need to create a window in which
392  * the video will be displayed. This window will also allow us to capture the
393  * events.
394  *****************************************************************************/
395 static int DirectXCreateWindow( vout_thread_t *p_vout )
396 {
397     HINSTANCE  hInstance;
398     HMENU      hMenu;
399     RECT       rect_window;
400     WNDCLASS   wc;                            /* window class components */
401     HICON      vlc_icon = NULL;
402     char       vlc_path[MAX_PATH+1];
403     int        i_style, i_stylex;
404
405     msg_Dbg( p_vout, "DirectXCreateWindow" );
406
407     /* Get this module's instance */
408     hInstance = GetModuleHandle(NULL);
409
410     /* If an external window was specified, we'll draw in it. */
411     p_vout->p_sys->hparent =
412         vout_RequestWindow( p_vout, &p_vout->p_sys->i_window_x,
413                             &p_vout->p_sys->i_window_y,
414                             &p_vout->p_sys->i_window_width,
415                             &p_vout->p_sys->i_window_height );
416
417     /* We create the window ourself, there is no previous window proc. */
418     p_vout->p_sys->pf_wndproc = NULL;
419
420     /* Get the Icon from the main app */
421     vlc_icon = NULL;
422 #ifndef UNDER_CE
423     if( GetModuleFileName( NULL, vlc_path, MAX_PATH ) )
424     {
425         vlc_icon = ExtractIcon( hInstance, vlc_path, 0 );
426     }
427 #endif
428
429     /* Fill in the window class structure */
430     wc.style         = CS_OWNDC|CS_DBLCLKS;          /* style: dbl click */
431     wc.lpfnWndProc   = (WNDPROC)DirectXEventProc;       /* event handler */
432     wc.cbClsExtra    = 0;                         /* no extra class data */
433     wc.cbWndExtra    = 0;                        /* no extra window data */
434     wc.hInstance     = hInstance;                            /* instance */
435     wc.hIcon         = vlc_icon;                /* load the vlc big icon */
436     wc.hCursor       = LoadCursor(NULL, IDC_ARROW);    /* default cursor */
437     wc.hbrBackground = GetStockObject(BLACK_BRUSH);  /* background color */
438     wc.lpszMenuName  = NULL;                                  /* no menu */
439     wc.lpszClassName = _T("VLC DirectX");         /* use a special class */
440
441     /* Register the window class */
442     if( !RegisterClass(&wc) )
443     {
444         WNDCLASS wndclass;
445
446         if( vlc_icon ) DestroyIcon( vlc_icon );
447
448         /* Check why it failed. If it's because one already exists
449          * then fine, otherwise return with an error. */
450         if( !GetClassInfo( hInstance, _T("VLC DirectX"), &wndclass ) )
451         {
452             msg_Err( p_vout, "DirectXCreateWindow RegisterClass FAILED (err=%lu)", GetLastError() );
453             return VLC_EGENERIC;
454         }
455     }
456
457     /* Register the video sub-window class */
458     wc.lpszClassName = _T("VLC DirectX video"); wc.hIcon = 0;
459     wc.hbrBackground = NULL; /* no background color */
460     if( !RegisterClass(&wc) )
461     {
462         WNDCLASS wndclass;
463
464         /* Check why it failed. If it's because one already exists
465          * then fine, otherwise return with an error. */
466         if( !GetClassInfo( hInstance, _T("VLC DirectX video"), &wndclass ) )
467         {
468             msg_Err( p_vout, "DirectXCreateWindow RegisterClass FAILED (err=%lu)", GetLastError() );
469             return VLC_EGENERIC;
470         }
471     }
472
473     /* When you create a window you give the dimensions you wish it to
474      * have. Unfortunatly these dimensions will include the borders and
475      * titlebar. We use the following function to find out the size of
476      * the window corresponding to the useable surface we want */
477     rect_window.top    = 10;
478     rect_window.left   = 10;
479     rect_window.right  = rect_window.left + p_vout->p_sys->i_window_width;
480     rect_window.bottom = rect_window.top + p_vout->p_sys->i_window_height;
481
482     if( var_GetBool( p_vout, "video-deco" ) )
483     {
484         /* Open with window decoration */
485         AdjustWindowRect( &rect_window, WS_OVERLAPPEDWINDOW|WS_SIZEBOX, 0 );
486         i_style = WS_OVERLAPPEDWINDOW|WS_SIZEBOX|WS_VISIBLE|WS_CLIPCHILDREN;
487         i_stylex = 0;
488     }
489     else
490     {
491         /* No window decoration */
492         AdjustWindowRect( &rect_window, WS_POPUP, 0 );
493         i_style = WS_POPUP|WS_VISIBLE|WS_CLIPCHILDREN;
494         i_stylex = 0; // WS_EX_TOOLWINDOW; Is TOOLWINDOW really needed ?
495                       // It messes up the fullscreen window.
496     }
497
498     if( p_vout->p_sys->hparent )
499     {
500         i_style = WS_VISIBLE|WS_CLIPCHILDREN|WS_CHILD;
501         i_stylex = 0;
502     }
503
504     p_vout->p_sys->i_window_style = i_style;
505
506     /* Create the window */
507     p_vout->p_sys->hwnd =
508         CreateWindowEx( WS_EX_NOPARENTNOTIFY | i_stylex,
509                     _T("VLC DirectX"),               /* name of window class */
510                     _T(VOUT_TITLE) _T(" (DirectX Output)"),  /* window title */
511                     i_style,                                 /* window style */
512                     (p_vout->p_sys->i_window_x < 0) ? CW_USEDEFAULT :
513                         (UINT)p_vout->p_sys->i_window_x,   /* default X coordinate */
514                     (p_vout->p_sys->i_window_y < 0) ? CW_USEDEFAULT :
515                         (UINT)p_vout->p_sys->i_window_y,   /* default Y coordinate */
516                     rect_window.right - rect_window.left,    /* window width */
517                     rect_window.bottom - rect_window.top,   /* window height */
518                     p_vout->p_sys->hparent,                 /* parent window */
519                     NULL,                          /* no menu in this window */
520                     hInstance,            /* handle of this program instance */
521                     (LPVOID)p_vout );            /* send p_vout to WM_CREATE */
522
523     if( !p_vout->p_sys->hwnd )
524     {
525         msg_Warn( p_vout, "DirectXCreateWindow create window FAILED (err=%lu)", GetLastError() );
526         return VLC_EGENERIC;
527     }
528
529     if( p_vout->p_sys->hparent )
530     {
531         LONG i_style;
532
533         /* We don't want the window owner to overwrite our client area */
534         i_style = GetWindowLong( p_vout->p_sys->hparent, GWL_STYLE );
535
536         if( !(i_style & WS_CLIPCHILDREN) )
537             /* Hmmm, apparently this is a blocking call... */
538             SetWindowLong( p_vout->p_sys->hparent, GWL_STYLE,
539                            i_style | WS_CLIPCHILDREN );
540
541         /* Create our fullscreen window */
542         p_vout->p_sys->hfswnd =
543             CreateWindowEx( WS_EX_APPWINDOW, _T("VLC DirectX"),
544                             _T(VOUT_TITLE) _T(" (DirectX Output)"),
545                             WS_OVERLAPPEDWINDOW|WS_CLIPCHILDREN|WS_SIZEBOX,
546                             CW_USEDEFAULT, CW_USEDEFAULT,
547                             CW_USEDEFAULT, CW_USEDEFAULT,
548                             NULL, NULL, hInstance, NULL );
549     }
550
551     /* Append a "Always On Top" entry in the system menu */
552     hMenu = GetSystemMenu( p_vout->p_sys->hwnd, FALSE );
553     AppendMenu( hMenu, MF_SEPARATOR, 0, _T("") );
554     AppendMenu( hMenu, MF_STRING | MF_UNCHECKED,
555                        IDM_TOGGLE_ON_TOP, _T("Always on &Top") );
556
557     /* Create video sub-window. This sub window will always exactly match
558      * the size of the video, which allows us to use crazy overlay colorkeys
559      * without having them shown outside of the video area. */
560     p_vout->p_sys->hvideownd =
561     CreateWindow( _T("VLC DirectX video"), _T(""),   /* window class */
562         WS_CHILD,                   /* window style, not visible initially */
563         0, 0,
564         p_vout->render.i_width,         /* default width */
565         p_vout->render.i_height,        /* default height */
566         p_vout->p_sys->hwnd,                    /* parent window */
567         NULL, hInstance,
568         (LPVOID)p_vout );            /* send p_vout to WM_CREATE */
569
570     if( !p_vout->p_sys->hvideownd )
571         msg_Warn( p_vout, "can't create video sub-window" );
572     else
573         msg_Dbg( p_vout, "created video sub-window" );
574
575     /* Now display the window */
576     ShowWindow( p_vout->p_sys->hwnd, SW_SHOW );
577
578     return VLC_SUCCESS;
579 }
580
581 /*****************************************************************************
582  * DirectXCloseWindow: close the window created by DirectXCreateWindow
583  *****************************************************************************
584  * This function returns all resources allocated by DirectXCreateWindow.
585  *****************************************************************************/
586 static void DirectXCloseWindow( vout_thread_t *p_vout )
587 {
588     msg_Dbg( p_vout, "DirectXCloseWindow" );
589
590     DestroyWindow( p_vout->p_sys->hwnd );
591     if( p_vout->p_sys->hfswnd ) DestroyWindow( p_vout->p_sys->hfswnd );
592
593     if( p_vout->p_sys->hparent )
594         vout_ReleaseWindow( p_vout, (void *)p_vout->p_sys->hparent );
595
596     p_vout->p_sys->hwnd = NULL;
597
598     /* We don't unregister the Window Class because it could lead to race
599      * conditions and it will be done anyway by the system when the app will
600      * exit */
601 }
602
603 /*****************************************************************************
604  * UpdateRects: update clipping rectangles
605  *****************************************************************************
606  * This function is called when the window position or size are changed, and
607  * its job is to update the source and destination RECTs used to display the
608  * picture.
609  *****************************************************************************/
610 void UpdateRects( vout_thread_t *p_vout, bool b_force )
611 {
612 #define rect_src p_vout->p_sys->rect_src
613 #define rect_src_clipped p_vout->p_sys->rect_src_clipped
614 #define rect_dest p_vout->p_sys->rect_dest
615 #define rect_dest_clipped p_vout->p_sys->rect_dest_clipped
616
617     int i_width, i_height, i_x, i_y;
618
619     RECT  rect;
620     POINT point;
621
622     /* Retrieve the window size */
623     GetClientRect( p_vout->p_sys->hwnd, &rect );
624
625     /* Retrieve the window position */
626     point.x = point.y = 0;
627     ClientToScreen( p_vout->p_sys->hwnd, &point );
628
629     /* If nothing changed, we can return */
630     if( !b_force
631          && p_vout->p_sys->i_window_width == rect.right
632          && p_vout->p_sys->i_window_height == rect.bottom
633          && p_vout->p_sys->i_window_x == point.x
634          && p_vout->p_sys->i_window_y == point.y )
635     {
636         return;
637     }
638
639     /* Update the window position and size */
640     p_vout->p_sys->i_window_x = point.x;
641     p_vout->p_sys->i_window_y = point.y;
642     p_vout->p_sys->i_window_width = rect.right;
643     p_vout->p_sys->i_window_height = rect.bottom;
644
645     vout_PlacePicture( p_vout, rect.right, rect.bottom,
646                        &i_x, &i_y, &i_width, &i_height );
647
648     if( p_vout->p_sys->hvideownd )
649         SetWindowPos( p_vout->p_sys->hvideownd, 0,
650                       i_x, i_y, i_width, i_height,
651                       SWP_NOCOPYBITS|SWP_NOZORDER|SWP_ASYNCWINDOWPOS );
652
653     /* Destination image position and dimensions */
654     rect_dest.left = point.x + i_x;
655     rect_dest.right = rect_dest.left + i_width;
656     rect_dest.top = point.y + i_y;
657     rect_dest.bottom = rect_dest.top + i_height;
658
659 #ifdef MODULE_NAME_IS_vout_directx
660     /* Apply overlay hardware constraints */
661     if( p_vout->p_sys->b_using_overlay )
662     {
663         if( p_vout->p_sys->i_align_dest_boundary )
664             rect_dest.left = ( rect_dest.left +
665                 p_vout->p_sys->i_align_dest_boundary / 2 ) &
666                 ~p_vout->p_sys->i_align_dest_boundary;
667
668         if( p_vout->p_sys->i_align_dest_size )
669             rect_dest.right = (( rect_dest.right - rect_dest.left +
670                 p_vout->p_sys->i_align_dest_size / 2 ) &
671                 ~p_vout->p_sys->i_align_dest_size) + rect_dest.left;
672     }
673
674     /* UpdateOverlay directdraw function doesn't automatically clip to the
675      * display size so we need to do it otherwise it will fail */
676
677     /* Clip the destination window */
678     if( !IntersectRect( &rect_dest_clipped, &rect_dest,
679                         &p_vout->p_sys->rect_display ) )
680     {
681         SetRectEmpty( &rect_src_clipped );
682         return;
683     }
684
685 #if 0
686     msg_Dbg( p_vout, "DirectXUpdateRects image_dst_clipped coords:"
687                      " %i,%i,%i,%i",
688                      rect_dest_clipped.left, rect_dest_clipped.top,
689                      rect_dest_clipped.right, rect_dest_clipped.bottom );
690 #endif
691
692 #else /* MODULE_NAME_IS_vout_directx */
693
694     /* AFAIK, there are no clipping constraints in Direct3D, OpenGL and GDI */
695     rect_dest_clipped = rect_dest;
696
697 #endif
698
699     /* the 2 following lines are to fix a bug when clicking on the desktop */
700     if( (rect_dest_clipped.right - rect_dest_clipped.left)==0 ||
701         (rect_dest_clipped.bottom - rect_dest_clipped.top)==0 )
702     {
703         SetRectEmpty( &rect_src_clipped );
704         return;
705     }
706
707     /* src image dimensions */
708     rect_src.left = 0;
709     rect_src.top = 0;
710     rect_src.right = p_vout->render.i_width;
711     rect_src.bottom = p_vout->render.i_height;
712
713     /* Clip the source image */
714     rect_src_clipped.left = p_vout->fmt_out.i_x_offset +
715       (rect_dest_clipped.left - rect_dest.left) *
716       p_vout->fmt_out.i_visible_width / (rect_dest.right - rect_dest.left);
717     rect_src_clipped.right = p_vout->fmt_out.i_x_offset +
718       p_vout->fmt_out.i_visible_width -
719       (rect_dest.right - rect_dest_clipped.right) *
720       p_vout->fmt_out.i_visible_width / (rect_dest.right - rect_dest.left);
721     rect_src_clipped.top = p_vout->fmt_out.i_y_offset +
722       (rect_dest_clipped.top - rect_dest.top) *
723       p_vout->fmt_out.i_visible_height / (rect_dest.bottom - rect_dest.top);
724     rect_src_clipped.bottom = p_vout->fmt_out.i_y_offset +
725       p_vout->fmt_out.i_visible_height -
726       (rect_dest.bottom - rect_dest_clipped.bottom) *
727       p_vout->fmt_out.i_visible_height / (rect_dest.bottom - rect_dest.top);
728
729 #ifdef MODULE_NAME_IS_vout_directx
730     /* Apply overlay hardware constraints */
731     if( p_vout->p_sys->b_using_overlay )
732     {
733         if( p_vout->p_sys->i_align_src_boundary )
734             rect_src_clipped.left = ( rect_src_clipped.left +
735                 p_vout->p_sys->i_align_src_boundary / 2 ) &
736                 ~p_vout->p_sys->i_align_src_boundary;
737
738         if( p_vout->p_sys->i_align_src_size )
739             rect_src_clipped.right = (( rect_src_clipped.right -
740                 rect_src_clipped.left +
741                 p_vout->p_sys->i_align_src_size / 2 ) &
742                 ~p_vout->p_sys->i_align_src_size) + rect_src_clipped.left;
743     }
744 #endif
745
746 #if 0
747     msg_Dbg( p_vout, "DirectXUpdateRects image_src_clipped"
748                      " coords: %i,%i,%i,%i",
749                      rect_src_clipped.left, rect_src_clipped.top,
750                      rect_src_clipped.right, rect_src_clipped.bottom );
751 #endif
752
753 #ifdef MODULE_NAME_IS_vout_directx
754     /* The destination coordinates need to be relative to the current
755      * directdraw primary surface (display) */
756     rect_dest_clipped.left -= p_vout->p_sys->rect_display.left;
757     rect_dest_clipped.right -= p_vout->p_sys->rect_display.left;
758     rect_dest_clipped.top -= p_vout->p_sys->rect_display.top;
759     rect_dest_clipped.bottom -= p_vout->p_sys->rect_display.top;
760
761     if( p_vout->p_sys->b_using_overlay )
762         DirectDrawUpdateOverlay( p_vout );
763 #endif
764
765     /* Signal the change in size/position */
766     p_vout->p_sys->i_changes |= DX_POSITION_CHANGE;
767
768 #undef rect_src
769 #undef rect_src_clipped
770 #undef rect_dest
771 #undef rect_dest_clipped
772 }
773
774 /*****************************************************************************
775  * DirectXEventProc: This is the window event processing function.
776  *****************************************************************************
777  * On Windows, when you create a window you have to attach an event processing
778  * function to it. The aim of this function is to manage "Queued Messages" and
779  * "Nonqueued Messages".
780  * Queued Messages are those picked up and retransmitted by vout_Manage
781  * (using the GetMessage and DispatchMessage functions).
782  * Nonqueued Messages are those that Windows will send directly to this
783  * procedure (like WM_DESTROY, WM_WINDOWPOSCHANGED...)
784  *****************************************************************************/
785 static long FAR PASCAL DirectXEventProc( HWND hwnd, UINT message,
786                                          WPARAM wParam, LPARAM lParam )
787 {
788     vout_thread_t *p_vout;
789
790     if( message == WM_CREATE )
791     {
792         /* Store p_vout for future use */
793         p_vout = (vout_thread_t *)((CREATESTRUCT *)lParam)->lpCreateParams;
794         SetWindowLongPtr( hwnd, GWLP_USERDATA, (LONG_PTR)p_vout );
795         return TRUE;
796     }
797     else
798     {
799         p_vout = (vout_thread_t *)GetWindowLongPtr( hwnd, GWLP_USERDATA );
800         if( !p_vout )
801         {
802             /* Hmmm mozilla does manage somehow to save the pointer to our
803              * windowproc and still calls it after the vout has been closed. */
804             return DefWindowProc(hwnd, message, wParam, lParam);
805         }
806     }
807
808 #ifndef UNDER_CE
809     /* Catch the screensaver and the monitor turn-off */
810     if( message == WM_SYSCOMMAND &&
811         ( (wParam & 0xFFF0) == SC_SCREENSAVE || (wParam & 0xFFF0) == SC_MONITORPOWER ) )
812     {
813         //if( p_vout ) msg_Dbg( p_vout, "WinProc WM_SYSCOMMAND screensaver" );
814         return 0; /* this stops them from happening */
815     }
816 #endif
817
818     if( hwnd == p_vout->p_sys->hvideownd )
819     {
820         switch( message )
821         {
822 #ifdef MODULE_NAME_IS_vout_directx
823         case WM_ERASEBKGND:
824         /* For overlay, we need to erase background */
825             return !p_vout->p_sys->b_using_overlay ?
826                 1 : DefWindowProc(hwnd, message, wParam, lParam);
827         case WM_PAINT:
828         /*
829         ** For overlay, DefWindowProc() will erase dirty regions
830         ** with colorkey.
831         ** For non-overlay, vout will paint the whole window at
832         ** regular interval, therefore dirty regions can be ignored
833         ** to minimize repaint.
834         */
835             if( !p_vout->p_sys->b_using_overlay )
836             {
837                 ValidateRect(hwnd, NULL);
838             }
839             // fall through to default
840 #else
841         /*
842         ** For OpenGL and Direct3D, vout will update the whole
843         ** window at regular interval, therefore dirty region
844         ** can be ignored to minimize repaint.
845         */
846         case WM_ERASEBKGND:
847             /* nothing to erase */
848             return 1;
849         case WM_PAINT:
850             /* nothing to repaint */
851             ValidateRect(hwnd, NULL);
852             // fall through
853 #endif
854         default:
855             return DefWindowProc(hwnd, message, wParam, lParam);
856         }
857     }
858
859     switch( message )
860     {
861
862     case WM_WINDOWPOSCHANGED:
863         UpdateRects( p_vout, true );
864         return 0;
865
866     /* the user wants to close the window */
867     case WM_CLOSE:
868     {
869         playlist_t * p_playlist = pl_Yield( p_vout );
870         playlist_Stop( p_playlist );
871         vlc_object_release( p_playlist );
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 }