comctl32/listview: Don't forward HDN_ITEMCHANGING/HDN_ITEMCHANGED to listview parent.
[wine/multimedia.git] / dlls / wined3d / wined3d_main.c
blob2d60a6fde0bd0e49e4157490bae976e39fe8694a
1 /*
2 * Direct3D wine internal interface main
4 * Copyright 2002-2003 The wine-d3d team
5 * Copyright 2002-2003 Raphael Junqueira
6 * Copyright 2004 Jason Edmeades
7 * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
8 * Copyright 2009 Henri Verbeet for CodeWeavers
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #include "config.h"
27 #include "initguid.h"
28 #include "wined3d_private.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
32 struct wined3d_wndproc
34 HWND window;
35 BOOL unicode;
36 WNDPROC proc;
37 IWineD3DDeviceImpl *device;
40 struct wined3d_wndproc_table
42 struct wined3d_wndproc *entries;
43 unsigned int count;
44 unsigned int size;
47 static struct wined3d_wndproc_table wndproc_table;
49 int num_lock = 0;
50 void (CDECL *wine_tsx11_lock_ptr)(void) = NULL;
51 void (CDECL *wine_tsx11_unlock_ptr)(void) = NULL;
53 static CRITICAL_SECTION wined3d_cs;
54 static CRITICAL_SECTION_DEBUG wined3d_cs_debug =
56 0, 0, &wined3d_cs,
57 {&wined3d_cs_debug.ProcessLocksList,
58 &wined3d_cs_debug.ProcessLocksList},
59 0, 0, {(DWORD_PTR)(__FILE__ ": wined3d_cs")}
61 static CRITICAL_SECTION wined3d_cs = {&wined3d_cs_debug, -1, 0, 0, 0, 0};
63 /* When updating default value here, make sure to update winecfg as well,
64 * where appropriate. */
65 struct wined3d_settings wined3d_settings =
67 VS_HW, /* Hardware by default */
68 PS_HW, /* Hardware by default */
69 TRUE, /* Use of GLSL enabled by default */
70 ORM_FBO, /* Use FBOs to do offscreen rendering */
71 RTL_READTEX, /* Default render target locking method */
72 PCI_VENDOR_NONE,/* PCI Vendor ID */
73 PCI_DEVICE_NONE,/* PCI Device ID */
74 0, /* The default of memory is set in init_driver_info */
75 NULL, /* No wine logo by default */
76 FALSE, /* Disable multisampling for now due to Nvidia driver bugs which happens for some users */
77 FALSE, /* No strict draw ordering. */
80 /* Do not call while under the GL lock. */
81 struct wined3d * CDECL wined3d_create(UINT version, void *parent)
83 struct wined3d *object;
84 HRESULT hr;
86 object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
87 if (!object)
89 ERR("Failed to allocate wined3d object memory.\n");
90 return NULL;
93 hr = wined3d_init(object, version, parent);
94 if (FAILED(hr))
96 WARN("Failed to initialize wined3d object, hr %#x.\n", hr);
97 HeapFree(GetProcessHeap(), 0, object);
98 return NULL;
101 TRACE("Created wined3d object %p for d3d%d support.\n", object, version);
103 return object;
106 static DWORD get_config_key(HKEY defkey, HKEY appkey, const char *name, char *buffer, DWORD size)
108 if (appkey && !RegQueryValueExA(appkey, name, 0, NULL, (BYTE *)buffer, &size)) return 0;
109 if (defkey && !RegQueryValueExA(defkey, name, 0, NULL, (BYTE *)buffer, &size)) return 0;
110 return ERROR_FILE_NOT_FOUND;
113 static DWORD get_config_key_dword(HKEY defkey, HKEY appkey, const char *name, DWORD *data)
115 DWORD type;
116 DWORD size = sizeof(DWORD);
117 if (appkey && !RegQueryValueExA(appkey, name, 0, &type, (BYTE *)data, &size) && (type == REG_DWORD)) return 0;
118 if (defkey && !RegQueryValueExA(defkey, name, 0, &type, (BYTE *)data, &size) && (type == REG_DWORD)) return 0;
119 return ERROR_FILE_NOT_FOUND;
122 static void CDECL wined3d_do_nothing(void)
126 static BOOL wined3d_dll_init(HINSTANCE hInstDLL)
128 DWORD wined3d_context_tls_idx;
129 HMODULE mod;
130 char buffer[MAX_PATH+10];
131 DWORD size = sizeof(buffer);
132 HKEY hkey = 0;
133 HKEY appkey = 0;
134 DWORD len, tmpvalue;
135 WNDCLASSA wc;
137 wined3d_context_tls_idx = TlsAlloc();
138 if (wined3d_context_tls_idx == TLS_OUT_OF_INDEXES)
140 DWORD err = GetLastError();
141 ERR("Failed to allocate context TLS index, err %#x.\n", err);
142 return FALSE;
144 context_set_tls_idx(wined3d_context_tls_idx);
146 /* We need our own window class for a fake window which we use to retrieve GL capabilities */
147 /* We might need CS_OWNDC in the future if we notice strange things on Windows.
148 * Various articles/posts about OpenGL problems on Windows recommend this. */
149 wc.style = CS_HREDRAW | CS_VREDRAW;
150 wc.lpfnWndProc = DefWindowProcA;
151 wc.cbClsExtra = 0;
152 wc.cbWndExtra = 0;
153 wc.hInstance = hInstDLL;
154 wc.hIcon = LoadIconA(NULL, (LPCSTR)IDI_WINLOGO);
155 wc.hCursor = LoadCursorA(NULL, (LPCSTR)IDC_ARROW);
156 wc.hbrBackground = NULL;
157 wc.lpszMenuName = NULL;
158 wc.lpszClassName = WINED3D_OPENGL_WINDOW_CLASS_NAME;
160 if (!RegisterClassA(&wc))
162 ERR("Failed to register window class 'WineD3D_OpenGL'!\n");
163 if (!TlsFree(wined3d_context_tls_idx))
165 DWORD err = GetLastError();
166 ERR("Failed to free context TLS index, err %#x.\n", err);
168 return FALSE;
171 DisableThreadLibraryCalls(hInstDLL);
173 mod = GetModuleHandleA( "winex11.drv" );
174 if (mod)
176 wine_tsx11_lock_ptr = (void *)GetProcAddress( mod, "wine_tsx11_lock" );
177 wine_tsx11_unlock_ptr = (void *)GetProcAddress( mod, "wine_tsx11_unlock" );
179 else /* We are most likely on Windows */
181 wine_tsx11_lock_ptr = wined3d_do_nothing;
182 wine_tsx11_unlock_ptr = wined3d_do_nothing;
184 /* @@ Wine registry key: HKCU\Software\Wine\Direct3D */
185 if ( RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Direct3D", &hkey ) ) hkey = 0;
187 len = GetModuleFileNameA( 0, buffer, MAX_PATH );
188 if (len && len < MAX_PATH)
190 HKEY tmpkey;
191 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\Direct3D */
192 if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmpkey ))
194 char *p, *appname = buffer;
195 if ((p = strrchr( appname, '/' ))) appname = p + 1;
196 if ((p = strrchr( appname, '\\' ))) appname = p + 1;
197 strcat( appname, "\\Direct3D" );
198 TRACE("appname = [%s]\n", appname);
199 if (RegOpenKeyA( tmpkey, appname, &appkey )) appkey = 0;
200 RegCloseKey( tmpkey );
204 if (hkey || appkey)
206 if ( !get_config_key( hkey, appkey, "VertexShaderMode", buffer, size) )
208 if (!strcmp(buffer,"none"))
210 TRACE("Disable vertex shaders\n");
211 wined3d_settings.vs_mode = VS_NONE;
214 if ( !get_config_key( hkey, appkey, "PixelShaderMode", buffer, size) )
216 if (!strcmp(buffer,"enabled"))
218 TRACE("Allow pixel shaders\n");
219 wined3d_settings.ps_mode = PS_HW;
221 if (!strcmp(buffer,"disabled"))
223 TRACE("Disable pixel shaders\n");
224 wined3d_settings.ps_mode = PS_NONE;
227 if ( !get_config_key( hkey, appkey, "UseGLSL", buffer, size) )
229 if (!strcmp(buffer,"disabled"))
231 TRACE("Use of GL Shading Language disabled\n");
232 wined3d_settings.glslRequested = FALSE;
235 if ( !get_config_key( hkey, appkey, "OffscreenRenderingMode", buffer, size) )
237 if (!strcmp(buffer,"backbuffer"))
239 TRACE("Using the backbuffer for offscreen rendering\n");
240 wined3d_settings.offscreen_rendering_mode = ORM_BACKBUFFER;
242 else if (!strcmp(buffer,"fbo"))
244 TRACE("Using FBOs for offscreen rendering\n");
245 wined3d_settings.offscreen_rendering_mode = ORM_FBO;
248 if ( !get_config_key( hkey, appkey, "RenderTargetLockMode", buffer, size) )
250 if (!strcmp(buffer,"disabled"))
252 TRACE("Disabling render target locking\n");
253 wined3d_settings.rendertargetlock_mode = RTL_DISABLE;
255 else if (!strcmp(buffer,"readdraw"))
257 TRACE("Using glReadPixels for render target reading and glDrawPixels for writing\n");
258 wined3d_settings.rendertargetlock_mode = RTL_READDRAW;
260 else if (!strcmp(buffer,"readtex"))
262 TRACE("Using glReadPixels for render target reading and textures for writing\n");
263 wined3d_settings.rendertargetlock_mode = RTL_READTEX;
266 if ( !get_config_key_dword( hkey, appkey, "VideoPciDeviceID", &tmpvalue) )
268 int pci_device_id = tmpvalue;
270 /* A pci device id is 16-bit */
271 if(pci_device_id > 0xffff)
273 ERR("Invalid value for VideoPciDeviceID. The value should be smaller or equal to 65535 or 0xffff\n");
275 else
277 TRACE("Using PCI Device ID %04x\n", pci_device_id);
278 wined3d_settings.pci_device_id = pci_device_id;
281 if ( !get_config_key_dword( hkey, appkey, "VideoPciVendorID", &tmpvalue) )
283 int pci_vendor_id = tmpvalue;
285 /* A pci device id is 16-bit */
286 if(pci_vendor_id > 0xffff)
288 ERR("Invalid value for VideoPciVendorID. The value should be smaller or equal to 65535 or 0xffff\n");
290 else
292 TRACE("Using PCI Vendor ID %04x\n", pci_vendor_id);
293 wined3d_settings.pci_vendor_id = pci_vendor_id;
296 if ( !get_config_key( hkey, appkey, "VideoMemorySize", buffer, size) )
298 int TmpVideoMemorySize = atoi(buffer);
299 if(TmpVideoMemorySize > 0)
301 wined3d_settings.emulated_textureram = TmpVideoMemorySize *1024*1024;
302 TRACE("Use %iMB = %d byte for emulated_textureram\n",
303 TmpVideoMemorySize,
304 wined3d_settings.emulated_textureram);
306 else
307 ERR("VideoMemorySize is %i but must be >0\n", TmpVideoMemorySize);
309 if ( !get_config_key( hkey, appkey, "WineLogo", buffer, size) )
311 size_t len = strlen(buffer) + 1;
313 wined3d_settings.logo = HeapAlloc(GetProcessHeap(), 0, len);
314 if (!wined3d_settings.logo) ERR("Failed to allocate logo path memory.\n");
315 else memcpy(wined3d_settings.logo, buffer, len);
317 if ( !get_config_key( hkey, appkey, "Multisampling", buffer, size) )
319 if (!strcmp(buffer,"enabled"))
321 TRACE("Allow multisampling\n");
322 wined3d_settings.allow_multisampling = TRUE;
325 if (!get_config_key(hkey, appkey, "StrictDrawOrdering", buffer, size)
326 && !strcmp(buffer,"enabled"))
328 TRACE("Enforcing strict draw ordering.\n");
329 wined3d_settings.strict_draw_ordering = TRUE;
332 if (wined3d_settings.vs_mode == VS_HW)
333 TRACE("Allow HW vertex shaders\n");
334 if (wined3d_settings.ps_mode == PS_NONE)
335 TRACE("Disable pixel shaders\n");
336 if (wined3d_settings.glslRequested)
337 TRACE("If supported by your system, GL Shading Language will be used\n");
339 if (appkey) RegCloseKey( appkey );
340 if (hkey) RegCloseKey( hkey );
342 return TRUE;
345 static BOOL wined3d_dll_destroy(HINSTANCE hInstDLL)
347 DWORD wined3d_context_tls_idx = context_get_tls_idx();
348 unsigned int i;
350 if (!TlsFree(wined3d_context_tls_idx))
352 DWORD err = GetLastError();
353 ERR("Failed to free context TLS index, err %#x.\n", err);
356 for (i = 0; i < wndproc_table.count; ++i)
358 /* Trying to unregister these would be futile. These entries can only
359 * exist if either we skipped them in wined3d_unregister_window() due
360 * to the application replacing the wndproc after the entry was
361 * registered, or if the application still has an active wined3d
362 * device. In the latter case the application has bigger problems than
363 * these entries. */
364 WARN("Leftover wndproc table entry %p.\n", &wndproc_table.entries[i]);
366 HeapFree(GetProcessHeap(), 0, wndproc_table.entries);
368 HeapFree(GetProcessHeap(), 0, wined3d_settings.logo);
369 UnregisterClassA(WINED3D_OPENGL_WINDOW_CLASS_NAME, hInstDLL);
371 return TRUE;
374 void WINAPI wined3d_mutex_lock(void)
376 EnterCriticalSection(&wined3d_cs);
379 void WINAPI wined3d_mutex_unlock(void)
381 LeaveCriticalSection(&wined3d_cs);
384 static struct wined3d_wndproc *wined3d_find_wndproc(HWND window)
386 unsigned int i;
388 for (i = 0; i < wndproc_table.count; ++i)
390 if (wndproc_table.entries[i].window == window)
392 return &wndproc_table.entries[i];
396 return NULL;
399 static LRESULT CALLBACK wined3d_wndproc(HWND window, UINT message, WPARAM wparam, LPARAM lparam)
401 struct wined3d_wndproc *entry;
402 IWineD3DDeviceImpl *device;
403 BOOL unicode;
404 WNDPROC proc;
406 wined3d_mutex_lock();
407 entry = wined3d_find_wndproc(window);
409 if (!entry)
411 wined3d_mutex_unlock();
412 ERR("Window %p is not registered with wined3d.\n", window);
413 return DefWindowProcW(window, message, wparam, lparam);
416 device = entry->device;
417 unicode = entry->unicode;
418 proc = entry->proc;
419 wined3d_mutex_unlock();
421 return device_process_message(device, window, unicode, message, wparam, lparam, proc);
424 BOOL wined3d_register_window(HWND window, IWineD3DDeviceImpl *device)
426 struct wined3d_wndproc *entry;
428 wined3d_mutex_lock();
430 if (wined3d_find_wndproc(window))
432 wined3d_mutex_unlock();
433 WARN("Window %p is already registered with wined3d.\n", window);
434 return TRUE;
437 if (wndproc_table.size == wndproc_table.count)
439 unsigned int new_size = max(1, wndproc_table.size * 2);
440 struct wined3d_wndproc *new_entries;
442 if (!wndproc_table.entries) new_entries = HeapAlloc(GetProcessHeap(), 0, new_size * sizeof(*new_entries));
443 else new_entries = HeapReAlloc(GetProcessHeap(), 0, wndproc_table.entries, new_size * sizeof(*new_entries));
445 if (!new_entries)
447 wined3d_mutex_unlock();
448 ERR("Failed to grow table.\n");
449 return FALSE;
452 wndproc_table.entries = new_entries;
453 wndproc_table.size = new_size;
456 entry = &wndproc_table.entries[wndproc_table.count++];
457 entry->window = window;
458 entry->unicode = IsWindowUnicode(window);
459 /* Set a window proc that matches the window. Some applications (e.g. NoX)
460 * replace the window proc after we've set ours, and expect to be able to
461 * call the previous one (ours) directly, without using CallWindowProc(). */
462 if (entry->unicode)
463 entry->proc = (WNDPROC)SetWindowLongPtrW(window, GWLP_WNDPROC, (LONG_PTR)wined3d_wndproc);
464 else
465 entry->proc = (WNDPROC)SetWindowLongPtrA(window, GWLP_WNDPROC, (LONG_PTR)wined3d_wndproc);
466 entry->device = device;
468 wined3d_mutex_unlock();
470 return TRUE;
473 void wined3d_unregister_window(HWND window)
475 struct wined3d_wndproc *entry, *last;
476 LONG_PTR proc;
478 wined3d_mutex_lock();
480 if (!(entry = wined3d_find_wndproc(window)))
482 wined3d_mutex_unlock();
483 ERR("Window %p is not registered with wined3d.\n", window);
484 return;
487 if (entry->unicode)
489 proc = GetWindowLongPtrW(window, GWLP_WNDPROC);
490 if (proc != (LONG_PTR)wined3d_wndproc)
492 wined3d_mutex_unlock();
493 WARN("Not unregistering window %p, window proc %#lx doesn't match wined3d window proc %p.\n",
494 window, proc, wined3d_wndproc);
495 return;
498 SetWindowLongPtrW(window, GWLP_WNDPROC, (LONG_PTR)entry->proc);
500 else
502 proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
503 if (proc != (LONG_PTR)wined3d_wndproc)
505 wined3d_mutex_unlock();
506 WARN("Not unregistering window %p, window proc %#lx doesn't match wined3d window proc %p.\n",
507 window, proc, wined3d_wndproc);
508 return;
511 SetWindowLongPtrA(window, GWLP_WNDPROC, (LONG_PTR)entry->proc);
514 last = &wndproc_table.entries[--wndproc_table.count];
515 if (entry != last) *entry = *last;
517 wined3d_mutex_unlock();
520 /* At process attach */
521 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
523 TRACE("WineD3D DLLMain Reason=%u\n", fdwReason);
525 switch (fdwReason)
527 case DLL_PROCESS_ATTACH:
528 return wined3d_dll_init(hInstDLL);
530 case DLL_PROCESS_DETACH:
531 return wined3d_dll_destroy(hInstDLL);
533 case DLL_THREAD_DETACH:
535 if (!context_set_current(NULL))
537 ERR("Failed to clear current context.\n");
539 return TRUE;
542 default:
543 return TRUE;