directx.c
上传用户:kjfoods
上传日期:2020-07-06
资源大小:29949k
文件大小:82k
源码类别:

midi

开发平台:

Unix_Linux

  1. /*****************************************************************************
  2.  * vout.c: Windows DirectX video output display method
  3.  *****************************************************************************
  4.  * Copyright (C) 2001-2009 the VideoLAN team
  5.  * $Id: cbb7eafca17135ba238cbd2fa744770c630eb6a0 $
  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.  * Preamble:
  25.  *
  26.  * This plugin will use YUV overlay if supported, using overlay will result in
  27.  * the best video quality (hardware interpolation when rescaling the picture)
  28.  * and the fastest display as it requires less processing.
  29.  *
  30.  * If YUV overlay is not supported this plugin will use RGB offscreen video
  31.  * surfaces that will be blitted onto the primary surface (display) to
  32.  * effectively display the pictures. This fallback method also enables us to
  33.  * display video in window mode.
  34.  *
  35.  *****************************************************************************/
  36. #include <errno.h>                                                 /* ENOMEM */
  37. #ifdef HAVE_CONFIG_H
  38. # include "config.h"
  39. #endif
  40. #include <vlc_common.h>
  41. #include <vlc_plugin.h>
  42. #include <vlc_interface.h>
  43. #include <vlc_playlist.h>
  44. #include <vlc_vout.h>
  45. #include <windows.h>
  46. #include <tchar.h>
  47. #include <ddraw.h>
  48. #include <commctrl.h>
  49. #ifndef UNDER_CE
  50. #   include <multimon.h>
  51. #endif
  52. #undef GetSystemMetrics
  53. #ifndef MONITOR_DEFAULTTONEAREST
  54. #   define MONITOR_DEFAULTTONEAREST 2
  55. #endif
  56. #include "vout.h"
  57. /*****************************************************************************
  58.  * picture_sys_t: direct buffer method descriptor
  59.  *****************************************************************************
  60.  * This structure is part of the picture descriptor, it describes the
  61.  * DirectX specific properties of a direct buffer.
  62.  *****************************************************************************/
  63. struct picture_sys_t
  64. {
  65.     LPDIRECTDRAWSURFACE2 p_surface;
  66.     LPDIRECTDRAWSURFACE2 p_front_surface;
  67.     DDSURFACEDESC        ddsd;
  68. };
  69. /*****************************************************************************
  70.  * DirectDraw GUIDs.
  71.  * Defining them here allows us to get rid of the dxguid library during
  72.  * the linking stage.
  73.  *****************************************************************************/
  74. #include <initguid.h>
  75. #undef GUID_EXT
  76. #define GUID_EXT
  77. DEFINE_GUID( IID_IDirectDraw2, 0xB3A6F3E0,0x2B43,0x11CF,0xA2,0xDE,0x00,0xAA,0x00,0xB9,0x33,0x56 );
  78. DEFINE_GUID( IID_IDirectDrawSurface2, 0x57805885,0x6eec,0x11cf,0x94,0x41,0xa8,0x23,0x03,0xc1,0x0e,0x27 );
  79. /*****************************************************************************
  80.  * Local prototypes.
  81.  *****************************************************************************/
  82. static int  OpenVideo  ( vlc_object_t * );
  83. static void CloseVideo ( vlc_object_t * );
  84. static int  Init      ( vout_thread_t * );
  85. static void End       ( vout_thread_t * );
  86. static int  Manage    ( vout_thread_t * );
  87. static void Display   ( vout_thread_t *, picture_t * );
  88. static void FirstDisplay( vout_thread_t *, picture_t * );
  89. static void SetPalette( vout_thread_t *, uint16_t *, uint16_t *, uint16_t * );
  90. static int  NewPictureVec  ( vout_thread_t *, picture_t *, int );
  91. static void FreePictureVec ( vout_thread_t *, picture_t *, int );
  92. static int  UpdatePictureStruct( vout_thread_t *, picture_t *, int );
  93. static int  DirectXInitDDraw      ( vout_thread_t *p_vout );
  94. static void DirectXCloseDDraw     ( vout_thread_t *p_vout );
  95. static int  DirectXCreateDisplay  ( vout_thread_t *p_vout );
  96. static void DirectXCloseDisplay   ( vout_thread_t *p_vout );
  97. static int  DirectXCreateSurface  ( vout_thread_t *p_vout,
  98.                                     LPDIRECTDRAWSURFACE2 *, int, int, int );
  99. static void DirectXCloseSurface   ( vout_thread_t *p_vout,
  100.                                     LPDIRECTDRAWSURFACE2 );
  101. static int  DirectXCreateClipper  ( vout_thread_t *p_vout );
  102. static void DirectXGetDDrawCaps   ( vout_thread_t *p_vout );
  103. static int  DirectXLockSurface    ( vout_thread_t *p_vout, picture_t *p_pic );
  104. static int  DirectXUnlockSurface  ( vout_thread_t *p_vout, picture_t *p_pic );
  105. static DWORD DirectXFindColorkey( vout_thread_t *p_vout, uint32_t *i_color );
  106. void SwitchWallpaperMode( vout_thread_t *, bool );
  107. /* Object variables callbacks */
  108. static int FindDevicesCallback( vlc_object_t *, char const *,
  109.                                 vlc_value_t, vlc_value_t, void * );
  110. static int WallpaperCallback( vlc_object_t *, char const *,
  111.                               vlc_value_t, vlc_value_t, void * );
  112. /*****************************************************************************
  113.  * Module descriptor
  114.  *****************************************************************************/
  115. #define HW_YUV_TEXT N_("Use hardware YUV->RGB conversions")
  116. #define HW_YUV_LONGTEXT N_( 
  117.     "Try to use hardware acceleration for YUV->RGB conversions. " 
  118.     "This option doesn't have any effect when using overlays." )
  119. #define SYSMEM_TEXT N_("Use video buffers in system memory")
  120. #define SYSMEM_LONGTEXT N_( 
  121.     "Create video buffers in system memory instead of video memory. This " 
  122.     "isn't recommended as usually using video memory allows to benefit from " 
  123.     "more hardware acceleration (like rescaling or YUV->RGB conversions). " 
  124.     "This option doesn't have any effect when using overlays." )
  125. #define TRIPLEBUF_TEXT N_("Use triple buffering for overlays")
  126. #define TRIPLEBUF_LONGTEXT N_( 
  127.     "Try to use triple buffering when using YUV overlays. That results in " 
  128.     "much better video quality (no flickering)." )
  129. #define DEVICE_TEXT N_("Name of desired display device")
  130. #define DEVICE_LONGTEXT N_("In a multiple monitor configuration, you can " 
  131.     "specify the Windows device name of the display that you want the video " 
  132.     "window to open on. For example, "\\.\DISPLAY1" or " 
  133.     ""\\.\DISPLAY2"." )
  134. #define WALLPAPER_TEXT N_("Enable wallpaper mode ")
  135. #define WALLPAPER_LONGTEXT N_( 
  136.     "The wallpaper mode allows you to display the video as the desktop " 
  137.     "background. Note that this feature only works in overlay mode and " 
  138.     "the desktop must not already have a wallpaper." )
  139. static const char *const ppsz_dev[] = { "" };
  140. static const char *const ppsz_dev_text[] = { N_("Default") };
  141. vlc_module_begin ()
  142.     set_shortname( "DirectX" )
  143.     set_category( CAT_VIDEO )
  144.     set_subcategory( SUBCAT_VIDEO_VOUT )
  145.     add_bool( "directx-hw-yuv", 1, NULL, HW_YUV_TEXT, HW_YUV_LONGTEXT,
  146.               true )
  147.     add_bool( "directx-use-sysmem", 0, NULL, SYSMEM_TEXT, SYSMEM_LONGTEXT,
  148.               true )
  149.     add_bool( "directx-3buffering", 1, NULL, TRIPLEBUF_TEXT,
  150.               TRIPLEBUF_LONGTEXT, true )
  151.     add_string( "directx-device", "", NULL, DEVICE_TEXT, DEVICE_LONGTEXT,
  152.                 true )
  153.         change_string_list( ppsz_dev, ppsz_dev_text, FindDevicesCallback )
  154.         change_action_add( FindDevicesCallback, N_("Refresh list") )
  155.     add_bool( "directx-wallpaper", 0, NULL, WALLPAPER_TEXT, WALLPAPER_LONGTEXT,
  156.               true )
  157.     set_description( N_("DirectX video output") )
  158.     set_capability( "video output", 100 )
  159.     add_shortcut( "directx" )
  160.     set_callbacks( OpenVideo, CloseVideo )
  161.     /* FIXME: Hack to avoid unregistering our window class */
  162.     linked_with_a_crap_library_which_uses_atexit ()
  163. vlc_module_end ()
  164. #if 0 /* FIXME */
  165.     /* check if we registered a window class because we need to
  166.      * unregister it */
  167.     WNDCLASS wndclass;
  168.     if( GetClassInfo( GetModuleHandle(NULL), "VLC DirectX", &wndclass ) )
  169.         UnregisterClass( "VLC DirectX", GetModuleHandle(NULL) );
  170. #endif
  171. /*****************************************************************************
  172.  * OpenVideo: allocate DirectX video thread output method
  173.  *****************************************************************************
  174.  * This function allocates and initialize the DirectX vout method.
  175.  *****************************************************************************/
  176. static int OpenVideo( vlc_object_t *p_this )
  177. {
  178.     vout_thread_t * p_vout = (vout_thread_t *)p_this;
  179.     vlc_value_t val;
  180.     HMODULE huser32;
  181.     /* Allocate structure */
  182.     p_vout->p_sys = calloc( 1, sizeof( vout_sys_t ) );
  183.     if( p_vout->p_sys == NULL )
  184.         return VLC_ENOMEM;
  185.     /* Initialisations */
  186.     p_vout->pf_init = Init;
  187.     p_vout->pf_end = End;
  188.     p_vout->pf_manage = Manage;
  189.     p_vout->pf_render = NULL;
  190.     p_vout->pf_display = FirstDisplay;
  191.     p_vout->p_sys->p_ddobject = NULL;
  192.     p_vout->p_sys->p_display = NULL;
  193.     p_vout->p_sys->p_current_surface = NULL;
  194.     p_vout->p_sys->p_clipper = NULL;
  195.     p_vout->p_sys->hwnd = p_vout->p_sys->hvideownd = NULL;
  196.     p_vout->p_sys->hparent = p_vout->p_sys->hfswnd = NULL;
  197.     p_vout->p_sys->i_changes = 0;
  198.     p_vout->p_sys->b_wallpaper = 0;
  199.     vlc_mutex_init( &p_vout->p_sys->lock );
  200.     SetRectEmpty( &p_vout->p_sys->rect_display );
  201.     SetRectEmpty( &p_vout->p_sys->rect_parent );
  202.     /* Multimonitor stuff */
  203.     p_vout->p_sys->hmonitor = NULL;
  204.     p_vout->p_sys->p_display_driver = NULL;
  205.     p_vout->p_sys->MonitorFromWindow = NULL;
  206.     p_vout->p_sys->GetMonitorInfo = NULL;
  207.     if( (huser32 = GetModuleHandle( _T("USER32") ) ) )
  208.     {
  209.         p_vout->p_sys->MonitorFromWindow = (HMONITOR (WINAPI *)( HWND, DWORD ))
  210.             GetProcAddress( huser32, _T("MonitorFromWindow") );
  211.         p_vout->p_sys->GetMonitorInfo =
  212. #ifndef UNICODE
  213.             GetProcAddress( huser32, "GetMonitorInfoA" );
  214. #else
  215.             GetProcAddress( huser32, _T("GetMonitorInfoW") );
  216. #endif
  217.     }
  218.     var_Create( p_vout, "overlay", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
  219.     var_Create( p_vout, "directx-use-sysmem", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
  220.     var_Create( p_vout, "directx-hw-yuv", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
  221.     var_Create( p_vout, "directx-3buffering", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
  222.     var_Create( p_vout, "directx-device", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
  223.     var_Create( p_vout, "video-title", VLC_VAR_STRING | VLC_VAR_DOINHERIT );
  224.     var_Create( p_vout, "disable-screensaver", VLC_VAR_BOOL | VLC_VAR_DOINHERIT );
  225.     p_vout->p_sys->b_cursor_hidden = 0;
  226.     p_vout->p_sys->i_lastmoved = mdate();
  227.     p_vout->p_sys->i_mouse_hide_timeout =
  228.         var_GetInteger(p_vout, "mouse-hide-timeout") * 1000;
  229.     /* Set main window's size */
  230.     p_vout->p_sys->i_window_width = p_vout->i_window_width;
  231.     p_vout->p_sys->i_window_height = p_vout->i_window_height;
  232.     /* Create the Vout EventThread, this thread is created by us to isolate
  233.      * the Win32 PeekMessage function calls. We want to do this because
  234.      * Windows can stay blocked inside this call for a long time, and when
  235.      * this happens it thus blocks vlc's video_output thread.
  236.      * DirectXEventThread will take care of the creation of the video
  237.      * window (because PeekMessage has to be called from the same thread which
  238.      * created the window). */
  239.     msg_Dbg( p_vout, "creating DirectXEventThread" );
  240.     p_vout->p_sys->p_event =
  241.         vlc_object_create( p_vout, sizeof(event_thread_t) );
  242.     p_vout->p_sys->p_event->p_vout = p_vout;
  243.     p_vout->p_sys->p_event->window_ready = CreateEvent( NULL, TRUE, FALSE, NULL );
  244.     if( vlc_thread_create( p_vout->p_sys->p_event, "Vout Events Thread",
  245.                            EventThread, 0 ) )
  246.     {
  247.         msg_Err( p_vout, "cannot create Vout EventThread" );
  248.         CloseHandle( p_vout->p_sys->p_event->window_ready );
  249.         vlc_object_release( p_vout->p_sys->p_event );
  250.         p_vout->p_sys->p_event = NULL;
  251.         goto error;
  252.     }
  253.     WaitForSingleObject( p_vout->p_sys->p_event->window_ready, INFINITE );
  254.     CloseHandle( p_vout->p_sys->p_event->window_ready );
  255.     if( p_vout->p_sys->p_event->b_error )
  256.     {
  257.         msg_Err( p_vout, "Vout EventThread failed" );
  258.         goto error;
  259.     }
  260.     vlc_object_attach( p_vout->p_sys->p_event, p_vout );
  261.     msg_Dbg( p_vout, "Vout EventThread running" );
  262.     /* Initialise DirectDraw */
  263.     if( DirectXInitDDraw( p_vout ) )
  264.     {
  265.         msg_Err( p_vout, "cannot initialize DirectX DirectDraw" );
  266.         goto error;
  267.     }
  268.     /* Create the directx display */
  269.     if( DirectXCreateDisplay( p_vout ) )
  270.     {
  271.         msg_Err( p_vout, "cannot initialize DirectX DirectDraw" );
  272.         goto error;
  273.     }
  274.     /* Variable to indicate if the window should be on top of others */
  275.     /* Trigger a callback right now */
  276.     var_TriggerCallback( p_vout, "video-on-top" );
  277.     /* Variable to indicate if the window should be on top of others */
  278.     /* Trigger a callback right now */
  279.     var_Create( p_vout, "directx-wallpaper", VLC_VAR_BOOL|VLC_VAR_DOINHERIT );
  280.     val.psz_string = _("Wallpaper");
  281.     var_Change( p_vout, "directx-wallpaper", VLC_VAR_SETTEXT, &val, NULL );
  282.     var_AddCallback( p_vout, "directx-wallpaper", WallpaperCallback, NULL );
  283.     var_TriggerCallback( p_vout, "directx-wallpaper" );
  284.     /* disable screensaver by temporarily changing system settings */
  285.     p_vout->p_sys->i_spi_lowpowertimeout = 0;
  286.     p_vout->p_sys->i_spi_powerofftimeout = 0;
  287.     p_vout->p_sys->i_spi_screensavetimeout = 0;
  288.     if( var_GetBool( p_vout, "disable-screensaver" ) ) {
  289.         msg_Dbg(p_vout, "disabling screen saver");
  290.         SystemParametersInfo(SPI_GETLOWPOWERTIMEOUT,
  291.             0, &(p_vout->p_sys->i_spi_lowpowertimeout), 0);
  292.         if( 0 != p_vout->p_sys->i_spi_lowpowertimeout ) {
  293.             SystemParametersInfo(SPI_SETLOWPOWERTIMEOUT, 0, NULL, 0);
  294.         }
  295.         SystemParametersInfo(SPI_GETPOWEROFFTIMEOUT, 0,
  296.             &(p_vout->p_sys->i_spi_powerofftimeout), 0);
  297.         if( 0 != p_vout->p_sys->i_spi_powerofftimeout ) {
  298.             SystemParametersInfo(SPI_SETPOWEROFFTIMEOUT, 0, NULL, 0);
  299.         }
  300.         SystemParametersInfo(SPI_GETSCREENSAVETIMEOUT, 0,
  301.             &(p_vout->p_sys->i_spi_screensavetimeout), 0);
  302.         if( 0 != p_vout->p_sys->i_spi_screensavetimeout ) {
  303.             SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT, 0, NULL, 0);
  304.         }
  305.     }
  306.     return VLC_SUCCESS;
  307.  error:
  308.     CloseVideo( VLC_OBJECT(p_vout) );
  309.     return VLC_EGENERIC;
  310. }
  311. /*****************************************************************************
  312.  * Init: initialize DirectX video thread output method
  313.  *****************************************************************************
  314.  * This function create the directx surfaces needed by the output thread.
  315.  * It is called at the beginning of the thread.
  316.  *****************************************************************************/
  317. static int Init( vout_thread_t *p_vout )
  318. {
  319.     int i_chroma_backup;
  320.     /* Get a few default parameters */
  321.     p_vout->p_sys->b_using_overlay = var_GetBool( p_vout, "overlay" );
  322.     p_vout->p_sys->b_use_sysmem = var_GetBool( p_vout, "directx-use-sysmem" );
  323.     p_vout->p_sys->b_hw_yuv = var_GetBool( p_vout, "directx-hw-yuv" );
  324.     p_vout->p_sys->b_3buf_overlay = var_GetBool( p_vout, "directx-3buffering" );
  325.     /* Initialise DirectDraw if not already done.
  326.      * We do this here because on multi-monitor systems we may have to
  327.      * re-create the directdraw surfaces. */
  328.     if( !p_vout->p_sys->p_ddobject &&
  329.         DirectXInitDDraw( p_vout ) != VLC_SUCCESS )
  330.     {
  331.         msg_Err( p_vout, "cannot initialize DirectDraw" );
  332.         return VLC_EGENERIC;
  333.     }
  334.     /* Create the directx display */
  335.     if( !p_vout->p_sys->p_display &&
  336.         DirectXCreateDisplay( p_vout ) != VLC_SUCCESS )
  337.     {
  338.         msg_Err( p_vout, "cannot initialize DirectDraw" );
  339.         return VLC_EGENERIC;
  340.     }
  341.     /* Initialize the output structure.
  342.      * Since DirectDraw can do rescaling for us, stick to the default
  343.      * coordinates and aspect. */
  344.     p_vout->output.i_width  = p_vout->render.i_width;
  345.     p_vout->output.i_height = p_vout->render.i_height;
  346.     p_vout->output.i_aspect = p_vout->render.i_aspect;
  347.     p_vout->fmt_out = p_vout->fmt_in;
  348.     UpdateRects( p_vout, true );
  349. #define MAX_DIRECTBUFFERS 1
  350.     /* Right now we use only 1 directbuffer because we don't want the
  351.      * video decoder to decode directly into direct buffers as they are
  352.      * created into video memory and video memory is _really_ slow */
  353.     /* Choose the chroma we will try first. */
  354.     switch( p_vout->render.i_chroma )
  355.     {
  356.         case VLC_FOURCC('Y','U','Y','2'):
  357.         case VLC_FOURCC('Y','U','N','V'):
  358.             p_vout->output.i_chroma = VLC_FOURCC('Y','U','Y','2');
  359.             break;
  360.         case VLC_FOURCC('U','Y','V','Y'):
  361.         case VLC_FOURCC('U','Y','N','V'):
  362.         case VLC_FOURCC('Y','4','2','2'):
  363.             p_vout->output.i_chroma = VLC_FOURCC('U','Y','V','Y');
  364.             break;
  365.         case VLC_FOURCC('Y','V','Y','U'):
  366.             p_vout->output.i_chroma = VLC_FOURCC('Y','V','Y','U');
  367.             break;
  368.         default:
  369.             p_vout->output.i_chroma = VLC_FOURCC('Y','V','1','2');
  370.             break;
  371.     }
  372.     NewPictureVec( p_vout, p_vout->p_picture, MAX_DIRECTBUFFERS );
  373.     i_chroma_backup = p_vout->output.i_chroma;
  374.     if( !I_OUTPUTPICTURES )
  375.     {
  376.         /* hmmm, it didn't work! Let's try commonly supported chromas */
  377.         if( p_vout->output.i_chroma != VLC_FOURCC('I','4','2','0') )
  378.         {
  379.             p_vout->output.i_chroma = VLC_FOURCC('Y','V','1','2');
  380.             NewPictureVec( p_vout, p_vout->p_picture, MAX_DIRECTBUFFERS );
  381.         }
  382.         if( !I_OUTPUTPICTURES )
  383.         {
  384.             /* hmmm, it still didn't work! Let's try another one */
  385.             p_vout->output.i_chroma = VLC_FOURCC('Y','U','Y','2');
  386.             NewPictureVec( p_vout, p_vout->p_picture, MAX_DIRECTBUFFERS );
  387.         }
  388.     }
  389.     if( !I_OUTPUTPICTURES )
  390.     {
  391.         /* If it still didn't work then don't try to use an overlay */
  392.         p_vout->output.i_chroma = i_chroma_backup;
  393.         p_vout->p_sys->b_using_overlay = 0;
  394.         msg_Warn( p_vout, "Could not initialize directx overlay" ) ;
  395.         NewPictureVec( p_vout, p_vout->p_picture, MAX_DIRECTBUFFERS );
  396.     }
  397.     /* Change the window title bar text */
  398.     PostMessage( p_vout->p_sys->hwnd, WM_VLC_CHANGE_TEXT, 0, 0 );
  399.     p_vout->fmt_out.i_chroma = p_vout->output.i_chroma;
  400.     return VLC_SUCCESS;
  401. }
  402. /*****************************************************************************
  403.  * End: terminate Sys video thread output method
  404.  *****************************************************************************
  405.  * Terminate an output method created by Create.
  406.  * It is called at the end of the thread.
  407.  *****************************************************************************/
  408. static void End( vout_thread_t *p_vout )
  409. {
  410.     FreePictureVec( p_vout, p_vout->p_picture, I_OUTPUTPICTURES );
  411.     DirectXCloseDisplay( p_vout );
  412.     DirectXCloseDDraw( p_vout );
  413.     return;
  414. }
  415. /*****************************************************************************
  416.  * CloseVideo: destroy Sys video thread output method
  417.  *****************************************************************************
  418.  * Terminate an output method created by Create
  419.  *****************************************************************************/
  420. static void CloseVideo( vlc_object_t *p_this )
  421. {
  422.     vout_thread_t * p_vout = (vout_thread_t *)p_this;
  423.     if( p_vout->b_fullscreen )
  424.     {
  425.         msg_Dbg( p_vout, "Quitting fullscreen" );
  426.         Win32ToggleFullscreen( p_vout );
  427.         /* Force fullscreen in the core for the next video */
  428.         var_SetBool( p_vout, "fullscreen", true );
  429.     }
  430.     if( p_vout->p_sys->p_event )
  431.     {
  432.         vlc_object_detach( p_vout->p_sys->p_event );
  433.         /* Kill Vout EventThread */
  434.         vlc_object_kill( p_vout->p_sys->p_event );
  435.         /* we need to be sure Vout EventThread won't stay stuck in
  436.          * GetMessage, so we send a fake message */
  437.         if( p_vout->p_sys->hwnd )
  438.         {
  439.             PostMessage( p_vout->p_sys->hwnd, WM_NULL, 0, 0);
  440.         }
  441.         vlc_thread_join( p_vout->p_sys->p_event );
  442.         vlc_object_release( p_vout->p_sys->p_event );
  443.     }
  444.     vlc_mutex_destroy( &p_vout->p_sys->lock );
  445.     /* Make sure the wallpaper is restored */
  446.     var_DelCallback( p_vout, "directx-wallpaper", WallpaperCallback, NULL );
  447.     SwitchWallpaperMode( p_vout, false );
  448.     /* restore screensaver system settings */
  449.     if( 0 != p_vout->p_sys->i_spi_lowpowertimeout ) {
  450.         SystemParametersInfo(SPI_SETLOWPOWERTIMEOUT,
  451.             p_vout->p_sys->i_spi_lowpowertimeout, NULL, 0);
  452.     }
  453.     if( 0 != p_vout->p_sys->i_spi_powerofftimeout ) {
  454.         SystemParametersInfo(SPI_SETPOWEROFFTIMEOUT,
  455.             p_vout->p_sys->i_spi_powerofftimeout, NULL, 0);
  456.     }
  457.     if( 0 != p_vout->p_sys->i_spi_screensavetimeout ) {
  458.         SystemParametersInfo(SPI_SETSCREENSAVETIMEOUT,
  459.             p_vout->p_sys->i_spi_screensavetimeout, NULL, 0);
  460.     }
  461.     free( p_vout->p_sys );
  462.     p_vout->p_sys = NULL;
  463. }
  464. /*****************************************************************************
  465.  * Manage: handle Sys events
  466.  *****************************************************************************
  467.  * This function should be called regularly by the video output thread.
  468.  * It returns a non null value if an error occurred.
  469.  *****************************************************************************/
  470. static int Manage( vout_thread_t *p_vout )
  471. {
  472.     /* If we do not control our window, we check for geometry changes
  473.      * ourselves because the parent might not send us its events. */
  474.     vlc_mutex_lock( &p_vout->p_sys->lock );
  475.     if( p_vout->p_sys->hparent && !p_vout->b_fullscreen )
  476.     {
  477.         RECT rect_parent;
  478.         POINT point;
  479.         vlc_mutex_unlock( &p_vout->p_sys->lock );
  480.         GetClientRect( p_vout->p_sys->hparent, &rect_parent );
  481.         point.x = point.y = 0;
  482.         ClientToScreen( p_vout->p_sys->hparent, &point );
  483.         OffsetRect( &rect_parent, point.x, point.y );
  484.         if( !EqualRect( &rect_parent, &p_vout->p_sys->rect_parent ) )
  485.         {
  486.             p_vout->p_sys->rect_parent = rect_parent;
  487.             /* This one is to force the update even if only
  488.              * the position has changed */
  489.             SetWindowPos( p_vout->p_sys->hwnd, 0, 1, 1,
  490.                           rect_parent.right - rect_parent.left,
  491.                           rect_parent.bottom - rect_parent.top, 0 );
  492.             SetWindowPos( p_vout->p_sys->hwnd, 0, 0, 0,
  493.                           rect_parent.right - rect_parent.left,
  494.                           rect_parent.bottom - rect_parent.top, 0 );
  495.         }
  496.     }
  497.     else
  498.     {
  499.         vlc_mutex_unlock( &p_vout->p_sys->lock );
  500.     }
  501.     /*
  502.      * Position Change
  503.      */
  504.     if( p_vout->p_sys->i_changes & DX_POSITION_CHANGE )
  505.     {
  506.         p_vout->p_sys->i_changes &= ~DX_POSITION_CHANGE;
  507.         /* Check if we are still on the same monitor */
  508.         if( p_vout->p_sys->MonitorFromWindow &&
  509.             p_vout->p_sys->hmonitor !=
  510.                 p_vout->p_sys->MonitorFromWindow( p_vout->p_sys->hwnd,
  511.                                                   MONITOR_DEFAULTTONEAREST ) )
  512.         {
  513.             /* This will force the vout core to recreate the picture buffers */
  514.             p_vout->i_changes |= VOUT_PICTURE_BUFFERS_CHANGE;
  515.         }
  516.     }
  517.     /* autoscale toggle */
  518.     if( p_vout->i_changes & VOUT_SCALE_CHANGE )
  519.     {
  520.         p_vout->i_changes &= ~VOUT_SCALE_CHANGE;
  521.         p_vout->b_autoscale = var_GetBool( p_vout, "autoscale" );
  522.         p_vout->i_zoom = (int) ZOOM_FP_FACTOR;
  523.         UpdateRects( p_vout, true );
  524.     }
  525.     /* scaling factor */
  526.     if( p_vout->i_changes & VOUT_ZOOM_CHANGE )
  527.     {
  528.         p_vout->i_changes &= ~VOUT_ZOOM_CHANGE;
  529.         p_vout->b_autoscale = false;
  530.         p_vout->i_zoom =
  531.             (int)( ZOOM_FP_FACTOR * var_GetFloat( p_vout, "scale" ) );
  532.         UpdateRects( p_vout, true );
  533.     }
  534.     /* Check for cropping / aspect changes */
  535.     if( p_vout->i_changes & VOUT_CROP_CHANGE ||
  536.         p_vout->i_changes & VOUT_ASPECT_CHANGE )
  537.     {
  538.         p_vout->i_changes &= ~VOUT_CROP_CHANGE;
  539.         p_vout->i_changes &= ~VOUT_ASPECT_CHANGE;
  540.         p_vout->fmt_out.i_x_offset = p_vout->fmt_in.i_x_offset;
  541.         p_vout->fmt_out.i_y_offset = p_vout->fmt_in.i_y_offset;
  542.         p_vout->fmt_out.i_visible_width = p_vout->fmt_in.i_visible_width;
  543.         p_vout->fmt_out.i_visible_height = p_vout->fmt_in.i_visible_height;
  544.         p_vout->fmt_out.i_aspect = p_vout->fmt_in.i_aspect;
  545.         p_vout->fmt_out.i_sar_num = p_vout->fmt_in.i_sar_num;
  546.         p_vout->fmt_out.i_sar_den = p_vout->fmt_in.i_sar_den;
  547.         p_vout->output.i_aspect = p_vout->fmt_in.i_aspect;
  548.         UpdateRects( p_vout, true );
  549.     }
  550.     /* We used to call the Win32 PeekMessage function here to read the window
  551.      * messages. But since window can stay blocked into this function for a
  552.      * long time (for example when you move your window on the screen), I
  553.      * decided to isolate PeekMessage in another thread. */
  554.     if( p_vout->p_sys->i_changes & DX_WALLPAPER_CHANGE )
  555.     {
  556.         SwitchWallpaperMode( p_vout, !p_vout->p_sys->b_wallpaper );
  557.         p_vout->p_sys->i_changes &= ~DX_WALLPAPER_CHANGE;
  558.         DirectDrawUpdateOverlay( p_vout );
  559.     }
  560.     /*
  561.      * Fullscreen change
  562.      */
  563.     if( p_vout->i_changes & VOUT_FULLSCREEN_CHANGE
  564.         || p_vout->p_sys->i_changes & VOUT_FULLSCREEN_CHANGE )
  565.     {
  566.         Win32ToggleFullscreen( p_vout );
  567.         p_vout->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
  568.         p_vout->p_sys->i_changes &= ~VOUT_FULLSCREEN_CHANGE;
  569.     }
  570.     /*
  571.      * Pointer change
  572.      */
  573.     if( p_vout->b_fullscreen && !p_vout->p_sys->b_cursor_hidden &&
  574.         (mdate() - p_vout->p_sys->i_lastmoved) >
  575.             p_vout->p_sys->i_mouse_hide_timeout )
  576.     {
  577.         POINT point;
  578.         HWND hwnd;
  579.         /* Hide the cursor only if it is inside our window */
  580.         GetCursorPos( &point );
  581.         hwnd = WindowFromPoint(point);
  582.         if( hwnd == p_vout->p_sys->hwnd || hwnd == p_vout->p_sys->hvideownd )
  583.         {
  584.             PostMessage( p_vout->p_sys->hwnd, WM_VLC_HIDE_MOUSE, 0, 0 );
  585.         }
  586.         else
  587.         {
  588.             p_vout->p_sys->i_lastmoved = mdate();
  589.         }
  590.     }
  591.     /*
  592.      * "Always on top" status change
  593.      */
  594.     if( p_vout->p_sys->b_on_top_change )
  595.     {
  596.         vlc_value_t val;
  597.         HMENU hMenu = GetSystemMenu( p_vout->p_sys->hwnd, FALSE );
  598.         var_Get( p_vout, "video-on-top", &val );
  599.         /* Set the window on top if necessary */
  600.         if( val.b_bool && !( GetWindowLong( p_vout->p_sys->hwnd, GWL_EXSTYLE )
  601.                            & WS_EX_TOPMOST ) )
  602.         {
  603.             CheckMenuItem( hMenu, IDM_TOGGLE_ON_TOP,
  604.                            MF_BYCOMMAND | MFS_CHECKED );
  605.             SetWindowPos( p_vout->p_sys->hwnd, HWND_TOPMOST, 0, 0, 0, 0,
  606.                           SWP_NOSIZE | SWP_NOMOVE );
  607.         }
  608.         else
  609.         /* The window shouldn't be on top */
  610.         if( !val.b_bool && ( GetWindowLong( p_vout->p_sys->hwnd, GWL_EXSTYLE )
  611.                            & WS_EX_TOPMOST ) )
  612.         {
  613.             CheckMenuItem( hMenu, IDM_TOGGLE_ON_TOP,
  614.                            MF_BYCOMMAND | MFS_UNCHECKED );
  615.             SetWindowPos( p_vout->p_sys->hwnd, HWND_NOTOPMOST, 0, 0, 0, 0,
  616.                           SWP_NOSIZE | SWP_NOMOVE );
  617.         }
  618.         p_vout->p_sys->b_on_top_change = false;
  619.     }
  620.     /* Check if the event thread is still running */
  621.     if( !vlc_object_alive (p_vout->p_sys->p_event) )
  622.     {
  623.         return VLC_EGENERIC; /* exit */
  624.     }
  625.     return VLC_SUCCESS;
  626. }
  627. /*****************************************************************************
  628.  * Display: displays previously rendered output
  629.  *****************************************************************************
  630.  * This function sends the currently rendered image to the display, wait until
  631.  * it is displayed and switch the two rendering buffers, preparing next frame.
  632.  *****************************************************************************/
  633. static void Display( vout_thread_t *p_vout, picture_t *p_pic )
  634. {
  635.     HRESULT dxresult;
  636.     if( (p_vout->p_sys->p_display == NULL) )
  637.     {
  638.         msg_Warn( p_vout, "no display!" );
  639.         return;
  640.     }
  641.     /* Our surface can be lost so be sure to check this
  642.      * and restore it if need be */
  643.     if( IDirectDrawSurface2_IsLost( p_vout->p_sys->p_display )
  644.         == DDERR_SURFACELOST )
  645.     {
  646.         if( IDirectDrawSurface2_Restore( p_vout->p_sys->p_display ) == DD_OK &&
  647.             p_vout->p_sys->b_using_overlay )
  648.             DirectDrawUpdateOverlay( p_vout );
  649.     }
  650.     if( !p_vout->p_sys->b_using_overlay )
  651.     {
  652.         DDBLTFX  ddbltfx;
  653.         /* We ask for the "NOTEARING" option */
  654.         memset( &ddbltfx, 0, sizeof(DDBLTFX) );
  655.         ddbltfx.dwSize = sizeof(DDBLTFX);
  656.         ddbltfx.dwDDFX = DDBLTFX_NOTEARING;
  657.         /* Blit video surface to display */
  658.         dxresult = IDirectDrawSurface2_Blt( p_vout->p_sys->p_display,
  659.                                             &p_vout->p_sys->rect_dest_clipped,
  660.                                             p_pic->p_sys->p_surface,
  661.                                             &p_vout->p_sys->rect_src_clipped,
  662.                                             DDBLT_ASYNC, &ddbltfx );
  663.         if( dxresult != DD_OK )
  664.         {
  665.             msg_Warn( p_vout, "could not blit surface (error %li)", dxresult );
  666.             return;
  667.         }
  668.     }
  669.     else /* using overlay */
  670.     {
  671.         /* Flip the overlay buffers if we are using back buffers */
  672.         if( p_pic->p_sys->p_front_surface == p_pic->p_sys->p_surface )
  673.         {
  674.             return;
  675.         }
  676.         dxresult = IDirectDrawSurface2_Flip( p_pic->p_sys->p_front_surface,
  677.                                              NULL, DDFLIP_WAIT );
  678.         if( dxresult != DD_OK )
  679.         {
  680.             msg_Warn( p_vout, "could not flip overlay (error %li)", dxresult );
  681.         }
  682.         /* set currently displayed pic */
  683.         p_vout->p_sys->p_current_surface = p_pic->p_sys->p_front_surface;
  684.         /* Lock surface to get all the required info */
  685.         if( DirectXLockSurface( p_vout, p_pic ) )
  686.         {
  687.             /* AAARRGG */
  688.             msg_Warn( p_vout, "cannot lock surface" );
  689.             return;
  690.         }
  691.         DirectXUnlockSurface( p_vout, p_pic );
  692.     }
  693. }
  694. /*
  695. ** this function is only used once when the first picture is received
  696. ** this function will show the video window once video is ready for
  697. ** display.
  698. */
  699. static void FirstDisplay( vout_thread_t *p_vout, picture_t *p_pic )
  700. {
  701.     /* get initial picture rendered on overlay surface */
  702.     Display(p_vout, p_pic);
  703.     IDirectDraw_WaitForVerticalBlank(p_vout->p_sys->p_ddobject,
  704.             DDWAITVB_BLOCKBEGIN, NULL);
  705.     if( p_vout->p_sys->b_using_overlay )
  706.     {
  707.         /* set the colorkey as the backgound brush for the video window */
  708.         SetClassLong( p_vout->p_sys->hvideownd, GCL_HBRBACKGROUND,
  709.                       (LONG)CreateSolidBrush( p_vout->p_sys->i_rgb_colorkey ) );
  710.     }
  711.     /*
  712.     ** Video window is initially hidden, show it now since we got a
  713.     ** picture to show.
  714.     */
  715.     SetWindowPos( p_vout->p_sys->hvideownd, NULL, 0, 0, 0, 0,
  716.         SWP_ASYNCWINDOWPOS|
  717.         SWP_FRAMECHANGED|
  718.         SWP_SHOWWINDOW|
  719.         SWP_NOMOVE|
  720.         SWP_NOSIZE|
  721.         SWP_NOZORDER );
  722.     /* use and restores proper display function for further pictures */
  723.     p_vout->pf_display = Display;
  724. }
  725. /* following functions are local */
  726. /*****************************************************************************
  727.  * DirectXEnumCallback: Device enumeration
  728.  *****************************************************************************
  729.  * This callback function is called by DirectDraw once for each
  730.  * available DirectDraw device.
  731.  *****************************************************************************/
  732. BOOL WINAPI DirectXEnumCallback( GUID* p_guid, LPTSTR psz_desc,
  733.                                  LPTSTR psz_drivername, VOID* p_context,
  734.                                  HMONITOR hmon )
  735. {
  736.     vout_thread_t *p_vout = (vout_thread_t *)p_context;
  737.     vlc_value_t device;
  738.     msg_Dbg( p_vout, "DirectXEnumCallback: %s, %s", psz_desc, psz_drivername );
  739.     if( hmon )
  740.     {
  741.         var_Get( p_vout, "directx-device", &device );
  742.         if( ( !device.psz_string || !*device.psz_string ) &&
  743.             hmon == p_vout->p_sys->hmonitor )
  744.         {
  745.             free( device.psz_string );
  746.         }
  747.         else if( strcmp( psz_drivername, device.psz_string ) == 0 )
  748.         {
  749.             MONITORINFO monitor_info;
  750.             monitor_info.cbSize = sizeof( MONITORINFO );
  751.             if( p_vout->p_sys->GetMonitorInfo( hmon, &monitor_info ) )
  752.             {
  753.                 RECT rect;
  754.                 /* Move window to the right screen */
  755.                 GetWindowRect( p_vout->p_sys->hwnd, &rect );
  756.                 if( !IntersectRect( &rect, &rect, &monitor_info.rcWork ) )
  757.                 {
  758.                     rect.left = monitor_info.rcWork.left;
  759.                     rect.top = monitor_info.rcWork.top;
  760.                     msg_Dbg( p_vout, "DirectXEnumCallback: setting window "
  761.                              "position to %ld,%ld", rect.left, rect.top );
  762.                     SetWindowPos( p_vout->p_sys->hwnd, NULL,
  763.                                   rect.left, rect.top, 0, 0,
  764.                                   SWP_NOSIZE | SWP_NOZORDER | SWP_NOACTIVATE );
  765.                 }
  766.             }
  767.             p_vout->p_sys->hmonitor = hmon;
  768.             free( device.psz_string );
  769.         }
  770.         else
  771.         {
  772.             free( device.psz_string );
  773.             return TRUE; /* Keep enumerating */
  774.         }
  775.         msg_Dbg( p_vout, "selecting %s, %s", psz_desc, psz_drivername );
  776.         p_vout->p_sys->p_display_driver = malloc( sizeof(GUID) );
  777.         if( p_vout->p_sys->p_display_driver )
  778.             memcpy( p_vout->p_sys->p_display_driver, p_guid, sizeof(GUID) );
  779.     }
  780.     return TRUE; /* Keep enumerating */
  781. }
  782. /*****************************************************************************
  783.  * DirectXInitDDraw: Takes care of all the DirectDraw initialisations
  784.  *****************************************************************************
  785.  * This function initialise and allocate resources for DirectDraw.
  786.  *****************************************************************************/
  787. static int DirectXInitDDraw( vout_thread_t *p_vout )
  788. {
  789.     HRESULT dxresult;
  790.     HRESULT (WINAPI *OurDirectDrawCreate)(GUID *,LPDIRECTDRAW *,IUnknown *);
  791.     HRESULT (WINAPI *OurDirectDrawEnumerateEx)( LPDDENUMCALLBACKEXA, LPVOID,
  792.                                                 DWORD );
  793.     LPDIRECTDRAW p_ddobject;
  794.     msg_Dbg( p_vout, "DirectXInitDDraw" );
  795.     /* Load direct draw DLL */
  796.     p_vout->p_sys->hddraw_dll = LoadLibrary(_T("DDRAW.DLL"));
  797.     if( p_vout->p_sys->hddraw_dll == NULL )
  798.     {
  799.         msg_Warn( p_vout, "DirectXInitDDraw failed loading ddraw.dll" );
  800.         goto error;
  801.     }
  802.     OurDirectDrawCreate =
  803.       (void *)GetProcAddress( p_vout->p_sys->hddraw_dll,
  804.                               _T("DirectDrawCreate") );
  805.     if( OurDirectDrawCreate == NULL )
  806.     {
  807.         msg_Err( p_vout, "DirectXInitDDraw failed GetProcAddress" );
  808.         goto error;
  809.     }
  810.     OurDirectDrawEnumerateEx =
  811.       (void *)GetProcAddress( p_vout->p_sys->hddraw_dll,
  812. #ifndef UNICODE
  813.                               "DirectDrawEnumerateExA" );
  814. #else
  815.                               _T("DirectDrawEnumerateExW") );
  816. #endif
  817.     if( OurDirectDrawEnumerateEx && p_vout->p_sys->MonitorFromWindow )
  818.     {
  819.         vlc_value_t device;
  820.         var_Get( p_vout, "directx-device", &device );
  821.         if( device.psz_string )
  822.         {
  823.             msg_Dbg( p_vout, "directx-device: %s", device.psz_string );
  824.             free( device.psz_string );
  825.         }
  826.         p_vout->p_sys->hmonitor =
  827.             p_vout->p_sys->MonitorFromWindow( p_vout->p_sys->hwnd,
  828.                                               MONITOR_DEFAULTTONEAREST );
  829.         /* Enumerate displays */
  830.         OurDirectDrawEnumerateEx( DirectXEnumCallback, p_vout,
  831.                                   DDENUM_ATTACHEDSECONDARYDEVICES );
  832.     }
  833.     /* Initialize DirectDraw now */
  834.     dxresult = OurDirectDrawCreate( p_vout->p_sys->p_display_driver,
  835.                                     &p_ddobject, NULL );
  836.     if( dxresult != DD_OK )
  837.     {
  838.         msg_Err( p_vout, "DirectXInitDDraw cannot initialize DDraw" );
  839.         goto error;
  840.     }
  841.     /* Get the IDirectDraw2 interface */
  842.     dxresult = IDirectDraw_QueryInterface( p_ddobject, &IID_IDirectDraw2,
  843.                                         (LPVOID *)&p_vout->p_sys->p_ddobject );
  844.     /* Release the unused interface */
  845.     IDirectDraw_Release( p_ddobject );
  846.     if( dxresult != DD_OK )
  847.     {
  848.         msg_Err( p_vout, "cannot get IDirectDraw2 interface" );
  849.         goto error;
  850.     }
  851.     /* Set DirectDraw Cooperative level, ie what control we want over Windows
  852.      * display */
  853.     dxresult = IDirectDraw2_SetCooperativeLevel( p_vout->p_sys->p_ddobject,
  854.                                                  NULL, DDSCL_NORMAL );
  855.     if( dxresult != DD_OK )
  856.     {
  857.         msg_Err( p_vout, "cannot set direct draw cooperative level" );
  858.         goto error;
  859.     }
  860.     /* Get the size of the current display device */
  861.     if( p_vout->p_sys->hmonitor && p_vout->p_sys->GetMonitorInfo )
  862.     {
  863.         MONITORINFO monitor_info;
  864.         monitor_info.cbSize = sizeof( MONITORINFO );
  865.         p_vout->p_sys->GetMonitorInfo( p_vout->p_sys->hmonitor,
  866.                                        &monitor_info );
  867.         p_vout->p_sys->rect_display = monitor_info.rcMonitor;
  868.     }
  869.     else
  870.     {
  871.         p_vout->p_sys->rect_display.left = 0;
  872.         p_vout->p_sys->rect_display.top = 0;
  873.         p_vout->p_sys->rect_display.right  = GetSystemMetrics(SM_CXSCREEN);
  874.         p_vout->p_sys->rect_display.bottom = GetSystemMetrics(SM_CYSCREEN);
  875.     }
  876.     msg_Dbg( p_vout, "screen dimensions (%lix%li,%lix%li)",
  877.              p_vout->p_sys->rect_display.left,
  878.              p_vout->p_sys->rect_display.top,
  879.              p_vout->p_sys->rect_display.right,
  880.              p_vout->p_sys->rect_display.bottom );
  881.     /* Probe the capabilities of the hardware */
  882.     DirectXGetDDrawCaps( p_vout );
  883.     msg_Dbg( p_vout, "End DirectXInitDDraw" );
  884.     return VLC_SUCCESS;
  885.  error:
  886.     if( p_vout->p_sys->p_ddobject )
  887.         IDirectDraw2_Release( p_vout->p_sys->p_ddobject );
  888.     if( p_vout->p_sys->hddraw_dll )
  889.         FreeLibrary( p_vout->p_sys->hddraw_dll );
  890.     p_vout->p_sys->hddraw_dll = NULL;
  891.     p_vout->p_sys->p_ddobject = NULL;
  892.     return VLC_EGENERIC;
  893. }
  894. /*****************************************************************************
  895.  * DirectXCreateDisplay: create the DirectDraw display.
  896.  *****************************************************************************
  897.  * Create and initialize display according to preferences specified in the vout
  898.  * thread fields.
  899.  *****************************************************************************/
  900. static int DirectXCreateDisplay( vout_thread_t *p_vout )
  901. {
  902.     HRESULT              dxresult;
  903.     DDSURFACEDESC        ddsd;
  904.     LPDIRECTDRAWSURFACE  p_display;
  905.     msg_Dbg( p_vout, "DirectXCreateDisplay" );
  906.     /* Now get the primary surface. This surface is what you actually see
  907.      * on your screen */
  908.     memset( &ddsd, 0, sizeof( DDSURFACEDESC ));
  909.     ddsd.dwSize = sizeof(DDSURFACEDESC);
  910.     ddsd.dwFlags = DDSD_CAPS;
  911.     ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
  912.     dxresult = IDirectDraw2_CreateSurface( p_vout->p_sys->p_ddobject,
  913.                                            &ddsd, &p_display, NULL );
  914.     if( dxresult != DD_OK )
  915.     {
  916.         msg_Err( p_vout, "cannot get primary surface (error %li)", dxresult );
  917.         return VLC_EGENERIC;
  918.     }
  919.     dxresult = IDirectDrawSurface_QueryInterface( p_display,
  920.                                          &IID_IDirectDrawSurface2,
  921.                                          (LPVOID *)&p_vout->p_sys->p_display );
  922.     /* Release the old interface */
  923.     IDirectDrawSurface_Release( p_display );
  924.     if ( dxresult != DD_OK )
  925.     {
  926.         msg_Err( p_vout, "cannot query IDirectDrawSurface2 interface "
  927.                          "(error %li)", dxresult );
  928.         return VLC_EGENERIC;
  929.     }
  930.     /* The clipper will be used only in non-overlay mode */
  931.     DirectXCreateClipper( p_vout );
  932.     /* Make sure the colorkey will be painted */
  933.     p_vout->p_sys->i_colorkey = 1;
  934.     p_vout->p_sys->i_rgb_colorkey =
  935.         DirectXFindColorkey( p_vout, &p_vout->p_sys->i_colorkey );
  936.     UpdateRects( p_vout, true );
  937.     return VLC_SUCCESS;
  938. }
  939. /*****************************************************************************
  940.  * DirectXCreateClipper: Create a clipper that will be used when blitting the
  941.  *                       RGB surface to the main display.
  942.  *****************************************************************************
  943.  * This clipper prevents us to modify by mistake anything on the screen
  944.  * which doesn't belong to our window. For example when a part of our video
  945.  * window is hidden by another window.
  946.  *****************************************************************************/
  947. static int DirectXCreateClipper( vout_thread_t *p_vout )
  948. {
  949.     HRESULT dxresult;
  950.     msg_Dbg( p_vout, "DirectXCreateClipper" );
  951.     /* Create the clipper */
  952.     dxresult = IDirectDraw2_CreateClipper( p_vout->p_sys->p_ddobject, 0,
  953.                                            &p_vout->p_sys->p_clipper, NULL );
  954.     if( dxresult != DD_OK )
  955.     {
  956.         msg_Warn( p_vout, "cannot create clipper (error %li)", dxresult );
  957.         goto error;
  958.     }
  959.     /* Associate the clipper to the window */
  960.     dxresult = IDirectDrawClipper_SetHWnd( p_vout->p_sys->p_clipper, 0,
  961.                                            p_vout->p_sys->hvideownd );
  962.     if( dxresult != DD_OK )
  963.     {
  964.         msg_Warn( p_vout, "cannot attach clipper to window (error %li)",
  965.                           dxresult );
  966.         goto error;
  967.     }
  968.     /* associate the clipper with the surface */
  969.     dxresult = IDirectDrawSurface_SetClipper(p_vout->p_sys->p_display,
  970.                                              p_vout->p_sys->p_clipper);
  971.     if( dxresult != DD_OK )
  972.     {
  973.         msg_Warn( p_vout, "cannot attach clipper to surface (error %li)",
  974.                           dxresult );
  975.         goto error;
  976.     }
  977.     return VLC_SUCCESS;
  978.  error:
  979.     if( p_vout->p_sys->p_clipper )
  980.     {
  981.         IDirectDrawClipper_Release( p_vout->p_sys->p_clipper );
  982.     }
  983.     p_vout->p_sys->p_clipper = NULL;
  984.     return VLC_EGENERIC;
  985. }
  986. /*****************************************************************************
  987.  * DirectXCreateSurface: create an YUV overlay or RGB surface for the video.
  988.  *****************************************************************************
  989.  * The best method of display is with an YUV overlay because the YUV->RGB
  990.  * conversion is done in hardware.
  991.  * You can also create a plain RGB surface.
  992.  * ( Maybe we could also try an RGB overlay surface, which could have hardware
  993.  * scaling and which would also be faster in window mode because you don't
  994.  * need to do any blitting to the main display...)
  995.  *****************************************************************************/
  996. static int DirectXCreateSurface( vout_thread_t *p_vout,
  997.                                  LPDIRECTDRAWSURFACE2 *pp_surface_final,
  998.                                  int i_chroma, int b_overlay,
  999.                                  int i_backbuffers )
  1000. {
  1001.     HRESULT dxresult;
  1002.     LPDIRECTDRAWSURFACE p_surface;
  1003.     DDSURFACEDESC ddsd;
  1004.     /* Create the video surface */
  1005.     if( b_overlay )
  1006.     {
  1007.         /* Now try to create the YUV overlay surface.
  1008.          * This overlay will be displayed on top of the primary surface.
  1009.          * A color key is used to determine whether or not the overlay will be
  1010.          * displayed, ie the overlay will be displayed in place of the primary
  1011.          * surface wherever the primary surface will have this color.
  1012.          * The video window has been created with a background of this color so
  1013.          * the overlay will be only displayed on top of this window */
  1014.         memset( &ddsd, 0, sizeof( DDSURFACEDESC ));
  1015.         ddsd.dwSize = sizeof(DDSURFACEDESC);
  1016.         ddsd.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
  1017.         ddsd.ddpfPixelFormat.dwFlags = DDPF_FOURCC;
  1018.         ddsd.ddpfPixelFormat.dwFourCC = i_chroma;
  1019.         ddsd.dwFlags = DDSD_CAPS | DDSD_HEIGHT | DDSD_WIDTH | DDSD_PIXELFORMAT;
  1020.         ddsd.dwFlags |= (i_backbuffers ? DDSD_BACKBUFFERCOUNT : 0);
  1021.         ddsd.ddsCaps.dwCaps = DDSCAPS_OVERLAY | DDSCAPS_VIDEOMEMORY;
  1022.         ddsd.ddsCaps.dwCaps |= (i_backbuffers ? DDSCAPS_COMPLEX | DDSCAPS_FLIP
  1023.                                 : 0 );
  1024.         ddsd.dwHeight = p_vout->render.i_height;
  1025.         ddsd.dwWidth = p_vout->render.i_width;
  1026.         ddsd.dwBackBufferCount = i_backbuffers;
  1027.         dxresult = IDirectDraw2_CreateSurface( p_vout->p_sys->p_ddobject,
  1028.                                                &ddsd, &p_surface, NULL );
  1029.         if( dxresult != DD_OK )
  1030.         {
  1031.             *pp_surface_final = NULL;
  1032.             return VLC_EGENERIC;
  1033.         }
  1034.     }
  1035.     if( !b_overlay )
  1036.     {
  1037.         bool b_rgb_surface =
  1038.             ( i_chroma == VLC_FOURCC('R','G','B','2') )
  1039.           || ( i_chroma == VLC_FOURCC('R','V','1','5') )
  1040.            || ( i_chroma == VLC_FOURCC('R','V','1','6') )
  1041.             || ( i_chroma == VLC_FOURCC('R','V','2','4') )
  1042.              || ( i_chroma == VLC_FOURCC('R','V','3','2') );
  1043.         memset( &ddsd, 0, sizeof( DDSURFACEDESC ) );
  1044.         ddsd.dwSize = sizeof(DDSURFACEDESC);
  1045.         ddsd.ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
  1046.         ddsd.dwFlags = DDSD_HEIGHT | DDSD_WIDTH | DDSD_CAPS;
  1047.         ddsd.ddsCaps.dwCaps = DDSCAPS_OFFSCREENPLAIN;
  1048.         ddsd.dwHeight = p_vout->render.i_height;
  1049.         ddsd.dwWidth = p_vout->render.i_width;
  1050.         if( p_vout->p_sys->b_use_sysmem )
  1051.             ddsd.ddsCaps.dwCaps |= DDSCAPS_SYSTEMMEMORY;
  1052.         else
  1053.             ddsd.ddsCaps.dwCaps |= DDSCAPS_VIDEOMEMORY;
  1054.         if( !b_rgb_surface )
  1055.         {
  1056.             ddsd.dwFlags |= DDSD_PIXELFORMAT;
  1057.             ddsd.ddpfPixelFormat.dwFlags = DDPF_FOURCC;
  1058.             ddsd.ddpfPixelFormat.dwFourCC = i_chroma;
  1059.         }
  1060.         dxresult = IDirectDraw2_CreateSurface( p_vout->p_sys->p_ddobject,
  1061.                                                &ddsd, &p_surface, NULL );
  1062.         if( dxresult != DD_OK )
  1063.         {
  1064.             *pp_surface_final = NULL;
  1065.             return VLC_EGENERIC;
  1066.         }
  1067.     }
  1068.     /* Now that the surface is created, try to get a newer DirectX interface */
  1069.     dxresult = IDirectDrawSurface_QueryInterface( p_surface,
  1070.                                      &IID_IDirectDrawSurface2,
  1071.                                      (LPVOID *)pp_surface_final );
  1072.     IDirectDrawSurface_Release( p_surface );    /* Release the old interface */
  1073.     if ( dxresult != DD_OK )
  1074.     {
  1075.         msg_Err( p_vout, "cannot query IDirectDrawSurface2 interface "
  1076.                          "(error %li)", dxresult );
  1077.         *pp_surface_final = NULL;
  1078.         return VLC_EGENERIC;
  1079.     }
  1080.     if( b_overlay )
  1081.     {
  1082.         /* Check the overlay is useable as some graphics cards allow creating
  1083.          * several overlays but only one can be used at one time. */
  1084.         p_vout->p_sys->p_current_surface = *pp_surface_final;
  1085.         if( DirectDrawUpdateOverlay( p_vout ) != VLC_SUCCESS )
  1086.         {
  1087.             IDirectDrawSurface2_Release( *pp_surface_final );
  1088.             *pp_surface_final = NULL;
  1089.             msg_Err( p_vout, "overlay unuseable (might already be in use)" );
  1090.             return VLC_EGENERIC;
  1091.         }
  1092.     }
  1093.     return VLC_SUCCESS;
  1094. }
  1095. /*****************************************************************************
  1096.  * DirectDrawUpdateOverlay: Move or resize overlay surface on video display.
  1097.  *****************************************************************************
  1098.  * This function is used to move or resize an overlay surface on the screen.
  1099.  * Ususally the overlay is moved by the user and thus, by a move or resize
  1100.  * event (in Manage).
  1101.  *****************************************************************************/
  1102. int DirectDrawUpdateOverlay( vout_thread_t *p_vout )
  1103. {
  1104.     DDOVERLAYFX     ddofx;
  1105.     DWORD           dwFlags;
  1106.     HRESULT         dxresult;
  1107.     RECT            rect_src = p_vout->p_sys->rect_src_clipped;
  1108.     RECT            rect_dest = p_vout->p_sys->rect_dest_clipped;
  1109.     if( !p_vout->p_sys->b_using_overlay ) return VLC_EGENERIC;
  1110.     if( p_vout->p_sys->b_wallpaper )
  1111.     {
  1112.         int i_x, i_y, i_width, i_height;
  1113.         rect_src.left = p_vout->fmt_out.i_x_offset;
  1114.         rect_src.top = p_vout->fmt_out.i_y_offset;
  1115.         rect_src.right = rect_src.left + p_vout->fmt_out.i_visible_width;
  1116.         rect_src.bottom = rect_src.top + p_vout->fmt_out.i_visible_height;
  1117.         rect_dest = p_vout->p_sys->rect_display;
  1118.         vout_PlacePicture( p_vout, rect_dest.right, rect_dest.bottom,
  1119.                            &i_x, &i_y, &i_width, &i_height );
  1120.         rect_dest.left += i_x;
  1121.         rect_dest.right = rect_dest.left + i_width;
  1122.         rect_dest.top += i_y;
  1123.         rect_dest.bottom = rect_dest.top + i_height;
  1124.     }
  1125.     vlc_mutex_lock( &p_vout->p_sys->lock );
  1126.     if( p_vout->p_sys->p_current_surface == NULL )
  1127.     {
  1128.         vlc_mutex_unlock( &p_vout->p_sys->lock );
  1129.         return VLC_EGENERIC;
  1130.     }
  1131.     /* The new window dimensions should already have been computed by the
  1132.      * caller of this function */
  1133.     /* Position and show the overlay */
  1134.     memset(&ddofx, 0, sizeof(DDOVERLAYFX));
  1135.     ddofx.dwSize = sizeof(DDOVERLAYFX);
  1136.     ddofx.dckDestColorkey.dwColorSpaceLowValue = p_vout->p_sys->i_colorkey;
  1137.     ddofx.dckDestColorkey.dwColorSpaceHighValue = p_vout->p_sys->i_colorkey;
  1138.     dwFlags = DDOVER_SHOW | DDOVER_KEYDESTOVERRIDE;
  1139.     dxresult = IDirectDrawSurface2_UpdateOverlay(
  1140.                    p_vout->p_sys->p_current_surface,
  1141.                    &rect_src, p_vout->p_sys->p_display, &rect_dest,
  1142.                    dwFlags, &ddofx );
  1143.     vlc_mutex_unlock( &p_vout->p_sys->lock );
  1144.     if(dxresult != DD_OK)
  1145.     {
  1146.         msg_Warn( p_vout, "DirectDrawUpdateOverlay cannot move/resize overlay" );
  1147.         return VLC_EGENERIC;
  1148.     }
  1149.     return VLC_SUCCESS;
  1150. }
  1151. /*****************************************************************************
  1152.  * DirectXCloseDDraw: Release the DDraw object allocated by DirectXInitDDraw
  1153.  *****************************************************************************
  1154.  * This function returns all resources allocated by DirectXInitDDraw.
  1155.  *****************************************************************************/
  1156. static void DirectXCloseDDraw( vout_thread_t *p_vout )
  1157. {
  1158.     msg_Dbg( p_vout, "DirectXCloseDDraw" );
  1159.     if( p_vout->p_sys->p_ddobject != NULL )
  1160.     {
  1161.         IDirectDraw2_Release(p_vout->p_sys->p_ddobject);
  1162.         p_vout->p_sys->p_ddobject = NULL;
  1163.     }
  1164.     if( p_vout->p_sys->hddraw_dll != NULL )
  1165.     {
  1166.         FreeLibrary( p_vout->p_sys->hddraw_dll );
  1167.         p_vout->p_sys->hddraw_dll = NULL;
  1168.     }
  1169.     free( p_vout->p_sys->p_display_driver );
  1170.     p_vout->p_sys->p_display_driver = NULL;
  1171.     p_vout->p_sys->hmonitor = NULL;
  1172. }
  1173. /*****************************************************************************
  1174.  * DirectXCloseDisplay: close and reset the DirectX display device
  1175.  *****************************************************************************
  1176.  * This function returns all resources allocated by DirectXCreateDisplay.
  1177.  *****************************************************************************/
  1178. static void DirectXCloseDisplay( vout_thread_t *p_vout )
  1179. {
  1180.     msg_Dbg( p_vout, "DirectXCloseDisplay" );
  1181.     if( p_vout->p_sys->p_clipper != NULL )
  1182.     {
  1183.         msg_Dbg( p_vout, "DirectXCloseDisplay clipper" );
  1184.         IDirectDrawClipper_Release( p_vout->p_sys->p_clipper );
  1185.         p_vout->p_sys->p_clipper = NULL;
  1186.     }
  1187.     if( p_vout->p_sys->p_display != NULL )
  1188.     {
  1189.         msg_Dbg( p_vout, "DirectXCloseDisplay display" );
  1190.         IDirectDrawSurface2_Release( p_vout->p_sys->p_display );
  1191.         p_vout->p_sys->p_display = NULL;
  1192.     }
  1193. }
  1194. /*****************************************************************************
  1195.  * DirectXCloseSurface: close the YUV overlay or RGB surface.
  1196.  *****************************************************************************
  1197.  * This function returns all resources allocated for the surface.
  1198.  *****************************************************************************/
  1199. static void DirectXCloseSurface( vout_thread_t *p_vout,
  1200.                                  LPDIRECTDRAWSURFACE2 p_surface )
  1201. {
  1202.     msg_Dbg( p_vout, "DirectXCloseSurface" );
  1203.     if( p_surface != NULL )
  1204.     {
  1205.         IDirectDrawSurface2_Release( p_surface );
  1206.     }
  1207. }
  1208. /*****************************************************************************
  1209.  * NewPictureVec: allocate a vector of identical pictures
  1210.  *****************************************************************************
  1211.  * Returns 0 on success, -1 otherwise
  1212.  *****************************************************************************/
  1213. static int NewPictureVec( vout_thread_t *p_vout, picture_t *p_pic,
  1214.                           int i_num_pics )
  1215. {
  1216.     int i;
  1217.     int i_ret = VLC_SUCCESS;
  1218.     LPDIRECTDRAWSURFACE2 p_surface;
  1219.     msg_Dbg( p_vout, "NewPictureVec overlay:%s chroma:%.4s",
  1220.              p_vout->p_sys->b_using_overlay ? "yes" : "no",
  1221.              (char *)&p_vout->output.i_chroma );
  1222.     I_OUTPUTPICTURES = 0;
  1223.     /* First we try to use an YUV overlay surface.
  1224.      * The overlay surface that we create won't be used to decode directly
  1225.      * into it because accessing video memory directly is way to slow (remember
  1226.      * that pictures are decoded macroblock per macroblock). Instead the video
  1227.      * will be decoded in picture buffers in system memory which will then be
  1228.      * memcpy() to the overlay surface. */
  1229.     if( p_vout->p_sys->b_using_overlay )
  1230.     {
  1231.         /* Triple buffering rocks! it doesn't have any processing overhead
  1232.          * (you don't have to wait for the vsync) and provides for a very nice
  1233.          * video quality (no tearing). */
  1234.         if( p_vout->p_sys->b_3buf_overlay )
  1235.             i_ret = DirectXCreateSurface( p_vout, &p_surface,
  1236.                                           p_vout->output.i_chroma,
  1237.                                           p_vout->p_sys->b_using_overlay,
  1238.                                           2 /* number of backbuffers */ );
  1239.         if( !p_vout->p_sys->b_3buf_overlay || i_ret != VLC_SUCCESS )
  1240.         {
  1241.             /* Try to reduce the number of backbuffers */
  1242.             i_ret = DirectXCreateSurface( p_vout, &p_surface,
  1243.                                           p_vout->output.i_chroma,
  1244.                                           p_vout->p_sys->b_using_overlay,
  1245.                                           0 /* number of backbuffers */ );
  1246.         }
  1247.         if( i_ret == VLC_SUCCESS )
  1248.         {
  1249.             DDSCAPS dds_caps;
  1250.             picture_t front_pic;
  1251.             picture_sys_t front_pic_sys;
  1252.             front_pic.p_sys = &front_pic_sys;
  1253.             /* Allocate internal structure */
  1254.             p_pic[0].p_sys = malloc( sizeof( picture_sys_t ) );
  1255.             if( p_pic[0].p_sys == NULL )
  1256.             {
  1257.                 DirectXCloseSurface( p_vout, p_surface );
  1258.                 return VLC_ENOMEM;
  1259.             }
  1260.             /* set front buffer */
  1261.             p_pic[0].p_sys->p_front_surface = p_surface;
  1262.             /* Get the back buffer */
  1263.             memset( &dds_caps, 0, sizeof( DDSCAPS ) );
  1264.             dds_caps.dwCaps = DDSCAPS_BACKBUFFER;
  1265.             if( DD_OK != IDirectDrawSurface2_GetAttachedSurface(
  1266.                                                 p_surface, &dds_caps,
  1267.                                                 &p_pic[0].p_sys->p_surface ) )
  1268.             {
  1269.                 msg_Warn( p_vout, "NewPictureVec could not get back buffer" );
  1270.                 /* front buffer is the same as back buffer */
  1271.                 p_pic[0].p_sys->p_surface = p_surface;
  1272.             }
  1273.             p_vout->p_sys->p_current_surface = front_pic.p_sys->p_surface =
  1274.                 p_pic[0].p_sys->p_front_surface;
  1275.             /* Reset the front buffer memory */
  1276.             if( DirectXLockSurface( p_vout, &front_pic ) == VLC_SUCCESS )
  1277.             {
  1278.                 int i,j;
  1279.                 for( i = 0; i < front_pic.i_planes; i++ )
  1280.                     for( j = 0; j < front_pic.p[i].i_visible_lines; j++)
  1281.                         memset( front_pic.p[i].p_pixels + j *
  1282.                                 front_pic.p[i].i_pitch, 127,
  1283.                                 front_pic.p[i].i_visible_pitch );
  1284.                 DirectXUnlockSurface( p_vout, &front_pic );
  1285.             }
  1286.             DirectDrawUpdateOverlay( p_vout );
  1287.             I_OUTPUTPICTURES = 1;
  1288.             msg_Dbg( p_vout, "YUV overlay created successfully" );
  1289.         }
  1290.     }
  1291.     /* As we can't have an overlay, we'll try to create a plain offscreen
  1292.      * surface. This surface will reside in video memory because there's a
  1293.      * better chance then that we'll be able to use some kind of hardware
  1294.      * acceleration like rescaling, blitting or YUV->RGB conversions.
  1295.      * We then only need to blit this surface onto the main display when we
  1296.      * want to display it */
  1297.     if( !p_vout->p_sys->b_using_overlay )
  1298.     {
  1299.         if( p_vout->p_sys->b_hw_yuv )
  1300.         {
  1301.             DWORD i_codes;
  1302.             DWORD *pi_codes;
  1303.             bool b_result = false;
  1304.             /* Check if the chroma is supported first. This is required
  1305.              * because a few buggy drivers don't mind creating the surface
  1306.              * even if they don't know about the chroma. */
  1307.             if( IDirectDraw2_GetFourCCCodes( p_vout->p_sys->p_ddobject,
  1308.                                              &i_codes, NULL ) == DD_OK )
  1309.             {
  1310.                 pi_codes = malloc( i_codes * sizeof(DWORD) );
  1311.                 if( pi_codes && IDirectDraw2_GetFourCCCodes(
  1312.                     p_vout->p_sys->p_ddobject, &i_codes, pi_codes ) == DD_OK )
  1313.                 {
  1314.                     for( i = 0; i < (int)i_codes; i++ )
  1315.                     {
  1316.                         if( p_vout->output.i_chroma == pi_codes[i] )
  1317.                         {
  1318.                             b_result = true;
  1319.                             break;
  1320.                         }
  1321.                     }
  1322.                 }
  1323.             }
  1324.             if( b_result )
  1325.                 i_ret = DirectXCreateSurface( p_vout, &p_surface,
  1326.                                               p_vout->output.i_chroma,
  1327.                                               0 /* no overlay */,
  1328.                                               0 /* no back buffers */ );
  1329.             else
  1330.                 p_vout->p_sys->b_hw_yuv = false;
  1331.         }
  1332.         if( i_ret || !p_vout->p_sys->b_hw_yuv )
  1333.         {
  1334.             /* Our last choice is to use a plain RGB surface */
  1335.             DDPIXELFORMAT ddpfPixelFormat;
  1336.             ddpfPixelFormat.dwSize = sizeof(DDPIXELFORMAT);
  1337.             IDirectDrawSurface2_GetPixelFormat( p_vout->p_sys->p_display,
  1338.                                                 &ddpfPixelFormat );
  1339.             if( ddpfPixelFormat.dwFlags & DDPF_RGB )
  1340.             {
  1341.                 switch( ddpfPixelFormat.dwRGBBitCount )
  1342.                 {
  1343.                 case 8:
  1344.                     p_vout->output.i_chroma = VLC_FOURCC('R','G','B','2');
  1345.                     p_vout->output.pf_setpalette = SetPalette;
  1346.                     break;
  1347.                 case 15:
  1348.                     p_vout->output.i_chroma = VLC_FOURCC('R','V','1','5');
  1349.                     break;
  1350.                 case 16:
  1351.                     p_vout->output.i_chroma = VLC_FOURCC('R','V','1','6');
  1352.                     break;
  1353.                 case 24:
  1354.                     p_vout->output.i_chroma = VLC_FOURCC('R','V','2','4');
  1355.                     break;
  1356.                 case 32:
  1357.                     p_vout->output.i_chroma = VLC_FOURCC('R','V','3','2');
  1358.                     break;
  1359.                 default:
  1360.                     msg_Err( p_vout, "unknown screen depth" );
  1361.                     return VLC_EGENERIC;
  1362.                 }
  1363.                 p_vout->output.i_rmask = ddpfPixelFormat.dwRBitMask;
  1364.                 p_vout->output.i_gmask = ddpfPixelFormat.dwGBitMask;
  1365.                 p_vout->output.i_bmask = ddpfPixelFormat.dwBBitMask;
  1366.             }
  1367.             p_vout->p_sys->b_hw_yuv = 0;
  1368.             i_ret = DirectXCreateSurface( p_vout, &p_surface,
  1369.                                           p_vout->output.i_chroma,
  1370.                                           0 /* no overlay */,
  1371.                                           0 /* no back buffers */ );
  1372.             if( i_ret && !p_vout->p_sys->b_use_sysmem )
  1373.             {
  1374.                 /* Give it a last try with b_use_sysmem enabled */
  1375.                 p_vout->p_sys->b_use_sysmem = 1;
  1376.                 i_ret = DirectXCreateSurface( p_vout, &p_surface,
  1377.                                               p_vout->output.i_chroma,
  1378.                                               0 /* no overlay */,
  1379.                                               0 /* no back buffers */ );
  1380.             }
  1381.         }
  1382.         if( i_ret == VLC_SUCCESS )
  1383.         {
  1384.             /* Allocate internal structure */
  1385.             p_pic[0].p_sys = malloc( sizeof( picture_sys_t ) );
  1386.             if( p_pic[0].p_sys == NULL )
  1387.             {
  1388.                 DirectXCloseSurface( p_vout, p_surface );
  1389.                 return VLC_ENOMEM;
  1390.             }
  1391.             p_pic[0].p_sys->p_surface = p_pic[0].p_sys->p_front_surface
  1392.                 = p_surface;
  1393.             I_OUTPUTPICTURES = 1;
  1394.             msg_Dbg( p_vout, "created plain surface of chroma:%.4s",
  1395.                      (char *)&p_vout->output.i_chroma );
  1396.         }
  1397.     }
  1398.     /* Now that we've got all our direct-buffers, we can finish filling in the
  1399.      * picture_t structures */
  1400.     for( i = 0; i < I_OUTPUTPICTURES; i++ )
  1401.     {
  1402.         p_pic[i].i_status = DESTROYED_PICTURE;
  1403.         p_pic[i].i_type   = DIRECT_PICTURE;
  1404.         p_pic[i].b_slow   = true;
  1405.         p_pic[i].pf_lock  = DirectXLockSurface;
  1406.         p_pic[i].pf_unlock = DirectXUnlockSurface;
  1407.         PP_OUTPUTPICTURE[i] = &p_pic[i];
  1408.         if( DirectXLockSurface( p_vout, &p_pic[i] ) != VLC_SUCCESS )
  1409.         {
  1410.             /* AAARRGG */
  1411.             FreePictureVec( p_vout, p_pic, I_OUTPUTPICTURES );
  1412.             I_OUTPUTPICTURES = 0;
  1413.             msg_Err( p_vout, "cannot lock surface" );
  1414.             return VLC_EGENERIC;
  1415.         }
  1416.         DirectXUnlockSurface( p_vout, &p_pic[i] );
  1417.     }
  1418.     msg_Dbg( p_vout, "End NewPictureVec (%s)",
  1419.              I_OUTPUTPICTURES ? "succeeded" : "failed" );
  1420.     return VLC_SUCCESS;
  1421. }
  1422. /*****************************************************************************
  1423.  * FreePicture: destroy a picture vector allocated with NewPictureVec
  1424.  *****************************************************************************
  1425.  *
  1426.  *****************************************************************************/
  1427. static void FreePictureVec( vout_thread_t *p_vout, picture_t *p_pic,
  1428.                             int i_num_pics )
  1429. {
  1430.     int i;
  1431.     vlc_mutex_lock( &p_vout->p_sys->lock );
  1432.     p_vout->p_sys->p_current_surface = 0;
  1433.     vlc_mutex_unlock( &p_vout->p_sys->lock );
  1434.     for( i = 0; i < i_num_pics; i++ )
  1435.     {
  1436.         DirectXCloseSurface( p_vout, p_pic[i].p_sys->p_front_surface );
  1437.         for( i = 0; i < i_num_pics; i++ )
  1438.         {
  1439.             free( p_pic[i].p_sys );
  1440.         }
  1441.     }
  1442. }
  1443. /*****************************************************************************
  1444.  * UpdatePictureStruct: updates the internal data in the picture_t structure
  1445.  *****************************************************************************
  1446.  * This will setup stuff for use by the video_output thread
  1447.  *****************************************************************************/
  1448. static int UpdatePictureStruct( vout_thread_t *p_vout, picture_t *p_pic,
  1449.                                 int i_chroma )
  1450. {
  1451.     switch( p_vout->output.i_chroma )
  1452.     {
  1453.         case VLC_FOURCC('R','G','B','2'):
  1454.         case VLC_FOURCC('R','V','1','5'):
  1455.         case VLC_FOURCC('R','V','1','6'):
  1456.         case VLC_FOURCC('R','V','2','4'):
  1457.         case VLC_FOURCC('R','V','3','2'):
  1458.             p_pic->p->p_pixels = p_pic->p_sys->ddsd.lpSurface;
  1459.             p_pic->p->i_lines = p_vout->output.i_height;
  1460.             p_pic->p->i_visible_lines = p_vout->output.i_height;
  1461.             p_pic->p->i_pitch = p_pic->p_sys->ddsd.lPitch;
  1462.             switch( p_vout->output.i_chroma )
  1463.             {
  1464.                 case VLC_FOURCC('R','G','B','2'):
  1465.                     p_pic->p->i_pixel_pitch = 1;
  1466.                     break;
  1467.                 case VLC_FOURCC('R','V','1','5'):
  1468.                 case VLC_FOURCC('R','V','1','6'):
  1469.                     p_pic->p->i_pixel_pitch = 2;
  1470.                     break;
  1471.                 case VLC_FOURCC('R','V','2','4'):
  1472.                     p_pic->p->i_pixel_pitch = 3;
  1473.                     break;
  1474.                 case VLC_FOURCC('R','V','3','2'):
  1475.                     p_pic->p->i_pixel_pitch = 4;
  1476.                     break;
  1477.                 default:
  1478.                     return VLC_EGENERIC;
  1479.             }
  1480.             p_pic->p->i_visible_pitch = p_vout->output.i_width *
  1481.               p_pic->p->i_pixel_pitch;
  1482.             p_pic->i_planes = 1;
  1483.             break;
  1484.         case VLC_FOURCC('Y','V','1','2'):
  1485.         case VLC_FOURCC('I','4','2','0'):
  1486.             /* U and V inverted compared to I420
  1487.              * Fixme: this should be handled by the vout core */
  1488.             p_vout->output.i_chroma = VLC_FOURCC('I','4','2','0');
  1489.             p_pic->Y_PIXELS = p_pic->p_sys->ddsd.lpSurface;
  1490.             p_pic->p[Y_PLANE].i_lines = p_vout->output.i_height;
  1491.             p_pic->p[Y_PLANE].i_visible_lines = p_vout->output.i_height;
  1492.             p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->ddsd.lPitch;
  1493.             p_pic->p[Y_PLANE].i_pixel_pitch = 1;
  1494.             p_pic->p[Y_PLANE].i_visible_pitch = p_vout->output.i_width *
  1495.               p_pic->p[Y_PLANE].i_pixel_pitch;
  1496.             p_pic->V_PIXELS =  p_pic->Y_PIXELS
  1497.               + p_pic->p[Y_PLANE].i_lines * p_pic->p[Y_PLANE].i_pitch;
  1498.             p_pic->p[V_PLANE].i_lines = p_vout->output.i_height / 2;
  1499.             p_pic->p[V_PLANE].i_visible_lines = p_vout->output.i_height / 2;
  1500.             p_pic->p[V_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
  1501.             p_pic->p[V_PLANE].i_pixel_pitch = 1;
  1502.             p_pic->p[V_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
  1503.               p_pic->p[V_PLANE].i_pixel_pitch;
  1504.             p_pic->U_PIXELS = p_pic->V_PIXELS
  1505.               + p_pic->p[V_PLANE].i_lines * p_pic->p[V_PLANE].i_pitch;
  1506.             p_pic->p[U_PLANE].i_lines = p_vout->output.i_height / 2;
  1507.             p_pic->p[U_PLANE].i_visible_lines = p_vout->output.i_height / 2;
  1508.             p_pic->p[U_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
  1509.             p_pic->p[U_PLANE].i_pixel_pitch = 1;
  1510.             p_pic->p[U_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
  1511.               p_pic->p[U_PLANE].i_pixel_pitch;
  1512.             p_pic->i_planes = 3;
  1513.             break;
  1514.         case VLC_FOURCC('I','Y','U','V'):
  1515.             p_pic->Y_PIXELS = p_pic->p_sys->ddsd.lpSurface;
  1516.             p_pic->p[Y_PLANE].i_lines = p_vout->output.i_height;
  1517.             p_pic->p[Y_PLANE].i_visible_lines = p_vout->output.i_height;
  1518.             p_pic->p[Y_PLANE].i_pitch = p_pic->p_sys->ddsd.lPitch;
  1519.             p_pic->p[Y_PLANE].i_pixel_pitch = 1;
  1520.             p_pic->p[Y_PLANE].i_visible_pitch = p_vout->output.i_width *
  1521.               p_pic->p[Y_PLANE].i_pixel_pitch;
  1522.             p_pic->U_PIXELS = p_pic->Y_PIXELS
  1523.               + p_pic->p[Y_PLANE].i_lines * p_pic->p[Y_PLANE].i_pitch;
  1524.             p_pic->p[U_PLANE].i_lines = p_vout->output.i_height / 2;
  1525.             p_pic->p[U_PLANE].i_visible_lines = p_vout->output.i_height / 2;
  1526.             p_pic->p[U_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
  1527.             p_pic->p[U_PLANE].i_pixel_pitch = 1;
  1528.             p_pic->p[U_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
  1529.               p_pic->p[U_PLANE].i_pixel_pitch;
  1530.             p_pic->V_PIXELS =  p_pic->U_PIXELS
  1531.               + p_pic->p[U_PLANE].i_lines * p_pic->p[U_PLANE].i_pitch;
  1532.             p_pic->p[V_PLANE].i_lines = p_vout->output.i_height / 2;
  1533.             p_pic->p[V_PLANE].i_visible_lines = p_vout->output.i_height / 2;
  1534.             p_pic->p[V_PLANE].i_pitch = p_pic->p[Y_PLANE].i_pitch / 2;
  1535.             p_pic->p[V_PLANE].i_pixel_pitch = 1;
  1536.             p_pic->p[V_PLANE].i_visible_pitch = p_vout->output.i_width / 2 *
  1537.               p_pic->p[V_PLANE].i_pixel_pitch;
  1538.             p_pic->i_planes = 3;
  1539.             break;
  1540.         case VLC_FOURCC('U','Y','V','Y'):
  1541.         case VLC_FOURCC('Y','U','Y','2'):
  1542.             p_pic->p->p_pixels = p_pic->p_sys->ddsd.lpSurface;
  1543.             p_pic->p->i_lines = p_vout->output.i_height;
  1544.             p_pic->p->i_visible_lines = p_vout->output.i_height;
  1545.             p_pic->p->i_pitch = p_pic->p_sys->ddsd.lPitch;
  1546.             p_pic->p->i_pixel_pitch = 2;
  1547.             p_pic->p->i_visible_pitch = p_vout->output.i_width *
  1548.               p_pic->p->i_pixel_pitch;
  1549.             p_pic->i_planes = 1;
  1550.             break;
  1551.         default:
  1552.             /* Unknown chroma, tell the guy to get lost */
  1553.             msg_Err( p_vout, "never heard of chroma 0x%.8x (%4.4s)",
  1554.                      p_vout->output.i_chroma,
  1555.                      (char*)&p_vout->output.i_chroma );
  1556.             return VLC_EGENERIC;
  1557.     }
  1558.     return VLC_SUCCESS;
  1559. }
  1560. /*****************************************************************************
  1561.  * DirectXGetDDrawCaps: Probe the capabilities of the hardware
  1562.  *****************************************************************************
  1563.  * It is nice to know which features are supported by the hardware so we can
  1564.  * find ways to optimize our rendering.
  1565.  *****************************************************************************/
  1566. static void DirectXGetDDrawCaps( vout_thread_t *p_vout )
  1567. {
  1568.     DDCAPS ddcaps;
  1569.     HRESULT dxresult;
  1570.     /* This is just an indication of whether or not we'll support overlay,
  1571.      * but with this test we don't know if we support YUV overlay */
  1572.     memset( &ddcaps, 0, sizeof( DDCAPS ));
  1573.     ddcaps.dwSize = sizeof(DDCAPS);
  1574.     dxresult = IDirectDraw2_GetCaps( p_vout->p_sys->p_ddobject,
  1575.                                      &ddcaps, NULL );
  1576.     if(dxresult != DD_OK )
  1577.     {
  1578.         msg_Warn( p_vout, "cannot get caps" );
  1579.     }
  1580.     else
  1581.     {
  1582.         bool bHasOverlay, bHasOverlayFourCC, bCanDeinterlace,
  1583.              bHasColorKey, bCanStretch, bCanBltFourcc,
  1584.              bAlignBoundarySrc, bAlignBoundaryDest,
  1585.              bAlignSizeSrc, bAlignSizeDest;
  1586.         /* Determine if the hardware supports overlay surfaces */
  1587.         bHasOverlay = (ddcaps.dwCaps & DDCAPS_OVERLAY) ? 1 : 0;
  1588.         /* Determine if the hardware supports overlay surfaces */
  1589.         bHasOverlayFourCC = (ddcaps.dwCaps & DDCAPS_OVERLAYFOURCC) ? 1 : 0;
  1590.         /* Determine if the hardware supports overlay deinterlacing */
  1591.         bCanDeinterlace = (ddcaps.dwCaps & DDCAPS2_CANFLIPODDEVEN) ? 1 : 0;
  1592.         /* Determine if the hardware supports colorkeying */
  1593.         bHasColorKey = (ddcaps.dwCaps & DDCAPS_COLORKEY) ? 1 : 0;
  1594.         /* Determine if the hardware supports scaling of the overlay surface */
  1595.         bCanStretch = (ddcaps.dwCaps & DDCAPS_OVERLAYSTRETCH) ? 1 : 0;
  1596.         /* Determine if the hardware supports color conversion during a blit */
  1597.         bCanBltFourcc = (ddcaps.dwCaps & DDCAPS_BLTFOURCC) ? 1 : 0;
  1598.         /* Determine overlay source boundary alignment */
  1599.         bAlignBoundarySrc = (ddcaps.dwCaps & DDCAPS_ALIGNBOUNDARYSRC) ? 1 : 0;
  1600.         /* Determine overlay destination boundary alignment */
  1601.         bAlignBoundaryDest = (ddcaps.dwCaps & DDCAPS_ALIGNBOUNDARYDEST) ? 1:0;
  1602.         /* Determine overlay destination size alignment */
  1603.         bAlignSizeSrc = (ddcaps.dwCaps & DDCAPS_ALIGNSIZESRC) ? 1 : 0;
  1604.         /* Determine overlay destination size alignment */
  1605.         bAlignSizeDest = (ddcaps.dwCaps & DDCAPS_ALIGNSIZEDEST) ? 1 : 0;
  1606.  
  1607.         msg_Dbg( p_vout, "DirectDraw Capabilities: overlay=%i yuvoverlay=%i "
  1608.                          "can_deinterlace_overlay=%i colorkey=%i stretch=%i "
  1609.                          "bltfourcc=%i",
  1610.                          bHasOverlay, bHasOverlayFourCC, bCanDeinterlace,
  1611.                          bHasColorKey, bCanStretch, bCanBltFourcc );
  1612.         if( bAlignBoundarySrc || bAlignBoundaryDest ||
  1613.             bAlignSizeSrc || bAlignSizeDest )
  1614.         {
  1615.             if( bAlignBoundarySrc ) p_vout->p_sys->i_align_src_boundary =
  1616.                 ddcaps.dwAlignBoundarySrc;
  1617.             if( bAlignBoundaryDest ) p_vout->p_sys->i_align_dest_boundary =
  1618.                 ddcaps.dwAlignBoundaryDest;
  1619.             if( bAlignSizeDest ) p_vout->p_sys->i_align_src_size =
  1620.                 ddcaps.dwAlignSizeSrc;
  1621.             if( bAlignSizeDest ) p_vout->p_sys->i_align_dest_size =
  1622.                 ddcaps.dwAlignSizeDest;
  1623.             msg_Dbg( p_vout, "align_boundary_src=%i,%i "
  1624.                      "align_boundary_dest=%i,%i "
  1625.                      "align_size_src=%i,%i align_size_dest=%i,%i",
  1626.                      bAlignBoundarySrc, p_vout->p_sys->i_align_src_boundary,
  1627.                      bAlignBoundaryDest, p_vout->p_sys->i_align_dest_boundary,
  1628.                      bAlignSizeSrc, p_vout->p_sys->i_align_src_size,
  1629.                      bAlignSizeDest, p_vout->p_sys->i_align_dest_size );
  1630.         }
  1631.         /* Don't ask for troubles */
  1632.         if( !bCanBltFourcc ) p_vout->p_sys->b_hw_yuv = FALSE;
  1633.     }
  1634. }
  1635. /*****************************************************************************
  1636.  * DirectXLockSurface: Lock surface and get picture data pointer
  1637.  *****************************************************************************
  1638.  * This function locks a surface and get the surface descriptor which amongst
  1639.  * other things has the pointer to the picture data.
  1640.  *****************************************************************************/
  1641. static int DirectXLockSurface( vout_thread_t *p_vout, picture_t *p_pic )
  1642. {
  1643.     HRESULT dxresult;
  1644.     /* Lock the surface to get a valid pointer to the picture buffer */
  1645.     memset( &p_pic->p_sys->ddsd, 0, sizeof( DDSURFACEDESC ));
  1646.     p_pic->p_sys->ddsd.dwSize = sizeof(DDSURFACEDESC);
  1647.     dxresult = IDirectDrawSurface2_Lock( p_pic->p_sys->p_surface,
  1648.                                          NULL, &p_pic->p_sys->ddsd,
  1649.                                          DDLOCK_NOSYSLOCK | DDLOCK_WAIT,
  1650.                                          NULL );
  1651.     if( dxresult != DD_OK )
  1652.     {
  1653.         if( dxresult == DDERR_INVALIDPARAMS )
  1654.         {
  1655.             /* DirectX 3 doesn't support the DDLOCK_NOSYSLOCK flag, resulting
  1656.              * in an invalid params error */
  1657.             dxresult = IDirectDrawSurface2_Lock( p_pic->p_sys->p_surface, NULL,
  1658.                                              &p_pic->p_sys->ddsd,
  1659.                                              DDLOCK_WAIT, NULL);
  1660.         }
  1661.         if( dxresult == DDERR_SURFACELOST )
  1662.         {
  1663.             /* Your surface can be lost so be sure
  1664.              * to check this and restore it if needed */
  1665.             /* When using overlays with back-buffers, we need to restore
  1666.              * the front buffer so the back-buffers get restored as well. */
  1667.             if( p_vout->p_sys->b_using_overlay  )
  1668.                 IDirectDrawSurface2_Restore( p_pic->p_sys->p_front_surface );
  1669.             else
  1670.                 IDirectDrawSurface2_Restore( p_pic->p_sys->p_surface );
  1671.             dxresult = IDirectDrawSurface2_Lock( p_pic->p_sys->p_surface, NULL,
  1672.                                                  &p_pic->p_sys->ddsd,
  1673.                                                  DDLOCK_WAIT, NULL);
  1674. #if 0
  1675.             if( dxresult == DDERR_SURFACELOST )
  1676.                 msg_Dbg( p_vout, "DirectXLockSurface: DDERR_SURFACELOST" );
  1677. #endif
  1678.         }
  1679.         if( dxresult != DD_OK )
  1680.         {
  1681.             return VLC_EGENERIC;
  1682.         }
  1683.     }
  1684.     /* Now we have a pointer to the surface memory, we can update our picture
  1685.      * structure. */
  1686.     if( UpdatePictureStruct( p_vout, p_pic, p_vout->output.i_chroma )
  1687.         != VLC_SUCCESS )
  1688.     {
  1689.         DirectXUnlockSurface( p_vout, p_pic );
  1690.         return VLC_EGENERIC;
  1691.     }
  1692.     else
  1693.         return VLC_SUCCESS;
  1694. }
  1695. /*****************************************************************************
  1696.  * DirectXUnlockSurface: Unlock a surface locked by DirectXLockSurface().
  1697.  *****************************************************************************/
  1698. static int DirectXUnlockSurface( vout_thread_t *p_vout, picture_t *p_pic )
  1699. {
  1700.     /* Unlock the Surface */
  1701.     if( IDirectDrawSurface2_Unlock( p_pic->p_sys->p_surface, NULL ) == DD_OK )
  1702.         return VLC_SUCCESS;
  1703.     else
  1704.         return VLC_EGENERIC;
  1705. }
  1706. /*****************************************************************************
  1707.  * DirectXFindColorkey: Finds out the 32bits RGB pixel value of the colorkey
  1708.  *****************************************************************************/
  1709. static DWORD DirectXFindColorkey( vout_thread_t *p_vout, uint32_t *pi_color )
  1710. {
  1711.     DDSURFACEDESC ddsd;
  1712.     HRESULT dxresult;
  1713.     COLORREF i_rgb = 0;
  1714.     uint32_t i_pixel_backup;
  1715.     HDC hdc;
  1716.     ddsd.dwSize = sizeof(ddsd);
  1717.     dxresult = IDirectDrawSurface2_Lock( p_vout->p_sys->p_display, NULL,
  1718.                                          &ddsd, DDLOCK_WAIT, NULL );
  1719.     if( dxresult != DD_OK ) return 0;
  1720.     i_pixel_backup = *(uint32_t *)ddsd.lpSurface;
  1721.     switch( ddsd.ddpfPixelFormat.dwRGBBitCount )
  1722.     {
  1723.     case 4:
  1724.         *(uint8_t *)ddsd.lpSurface = *pi_color | (*pi_color << 4);
  1725.         break;
  1726.     case 8:
  1727.         *(uint8_t *)ddsd.lpSurface = *pi_color;
  1728.         break;
  1729.     case 15:
  1730.     case 16:
  1731.         *(uint16_t *)ddsd.lpSurface = *pi_color;
  1732.         break;
  1733.     case 24:
  1734.         /* Seems to be problematic so we'll just put black as the colorkey */
  1735.         *pi_color = 0;
  1736.     default:
  1737.         *(uint32_t *)ddsd.lpSurface = *pi_color;
  1738.         break;
  1739.     }
  1740.     IDirectDrawSurface2_Unlock( p_vout->p_sys->p_display, NULL );
  1741.     if( IDirectDrawSurface2_GetDC( p_vout->p_sys->p_display, &hdc ) == DD_OK )
  1742.     {
  1743.         i_rgb = GetPixel( hdc, 0, 0 );
  1744.         IDirectDrawSurface2_ReleaseDC( p_vout->p_sys->p_display, hdc );
  1745.     }
  1746.     ddsd.dwSize = sizeof(ddsd);
  1747.     dxresult = IDirectDrawSurface2_Lock( p_vout->p_sys->p_display, NULL,
  1748.                                          &ddsd, DDLOCK_WAIT, NULL );
  1749.     if( dxresult != DD_OK ) return i_rgb;
  1750.     *(uint32_t *)ddsd.lpSurface = i_pixel_backup;
  1751.     IDirectDrawSurface2_Unlock( p_vout->p_sys->p_display, NULL );
  1752.     return i_rgb;
  1753. }
  1754. /*****************************************************************************
  1755.  * A few toolbox functions
  1756.  *****************************************************************************/
  1757. void SwitchWallpaperMode( vout_thread_t *p_vout, bool b_on )
  1758. {
  1759.     HWND hwnd;
  1760.     if( p_vout->p_sys->b_wallpaper == b_on ) return; /* Nothing to do */
  1761.     hwnd = FindWindow( _T("Progman"), NULL );
  1762.     if( hwnd ) hwnd = FindWindowEx( hwnd, NULL, _T("SHELLDLL_DefView"), NULL );
  1763.     if( hwnd ) hwnd = FindWindowEx( hwnd, NULL, _T("SysListView32"), NULL );
  1764.     if( !hwnd )
  1765.     {
  1766.         msg_Warn( p_vout, "couldn't find "SysListView32" window, "
  1767.                   "wallpaper mode not supported" );
  1768.         return;
  1769.     }
  1770.     p_vout->p_sys->b_wallpaper = b_on;
  1771.     msg_Dbg( p_vout, "wallpaper mode %s", b_on ? "enabled" : "disabled" );
  1772.     if( p_vout->p_sys->b_wallpaper )
  1773.     {
  1774.         p_vout->p_sys->color_bkg = ListView_GetBkColor( hwnd );
  1775.         p_vout->p_sys->color_bkgtxt = ListView_GetTextBkColor( hwnd );
  1776.         ListView_SetBkColor( hwnd, p_vout->p_sys->i_rgb_colorkey );
  1777.         ListView_SetTextBkColor( hwnd, p_vout->p_sys->i_rgb_colorkey );
  1778.     }
  1779.     else if( hwnd )
  1780.     {
  1781.         ListView_SetBkColor( hwnd, p_vout->p_sys->color_bkg );
  1782.         ListView_SetTextBkColor( hwnd, p_vout->p_sys->color_bkgtxt );
  1783.     }
  1784.     /* Update desktop */
  1785.     InvalidateRect( hwnd, NULL, TRUE );
  1786.     UpdateWindow( hwnd );
  1787. }
  1788. /*****************************************************************************
  1789.  * config variable callback
  1790.  *****************************************************************************/
  1791. BOOL WINAPI DirectXEnumCallback2( GUID* p_guid, LPTSTR psz_desc,
  1792.                                   LPTSTR psz_drivername, VOID* p_context,
  1793.                                   HMONITOR hmon )
  1794. {
  1795.     module_config_t *p_item = (module_config_t *)p_context;
  1796.     p_item->ppsz_list =
  1797.         (char **)realloc( p_item->ppsz_list,
  1798.                           (p_item->i_list+2) * sizeof(char *) );
  1799.     p_item->ppsz_list_text =
  1800.         (char **)realloc( p_item->ppsz_list_text,
  1801.                           (p_item->i_list+2) * sizeof(char *) );
  1802.     p_item->ppsz_list[p_item->i_list] = strdup( psz_drivername );
  1803.     p_item->ppsz_list_text[p_item->i_list] = NULL;
  1804.     p_item->i_list++;
  1805.     p_item->ppsz_list[p_item->i_list] = NULL;
  1806.     p_item->ppsz_list_text[p_item->i_list] = NULL;
  1807.     return TRUE; /* Keep enumerating */
  1808. }
  1809. static int FindDevicesCallback( vlc_object_t *p_this, char const *psz_name,
  1810.                                vlc_value_t newval, vlc_value_t oldval, void *d)
  1811. {
  1812.     HRESULT (WINAPI *OurDirectDrawEnumerateEx)( LPDDENUMCALLBACKEXA, LPVOID,
  1813.                                                 DWORD );
  1814.     HINSTANCE hddraw_dll;
  1815.     module_config_t *p_item;
  1816.     int i;
  1817.     p_item = config_FindConfig( p_this, psz_name );
  1818.     if( !p_item ) return VLC_SUCCESS;
  1819.     /* Clear-up the current list */
  1820.     if( p_item->i_list )
  1821.     {
  1822.         /* Keep the first entry */
  1823.         for( i = 1; i < p_item->i_list; i++ )
  1824.         {
  1825.             free( p_item->ppsz_list[i] );
  1826.             free( p_item->ppsz_list_text[i] );
  1827.         }
  1828.         /* TODO: Remove when no more needed */
  1829.         p_item->ppsz_list[i] = NULL;
  1830.         p_item->ppsz_list_text[i] = NULL;
  1831.     }
  1832.     p_item->i_list = 1;
  1833.     /* Load direct draw DLL */
  1834.     hddraw_dll = LoadLibrary(_T("DDRAW.DLL"));
  1835.     if( hddraw_dll == NULL ) return VLC_SUCCESS;
  1836.     OurDirectDrawEnumerateEx =
  1837. #ifndef UNICODE
  1838.       (void *)GetProcAddress( hddraw_dll, "DirectDrawEnumerateExA" );
  1839. #else
  1840.       (void *)GetProcAddress( hddraw_dll, _T("DirectDrawEnumerateExW") );
  1841. #endif
  1842.     if( OurDirectDrawEnumerateEx )
  1843.     {
  1844.         /* Enumerate displays */
  1845.         OurDirectDrawEnumerateEx( DirectXEnumCallback2, p_item,
  1846.                                   DDENUM_ATTACHEDSECONDARYDEVICES );
  1847.     }
  1848.     FreeLibrary( hddraw_dll );
  1849.     /* Signal change to the interface */
  1850.     p_item->b_dirty = true;
  1851.     return VLC_SUCCESS;
  1852. }
  1853. static int WallpaperCallback( vlc_object_t *p_this, char const *psz_cmd,
  1854.                               vlc_value_t oldval, vlc_value_t newval,
  1855.                               void *p_data )
  1856. {
  1857.     vout_thread_t *p_vout = (vout_thread_t *)p_this;
  1858.     if( (newval.b_bool && !p_vout->p_sys->b_wallpaper) ||
  1859.         (!newval.b_bool && p_vout->p_sys->b_wallpaper) )
  1860.     {
  1861.         playlist_t *p_playlist = pl_Hold( p_vout );
  1862.         if( p_playlist )
  1863.         {
  1864.             /* Modify playlist as well because the vout might have to be
  1865.              * restarted */
  1866.             var_Create( p_playlist, "directx-wallpaper", VLC_VAR_BOOL );
  1867.             var_Set( p_playlist, "directx-wallpaper", newval );
  1868.             pl_Release( p_vout );
  1869.         }
  1870.         p_vout->p_sys->i_changes |= DX_WALLPAPER_CHANGE;
  1871.     }
  1872.     return VLC_SUCCESS;
  1873. }
  1874. /*****************************************************************************
  1875.  * SetPalette: sets an 8 bpp palette
  1876.  *****************************************************************************/
  1877. static void SetPalette( vout_thread_t *p_vout,
  1878.                         uint16_t *red, uint16_t *green, uint16_t *blue )
  1879. {
  1880.     msg_Err( p_vout, "FIXME: SetPalette unimplemented" );
  1881. }