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