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