manpage: Fix wrong option name in input example
[mplayer/glamo.git] / libvo / vo_directx.c
blob00294b881d98657578fcb7c488213daf42fa1bcb
1 /*
2 * Directx v2 or later DirectDraw interface
4 * Copyright (c) 2002 - 2005 Sascha Sommer <saschasommer@freenet.de>
6 * This file is part of MPlayer.
8 * MPlayer is free software; you can redistribute it and/or modify
9 * it under the terms of the GNU General Public License as published by
10 * the Free Software Foundation; either version 2 of the License, or
11 * (at your option) any later version.
13 * MPlayer is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
16 * GNU General Public License for more details.
18 * You should have received a copy of the GNU General Public License along
19 * with MPlayer; if not, write to the Free Software Foundation, Inc.,
20 * 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
23 #include <windows.h>
24 #include <windowsx.h>
25 #include <ddraw.h>
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <errno.h>
29 #include "config.h"
30 #include "video_out.h"
31 #include "video_out_internal.h"
32 #include "fastmemcpy.h"
33 #include "input/input.h"
34 #include "osdep/keycodes.h"
35 #include "input/mouse.h"
36 #include "mp_msg.h"
37 #include "aspect.h"
38 #include "geometry.h"
39 #include "mp_fifo.h"
40 #include "sub.h"
42 #ifndef WM_XBUTTONDOWN
43 # define WM_XBUTTONDOWN 0x020B
44 # define WM_XBUTTONUP 0x020C
45 # define WM_XBUTTONDBLCLK 0x020D
46 #endif
48 #define WNDCLASSNAME_WINDOWED "MPlayer - The Movie Player"
49 #define WNDCLASSNAME_FULLSCREEN "MPlayer - Fullscreen"
50 #define WNDSTYLE WS_OVERLAPPEDWINDOW|WS_SIZEBOX
52 static LPDIRECTDRAWCOLORCONTROL g_cc = NULL; //color control interface
53 static LPDIRECTDRAW7 g_lpdd = NULL; //DirectDraw Object
54 static LPDIRECTDRAWSURFACE7 g_lpddsPrimary = NULL; //Primary Surface: viewport through the Desktop
55 static LPDIRECTDRAWSURFACE7 g_lpddsOverlay = NULL; //Overlay Surface
56 static LPDIRECTDRAWSURFACE7 g_lpddsBack = NULL; //Back surface
57 static LPDIRECTDRAWCLIPPER g_lpddclipper; //clipper object, can only be used without overlay
58 static DDSURFACEDESC2 ddsdsf; //surface descripiton needed for locking
59 static HINSTANCE hddraw_dll; //handle to ddraw.dll
60 static RECT rd; //rect of our stretched image
61 static RECT rs; //rect of our source image
62 static HWND hWnd=NULL; //handle to the window
63 static HWND hWndFS=NULL; //fullscreen window
64 static HBRUSH colorbrush = NULL; // Handle to colorkey brush
65 static HBRUSH blackbrush = NULL; // Handle to black brush
66 static HICON mplayericon = NULL; // Handle to mplayer icon
67 static HCURSOR mplayercursor = NULL; // Handle to mplayer cursor
68 static uint32_t image_width, image_height; //image width and height
69 static uint32_t d_image_width, d_image_height; //image width and height zoomed
70 static uint8_t *image=NULL; //image data
71 static void* tmp_image = NULL;
72 static uint32_t image_format=0; //image format
73 static uint32_t primary_image_format;
74 static uint32_t vm_height=0;
75 static uint32_t vm_width=0;
76 static uint32_t vm_bpp=0;
77 static uint32_t dstride; //surface stride
78 static uint32_t nooverlay = 0; //NonOverlay mode
79 static DWORD destcolorkey; //colorkey for our surface
80 static COLORREF windowcolor = RGB(0,0,16); //windowcolor == colorkey
81 static int adapter_count=0;
82 static GUID selected_guid;
83 static GUID *selected_guid_ptr = NULL;
84 static RECT monitor_rect; //monitor coordinates
85 static float window_aspect;
86 static BOOL (WINAPI* myGetMonitorInfo)(HMONITOR, LPMONITORINFO) = NULL;
87 static RECT last_rect = {0xDEADC0DE, 0xDEADC0DE, 0xDEADC0DE, 0xDEADC0DE};
89 /*****************************************************************************
90 * DirectDraw GUIDs.
91 * Defining them here allows us to get rid of the dxguid library during
92 * the linking stage.
93 *****************************************************************************/
94 #define IID_IDirectDraw7 MP_IID_IDirectDraw7
95 static const GUID MP_IID_IDirectDraw7 =
97 0x15e65ec0,0x3b9c,0x11d2,{0xb9,0x2f,0x00,0x60,0x97,0x97,0xea,0x5b}
100 #define IID_IDirectDrawColorControl MP_IID_IDirectDrawColorControl
101 static const GUID MP_IID_IDirectDrawColorControl =
103 0x4b9f0ee0,0x0d7e,0x11d0,{0x9b,0x06,0x00,0xa0,0xc9,0x03,0xa3,0xb8}
107 typedef struct directx_fourcc_caps
109 char* img_format_name; //human readable name
110 uint32_t img_format; //as MPlayer image format
111 uint32_t drv_caps; //what hw supports with this format
112 DDPIXELFORMAT g_ddpfOverlay; //as Directx Sourface description
113 } directx_fourcc_caps;
116 static directx_fourcc_caps g_ddpf[] =
118 {"YV12 ",IMGFMT_YV12 ,0,{sizeof(DDPIXELFORMAT), DDPF_FOURCC,MAKEFOURCC('Y','V','1','2'),0,0,0,0,0}},
119 {"I420 ",IMGFMT_I420 ,0,{sizeof(DDPIXELFORMAT), DDPF_FOURCC,MAKEFOURCC('I','4','2','0'),0,0,0,0,0}}, //yv12 with swapped uv
120 {"IYUV ",IMGFMT_IYUV ,0,{sizeof(DDPIXELFORMAT), DDPF_FOURCC,MAKEFOURCC('I','Y','U','V'),0,0,0,0,0}}, //same as i420
121 {"YVU9 ",IMGFMT_YVU9 ,0,{sizeof(DDPIXELFORMAT), DDPF_FOURCC,MAKEFOURCC('Y','V','U','9'),0,0,0,0,0}},
122 {"YUY2 ",IMGFMT_YUY2 ,0,{sizeof(DDPIXELFORMAT), DDPF_FOURCC,MAKEFOURCC('Y','U','Y','2'),0,0,0,0,0}},
123 {"UYVY ",IMGFMT_UYVY ,0,{sizeof(DDPIXELFORMAT), DDPF_FOURCC,MAKEFOURCC('U','Y','V','Y'),0,0,0,0,0}},
124 {"BGR8 ",IMGFMT_BGR8 ,0,{sizeof(DDPIXELFORMAT), DDPF_RGB, 0, 8, 0x00000000, 0x00000000, 0x00000000, 0}},
125 {"RGB15",IMGFMT_RGB15,0,{sizeof(DDPIXELFORMAT), DDPF_RGB, 0, 16, 0x0000001F, 0x000003E0, 0x00007C00, 0}}, //RGB 5:5:5
126 {"BGR15",IMGFMT_BGR15,0,{sizeof(DDPIXELFORMAT), DDPF_RGB, 0, 16, 0x00007C00, 0x000003E0, 0x0000001F, 0}},
127 {"RGB16",IMGFMT_RGB16,0,{sizeof(DDPIXELFORMAT), DDPF_RGB, 0, 16, 0x0000001F, 0x000007E0, 0x0000F800, 0}}, //RGB 5:6:5
128 {"BGR16",IMGFMT_BGR16,0,{sizeof(DDPIXELFORMAT), DDPF_RGB, 0, 16, 0x0000F800, 0x000007E0, 0x0000001F, 0}},
129 {"RGB24",IMGFMT_RGB24,0,{sizeof(DDPIXELFORMAT), DDPF_RGB, 0, 24, 0x000000FF, 0x0000FF00, 0x00FF0000, 0}},
130 {"BGR24",IMGFMT_BGR24,0,{sizeof(DDPIXELFORMAT), DDPF_RGB, 0, 24, 0x00FF0000, 0x0000FF00, 0x000000FF, 0}},
131 {"RGB32",IMGFMT_RGB32,0,{sizeof(DDPIXELFORMAT), DDPF_RGB, 0, 32, 0x000000FF, 0x0000FF00, 0x00FF0000, 0}},
132 {"BGR32",IMGFMT_BGR32,0,{sizeof(DDPIXELFORMAT), DDPF_RGB, 0, 32, 0x00FF0000, 0x0000FF00, 0x000000FF, 0}}
134 #define NUM_FORMATS (sizeof(g_ddpf) / sizeof(g_ddpf[0]))
136 static const vo_info_t info =
138 "Directx DDraw YUV/RGB/BGR renderer",
139 "directx",
140 "Sascha Sommer <saschasommer@freenet.de>",
144 const LIBVO_EXTERN(directx)
146 static void draw_alpha(int x0, int y0, int w, int h, unsigned char *src,
147 unsigned char *srca, int stride)
149 switch(image_format) {
150 case IMGFMT_YV12 :
151 case IMGFMT_I420 :
152 case IMGFMT_IYUV :
153 case IMGFMT_YVU9 :
154 vo_draw_alpha_yv12(w,h,src,srca,stride,((uint8_t *) image) + dstride*y0 + x0,dstride);
155 break;
156 case IMGFMT_YUY2 :
157 vo_draw_alpha_yuy2(w,h,src,srca,stride,((uint8_t *) image)+ dstride*y0 + 2*x0 ,dstride);
158 break;
159 case IMGFMT_UYVY :
160 vo_draw_alpha_yuy2(w,h,src,srca,stride,((uint8_t *) image) + dstride*y0 + 2*x0 + 1,dstride);
161 break;
162 case IMGFMT_RGB15:
163 case IMGFMT_BGR15:
164 vo_draw_alpha_rgb15(w,h,src,srca,stride,((uint8_t *) image)+dstride*y0+2*x0,dstride);
165 break;
166 case IMGFMT_RGB16:
167 case IMGFMT_BGR16:
168 vo_draw_alpha_rgb16(w,h,src,srca,stride,((uint8_t *) image)+dstride*y0+2*x0,dstride);
169 break;
170 case IMGFMT_RGB24:
171 case IMGFMT_BGR24:
172 vo_draw_alpha_rgb24(w,h,src,srca,stride,((uint8_t *) image)+dstride*y0+4*x0,dstride);
173 break;
174 case IMGFMT_RGB32:
175 case IMGFMT_BGR32:
176 vo_draw_alpha_rgb32(w,h,src,srca,stride,((uint8_t *) image)+dstride*y0+4*x0,dstride);
177 break;
181 static void draw_osd(void)
183 vo_draw_text(image_width,image_height,draw_alpha);
186 static int
187 query_format(uint32_t format)
189 uint32_t i=0;
190 while ( i < NUM_FORMATS )
192 if (g_ddpf[i].img_format == format)
193 return g_ddpf[i].drv_caps;
194 i++;
196 return 0;
199 static uint32_t Directx_CreatePrimarySurface(void)
201 DDSURFACEDESC2 ddsd;
202 //cleanup
203 if(g_lpddsPrimary)g_lpddsPrimary->lpVtbl->Release(g_lpddsPrimary);
204 g_lpddsPrimary=NULL;
206 if(vidmode)g_lpdd->lpVtbl->SetDisplayMode(g_lpdd,vm_width,vm_height,vm_bpp,vo_refresh_rate,0);
207 ZeroMemory(&ddsd, sizeof(ddsd));
208 ddsd.dwSize = sizeof(ddsd);
209 //set flags and create a primary surface.
210 ddsd.dwFlags = DDSD_CAPS;
211 ddsd.ddsCaps.dwCaps = DDSCAPS_PRIMARYSURFACE;
212 if(g_lpdd->lpVtbl->CreateSurface(g_lpdd,&ddsd, &g_lpddsPrimary, NULL )== DD_OK)
213 mp_msg(MSGT_VO, MSGL_DBG3,"<vo_directx><INFO>primary surface created\n");
214 else
216 mp_msg(MSGT_VO, MSGL_FATAL,"<vo_directx><FATAL ERROR>could not create primary surface\n");
217 return 1;
219 return 0;
222 static uint32_t Directx_CreateOverlay(uint32_t imgfmt)
224 HRESULT ddrval;
225 DDSURFACEDESC2 ddsdOverlay;
226 uint32_t i=0;
227 while ( i < NUM_FORMATS +1 && imgfmt != g_ddpf[i].img_format)
229 i++;
231 if (!g_lpdd || !g_lpddsPrimary)
232 return 1;
233 //cleanup
234 if (g_lpddsOverlay)g_lpddsOverlay->lpVtbl->Release(g_lpddsOverlay);
235 if (g_lpddsBack)g_lpddsBack->lpVtbl->Release(g_lpddsBack);
236 g_lpddsOverlay= NULL;
237 g_lpddsBack = NULL;
238 //create our overlay
239 ZeroMemory(&ddsdOverlay, sizeof(ddsdOverlay));
240 ddsdOverlay.dwSize = sizeof(ddsdOverlay);
241 ddsdOverlay.ddsCaps.dwCaps=DDSCAPS_OVERLAY | DDSCAPS_FLIP | DDSCAPS_COMPLEX | DDSCAPS_VIDEOMEMORY;
242 ddsdOverlay.dwFlags= DDSD_CAPS|DDSD_HEIGHT|DDSD_WIDTH|DDSD_BACKBUFFERCOUNT| DDSD_PIXELFORMAT;
243 ddsdOverlay.dwWidth=image_width;
244 ddsdOverlay.dwHeight=image_height;
245 ddsdOverlay.dwBackBufferCount=2;
246 ddsdOverlay.ddpfPixelFormat=g_ddpf[i].g_ddpfOverlay;
247 if(vo_doublebuffering) //tribblebuffering
249 if (g_lpdd->lpVtbl->CreateSurface(g_lpdd,&ddsdOverlay, &g_lpddsOverlay, NULL)== DD_OK)
251 mp_msg(MSGT_VO, MSGL_V,"<vo_directx><INFO>overlay with format %s created\n",g_ddpf[i].img_format_name);
252 //get the surface directly attached to the primary (the back buffer)
253 ddsdOverlay.ddsCaps.dwCaps = DDSCAPS_BACKBUFFER;
254 if(g_lpddsOverlay->lpVtbl->GetAttachedSurface(g_lpddsOverlay,&ddsdOverlay.ddsCaps, &g_lpddsBack) != DD_OK)
256 mp_msg(MSGT_VO, MSGL_FATAL,"<vo_directx><FATAL ERROR>can't get attached surface\n");
257 return 1;
259 return 0;
261 vo_doublebuffering=0; //disable tribblebuffering
262 mp_msg(MSGT_VO, MSGL_V,"<vo_directx><WARN>cannot create tribblebuffer overlay with format %s\n",g_ddpf[i].img_format_name);
264 //single buffer
265 mp_msg(MSGT_VO, MSGL_V,"<vo_directx><INFO>using singlebuffer overlay\n");
266 ddsdOverlay.dwBackBufferCount=0;
267 ddsdOverlay.ddsCaps.dwCaps=DDSCAPS_OVERLAY | DDSCAPS_VIDEOMEMORY;
268 ddsdOverlay.dwFlags= DDSD_CAPS|DDSD_HEIGHT|DDSD_WIDTH|DDSD_PIXELFORMAT;
269 ddsdOverlay.ddpfPixelFormat=g_ddpf[i].g_ddpfOverlay;
270 // try to create the overlay surface
271 ddrval = g_lpdd->lpVtbl->CreateSurface(g_lpdd,&ddsdOverlay, &g_lpddsOverlay, NULL);
272 if(ddrval != DD_OK)
274 if(ddrval == DDERR_INVALIDPIXELFORMAT)mp_msg(MSGT_VO,MSGL_V,"<vo_directx><ERROR> invalid pixelformat: %s\n",g_ddpf[i].img_format_name);
275 else mp_msg(MSGT_VO, MSGL_ERR,"<vo_directx><ERROR>");
276 switch(ddrval)
278 case DDERR_INCOMPATIBLEPRIMARY:
279 {mp_msg(MSGT_VO, MSGL_ERR,"incompatible primary surface\n");break;}
280 case DDERR_INVALIDCAPS:
281 {mp_msg(MSGT_VO, MSGL_ERR,"invalid caps\n");break;}
282 case DDERR_INVALIDOBJECT:
283 {mp_msg(MSGT_VO, MSGL_ERR,"invalid object\n");break;}
284 case DDERR_INVALIDPARAMS:
285 {mp_msg(MSGT_VO, MSGL_ERR,"invalid parameters\n");break;}
286 case DDERR_NODIRECTDRAWHW:
287 {mp_msg(MSGT_VO, MSGL_ERR,"no directdraw hardware\n");break;}
288 case DDERR_NOEMULATION:
289 {mp_msg(MSGT_VO, MSGL_ERR,"can't emulate\n");break;}
290 case DDERR_NOFLIPHW:
291 {mp_msg(MSGT_VO, MSGL_ERR,"hardware can't do flip\n");break;}
292 case DDERR_NOOVERLAYHW:
293 {mp_msg(MSGT_VO, MSGL_ERR,"hardware can't do overlay\n");break;}
294 case DDERR_OUTOFMEMORY:
295 {mp_msg(MSGT_VO, MSGL_ERR,"not enough system memory\n");break;}
296 case DDERR_UNSUPPORTEDMODE:
297 {mp_msg(MSGT_VO, MSGL_ERR,"unsupported mode\n");break;}
298 case DDERR_OUTOFVIDEOMEMORY:
299 {mp_msg(MSGT_VO, MSGL_ERR,"not enough video memory\n");break;}
300 default:
301 mp_msg(MSGT_VO, MSGL_ERR,"create surface failed with 0x%x\n",ddrval);
303 return 1;
305 g_lpddsBack = g_lpddsOverlay;
306 return 0;
309 static uint32_t Directx_CreateBackpuffer(void)
311 DDSURFACEDESC2 ddsd;
312 //cleanup
313 if (g_lpddsBack)g_lpddsBack->lpVtbl->Release(g_lpddsBack);
314 g_lpddsBack=NULL;
315 ZeroMemory(&ddsd, sizeof(ddsd));
316 ddsd.dwSize = sizeof(ddsd);
317 ddsd.ddsCaps.dwCaps= DDSCAPS_OFFSCREENPLAIN | DDSCAPS_SYSTEMMEMORY;
318 ddsd.dwFlags= DDSD_CAPS | DDSD_WIDTH | DDSD_HEIGHT;
319 ddsd.dwWidth=image_width;
320 ddsd.dwHeight=image_height;
321 if(g_lpdd->lpVtbl->CreateSurface( g_lpdd, &ddsd, &g_lpddsBack, 0 ) != DD_OK )
323 mp_msg(MSGT_VO, MSGL_FATAL,"<vo_directx><FATAL ERROR>can't create backpuffer\n");
324 return 1;
326 mp_msg(MSGT_VO, MSGL_DBG3,"<vo_directx><INFO>backbuffer created\n");
327 return 0;
330 static void uninit(void)
332 if (g_cc != NULL)
334 g_cc->lpVtbl->Release(g_cc);
336 g_cc=NULL;
337 if (g_lpddclipper != NULL) g_lpddclipper->lpVtbl->Release(g_lpddclipper);
338 g_lpddclipper = NULL;
339 mp_msg(MSGT_VO, MSGL_DBG3,"<vo_directx><INFO>clipper released\n");
340 if (g_lpddsBack != NULL) g_lpddsBack->lpVtbl->Release(g_lpddsBack);
341 g_lpddsBack = NULL;
342 mp_msg(MSGT_VO, MSGL_DBG3,"<vo_directx><INFO>back surface released\n");
343 if(vo_doublebuffering && !nooverlay)
345 if (g_lpddsOverlay != NULL)g_lpddsOverlay->lpVtbl->Release(g_lpddsOverlay);
346 g_lpddsOverlay = NULL;
347 mp_msg(MSGT_VO, MSGL_DBG3,"<vo_directx><INFO>overlay surface released\n");
349 if (g_lpddsPrimary != NULL) g_lpddsPrimary->lpVtbl->Release(g_lpddsPrimary);
350 g_lpddsPrimary = NULL;
351 mp_msg(MSGT_VO, MSGL_DBG3,"<vo_directx><INFO>primary released\n");
352 if(hWndFS)DestroyWindow(hWndFS);
353 hWndFS = NULL;
354 if((WinID == -1) && hWnd) DestroyWindow(hWnd);
355 hWnd = NULL;
356 mp_msg(MSGT_VO, MSGL_DBG3,"<vo_directx><INFO>window destroyed\n");
357 UnregisterClass(WNDCLASSNAME_WINDOWED, GetModuleHandle(NULL));
358 UnregisterClass(WNDCLASSNAME_FULLSCREEN, GetModuleHandle(NULL));
359 if (mplayericon) DestroyIcon(mplayericon);
360 mplayericon = NULL;
361 if (mplayercursor) DestroyCursor(mplayercursor);
362 mplayercursor = NULL;
363 if (blackbrush) DeleteObject(blackbrush);
364 blackbrush = NULL;
365 if (colorbrush) DeleteObject(colorbrush);
366 colorbrush = NULL;
367 mp_msg(MSGT_VO, MSGL_DBG3,"<vo_directx><INFO>GDI resources deleted\n");
368 if (g_lpdd != NULL){
369 if(vidmode)g_lpdd->lpVtbl->RestoreDisplayMode(g_lpdd);
370 g_lpdd->lpVtbl->Release(g_lpdd);
372 mp_msg(MSGT_VO, MSGL_DBG3,"<vo_directx><INFO>directdrawobject released\n");
373 FreeLibrary( hddraw_dll);
374 hddraw_dll= NULL;
375 mp_msg(MSGT_VO, MSGL_DBG3,"<vo_directx><INFO>ddraw.dll freed\n");
376 mp_msg(MSGT_VO, MSGL_DBG3,"<vo_directx><INFO>uninitialized\n");
379 static BOOL WINAPI EnumCallbackEx(GUID FAR *lpGUID, LPSTR lpDriverDescription, LPSTR lpDriverName, LPVOID lpContext, HMONITOR hm)
381 mp_msg(MSGT_VO, MSGL_INFO ,"<vo_directx> adapter %d: ", adapter_count);
383 if (!lpGUID)
385 mp_msg(MSGT_VO, MSGL_INFO ,"%s", "Primary Display Adapter");
387 else
389 mp_msg(MSGT_VO, MSGL_INFO ,"%s", lpDriverDescription);
392 if(adapter_count == vo_adapter_num){
393 MONITORINFO mi;
394 if (!lpGUID)
395 selected_guid_ptr = NULL;
396 else
398 selected_guid = *lpGUID;
399 selected_guid_ptr = &selected_guid;
401 mi.cbSize = sizeof(mi);
403 if (myGetMonitorInfo(hm, &mi)) {
404 monitor_rect = mi.rcMonitor;
406 mp_msg(MSGT_VO, MSGL_INFO ,"\t\t<--");
408 mp_msg(MSGT_VO, MSGL_INFO ,"\n");
410 adapter_count++;
412 return 1; // list all adapters
415 static uint32_t Directx_InitDirectDraw(void)
417 HRESULT (WINAPI *OurDirectDrawCreateEx)(GUID *,LPVOID *, REFIID,IUnknown FAR *);
418 DDSURFACEDESC2 ddsd;
419 LPDIRECTDRAWENUMERATEEX OurDirectDrawEnumerateEx;
420 HINSTANCE user32dll=LoadLibrary("user32.dll");
422 adapter_count = 0;
423 if(user32dll){
424 myGetMonitorInfo=GetProcAddress(user32dll,"GetMonitorInfoA");
425 if(!myGetMonitorInfo && vo_adapter_num){
426 mp_msg(MSGT_VO, MSGL_ERR, "<vo_directx> -adapter is not supported on Win95\n");
427 vo_adapter_num = 0;
431 mp_msg(MSGT_VO, MSGL_DBG3,"<vo_directx><INFO>Initing DirectDraw\n" );
433 //load direct draw DLL: based on videolans code
434 hddraw_dll = LoadLibrary("DDRAW.DLL");
435 if( hddraw_dll == NULL )
437 mp_msg(MSGT_VO, MSGL_FATAL,"<vo_directx><FATAL ERROR>failed loading ddraw.dll\n" );
438 return 1;
441 last_rect.left = 0xDEADC0DE; // reset window position cache
443 if(vo_adapter_num){ //display other than default
444 OurDirectDrawEnumerateEx = (LPDIRECTDRAWENUMERATEEX) GetProcAddress(hddraw_dll,"DirectDrawEnumerateExA");
445 if (!OurDirectDrawEnumerateEx){
446 FreeLibrary( hddraw_dll );
447 hddraw_dll = NULL;
448 mp_msg(MSGT_VO, MSGL_FATAL,"<vo_directx><FATAL ERROR>failed geting proc address: DirectDrawEnumerateEx\n");
449 mp_msg(MSGT_VO, MSGL_FATAL,"<vo_directx><FATAL ERROR>no directx 7 or higher installed\n");
450 return 1;
453 // enumerate all display devices attached to the desktop
454 OurDirectDrawEnumerateEx(EnumCallbackEx, NULL, DDENUM_ATTACHEDSECONDARYDEVICES );
456 if(vo_adapter_num >= adapter_count)
457 mp_msg(MSGT_VO, MSGL_ERR,"Selected adapter (%d) doesn't exist: Default Display Adapter selected\n",vo_adapter_num);
459 FreeLibrary(user32dll);
461 OurDirectDrawCreateEx = (void *)GetProcAddress(hddraw_dll, "DirectDrawCreateEx");
462 if ( OurDirectDrawCreateEx == NULL )
464 FreeLibrary( hddraw_dll );
465 hddraw_dll = NULL;
466 mp_msg(MSGT_VO, MSGL_FATAL,"<vo_directx><FATAL ERROR>failed geting proc address: DirectDrawCreateEx\n");
467 return 1;
470 // initialize DirectDraw and create directx v7 object
471 if (OurDirectDrawCreateEx(selected_guid_ptr, (VOID**)&g_lpdd, &IID_IDirectDraw7, NULL ) != DD_OK )
473 FreeLibrary( hddraw_dll );
474 hddraw_dll = NULL;
475 mp_msg(MSGT_VO, MSGL_FATAL,"<vo_directx><FATAL ERROR>can't initialize ddraw\n");
476 return 1;
479 //get current screen siz for selected monitor ...
480 ddsd.dwSize=sizeof(ddsd);
481 ddsd.dwFlags=DDSD_WIDTH|DDSD_HEIGHT|DDSD_PIXELFORMAT;
482 g_lpdd->lpVtbl->GetDisplayMode(g_lpdd, &ddsd);
483 if(vo_screenwidth && vo_screenheight)
485 vm_height=vo_screenheight;
486 vm_width=vo_screenwidth;
488 else
490 vm_height=ddsd.dwHeight;
491 vm_width=ddsd.dwWidth;
495 if(vo_dbpp)vm_bpp=vo_dbpp;
496 else vm_bpp=ddsd.ddpfPixelFormat.dwRGBBitCount;
498 if(vidmode){
499 if (g_lpdd->lpVtbl->SetCooperativeLevel(g_lpdd, hWnd, DDSCL_EXCLUSIVE|DDSCL_FULLSCREEN) != DD_OK)
501 mp_msg(MSGT_VO, MSGL_FATAL,"<vo_directx><FATAL ERROR>can't set cooperativelevel for exclusive mode\n");
502 return 1;
504 /*SetDisplayMode(ddobject,width,height,bpp,refreshrate,aditionalflags)*/
505 if(g_lpdd->lpVtbl->SetDisplayMode(g_lpdd,vm_width, vm_height, vm_bpp,0,0) != DD_OK)
507 mp_msg(MSGT_VO, MSGL_FATAL,"<vo_directx><FATAL ERROR>can't set displaymode\n");
508 return 1;
510 mp_msg(MSGT_VO, MSGL_V,"<vo_directx><INFO>Initialized adapter %i for %i x %i @ %i \n",vo_adapter_num,vm_width,vm_height,vm_bpp);
511 return 0;
513 if (g_lpdd->lpVtbl->SetCooperativeLevel(g_lpdd, hWnd, DDSCL_NORMAL) != DD_OK) // or DDSCL_SETFOCUSWINDOW
515 mp_msg(MSGT_VO, MSGL_FATAL,"<vo_directx><FATAL ERROR>could not set cooperativelevel for hardwarecheck\n");
516 return 1;
518 mp_msg(MSGT_VO, MSGL_DBG3,"<vo_directx><INFO>DirectDraw Initialized\n");
519 return 0;
522 static void check_events(void)
524 MSG msg;
525 while (PeekMessage(&msg, NULL, 0, 0,PM_REMOVE))
527 TranslateMessage(&msg);
528 DispatchMessage(&msg);
532 static uint32_t Directx_ManageDisplay(void)
534 HRESULT ddrval;
535 DDCAPS capsDrv;
536 DDOVERLAYFX ovfx;
537 DWORD dwUpdateFlags=0;
538 int width,height;
540 if(!vidmode && !vo_fs && WinID!=-1) {
541 RECT current_rect = {0, 0, 0, 0};
542 GetWindowRect(hWnd, &current_rect);
543 if ((current_rect.left == last_rect.left)
544 && (current_rect.top == last_rect.top)
545 && (current_rect.right == last_rect.right)
546 && (current_rect.bottom == last_rect.bottom))
547 return 0;
548 last_rect = current_rect;
551 if(vo_fs || vidmode){
552 aspect(&width,&height,A_ZOOM);
553 rd.left=(vo_screenwidth-width)/2;
554 rd.top=(vo_screenheight-height)/2;
555 if (WinID == -1)
556 if(ShowCursor(FALSE)>=0)while(ShowCursor(FALSE)>=0){}
558 else if (WinID != -1 && vo_geometry) {
559 POINT pt;
560 pt.x = vo_dx;
561 pt.y = vo_dy;
562 ClientToScreen(hWnd,&pt);
563 width=d_image_width;
564 height=d_image_height;
565 rd.left = pt.x;
566 rd.top = pt.y;
567 while(ShowCursor(TRUE)<=0){}
569 else {
570 POINT pt;
571 pt.x = 0; //overlayposition relative to the window
572 pt.y = 0;
573 ClientToScreen(hWnd,&pt);
574 GetClientRect(hWnd, &rd);
575 width=rd.right - rd.left;
576 height=rd.bottom - rd.top;
577 pt.x -= monitor_rect.left; /* move coordinates from global to local monitor space */
578 pt.y -= monitor_rect.top;
579 rd.right -= monitor_rect.left;
580 rd.bottom -= monitor_rect.top;
581 rd.left = pt.x;
582 rd.top = pt.y;
583 if(!nooverlay && (!width || !height)){
584 /*window is minimized*/
585 ddrval = g_lpddsOverlay->lpVtbl->UpdateOverlay(g_lpddsOverlay,NULL, g_lpddsPrimary, NULL, DDOVER_HIDE, NULL);
586 return 0;
588 if(vo_keepaspect){
589 int tmpheight=((float)width/window_aspect);
590 tmpheight+=tmpheight%2;
591 if(tmpheight > height){
592 width=((float)height*window_aspect);
593 width+=width%2;
595 else height=tmpheight;
597 if (WinID == -1)
598 while(ShowCursor(TRUE)<=0){}
600 rd.right=rd.left+width;
601 rd.bottom=rd.top+height;
603 /*ok, let's workaround some overlay limitations*/
604 if(!nooverlay)
606 uint32_t uStretchFactor1000; //minimum stretch
607 uint32_t xstretch1000,ystretch1000;
608 /*get driver capabilities*/
609 ZeroMemory(&capsDrv, sizeof(capsDrv));
610 capsDrv.dwSize = sizeof(capsDrv);
611 if(g_lpdd->lpVtbl->GetCaps(g_lpdd,&capsDrv, NULL) != DD_OK)return 1;
612 /*get minimum stretch, depends on display adaptor and mode (refresh rate!) */
613 uStretchFactor1000 = capsDrv.dwMinOverlayStretch>1000 ? capsDrv.dwMinOverlayStretch : 1000;
614 rd.right = ((width+rd.left)*uStretchFactor1000+999)/1000;
615 rd.bottom = (height+rd.top)*uStretchFactor1000/1000;
616 /*calculate xstretch1000 and ystretch1000*/
617 xstretch1000 = ((rd.right - rd.left)* 1000)/image_width ;
618 ystretch1000 = ((rd.bottom - rd.top)* 1000)/image_height;
619 rs.left=0;
620 rs.right=image_width;
621 rs.top=0;
622 rs.bottom=image_height;
623 if(rd.left < 0)rs.left=(-rd.left*1000)/xstretch1000;
624 if(rd.top < 0)rs.top=(-rd.top*1000)/ystretch1000;
625 if(rd.right > vo_screenwidth)rs.right=((vo_screenwidth-rd.left)*1000)/xstretch1000;
626 if(rd.bottom > vo_screenheight)rs.bottom=((vo_screenheight-rd.top)*1000)/ystretch1000;
627 /*do not allow to zoom or shrink if hardware isn't able to do so*/
628 if((width < image_width)&& !(capsDrv.dwFXCaps & DDFXCAPS_OVERLAYSHRINKX))
630 if(capsDrv.dwFXCaps & DDFXCAPS_OVERLAYSHRINKXN)mp_msg(MSGT_VO, MSGL_ERR,"<vo_directx><ERROR>can only shrinkN\n");
631 else mp_msg(MSGT_VO, MSGL_ERR,"<vo_directx><ERROR>can't shrink x\n");
632 rd.right=rd.left+image_width;
634 else if((width > image_width)&& !(capsDrv.dwFXCaps & DDFXCAPS_OVERLAYSTRETCHX))
636 if(capsDrv.dwFXCaps & DDFXCAPS_OVERLAYSTRETCHXN)mp_msg(MSGT_VO, MSGL_ERR,"<vo_directx><ERROR>can only stretchN\n");
637 else mp_msg(MSGT_VO, MSGL_ERR,"<vo_directx><ERROR>can't stretch x\n");
638 rd.right = rd.left+image_width;
640 if((height < image_height) && !(capsDrv.dwFXCaps & DDFXCAPS_OVERLAYSHRINKY))
642 if(capsDrv.dwFXCaps & DDFXCAPS_OVERLAYSHRINKYN)mp_msg(MSGT_VO, MSGL_ERR,"<vo_directx><ERROR>can only shrinkN\n");
643 else mp_msg(MSGT_VO, MSGL_ERR,"<vo_directx><ERROR>can't shrink y\n");
644 rd.bottom = rd.top + image_height;
646 else if((height > image_height ) && !(capsDrv.dwFXCaps & DDFXCAPS_OVERLAYSTRETCHY))
648 if(capsDrv.dwFXCaps & DDFXCAPS_OVERLAYSTRETCHYN)mp_msg(MSGT_VO, MSGL_ERR,"<vo_directx><ERROR>can only stretchN\n");
649 else mp_msg(MSGT_VO, MSGL_ERR,"<vo_directx><ERROR>can't stretch y\n");
650 rd.bottom = rd.top + image_height;
652 /*the last thing to check are alignment restrictions
653 these expressions (x & -y) just do alignment by dropping low order bits...
654 so to round up, we add first, then truncate*/
655 if((capsDrv.dwCaps & DDCAPS_ALIGNBOUNDARYSRC) && capsDrv.dwAlignBoundarySrc)
656 rs.left = (rs.left + capsDrv.dwAlignBoundarySrc / 2) & -(signed)(capsDrv.dwAlignBoundarySrc);
657 if((capsDrv.dwCaps & DDCAPS_ALIGNSIZESRC) && capsDrv.dwAlignSizeSrc)
658 rs.right = rs.left + ((rs.right - rs.left + capsDrv.dwAlignSizeSrc / 2) & -(signed) (capsDrv.dwAlignSizeSrc));
659 if((capsDrv.dwCaps & DDCAPS_ALIGNBOUNDARYDEST) && capsDrv.dwAlignBoundaryDest)
660 rd.left = (rd.left + capsDrv.dwAlignBoundaryDest / 2) & -(signed)(capsDrv.dwAlignBoundaryDest);
661 if((capsDrv.dwCaps & DDCAPS_ALIGNSIZEDEST) && capsDrv.dwAlignSizeDest)
662 rd.right = rd.left + ((rd.right - rd.left) & -(signed) (capsDrv.dwAlignSizeDest));
663 /*create an overlay FX structure to specify a destination color key*/
664 ZeroMemory(&ovfx, sizeof(ovfx));
665 ovfx.dwSize = sizeof(ovfx);
666 if(vo_fs||vidmode)
668 ovfx.dckDestColorkey.dwColorSpaceLowValue = 0;
669 ovfx.dckDestColorkey.dwColorSpaceHighValue = 0;
671 else
673 ovfx.dckDestColorkey.dwColorSpaceLowValue = destcolorkey;
674 ovfx.dckDestColorkey.dwColorSpaceHighValue = destcolorkey;
676 // set the flags we'll send to UpdateOverlay //DDOVER_AUTOFLIP|DDOVERFX_MIRRORLEFTRIGHT|DDOVERFX_MIRRORUPDOWN could be useful?;
677 dwUpdateFlags = DDOVER_SHOW | DDOVER_DDFX;
678 /*if hardware can't do colorkeying set the window on top*/
679 if(capsDrv.dwCKeyCaps & DDCKEYCAPS_DESTOVERLAY) dwUpdateFlags |= DDOVER_KEYDESTOVERRIDE;
680 else if (!tmp_image) vo_ontop = 1;
682 else
684 g_lpddclipper->lpVtbl->SetHWnd(g_lpddclipper, 0,(vo_fs && !vidmode)?hWndFS: hWnd);
687 if(!vidmode && !vo_fs){
688 if(WinID == -1) {
689 RECT rdw=rd;
690 if (vo_border)
691 AdjustWindowRect(&rdw,WNDSTYLE,FALSE);
692 // printf("window: %i %i %ix%i\n",rdw.left,rdw.top,rdw.right - rdw.left,rdw.bottom - rdw.top);
693 rdw.left += monitor_rect.left; /* move to global coordinate space */
694 rdw.top += monitor_rect.top;
695 rdw.right += monitor_rect.left;
696 rdw.bottom += monitor_rect.top;
697 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);
700 else SetWindowPos(vidmode?hWnd:hWndFS,vo_rootwin?HWND_BOTTOM:HWND_TOPMOST,0,0,0,0,SWP_NOMOVE|SWP_NOSIZE|SWP_NOOWNERZORDER|SWP_NOCOPYBITS);
702 /*make sure the overlay is inside the screen*/
703 if(rd.left<0)rd.left=0;
704 if(rd.right>vo_screenwidth)rd.right=vo_screenwidth;
705 if(rd.top<0)rd.top=0;
706 if(rd.bottom>vo_screenheight)rd.bottom=vo_screenheight;
708 /*for nonoverlay mode we are finished, for overlay mode we have to display the overlay first*/
709 if(nooverlay)return 0;
711 // printf("overlay: %i %i %ix%i\n",rd.left,rd.top,rd.right - rd.left,rd.bottom - rd.top);
712 ddrval = g_lpddsOverlay->lpVtbl->UpdateOverlay(g_lpddsOverlay,&rs, g_lpddsPrimary, &rd, dwUpdateFlags, &ovfx);
713 if(FAILED(ddrval))
715 // one cause might be the driver lied about minimum stretch
716 // we should try upping the destination size a bit, or
717 // perhaps shrinking the source size
718 mp_msg(MSGT_VO, MSGL_ERR ,"<vo_directx><ERROR>UpdateOverlay failed\n" );
719 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 );
720 mp_msg(MSGT_VO, MSGL_ERR ,"<vo_directx><ERROR>");
721 switch (ddrval)
723 case DDERR_NOSTRETCHHW:
724 {mp_msg(MSGT_VO, MSGL_ERR ,"hardware can't stretch: try to size the window back\n");break;}
725 case DDERR_INVALIDRECT:
726 {mp_msg(MSGT_VO, MSGL_ERR ,"invalid rectangle\n");break;}
727 case DDERR_INVALIDPARAMS:
728 {mp_msg(MSGT_VO, MSGL_ERR ,"invalid parameters\n");break;}
729 case DDERR_HEIGHTALIGN:
730 {mp_msg(MSGT_VO, MSGL_ERR ,"height align\n");break;}
731 case DDERR_XALIGN:
732 {mp_msg(MSGT_VO, MSGL_ERR ,"x align\n");break;}
733 case DDERR_UNSUPPORTED:
734 {mp_msg(MSGT_VO, MSGL_ERR ,"unsupported\n");break;}
735 case DDERR_INVALIDSURFACETYPE:
736 {mp_msg(MSGT_VO, MSGL_ERR ,"invalid surfacetype\n");break;}
737 case DDERR_INVALIDOBJECT:
738 {mp_msg(MSGT_VO, MSGL_ERR ,"invalid object\n");break;}
739 case DDERR_SURFACELOST:
741 mp_msg(MSGT_VO, MSGL_ERR ,"surfaces lost\n");
742 g_lpddsOverlay->lpVtbl->Restore( g_lpddsOverlay ); //restore and try again
743 g_lpddsPrimary->lpVtbl->Restore( g_lpddsPrimary );
744 ddrval = g_lpddsOverlay->lpVtbl->UpdateOverlay(g_lpddsOverlay,&rs, g_lpddsPrimary, &rd, dwUpdateFlags, &ovfx);
745 if(ddrval !=DD_OK)mp_msg(MSGT_VO, MSGL_FATAL ,"<vo_directx><FATAL ERROR>UpdateOverlay failed again\n" );
746 break;
748 default:
749 mp_msg(MSGT_VO, MSGL_ERR ," 0x%x\n",ddrval);
751 /*ok we can't do anything about it -> hide overlay*/
752 if(ddrval != DD_OK)
754 ddrval = g_lpddsOverlay->lpVtbl->UpdateOverlay(g_lpddsOverlay,NULL, g_lpddsPrimary, NULL, DDOVER_HIDE, NULL);
755 return 1;
758 return 0;
761 //find out supported overlay pixelformats
762 static uint32_t Directx_CheckOverlayPixelformats(void)
764 DDCAPS capsDrv;
765 HRESULT ddrval;
766 DDSURFACEDESC2 ddsdOverlay;
767 uint32_t i;
768 uint32_t formatcount = 0;
769 //get driver caps to determine overlay support
770 ZeroMemory(&capsDrv, sizeof(capsDrv));
771 capsDrv.dwSize = sizeof(capsDrv);
772 ddrval = g_lpdd->lpVtbl->GetCaps(g_lpdd,&capsDrv, NULL);
773 if (FAILED(ddrval))
775 mp_msg(MSGT_VO, MSGL_ERR ,"<vo_directx><ERROR>failed getting ddrawcaps\n");
776 return 1;
778 if (!(capsDrv.dwCaps & DDCAPS_OVERLAY))
780 mp_msg(MSGT_VO, MSGL_ERR ,"<vo_directx><ERROR>Your card doesn't support overlay\n");
781 return 1;
783 mp_msg(MSGT_VO, MSGL_V ,"<vo_directx><INFO>testing supported overlay pixelformats\n");
784 //it is not possible to query for pixel formats supported by the
785 //overlay hardware: try out various formats till one works
786 ZeroMemory(&ddsdOverlay, sizeof(ddsdOverlay));
787 ddsdOverlay.dwSize = sizeof(ddsdOverlay);
788 ddsdOverlay.ddsCaps.dwCaps=DDSCAPS_OVERLAY | DDSCAPS_VIDEOMEMORY;
789 ddsdOverlay.dwFlags= DDSD_CAPS|DDSD_HEIGHT|DDSD_WIDTH| DDSD_PIXELFORMAT;
790 ddsdOverlay.dwWidth=300;
791 ddsdOverlay.dwHeight=280;
792 ddsdOverlay.dwBackBufferCount=0;
793 //try to create an overlay surface using one of the pixel formats in our global list
794 i=0;
797 ddsdOverlay.ddpfPixelFormat=g_ddpf[i].g_ddpfOverlay;
798 ddrval = g_lpdd->lpVtbl->CreateSurface(g_lpdd,&ddsdOverlay, &g_lpddsOverlay, NULL);
799 if (ddrval == DD_OK)
801 mp_msg(MSGT_VO, MSGL_V ,"<vo_directx><FORMAT OVERLAY>%i %s supported\n",i,g_ddpf[i].img_format_name);
802 g_ddpf[i].drv_caps = VFCAP_CSP_SUPPORTED |VFCAP_OSD |VFCAP_CSP_SUPPORTED_BY_HW | VFCAP_HWSCALE_UP;
803 formatcount++;}
804 else mp_msg(MSGT_VO, MSGL_V ,"<vo_directx><FORMAT OVERLAY>%i %s not supported\n",i,g_ddpf[i].img_format_name);
805 if (g_lpddsOverlay != NULL) {g_lpddsOverlay->lpVtbl->Release(g_lpddsOverlay);g_lpddsOverlay = NULL;}
806 } while( ++i < NUM_FORMATS );
807 mp_msg(MSGT_VO, MSGL_V ,"<vo_directx><INFO>Your card supports %i of %i overlayformats\n",formatcount, NUM_FORMATS);
808 if (formatcount == 0)
810 mp_msg(MSGT_VO, MSGL_V ,"<vo_directx><WARN>Your card supports overlay, but we couldn't create one\n");
811 mp_msg(MSGT_VO, MSGL_V ,"<vo_directx><INFO>This can have the following reasons:\n");
812 mp_msg(MSGT_VO, MSGL_V ,"<vo_directx><INFO>- you are already using an overlay with another app\n");
813 mp_msg(MSGT_VO, MSGL_V ,"<vo_directx><INFO>- you don't have enough videomemory\n");
814 mp_msg(MSGT_VO, MSGL_V ,"<vo_directx><INFO>- vo_directx doesn't support the cards overlay pixelformat\n");
815 return 1;
817 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
818 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 ;)
819 return 0;
822 //find out the Pixelformat of the Primary Surface
823 static uint32_t Directx_CheckPrimaryPixelformat(void)
825 uint32_t i=0;
826 uint32_t formatcount = 0;
827 DDPIXELFORMAT ddpf;
828 DDSURFACEDESC2 ddsd;
829 HDC hdc;
830 HRESULT hres;
831 COLORREF rgbT=RGB(0,0,0);
832 mp_msg(MSGT_VO, MSGL_V ,"<vo_directx><INFO>checking primary surface\n");
833 memset( &ddpf, 0, sizeof( DDPIXELFORMAT ));
834 ddpf.dwSize = sizeof( DDPIXELFORMAT );
835 //we have to create a primary surface first
836 if(Directx_CreatePrimarySurface()!=0)return 1;
837 if(g_lpddsPrimary->lpVtbl->GetPixelFormat( g_lpddsPrimary, &ddpf ) != DD_OK )
839 mp_msg(MSGT_VO, MSGL_FATAL ,"<vo_directx><FATAL ERROR>can't get pixelformat\n");
840 return 1;
842 while ( i < NUM_FORMATS )
844 if (g_ddpf[i].g_ddpfOverlay.dwRGBBitCount == ddpf.dwRGBBitCount)
846 if (g_ddpf[i].g_ddpfOverlay.dwRBitMask == ddpf.dwRBitMask)
848 mp_msg(MSGT_VO, MSGL_V ,"<vo_directx><FORMAT PRIMARY>%i %s supported\n",i,g_ddpf[i].img_format_name);
849 g_ddpf[i].drv_caps = VFCAP_CSP_SUPPORTED |VFCAP_OSD;
850 formatcount++;
851 primary_image_format=g_ddpf[i].img_format;
854 i++;
856 //get the colorkey for overlay mode
857 destcolorkey = CLR_INVALID;
858 if (windowcolor != CLR_INVALID && g_lpddsPrimary->lpVtbl->GetDC(g_lpddsPrimary,&hdc) == DD_OK)
860 rgbT = GetPixel(hdc, 0, 0);
861 SetPixel(hdc, 0, 0, windowcolor);
862 g_lpddsPrimary->lpVtbl->ReleaseDC(g_lpddsPrimary,hdc);
864 // read back the converted color
865 ddsd.dwSize = sizeof(ddsd);
866 while ((hres = g_lpddsPrimary->lpVtbl->Lock(g_lpddsPrimary,NULL, &ddsd, 0, NULL)) == DDERR_WASSTILLDRAWING)
868 if (hres == DD_OK)
870 destcolorkey = *(DWORD *) ddsd.lpSurface;
871 if (ddsd.ddpfPixelFormat.dwRGBBitCount < 32)
872 destcolorkey &= (1 << ddsd.ddpfPixelFormat.dwRGBBitCount) - 1;
873 g_lpddsPrimary->lpVtbl->Unlock(g_lpddsPrimary,NULL);
875 if (windowcolor != CLR_INVALID && g_lpddsPrimary->lpVtbl->GetDC(g_lpddsPrimary,&hdc) == DD_OK)
877 SetPixel(hdc, 0, 0, rgbT);
878 g_lpddsPrimary->lpVtbl->ReleaseDC(g_lpddsPrimary,hdc);
880 //release primary
881 g_lpddsPrimary->lpVtbl->Release(g_lpddsPrimary);
882 g_lpddsPrimary = NULL;
883 if(formatcount==0)
885 mp_msg(MSGT_VO, MSGL_FATAL ,"<vo_directx><FATAL ERROR>Unknown Pixelformat\n");
886 return 1;
888 return 0;
891 //function handles input
892 static LRESULT CALLBACK WndProc(HWND hWnd, UINT message, WPARAM wParam, LPARAM lParam)
894 switch (message)
896 case WM_MOUSEACTIVATE:
897 return MA_ACTIVATEANDEAT;
898 case WM_NCACTIVATE:
900 if(vidmode && adapter_count > 2) //only disable if more than one adapter.
901 return 0;
902 break;
904 case WM_DESTROY:
906 PostQuitMessage(0);
907 return 0;
909 case WM_CLOSE:
911 mplayer_put_key(KEY_CLOSE_WIN);
912 return 0;
914 case WM_WINDOWPOSCHANGED:
916 //printf("Windowposchange\n");
917 if(g_lpddsBack != NULL) //or it will crash with -vm
919 Directx_ManageDisplay();
921 break;
923 case WM_SYSCOMMAND:
925 switch (wParam)
926 { //kill screensaver etc.
927 //note: works only when the window is active
928 //you can workaround this by disabling the allow screensaver option in
929 //the link to the app
930 case SC_SCREENSAVE:
931 case SC_MONITORPOWER:
932 mp_msg(MSGT_VO, MSGL_V ,"<vo_directx><INFO>killing screensaver\n" );
933 return 0;
934 case SC_MAXIMIZE:
935 if (!vo_fs) control(VOCTRL_FULLSCREEN, NULL);
936 return 0;
938 break;
940 case WM_KEYDOWN:
942 switch (wParam)
944 case VK_LEFT:
945 {mplayer_put_key(KEY_LEFT);break;}
946 case VK_UP:
947 {mplayer_put_key(KEY_UP);break;}
948 case VK_RIGHT:
949 {mplayer_put_key(KEY_RIGHT);break;}
950 case VK_DOWN:
951 {mplayer_put_key(KEY_DOWN);break;}
952 case VK_TAB:
953 {mplayer_put_key(KEY_TAB);break;}
954 case VK_BACK:
955 {mplayer_put_key(KEY_BS);break;}
956 case VK_DELETE:
957 {mplayer_put_key(KEY_DELETE);break;}
958 case VK_INSERT:
959 {mplayer_put_key(KEY_INSERT);break;}
960 case VK_HOME:
961 {mplayer_put_key(KEY_HOME);break;}
962 case VK_END:
963 {mplayer_put_key(KEY_END);break;}
964 case VK_PRIOR:
965 {mplayer_put_key(KEY_PAGE_UP);break;}
966 case VK_NEXT:
967 {mplayer_put_key(KEY_PAGE_DOWN);break;}
968 case VK_ESCAPE:
969 {mplayer_put_key(KEY_ESC);break;}
971 break;
973 case WM_CHAR:
975 mplayer_put_key(wParam);
976 break;
978 case WM_LBUTTONDOWN:
980 if (!vo_nomouse_input)
981 mplayer_put_key(MOUSE_BTN0);
982 break;
984 case WM_MBUTTONDOWN:
986 if (!vo_nomouse_input)
987 mplayer_put_key(MOUSE_BTN1);
988 break;
990 case WM_RBUTTONDOWN:
992 if (!vo_nomouse_input)
993 mplayer_put_key(MOUSE_BTN2);
994 break;
996 case WM_LBUTTONDBLCLK:
998 if(!vo_nomouse_input)
999 mplayer_put_key(MOUSE_BTN0_DBL);
1000 break;
1002 case WM_MBUTTONDBLCLK:
1004 if(!vo_nomouse_input)
1005 mplayer_put_key(MOUSE_BTN1_DBL);
1006 break;
1008 case WM_RBUTTONDBLCLK:
1010 if(!vo_nomouse_input)
1011 mplayer_put_key(MOUSE_BTN2_DBL);
1012 break;
1014 case WM_MOUSEWHEEL:
1016 int x;
1017 if (vo_nomouse_input)
1018 break;
1019 x = GET_WHEEL_DELTA_WPARAM(wParam);
1020 if (x > 0)
1021 mplayer_put_key(MOUSE_BTN3);
1022 else
1023 mplayer_put_key(MOUSE_BTN4);
1024 break;
1026 case WM_XBUTTONDOWN:
1028 if (vo_nomouse_input)
1029 break;
1030 if (HIWORD(wParam) == 1)
1031 mplayer_put_key(MOUSE_BTN5);
1032 else
1033 mplayer_put_key(MOUSE_BTN6);
1034 break;
1036 case WM_XBUTTONDBLCLK:
1038 if (vo_nomouse_input)
1039 break;
1040 if (HIWORD(wParam) == 1)
1041 mplayer_put_key(MOUSE_BTN5_DBL);
1042 else
1043 mplayer_put_key(MOUSE_BTN6_DBL);
1044 break;
1048 return DefWindowProc(hWnd, message, wParam, lParam);
1052 static int preinit(const char *arg)
1054 HINSTANCE hInstance = GetModuleHandle(NULL);
1055 char exedir[MAX_PATH];
1056 WNDCLASS wc;
1057 if(arg)
1059 if(strstr(arg,"noaccel"))
1061 mp_msg(MSGT_VO,MSGL_V,"<vo_directx><INFO>disabled overlay\n");
1062 nooverlay = 1;
1065 /*load icon from the main app*/
1066 if(GetModuleFileName(NULL,exedir,MAX_PATH))
1068 mplayericon = ExtractIcon( hInstance, exedir, 0 );
1070 if(!mplayericon)mplayericon=LoadIcon(NULL,IDI_APPLICATION);
1071 mplayercursor = LoadCursor(NULL, IDC_ARROW);
1072 monitor_rect.right=GetSystemMetrics(SM_CXSCREEN);
1073 monitor_rect.bottom=GetSystemMetrics(SM_CYSCREEN);
1075 windowcolor = vo_colorkey;
1076 colorbrush = CreateSolidBrush(windowcolor);
1077 blackbrush = (HBRUSH)GetStockObject(BLACK_BRUSH);
1078 wc.style = CS_HREDRAW | CS_VREDRAW | CS_DBLCLKS;
1079 wc.lpfnWndProc = WndProc;
1080 wc.cbClsExtra = 0;
1081 wc.cbWndExtra = 0;
1082 wc.hInstance = hInstance;
1083 wc.hCursor = mplayercursor;
1084 wc.hIcon = mplayericon;
1085 wc.hbrBackground = vidmode ? blackbrush : colorbrush;
1086 wc.lpszClassName = WNDCLASSNAME_WINDOWED;
1087 wc.lpszMenuName = NULL;
1088 RegisterClass(&wc);
1089 if (WinID != -1) hWnd = WinID;
1090 else
1091 hWnd = CreateWindowEx(vidmode?WS_EX_TOPMOST:0,
1092 WNDCLASSNAME_WINDOWED,"",(vidmode || !vo_border)?WS_POPUP:WNDSTYLE,
1093 CW_USEDEFAULT, CW_USEDEFAULT, 100, 100,NULL,NULL,hInstance,NULL);
1094 wc.hbrBackground = blackbrush;
1095 wc.lpszClassName = WNDCLASSNAME_FULLSCREEN;
1096 RegisterClass(&wc);
1098 if (Directx_InitDirectDraw()!= 0)return 1; //init DirectDraw
1100 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);
1101 mp_msg(MSGT_VO, MSGL_DBG3 ,"<vo_directx><INFO>initial mplayer windows created\n");
1103 if (Directx_CheckPrimaryPixelformat()!=0)return 1;
1104 if (!nooverlay && Directx_CheckOverlayPixelformats() == 0) //check for supported hardware
1106 mp_msg(MSGT_VO, MSGL_V ,"<vo_directx><INFO>hardware supports overlay\n");
1107 nooverlay = 0;
1109 else //if we can't have overlay we create a backpuffer with the same imageformat as the primary surface
1111 mp_msg(MSGT_VO, MSGL_V ,"<vo_directx><INFO>using backpuffer\n");
1112 nooverlay = 1;
1114 mp_msg(MSGT_VO, MSGL_DBG3 ,"<vo_directx><INFO>preinit succesfully finished\n");
1115 return 0;
1118 static int draw_slice(uint8_t *src[], int stride[], int w,int h,int x,int y )
1120 uint8_t *s;
1121 uint8_t *d;
1122 uint32_t uvstride=dstride/2;
1123 // copy Y
1124 d=image+dstride*y+x;
1125 s=src[0];
1126 mem2agpcpy_pic(d,s,w,h,dstride,stride[0]);
1128 w/=2;h/=2;x/=2;y/=2;
1130 // copy U
1131 d=image+dstride*image_height + uvstride*y+x;
1132 if(image_format == IMGFMT_YV12)s=src[2];
1133 else s=src[1];
1134 mem2agpcpy_pic(d,s,w,h,uvstride,stride[1]);
1136 // copy V
1137 d=image+dstride*image_height +uvstride*(image_height/2) + uvstride*y+x;
1138 if(image_format == IMGFMT_YV12)s=src[1];
1139 else s=src[2];
1140 mem2agpcpy_pic(d,s,w,h,uvstride,stride[2]);
1141 return 0;
1144 static void flip_page(void)
1146 HRESULT dxresult;
1147 g_lpddsBack->lpVtbl->Unlock (g_lpddsBack,NULL);
1148 if (vo_doublebuffering)
1150 // flip to the next image in the sequence
1151 dxresult = g_lpddsOverlay->lpVtbl->Flip( g_lpddsOverlay,NULL, DDFLIP_WAIT);
1152 if(dxresult == DDERR_SURFACELOST)
1154 mp_msg(MSGT_VO, MSGL_ERR,"<vo_directx><ERROR><vo_directx><INFO>Restoring Surface\n");
1155 g_lpddsBack->lpVtbl->Restore( g_lpddsBack );
1156 // restore overlay and primary before calling
1157 // Directx_ManageDisplay() to avoid error messages
1158 g_lpddsOverlay->lpVtbl->Restore( g_lpddsOverlay );
1159 g_lpddsPrimary->lpVtbl->Restore( g_lpddsPrimary );
1160 // update overlay in case we return from screensaver
1161 Directx_ManageDisplay();
1162 dxresult = g_lpddsOverlay->lpVtbl->Flip( g_lpddsOverlay,NULL, DDFLIP_WAIT);
1164 if(dxresult != DD_OK)mp_msg(MSGT_VO, MSGL_ERR,"<vo_directx><ERROR>can't flip page\n");
1166 if(nooverlay)
1168 DDBLTFX ddbltfx;
1169 // ask for the "NOTEARING" option
1170 memset( &ddbltfx, 0, sizeof(DDBLTFX) );
1171 ddbltfx.dwSize = sizeof(DDBLTFX);
1172 ddbltfx.dwDDFX = DDBLTFX_NOTEARING;
1173 g_lpddsPrimary->lpVtbl->Blt(g_lpddsPrimary, &rd, g_lpddsBack, NULL, DDBLT_WAIT, &ddbltfx);
1175 if (g_lpddsBack->lpVtbl->Lock(g_lpddsBack,NULL,&ddsdsf, DDLOCK_NOSYSLOCK | DDLOCK_WAIT , NULL) == DD_OK) {
1176 if(vo_directrendering && (dstride != ddsdsf.lPitch)){
1177 mp_msg(MSGT_VO,MSGL_WARN,"<vo_directx><WARN>stride changed !!!! disabling direct rendering\n");
1178 vo_directrendering=0;
1180 if (tmp_image)
1181 free(tmp_image);
1182 tmp_image = NULL;
1183 dstride = ddsdsf.lPitch;
1184 image = ddsdsf.lpSurface;
1185 } else if (!tmp_image) {
1186 mp_msg(MSGT_VO, MSGL_WARN, "<vo_directx><WARN>Locking the surface failed, rendering to a hidden surface!\n");
1187 tmp_image = image = calloc(1, image_height * dstride * 2);
1191 static int draw_frame(uint8_t *src[])
1193 fast_memcpy( image, *src, dstride * image_height );
1194 return 0;
1197 static uint32_t get_image(mp_image_t *mpi)
1199 if(mpi->flags&MP_IMGFLAG_READABLE) {mp_msg(MSGT_VO, MSGL_V,"<vo_directx><ERROR>slow video ram\n");return VO_FALSE;}
1200 if(mpi->type==MP_IMGTYPE_STATIC) {mp_msg(MSGT_VO, MSGL_V,"<vo_directx><ERROR>not static\n");return VO_FALSE;}
1201 if((mpi->width==dstride) || (mpi->flags&(MP_IMGFLAG_ACCEPT_STRIDE|MP_IMGFLAG_ACCEPT_WIDTH)))
1203 if(mpi->flags&MP_IMGFLAG_PLANAR)
1205 if(image_format == IMGFMT_YV12)
1207 mpi->planes[2]= image + dstride*image_height;
1208 mpi->planes[1]= image + dstride*image_height+ dstride*image_height/4;
1209 mpi->stride[1]=mpi->stride[2]=dstride/2;
1211 else if(image_format == IMGFMT_IYUV || image_format == IMGFMT_I420)
1213 mpi->planes[1]= image + dstride*image_height;
1214 mpi->planes[2]= image + dstride*image_height+ dstride*image_height/4;
1215 mpi->stride[1]=mpi->stride[2]=dstride/2;
1217 else if(image_format == IMGFMT_YVU9)
1219 mpi->planes[2] = image + dstride*image_height;
1220 mpi->planes[1] = image + dstride*image_height+ dstride*image_height/16;
1221 mpi->stride[1]=mpi->stride[2]=dstride/4;
1224 mpi->planes[0]=image;
1225 mpi->stride[0]=dstride;
1226 mpi->width=image_width;
1227 mpi->height=image_height;
1228 mpi->flags|=MP_IMGFLAG_DIRECT;
1229 mp_msg(MSGT_VO, MSGL_DBG3, "<vo_directx><INFO>Direct Rendering ENABLED\n");
1230 return VO_TRUE;
1232 return VO_FALSE;
1235 static uint32_t put_image(mp_image_t *mpi){
1237 uint8_t *d;
1238 uint8_t *s;
1239 uint32_t x = mpi->x;
1240 uint32_t y = mpi->y;
1241 uint32_t w = mpi->w;
1242 uint32_t h = mpi->h;
1244 if (WinID != -1) Directx_ManageDisplay();
1246 if((mpi->flags&MP_IMGFLAG_DIRECT)||(mpi->flags&MP_IMGFLAG_DRAW_CALLBACK))
1248 mp_msg(MSGT_VO, MSGL_DBG3 ,"<vo_directx><INFO>put_image: nothing to do: drawslices\n");
1249 return VO_TRUE;
1252 if (mpi->flags&MP_IMGFLAG_PLANAR)
1255 if(image_format!=IMGFMT_YVU9)draw_slice(mpi->planes,mpi->stride,mpi->w,mpi->h,0,0);
1256 else
1258 // copy Y
1259 d=image+dstride*y+x;
1260 s=mpi->planes[0];
1261 mem2agpcpy_pic(d,s,w,h,dstride,mpi->stride[0]);
1262 w/=4;h/=4;x/=4;y/=4;
1263 // copy V
1264 d=image+dstride*image_height + dstride*y/4+x;
1265 s=mpi->planes[2];
1266 mem2agpcpy_pic(d,s,w,h,dstride/4,mpi->stride[1]);
1267 // copy U
1268 d=image+dstride*image_height + dstride*image_height/16 + dstride/4*y+x;
1269 s=mpi->planes[1];
1270 mem2agpcpy_pic(d,s,w,h,dstride/4,mpi->stride[2]);
1273 else //packed
1275 mem2agpcpy_pic(image, mpi->planes[0], w * (mpi->bpp / 8), h, dstride, mpi->stride[0]);
1277 return VO_TRUE;
1280 static int
1281 config(uint32_t width, uint32_t height, uint32_t d_width, uint32_t d_height, uint32_t options, char *title, uint32_t format)
1283 RECT rd;
1284 vo_fs = options & 0x01;
1285 image_format = format;
1286 image_width = width;
1287 image_height = height;
1288 d_image_width = d_width;
1289 d_image_height = d_height;
1290 if(format != primary_image_format)nooverlay = 0;
1291 window_aspect= (float)d_image_width / (float)d_image_height;
1293 /*release all directx objects*/
1294 if (g_cc != NULL)g_cc->lpVtbl->Release(g_cc);
1295 g_cc=NULL;
1296 if(g_lpddclipper)g_lpddclipper->lpVtbl->Release(g_lpddclipper);
1297 g_lpddclipper=NULL;
1298 if (g_lpddsBack != NULL) g_lpddsBack->lpVtbl->Release(g_lpddsBack);
1299 g_lpddsBack = NULL;
1300 if(vo_doublebuffering)
1301 if (g_lpddsOverlay != NULL)g_lpddsOverlay->lpVtbl->Release(g_lpddsOverlay);
1302 g_lpddsOverlay = NULL;
1303 if (g_lpddsPrimary != NULL) g_lpddsPrimary->lpVtbl->Release(g_lpddsPrimary);
1304 g_lpddsPrimary = NULL;
1305 mp_msg(MSGT_VO, MSGL_DBG3,"<vo_directx><INFO>overlay surfaces released\n");
1307 if(!vidmode){
1308 if(!vo_geometry){
1309 GetWindowRect(hWnd,&rd);
1310 vo_dx=rd.left;
1311 vo_dy=rd.top;
1313 vo_dx += monitor_rect.left; /* move position to global window space */
1314 vo_dy += monitor_rect.top;
1315 rd.left = vo_dx;
1316 rd.top = vo_dy;
1317 rd.right = rd.left + d_image_width;
1318 rd.bottom = rd.top + d_image_height;
1319 if (WinID == -1) {
1320 if (vo_border)
1321 AdjustWindowRect(&rd,WNDSTYLE,FALSE);
1322 SetWindowPos(hWnd,NULL, vo_dx, vo_dy,rd.right-rd.left,rd.bottom-rd.top,SWP_SHOWWINDOW|SWP_NOOWNERZORDER);
1325 else ShowWindow(hWnd,SW_SHOW);
1327 if(vo_fs && !vidmode)ShowWindow(hWndFS,SW_SHOW);
1328 if (WinID == -1)
1329 SetWindowText(hWnd,title);
1332 if(vidmode)vo_fs=0;
1335 /*create the surfaces*/
1336 if(Directx_CreatePrimarySurface())return 1;
1338 //create palette for 256 color mode
1339 if(image_format==IMGFMT_BGR8){
1340 LPDIRECTDRAWPALETTE ddpalette=NULL;
1341 char* palette=malloc(4*256);
1342 int i;
1343 for(i=0; i<256; i++){
1344 palette[4*i+0] = ((i >> 5) & 0x07) * 255 / 7;
1345 palette[4*i+1] = ((i >> 2) & 0x07) * 255 / 7;
1346 palette[4*i+2] = ((i >> 0) & 0x03) * 255 / 3;
1347 palette[4*i+3] = PC_NOCOLLAPSE;
1349 g_lpdd->lpVtbl->CreatePalette(g_lpdd,DDPCAPS_8BIT|DDPCAPS_INITIALIZE,palette,&ddpalette,NULL);
1350 g_lpddsPrimary->lpVtbl->SetPalette(g_lpddsPrimary,ddpalette);
1351 free(palette);
1352 ddpalette->lpVtbl->Release(ddpalette);
1355 if (!nooverlay && Directx_CreateOverlay(image_format))
1357 if(format == primary_image_format)nooverlay=1; /*overlay creation failed*/
1358 else {
1359 mp_msg(MSGT_VO, MSGL_FATAL,"<vo_directx><FATAL ERROR>can't use overlay mode: please use -vo directx:noaccel\n");
1360 return 1;
1363 if(nooverlay)
1365 if(Directx_CreateBackpuffer())
1367 mp_msg(MSGT_VO, MSGL_FATAL,"<vo_directx><FATAL ERROR>can't get the driver to work on your system :(\n");
1368 return 1;
1370 mp_msg(MSGT_VO, MSGL_V,"<vo_directx><INFO>back surface created\n");
1371 vo_doublebuffering = 0;
1372 /*create clipper for nonoverlay mode*/
1373 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;}
1374 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;}
1375 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;}
1376 mp_msg(MSGT_VO, MSGL_DBG3,"<vo_directx><INFO>clipper succesfully created\n");
1377 }else{
1378 if(DD_OK != g_lpddsOverlay->lpVtbl->QueryInterface(g_lpddsOverlay,&IID_IDirectDrawColorControl,(void**)&g_cc))
1379 mp_msg(MSGT_VO, MSGL_V,"<vo_directx><WARN>unable to get DirectDraw ColorControl interface\n");
1381 Directx_ManageDisplay();
1382 memset(&ddsdsf, 0,sizeof(DDSURFACEDESC2));
1383 ddsdsf.dwSize = sizeof (DDSURFACEDESC2);
1384 if (g_lpddsBack->lpVtbl->Lock(g_lpddsBack,NULL,&ddsdsf, DDLOCK_NOSYSLOCK | DDLOCK_WAIT, NULL) == DD_OK) {
1385 dstride = ddsdsf.lPitch;
1386 image = ddsdsf.lpSurface;
1387 return 0;
1389 mp_msg(MSGT_VO, MSGL_V, "<vo_directx><ERROR>Initial Lock on the Surface failed.\n");
1390 return 1;
1393 //function to set color controls
1394 // brightness [0, 10000]
1395 // contrast [0, 20000]
1396 // hue [-180, 180]
1397 // saturation [0, 20000]
1398 static uint32_t color_ctrl_set(char *what, int value)
1400 uint32_t r = VO_NOTIMPL;
1401 DDCOLORCONTROL dcc;
1402 //printf("\n*** %s = %d\n", what, value);
1403 if (!g_cc) {
1404 //printf("\n *** could not get color control interface!!!\n");
1405 return VO_NOTIMPL;
1407 ZeroMemory(&dcc, sizeof(dcc));
1408 dcc.dwSize = sizeof(dcc);
1410 if (!strcmp(what, "brightness")) {
1411 dcc.dwFlags = DDCOLOR_BRIGHTNESS;
1412 dcc.lBrightness = (value + 100) * 10000 / 200;
1413 r = VO_TRUE;
1414 } else if (!strcmp(what, "contrast")) {
1415 dcc.dwFlags = DDCOLOR_CONTRAST;
1416 dcc.lContrast = (value + 100) * 20000 / 200;
1417 r = VO_TRUE;
1418 } else if (!strcmp(what, "hue")) {
1419 dcc.dwFlags = DDCOLOR_HUE;
1420 dcc.lHue = value * 180 / 100;
1421 r = VO_TRUE;
1422 } else if (!strcmp(what, "saturation")) {
1423 dcc.dwFlags = DDCOLOR_SATURATION;
1424 dcc.lSaturation = (value + 100) * 20000 / 200;
1425 r = VO_TRUE;
1428 if (r == VO_TRUE) {
1429 g_cc->lpVtbl->SetColorControls(g_cc, &dcc);
1431 return r;
1434 //analoguous to color_ctrl_set
1435 static uint32_t color_ctrl_get(char *what, int *value)
1437 uint32_t r = VO_NOTIMPL;
1438 DDCOLORCONTROL dcc;
1439 if (!g_cc) {
1440 //printf("\n *** could not get color control interface!!!\n");
1441 return VO_NOTIMPL;
1443 ZeroMemory(&dcc, sizeof(dcc));
1444 dcc.dwSize = sizeof(dcc);
1446 if (g_cc->lpVtbl->GetColorControls(g_cc, &dcc) != DD_OK) {
1447 return r;
1450 if (!strcmp(what, "brightness") && (dcc.dwFlags & DDCOLOR_BRIGHTNESS)) {
1451 *value = dcc.lBrightness * 200 / 10000 - 100;
1452 r = VO_TRUE;
1453 } else if (!strcmp(what, "contrast") && (dcc.dwFlags & DDCOLOR_CONTRAST)) {
1454 *value = dcc.lContrast * 200 / 20000 - 100;
1455 r = VO_TRUE;
1456 } else if (!strcmp(what, "hue") && (dcc.dwFlags & DDCOLOR_HUE)) {
1457 *value = dcc.lHue * 100 / 180;
1458 r = VO_TRUE;
1459 } else if (!strcmp(what, "saturation") && (dcc.dwFlags & DDCOLOR_SATURATION)) {
1460 *value = dcc.lSaturation * 200 / 20000 - 100;
1461 r = VO_TRUE;
1463 // printf("\n*** %s = %d\n", what, *value);
1465 return r;
1468 static int control(uint32_t request, void *data)
1470 switch (request) {
1472 case VOCTRL_GET_IMAGE:
1473 return get_image(data);
1474 case VOCTRL_QUERY_FORMAT:
1475 last_rect.left = 0xDEADC0DE; // reset window position cache
1476 return query_format(*((uint32_t*)data));
1477 case VOCTRL_DRAW_IMAGE:
1478 return put_image(data);
1479 case VOCTRL_BORDER:
1480 if(WinID != -1) return VO_TRUE;
1481 if(vidmode)
1483 mp_msg(MSGT_VO, MSGL_ERR,"<vo_directx><ERROR>border has no meaning in exclusive mode\n");
1485 else
1487 if(vo_border) {
1488 vo_border = 0;
1489 SetWindowLong(hWnd, GWL_STYLE, WS_POPUP);
1490 } else {
1491 vo_border = 1;
1492 SetWindowLong(hWnd, GWL_STYLE, WNDSTYLE);
1494 // needed AFAICT to force the window to
1495 // redisplay with the new style. --Joey
1496 if (!vo_fs) {
1497 ShowWindow(hWnd,SW_HIDE);
1498 ShowWindow(hWnd,SW_SHOW);
1500 last_rect.left = 0xDEADC0DE; // reset window position cache
1501 Directx_ManageDisplay();
1503 return VO_TRUE;
1504 case VOCTRL_ONTOP:
1505 if(WinID != -1) return VO_TRUE;
1506 if(vidmode)
1508 mp_msg(MSGT_VO, MSGL_ERR,"<vo_directx><ERROR>ontop has no meaning in exclusive mode\n");
1510 else
1512 if(vo_ontop) vo_ontop = 0;
1513 else vo_ontop = 1;
1514 last_rect.left = 0xDEADC0DE; // reset window position cache
1515 Directx_ManageDisplay();
1517 return VO_TRUE;
1518 case VOCTRL_ROOTWIN:
1519 if(WinID != -1) return VO_TRUE;
1520 if(vidmode)
1522 mp_msg(MSGT_VO, MSGL_ERR,"<vo_directx><ERROR>rootwin has no meaning in exclusive mode\n");
1524 else
1526 if(vo_rootwin) vo_rootwin = 0;
1527 else vo_rootwin = 1;
1528 last_rect.left = 0xDEADC0DE; // reset window position cache
1529 Directx_ManageDisplay();
1531 return VO_TRUE;
1532 case VOCTRL_FULLSCREEN:
1534 if(vidmode)
1536 mp_msg(MSGT_VO, MSGL_ERR,"<vo_directx><ERROR>currently we do not allow to switch from exclusive to windowed mode\n");
1538 else
1540 if(!vo_fs)
1542 vo_fs=1;
1543 ShowWindow(hWndFS,SW_SHOW);
1544 ShowWindow(hWnd,SW_HIDE);
1545 SetForegroundWindow(hWndFS);
1547 else
1549 vo_fs=0;
1550 ShowWindow(hWndFS,SW_HIDE);
1551 ShowWindow(hWnd,SW_SHOW);
1553 last_rect.left = 0xDEADC0DE; // reset window position cache
1554 Directx_ManageDisplay();
1555 break;
1557 return VO_TRUE;
1559 case VOCTRL_SET_EQUALIZER: {
1560 struct voctrl_set_equalizer_args *args = data;
1561 return color_ctrl_set(args->name, args->value);
1563 case VOCTRL_GET_EQUALIZER: {
1564 struct voctrl_get_equalizer_args *args = data;
1565 return color_ctrl_get(args->name, args->valueptr);
1567 case VOCTRL_UPDATE_SCREENINFO:
1568 if (vidmode) {
1569 vo_screenwidth = vm_width;
1570 vo_screenheight = vm_height;
1571 } else {
1572 vo_screenwidth = monitor_rect.right - monitor_rect.left;
1573 vo_screenheight = monitor_rect.bottom - monitor_rect.top;
1575 aspect_save_screenres(vo_screenwidth, vo_screenheight);
1576 return VO_TRUE;
1577 case VOCTRL_RESET:
1578 last_rect.left = 0xDEADC0DE; // reset window position cache
1579 // fall-through intended
1581 return VO_NOTIMPL;