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