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