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