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