Add a desktop mode to the Direct3d video output. This allow displaying video on a...
[vlc.git] / 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_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 #ifdef UNICODE
347             {
348                 wchar_t *psz_title = malloc( strlen(val.psz_string) * 2 + 2 );
349                 if( psz_title )
350                 {
351                     mbstowcs( psz_title, val.psz_string, strlen(val.psz_string)*2);
352                     psz_title[strlen(val.psz_string)] = 0;
353                     free( val.psz_string ); val.psz_string = (char *)psz_title;
354                 }
355             }
356 #endif
357
358             SetWindowText( p_event->p_vout->p_sys->hwnd,
359                            (LPCTSTR)val.psz_string );
360             if( p_event->p_vout->p_sys->hfswnd )
361                 SetWindowText( p_event->p_vout->p_sys->hfswnd,
362                                (LPCTSTR)val.psz_string );
363             free( val.psz_string );
364             break;
365
366         default:
367             /* Messages we don't handle directly are dispatched to the
368              * window procedure */
369             TranslateMessage(&msg);
370             DispatchMessage(&msg);
371             break;
372
373         } /* End Switch */
374
375     } /* End Main loop */
376
377     /* Check for WM_QUIT if we created the window */
378     if( !p_event->p_vout->p_sys->hparent && msg.message == WM_QUIT )
379     {
380         msg_Warn( p_event, "WM_QUIT... should not happen!!" );
381         p_event->p_vout->p_sys->hwnd = NULL; /* Window already destroyed */
382     }
383
384     msg_Dbg( p_event, "DirectXEventThread terminating" );
385
386     DirectXCloseWindow( p_event->p_vout );
387     vlc_restorecancel (canc);
388     
389     /* clear the changes formerly signaled */
390     p_event->p_vout->p_sys->i_changes = EVENT_THREAD_ENDED;
391     return NULL;
392 }
393
394
395 /* following functions are local */
396
397 /*****************************************************************************
398  * DirectXCreateWindow: create a window for the video.
399  *****************************************************************************
400  * Before creating a direct draw surface, we need to create a window in which
401  * the video will be displayed. This window will also allow us to capture the
402  * events.
403  *****************************************************************************/
404 static int DirectXCreateWindow( vout_thread_t *p_vout )
405 {
406     HINSTANCE  hInstance;
407     HMENU      hMenu;
408     RECT       rect_window;
409     WNDCLASS   wc;                            /* window class components */
410     HICON      vlc_icon = NULL;
411     char       vlc_path[MAX_PATH+1];
412     int        i_style, i_stylex;
413
414     msg_Dbg( p_vout, "DirectXCreateWindow" );
415
416     /* Get this module's instance */
417     hInstance = GetModuleHandle(NULL);
418
419     #ifdef MODULE_NAME_IS_direct3d
420     if( !p_vout->p_sys->b_desktop )
421     {
422     #endif
423         /* If an external window was specified, we'll draw in it. */
424         p_vout->p_sys->parent_window =
425             vout_RequestHWND( p_vout, &p_vout->p_sys->i_window_x,
426                             &p_vout->p_sys->i_window_y,
427                             &p_vout->p_sys->i_window_width,
428                             &p_vout->p_sys->i_window_height );
429         if( p_vout->p_sys->parent_window )
430             p_vout->p_sys->hparent = p_vout->p_sys->parent_window->handle.hwnd;
431     #ifdef MODULE_NAME_IS_direct3d
432     }
433     else
434     {
435         /* Find Program Manager */
436         HWND hwnd = FindWindow( _T("Progman"), NULL );
437         if( hwnd ) hwnd = FindWindowEx( hwnd, NULL, _T("SHELLDLL_DefView"), NULL );
438         if( hwnd ) hwnd = FindWindowEx( hwnd, NULL, _T("SysListView32"), NULL );
439         if( !hwnd )
440             msg_Err( p_vout, "Couldn't find desktop icon window. Desktop mode can't be established." );
441         p_vout->p_sys->hparent = hwnd;
442     }
443     #endif
444
445     /* We create the window ourself, there is no previous window proc. */
446     p_vout->p_sys->pf_wndproc = NULL;
447
448     /* Get the Icon from the main app */
449     vlc_icon = NULL;
450 #ifndef UNDER_CE
451     if( GetModuleFileName( NULL, vlc_path, MAX_PATH ) )
452     {
453         vlc_icon = ExtractIcon( hInstance, vlc_path, 0 );
454     }
455 #endif
456
457     /* Fill in the window class structure */
458     wc.style         = CS_OWNDC|CS_DBLCLKS;          /* style: dbl click */
459     wc.lpfnWndProc   = (WNDPROC)DirectXEventProc;       /* event handler */
460     wc.cbClsExtra    = 0;                         /* no extra class data */
461     wc.cbWndExtra    = 0;                        /* no extra window data */
462     wc.hInstance     = hInstance;                            /* instance */
463     wc.hIcon         = vlc_icon;                /* load the vlc big icon */
464     wc.hCursor       = LoadCursor(NULL, IDC_ARROW);    /* default cursor */
465     wc.hbrBackground = GetStockObject(BLACK_BRUSH);  /* background color */
466     wc.lpszMenuName  = NULL;                                  /* no menu */
467     wc.lpszClassName = _T("VLC DirectX");         /* use a special class */
468
469     /* Register the window class */
470     if( !RegisterClass(&wc) )
471     {
472         WNDCLASS wndclass;
473
474         if( vlc_icon ) DestroyIcon( vlc_icon );
475
476         /* Check why it failed. If it's because one already exists
477          * then fine, otherwise return with an error. */
478         if( !GetClassInfo( hInstance, _T("VLC DirectX"), &wndclass ) )
479         {
480             msg_Err( p_vout, "DirectXCreateWindow RegisterClass FAILED (err=%lu)", GetLastError() );
481             return VLC_EGENERIC;
482         }
483     }
484
485     /* Register the video sub-window class */
486     wc.lpszClassName = _T("VLC DirectX video"); wc.hIcon = 0;
487     wc.hbrBackground = NULL; /* no background color */
488     if( !RegisterClass(&wc) )
489     {
490         WNDCLASS wndclass;
491
492         /* Check why it failed. If it's because one already exists
493          * then fine, otherwise return with an error. */
494         if( !GetClassInfo( hInstance, _T("VLC DirectX video"), &wndclass ) )
495         {
496             msg_Err( p_vout, "DirectXCreateWindow RegisterClass FAILED (err=%lu)", GetLastError() );
497             return VLC_EGENERIC;
498         }
499     }
500
501     /* When you create a window you give the dimensions you wish it to
502      * have. Unfortunatly these dimensions will include the borders and
503      * titlebar. We use the following function to find out the size of
504      * the window corresponding to the useable surface we want */
505     rect_window.top    = 10;
506     rect_window.left   = 10;
507     rect_window.right  = rect_window.left + p_vout->p_sys->i_window_width;
508     rect_window.bottom = rect_window.top + p_vout->p_sys->i_window_height;
509
510     if( var_GetBool( p_vout, "video-deco" ) )
511     {
512         /* Open with window decoration */
513         AdjustWindowRect( &rect_window, WS_OVERLAPPEDWINDOW|WS_SIZEBOX, 0 );
514         i_style = WS_OVERLAPPEDWINDOW|WS_SIZEBOX|WS_VISIBLE|WS_CLIPCHILDREN;
515         i_stylex = 0;
516     }
517     else
518     {
519         /* No window decoration */
520         AdjustWindowRect( &rect_window, WS_POPUP, 0 );
521         i_style = WS_POPUP|WS_VISIBLE|WS_CLIPCHILDREN;
522         i_stylex = 0; // WS_EX_TOOLWINDOW; Is TOOLWINDOW really needed ?
523                       // It messes up the fullscreen window.
524     }
525
526     if( p_vout->p_sys->hparent )
527     {
528         i_style = WS_VISIBLE|WS_CLIPCHILDREN|WS_CHILD;
529         i_stylex = 0;
530     }
531
532     p_vout->p_sys->i_window_style = i_style;
533
534     /* Create the window */
535     p_vout->p_sys->hwnd =
536         CreateWindowEx( WS_EX_NOPARENTNOTIFY | i_stylex,
537                     _T("VLC DirectX"),               /* name of window class */
538                     _T(VOUT_TITLE) _T(" (DirectX Output)"),  /* window title */
539                     i_style,                                 /* window style */
540                     (p_vout->p_sys->i_window_x < 0) ? CW_USEDEFAULT :
541                         (UINT)p_vout->p_sys->i_window_x,   /* default X coordinate */
542                     (p_vout->p_sys->i_window_y < 0) ? CW_USEDEFAULT :
543                         (UINT)p_vout->p_sys->i_window_y,   /* default Y coordinate */
544                     rect_window.right - rect_window.left,    /* window width */
545                     rect_window.bottom - rect_window.top,   /* window height */
546                     p_vout->p_sys->hparent,                 /* parent window */
547                     NULL,                          /* no menu in this window */
548                     hInstance,            /* handle of this program instance */
549                     (LPVOID)p_vout );            /* send p_vout to WM_CREATE */
550
551     if( !p_vout->p_sys->hwnd )
552     {
553         msg_Warn( p_vout, "DirectXCreateWindow create window FAILED (err=%lu)", GetLastError() );
554         return VLC_EGENERIC;
555     }
556
557     if( p_vout->p_sys->hparent )
558     {
559         LONG i_style;
560
561         /* We don't want the window owner to overwrite our client area */
562         i_style = GetWindowLong( p_vout->p_sys->hparent, GWL_STYLE );
563
564         if( !(i_style & WS_CLIPCHILDREN) )
565             /* Hmmm, apparently this is a blocking call... */
566             SetWindowLong( p_vout->p_sys->hparent, GWL_STYLE,
567                            i_style | WS_CLIPCHILDREN );
568
569         /* Create our fullscreen window */
570         p_vout->p_sys->hfswnd =
571             CreateWindowEx( WS_EX_APPWINDOW, _T("VLC DirectX"),
572                             _T(VOUT_TITLE) _T(" (DirectX Output)"),
573                             WS_OVERLAPPEDWINDOW|WS_CLIPCHILDREN|WS_SIZEBOX,
574                             CW_USEDEFAULT, CW_USEDEFAULT,
575                             CW_USEDEFAULT, CW_USEDEFAULT,
576                             NULL, NULL, hInstance, NULL );
577     }
578
579     /* Append a "Always On Top" entry in the system menu */
580     hMenu = GetSystemMenu( p_vout->p_sys->hwnd, FALSE );
581     AppendMenu( hMenu, MF_SEPARATOR, 0, _T("") );
582     AppendMenu( hMenu, MF_STRING | MF_UNCHECKED,
583                        IDM_TOGGLE_ON_TOP, _T("Always on &Top") );
584
585     /* Create video sub-window. This sub window will always exactly match
586      * the size of the video, which allows us to use crazy overlay colorkeys
587      * without having them shown outside of the video area. */
588     p_vout->p_sys->hvideownd =
589     CreateWindow( _T("VLC DirectX video"), _T(""),   /* window class */
590         WS_CHILD,                   /* window style, not visible initially */
591         0, 0,
592         p_vout->render.i_width,         /* default width */
593         p_vout->render.i_height,        /* default height */
594         p_vout->p_sys->hwnd,                    /* parent window */
595         NULL, hInstance,
596         (LPVOID)p_vout );            /* send p_vout to WM_CREATE */
597
598     if( !p_vout->p_sys->hvideownd )
599         msg_Warn( p_vout, "can't create video sub-window" );
600     else
601         msg_Dbg( p_vout, "created video sub-window" );
602
603     /* Now display the window */
604     ShowWindow( p_vout->p_sys->hwnd, SW_SHOW );
605
606     return VLC_SUCCESS;
607 }
608
609 /*****************************************************************************
610  * DirectXCloseWindow: close the window created by DirectXCreateWindow
611  *****************************************************************************
612  * This function returns all resources allocated by DirectXCreateWindow.
613  *****************************************************************************/
614 static void DirectXCloseWindow( vout_thread_t *p_vout )
615 {
616     msg_Dbg( p_vout, "DirectXCloseWindow" );
617
618     DestroyWindow( p_vout->p_sys->hwnd );
619     if( p_vout->p_sys->hfswnd ) DestroyWindow( p_vout->p_sys->hfswnd );
620
621     #ifdef MODULE_NAME_IS_direct3d
622     if( !p_vout->p_sys->b_desktop )
623     #endif
624         vout_ReleaseWindow( p_vout->p_sys->parent_window );
625     p_vout->p_sys->hwnd = NULL;
626
627     /* We don't unregister the Window Class because it could lead to race
628      * conditions and it will be done anyway by the system when the app will
629      * exit */
630 }
631
632 /*****************************************************************************
633  * UpdateRects: update clipping rectangles
634  *****************************************************************************
635  * This function is called when the window position or size are changed, and
636  * its job is to update the source and destination RECTs used to display the
637  * picture.
638  *****************************************************************************/
639 void UpdateRects( vout_thread_t *p_vout, bool b_force )
640 {
641 #define rect_src p_vout->p_sys->rect_src
642 #define rect_src_clipped p_vout->p_sys->rect_src_clipped
643 #define rect_dest p_vout->p_sys->rect_dest
644 #define rect_dest_clipped p_vout->p_sys->rect_dest_clipped
645
646     int i_width, i_height, i_x, i_y;
647
648     RECT  rect;
649     POINT point;
650
651     /* Retrieve the window size */
652     GetClientRect( p_vout->p_sys->hwnd, &rect );
653
654     /* Retrieve the window position */
655     point.x = point.y = 0;
656     ClientToScreen( p_vout->p_sys->hwnd, &point );
657
658     /* If nothing changed, we can return */
659     if( !b_force
660          && p_vout->p_sys->i_window_width == rect.right
661          && p_vout->p_sys->i_window_height == rect.bottom
662          && p_vout->p_sys->i_window_x == point.x
663          && p_vout->p_sys->i_window_y == point.y )
664     {
665         return;
666     }
667
668     /* Update the window position and size */
669     p_vout->p_sys->i_window_x = point.x;
670     p_vout->p_sys->i_window_y = point.y;
671     p_vout->p_sys->i_window_width = rect.right;
672     p_vout->p_sys->i_window_height = rect.bottom;
673
674     vout_PlacePicture( p_vout, rect.right, rect.bottom,
675                        &i_x, &i_y, &i_width, &i_height );
676
677     if( p_vout->p_sys->hvideownd )
678         SetWindowPos( p_vout->p_sys->hvideownd, 0,
679                       i_x, i_y, i_width, i_height,
680                       SWP_NOCOPYBITS|SWP_NOZORDER|SWP_ASYNCWINDOWPOS );
681
682     /* Destination image position and dimensions */
683     rect_dest.left = point.x + i_x;
684     rect_dest.right = rect_dest.left + i_width;
685     rect_dest.top = point.y + i_y;
686     rect_dest.bottom = rect_dest.top + i_height;
687
688 #ifdef MODULE_NAME_IS_directx
689     /* Apply overlay hardware constraints */
690     if( p_vout->p_sys->b_using_overlay )
691     {
692         if( p_vout->p_sys->i_align_dest_boundary )
693             rect_dest.left = ( rect_dest.left +
694                 p_vout->p_sys->i_align_dest_boundary / 2 ) &
695                 ~p_vout->p_sys->i_align_dest_boundary;
696
697         if( p_vout->p_sys->i_align_dest_size )
698             rect_dest.right = (( rect_dest.right - rect_dest.left +
699                 p_vout->p_sys->i_align_dest_size / 2 ) &
700                 ~p_vout->p_sys->i_align_dest_size) + rect_dest.left;
701     }
702
703     /* UpdateOverlay directdraw function doesn't automatically clip to the
704      * display size so we need to do it otherwise it will fail */
705
706     /* Clip the destination window */
707     if( !IntersectRect( &rect_dest_clipped, &rect_dest,
708                         &p_vout->p_sys->rect_display ) )
709     {
710         SetRectEmpty( &rect_src_clipped );
711         return;
712     }
713
714 #ifndef NDEBUG
715     msg_Dbg( p_vout, "DirectXUpdateRects image_dst_clipped coords:"
716                      " %i,%i,%i,%i",
717                      rect_dest_clipped.left, rect_dest_clipped.top,
718                      rect_dest_clipped.right, rect_dest_clipped.bottom );
719 #endif
720
721 #else /* MODULE_NAME_IS_directx */
722
723     /* AFAIK, there are no clipping constraints in Direct3D, OpenGL and GDI */
724     rect_dest_clipped = rect_dest;
725
726 #endif
727
728     /* the 2 following lines are to fix a bug when clicking on the desktop */
729     if( (rect_dest_clipped.right - rect_dest_clipped.left)==0 ||
730         (rect_dest_clipped.bottom - rect_dest_clipped.top)==0 )
731     {
732         SetRectEmpty( &rect_src_clipped );
733         return;
734     }
735
736     /* src image dimensions */
737     rect_src.left = 0;
738     rect_src.top = 0;
739     rect_src.right = p_vout->render.i_width;
740     rect_src.bottom = p_vout->render.i_height;
741
742     /* Clip the source image */
743     rect_src_clipped.left = p_vout->fmt_out.i_x_offset +
744       (rect_dest_clipped.left - rect_dest.left) *
745       p_vout->fmt_out.i_visible_width / (rect_dest.right - rect_dest.left);
746     rect_src_clipped.right = p_vout->fmt_out.i_x_offset +
747       p_vout->fmt_out.i_visible_width -
748       (rect_dest.right - rect_dest_clipped.right) *
749       p_vout->fmt_out.i_visible_width / (rect_dest.right - rect_dest.left);
750     rect_src_clipped.top = p_vout->fmt_out.i_y_offset +
751       (rect_dest_clipped.top - rect_dest.top) *
752       p_vout->fmt_out.i_visible_height / (rect_dest.bottom - rect_dest.top);
753     rect_src_clipped.bottom = p_vout->fmt_out.i_y_offset +
754       p_vout->fmt_out.i_visible_height -
755       (rect_dest.bottom - rect_dest_clipped.bottom) *
756       p_vout->fmt_out.i_visible_height / (rect_dest.bottom - rect_dest.top);
757
758 #ifdef MODULE_NAME_IS_directx
759     /* Apply overlay hardware constraints */
760     if( p_vout->p_sys->b_using_overlay )
761     {
762         if( p_vout->p_sys->i_align_src_boundary )
763             rect_src_clipped.left = ( rect_src_clipped.left +
764                 p_vout->p_sys->i_align_src_boundary / 2 ) &
765                 ~p_vout->p_sys->i_align_src_boundary;
766
767         if( p_vout->p_sys->i_align_src_size )
768             rect_src_clipped.right = (( rect_src_clipped.right -
769                 rect_src_clipped.left +
770                 p_vout->p_sys->i_align_src_size / 2 ) &
771                 ~p_vout->p_sys->i_align_src_size) + rect_src_clipped.left;
772     }
773 #endif
774
775 #ifndef NDEBUG
776     msg_Dbg( p_vout, "DirectXUpdateRects image_src_clipped"
777                      " coords: %i,%i,%i,%i",
778                      rect_src_clipped.left, rect_src_clipped.top,
779                      rect_src_clipped.right, rect_src_clipped.bottom );
780 #endif
781
782 #ifdef MODULE_NAME_IS_directx
783     /* The destination coordinates need to be relative to the current
784      * directdraw primary surface (display) */
785     rect_dest_clipped.left -= p_vout->p_sys->rect_display.left;
786     rect_dest_clipped.right -= p_vout->p_sys->rect_display.left;
787     rect_dest_clipped.top -= p_vout->p_sys->rect_display.top;
788     rect_dest_clipped.bottom -= p_vout->p_sys->rect_display.top;
789
790     if( p_vout->p_sys->b_using_overlay )
791         DirectDrawUpdateOverlay( p_vout );
792 #endif
793
794     /* Signal the change in size/position */
795     p_vout->p_sys->i_changes |= DX_POSITION_CHANGE;
796
797 #undef rect_src
798 #undef rect_src_clipped
799 #undef rect_dest
800 #undef rect_dest_clipped
801 }
802
803 /*****************************************************************************
804  * DirectXEventProc: This is the window event processing function.
805  *****************************************************************************
806  * On Windows, when you create a window you have to attach an event processing
807  * function to it. The aim of this function is to manage "Queued Messages" and
808  * "Nonqueued Messages".
809  * Queued Messages are those picked up and retransmitted by vout_Manage
810  * (using the GetMessage and DispatchMessage functions).
811  * Nonqueued Messages are those that Windows will send directly to this
812  * procedure (like WM_DESTROY, WM_WINDOWPOSCHANGED...)
813  *****************************************************************************/
814 static long FAR PASCAL DirectXEventProc( HWND hwnd, UINT message,
815                                          WPARAM wParam, LPARAM lParam )
816 {
817     vout_thread_t *p_vout;
818
819     if( message == WM_CREATE )
820     {
821         /* Store p_vout for future use */
822         p_vout = (vout_thread_t *)((CREATESTRUCT *)lParam)->lpCreateParams;
823         SetWindowLongPtr( hwnd, GWLP_USERDATA, (LONG_PTR)p_vout );
824         return TRUE;
825     }
826     else
827     {
828         p_vout = (vout_thread_t *)GetWindowLongPtr( hwnd, GWLP_USERDATA );
829         if( !p_vout )
830         {
831             /* Hmmm mozilla does manage somehow to save the pointer to our
832              * windowproc and still calls it after the vout has been closed. */
833             return DefWindowProc(hwnd, message, wParam, lParam);
834         }
835     }
836
837 #ifndef UNDER_CE
838     /* Catch the screensaver and the monitor turn-off */
839     if( message == WM_SYSCOMMAND &&
840         ( (wParam & 0xFFF0) == SC_SCREENSAVE || (wParam & 0xFFF0) == SC_MONITORPOWER ) )
841     {
842         //if( p_vout ) msg_Dbg( p_vout, "WinProc WM_SYSCOMMAND screensaver" );
843         return 0; /* this stops them from happening */
844     }
845 #endif
846
847     if( hwnd == p_vout->p_sys->hvideownd )
848     {
849         switch( message )
850         {
851 #ifdef MODULE_NAME_IS_directx
852         case WM_ERASEBKGND:
853         /* For overlay, we need to erase background */
854             return !p_vout->p_sys->b_using_overlay ?
855                 1 : DefWindowProc(hwnd, message, wParam, lParam);
856         case WM_PAINT:
857         /*
858         ** For overlay, DefWindowProc() will erase dirty regions
859         ** with colorkey.
860         ** For non-overlay, vout will paint the whole window at
861         ** regular interval, therefore dirty regions can be ignored
862         ** to minimize repaint.
863         */
864             if( !p_vout->p_sys->b_using_overlay )
865             {
866                 ValidateRect(hwnd, NULL);
867             }
868             // fall through to default
869 #else
870         /*
871         ** For OpenGL and Direct3D, vout will update the whole
872         ** window at regular interval, therefore dirty region
873         ** can be ignored to minimize repaint.
874         */
875         case WM_ERASEBKGND:
876             /* nothing to erase */
877             return 1;
878         case WM_PAINT:
879             /* nothing to repaint */
880             ValidateRect(hwnd, NULL);
881             // fall through
882 #endif
883         default:
884             return DefWindowProc(hwnd, message, wParam, lParam);
885         }
886     }
887
888     switch( message )
889     {
890
891     case WM_WINDOWPOSCHANGED:
892         UpdateRects( p_vout, true );
893         return 0;
894
895     /* the user wants to close the window */
896     case WM_CLOSE:
897     {
898         playlist_t * p_playlist = pl_Hold( p_vout );
899         if( p_playlist )
900         {
901             playlist_Stop( p_playlist );
902             pl_Release( p_vout );
903         }
904         return 0;
905     }
906
907     /* the window has been closed so shut down everything now */
908     case WM_DESTROY:
909         msg_Dbg( p_vout, "WinProc WM_DESTROY" );
910         /* just destroy the window */
911         PostQuitMessage( 0 );
912         return 0;
913
914     case WM_SYSCOMMAND:
915         switch (wParam)
916         {
917             case IDM_TOGGLE_ON_TOP:            /* toggle the "on top" status */
918             {
919                 vlc_value_t val;
920                 msg_Dbg( p_vout, "WinProc WM_SYSCOMMAND: IDM_TOGGLE_ON_TOP");
921
922                 /* Change the current value */
923                 var_Get( p_vout, "video-on-top", &val );
924                 val.b_bool = !val.b_bool;
925                 var_Set( p_vout, "video-on-top", val );
926                 return 0;
927             }
928         }
929         break;
930
931     case WM_PAINT:
932     case WM_NCPAINT:
933     case WM_ERASEBKGND:
934         return DefWindowProc(hwnd, message, wParam, lParam);
935
936     case WM_KILLFOCUS:
937 #ifdef MODULE_NAME_IS_wingapi
938         p_vout->p_sys->b_focus = false;
939         if( !p_vout->p_sys->b_parent_focus ) GXSuspend();
940 #endif
941 #ifdef UNDER_CE
942         if( hwnd == p_vout->p_sys->hfswnd )
943         {
944             HWND htbar = FindWindow( _T("HHTaskbar"), NULL );
945             ShowWindow( htbar, SW_SHOW );
946         }
947
948         if( !p_vout->p_sys->hparent ||
949             hwnd == p_vout->p_sys->hfswnd )
950         {
951             SHFullScreen( hwnd, SHFS_SHOWSIPBUTTON );
952         }
953 #endif
954         return 0;
955
956     case WM_SETFOCUS:
957 #ifdef MODULE_NAME_IS_wingapi
958         p_vout->p_sys->b_focus = true;
959         GXResume();
960 #endif
961 #ifdef UNDER_CE
962         if( p_vout->p_sys->hparent &&
963             hwnd != p_vout->p_sys->hfswnd && p_vout->b_fullscreen )
964             p_vout->p_sys->i_changes |= VOUT_FULLSCREEN_CHANGE;
965
966         if( hwnd == p_vout->p_sys->hfswnd )
967         {
968             HWND htbar = FindWindow( _T("HHTaskbar"), NULL );
969             ShowWindow( htbar, SW_HIDE );
970         }
971
972         if( !p_vout->p_sys->hparent ||
973             hwnd == p_vout->p_sys->hfswnd )
974         {
975             SHFullScreen( hwnd, SHFS_HIDESIPBUTTON );
976         }
977 #endif
978         return 0;
979
980     default:
981         //msg_Dbg( p_vout, "WinProc WM Default %i", message );
982         break;
983     }
984
985     /* Let windows handle the message */
986     return DefWindowProc(hwnd, message, wParam, lParam);
987 }
988
989 static struct
990 {
991     int i_dxkey;
992     int i_vlckey;
993
994 } dxkeys_to_vlckeys[] =
995 {
996     { VK_F1, KEY_F1 }, { VK_F2, KEY_F2 }, { VK_F3, KEY_F3 }, { VK_F4, KEY_F4 },
997     { VK_F5, KEY_F5 }, { VK_F6, KEY_F6 }, { VK_F7, KEY_F7 }, { VK_F8, KEY_F8 },
998     { VK_F9, KEY_F9 }, { VK_F10, KEY_F10 }, { VK_F11, KEY_F11 },
999     { VK_F12, KEY_F12 },
1000
1001     { VK_RETURN, KEY_ENTER },
1002     { VK_SPACE, KEY_SPACE },
1003     { VK_ESCAPE, KEY_ESC },
1004
1005     { VK_LEFT, KEY_LEFT },
1006     { VK_RIGHT, KEY_RIGHT },
1007     { VK_UP, KEY_UP },
1008     { VK_DOWN, KEY_DOWN },
1009
1010     { VK_HOME, KEY_HOME },
1011     { VK_END, KEY_END },
1012     { VK_PRIOR, KEY_PAGEUP },
1013     { VK_NEXT, KEY_PAGEDOWN },
1014
1015     { VK_INSERT, KEY_INSERT },
1016     { VK_DELETE, KEY_DELETE },
1017
1018     { VK_CONTROL, 0 },
1019     { VK_SHIFT, 0 },
1020     { VK_MENU, 0 },
1021
1022     { VK_BROWSER_BACK, KEY_BROWSER_BACK },
1023     { VK_BROWSER_FORWARD, KEY_BROWSER_FORWARD },
1024     { VK_BROWSER_REFRESH, KEY_BROWSER_REFRESH },
1025     { VK_BROWSER_STOP, KEY_BROWSER_STOP },
1026     { VK_BROWSER_SEARCH, KEY_BROWSER_SEARCH },
1027     { VK_BROWSER_FAVORITES, KEY_BROWSER_FAVORITES },
1028     { VK_BROWSER_HOME, KEY_BROWSER_HOME },
1029     { VK_VOLUME_MUTE, KEY_VOLUME_MUTE },
1030     { VK_VOLUME_DOWN, KEY_VOLUME_DOWN },
1031     { VK_VOLUME_UP, KEY_VOLUME_UP },
1032     { VK_MEDIA_NEXT_TRACK, KEY_MEDIA_NEXT_TRACK },
1033     { VK_MEDIA_PREV_TRACK, KEY_MEDIA_PREV_TRACK },
1034     { VK_MEDIA_STOP, KEY_MEDIA_STOP },
1035     { VK_MEDIA_PLAY_PAUSE, KEY_MEDIA_PLAY_PAUSE },
1036
1037     { 0, 0 }
1038 };
1039
1040 static int DirectXConvertKey( int i_key )
1041 {
1042     int i;
1043
1044     for( i = 0; dxkeys_to_vlckeys[i].i_dxkey != 0; i++ )
1045     {
1046         if( dxkeys_to_vlckeys[i].i_dxkey == i_key )
1047         {
1048             return dxkeys_to_vlckeys[i].i_vlckey;
1049         }
1050     }
1051
1052     return 0;
1053 }
1054
1055 /*****************************************************************************
1056  * Control: control facility for the vout
1057  *****************************************************************************/
1058 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
1059 {
1060     unsigned int *pi_width, *pi_height;
1061         bool b_bool;
1062     RECT rect_window;
1063     POINT point;
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     return vout_ControlWindow( p_vout->p_sys->parent_window, i_query, args );
1112 }
1113
1114 static int ControlParentWindow( vout_thread_t *p_vout, int i_query, ... )
1115 {
1116     va_list args;
1117     int ret;
1118
1119     va_start( args, i_query );
1120     ret = vaControlParentWindow( p_vout, i_query, args );
1121     va_end( args );
1122     return ret;
1123 }
1124
1125 void Win32ToggleFullscreen( vout_thread_t *p_vout )
1126 {
1127     HWND hwnd = (p_vout->p_sys->hparent && p_vout->p_sys->hfswnd) ?
1128         p_vout->p_sys->hfswnd : p_vout->p_sys->hwnd;
1129
1130     /* Save the current windows placement/placement to restore
1131        when fullscreen is over */
1132     WINDOWPLACEMENT window_placement = getWindowState( hwnd );
1133
1134     p_vout->b_fullscreen = ! p_vout->b_fullscreen;
1135
1136     /* We want to go to Fullscreen */
1137     if( p_vout->b_fullscreen )
1138     {
1139         msg_Dbg( p_vout, "entering fullscreen mode" );
1140
1141         /* Change window style, no borders and no title bar */
1142         int i_style = WS_CLIPCHILDREN | WS_VISIBLE;
1143         SetWindowLong( hwnd, GWL_STYLE, i_style );
1144
1145         if( p_vout->p_sys->hparent )
1146         {
1147 #ifdef UNDER_CE
1148             POINT point = {0,0};
1149             RECT rect;
1150             ClientToScreen( p_vout->p_sys->hwnd, &point );
1151             GetClientRect( p_vout->p_sys->hwnd, &rect );
1152             SetWindowPos( hwnd, 0, point.x, point.y,
1153                           rect.right, rect.bottom,
1154                           SWP_NOZORDER|SWP_FRAMECHANGED );
1155 #else
1156             /* Retrieve current window position so fullscreen will happen
1157             *on the right screen */
1158             HMONITOR hmon = MonitorFromWindow(p_vout->p_sys->hparent,
1159                                             MONITOR_DEFAULTTONEAREST);
1160             MONITORINFO mi = {sizeof(mi)};
1161             if (GetMonitorInfo(hmon, &mi))
1162             SetWindowPos( hwnd, 0,
1163                             mi.rcMonitor.left,
1164                             mi.rcMonitor.top,
1165                             mi.rcMonitor.right - mi.rcMonitor.left,
1166                             mi.rcMonitor.bottom - mi.rcMonitor.top,
1167                             SWP_NOZORDER|SWP_FRAMECHANGED );
1168 #endif
1169         }
1170         else
1171         {
1172             /* Maximize non embedded window */
1173             ShowWindow( hwnd, SW_SHOWMAXIMIZED );
1174         }
1175
1176         if( p_vout->p_sys->hparent )
1177         {
1178             /* Hide the previous window */
1179             RECT rect;
1180             GetClientRect( hwnd, &rect );
1181             SetParent( p_vout->p_sys->hwnd, hwnd );
1182             SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
1183                           rect.right, rect.bottom,
1184                           SWP_NOZORDER|SWP_FRAMECHANGED );
1185
1186 #ifdef UNDER_CE
1187             HWND topLevelParent = GetParent( p_vout->p_sys->hparent );
1188 #else
1189             HWND topLevelParent = GetAncestor( p_vout->p_sys->hparent, GA_ROOT );
1190 #endif
1191             ShowWindow( topLevelParent, SW_HIDE );
1192         }
1193
1194         SetForegroundWindow( hwnd );
1195     }
1196     else
1197     {
1198         msg_Dbg( p_vout, "leaving fullscreen mode" );
1199         /* Change window style, no borders and no title bar */
1200         SetWindowLong( hwnd, GWL_STYLE, p_vout->p_sys->i_window_style );
1201
1202         if( p_vout->p_sys->hparent )
1203         {
1204             RECT rect;
1205             GetClientRect( p_vout->p_sys->hparent, &rect );
1206             SetParent( p_vout->p_sys->hwnd, p_vout->p_sys->hparent );
1207             SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
1208                           rect.right, rect.bottom,
1209                           SWP_NOZORDER|SWP_FRAMECHANGED );
1210
1211 #ifdef UNDER_CE
1212             HWND topLevelParent = GetParent( p_vout->p_sys->hparent );
1213 #else
1214             HWND topLevelParent = GetAncestor( p_vout->p_sys->hparent, GA_ROOT );
1215 #endif
1216             ShowWindow( topLevelParent, SW_SHOW );
1217             SetForegroundWindow( p_vout->p_sys->hparent );
1218             ShowWindow( hwnd, SW_HIDE );
1219         }
1220         else
1221         {
1222             /* return to normal window for non embedded vout */
1223             SetWindowPlacement( hwnd, &window_placement );
1224             ShowWindow( hwnd, SW_SHOWNORMAL );
1225         }
1226
1227         /* Make sure the mouse cursor is displayed */
1228         PostMessage( p_vout->p_sys->hwnd, WM_VLC_SHOW_MOUSE, 0, 0 );
1229     }
1230
1231     /* Update the object variable and trigger callback */
1232     var_SetBool( p_vout, "fullscreen", p_vout->b_fullscreen );
1233 }
1234
1235 void DisableScreensaver( vout_thread_t *p_vout )
1236 {
1237     /* disable screensaver by temporarily changing system settings */
1238     p_vout->p_sys->i_spi_lowpowertimeout = 0;
1239     p_vout->p_sys->i_spi_powerofftimeout = 0;
1240     p_vout->p_sys->i_spi_screensavetimeout = 0;
1241     if( var_GetBool( p_vout, "disable-screensaver" ) )
1242     {
1243         msg_Dbg(p_vout, "disabling screen saver");
1244         SystemParametersInfo(SPI_GETLOWPOWERTIMEOUT,
1245             0, &(p_vout->p_sys->i_spi_lowpowertimeout), 0);
1246         if( 0 != p_vout->p_sys->i_spi_lowpowertimeout ) {
1247             SystemParametersInfo(SPI_SETLOWPOWERTIMEOUT, 0, NULL, 0);
1248         }
1249         SystemParametersInfo(SPI_GETPOWEROFFTIMEOUT, 0,
1250             &(p_vout->p_sys->i_spi_powerofftimeout), 0);
1251         if( 0 != p_vout->p_sys->i_spi_powerofftimeout ) {
1252             SystemParametersInfo(SPI_SETPOWEROFFTIMEOUT, 0, NULL, 0);
1253         }
1254         SystemParametersInfo(SPI_GETSCREENSAVETIMEOUT, 0,
1255             &(p_vout->p_sys->i_spi_screensavetimeout), 0);
1256         if( 0 != p_vout->p_sys->i_spi_screensavetimeout ) {
1257             SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT, 0, NULL, 0);
1258         }
1259     }
1260 }
1261
1262 void RestoreScreensaver( vout_thread_t *p_vout )
1263 {
1264     /* restore screensaver system settings */
1265     if( 0 != p_vout->p_sys->i_spi_lowpowertimeout ) {
1266         SystemParametersInfo(SPI_SETLOWPOWERTIMEOUT,
1267             p_vout->p_sys->i_spi_lowpowertimeout, NULL, 0);
1268     }
1269     if( 0 != p_vout->p_sys->i_spi_powerofftimeout ) {
1270         SystemParametersInfo(SPI_SETPOWEROFFTIMEOUT,
1271             p_vout->p_sys->i_spi_powerofftimeout, NULL, 0);
1272     }
1273     if( 0 != p_vout->p_sys->i_spi_screensavetimeout ) {
1274         SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT,
1275             p_vout->p_sys->i_spi_screensavetimeout, NULL, 0);
1276     }
1277 }
1278
1279 int CreateEventThread( vout_thread_t *p_vout )
1280 {
1281     /* Create the Vout EventThread, this thread is created by us to isolate
1282      * the Win32 PeekMessage function calls. We want to do this because
1283      * Windows can stay blocked inside this call for a long time, and when
1284      * this happens it thus blocks vlc's video_output thread.
1285      * Vout EventThread will take care of the creation of the video
1286      * window (because PeekMessage has to be called from the same thread which
1287      * created the window). */
1288     msg_Dbg( p_vout, "creating Vout EventThread" );
1289     p_vout->p_sys->p_event =
1290         vlc_object_create( p_vout, sizeof(event_thread_t) );
1291     p_vout->p_sys->p_event->p_vout = p_vout;
1292     p_vout->p_sys->p_event->window_ready = CreateEvent( NULL, TRUE, FALSE, NULL );
1293     if( vlc_thread_create( p_vout->p_sys->p_event, "Vout Events Thread",
1294                            EventThread, 0 ) )
1295     {
1296         msg_Err( p_vout, "cannot create Vout EventThread" );
1297         CloseHandle( p_vout->p_sys->p_event->window_ready );
1298         vlc_object_release( p_vout->p_sys->p_event );
1299         p_vout->p_sys->p_event = NULL;
1300         return 0;
1301     }
1302     WaitForSingleObject( p_vout->p_sys->p_event->window_ready, INFINITE );
1303     CloseHandle( p_vout->p_sys->p_event->window_ready );
1304
1305     if( p_vout->p_sys->p_event->b_error )
1306     {
1307         msg_Err( p_vout, "Vout EventThread failed" );
1308         return 0;
1309     }
1310
1311     vlc_object_attach( p_vout->p_sys->p_event, p_vout );
1312
1313     msg_Dbg( p_vout, "Vout EventThread running" );
1314     return 1;
1315 }
1316
1317 void StopEventThread( vout_thread_t *p_vout )
1318 {
1319     if( p_vout->b_fullscreen )
1320     {
1321         msg_Dbg( p_vout, "Quitting fullscreen" );
1322         Win32ToggleFullscreen( p_vout );
1323         /* Force fullscreen in the core for the next video */
1324         var_SetBool( p_vout, "fullscreen", true );
1325     }
1326
1327     if( p_vout->p_sys->p_event )
1328     {
1329         vlc_object_detach( p_vout->p_sys->p_event );
1330
1331         /* Kill Vout EventThread */
1332         vlc_object_kill( p_vout->p_sys->p_event );
1333
1334         /* we need to be sure Vout EventThread won't stay stuck in
1335          * GetMessage, so we send a fake message */
1336         if( p_vout->p_sys->hwnd )
1337         {
1338             PostMessage( p_vout->p_sys->hwnd, WM_NULL, 0, 0);
1339         }
1340
1341         vlc_thread_join( p_vout->p_sys->p_event );
1342         vlc_object_release( p_vout->p_sys->p_event );
1343     }
1344
1345     if( !p_vout->p_sys->i_changes & SWITCHING_MODE_FLAG )
1346         vlc_mutex_destroy( &p_vout->p_sys->lock );
1347 }