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