typo fixes
[mplayer/greg.git] / libvo / vo_directx.c
blob57603104d70daf656c6318f8b35eb619bd8b1bb3
1 /******************************************************************************
2 * vo_directx.c: Directx v2 or later DirectDraw interface for MPlayer
3 * Copyright (c) 2002 - 2005 Sascha Sommer <saschasommer@freenet.de>.
5 * This program is free software; you can redistribute it and/or modify
6 * it under the terms of the GNU General Public License as published by
7 * the Free Software Foundation; either version 2 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software
17 * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111, USA.
19 *****************************************************************************/
21 #include <windows.h>
22 #include <windowsx.h>
23 #include <ddraw.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <errno.h>
27 #include "config.h"
28 #include "video_out.h"
29 #include "video_out_internal.h"
30 #include "fastmemcpy.h"
31 #include "input/input.h"
32 #include "osdep/keycodes.h"
33 #include "input/mouse.h"
34 #include "mp_msg.h"
35 #include "aspect.h"
36 #include "geometry.h"
38 #ifndef WM_XBUTTONDOWN
39 # define WM_XBUTTONDOWN 0x020B
40 # define WM_XBUTTONUP 0x020C
41 # define WM_XBUTTONDBLCLK 0x020D
42 #endif
44 #define WNDCLASSNAME_WINDOWED "MPlayer - The Movie Player"
45 #define WNDCLASSNAME_FULLSCREEN "MPlayer - Fullscreen"
46 #define WNDSTYLE WS_OVERLAPPEDWINDOW|WS_SIZEBOX
48 static LPDIRECTDRAWCOLORCONTROL g_cc = NULL; //color control interface
49 static LPDIRECTDRAW7 g_lpdd = NULL; //DirectDraw Object
50 static LPDIRECTDRAWSURFACE7 g_lpddsPrimary = NULL; //Primary Surface: viewport through the Desktop
51 static LPDIRECTDRAWSURFACE7 g_lpddsOverlay = NULL; //Overlay Surface
52 static LPDIRECTDRAWSURFACE7 g_lpddsBack = NULL; //Back surface
53 static LPDIRECTDRAWCLIPPER g_lpddclipper; //clipper object, can only be used without overlay
54 static DDSURFACEDESC2 ddsdsf; //surface descripiton needed for locking
55 static HINSTANCE hddraw_dll; //handle to ddraw.dll
56 static RECT rd; //rect of our stretched image
57 static RECT rs; //rect of our source image
58 static HWND hWnd=NULL; //handle to the window
59 static HWND hWndFS=NULL; //fullscreen window
60 static HBRUSH colorbrush = NULL; // Handle to colorkey brush
61 static HBRUSH blackbrush = NULL; // Handle to black brush
62 static HICON mplayericon = NULL; // Handle to mplayer icon
63 static HCURSOR mplayercursor = NULL; // Handle to mplayer cursor
64 static uint32_t image_width, image_height; //image width and height
65 static uint32_t d_image_width, d_image_height; //image width and height zoomed
66 static uint8_t *image=NULL; //image data
67 static uint32_t image_format=0; //image format
68 static uint32_t primary_image_format;
69 static uint32_t vm_height=0;
70 static uint32_t vm_width=0;
71 static uint32_t vm_bpp=0;
72 static uint32_t dstride; //surface stride
73 static uint32_t nooverlay = 0; //NonOverlay mode
74 static DWORD destcolorkey; //colorkey for our surface
75 static COLORREF windowcolor = RGB(0,0,16); //windowcolor == colorkey
76 static int adapter_count=0;
77 static GUID selected_guid;
78 static GUID *selected_guid_ptr = NULL;
79 static RECT monitor_rect; //monitor coordinates
80 static float window_aspect;
81 static BOOL (WINAPI* myGetMonitorInfo)(HMONITOR, LPMONITORINFO) = NULL;
82 static RECT last_rect = {0xDEADC0DE, 0xDEADC0DE, 0xDEADC0DE, 0xDEADC0DE};
84 extern void mplayer_put_key(int code); //let mplayer handel the keyevents
85 extern void vo_draw_text(int dxs,int dys,void (*draw_alpha)(int x0,int y0, int w,int h, unsigned char* src, unsigned char *srca, int stride));
86 extern int vidmode;
88 /*****************************************************************************
89 * DirectDraw GUIDs.
90 * Defining them here allows us to get rid of the dxguid library during
91 * the linking stage.
92 *****************************************************************************/
93 const GUID IID_IDirectDraw7 =
95 0x15e65ec0,0x3b9c,0x11d2,{0xb9,0x2f,0x00,0x60,0x97,0x97,0xea,0x5b}
98 const GUID IID_IDirectDrawColorControl =
100 0x4b9f0ee0,0x0d7e,0x11d0,{0x9b,0x06,0x00,0xa0,0xc9,0x03,0xa3,0xb8}
104 typedef struct directx_fourcc_caps
106 char* img_format_name; //human readable name
107 uint32_t img_format; //as MPlayer image format
108 uint32_t drv_caps; //what hw supports with this format
109 DDPIXELFORMAT g_ddpfOverlay; //as Directx Sourface description
110 } directx_fourcc_caps;
113 static directx_fourcc_caps g_ddpf[] =
115 {"YV12 ",IMGFMT_YV12 ,0,{sizeof(DDPIXELFORMAT), DDPF_FOURCC,MAKEFOURCC('Y','V','1','2'),0,0,0,0,0}},
116 {"I420 ",IMGFMT_I420 ,0,{sizeof(DDPIXELFORMAT), DDPF_FOURCC,MAKEFOURCC('I','4','2','0'),0,0,0,0,0}}, //yv12 with swapped uv
117 {"IYUV ",IMGFMT_IYUV ,0,{sizeof(DDPIXELFORMAT), DDPF_FOURCC,MAKEFOURCC('I','Y','U','V'),0,0,0,0,0}}, //same as i420
118 {"YVU9 ",IMGFMT_YVU9 ,0,{sizeof(DDPIXELFORMAT), DDPF_FOURCC,MAKEFOURCC('Y','V','U','9'),0,0,0,0,0}},
119 {"YUY2 ",IMGFMT_YUY2 ,0,{sizeof(DDPIXELFORMAT), DDPF_FOURCC,MAKEFOURCC('Y','U','Y','2'),0,0,0,0,0}},
120 {"UYVY ",IMGFMT_UYVY ,0,{sizeof(DDPIXELFORMAT), DDPF_FOURCC,MAKEFOURCC('U','Y','V','Y'),0,0,0,0,0}},
121 {"BGR8 ",IMGFMT_BGR8 ,0,{sizeof(DDPIXELFORMAT), DDPF_RGB, 0, 8, 0x00000000, 0x00000000, 0x00000000, 0}},
122 {"RGB15",IMGFMT_RGB15,0,{sizeof(DDPIXELFORMAT), DDPF_RGB, 0, 16, 0x0000001F, 0x000003E0, 0x00007C00, 0}}, //RGB 5:5:5
123 {"BGR15",IMGFMT_BGR15,0,{sizeof(DDPIXELFORMAT), DDPF_RGB, 0, 16, 0x00007C00, 0x000003E0, 0x0000001F, 0}},
124 {"RGB16",IMGFMT_RGB16,0,{sizeof(DDPIXELFORMAT), DDPF_RGB, 0, 16, 0x0000001F, 0x000007E0, 0x0000F800, 0}}, //RGB 5:6:5
125 {"BGR16",IMGFMT_BGR16,0,{sizeof(DDPIXELFORMAT), DDPF_RGB, 0, 16, 0x0000F800, 0x000007E0, 0x0000001F, 0}},
126 {"RGB24",IMGFMT_RGB24,0,{sizeof(DDPIXELFORMAT), DDPF_RGB, 0, 24, 0x000000FF, 0x0000FF00, 0x00FF0000, 0}},
127 {"BGR24",IMGFMT_BGR24,0,{sizeof(DDPIXELFORMAT), DDPF_RGB, 0, 24, 0x00FF0000, 0x0000FF00, 0x000000FF, 0}},
128 {"RGB32",IMGFMT_RGB32,0,{sizeof(DDPIXELFORMAT), DDPF_RGB, 0, 32, 0x000000FF, 0x0000FF00, 0x00FF0000, 0}},
129 {"BGR32",IMGFMT_BGR32,0,{sizeof(DDPIXELFORMAT), DDPF_RGB, 0, 32, 0x00FF0000, 0x0000FF00, 0x000000FF, 0}}
131 #define NUM_FORMATS (sizeof(g_ddpf) / sizeof(g_ddpf[0]))
133 static vo_info_t info =
135 "Directx DDraw YUV/RGB/BGR renderer",
136 "directx",
137 "Sascha Sommer <saschasommer@freenet.de>",
141 LIBVO_EXTERN(directx)
143 static void draw_alpha(int x0, int y0, int w, int h, unsigned char *src,
144 unsigned char *srca, int stride)
146 switch(image_format) {
147 case IMGFMT_YV12 :
148 case IMGFMT_I420 :
149 case IMGFMT_IYUV :
150 case IMGFMT_YVU9 :
151 vo_draw_alpha_yv12(w,h,src,srca,stride,((uint8_t *) image) + dstride*y0 + x0,dstride);
152 break;
153 case IMGFMT_YUY2 :
154 vo_draw_alpha_yuy2(w,h,src,srca,stride,((uint8_t *) image)+ dstride*y0 + 2*x0 ,dstride);
155 break;
156 case IMGFMT_UYVY :
157 vo_draw_alpha_yuy2(w,h,src,srca,stride,((uint8_t *) image) + dstride*y0 + 2*x0 + 1,dstride);
158 break;
159 case IMGFMT_RGB15:
160 case IMGFMT_BGR15:
161 vo_draw_alpha_rgb15(w,h,src,srca,stride,((uint8_t *) image)+dstride*y0+2*x0,dstride);
162 break;
163 case IMGFMT_RGB16:
164 case IMGFMT_BGR16:
165 vo_draw_alpha_rgb16(w,h,src,srca,stride,((uint8_t *) image)+dstride*y0+2*x0,dstride);
166 break;
167 case IMGFMT_RGB24:
168 case IMGFMT_BGR24:
169 vo_draw_alpha_rgb24(w,h,src,srca,stride,((uint8_t *) image)+dstride*y0+4*x0,dstride);
170 break;
171 case IMGFMT_RGB32:
172 case IMGFMT_BGR32:
173 vo_draw_alpha_rgb32(w,h,src,srca,stride,((uint8_t *) image)+dstride*y0+4*x0,dstride);
174 break;
178 static void draw_osd(void)
180 vo_draw_text(image_width,image_height,draw_alpha);
183 static int
184 query_format(uint32_t format)
186 uint32_t i=0;
187 while ( i < NUM_FORMATS )
189 if (g_ddpf[i].img_format == format)
190 return g_ddpf[i].drv_caps;
191 i++;
193 return 0;
196 static uint32_t Directx_CreatePrimarySurface()
198 DDSURFACEDESC2 ddsd;
199 //cleanup
200 if(g_lpddsPrimary)g_lpddsPrimary->lpVtbl->Release(g_lpddsPrimary);
201 g_lpddsPrimary=NULL;
203 if(vidmode)g_lpdd->lpVtbl->SetDisplayMode(g_lpdd,vm_width,vm_height,vm_bpp,vo_refresh_rate,0);
204 ZeroMemory(&ddsd, sizeof(ddsd));
205 ddsd.dwSize = sizeof(ddsd);
206 //set flags and create a primary surface.
207 ddsd.dwFlags = DDSD_CAPS;
208 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
209 if(g_lpdd->lpVtbl->CreateSurface(g_lpdd,&ddsd, &g_lpddsPrimary, NULL )== DD_OK)
210 mp_msg(MSGT_VO, MSGL_DBG3,"<vo_directx><INFO>primary surface created\n");
211 else
213 mp_msg(MSGT_VO, MSGL_FATAL,"<vo_directx><FATAL ERROR>could not create primary surface\n");
214 return 1;
216 return 0;
219 static uint32_t Directx_CreateOverlay(uint32_t imgfmt)
221 HRESULT ddrval;
222 DDSURFACEDESC2 ddsdOverlay;
223 uint32_t i=0;
224 while ( i < NUM_FORMATS +1 && imgfmt != g_ddpf[i].img_format)
226 i++;
228 if (!g_lpdd || !g_lpddsPrimary)
229 return 1;
230 //cleanup
231 if (g_lpddsOverlay)g_lpddsOverlay->lpVtbl->Release(g_lpddsOverlay);
232 if (g_lpddsBack)g_lpddsBack->lpVtbl->Release(g_lpddsBack);
233 g_lpddsOverlay= NULL;
234 g_lpddsBack = NULL;
235 //create our overlay
236 ZeroMemory(&ddsdOverlay, sizeof(ddsdOverlay));
237 ddsdOverlay.dwSize = sizeof(ddsdOverlay);
238 ddsdOverlay.ddsCaps.dwCaps=DDSCAPS_OVERLAY | DDSCAPS_FLIP | DDSCAPS_COMPLEX | DDSCAPS_VIDEOMEMORY;
239 ddsdOverlay.dwFlags= DDSD_CAPS|DDSD_HEIGHT|DDSD_WIDTH|DDSD_BACKBUFFERCOUNT| DDSD_PIXELFORMAT;
240 ddsdOverlay.dwWidth=image_width;
241 ddsdOverlay.dwHeight=image_height;
242 ddsdOverlay.dwBackBufferCount=2;
243 ddsdOverlay.ddpfPixelFormat=g_ddpf[i].g_ddpfOverlay;
244 if(vo_doublebuffering) //tribblebuffering
246 if (g_lpdd->lpVtbl->CreateSurface(g_lpdd,&ddsdOverlay, &g_lpddsOverlay, NULL)== DD_OK)
248 mp_msg(MSGT_VO, MSGL_V,"<vo_directx><INFO>overlay with format %s created\n",g_ddpf[i].img_format_name);
249 //get the surface directly attached to the primary (the back buffer)
250 ddsdOverlay.ddsCaps.dwCaps = DDSCAPS_BACKBUFFER;
251 if(g_lpddsOverlay->lpVtbl->GetAttachedSurface(g_lpddsOverlay,&ddsdOverlay.ddsCaps, &g_lpddsBack) != DD_OK)
253 mp_msg(MSGT_VO, MSGL_FATAL,"<vo_directx><FATAL ERROR>can't get attached surface\n");
254 return 1;
256 return 0;
258 vo_doublebuffering=0; //disable tribblebuffering
259 mp_msg(MSGT_VO, MSGL_V,"<vo_directx><WARN>cannot create tribblebuffer overlay with format %s\n",g_ddpf[i].img_format_name);
261 //single buffer
262 mp_msg(MSGT_VO, MSGL_V,"<vo_directx><INFO>using singlebuffer overlay\n");
263 ddsdOverlay.dwBackBufferCount=0;
264 ddsdOverlay.ddsCaps.dwCaps=DDSCAPS_OVERLAY | DDSCAPS_VIDEOMEMORY;
265 ddsdOverlay.dwFlags= DDSD_CAPS|DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT;
266 ddsdOverlay.ddpfPixelFormat=g_ddpf[i].g_ddpfOverlay;
267 // try to create the overlay surface
268 ddrval = g_lpdd->lpVtbl->CreateSurface(g_lpdd,&ddsdOverlay, &g_lpddsOverlay, NULL);
269 if(ddrval != DD_OK)
271 if(ddrval == DDERR_INVALIDPIXELFORMAT)mp_msg(MSGT_VO,MSGL_V,"<vo_directx><ERROR> invalid pixelformat: %s\n",g_ddpf[i].img_format_name);
272 else mp_msg(MSGT_VO, MSGL_ERR,"<vo_directx><ERROR>");
273 switch(ddrval)
275 case DDERR_INCOMPATIBLEPRIMARY:
276 {mp_msg(MSGT_VO, MSGL_ERR,"incompatible primary surface\n");break;}
277 case DDERR_INVALIDCAPS:
278 {mp_msg(MSGT_VO, MSGL_ERR,"invalid caps\n");break;}
279 case DDERR_INVALIDOBJECT:
280 {mp_msg(MSGT_VO, MSGL_ERR,"invalid object\n");break;}
281 case DDERR_INVALIDPARAMS:
282 {mp_msg(MSGT_VO, MSGL_ERR,"invalid parameters\n");break;}
283 case DDERR_NODIRECTDRAWHW:
284 {mp_msg(MSGT_VO, MSGL_ERR,"no directdraw hardware\n");break;}
285 case DDERR_NOEMULATION:
286 {mp_msg(MSGT_VO, MSGL_ERR,"can't emulate\n");break;}
287 case DDERR_NOFLIPHW:
288 {mp_msg(MSGT_VO, MSGL_ERR,"hardware can't do flip\n");break;}
289 case DDERR_NOOVERLAYHW:
290 {mp_msg(MSGT_VO, MSGL_ERR,"hardware can't do overlay\n");break;}
291 case DDERR_OUTOFMEMORY:
292 {mp_msg(MSGT_VO, MSGL_ERR,"not enough system memory\n");break;}
293 case DDERR_UNSUPPORTEDMODE:
294 {mp_msg(MSGT_VO, MSGL_ERR,"unsupported mode\n");break;}
295 case DDERR_OUTOFVIDEOMEMORY:
296 {mp_msg(MSGT_VO, MSGL_ERR,"not enough video memory\n");break;}
297 default:
298 mp_msg(MSGT_VO, MSGL_ERR,"create surface failed with 0x%x\n",ddrval);
300 return 1;
302 g_lpddsBack = g_lpddsOverlay;
303 return 0;
306 static uint32_t Directx_CreateBackpuffer()
308 DDSURFACEDESC2 ddsd;
309 //cleanup
310 if (g_lpddsBack)g_lpddsBack->lpVtbl->Release(g_lpddsBack);
311 g_lpddsBack=NULL;
312 ZeroMemory(&ddsd, sizeof(ddsd));
313 ddsd.dwSize = sizeof(ddsd);
314 ddsd.ddsCaps.dwCaps= DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY;
315 ddsd.dwFlags= DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
316 ddsd.dwWidth=image_width;
317 ddsd.dwHeight=image_height;
318 if(g_lpdd->lpVtbl->CreateSurface( g_lpdd, &ddsd, &g_lpddsBack, 0 ) != DD_OK )
320 mp_msg(MSGT_VO, MSGL_FATAL,"<vo_directx><FATAL ERROR>can't create backpuffer\n");
321 return 1;
323 mp_msg(MSGT_VO, MSGL_DBG3,"<vo_directx><INFO>backbuffer created\n");
324 return 0;
327 static void uninit(void)
329 if (g_cc != NULL)
331 g_cc->lpVtbl->Release(g_cc);
333 g_cc=NULL;
334 if (g_lpddclipper != NULL) g_lpddclipper->lpVtbl->Release(g_lpddclipper);
335 g_lpddclipper = NULL;
336 mp_msg(MSGT_VO, MSGL_DBG3,"<vo_directx><INFO>clipper released\n");
337 if (g_lpddsBack != NULL) g_lpddsBack->lpVtbl->Release(g_lpddsBack);
338 g_lpddsBack = NULL;
339 mp_msg(MSGT_VO, MSGL_DBG3,"<vo_directx><INFO>back surface released\n");
340 if(vo_doublebuffering && !nooverlay)
342 if (g_lpddsOverlay != NULL)g_lpddsOverlay->lpVtbl->Release(g_lpddsOverlay);
343 g_lpddsOverlay = NULL;
344 mp_msg(MSGT_VO, MSGL_DBG3,"<vo_directx><INFO>overlay surface released\n");
346 if (g_lpddsPrimary != NULL) g_lpddsPrimary->lpVtbl->Release(g_lpddsPrimary);
347 g_lpddsPrimary = NULL;
348 mp_msg(MSGT_VO, MSGL_DBG3,"<vo_directx><INFO>primary released\n");
349 if(hWndFS)DestroyWindow(hWndFS);
350 hWndFS = NULL;
351 if((WinID == -1) && hWnd) DestroyWindow(hWnd);
352 hWnd = NULL;
353 mp_msg(MSGT_VO, MSGL_DBG3,"<vo_directx><INFO>window destroyed\n");
354 UnregisterClass(WNDCLASSNAME_WINDOWED, GetModuleHandle(NULL));
355 UnregisterClass(WNDCLASSNAME_FULLSCREEN, GetModuleHandle(NULL));
356 if (mplayericon) DestroyIcon(mplayericon);
357 mplayericon = NULL;
358 if (mplayercursor) DestroyCursor(mplayercursor);
359 mplayercursor = NULL;
360 if (blackbrush) DeleteObject(blackbrush);
361 blackbrush = NULL;
362 if (colorbrush) DeleteObject(colorbrush);
363 colorbrush = NULL;
364 mp_msg(MSGT_VO, MSGL_DBG3,"<vo_directx><INFO>GDI resources deleted\n");
365 if (g_lpdd != NULL){
366 if(vidmode)g_lpdd->lpVtbl->RestoreDisplayMode(g_lpdd);
367 g_lpdd->lpVtbl->Release(g_lpdd);
369 mp_msg(MSGT_VO, MSGL_DBG3,"<vo_directx><INFO>directdrawobject released\n");
370 FreeLibrary( hddraw_dll);
371 hddraw_dll= NULL;
372 mp_msg(MSGT_VO, MSGL_DBG3,"<vo_directx><INFO>ddraw.dll freed\n");
373 mp_msg(MSGT_VO, MSGL_DBG3,"<vo_directx><INFO>uninited\n");
376 static BOOL WINAPI EnumCallbackEx(GUID FAR *lpGUID, LPSTR lpDriverDescription, LPSTR lpDriverName, LPVOID lpContext, HMONITOR hm)
378 mp_msg(MSGT_VO, MSGL_INFO ,"<vo_directx> adapter %d: ", adapter_count);
380 if (!lpGUID)
382 mp_msg(MSGT_VO, MSGL_INFO ,"%s", "Primary Display Adapter");
384 else
386 mp_msg(MSGT_VO, MSGL_INFO ,"%s", lpDriverDescription);
389 if(adapter_count == vo_adapter_num){
390 MONITORINFO mi;
391 if (!lpGUID)
392 selected_guid_ptr = NULL;
393 else
395 selected_guid = *lpGUID;
396 selected_guid_ptr = &selected_guid;
398 mi.cbSize = sizeof(mi);
400 if (myGetMonitorInfo(hm, &mi)) {
401 monitor_rect = mi.rcMonitor;
403 mp_msg(MSGT_VO, MSGL_INFO ,"\t\t<--");
405 mp_msg(MSGT_VO, MSGL_INFO ,"\n");
407 adapter_count++;
409 return 1; // list all adapters
412 static uint32_t Directx_InitDirectDraw()
414 HRESULT (WINAPI *OurDirectDrawCreateEx)(GUID *,LPVOID *, REFIID,IUnknown FAR *);
415 LPDIRECTDRAW lpDDraw;
416 DDSURFACEDESC2 ddsd;
417 LPDIRECTDRAWENUMERATEEX OurDirectDrawEnumerateEx;
418 HINSTANCE user32dll=LoadLibrary("user32.dll");
420 adapter_count = 0;
421 if(user32dll){
422 myGetMonitorInfo=GetProcAddress(user32dll,"GetMonitorInfoA");
423 if(!myGetMonitorInfo && vo_adapter_num){
424 mp_msg(MSGT_VO, MSGL_ERR, "<vo_directx> -adapter is not supported on Win95\n");
425 vo_adapter_num = 0;
429 mp_msg(MSGT_VO, MSGL_DBG3,"<vo_directx><INFO>Initing DirectDraw\n" );
431 //load direct draw DLL: based on videolans code
432 hddraw_dll = LoadLibrary("DDRAW.DLL");
433 if( hddraw_dll == NULL )
435 mp_msg(MSGT_VO, MSGL_FATAL,"<vo_directx><FATAL ERROR>failed loading ddraw.dll\n" );
436 return 1;
439 last_rect.left = 0xDEADC0DE; // reset window position cache
441 if(vo_adapter_num){ //display other than default
442 OurDirectDrawEnumerateEx = (LPDIRECTDRAWENUMERATEEX) GetProcAddress(hddraw_dll,"DirectDrawEnumerateExA");
443 if (!OurDirectDrawEnumerateEx){
444 FreeLibrary( hddraw_dll );
445 hddraw_dll = NULL;
446 mp_msg(MSGT_VO, MSGL_FATAL,"<vo_directx><FATAL ERROR>failed geting proc address: DirectDrawEnumerateEx\n");
447 mp_msg(MSGT_VO, MSGL_FATAL,"<vo_directx><FATAL ERROR>no directx 7 or higher installed\n");
448 return 1;
451 // enumerate all display devices attached to the desktop
452 OurDirectDrawEnumerateEx(EnumCallbackEx, NULL, DDENUM_ATTACHEDSECONDARYDEVICES );
454 if(vo_adapter_num >= adapter_count)
455 mp_msg(MSGT_VO, MSGL_ERR,"Selected adapter (%d) doesn't exist: Default Display Adapter selected\n",vo_adapter_num);
457 FreeLibrary(user32dll);
459 OurDirectDrawCreateEx = (void *)GetProcAddress(hddraw_dll, "DirectDrawCreateEx");
460 if ( OurDirectDrawCreateEx == NULL )
462 FreeLibrary( hddraw_dll );
463 hddraw_dll = NULL;
464 mp_msg(MSGT_VO, MSGL_FATAL,"<vo_directx><FATAL ERROR>failed geting proc address: DirectDrawCreateEx\n");
465 return 1;
468 // initialize DirectDraw and create directx v7 object
469 if (OurDirectDrawCreateEx(selected_guid_ptr, (VOID**)&g_lpdd, &IID_IDirectDraw7, NULL ) != DD_OK )
471 FreeLibrary( hddraw_dll );
472 hddraw_dll = NULL;
473 mp_msg(MSGT_VO, MSGL_FATAL,"<vo_directx><FATAL ERROR>can't initialize ddraw\n");
474 return 1;
477 //get current screen siz for selected monitor ...
478 ddsd.dwSize=sizeof(ddsd);
479 ddsd.dwFlags=DDSD_WIDTH|DDSD_HEIGHT|DDSD_PIXELFORMAT;
480 g_lpdd->lpVtbl->GetDisplayMode(g_lpdd, &ddsd);
481 if(vo_screenwidth && vo_screenheight)
483 vm_height=vo_screenheight;
484 vm_width=vo_screenwidth;
486 else
488 vm_height=ddsd.dwHeight;
489 vm_width=ddsd.dwWidth;
493 if(vo_dbpp)vm_bpp=vo_dbpp;
494 else vm_bpp=ddsd.ddpfPixelFormat.dwRGBBitCount;
496 if(vidmode){
497 if (g_lpdd->lpVtbl->SetCooperativeLevel(g_lpdd, hWnd, DDSCL_EXCLUSIVE|DDSCL_FULLSCREEN) != DD_OK)
499 mp_msg(MSGT_VO, MSGL_FATAL,"<vo_directx><FATAL ERROR>can't set cooperativelevel for exclusive mode\n");
500 return 1;
502 /*SetDisplayMode(ddobject,width,height,bpp,refreshrate,aditionalflags)*/
503 if(g_lpdd->lpVtbl->SetDisplayMode(g_lpdd,vm_width, vm_height, vm_bpp,0,0) != DD_OK)
505 mp_msg(MSGT_VO, MSGL_FATAL,"<vo_directx><FATAL ERROR>can't set displaymode\n");
506 return 1;
508 mp_msg(MSGT_VO, MSGL_V,"<vo_directx><INFO>Inited adapter %i for %i x %i @ %i \n",vo_adapter_num,vm_width,vm_height,vm_bpp);
509 return 0;
511 if (g_lpdd->lpVtbl->SetCooperativeLevel(g_lpdd, hWnd, DDSCL_NORMAL) != DD_OK) // or DDSCL_SETFOCUSWINDOW
513 mp_msg(MSGT_VO, MSGL_FATAL,"<vo_directx><FATAL ERROR>could not set cooperativelevel for hardwarecheck\n");
514 return 1;
516 mp_msg(MSGT_VO, MSGL_DBG3,"<vo_directx><INFO>DirectDraw Inited\n");
517 return 0;
520 static void check_events(void)
522 MSG msg;
523 while (PeekMessage(&msg, NULL, 0, 0,PM_REMOVE))
525 TranslateMessage(&msg);
526 DispatchMessage(&msg);
530 static uint32_t Directx_ManageDisplay()
532 HRESULT ddrval;
533 DDCAPS capsDrv;
534 DDOVERLAYFX ovfx;
535 DWORD dwUpdateFlags=0;
536 int width,height;
538 if(!vidmode && !vo_fs && WinID!=-1) {
539 RECT current_rect = {0, 0, 0, 0};
540 GetWindowRect(hWnd, &current_rect);
541 if ((current_rect.left == last_rect.left)
542 && (current_rect.top == last_rect.top)
543 && (current_rect.right == last_rect.right)
544 && (current_rect.bottom == last_rect.bottom))
545 return 0;
546 last_rect = current_rect;
549 if(vo_fs || vidmode){
550 aspect(&width,&height,A_ZOOM);
551 rd.left=(vo_screenwidth-width)/2;
552 rd.top=(vo_screenheight-height)/2;
553 if (WinID == -1)
554 if(ShowCursor(FALSE)>=0)while(ShowCursor(FALSE)>=0){}
556 else if (WinID != -1 && vo_geometry) {
557 POINT pt;
558 pt.x = vo_dx;
559 pt.y = vo_dy;
560 ClientToScreen(hWnd,&pt);
561 width=d_image_width;
562 height=d_image_height;
563 rd.left = pt.x;
564 rd.top = pt.y;
565 while(ShowCursor(TRUE)<=0){}
567 else {
568 POINT pt;
569 pt.x = 0; //overlayposition relative to the window
570 pt.y = 0;
571 ClientToScreen(hWnd,&pt);
572 GetClientRect(hWnd, &rd);
573 width=rd.right - rd.left;
574 height=rd.bottom - rd.top;
575 pt.x -= monitor_rect.left; /* move coordinates from global to local monitor space */
576 pt.y -= monitor_rect.top;
577 rd.right -= monitor_rect.left;
578 rd.bottom -= monitor_rect.top;
579 rd.left = pt.x;
580 rd.top = pt.y;
581 if(!nooverlay && (!width || !height)){
582 /*window is minimized*/
583 ddrval = g_lpddsOverlay->lpVtbl->UpdateOverlay(g_lpddsOverlay,NULL, g_lpddsPrimary, NULL, DDOVER_HIDE, NULL);
584 return 0;
586 if(vo_keepaspect){
587 int tmpheight=((float)width/window_aspect);
588 tmpheight+=tmpheight%2;
589 if(tmpheight > height){
590 width=((float)height*window_aspect);
591 width+=width%2;
593 else height=tmpheight;
595 if (WinID == -1)
596 while(ShowCursor(TRUE)<=0){}
598 rd.right=rd.left+width;
599 rd.bottom=rd.top+height;
601 /*ok, let's workaround some overlay limitations*/
602 if(!nooverlay)
604 uint32_t uStretchFactor1000; //minimum stretch
605 uint32_t xstretch1000,ystretch1000;
606 /*get driver capabilities*/
607 ZeroMemory(&capsDrv, sizeof(capsDrv));
608 capsDrv.dwSize = sizeof(capsDrv);
609 if(g_lpdd->lpVtbl->GetCaps(g_lpdd,&capsDrv, NULL) != DD_OK)return 1;
610 /*get minimum stretch, depends on display adaptor and mode (refresh rate!) */
611 uStretchFactor1000 = capsDrv.dwMinOverlayStretch>1000 ? capsDrv.dwMinOverlayStretch : 1000;
612 rd.right = ((width+rd.left)*uStretchFactor1000+999)/1000;
613 rd.bottom = (height+rd.top)*uStretchFactor1000/1000;
614 /*calculate xstretch1000 and ystretch1000*/
615 xstretch1000 = ((rd.right - rd.left)* 1000)/image_width ;
616 ystretch1000 = ((rd.bottom - rd.top)* 1000)/image_height;
617 rs.left=0;
618 rs.right=image_width;
619 rs.top=0;
620 rs.bottom=image_height;
621 if(rd.left < 0)rs.left=(-rd.left*1000)/xstretch1000;
622 if(rd.top < 0)rs.top=(-rd.top*1000)/ystretch1000;
623 if(rd.right > vo_screenwidth)rs.right=((vo_screenwidth-rd.left)*1000)/xstretch1000;
624 if(rd.bottom > vo_screenheight)rs.bottom=((vo_screenheight-rd.top)*1000)/ystretch1000;
625 /*do not allow to zoom or shrink if hardware isn't able to do so*/
626 if((width < image_width)&& !(capsDrv.dwFXCaps & DDFXCAPS_OVERLAYSHRINKX))
628 if(capsDrv.dwFXCaps & DDFXCAPS_OVERLAYSHRINKXN)mp_msg(MSGT_VO, MSGL_ERR,"<vo_directx><ERROR>can only shrinkN\n");
629 else mp_msg(MSGT_VO, MSGL_ERR,"<vo_directx><ERROR>can't shrink x\n");
630 rd.right=rd.left+image_width;
632 else if((width > image_width)&& !(capsDrv.dwFXCaps & DDFXCAPS_OVERLAYSTRETCHX))
634 if(capsDrv.dwFXCaps & DDFXCAPS_OVERLAYSTRETCHXN)mp_msg(MSGT_VO, MSGL_ERR,"<vo_directx><ERROR>can only stretchN\n");
635 else mp_msg(MSGT_VO, MSGL_ERR,"<vo_directx><ERROR>can't stretch x\n");
636 rd.right = rd.left+image_width;
638 if((height < image_height) && !(capsDrv.dwFXCaps & DDFXCAPS_OVERLAYSHRINKY))
640 if(capsDrv.dwFXCaps & DDFXCAPS_OVERLAYSHRINKYN)mp_msg(MSGT_VO, MSGL_ERR,"<vo_directx><ERROR>can only shrinkN\n");
641 else mp_msg(MSGT_VO, MSGL_ERR,"<vo_directx><ERROR>can't shrink y\n");
642 rd.bottom = rd.top + image_height;
644 else if((height > image_height ) && !(capsDrv.dwFXCaps & DDFXCAPS_OVERLAYSTRETCHY))
646 if(capsDrv.dwFXCaps & DDFXCAPS_OVERLAYSTRETCHYN)mp_msg(MSGT_VO, MSGL_ERR,"<vo_directx><ERROR>can only stretchN\n");
647 else mp_msg(MSGT_VO, MSGL_ERR,"<vo_directx><ERROR>can't stretch y\n");
648 rd.bottom = rd.top + image_height;
650 /*the last thing to check are alignment restrictions
651 these expressions (x & -y) just do alignment by dropping low order bits...
652 so to round up, we add first, then truncate*/
653 if((capsDrv.dwCaps & DDCAPS_ALIGNBOUNDARYSRC) && capsDrv.dwAlignBoundarySrc)
654 rs.left = (rs.left + capsDrv.dwAlignBoundarySrc / 2) & -(signed)(capsDrv.dwAlignBoundarySrc);
655 if((capsDrv.dwCaps & DDCAPS_ALIGNSIZESRC) && capsDrv.dwAlignSizeSrc)
656 rs.right = rs.left + ((rs.right - rs.left + capsDrv.dwAlignSizeSrc / 2) & -(signed) (capsDrv.dwAlignSizeSrc));
657 if((capsDrv.dwCaps & DDCAPS_ALIGNBOUNDARYDEST) && capsDrv.dwAlignBoundaryDest)
658 rd.left = (rd.left + capsDrv.dwAlignBoundaryDest / 2) & -(signed)(capsDrv.dwAlignBoundaryDest);
659 if((capsDrv.dwCaps & DDCAPS_ALIGNSIZEDEST) && capsDrv.dwAlignSizeDest)
660 rd.right = rd.left + ((rd.right - rd.left) & -(signed) (capsDrv.dwAlignSizeDest));
661 /*create an overlay FX structure to specify a destination color key*/
662 ZeroMemory(&ovfx, sizeof(ovfx));
663 ovfx.dwSize = sizeof(ovfx);
664 if(vo_fs||vidmode)
666 ovfx.dckDestColorkey.dwColorSpaceLowValue = 0;
667 ovfx.dckDestColorkey.dwColorSpaceHighValue = 0;
669 else
671 ovfx.dckDestColorkey.dwColorSpaceLowValue = destcolorkey;
672 ovfx.dckDestColorkey.dwColorSpaceHighValue = destcolorkey;
674 // set the flags we'll send to UpdateOverlay //DDOVER_AUTOFLIP|DDOVERFX_MIRRORLEFTRIGHT|DDOVERFX_MIRRORUPDOWN could be usefull?;
675 dwUpdateFlags = DDOVER_SHOW | DDOVER_DDFX;
676 /*if hardware can't do colorkeying set the window on top*/
677 if(capsDrv.dwCKeyCaps & DDCKEYCAPS_DESTOVERLAY) dwUpdateFlags |= DDOVER_KEYDESTOVERRIDE;
678 else vo_ontop = 1;
680 else
682 g_lpddclipper->lpVtbl->SetHWnd(g_lpddclipper, 0,(vo_fs && !vidmode)?hWndFS: hWnd);
685 if(!vidmode && !vo_fs){
686 if(WinID == -1) {
687 RECT rdw=rd;
688 if (vo_border)
689 AdjustWindowRect(&rdw,WNDSTYLE,FALSE);
690 // printf("window: %i %i %ix%i\n",rdw.left,rdw.top,rdw.right - rdw.left,rdw.bottom - rdw.top);
691 rdw.left += monitor_rect.left; /* move to global coordinate space */
692 rdw.top += monitor_rect.top;
693 rdw.right += monitor_rect.left;
694 rdw.bottom += monitor_rect.top;
695 SetWindowPos(hWnd,(vo_ontop)?HWND_TOPMOST:(vo_rootwin?HWND_BOTTOM:HWND_NOTOPMOST),rdw.left,rdw.top,rdw.right-rdw.left,rdw.bottom-rdw.top,SWP_NOOWNERZORDER);
698 else SetWindowPos(vidmode?hWnd:hWndFS,vo_rootwin?HWND_BOTTOM:HWND_TOPMOST,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE|SWP_NOOWNERZORDER|SWP_NOCOPYBITS);
700 /*make sure the overlay is inside the screen*/
701 if(rd.left<0)rd.left=0;
702 if(rd.right>vo_screenwidth)rd.right=vo_screenwidth;
703 if(rd.top<0)rd.top=0;
704 if(rd.bottom>vo_screenheight)rd.bottom=vo_screenheight;
706 /*for nonoverlay mode we are finished, for overlay mode we have to display the overlay first*/
707 if(nooverlay)return 0;
709 // printf("overlay: %i %i %ix%i\n",rd.left,rd.top,rd.right - rd.left,rd.bottom - rd.top);
710 ddrval = g_lpddsOverlay->lpVtbl->UpdateOverlay(g_lpddsOverlay,&rs, g_lpddsPrimary, &rd, dwUpdateFlags, &ovfx);
711 if(FAILED(ddrval))
713 // one cause might be the driver lied about minimum stretch
714 // we should try upping the destination size a bit, or
715 // perhaps shrinking the source size
716 mp_msg(MSGT_VO, MSGL_ERR ,"<vo_directx><ERROR>UpdateOverlay failed\n" );
717 mp_msg(MSGT_VO, MSGL_ERR ,"<vo_directx><ERROR>Overlay:x1:%i,y1:%i,x2:%i,y2:%i,w:%i,h:%i\n",rd.left,rd.top,rd.right,rd.bottom,rd.right - rd.left,rd.bottom - rd.top );
718 mp_msg(MSGT_VO, MSGL_ERR ,"<vo_directx><ERROR>");
719 switch (ddrval)
721 case DDERR_NOSTRETCHHW:
722 {mp_msg(MSGT_VO, MSGL_ERR ,"hardware can't stretch: try to size the window back\n");break;}
723 case DDERR_INVALIDRECT:
724 {mp_msg(MSGT_VO, MSGL_ERR ,"invalid rectangle\n");break;}
725 case DDERR_INVALIDPARAMS:
726 {mp_msg(MSGT_VO, MSGL_ERR ,"invalid parameters\n");break;}
727 case DDERR_HEIGHTALIGN:
728 {mp_msg(MSGT_VO, MSGL_ERR ,"height align\n");break;}
729 case DDERR_XALIGN:
730 {mp_msg(MSGT_VO, MSGL_ERR ,"x align\n");break;}
731 case DDERR_UNSUPPORTED:
732 {mp_msg(MSGT_VO, MSGL_ERR ,"unsupported\n");break;}
733 case DDERR_INVALIDSURFACETYPE:
734 {mp_msg(MSGT_VO, MSGL_ERR ,"invalid surfacetype\n");break;}
735 case DDERR_INVALIDOBJECT:
736 {mp_msg(MSGT_VO, MSGL_ERR ,"invalid object\n");break;}
737 case DDERR_SURFACELOST:
739 mp_msg(MSGT_VO, MSGL_ERR ,"surfaces lost\n");
740 g_lpddsOverlay->lpVtbl->Restore( g_lpddsOverlay ); //restore and try again
741 g_lpddsPrimary->lpVtbl->Restore( g_lpddsPrimary );
742 ddrval = g_lpddsOverlay->lpVtbl->UpdateOverlay(g_lpddsOverlay,&rs, g_lpddsPrimary, &rd, dwUpdateFlags, &ovfx);
743 if(ddrval !=DD_OK)mp_msg(MSGT_VO, MSGL_FATAL ,"<vo_directx><FATAL ERROR>UpdateOverlay failed again\n" );
744 break;
746 default:
747 mp_msg(MSGT_VO, MSGL_ERR ," 0x%x\n",ddrval);
749 /*ok we can't do anything about it -> hide overlay*/
750 if(ddrval != DD_OK)
752 ddrval = g_lpddsOverlay->lpVtbl->UpdateOverlay(g_lpddsOverlay,NULL, g_lpddsPrimary, NULL, DDOVER_HIDE, NULL);
753 return 1;
756 return 0;
759 //find out supported overlay pixelformats
760 static uint32_t Directx_CheckOverlayPixelformats()
762 DDCAPS capsDrv;
763 HRESULT ddrval;
764 DDSURFACEDESC2 ddsdOverlay;
765 uint32_t i;
766 uint32_t formatcount = 0;
767 //get driver caps to determine overlay support
768 ZeroMemory(&capsDrv, sizeof(capsDrv));
769 capsDrv.dwSize = sizeof(capsDrv);
770 ddrval = g_lpdd->lpVtbl->GetCaps(g_lpdd,&capsDrv, NULL);
771 if (FAILED(ddrval))
773 mp_msg(MSGT_VO, MSGL_ERR ,"<vo_directx><ERROR>failed getting ddrawcaps\n");
774 return 1;
776 if (!(capsDrv.dwCaps & DDCAPS_OVERLAY))
778 mp_msg(MSGT_VO, MSGL_ERR ,"<vo_directx><ERROR>Your card doesn't support overlay\n");
779 return 1;
781 mp_msg(MSGT_VO, MSGL_V ,"<vo_directx><INFO>testing supported overlay pixelformats\n");
782 //it is not possible to query for pixel formats supported by the
783 //overlay hardware: try out various formats till one works
784 ZeroMemory(&ddsdOverlay, sizeof(ddsdOverlay));
785 ddsdOverlay.dwSize = sizeof(ddsdOverlay);
786 ddsdOverlay.ddsCaps.dwCaps=DDSCAPS_OVERLAY | DDSCAPS_VIDEOMEMORY;
787 ddsdOverlay.dwFlags= DDSD_CAPS|DDSD_HEIGHT|DDSD_WIDTH| DDSD_PIXELFORMAT;
788 ddsdOverlay.dwWidth=300;
789 ddsdOverlay.dwHeight=280;
790 ddsdOverlay.dwBackBufferCount=0;
791 //try to create an overlay surface using one of the pixel formats in our global list
792 i=0;
795 ddsdOverlay.ddpfPixelFormat=g_ddpf[i].g_ddpfOverlay;
796 ddrval = g_lpdd->lpVtbl->CreateSurface(g_lpdd,&ddsdOverlay, &g_lpddsOverlay, NULL);
797 if (ddrval == DD_OK)
799 mp_msg(MSGT_VO, MSGL_V ,"<vo_directx><FORMAT OVERLAY>%i %s supported\n",i,g_ddpf[i].img_format_name);
800 g_ddpf[i].drv_caps = VFCAP_CSP_SUPPORTED |VFCAP_OSD |VFCAP_CSP_SUPPORTED_BY_HW | VFCAP_HWSCALE_UP;
801 formatcount++;}
802 else mp_msg(MSGT_VO, MSGL_V ,"<vo_directx><FORMAT OVERLAY>%i %s not supported\n",i,g_ddpf[i].img_format_name);
803 if (g_lpddsOverlay != NULL) {g_lpddsOverlay->lpVtbl->Release(g_lpddsOverlay);g_lpddsOverlay = NULL;}
804 } while( ++i < NUM_FORMATS );
805 mp_msg(MSGT_VO, MSGL_V ,"<vo_directx><INFO>Your card supports %i of %i overlayformats\n",formatcount, NUM_FORMATS);
806 if (formatcount == 0)
808 mp_msg(MSGT_VO, MSGL_V ,"<vo_directx><WARN>Your card supports overlay, but we couldn't create one\n");
809 mp_msg(MSGT_VO, MSGL_V ,"<vo_directx><INFO>This can have the following reasons:\n");
810 mp_msg(MSGT_VO, MSGL_V ,"<vo_directx><INFO>- you are already using an overlay with another app\n");
811 mp_msg(MSGT_VO, MSGL_V ,"<vo_directx><INFO>- you don't have enough videomemory\n");
812 mp_msg(MSGT_VO, MSGL_V ,"<vo_directx><INFO>- vo_directx doesn't support the cards overlay pixelformat\n");
813 return 1;
815 if(capsDrv.dwFXCaps & DDFXCAPS_OVERLAYMIRRORLEFTRIGHT)mp_msg(MSGT_VO, MSGL_V ,"<vo_directx><INFO>can mirror left right\n"); //I don't have hardware which
816 if(capsDrv.dwFXCaps & DDFXCAPS_OVERLAYMIRRORUPDOWN )mp_msg(MSGT_VO, MSGL_V ,"<vo_directx><INFO>can mirror up down\n"); //supports those send me one and I'll implement ;)
817 return 0;
820 //find out the Pixelformat of the Primary Surface
821 static uint32_t Directx_CheckPrimaryPixelformat()
823 uint32_t i=0;
824 uint32_t formatcount = 0;
825 DDPIXELFORMAT ddpf;
826 DDSURFACEDESC2 ddsd;
827 HDC hdc;
828 HRESULT hres;
829 COLORREF rgbT=RGB(0,0,0);
830 mp_msg(MSGT_VO, MSGL_V ,"<vo_directx><INFO>checking primary surface\n");
831 memset( &ddpf, 0, sizeof( DDPIXELFORMAT ));
832 ddpf.dwSize = sizeof( DDPIXELFORMAT );
833 //we have to create a primary surface first
834 if(Directx_CreatePrimarySurface()!=0)return 1;
835 if(g_lpddsPrimary->lpVtbl->GetPixelFormat( g_lpddsPrimary, &ddpf ) != DD_OK )
837 mp_msg(MSGT_VO, MSGL_FATAL ,"<vo_directx><FATAL ERROR>can't get pixelformat\n");
838 return 1;
840 while ( i < NUM_FORMATS )
842 if (g_ddpf[i].g_ddpfOverlay.dwRGBBitCount == ddpf.dwRGBBitCount)
844 if (g_ddpf[i].g_ddpfOverlay.dwRBitMask == ddpf.dwRBitMask)
846 mp_msg(MSGT_VO, MSGL_V ,"<vo_directx><FORMAT PRIMARY>%i %s supported\n",i,g_ddpf[i].img_format_name);
847 g_ddpf[i].drv_caps = VFCAP_CSP_SUPPORTED |VFCAP_OSD;
848 formatcount++;
849 primary_image_format=g_ddpf[i].img_format;
852 i++;
854 //get the colorkey for overlay mode
855 destcolorkey = CLR_INVALID;
856 if (windowcolor != CLR_INVALID && g_lpddsPrimary->lpVtbl->GetDC(g_lpddsPrimary,&hdc) == DD_OK)
858 rgbT = GetPixel(hdc, 0, 0);
859 SetPixel(hdc, 0, 0, windowcolor);
860 g_lpddsPrimary->lpVtbl->ReleaseDC(g_lpddsPrimary,hdc);
862 // read back the converted color
863 ddsd.dwSize = sizeof(ddsd);
864 while ((hres = g_lpddsPrimary->lpVtbl->Lock(g_lpddsPrimary,NULL, &ddsd, 0, NULL)) == DDERR_WASSTILLDRAWING)
866 if (hres == DD_OK)
868 destcolorkey = *(DWORD *) ddsd.lpSurface;
869 if (ddsd.ddpfPixelFormat.dwRGBBitCount < 32)
870 destcolorkey &= (1 << ddsd.ddpfPixelFormat.dwRGBBitCount) - 1;
871 g_lpddsPrimary->lpVtbl->Unlock(g_lpddsPrimary,NULL);
873 if (windowcolor != CLR_INVALID && g_lpddsPrimary->lpVtbl->GetDC(g_lpddsPrimary,&hdc) == DD_OK)
875 SetPixel(hdc, 0, 0, rgbT);
876 g_lpddsPrimary->lpVtbl->ReleaseDC(g_lpddsPrimary,hdc);
878 //release primary
879 g_lpddsPrimary->lpVtbl->Release(g_lpddsPrimary);
880 g_lpddsPrimary = NULL;
881 if(formatcount==0)
883 mp_msg(MSGT_VO, MSGL_FATAL ,"<vo_directx><FATAL ERROR>Unknown Pixelformat\n");
884 return 1;
886 return 0;
889 //function handles input
890 static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
892 switch (message)
894 case WM_MOUSEACTIVATE:
895 return MA_ACTIVATEANDEAT;
896 case WM_NCACTIVATE:
898 if(vidmode && adapter_count > 2) //only disable if more than one adapter.
899 return 0;
900 break;
902 case WM_DESTROY:
904 PostQuitMessage(0);
905 return 0;
907 case WM_CLOSE:
909 mplayer_put_key(KEY_CLOSE_WIN);
910 return 0;
912 case WM_WINDOWPOSCHANGED:
914 //printf("Windowposchange\n");
915 if(g_lpddsBack != NULL) //or it will crash with -vm
917 Directx_ManageDisplay();
919 break;
921 case WM_SYSCOMMAND:
923 switch (wParam)
924 { //kill screensaver etc.
925 //note: works only when the window is active
926 //you can workaround this by disabling the allow screensaver option in
927 //the link to the app
928 case SC_SCREENSAVE:
929 case SC_MONITORPOWER:
930 mp_msg(MSGT_VO, MSGL_V ,"<vo_directx><INFO>killing screensaver\n" );
931 return 0;
932 case SC_MAXIMIZE:
933 if (!vo_fs) control(VOCTRL_FULLSCREEN, NULL);
934 return 0;
936 break;
938 case WM_KEYDOWN:
940 switch (wParam)
942 case VK_LEFT:
943 {mplayer_put_key(KEY_LEFT);break;}
944 case VK_UP:
945 {mplayer_put_key(KEY_UP);break;}
946 case VK_RIGHT:
947 {mplayer_put_key(KEY_RIGHT);break;}
948 case VK_DOWN:
949 {mplayer_put_key(KEY_DOWN);break;}
950 case VK_TAB:
951 {mplayer_put_key(KEY_TAB);break;}
952 case VK_BACK:
953 {mplayer_put_key(KEY_BS);break;}
954 case VK_DELETE:
955 {mplayer_put_key(KEY_DELETE);break;}
956 case VK_INSERT:
957 {mplayer_put_key(KEY_INSERT);break;}
958 case VK_HOME:
959 {mplayer_put_key(KEY_HOME);break;}
960 case VK_END:
961 {mplayer_put_key(KEY_END);break;}
962 case VK_PRIOR:
963 {mplayer_put_key(KEY_PAGE_UP);break;}
964 case VK_NEXT:
965 {mplayer_put_key(KEY_PAGE_DOWN);break;}
966 case VK_ESCAPE:
967 {mplayer_put_key(KEY_ESC);break;}
969 break;
971 case WM_CHAR:
973 mplayer_put_key(wParam);
974 break;
976 case WM_LBUTTONDOWN:
978 if (!vo_nomouse_input)
979 mplayer_put_key(MOUSE_BTN0);
980 break;
982 case WM_MBUTTONDOWN:
984 if (!vo_nomouse_input)
985 mplayer_put_key(MOUSE_BTN1);
986 break;
988 case WM_RBUTTONDOWN:
990 if (!vo_nomouse_input)
991 mplayer_put_key(MOUSE_BTN2);
992 break;
994 case WM_LBUTTONDBLCLK:
996 if(!vo_nomouse_input)
997 mplayer_put_key(MOUSE_BTN0_DBL);
998 break;
1000 case WM_MBUTTONDBLCLK:
1002 if(!vo_nomouse_input)
1003 mplayer_put_key(MOUSE_BTN1_DBL);
1004 break;
1006 case WM_RBUTTONDBLCLK:
1008 if(!vo_nomouse_input)
1009 mplayer_put_key(MOUSE_BTN2_DBL);
1010 break;
1012 case WM_MOUSEWHEEL:
1014 if (vo_nomouse_input)
1015 break;
1016 int x = GET_WHEEL_DELTA_WPARAM(wParam);
1017 if (x > 0)
1018 mplayer_put_key(MOUSE_BTN3);
1019 else
1020 mplayer_put_key(MOUSE_BTN4);
1021 break;
1023 case WM_XBUTTONDOWN:
1025 if (vo_nomouse_input)
1026 break;
1027 if (HIWORD(wParam) == 1)
1028 mplayer_put_key(MOUSE_BTN5);
1029 else
1030 mplayer_put_key(MOUSE_BTN6);
1031 break;
1033 case WM_XBUTTONDBLCLK:
1035 if (vo_nomouse_input)
1036 break;
1037 if (HIWORD(wParam) == 1)
1038 mplayer_put_key(MOUSE_BTN5_DBL);
1039 else
1040 mplayer_put_key(MOUSE_BTN6_DBL);
1041 break;
1045 return DefWindowProc(hWnd, message, wParam, lParam);
1049 static int preinit(const char *arg)
1051 HINSTANCE hInstance = GetModuleHandle(NULL);
1052 char exedir[MAX_PATH];
1053 WNDCLASS wc;
1054 if(arg)
1056 if(strstr(arg,"noaccel"))
1058 mp_msg(MSGT_VO,MSGL_V,"<vo_directx><INFO>disabled overlay\n");
1059 nooverlay = 1;
1062 /*load icon from the main app*/
1063 if(GetModuleFileName(NULL,exedir,MAX_PATH))
1065 mplayericon = ExtractIcon( hInstance, exedir, 0 );
1067 if(!mplayericon)mplayericon=LoadIcon(NULL,IDI_APPLICATION);
1068 mplayercursor = LoadCursor(NULL, IDC_ARROW);
1069 monitor_rect.right=GetSystemMetrics(SM_CXSCREEN);
1070 monitor_rect.bottom=GetSystemMetrics(SM_CYSCREEN);
1072 windowcolor = vo_colorkey;
1073 colorbrush = CreateSolidBrush(windowcolor);
1074 blackbrush = (HBRUSH)GetStockObject(BLACK_BRUSH);
1075 wc.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
1076 wc.lpfnWndProc = WndProc;
1077 wc.cbClsExtra = 0;
1078 wc.cbWndExtra = 0;
1079 wc.hInstance = hInstance;
1080 wc.hCursor = mplayercursor;
1081 wc.hIcon = mplayericon;
1082 wc.hbrBackground = vidmode ? blackbrush : colorbrush;
1083 wc.lpszClassName = WNDCLASSNAME_WINDOWED;
1084 wc.lpszMenuName = NULL;
1085 RegisterClass(&wc);
1086 if (WinID != -1) hWnd = WinID;
1087 else
1088 hWnd = CreateWindowEx(vidmode?WS_EX_TOPMOST:0,
1089 WNDCLASSNAME_WINDOWED,"",(vidmode || !vo_border)?WS_POPUP:WNDSTYLE,
1090 CW_USEDEFAULT, CW_USEDEFAULT, 100, 100,NULL,NULL,hInstance,NULL);
1091 wc.hbrBackground = blackbrush;
1092 wc.lpszClassName = WNDCLASSNAME_FULLSCREEN;
1093 RegisterClass(&wc);
1095 if (Directx_InitDirectDraw()!= 0)return 1; //init DirectDraw
1097 if(!vidmode)hWndFS = CreateWindow(WNDCLASSNAME_FULLSCREEN,"MPlayer Fullscreen",WS_POPUP,monitor_rect.left,monitor_rect.top,monitor_rect.right-monitor_rect.left,monitor_rect.bottom-monitor_rect.top,hWnd,NULL,hInstance,NULL);
1098 mp_msg(MSGT_VO, MSGL_DBG3 ,"<vo_directx><INFO>initial mplayer windows created\n");
1100 if (Directx_CheckPrimaryPixelformat()!=0)return 1;
1101 if (!nooverlay && Directx_CheckOverlayPixelformats() == 0) //check for supported hardware
1103 mp_msg(MSGT_VO, MSGL_V ,"<vo_directx><INFO>hardware supports overlay\n");
1104 nooverlay = 0;
1106 else //if we can't have overlay we create a backpuffer with the same imageformat as the primary surface
1108 mp_msg(MSGT_VO, MSGL_V ,"<vo_directx><INFO>using backpuffer\n");
1109 nooverlay = 1;
1111 mp_msg(MSGT_VO, MSGL_DBG3 ,"<vo_directx><INFO>preinit succesfully finished\n");
1112 return 0;
1115 static int draw_slice(uint8_t *src[], int stride[], int w,int h,int x,int y )
1117 uint8_t *s;
1118 uint8_t *d;
1119 uint32_t i=0, uvstride=dstride/2;
1120 // copy Y
1121 d=image+dstride*y+x;
1122 s=src[0];
1123 for(i=0;i<h;i++){
1124 memcpy(d,s,w);
1125 s+=stride[0];
1126 d+=dstride;
1129 w/=2;h/=2;x/=2;y/=2;
1131 // copy U
1132 d=image+dstride*image_height + uvstride*y+x;
1133 if(image_format == IMGFMT_YV12)s=src[2];
1134 else s=src[1];
1135 for(i=0;i<h;i++){
1136 memcpy(d,s,w);
1137 s+=stride[1];
1138 d+=uvstride;
1141 // copy V
1142 d=image+dstride*image_height +uvstride*(image_height/2) + uvstride*y+x;
1143 if(image_format == IMGFMT_YV12)s=src[1];
1144 else s=src[2];
1145 for(i=0;i<h;i++){
1146 memcpy(d,s,w);
1147 s+=stride[2];
1148 d+=uvstride;
1150 return 0;
1153 static void flip_page(void)
1155 HRESULT dxresult;
1156 g_lpddsBack->lpVtbl->Unlock (g_lpddsBack,NULL);
1157 if (vo_doublebuffering)
1159 // flip to the next image in the sequence
1160 dxresult = g_lpddsOverlay->lpVtbl->Flip( g_lpddsOverlay,NULL, DDFLIP_WAIT);
1161 if(dxresult == DDERR_SURFACELOST)
1163 mp_msg(MSGT_VO, MSGL_ERR,"<vo_directx><ERROR><vo_directx><INFO>Restoring Surface\n");
1164 g_lpddsBack->lpVtbl->Restore( g_lpddsBack );
1165 dxresult = g_lpddsOverlay->lpVtbl->Flip( g_lpddsOverlay,NULL, DDFLIP_WAIT);
1167 if(dxresult != DD_OK)mp_msg(MSGT_VO, MSGL_ERR,"<vo_directx><ERROR>can't flip page\n");
1169 if(nooverlay)
1171 DDBLTFX ddbltfx;
1172 // ask for the "NOTEARING" option
1173 memset( &ddbltfx, 0, sizeof(DDBLTFX) );
1174 ddbltfx.dwSize = sizeof(DDBLTFX);
1175 ddbltfx.dwDDFX = DDBLTFX_NOTEARING;
1176 g_lpddsPrimary->lpVtbl->Blt(g_lpddsPrimary, &rd, g_lpddsBack, NULL, DDBLT_WAIT, &ddbltfx);
1178 g_lpddsBack->lpVtbl->Lock(g_lpddsBack,NULL,&ddsdsf, DDLOCK_NOSYSLOCK | DDLOCK_WAIT , NULL);
1179 if(vo_directrendering && (dstride != ddsdsf.lPitch)){
1180 mp_msg(MSGT_VO,MSGL_WARN,"<vo_directx><WARN>stride changed !!!! disabling direct rendering\n");
1181 vo_directrendering=0;
1183 dstride = ddsdsf.lPitch;
1184 image = ddsdsf.lpSurface;
1187 static int draw_frame(uint8_t *src[])
1189 memcpy( image, *src, dstride * image_height );
1190 return 0;
1193 static uint32_t get_image(mp_image_t *mpi)
1195 if(mpi->flags&MP_IMGFLAG_READABLE) {mp_msg(MSGT_VO, MSGL_V,"<vo_directx><ERROR>slow video ram\n");return VO_FALSE;}
1196 if(mpi->type==MP_IMGTYPE_STATIC) {mp_msg(MSGT_VO, MSGL_V,"<vo_directx><ERROR>not static\n");return VO_FALSE;}
1197 if((mpi->width==dstride) || (mpi->flags&(MP_IMGFLAG_ACCEPT_STRIDE|MP_IMGFLAG_ACCEPT_WIDTH)))
1199 if(mpi->flags&MP_IMGFLAG_PLANAR)
1201 if(image_format == IMGFMT_YV12)
1203 mpi->planes[2]= image + dstride*image_height;
1204 mpi->planes[1]= image + dstride*image_height+ dstride*image_height/4;
1205 mpi->stride[1]=mpi->stride[2]=dstride/2;
1207 else if(image_format == IMGFMT_IYUV || image_format == IMGFMT_I420)
1209 mpi->planes[1]= image + dstride*image_height;
1210 mpi->planes[2]= image + dstride*image_height+ dstride*image_height/4;
1211 mpi->stride[1]=mpi->stride[2]=dstride/2;
1213 else if(image_format == IMGFMT_YVU9)
1215 mpi->planes[2] = image + dstride*image_height;
1216 mpi->planes[1] = image + dstride*image_height+ dstride*image_height/16;
1217 mpi->stride[1]=mpi->stride[2]=dstride/4;
1220 mpi->planes[0]=image;
1221 mpi->stride[0]=dstride;
1222 mpi->width=image_width;
1223 mpi->height=image_height;
1224 mpi->flags|=MP_IMGFLAG_DIRECT;
1225 mp_msg(MSGT_VO, MSGL_DBG3, "<vo_directx><INFO>Direct Rendering ENABLED\n");
1226 return VO_TRUE;
1228 return VO_FALSE;
1231 static uint32_t put_image(mp_image_t *mpi){
1233 uint32_t i = 0;
1234 uint8_t *d;
1235 uint8_t *s;
1236 uint32_t x = mpi->x;
1237 uint32_t y = mpi->y;
1238 uint32_t w = mpi->w;
1239 uint32_t h = mpi->h;
1241 if (WinID != -1) Directx_ManageDisplay();
1243 if((mpi->flags&MP_IMGFLAG_DIRECT)||(mpi->flags&MP_IMGFLAG_DRAW_CALLBACK))
1245 mp_msg(MSGT_VO, MSGL_DBG3 ,"<vo_directx><INFO>put_image: nothing to do: drawslices\n");
1246 return VO_TRUE;
1249 if (mpi->flags&MP_IMGFLAG_PLANAR)
1252 if(image_format!=IMGFMT_YVU9)draw_slice(mpi->planes,mpi->stride,mpi->w,mpi->h,0,0);
1253 else
1255 // copy Y
1256 d=image+dstride*y+x;
1257 s=mpi->planes[0];
1258 for(i=0;i<h;i++){
1259 memcpy(d,s,w);
1260 s+=mpi->stride[0];
1261 d+=dstride;
1263 w/=4;h/=4;x/=4;y/=4;
1264 // copy V
1265 d=image+dstride*image_height + dstride*y/4+x;
1266 s=mpi->planes[2];
1267 for(i=0;i<h;i++){
1268 memcpy(d,s,w);
1269 s+=mpi->stride[1];
1270 d+=dstride/4;
1272 // copy U
1273 d=image+dstride*image_height + dstride*image_height/16 + dstride/4*y+x;
1274 s=mpi->planes[1];
1275 for(i=0;i<h;i++){
1276 memcpy(d,s,w);
1277 s+=mpi->stride[2];
1278 d+=dstride/4;
1282 else //packed
1284 memcpy( image, mpi->planes[0], image_height * dstride);
1286 return VO_TRUE;
1289 static int
1290 config(uint32_t width, uint32_t height, uint32_t d_width, uint32_t d_height, uint32_t options, char *title, uint32_t format)
1292 RECT rd;
1293 vo_screenwidth = monitor_rect.right - monitor_rect.left;
1294 vo_screenheight = monitor_rect.bottom - monitor_rect.top;
1295 vo_fs = options & 0x01;
1296 image_format = format;
1297 image_width = width;
1298 image_height = height;
1299 d_image_width = d_width;
1300 d_image_height = d_height;
1301 if(format != primary_image_format)nooverlay = 0;
1302 aspect_save_orig(image_width,image_height);
1303 aspect_save_prescale(d_image_width,d_image_height);
1304 if(vidmode){
1305 vo_screenwidth=vm_width;
1306 vo_screenheight=vm_height;
1308 aspect_save_screenres(vo_screenwidth,vo_screenheight);
1309 aspect(&d_image_width, &d_image_height, A_NOZOOM);
1310 window_aspect= (float)d_image_width / (float)d_image_height;
1311 vo_dx = 0;
1312 vo_dy = 0;
1314 /*release all directx objects*/
1315 if (g_cc != NULL)g_cc->lpVtbl->Release(g_cc);
1316 g_cc=NULL;
1317 if(g_lpddclipper)g_lpddclipper->lpVtbl->Release(g_lpddclipper);
1318 g_lpddclipper=NULL;
1319 if (g_lpddsBack != NULL) g_lpddsBack->lpVtbl->Release(g_lpddsBack);
1320 g_lpddsBack = NULL;
1321 if(vo_doublebuffering)
1322 if (g_lpddsOverlay != NULL)g_lpddsOverlay->lpVtbl->Release(g_lpddsOverlay);
1323 g_lpddsOverlay = NULL;
1324 if (g_lpddsPrimary != NULL) g_lpddsPrimary->lpVtbl->Release(g_lpddsPrimary);
1325 g_lpddsPrimary = NULL;
1326 mp_msg(MSGT_VO, MSGL_DBG3,"<vo_directx><INFO>overlay surfaces released\n");
1328 if(!vidmode){
1329 if(vo_geometry){
1330 vo_dx= ( vo_screenwidth - d_image_width ) / 2; vo_dy=( vo_screenheight - d_image_height ) / 2;
1331 geometry(&vo_dx, &vo_dy, &d_image_width, &d_image_height, vo_screenwidth, vo_screenheight);
1333 else {
1334 GetWindowRect(hWnd,&rd);
1335 vo_dx=rd.left;
1336 vo_dy=rd.top;
1338 vo_dx += monitor_rect.left; /* move position to global window space */
1339 vo_dy += monitor_rect.top;
1340 rd.left = vo_dx;
1341 rd.top = vo_dy;
1342 rd.right = rd.left + d_image_width;
1343 rd.bottom = rd.top + d_image_height;
1344 if (WinID == -1) {
1345 if (vo_border)
1346 AdjustWindowRect(&rd,WNDSTYLE,FALSE);
1347 SetWindowPos(hWnd,NULL, vo_dx, vo_dy,rd.right-rd.left,rd.bottom-rd.top,SWP_SHOWWINDOW|SWP_NOOWNERZORDER);
1350 else ShowWindow(hWnd,SW_SHOW);
1352 if(vo_fs && !vidmode)ShowWindow(hWndFS,SW_SHOW);
1353 if (WinID == -1)
1354 SetWindowText(hWnd,title);
1357 if(vidmode)vo_fs=0;
1360 /*create the surfaces*/
1361 if(Directx_CreatePrimarySurface())return 1;
1363 //create palette for 256 color mode
1364 if(image_format==IMGFMT_BGR8){
1365 LPDIRECTDRAWPALETTE ddpalette=NULL;
1366 char* palette=malloc(4*256);
1367 int i;
1368 for(i=0; i<256; i++){
1369 palette[4*i+0] = ((i >> 5) & 0x07) * 255 / 7;
1370 palette[4*i+1] = ((i >> 2) & 0x07) * 255 / 7;
1371 palette[4*i+2] = ((i >> 0) & 0x03) * 255 / 3;
1372 palette[4*i+3] = PC_NOCOLLAPSE;
1374 g_lpdd->lpVtbl->CreatePalette(g_lpdd,DDPCAPS_8BIT|DDPCAPS_INITIALIZE,palette,&ddpalette,NULL);
1375 g_lpddsPrimary->lpVtbl->SetPalette(g_lpddsPrimary,ddpalette);
1376 free(palette);
1377 ddpalette->lpVtbl->Release(ddpalette);
1380 if (!nooverlay && Directx_CreateOverlay(image_format))
1382 if(format == primary_image_format)nooverlay=1; /*overlay creation failed*/
1383 else {
1384 mp_msg(MSGT_VO, MSGL_FATAL,"<vo_directx><FATAL ERROR>can't use overlay mode: please use -vo directx:noaccel\n");
1385 return 1;
1388 if(nooverlay)
1390 if(Directx_CreateBackpuffer())
1392 mp_msg(MSGT_VO, MSGL_FATAL,"<vo_directx><FATAL ERROR>can't get the driver to work on your system :(\n");
1393 return 1;
1395 mp_msg(MSGT_VO, MSGL_V,"<vo_directx><INFO>back surface created\n");
1396 vo_doublebuffering = 0;
1397 /*create clipper for nonoverlay mode*/
1398 if(g_lpdd->lpVtbl->CreateClipper(g_lpdd, 0, &g_lpddclipper,NULL)!= DD_OK){mp_msg(MSGT_VO, MSGL_FATAL,"<vo_directx><FATAL ERROR>can't create clipper\n");return 1;}
1399 if(g_lpddclipper->lpVtbl->SetHWnd (g_lpddclipper, 0, hWnd)!= DD_OK){mp_msg(MSGT_VO, MSGL_FATAL,"<vo_directx><FATAL ERROR>can't associate clipper with window\n");return 1;}
1400 if(g_lpddsPrimary->lpVtbl->SetClipper (g_lpddsPrimary,g_lpddclipper)!=DD_OK){mp_msg(MSGT_VO, MSGL_FATAL,"<vo_directx><FATAL ERROR>can't associate primary surface with clipper\n");return 1;}
1401 mp_msg(MSGT_VO, MSGL_DBG3,"<vo_directx><INFO>clipper succesfully created\n");
1402 }else{
1403 if(DD_OK != g_lpddsOverlay->lpVtbl->QueryInterface(g_lpddsOverlay,&IID_IDirectDrawColorControl,(void**)&g_cc))
1404 mp_msg(MSGT_VO, MSGL_V,"<vo_directx><WARN>unable to get DirectDraw ColorControl interface\n");
1406 Directx_ManageDisplay();
1407 memset(&ddsdsf, 0,sizeof(DDSURFACEDESC2));
1408 ddsdsf.dwSize = sizeof (DDSURFACEDESC2);
1409 g_lpddsBack->lpVtbl->Lock(g_lpddsBack,NULL,&ddsdsf, DDLOCK_NOSYSLOCK | DDLOCK_WAIT, NULL);
1410 dstride = ddsdsf.lPitch;
1411 image = ddsdsf.lpSurface;
1412 return 0;
1415 //function to set color controls
1416 // brightness [0, 10000]
1417 // contrast [0, 20000]
1418 // hue [-180, 180]
1419 // saturation [0, 20000]
1420 static uint32_t color_ctrl_set(char *what, int value)
1422 uint32_t r = VO_NOTIMPL;
1423 DDCOLORCONTROL dcc;
1424 //printf("\n*** %s = %d\n", what, value);
1425 if (!g_cc) {
1426 //printf("\n *** could not get color control interface!!!\n");
1427 return VO_NOTIMPL;
1429 ZeroMemory(&dcc, sizeof(dcc));
1430 dcc.dwSize = sizeof(dcc);
1432 if (!strcmp(what, "brightness")) {
1433 dcc.dwFlags = DDCOLOR_BRIGHTNESS;
1434 dcc.lBrightness = (value + 100) * 10000 / 200;
1435 r = VO_TRUE;
1436 } else if (!strcmp(what, "contrast")) {
1437 dcc.dwFlags = DDCOLOR_CONTRAST;
1438 dcc.lContrast = (value + 100) * 20000 / 200;
1439 r = VO_TRUE;
1440 } else if (!strcmp(what, "hue")) {
1441 dcc.dwFlags = DDCOLOR_HUE;
1442 dcc.lHue = value * 180 / 100;
1443 r = VO_TRUE;
1444 } else if (!strcmp(what, "saturation")) {
1445 dcc.dwFlags = DDCOLOR_SATURATION;
1446 dcc.lSaturation = (value + 100) * 20000 / 200;
1447 r = VO_TRUE;
1450 if (r == VO_TRUE) {
1451 g_cc->lpVtbl->SetColorControls(g_cc, &dcc);
1453 return r;
1456 //analoguous to color_ctrl_set
1457 static uint32_t color_ctrl_get(char *what, int *value)
1459 uint32_t r = VO_NOTIMPL;
1460 DDCOLORCONTROL dcc;
1461 if (!g_cc) {
1462 //printf("\n *** could not get color control interface!!!\n");
1463 return VO_NOTIMPL;
1465 ZeroMemory(&dcc, sizeof(dcc));
1466 dcc.dwSize = sizeof(dcc);
1468 if (g_cc->lpVtbl->GetColorControls(g_cc, &dcc) != DD_OK) {
1469 return r;
1472 if (!strcmp(what, "brightness") && (dcc.dwFlags & DDCOLOR_BRIGHTNESS)) {
1473 *value = dcc.lBrightness * 200 / 10000 - 100;
1474 r = VO_TRUE;
1475 } else if (!strcmp(what, "contrast") && (dcc.dwFlags & DDCOLOR_CONTRAST)) {
1476 *value = dcc.lContrast * 200 / 20000 - 100;
1477 r = VO_TRUE;
1478 } else if (!strcmp(what, "hue") && (dcc.dwFlags & DDCOLOR_HUE)) {
1479 *value = dcc.lHue * 100 / 180;
1480 r = VO_TRUE;
1481 } else if (!strcmp(what, "saturation") && (dcc.dwFlags & DDCOLOR_SATURATION)) {
1482 *value = dcc.lSaturation * 200 / 20000 - 100;
1483 r = VO_TRUE;
1485 // printf("\n*** %s = %d\n", what, *value);
1487 return r;
1490 static int control(uint32_t request, void *data, ...)
1492 switch (request) {
1494 case VOCTRL_GET_IMAGE:
1495 return get_image(data);
1496 case VOCTRL_QUERY_FORMAT:
1497 last_rect.left = 0xDEADC0DE; // reset window position cache
1498 return query_format(*((uint32_t*)data));
1499 case VOCTRL_DRAW_IMAGE:
1500 return put_image(data);
1501 case VOCTRL_BORDER:
1502 if(WinID != -1) return VO_TRUE;
1503 if(vidmode)
1505 mp_msg(MSGT_VO, MSGL_ERR,"<vo_directx><ERROR>border has no meaning in exclusive mode\n");
1507 else
1509 if(vo_border) {
1510 vo_border = 0;
1511 SetWindowLong(hWnd, GWL_STYLE, WS_POPUP);
1512 } else {
1513 vo_border = 1;
1514 SetWindowLong(hWnd, GWL_STYLE, WNDSTYLE);
1516 // needed AFAICT to force the window to
1517 // redisplay with the new style. --Joey
1518 if (!vo_fs) {
1519 ShowWindow(hWnd,SW_HIDE);
1520 ShowWindow(hWnd,SW_SHOW);
1522 last_rect.left = 0xDEADC0DE; // reset window position cache
1523 Directx_ManageDisplay();
1525 return VO_TRUE;
1526 case VOCTRL_ONTOP:
1527 if(WinID != -1) return VO_TRUE;
1528 if(vidmode)
1530 mp_msg(MSGT_VO, MSGL_ERR,"<vo_directx><ERROR>ontop has no meaning in exclusive mode\n");
1532 else
1534 if(vo_ontop) vo_ontop = 0;
1535 else vo_ontop = 1;
1536 last_rect.left = 0xDEADC0DE; // reset window position cache
1537 Directx_ManageDisplay();
1539 return VO_TRUE;
1540 case VOCTRL_ROOTWIN:
1541 if(WinID != -1) return VO_TRUE;
1542 if(vidmode)
1544 mp_msg(MSGT_VO, MSGL_ERR,"<vo_directx><ERROR>rootwin has no meaning in exclusive mode\n");
1546 else
1548 if(vo_rootwin) vo_rootwin = 0;
1549 else vo_rootwin = 1;
1550 last_rect.left = 0xDEADC0DE; // reset window position cache
1551 Directx_ManageDisplay();
1553 return VO_TRUE;
1554 case VOCTRL_FULLSCREEN:
1556 if(vidmode)
1558 mp_msg(MSGT_VO, MSGL_ERR,"<vo_directx><ERROR>currently we do not allow to switch from exclusive to windowed mode\n");
1560 else
1562 if(!vo_fs)
1564 vo_fs=1;
1565 ShowWindow(hWndFS,SW_SHOW);
1566 ShowWindow(hWnd,SW_HIDE);
1567 SetForegroundWindow(hWndFS);
1569 else
1571 vo_fs=0;
1572 ShowWindow(hWndFS,SW_HIDE);
1573 ShowWindow(hWnd,SW_SHOW);
1575 last_rect.left = 0xDEADC0DE; // reset window position cache
1576 Directx_ManageDisplay();
1577 break;
1579 return VO_TRUE;
1581 case VOCTRL_SET_EQUALIZER: {
1582 va_list ap;
1583 int value;
1585 va_start(ap, data);
1586 value = va_arg(ap, int);
1587 va_end(ap);
1588 return color_ctrl_set(data, value);
1590 case VOCTRL_GET_EQUALIZER: {
1591 va_list ap;
1592 int *value;
1594 va_start(ap, data);
1595 value = va_arg(ap, int*);
1596 va_end(ap);
1597 return color_ctrl_get(data, value);
1599 case VOCTRL_RESET:
1600 last_rect.left = 0xDEADC0DE; // reset window position cache
1601 // fall-through intended
1603 return VO_NOTIMPL;