]> git.sesse.net Git - vlc/blobdiff - modules/video_output/msw/events.c
Win32: add support for Win 7 taskbar thumbnails
[vlc] / modules / video_output / msw / events.c
index bbed9dbceb9ad8022cc26743771c3a5d6c65308c..365ee1ac614d8ea98eb7c65aeb5ae5a88705e550 100644 (file)
@@ -1,7 +1,7 @@
 /*****************************************************************************
  * events.c: Windows DirectX video output events handler
  *****************************************************************************
- * Copyright (C) 2001-2004 the VideoLAN team
+ * Copyright (C) 2001-2009 the VideoLAN team
  * $Id$
  *
  * Authors: Gildas Bazin <gbazin@videolan.org>
  * Preamble: This file contains the functions related to the creation of
  *             a window and the handling of its messages (events).
  *****************************************************************************/
+#ifdef HAVE_CONFIG_H
+# include "config.h"
+#endif
+
 #include <errno.h>                                                 /* ENOMEM */
-#include <stdlib.h>                                                /* free() */
 #include <ctype.h>                                              /* tolower() */
-#include <string.h>                                            /* strerror() */
 
 #ifndef _WIN32_WINNT
 #   define _WIN32_WINNT 0x0500
 #endif
 
-#include <vlc/vlc.h>
+#include <vlc_common.h>
 #include <vlc_interface.h>
 #include <vlc_playlist.h>
 #include <vlc_vout.h>
+#include <vlc_vout_window.h>
 
 #include <windows.h>
+#include <tchar.h>
 #include <windowsx.h>
 #include <shellapi.h>
 
-#ifdef MODULE_NAME_IS_vout_directx
+#ifdef MODULE_NAME_IS_directx
 #include <ddraw.h>
 #endif
 #ifdef MODULE_NAME_IS_direct3d
 #include <GL/gl.h>
 #endif
 
-#include "vlc_keys.h"
+#include <vlc_keys.h>
 #include "vout.h"
 
-#if defined(UNDER_CE) && !defined(__PLUGIN__) /*FIXME*/
-#   define SHFS_SHOWSIPBUTTON 0x0004
+#ifndef UNDER_CE
+#include <vlc_windows_interfaces.h>
+#endif
+
+#ifdef UNDER_CE
+#include <aygshell.h>
+    //WINSHELLAPI BOOL WINAPI SHFullScreen(HWND hwndRequester, DWORD dwState);
+#endif
+
+/*#if defined(UNDER_CE) && !defined(__PLUGIN__) --FIXME*/
+/*#   define SHFS_SHOWSIPBUTTON 0x0004
 #   define SHFS_HIDESIPBUTTON 0x0008
 #   define MENU_HEIGHT 26
     BOOL SHFullScreen(HWND hwndRequester, DWORD dwState);
-#endif
+#endif*/
+
 
 /*****************************************************************************
  * Local prototypes.
@@ -72,18 +86,13 @@ static void DirectXCloseWindow ( vout_thread_t *p_vout );
 static long FAR PASCAL DirectXEventProc( HWND, UINT, WPARAM, LPARAM );
 
 static int Control( vout_thread_t *p_vout, int i_query, va_list args );
+static int vaControlParentWindow( vout_thread_t *, int, va_list );
 
-static void DirectXPopupMenu( event_thread_t *p_event, vlc_bool_t b_open )
+static void DirectXPopupMenu( event_thread_t *p_event, bool b_open )
 {
-    playlist_t *p_playlist =
-        vlc_object_find( p_event, VLC_OBJECT_PLAYLIST, FIND_ANYWHERE );
-    if( p_playlist != NULL )
-    {
-        vlc_value_t val;
-        val.b_bool = b_open;
-        var_Set( p_playlist, "intf-popupmenu", val );
-        vlc_object_release( p_playlist );
-    }
+    vlc_value_t val;
+    val.b_bool = b_open;
+    var_Set( p_event->p_libvlc, "intf-popupmenu", val );
 }
 
 static int DirectXConvertKey( int i_key );
@@ -96,13 +105,15 @@ static int DirectXConvertKey( int i_key );
  * The main goal of this thread is to isolate the Win32 PeekMessage function
  * because this one can block for a long time.
  *****************************************************************************/
-void E_(EventThread)( event_thread_t *p_event )
+void* EventThread( vlc_object_t *p_this )
 {
+    event_thread_t *p_event = (event_thread_t *)p_this;
     MSG msg;
     POINT old_mouse_pos = {0,0}, mouse_pos;
     vlc_value_t val;
-    int i_width, i_height, i_x, i_y;
+    unsigned int i_width, i_height, i_x, i_y;
     HMODULE hkernel32;
+    int canc = vlc_savecancel ();
 
     /* Initialisation */
     p_event->p_vout->pf_control = Control;
@@ -112,12 +123,12 @@ void E_(EventThread)( event_thread_t *p_event )
      * message queue */
     if( DirectXCreateWindow( p_event->p_vout ) )
     {
-        msg_Err( p_event, "out of memory" );
-        p_event->b_dead = VLC_TRUE;
+        vlc_restorecancel (canc);
+        return NULL;
     }
 
     /* Signal the creation of the window */
-    vlc_thread_ready( p_event );
+    SetEvent( p_event->window_ready );
 
 #ifndef UNDER_CE
     /* Set power management stuff */
@@ -138,10 +149,10 @@ void E_(EventThread)( event_thread_t *p_event )
 
     /* Main loop */
     /* GetMessage will sleep if there's no message in the queue */
-    while( !p_event->b_die && GetMessage( &msg, 0, 0, 0 ) )
+    while( vlc_object_alive (p_event) && GetMessage( &msg, 0, 0, 0 ) )
     {
         /* Check if we are asked to exit */
-        if( p_event->b_die )
+        if( !vlc_object_alive (p_event) )
             break;
 
         switch( msg.message )
@@ -170,8 +181,7 @@ void E_(EventThread)( event_thread_t *p_event )
                     p_event->p_vout->fmt_in.i_y_offset;
                 var_Set( p_event->p_vout, "mouse-y", val );
 
-                val.b_bool = VLC_TRUE;
-                var_Set( p_event->p_vout, "mouse-moved", val );
+                var_SetBool( p_event->p_vout, "mouse-moved", true );
             }
 
         case WM_NCMOUSEMOVE:
@@ -192,14 +202,14 @@ void E_(EventThread)( event_thread_t *p_event )
 
         case WM_VLC_HIDE_MOUSE:
             if( p_event->p_vout->p_sys->b_cursor_hidden ) break;
-            p_event->p_vout->p_sys->b_cursor_hidden = VLC_TRUE;
+            p_event->p_vout->p_sys->b_cursor_hidden = true;
             GetCursorPos( &old_mouse_pos );
             ShowCursor( FALSE );
             break;
 
         case WM_VLC_SHOW_MOUSE:
             if( !p_event->p_vout->p_sys->b_cursor_hidden ) break;
-            p_event->p_vout->p_sys->b_cursor_hidden = VLC_FALSE;
+            p_event->p_vout->p_sys->b_cursor_hidden = false;
             GetCursorPos( &old_mouse_pos );
             ShowCursor( TRUE );
             break;
@@ -208,7 +218,7 @@ void E_(EventThread)( event_thread_t *p_event )
             var_Get( p_event->p_vout, "mouse-button-down", &val );
             val.i_int |= 1;
             var_Set( p_event->p_vout, "mouse-button-down", val );
-            DirectXPopupMenu( p_event, VLC_FALSE );
+            DirectXPopupMenu( p_event, false );
             break;
 
         case WM_LBUTTONUP:
@@ -216,8 +226,7 @@ void E_(EventThread)( event_thread_t *p_event )
             val.i_int &= ~1;
             var_Set( p_event->p_vout, "mouse-button-down", val );
 
-            val.b_bool = VLC_TRUE;
-            var_Set( p_event->p_vout, "mouse-clicked", val );
+            var_SetBool( p_event->p_vout, "mouse-clicked", true );
             break;
 
         case WM_LBUTTONDBLCLK:
@@ -228,7 +237,7 @@ void E_(EventThread)( event_thread_t *p_event )
             var_Get( p_event->p_vout, "mouse-button-down", &val );
             val.i_int |= 2;
             var_Set( p_event->p_vout, "mouse-button-down", val );
-            DirectXPopupMenu( p_event, VLC_FALSE );
+            DirectXPopupMenu( p_event, false );
             break;
 
         case WM_MBUTTONUP:
@@ -241,14 +250,14 @@ void E_(EventThread)( event_thread_t *p_event )
             var_Get( p_event->p_vout, "mouse-button-down", &val );
             val.i_int |= 4;
             var_Set( p_event->p_vout, "mouse-button-down", val );
-            DirectXPopupMenu( p_event, VLC_FALSE );
+            DirectXPopupMenu( p_event, false );
             break;
 
         case WM_RBUTTONUP:
             var_Get( p_event->p_vout, "mouse-button-down", &val );
             val.i_int &= ~4;
             var_Set( p_event->p_vout, "mouse-button-down", val );
-            DirectXPopupMenu( p_event, VLC_TRUE );
+            DirectXPopupMenu( p_event, true );
             break;
 
         case WM_KEYDOWN:
@@ -314,7 +323,7 @@ void E_(EventThread)( event_thread_t *p_event )
             var_Get( p_event->p_vout, "video-title", &val );
             if( !val.psz_string || !*val.psz_string ) /* Default video title */
             {
-                if( val.psz_string ) free( val.psz_string );
+                free( val.psz_string );
 
 #ifdef MODULE_NAME_IS_wingdi
                 val.psz_string = strdup( VOUT_TITLE " (WinGDI output)" );
@@ -328,24 +337,25 @@ void E_(EventThread)( event_thread_t *p_event )
 #ifdef MODULE_NAME_IS_direct3d
                 val.psz_string = strdup( VOUT_TITLE " (Direct3D output)" );
 #endif
-#ifdef MODULE_NAME_IS_vout_directx
-                if( p_event->p_vout->p_sys->b_using_overlay ) val.psz_string = 
+#ifdef MODULE_NAME_IS_directx
+                if( p_event->p_vout->p_sys->b_using_overlay ) val.psz_string =
                     strdup( VOUT_TITLE " (hardware YUV overlay DirectX output)" );
-                else if( p_event->p_vout->p_sys->b_hw_yuv ) val.psz_string = 
+                else if( p_event->p_vout->p_sys->b_hw_yuv ) val.psz_string =
                     strdup( VOUT_TITLE " (hardware YUV DirectX output)" );
-                else val.psz_string = 
+                else val.psz_string =
                     strdup( VOUT_TITLE " (software RGB DirectX output)" );
 #endif
             }
 
-#ifdef UNICODE
             {
                 wchar_t *psz_title = malloc( strlen(val.psz_string) * 2 + 2 );
-                mbstowcs( psz_title, val.psz_string, strlen(val.psz_string)*2);
-                psz_title[strlen(val.psz_string)] = 0;
-                free( val.psz_string ); val.psz_string = (char *)psz_title;
+                if( psz_title )
+                {
+                    mbstowcs( psz_title, val.psz_string, strlen(val.psz_string)*2);
+                    psz_title[strlen(val.psz_string)] = 0;
+                    free( val.psz_string ); val.psz_string = (char *)psz_title;
+                }
             }
-#endif
 
             SetWindowText( p_event->p_vout->p_sys->hwnd,
                            (LPCTSTR)val.psz_string );
@@ -375,10 +385,12 @@ void E_(EventThread)( event_thread_t *p_event )
 
     msg_Dbg( p_event, "DirectXEventThread terminating" );
 
-    /* clear the changes formerly signaled */
-    p_event->p_vout->p_sys->i_changes = 0;
-
     DirectXCloseWindow( p_event->p_vout );
+    vlc_restorecancel (canc);
+
+    /* clear the changes formerly signaled */
+    p_event->p_vout->p_sys->i_changes = EVENT_THREAD_ENDED;
+    return NULL;
 }
 
 
@@ -406,12 +418,35 @@ static int DirectXCreateWindow( vout_thread_t *p_vout )
     /* Get this module's instance */
     hInstance = GetModuleHandle(NULL);
 
-    /* If an external window was specified, we'll draw in it. */
-    p_vout->p_sys->hparent =
-        vout_RequestWindow( p_vout, &p_vout->p_sys->i_window_x,
-                            &p_vout->p_sys->i_window_y,
-                            &p_vout->p_sys->i_window_width,
-                            &p_vout->p_sys->i_window_height );
+    #ifdef MODULE_NAME_IS_direct3d
+    if( !p_vout->p_sys->b_desktop )
+    {
+    #endif
+        vout_window_cfg_t wnd_cfg;
+        memset( &wnd_cfg, 0, sizeof(wnd_cfg) );
+        wnd_cfg.type   = VOUT_WINDOW_TYPE_HWND;
+        wnd_cfg.x      = p_vout->p_sys->i_window_x;
+        wnd_cfg.y      = p_vout->p_sys->i_window_y;
+        wnd_cfg.width  = p_vout->p_sys->i_window_width;
+        wnd_cfg.height = p_vout->p_sys->i_window_height;
+
+        /* If an external window was specified, we'll draw in it. */
+        p_vout->p_sys->parent_window = vout_window_New( VLC_OBJECT(p_vout), NULL, &wnd_cfg );
+        if( p_vout->p_sys->parent_window )
+            p_vout->p_sys->hparent = p_vout->p_sys->parent_window->handle.hwnd;
+    #ifdef MODULE_NAME_IS_direct3d
+    }
+    else
+    {
+        /* Find Program Manager */
+        HWND hwnd = FindWindow( _T("Progman"), NULL );
+        if( hwnd ) hwnd = FindWindowEx( hwnd, NULL, _T("SHELLDLL_DefView"), NULL );
+        if( hwnd ) hwnd = FindWindowEx( hwnd, NULL, _T("SysListView32"), NULL );
+        if( !hwnd )
+            msg_Err( p_vout, "Couldn't find desktop icon window. Desktop mode can't be established." );
+        p_vout->p_sys->hparent = hwnd;
+    }
+    #endif
 
     /* We create the window ourself, there is no previous window proc. */
     p_vout->p_sys->pf_wndproc = NULL;
@@ -589,9 +624,10 @@ static void DirectXCloseWindow( vout_thread_t *p_vout )
     DestroyWindow( p_vout->p_sys->hwnd );
     if( p_vout->p_sys->hfswnd ) DestroyWindow( p_vout->p_sys->hfswnd );
 
-    if( p_vout->p_sys->hparent )
-        vout_ReleaseWindow( p_vout, (void *)p_vout->p_sys->hparent );
-
+    #ifdef MODULE_NAME_IS_direct3d
+    if( !p_vout->p_sys->b_desktop )
+    #endif
+        vout_window_Delete( p_vout->p_sys->parent_window );
     p_vout->p_sys->hwnd = NULL;
 
     /* We don't unregister the Window Class because it could lead to race
@@ -606,14 +642,14 @@ static void DirectXCloseWindow( vout_thread_t *p_vout )
  * its job is to update the source and destination RECTs used to display the
  * picture.
  *****************************************************************************/
-void E_(UpdateRects)( vout_thread_t *p_vout, vlc_bool_t b_force )
+void UpdateRects( vout_thread_t *p_vout, bool b_force )
 {
 #define rect_src p_vout->p_sys->rect_src
 #define rect_src_clipped p_vout->p_sys->rect_src_clipped
 #define rect_dest p_vout->p_sys->rect_dest
 #define rect_dest_clipped p_vout->p_sys->rect_dest_clipped
 
-    int i_width, i_height, i_x, i_y;
+    unsigned int i_width, i_height, i_x, i_y;
 
     RECT  rect;
     POINT point;
@@ -655,18 +691,18 @@ void E_(UpdateRects)( vout_thread_t *p_vout, vlc_bool_t b_force )
     rect_dest.top = point.y + i_y;
     rect_dest.bottom = rect_dest.top + i_height;
 
-#ifdef MODULE_NAME_IS_vout_directx
+#ifdef MODULE_NAME_IS_directx
     /* Apply overlay hardware constraints */
     if( p_vout->p_sys->b_using_overlay )
     {
         if( p_vout->p_sys->i_align_dest_boundary )
             rect_dest.left = ( rect_dest.left +
-                p_vout->p_sys->i_align_dest_boundary / 2 ) & 
+                p_vout->p_sys->i_align_dest_boundary / 2 ) &
                 ~p_vout->p_sys->i_align_dest_boundary;
 
         if( p_vout->p_sys->i_align_dest_size )
             rect_dest.right = (( rect_dest.right - rect_dest.left +
-                p_vout->p_sys->i_align_dest_size / 2 ) & 
+                p_vout->p_sys->i_align_dest_size / 2 ) &
                 ~p_vout->p_sys->i_align_dest_size) + rect_dest.left;
     }
 
@@ -681,13 +717,20 @@ void E_(UpdateRects)( vout_thread_t *p_vout, vlc_bool_t b_force )
         return;
     }
 
-#if 0
+#ifndef NDEBUG
     msg_Dbg( p_vout, "DirectXUpdateRects image_dst_clipped coords:"
-                     " %i,%i,%i,%i",
+                     " %li,%li,%li,%li",
                      rect_dest_clipped.left, rect_dest_clipped.top,
                      rect_dest_clipped.right, rect_dest_clipped.bottom );
 #endif
 
+#else /* MODULE_NAME_IS_directx */
+
+    /* AFAIK, there are no clipping constraints in Direct3D, OpenGL and GDI */
+    rect_dest_clipped = rect_dest;
+
+#endif
+
     /* the 2 following lines are to fix a bug when clicking on the desktop */
     if( (rect_dest_clipped.right - rect_dest_clipped.left)==0 ||
         (rect_dest_clipped.bottom - rect_dest_clipped.top)==0 )
@@ -695,12 +738,6 @@ void E_(UpdateRects)( vout_thread_t *p_vout, vlc_bool_t b_force )
         SetRectEmpty( &rect_src_clipped );
         return;
     }
-#else /* MODULE_NAME_IS_vout_directx */
-
-    /* AFAIK, there are no clipping constraints in Direct3D, OpenGL and GDI */
-    rect_dest_clipped = rect_dest;
-
-#endif
 
     /* src image dimensions */
     rect_src.left = 0;
@@ -724,31 +761,31 @@ void E_(UpdateRects)( vout_thread_t *p_vout, vlc_bool_t b_force )
       (rect_dest.bottom - rect_dest_clipped.bottom) *
       p_vout->fmt_out.i_visible_height / (rect_dest.bottom - rect_dest.top);
 
-#ifdef MODULE_NAME_IS_vout_directx
+#ifdef MODULE_NAME_IS_directx
     /* Apply overlay hardware constraints */
     if( p_vout->p_sys->b_using_overlay )
     {
         if( p_vout->p_sys->i_align_src_boundary )
             rect_src_clipped.left = ( rect_src_clipped.left +
-                p_vout->p_sys->i_align_src_boundary / 2 ) & 
+                p_vout->p_sys->i_align_src_boundary / 2 ) &
                 ~p_vout->p_sys->i_align_src_boundary;
 
         if( p_vout->p_sys->i_align_src_size )
             rect_src_clipped.right = (( rect_src_clipped.right -
                 rect_src_clipped.left +
-                p_vout->p_sys->i_align_src_size / 2 ) & 
+                p_vout->p_sys->i_align_src_size / 2 ) &
                 ~p_vout->p_sys->i_align_src_size) + rect_src_clipped.left;
     }
 #endif
 
-#if 0
+#ifndef NDEBUG
     msg_Dbg( p_vout, "DirectXUpdateRects image_src_clipped"
-                     " coords: %i,%i,%i,%i",
+                     " coords: %li,%li,%li,%li",
                      rect_src_clipped.left, rect_src_clipped.top,
                      rect_src_clipped.right, rect_src_clipped.bottom );
 #endif
 
-#ifdef MODULE_NAME_IS_vout_directx
+#ifdef MODULE_NAME_IS_directx
     /* The destination coordinates need to be relative to the current
      * directdraw primary surface (display) */
     rect_dest_clipped.left -= p_vout->p_sys->rect_display.left;
@@ -760,6 +797,39 @@ void E_(UpdateRects)( vout_thread_t *p_vout, vlc_bool_t b_force )
         DirectDrawUpdateOverlay( p_vout );
 #endif
 
+#ifndef UNDER_CE
+    /* Windows 7 taskbar thumbnail code */
+    LPTASKBARLIST3 p_taskbl;
+    OSVERSIONINFO winVer;
+    winVer.dwOSVersionInfoSize = sizeof(OSVERSIONINFO);
+    if( GetVersionEx(&winVer) && winVer.dwMajorVersion > 5 && winVer.dwMajorVersion > 0 )
+    {
+        CoInitialize( 0 );
+
+        if( S_OK == CoCreateInstance( &clsid_ITaskbarList,
+                    NULL, CLSCTX_INPROC_SERVER,
+                    &IID_ITaskbarList3,
+                    (void **)&p_taskbl) )
+        {
+            RECT rect_video, rect_parent, rect_relative;
+            HWND hroot = GetAncestor(p_vout->p_sys->hwnd,GA_ROOT);
+
+            p_taskbl->vt->HrInit(p_taskbl);
+            GetWindowRect(p_vout->p_sys->hvideownd, &rect_video);
+            GetWindowRect(hroot, &rect_parent);
+            rect_relative.left = rect_video.left - rect_parent.left - 8;
+            rect_relative.right = rect_video.right - rect_video.left + rect_relative.left;
+            rect_relative.top = rect_video.top - rect_parent.top - 10;
+            rect_relative.bottom = rect_video.bottom - rect_video.top + rect_relative.top - 25;
+
+            if (S_OK != p_taskbl->vt->SetThumbnailClip(p_taskbl, hroot, &rect_relative))
+                msg_Err( p_vout, "SetThumbNailClip failed");
+
+            p_taskbl->vt->Release(p_taskbl);
+        }
+        CoUninitialize();
+    }
+#endif
     /* Signal the change in size/position */
     p_vout->p_sys->i_changes |= DX_POSITION_CHANGE;
 
@@ -794,7 +864,8 @@ static long FAR PASCAL DirectXEventProc( HWND hwnd, UINT message,
     }
     else
     {
-        p_vout = (vout_thread_t *)GetWindowLongPtr( hwnd, GWLP_USERDATA );
+        LONG_PTR p_user_data = GetWindowLongPtr( hwnd, GWLP_USERDATA );
+        p_vout = (vout_thread_t *)p_user_data;
         if( !p_vout )
         {
             /* Hmmm mozilla does manage somehow to save the pointer to our
@@ -817,7 +888,7 @@ static long FAR PASCAL DirectXEventProc( HWND hwnd, UINT message,
     {
         switch( message )
         {
-#ifdef MODULE_NAME_IS_vout_directx
+#ifdef MODULE_NAME_IS_directx
         case WM_ERASEBKGND:
         /* For overlay, we need to erase background */
             return !p_vout->p_sys->b_using_overlay ?
@@ -826,7 +897,7 @@ static long FAR PASCAL DirectXEventProc( HWND hwnd, UINT message,
         /*
         ** For overlay, DefWindowProc() will erase dirty regions
         ** with colorkey.
-        ** For non-overlay, vout will paint the whole window at 
+        ** For non-overlay, vout will paint the whole window at
         ** regular interval, therefore dirty regions can be ignored
         ** to minimize repaint.
         */
@@ -848,7 +919,7 @@ static long FAR PASCAL DirectXEventProc( HWND hwnd, UINT message,
             /* nothing to repaint */
             ValidateRect(hwnd, NULL);
             // fall through
-#endif 
+#endif
         default:
             return DefWindowProc(hwnd, message, wParam, lParam);
         }
@@ -858,22 +929,18 @@ static long FAR PASCAL DirectXEventProc( HWND hwnd, UINT message,
     {
 
     case WM_WINDOWPOSCHANGED:
-        E_(UpdateRects)( p_vout, VLC_TRUE );
+        UpdateRects( p_vout, true );
         return 0;
 
     /* the user wants to close the window */
     case WM_CLOSE:
     {
-        playlist_t * p_playlist =
-            (playlist_t *)vlc_object_find( p_vout, VLC_OBJECT_PLAYLIST,
-                                           FIND_ANYWHERE );
-        if( p_playlist == NULL )
+        playlist_t * p_playlist = pl_Hold( p_vout );
+        if( p_playlist )
         {
-            return 0;
+            playlist_Stop( p_playlist );
+            pl_Release( p_vout );
         }
-
-        playlist_Stop( p_playlist );
-        vlc_object_release( p_playlist );
         return 0;
     }
 
@@ -908,44 +975,44 @@ static long FAR PASCAL DirectXEventProc( HWND hwnd, UINT message,
 
     case WM_KILLFOCUS:
 #ifdef MODULE_NAME_IS_wingapi
-        p_vout->p_sys->b_focus = VLC_FALSE;
+        p_vout->p_sys->b_focus = false;
         if( !p_vout->p_sys->b_parent_focus ) GXSuspend();
 #endif
 #ifdef UNDER_CE
-        if( hWnd == p_vout->p_sys->hfswnd )
+        if( hwnd == p_vout->p_sys->hfswnd )
         {
             HWND htbar = FindWindow( _T("HHTaskbar"), NULL );
             ShowWindow( htbar, SW_SHOW );
         }
 
         if( !p_vout->p_sys->hparent ||
-            hWnd == p_vout->p_sys->hfswnd )
+            hwnd == p_vout->p_sys->hfswnd )
         {
-            SHFullScreen( hWnd, SHFS_SHOWSIPBUTTON );
+            SHFullScreen( hwnd, SHFS_SHOWSIPBUTTON );
         }
 #endif
         return 0;
 
     case WM_SETFOCUS:
 #ifdef MODULE_NAME_IS_wingapi
-        p_vout->p_sys->b_focus = VLC_TRUE;
+        p_vout->p_sys->b_focus = true;
         GXResume();
 #endif
 #ifdef UNDER_CE
         if( p_vout->p_sys->hparent &&
-            hWnd != p_vout->p_sys->hfswnd && p_vout->b_fullscreen )
+            hwnd != p_vout->p_sys->hfswnd && p_vout->b_fullscreen )
             p_vout->p_sys->i_changes |= VOUT_FULLSCREEN_CHANGE;
 
-        if( hWnd == p_vout->p_sys->hfswnd )
+        if( hwnd == p_vout->p_sys->hfswnd )
         {
             HWND htbar = FindWindow( _T("HHTaskbar"), NULL );
             ShowWindow( htbar, SW_HIDE );
         }
 
         if( !p_vout->p_sys->hparent ||
-            hWnd == p_vout->p_sys->hfswnd )
+            hwnd == p_vout->p_sys->hfswnd )
         {
-            SHFullScreen( hWnd, SHFS_HIDESIPBUTTON );
+            SHFullScreen( hwnd, SHFS_HIDESIPBUTTON );
         }
 #endif
         return 0;
@@ -1030,30 +1097,13 @@ static int DirectXConvertKey( int i_key )
  *****************************************************************************/
 static int Control( vout_thread_t *p_vout, int i_query, va_list args )
 {
-    unsigned int *pi_width, *pi_height;
     RECT rect_window;
-    POINT point;
 
     switch( i_query )
     {
-    case VOUT_GET_SIZE:
-        if( p_vout->p_sys->hparent )
-            return vout_ControlWindow( p_vout,
-                    (void *)p_vout->p_sys->hparent, i_query, args );
-
-        pi_width  = va_arg( args, unsigned int * );
-        pi_height = va_arg( args, unsigned int * );
-
-        GetClientRect( p_vout->p_sys->hwnd, &rect_window );
-
-        *pi_width  = rect_window.right - rect_window.left;
-        *pi_height = rect_window.bottom - rect_window.top;
-        return VLC_SUCCESS;
-
     case VOUT_SET_SIZE:
-        if( p_vout->p_sys->hparent )
-            return vout_ControlWindow( p_vout,
-                    (void *)p_vout->p_sys->hparent, i_query, args );
+        if( p_vout->p_sys->parent_window )
+            return vaControlParentWindow( p_vout, i_query, args );
 
         /* Update dimensions */
         rect_window.top = rect_window.left = 0;
@@ -1069,116 +1119,86 @@ static int Control( vout_thread_t *p_vout, int i_query, va_list args )
 
         return VLC_SUCCESS;
 
-    case VOUT_CLOSE:
-        ShowWindow( p_vout->p_sys->hwnd, SW_HIDE );
-    case VOUT_REPARENT:
-        /* Retrieve the window position */
-        point.x = point.y = 0;
-        ClientToScreen( p_vout->p_sys->hwnd, &point );
-
-        HWND d = 0;
-
-        if( i_query == VOUT_REPARENT ) d = (HWND)va_arg( args, int );
-        if( !d )
-        {
-            vlc_mutex_lock( &p_vout->p_sys->lock );
-            p_vout->p_sys->hparent = 0;
-            vlc_mutex_unlock( &p_vout->p_sys->lock );
-            SetParent( p_vout->p_sys->hwnd, 0 );
-            p_vout->p_sys->i_window_style =
-                WS_CLIPCHILDREN | WS_OVERLAPPEDWINDOW | WS_SIZEBOX;
-            SetWindowLong( p_vout->p_sys->hwnd, GWL_STYLE,
-                           p_vout->p_sys->i_window_style |
-                           (i_query == VOUT_CLOSE ? 0 : WS_VISIBLE) );
-            SetWindowLong( p_vout->p_sys->hwnd, GWL_EXSTYLE, WS_EX_APPWINDOW );
-            SetWindowPos( p_vout->p_sys->hwnd, 0, point.x, point.y, 0, 0,
-                          SWP_NOSIZE|SWP_NOZORDER|SWP_FRAMECHANGED );
-        }
-        else
-        {
-            vlc_mutex_lock( &p_vout->p_sys->lock );
-            p_vout->p_sys->hparent = d;
-            vlc_mutex_unlock( &p_vout->p_sys->lock );
-
-            SetParent( p_vout->p_sys->hwnd, d );
-            p_vout->p_sys->i_window_style = WS_CLIPCHILDREN;
-            SetWindowLong( p_vout->p_sys->hwnd, GWL_STYLE,
-                           p_vout->p_sys->i_window_style |
-                           (i_query == VOUT_CLOSE ? 0 : WS_VISIBLE) );
-            SetWindowLong( p_vout->p_sys->hwnd, GWL_EXSTYLE, WS_EX_APPWINDOW );
-
-            /* Retrieve the parent size */
-            RECT  rect;
-            GetClientRect( d, &rect );
-            SetWindowPos( p_vout->p_sys->hwnd, d, point.x, point.y, rect.right, rect.bottom,
-                          SWP_FRAMECHANGED );
-        }
-
-        return vout_vaControlDefault( p_vout, i_query, args );
-
     case VOUT_SET_STAY_ON_TOP:
         if( p_vout->p_sys->hparent && !var_GetBool( p_vout, "fullscreen" ) )
-            return vout_ControlWindow( p_vout,
-                    (void *)p_vout->p_sys->hparent, i_query, args );
+            return vaControlParentWindow( p_vout, i_query, args );
 
-        p_vout->p_sys->b_on_top_change = VLC_TRUE;
+        p_vout->p_sys->b_on_top_change = true;
         return VLC_SUCCESS;
 
-#ifdef MODULE_NAME_IS_wingapi
-    case VOUT_SET_FOCUS:
-        b_bool = va_arg( args, vlc_bool_t );
-        p_vout->p_sys->b_parent_focus = b_bool;
-        if( b_bool ) GXResume();
-        else if( !p_vout->p_sys->b_focus ) GXSuspend();
-        return VLC_SUCCESS;
-#endif
-
     default:
-        return vout_vaControlDefault( p_vout, i_query, args );
+        return VLC_EGENERIC;
     }
 }
 
 
-/* Internal wrapper over GetWindowPlacement / SetWindowPlacement */ 
-static void SetWindowState(HWND hwnd, int nShowCmd)
+/* Internal wrapper over GetWindowPlacement */
+static WINDOWPLACEMENT getWindowState(HWND hwnd)
 {
     WINDOWPLACEMENT window_placement;
     window_placement.length = sizeof(WINDOWPLACEMENT);
     GetWindowPlacement( hwnd, &window_placement );
-    window_placement.showCmd = nShowCmd;
-    SetWindowPlacement( hwnd, &window_placement );
-    SetWindowPos( hwnd, 0, 0, 0, 0, 0,
-        SWP_NOMOVE|SWP_NOSIZE|SWP_NOZORDER|SWP_FRAMECHANGED);
+    return window_placement;
 }
 
 /* Internal wrapper to call vout_ControlWindow for hparent */
-static void ControlParentWindow( vout_thread_t *p_vout, int i_query, ... )
+static int vaControlParentWindow( vout_thread_t *p_vout, int i_query,
+                                   va_list args )
+{
+    switch( i_query )
+    {
+    case VOUT_SET_SIZE:
+    {
+        const unsigned i_width  = va_arg(args, unsigned);
+        const unsigned i_height = va_arg(args, unsigned);
+        return vout_window_SetSize( p_vout->p_sys->parent_window, i_width, i_height );
+    }
+    case VOUT_SET_STAY_ON_TOP:
+    {
+        const bool is_on_top = va_arg(args, int);
+        return vout_window_SetOnTop( p_vout->p_sys->parent_window, is_on_top );
+    }
+    default:
+        return VLC_EGENERIC;
+    }
+}
+
+#if 0
+static int ControlParentWindow( vout_thread_t *p_vout, int i_query, ... )
 {
     va_list args;
+    int ret;
+
     va_start( args, i_query );
-    vout_ControlWindow( p_vout,
-        (void *)p_vout->p_sys->hparent, i_query, args );
+    ret = vaControlParentWindow( p_vout, i_query, args );
     va_end( args );
+    return ret;
 }
+#endif
 
 void Win32ToggleFullscreen( vout_thread_t *p_vout )
 {
     HWND hwnd = (p_vout->p_sys->hparent && p_vout->p_sys->hfswnd) ?
         p_vout->p_sys->hfswnd : p_vout->p_sys->hwnd;
 
+    /* Save the current windows placement/placement to restore
+       when fullscreen is over */
+    WINDOWPLACEMENT window_placement = getWindowState( hwnd );
+
     p_vout->b_fullscreen = ! p_vout->b_fullscreen;
 
+    /* We want to go to Fullscreen */
     if( p_vout->b_fullscreen )
     {
         msg_Dbg( p_vout, "entering fullscreen mode" );
+
         /* Change window style, no borders and no title bar */
         int i_style = WS_CLIPCHILDREN | WS_VISIBLE;
         SetWindowLong( hwnd, GWL_STYLE, i_style );
 
         if( p_vout->p_sys->hparent )
         {
-            /* Retrieve current window position so fullscreen will happen
-            * on the right screen */
+#ifdef UNDER_CE
             POINT point = {0,0};
             RECT rect;
             ClientToScreen( p_vout->p_sys->hwnd, &point );
@@ -1186,13 +1206,30 @@ void Win32ToggleFullscreen( vout_thread_t *p_vout )
             SetWindowPos( hwnd, 0, point.x, point.y,
                           rect.right, rect.bottom,
                           SWP_NOZORDER|SWP_FRAMECHANGED );
+#else
+            /* Retrieve current window position so fullscreen will happen
+            *on the right screen */
+            HMONITOR hmon = MonitorFromWindow(p_vout->p_sys->hparent,
+                                            MONITOR_DEFAULTTONEAREST);
+            MONITORINFO mi;
+            if (GetMonitorInfo(hmon, &mi))
+            SetWindowPos( hwnd, 0,
+                            mi.rcMonitor.left,
+                            mi.rcMonitor.top,
+                            mi.rcMonitor.right - mi.rcMonitor.left,
+                            mi.rcMonitor.bottom - mi.rcMonitor.top,
+                            SWP_NOZORDER|SWP_FRAMECHANGED );
+#endif
+        }
+        else
+        {
+            /* Maximize non embedded window */
+            ShowWindow( hwnd, SW_SHOWMAXIMIZED );
         }
-
-        /* Maximize window */
-        SetWindowState( hwnd, SW_SHOWMAXIMIZED );
 
         if( p_vout->p_sys->hparent )
         {
+            /* Hide the previous window */
             RECT rect;
             GetClientRect( hwnd, &rect );
             SetParent( p_vout->p_sys->hwnd, hwnd );
@@ -1200,7 +1237,11 @@ void Win32ToggleFullscreen( vout_thread_t *p_vout )
                           rect.right, rect.bottom,
                           SWP_NOZORDER|SWP_FRAMECHANGED );
 
+#ifdef UNDER_CE
+            HWND topLevelParent = GetParent( p_vout->p_sys->hparent );
+#else
             HWND topLevelParent = GetAncestor( p_vout->p_sys->hparent, GA_ROOT );
+#endif
             ShowWindow( topLevelParent, SW_HIDE );
         }
 
@@ -1212,9 +1253,6 @@ void Win32ToggleFullscreen( vout_thread_t *p_vout )
         /* Change window style, no borders and no title bar */
         SetWindowLong( hwnd, GWL_STYLE, p_vout->p_sys->i_window_style );
 
-        /* Normal window */
-        SetWindowState( hwnd, SW_SHOWNORMAL );
-
         if( p_vout->p_sys->hparent )
         {
             RECT rect;
@@ -1224,26 +1262,142 @@ void Win32ToggleFullscreen( vout_thread_t *p_vout )
                           rect.right, rect.bottom,
                           SWP_NOZORDER|SWP_FRAMECHANGED );
 
+#ifdef UNDER_CE
+            HWND topLevelParent = GetParent( p_vout->p_sys->hparent );
+#else
             HWND topLevelParent = GetAncestor( p_vout->p_sys->hparent, GA_ROOT );
+#endif
             ShowWindow( topLevelParent, SW_SHOW );
             SetForegroundWindow( p_vout->p_sys->hparent );
             ShowWindow( hwnd, SW_HIDE );
-
-            /* Update "video-on-top" status for main interface window, it
-            needs to be updated as we were hiding VOUT_SET_STAY_ON_TOP
-            queries from it while we were in fullscreen mode */
-            int b_ontop = var_GetBool( p_vout, "video-on-top" );
-            ControlParentWindow( p_vout, VOUT_SET_STAY_ON_TOP, b_ontop );
+        }
+        else
+        {
+            /* return to normal window for non embedded vout */
+            SetWindowPlacement( hwnd, &window_placement );
+            ShowWindow( hwnd, SW_SHOWNORMAL );
         }
 
         /* Make sure the mouse cursor is displayed */
         PostMessage( p_vout->p_sys->hwnd, WM_VLC_SHOW_MOUSE, 0, 0 );
     }
 
+    /* Update the object variable and trigger callback */
+    var_SetBool( p_vout, "fullscreen", p_vout->b_fullscreen );
+}
+
+#ifndef UNDER_CE
+void DisableScreensaver( vout_thread_t *p_vout )
+{
+    /* disable screensaver by temporarily changing system settings */
+    p_vout->p_sys->i_spi_lowpowertimeout = 0;
+    p_vout->p_sys->i_spi_powerofftimeout = 0;
+    p_vout->p_sys->i_spi_screensavetimeout = 0;
+    if( var_GetBool( p_vout, "disable-screensaver" ) )
+    {
+        msg_Dbg(p_vout, "disabling screen saver");
+        SystemParametersInfo(SPI_GETLOWPOWERTIMEOUT,
+            0, &(p_vout->p_sys->i_spi_lowpowertimeout), 0);
+        if( 0 != p_vout->p_sys->i_spi_lowpowertimeout ) {
+            SystemParametersInfo(SPI_SETLOWPOWERTIMEOUT, 0, NULL, 0);
+        }
+        SystemParametersInfo(SPI_GETPOWEROFFTIMEOUT, 0,
+            &(p_vout->p_sys->i_spi_powerofftimeout), 0);
+        if( 0 != p_vout->p_sys->i_spi_powerofftimeout ) {
+            SystemParametersInfo(SPI_SETPOWEROFFTIMEOUT, 0, NULL, 0);
+        }
+        SystemParametersInfo(SPI_GETSCREENSAVETIMEOUT, 0,
+            &(p_vout->p_sys->i_spi_screensavetimeout), 0);
+        if( 0 != p_vout->p_sys->i_spi_screensavetimeout ) {
+            SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT, 0, NULL, 0);
+        }
+    }
+}
+
+void RestoreScreensaver( vout_thread_t *p_vout )
+{
+    /* restore screensaver system settings */
+    if( 0 != p_vout->p_sys->i_spi_lowpowertimeout ) {
+        SystemParametersInfo(SPI_SETLOWPOWERTIMEOUT,
+            p_vout->p_sys->i_spi_lowpowertimeout, NULL, 0);
+    }
+    if( 0 != p_vout->p_sys->i_spi_powerofftimeout ) {
+        SystemParametersInfo(SPI_SETPOWEROFFTIMEOUT,
+            p_vout->p_sys->i_spi_powerofftimeout, NULL, 0);
+    }
+    if( 0 != p_vout->p_sys->i_spi_screensavetimeout ) {
+        SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT,
+            p_vout->p_sys->i_spi_screensavetimeout, NULL, 0);
+    }
+}
+#endif
+
+int CreateEventThread( vout_thread_t *p_vout )
+{
+    /* Create the Vout EventThread, this thread is created by us to isolate
+     * the Win32 PeekMessage function calls. We want to do this because
+     * Windows can stay blocked inside this call for a long time, and when
+     * this happens it thus blocks vlc's video_output thread.
+     * Vout EventThread will take care of the creation of the video
+     * window (because PeekMessage has to be called from the same thread which
+     * created the window). */
+    msg_Dbg( p_vout, "creating Vout EventThread" );
+    p_vout->p_sys->p_event =
+        vlc_object_create( p_vout, sizeof(event_thread_t) );
+    p_vout->p_sys->p_event->p_vout = p_vout;
+    p_vout->p_sys->p_event->window_ready = CreateEvent( NULL, TRUE, FALSE, NULL );
+    if( vlc_thread_create( p_vout->p_sys->p_event, "Vout Events Thread",
+                           EventThread, 0 ) )
+    {
+        msg_Err( p_vout, "cannot create Vout EventThread" );
+        CloseHandle( p_vout->p_sys->p_event->window_ready );
+        vlc_object_release( p_vout->p_sys->p_event );
+        p_vout->p_sys->p_event = NULL;
+        return 0;
+    }
+    WaitForSingleObject( p_vout->p_sys->p_event->window_ready, INFINITE );
+    CloseHandle( p_vout->p_sys->p_event->window_ready );
+
+    if( p_vout->p_sys->p_event->b_error )
+    {
+        msg_Err( p_vout, "Vout EventThread failed" );
+        return 0;
+    }
+
+    vlc_object_attach( p_vout->p_sys->p_event, p_vout );
+
+    msg_Dbg( p_vout, "Vout EventThread running" );
+    return 1;
+}
+
+void StopEventThread( vout_thread_t *p_vout )
+{
+    if( p_vout->b_fullscreen )
     {
-        vlc_value_t val;
-        /* Update the object variable and trigger callback */
-        val.b_bool = p_vout->b_fullscreen;
-        var_Set( p_vout, "fullscreen", val );
+        msg_Dbg( p_vout, "Quitting fullscreen" );
+        Win32ToggleFullscreen( p_vout );
+        /* Force fullscreen in the core for the next video */
+        var_SetBool( p_vout, "fullscreen", true );
     }
+
+    if( p_vout->p_sys->p_event )
+    {
+        vlc_object_detach( p_vout->p_sys->p_event );
+
+        /* Kill Vout EventThread */
+        vlc_object_kill( p_vout->p_sys->p_event );
+
+        /* we need to be sure Vout EventThread won't stay stuck in
+         * GetMessage, so we send a fake message */
+        if( p_vout->p_sys->hwnd )
+        {
+            PostMessage( p_vout->p_sys->hwnd, WM_NULL, 0, 0);
+        }
+
+        vlc_thread_join( p_vout->p_sys->p_event );
+        vlc_object_release( p_vout->p_sys->p_event );
+    }
+
+    if( !( p_vout->p_sys->i_changes & SWITCHING_MODE_FLAG ) )
+        vlc_mutex_destroy( &p_vout->p_sys->lock );
 }