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