wined3d: Don't override the default renderer in wined3d_dll_init().
[wine.git] / dlls / wined3d / wined3d_main.c
blob1368b4fc90545ffdc63fdaa6b227cb5bc031d320
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 #define VKD3D_NO_VULKAN_H
26 #define VKD3D_NO_WIN32_TYPES
27 #include "initguid.h"
28 #include "wined3d_private.h"
29 #include "wined3d_gl.h"
30 #include "d3d12.h"
31 #define VK_NO_PROTOTYPES
32 #include "wine/vulkan.h"
33 #include <vkd3d.h>
35 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
36 WINE_DECLARE_DEBUG_CHANNEL(vkd3d);
37 WINE_DECLARE_DEBUG_CHANNEL(winediag);
39 struct wined3d_wndproc
41 struct wined3d *wined3d;
42 HWND window;
43 BOOL unicode;
44 BOOL filter;
45 WNDPROC proc;
46 struct wined3d_device *device;
47 uint32_t flags;
50 struct wined3d_wndproc_table
52 struct wined3d_wndproc *entries;
53 SIZE_T count;
54 SIZE_T size;
57 struct wined3d_window_hook
59 HHOOK hook;
60 DWORD thread_id;
61 unsigned int count;
64 struct wined3d_registered_swapchain_state
66 struct wined3d_swapchain_state *state;
67 DWORD thread_id;
70 struct wined3d_swapchain_state_table
72 struct wined3d_window_hook *hooks;
73 SIZE_T hooks_size;
74 SIZE_T hook_count;
76 struct wined3d_registered_swapchain_state *states;
77 SIZE_T states_size;
78 SIZE_T state_count;
81 static struct wined3d_wndproc_table wndproc_table;
82 static struct wined3d_swapchain_state_table swapchain_state_table;
84 static CRITICAL_SECTION wined3d_cs;
85 static CRITICAL_SECTION_DEBUG wined3d_cs_debug =
87 0, 0, &wined3d_cs,
88 {&wined3d_cs_debug.ProcessLocksList,
89 &wined3d_cs_debug.ProcessLocksList},
90 0, 0, {(DWORD_PTR)(__FILE__ ": wined3d_cs")}
92 static CRITICAL_SECTION wined3d_cs = {&wined3d_cs_debug, -1, 0, 0, 0, 0};
94 static CRITICAL_SECTION wined3d_wndproc_cs;
95 static CRITICAL_SECTION_DEBUG wined3d_wndproc_cs_debug =
97 0, 0, &wined3d_wndproc_cs,
98 {&wined3d_wndproc_cs_debug.ProcessLocksList,
99 &wined3d_wndproc_cs_debug.ProcessLocksList},
100 0, 0, {(DWORD_PTR)(__FILE__ ": wined3d_wndproc_cs")}
102 static CRITICAL_SECTION wined3d_wndproc_cs = {&wined3d_wndproc_cs_debug, -1, 0, 0, 0, 0};
104 CRITICAL_SECTION wined3d_command_cs;
105 static CRITICAL_SECTION_DEBUG wined3d_command_cs_debug =
107 0, 0, &wined3d_command_cs,
108 {&wined3d_command_cs_debug.ProcessLocksList,
109 &wined3d_command_cs_debug.ProcessLocksList},
110 0, 0, {(DWORD_PTR)(__FILE__ ": wined3d_command_cs")}
112 CRITICAL_SECTION wined3d_command_cs = {&wined3d_command_cs_debug, -1, 0, 0, 0, 0};
114 /* When updating default value here, make sure to update winecfg as well,
115 * where appropriate. */
116 struct wined3d_settings wined3d_settings =
118 .cs_multithreaded = WINED3D_CSMT_ENABLE,
119 .max_gl_version = MAKEDWORD_VERSION(4, 4),
120 .offscreen_rendering_mode = ORM_FBO,
121 .pci_vendor_id = PCI_VENDOR_NONE,
122 .pci_device_id = PCI_DEVICE_NONE,
123 .multisample_textures = TRUE,
124 .sample_count = ~0u,
125 .max_sm_vs = UINT_MAX,
126 .max_sm_ps = UINT_MAX,
127 .max_sm_ds = UINT_MAX,
128 .max_sm_hs = UINT_MAX,
129 .max_sm_gs = UINT_MAX,
130 .max_sm_cs = UINT_MAX,
131 .renderer = WINED3D_RENDERER_AUTO,
132 .shader_backend = WINED3D_SHADER_BACKEND_AUTO,
135 enum wined3d_renderer CDECL wined3d_get_renderer(void)
137 if (wined3d_settings.renderer == WINED3D_RENDERER_AUTO)
138 return WINED3D_RENDERER_OPENGL;
140 return wined3d_settings.renderer;
143 struct wined3d * CDECL wined3d_create(uint32_t flags)
145 struct wined3d *object;
146 HRESULT hr;
148 if (!(object = heap_alloc_zero(FIELD_OFFSET(struct wined3d, adapters[1]))))
150 ERR("Failed to allocate wined3d object memory.\n");
151 return NULL;
154 if (wined3d_settings.renderer == WINED3D_RENDERER_NO3D)
155 flags |= WINED3D_NO3D;
157 if (FAILED(hr = wined3d_init(object, flags)))
159 WARN("Failed to initialize wined3d object, hr %#lx.\n", hr);
160 heap_free(object);
161 return NULL;
164 TRACE("Created wined3d object %p.\n", object);
166 return object;
169 static bool is_option_separator(char c)
171 return c == ',' || c == ';' || c == '\0';
174 static const char *config_list_get_value(const char *string, const char *key, size_t *len)
176 const char *p, *end;
177 char prev_char;
179 p = string;
180 while (p)
182 if ((p = strstr(p, key)))
184 prev_char = p > string ? p[-1] : 0;
185 p += strlen(key);
187 if (is_option_separator(prev_char) && *p == '=')
189 if ((end = strpbrk(p + 1, ",;")))
190 *len = end - (p + 1);
191 else
192 *len = strlen(p + 1);
193 return p + 1;
198 return NULL;
201 static DWORD get_config_key(HKEY defkey, HKEY appkey, const char *env, const char *name, char *buffer, DWORD size)
203 const char *env_value;
204 size_t env_len;
206 if ((env_value = config_list_get_value(env, name, &env_len)) && env_len < size)
208 memcpy(buffer, env_value, env_len);
209 buffer[env_len] = 0;
210 return 0;
212 if (appkey && !RegQueryValueExA(appkey, name, 0, NULL, (BYTE *)buffer, &size)) return 0;
213 if (defkey && !RegQueryValueExA(defkey, name, 0, NULL, (BYTE *)buffer, &size)) return 0;
214 return ERROR_FILE_NOT_FOUND;
217 static DWORD get_config_key_dword(HKEY defkey, HKEY appkey, const char *env, const char *name, unsigned int *value)
219 DWORD type, data, size;
220 const char *env_value;
221 size_t env_len;
222 char *end;
224 if ((env_value = config_list_get_value(env, name, &env_len)))
226 *value = strtoul(env_value, &end, 0);
227 if (end != env_value)
228 return 0;
230 size = sizeof(data);
231 if (appkey && !RegQueryValueExA(appkey, name, 0, &type, (BYTE *)&data, &size) && type == REG_DWORD) goto success;
232 size = sizeof(data);
233 if (defkey && !RegQueryValueExA(defkey, name, 0, &type, (BYTE *)&data, &size) && type == REG_DWORD) goto success;
235 return ERROR_FILE_NOT_FOUND;
237 success:
238 *value = data;
239 return 0;
242 BOOL wined3d_get_app_name(char *app_name, unsigned int app_name_size)
244 char buffer[MAX_PATH];
245 unsigned int len;
246 char *p, *name;
248 len = GetModuleFileNameA(0, buffer, ARRAY_SIZE(buffer));
249 if (!(len && len < MAX_PATH))
250 return FALSE;
252 name = buffer;
253 if ((p = strrchr(name, '/' )))
254 name = p + 1;
255 if ((p = strrchr(name, '\\')))
256 name = p + 1;
258 len = strlen(name) + 1;
259 if (app_name_size < len)
260 return FALSE;
262 memcpy(app_name, name, len);
263 return TRUE;
266 static void vkd3d_log_callback(const char *fmt, va_list args)
268 char buffer[1024];
270 vsnprintf(buffer, sizeof(buffer), fmt, args);
271 __wine_dbg_output(buffer);
274 static BOOL wined3d_dll_init(HINSTANCE hInstDLL)
276 DWORD wined3d_context_tls_idx;
277 char buffer[MAX_PATH+10];
278 DWORD size = sizeof(buffer);
279 const char *env;
280 HKEY hkey = 0;
281 HKEY appkey = 0;
282 unsigned int tmpvalue;
283 WNDCLASSA wc;
285 wined3d_context_tls_idx = TlsAlloc();
286 if (wined3d_context_tls_idx == TLS_OUT_OF_INDEXES)
288 unsigned int err = GetLastError();
289 ERR("Failed to allocate context TLS index, err %#x.\n", err);
290 return FALSE;
292 context_set_tls_idx(wined3d_context_tls_idx);
294 /* We need our own window class for a fake window which we use to retrieve GL capabilities */
295 /* We might need CS_OWNDC in the future if we notice strange things on Windows.
296 * Various articles/posts about OpenGL problems on Windows recommend this. */
297 wc.style = CS_HREDRAW | CS_VREDRAW;
298 wc.lpfnWndProc = DefWindowProcA;
299 wc.cbClsExtra = 0;
300 wc.cbWndExtra = 0;
301 wc.hInstance = hInstDLL;
302 wc.hIcon = LoadIconA(NULL, (const char *)IDI_WINLOGO);
303 wc.hCursor = LoadCursorA(NULL, (const char *)IDC_ARROW);
304 wc.hbrBackground = NULL;
305 wc.lpszMenuName = NULL;
306 wc.lpszClassName = WINED3D_OPENGL_WINDOW_CLASS_NAME;
308 if (!RegisterClassA(&wc))
310 ERR("Failed to register window class 'WineD3D_OpenGL'!\n");
311 if (!TlsFree(wined3d_context_tls_idx))
313 unsigned int err = GetLastError();
314 ERR("Failed to free context TLS index, err %#x.\n", err);
316 return FALSE;
319 DisableThreadLibraryCalls(hInstDLL);
321 /* @@ Wine registry key: HKCU\Software\Wine\Direct3D */
322 if ( RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Direct3D", &hkey ) ) hkey = 0;
324 if (wined3d_get_app_name(buffer, ARRAY_SIZE(buffer)))
326 HKEY tmpkey;
327 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\Direct3D */
328 if (!RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmpkey))
330 strcat(buffer, "\\Direct3D");
331 TRACE("Application name %s.\n", buffer);
332 if (RegOpenKeyA(tmpkey, buffer, &appkey)) appkey = 0;
333 RegCloseKey(tmpkey);
337 /* Allow modifying settings using the WINE_D3D_CONFIG environment variable,
338 * which takes precedence over registry keys. An example is as follows:
340 * WINE_D3D_CONFIG=csmt=0x1,shader_backend=glsl
342 env = getenv("WINE_D3D_CONFIG");
344 if (hkey || appkey || env)
346 if (!get_config_key_dword(hkey, appkey, env, "csmt", &wined3d_settings.cs_multithreaded))
347 ERR_(winediag)("Setting multithreaded command stream to %#x.\n", wined3d_settings.cs_multithreaded);
348 if (!get_config_key_dword(hkey, appkey, env, "MaxVersionGL", &tmpvalue))
350 ERR_(winediag)("Setting maximum allowed wined3d GL version to %u.%u.\n",
351 tmpvalue >> 16, tmpvalue & 0xffff);
352 wined3d_settings.max_gl_version = tmpvalue;
354 if (!get_config_key(hkey, appkey, env, "shader_backend", buffer, size))
356 if (!stricmp(buffer, "glsl"))
358 ERR_(winediag)("Using the GLSL shader backend.\n");
359 wined3d_settings.shader_backend = WINED3D_SHADER_BACKEND_GLSL;
361 else if (!stricmp(buffer, "arb"))
363 ERR_(winediag)("Using the ARB shader backend.\n");
364 wined3d_settings.shader_backend = WINED3D_SHADER_BACKEND_ARB;
366 else if (!stricmp(buffer, "none"))
368 ERR_(winediag)("Disabling shader backends.\n");
369 wined3d_settings.shader_backend = WINED3D_SHADER_BACKEND_NONE;
372 if (wined3d_settings.shader_backend == WINED3D_SHADER_BACKEND_ARB
373 || wined3d_settings.shader_backend == WINED3D_SHADER_BACKEND_NONE)
375 ERR_(winediag)("The GLSL shader backend has been disabled. You get to keep all the pieces if it breaks.\n");
376 TRACE("Use of GL Shading Language disabled.\n");
378 if (!get_config_key(hkey, appkey, env, "OffscreenRenderingMode", buffer, size)
379 && !strcmp(buffer,"backbuffer"))
380 wined3d_settings.offscreen_rendering_mode = ORM_BACKBUFFER;
381 if (!get_config_key_dword(hkey, appkey, env, "VideoPciDeviceID", &tmpvalue))
383 int pci_device_id = tmpvalue;
385 /* A pci device id is 16-bit */
386 if(pci_device_id > 0xffff)
388 ERR("Invalid value for VideoPciDeviceID. The value should be smaller or equal to 65535 or 0xffff\n");
390 else
392 TRACE("Using PCI Device ID %04x\n", pci_device_id);
393 wined3d_settings.pci_device_id = pci_device_id;
396 if (!get_config_key_dword(hkey, appkey, env, "VideoPciVendorID", &tmpvalue))
398 int pci_vendor_id = tmpvalue;
400 /* A pci device id is 16-bit */
401 if(pci_vendor_id > 0xffff)
403 ERR("Invalid value for VideoPciVendorID. The value should be smaller or equal to 65535 or 0xffff\n");
405 else
407 TRACE("Using PCI Vendor ID %04x\n", pci_vendor_id);
408 wined3d_settings.pci_vendor_id = pci_vendor_id;
411 if (!get_config_key(hkey, appkey, env, "VideoMemorySize", buffer, size))
413 int TmpVideoMemorySize = atoi(buffer);
414 if(TmpVideoMemorySize > 0)
416 wined3d_settings.emulated_textureram = (UINT64)TmpVideoMemorySize *1024*1024;
417 TRACE("Use %iMiB = 0x%s bytes for emulated_textureram\n",
418 TmpVideoMemorySize,
419 wine_dbgstr_longlong(wined3d_settings.emulated_textureram));
421 else
422 ERR("VideoMemorySize is %i but must be >0\n", TmpVideoMemorySize);
424 if (!get_config_key(hkey, appkey, env, "WineLogo", buffer, size))
426 size_t len = strlen(buffer) + 1;
428 if (!(wined3d_settings.logo = heap_alloc(len)))
429 ERR("Failed to allocate logo path memory.\n");
430 else
431 memcpy(wined3d_settings.logo, buffer, len);
433 if (!get_config_key_dword(hkey, appkey, env, "MultisampleTextures", &wined3d_settings.multisample_textures))
434 ERR_(winediag)("Setting multisample textures to %#x.\n", wined3d_settings.multisample_textures);
435 if (!get_config_key_dword(hkey, appkey, env, "SampleCount", &wined3d_settings.sample_count))
436 ERR_(winediag)("Forcing sample count to %u. This may not be compatible with all applications.\n",
437 wined3d_settings.sample_count);
438 if (!get_config_key(hkey, appkey, env, "CheckFloatConstants", buffer, size)
439 && !strcmp(buffer, "enabled"))
441 TRACE("Checking relative addressing indices in float constants.\n");
442 wined3d_settings.check_float_constants = TRUE;
444 if (!get_config_key_dword(hkey, appkey, env, "strict_shader_math", &wined3d_settings.strict_shader_math))
445 ERR_(winediag)("Setting strict shader math to %#x.\n", wined3d_settings.strict_shader_math);
446 if (!get_config_key_dword(hkey, appkey, env, "MaxShaderModelVS", &wined3d_settings.max_sm_vs))
447 TRACE("Limiting VS shader model to %u.\n", wined3d_settings.max_sm_vs);
448 if (!get_config_key_dword(hkey, appkey, env, "MaxShaderModelHS", &wined3d_settings.max_sm_hs))
449 TRACE("Limiting HS shader model to %u.\n", wined3d_settings.max_sm_hs);
450 if (!get_config_key_dword(hkey, appkey, env, "MaxShaderModelDS", &wined3d_settings.max_sm_ds))
451 TRACE("Limiting DS shader model to %u.\n", wined3d_settings.max_sm_ds);
452 if (!get_config_key_dword(hkey, appkey, env, "MaxShaderModelGS", &wined3d_settings.max_sm_gs))
453 TRACE("Limiting GS shader model to %u.\n", wined3d_settings.max_sm_gs);
454 if (!get_config_key_dword(hkey, appkey, env, "MaxShaderModelPS", &wined3d_settings.max_sm_ps))
455 TRACE("Limiting PS shader model to %u.\n", wined3d_settings.max_sm_ps);
456 if (!get_config_key_dword(hkey, appkey, env, "MaxShaderModelCS", &wined3d_settings.max_sm_cs))
457 TRACE("Limiting CS shader model to %u.\n", wined3d_settings.max_sm_cs);
458 if (!get_config_key(hkey, appkey, env, "renderer", buffer, size))
460 if (!strcmp(buffer, "vulkan"))
462 ERR_(winediag)("Using the Vulkan renderer.\n");
463 wined3d_settings.renderer = WINED3D_RENDERER_VULKAN;
465 else if (!strcmp(buffer, "gl"))
467 ERR_(winediag)("Using the OpenGL renderer.\n");
468 wined3d_settings.renderer = WINED3D_RENDERER_OPENGL;
470 else if (!strcmp(buffer, "gdi") || !strcmp(buffer, "no3d"))
472 ERR_(winediag)("Disabling 3D support.\n");
473 wined3d_settings.renderer = WINED3D_RENDERER_NO3D;
476 if (!get_config_key_dword(hkey, appkey, env, "cb_access_map_w", &tmpvalue) && tmpvalue)
478 TRACE("Forcing all constant buffers to be write-mappable.\n");
479 wined3d_settings.cb_access_map_w = TRUE;
483 if (appkey) RegCloseKey( appkey );
484 if (hkey) RegCloseKey( hkey );
486 if (!getenv( "VKD3D_DEBUG" ))
488 if (TRACE_ON(vkd3d)) putenv( "VKD3D_DEBUG=trace" );
489 else if (WARN_ON(vkd3d)) putenv( "VKD3D_DEBUG=warn" );
490 else if (FIXME_ON(vkd3d)) putenv( "VKD3D_DEBUG=fixme" );
491 else if (ERR_ON(vkd3d)) putenv( "VKD3D_DEBUG=err" );
492 else putenv( "VKD3D_DEBUG=none" );
494 if (!getenv( "VKD3D_SHADER_DEBUG" ))
496 if (TRACE_ON(vkd3d)) putenv( "VKD3D_SHADER_DEBUG=trace" );
497 else if (WARN_ON(vkd3d)) putenv( "VKD3D_SHADER_DEBUG=warn" );
498 else if (FIXME_ON(vkd3d)) putenv( "VKD3D_SHADER_DEBUG=fixme" );
499 else if (ERR_ON(vkd3d)) putenv( "VKD3D_SHADER_DEBUG=err" );
500 else putenv( "VKD3D_SHADER_DEBUG=none" );
503 vkd3d_set_log_callback(vkd3d_log_callback);
505 return TRUE;
508 static BOOL wined3d_dll_destroy(HINSTANCE hInstDLL)
510 DWORD wined3d_context_tls_idx = context_get_tls_idx();
511 unsigned int i;
513 if (!TlsFree(wined3d_context_tls_idx))
515 unsigned int err = GetLastError();
516 ERR("Failed to free context TLS index, err %#x.\n", err);
519 for (i = 0; i < wndproc_table.count; ++i)
521 /* Trying to unregister these would be futile. These entries can only
522 * exist if either we skipped them in wined3d_unregister_window() due
523 * to the application replacing the wndproc after the entry was
524 * registered, or if the application still has an active wined3d
525 * device. In the latter case the application has bigger problems than
526 * these entries. */
527 WARN("Leftover wndproc table entry %p.\n", &wndproc_table.entries[i]);
529 heap_free(wndproc_table.entries);
531 heap_free(swapchain_state_table.states);
532 for (i = 0; i < swapchain_state_table.hook_count; ++i)
534 WARN("Leftover swapchain state hook %p.\n", &swapchain_state_table.hooks[i]);
535 UnhookWindowsHookEx(swapchain_state_table.hooks[i].hook);
537 heap_free(swapchain_state_table.hooks);
539 heap_free(wined3d_settings.logo);
540 UnregisterClassA(WINED3D_OPENGL_WINDOW_CLASS_NAME, hInstDLL);
542 DeleteCriticalSection(&wined3d_command_cs);
544 DeleteCriticalSection(&wined3d_wndproc_cs);
545 DeleteCriticalSection(&wined3d_cs);
546 return TRUE;
549 void WINAPI wined3d_mutex_lock(void)
551 EnterCriticalSection(&wined3d_cs);
554 void WINAPI wined3d_mutex_unlock(void)
556 LeaveCriticalSection(&wined3d_cs);
559 static void wined3d_wndproc_mutex_lock(void)
561 EnterCriticalSection(&wined3d_wndproc_cs);
564 static void wined3d_wndproc_mutex_unlock(void)
566 LeaveCriticalSection(&wined3d_wndproc_cs);
569 static struct wined3d_output * wined3d_get_output_from_window(const struct wined3d *wined3d,
570 HWND hwnd)
572 unsigned int adapter_idx, output_idx;
573 struct wined3d_adapter *adapter;
574 MONITORINFOEXW monitor_info;
575 HMONITOR monitor;
577 TRACE("wined3d %p, hwnd %p.\n", wined3d, hwnd);
579 monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
580 monitor_info.cbSize = sizeof(monitor_info);
581 if (!GetMonitorInfoW(monitor, (MONITORINFO *)&monitor_info))
583 ERR("GetMonitorInfoW failed, error %#lx.\n", GetLastError());
584 return NULL;
587 for (adapter_idx = 0; adapter_idx < wined3d->adapter_count; ++adapter_idx)
589 adapter = wined3d->adapters[adapter_idx];
590 for (output_idx = 0; output_idx < adapter->output_count; ++output_idx)
592 if (!lstrcmpiW(adapter->outputs[output_idx].device_name, monitor_info.szDevice))
593 return &adapter->outputs[output_idx];
597 return NULL;
600 static struct wined3d_wndproc *wined3d_find_wndproc(HWND window, struct wined3d *wined3d)
602 unsigned int i;
604 for (i = 0; i < wndproc_table.count; ++i)
606 struct wined3d_wndproc *entry = &wndproc_table.entries[i];
608 if (entry->window == window && entry->wined3d == wined3d)
609 return entry;
612 return NULL;
615 BOOL wined3d_filter_messages(HWND window, BOOL filter)
617 struct wined3d_wndproc *entry;
618 BOOL ret;
620 wined3d_wndproc_mutex_lock();
622 if (!(entry = wined3d_find_wndproc(window, NULL)))
624 wined3d_wndproc_mutex_unlock();
625 return FALSE;
628 ret = entry->filter;
629 entry->filter = filter;
631 wined3d_wndproc_mutex_unlock();
633 return ret;
636 static LRESULT CALLBACK wined3d_wndproc(HWND window, UINT message, WPARAM wparam, LPARAM lparam)
638 struct wined3d_wndproc *entry;
639 struct wined3d_device *device;
640 BOOL unicode, filter;
641 WNDPROC proc;
643 wined3d_wndproc_mutex_lock();
645 if (!(entry = wined3d_find_wndproc(window, NULL)))
647 wined3d_wndproc_mutex_unlock();
648 ERR("Window %p is not registered with wined3d.\n", window);
649 return DefWindowProcW(window, message, wparam, lparam);
652 device = entry->device;
653 unicode = entry->unicode;
654 filter = entry->filter;
655 proc = entry->proc;
656 wined3d_wndproc_mutex_unlock();
658 if (device)
660 if (filter && message != WM_DISPLAYCHANGE)
662 TRACE("Filtering message: window %p, message %#x, wparam %#Ix, lparam %#Ix.\n",
663 window, message, wparam, lparam);
665 if (unicode)
666 return DefWindowProcW(window, message, wparam, lparam);
667 return DefWindowProcA(window, message, wparam, lparam);
670 return device_process_message(device, window, unicode, message, wparam, lparam, proc);
672 if (unicode)
673 return CallWindowProcW(proc, window, message, wparam, lparam);
674 return CallWindowProcA(proc, window, message, wparam, lparam);
677 static LRESULT CALLBACK wined3d_hook_proc(int code, WPARAM wparam, LPARAM lparam)
679 struct wined3d_swapchain_desc swapchain_desc;
680 struct wined3d_swapchain_state *state;
681 struct wined3d_wndproc *entry;
682 struct wined3d_output *output;
683 MSG *msg = (MSG *)lparam;
684 unsigned int i;
686 /* Handle Alt+Enter. */
687 if (code == HC_ACTION && msg->message == WM_SYSKEYDOWN
688 && msg->wParam == VK_RETURN && (msg->lParam & (KF_ALTDOWN << 16)))
690 wined3d_wndproc_mutex_lock();
692 for (i = 0; i < swapchain_state_table.state_count; ++i)
694 state = swapchain_state_table.states[i].state;
696 if (state->device_window != msg->hwnd)
697 continue;
699 if ((entry = wined3d_find_wndproc(msg->hwnd, state->wined3d))
700 && (entry->flags & (WINED3D_REGISTER_WINDOW_NO_WINDOW_CHANGES
701 | WINED3D_REGISTER_WINDOW_NO_ALT_ENTER)))
702 continue;
704 swapchain_desc = state->desc;
705 swapchain_desc.windowed = !swapchain_desc.windowed;
706 if (!(output = wined3d_get_output_from_window(state->wined3d, state->device_window)))
708 ERR("Failed to get output from window %p.\n", state->device_window);
709 break;
711 swapchain_desc.output = output;
712 wined3d_swapchain_state_set_fullscreen(state, &swapchain_desc, NULL);
714 wined3d_wndproc_mutex_unlock();
716 return 1;
719 wined3d_wndproc_mutex_unlock();
722 return CallNextHookEx(0, code, wparam, lparam);
725 BOOL CDECL wined3d_register_window(struct wined3d *wined3d, HWND window,
726 struct wined3d_device *device, unsigned int flags)
728 struct wined3d_wndproc *entry;
730 TRACE("wined3d %p, window %p, device %p, flags %#x.\n", wined3d, window, device, flags);
732 wined3d_wndproc_mutex_lock();
734 if ((entry = wined3d_find_wndproc(window, wined3d)))
736 if (!wined3d)
737 WARN("Window %p is already registered with wined3d.\n", window);
738 entry->flags = flags;
739 wined3d_wndproc_mutex_unlock();
740 return TRUE;
743 if (!wined3d_array_reserve((void **)&wndproc_table.entries, &wndproc_table.size,
744 wndproc_table.count + 1, sizeof(*entry)))
746 wined3d_wndproc_mutex_unlock();
747 ERR("Failed to grow table.\n");
748 return FALSE;
751 entry = &wndproc_table.entries[wndproc_table.count++];
752 entry->window = window;
753 entry->unicode = IsWindowUnicode(window);
754 if (!wined3d)
756 /* Set a window proc that matches the window. Some applications (e.g.
757 * NoX) replace the window proc after we've set ours, and expect to be
758 * able to call the previous one (ours) directly, without using
759 * CallWindowProc(). */
760 if (entry->unicode)
761 entry->proc = (WNDPROC)SetWindowLongPtrW(window, GWLP_WNDPROC, (LONG_PTR)wined3d_wndproc);
762 else
763 entry->proc = (WNDPROC)SetWindowLongPtrA(window, GWLP_WNDPROC, (LONG_PTR)wined3d_wndproc);
765 else
767 entry->proc = NULL;
769 entry->device = device;
770 entry->wined3d = wined3d;
771 entry->flags = flags;
773 wined3d_wndproc_mutex_unlock();
775 return TRUE;
778 static BOOL restore_wndproc(struct wined3d_wndproc *entry)
780 LONG_PTR proc;
782 if (entry->unicode)
784 proc = GetWindowLongPtrW(entry->window, GWLP_WNDPROC);
785 if (proc != (LONG_PTR)wined3d_wndproc)
786 return FALSE;
787 SetWindowLongPtrW(entry->window, GWLP_WNDPROC, (LONG_PTR)entry->proc);
789 else
791 proc = GetWindowLongPtrA(entry->window, GWLP_WNDPROC);
792 if (proc != (LONG_PTR)wined3d_wndproc)
793 return FALSE;
794 SetWindowLongPtrA(entry->window, GWLP_WNDPROC, (LONG_PTR)entry->proc);
797 return TRUE;
800 void wined3d_unregister_window(HWND window)
802 struct wined3d_wndproc *entry, *last;
804 wined3d_wndproc_mutex_lock();
806 if (!(entry = wined3d_find_wndproc(window, NULL)))
808 wined3d_wndproc_mutex_unlock();
809 ERR("Window %p is not registered with wined3d.\n", window);
810 return;
813 if (entry->proc && !restore_wndproc(entry))
815 entry->device = NULL;
816 WARN("Not unregistering window %p, current window proc doesn't match wined3d window proc.\n", window);
817 wined3d_wndproc_mutex_unlock();
818 return;
821 last = &wndproc_table.entries[--wndproc_table.count];
822 if (entry != last) *entry = *last;
824 wined3d_wndproc_mutex_unlock();
827 void CDECL wined3d_unregister_windows(struct wined3d *wined3d)
829 struct wined3d_wndproc *entry, *last;
830 unsigned int i = 0;
832 TRACE("wined3d %p.\n", wined3d);
834 wined3d_wndproc_mutex_lock();
836 while (i < wndproc_table.count)
838 entry = &wndproc_table.entries[i];
840 if (entry->wined3d != wined3d)
842 ++i;
843 continue;
846 if (entry->proc && !restore_wndproc(entry))
848 entry->device = NULL;
849 WARN("Not unregistering window %p, current window proc doesn't match wined3d window proc.\n",
850 entry->window);
851 ++i;
852 continue;
855 last = &wndproc_table.entries[--wndproc_table.count];
856 if (entry != last)
857 *entry = *last;
858 else
859 ++i;
862 wined3d_wndproc_mutex_unlock();
865 static struct wined3d_window_hook *wined3d_find_hook(DWORD thread_id)
867 unsigned int i;
869 for (i = 0; i < swapchain_state_table.hook_count; ++i)
871 if (swapchain_state_table.hooks[i].thread_id == thread_id)
872 return &swapchain_state_table.hooks[i];
875 return NULL;
878 void wined3d_swapchain_state_register(struct wined3d_swapchain_state *state)
880 struct wined3d_registered_swapchain_state *state_entry;
881 struct wined3d_window_hook *hook;
883 wined3d_wndproc_mutex_lock();
885 if (!wined3d_array_reserve((void **)&swapchain_state_table.states, &swapchain_state_table.states_size,
886 swapchain_state_table.state_count + 1, sizeof(*state_entry)))
888 wined3d_wndproc_mutex_unlock();
889 return;
892 state_entry = &swapchain_state_table.states[swapchain_state_table.state_count++];
893 state_entry->state = state;
894 state_entry->thread_id = GetWindowThreadProcessId(state->device_window, NULL);
896 if ((hook = wined3d_find_hook(state_entry->thread_id)))
898 ++hook->count;
899 wined3d_wndproc_mutex_unlock();
900 return;
903 if (!wined3d_array_reserve((void **)&swapchain_state_table.hooks, &swapchain_state_table.hooks_size,
904 swapchain_state_table.hook_count + 1, sizeof(*hook)))
906 --swapchain_state_table.state_count;
907 wined3d_wndproc_mutex_unlock();
908 return;
911 hook = &swapchain_state_table.hooks[swapchain_state_table.hook_count++];
912 hook->thread_id = state_entry->thread_id;
913 hook->hook = SetWindowsHookExW(WH_GETMESSAGE, wined3d_hook_proc, 0, hook->thread_id);
914 hook->count = 1;
916 wined3d_wndproc_mutex_unlock();
919 static void wined3d_swapchain_state_unregister(struct wined3d_swapchain_state *state)
921 struct wined3d_registered_swapchain_state *state_entry, *last_state_entry;
922 struct wined3d_window_hook *hook, *last_hook;
923 unsigned int i;
925 wined3d_wndproc_mutex_lock();
927 for (i = 0; i < swapchain_state_table.state_count; ++i)
929 state_entry = &swapchain_state_table.states[i];
931 if (state_entry->state != state)
932 continue;
934 if ((hook = wined3d_find_hook(state_entry->thread_id)) && !--hook->count)
936 UnhookWindowsHookEx(hook->hook);
937 last_hook = &swapchain_state_table.hooks[--swapchain_state_table.hook_count];
938 if (hook != last_hook)
939 *hook = *last_hook;
942 last_state_entry = &swapchain_state_table.states[--swapchain_state_table.state_count];
943 if (state_entry != last_state_entry)
944 *state_entry = *last_state_entry;
946 break;
949 wined3d_wndproc_mutex_unlock();
952 void wined3d_swapchain_state_cleanup(struct wined3d_swapchain_state *state)
954 wined3d_swapchain_state_unregister(state);
957 /* At process attach */
958 BOOL WINAPI DllMain(HINSTANCE inst, DWORD reason, void *reserved)
960 switch (reason)
962 case DLL_PROCESS_ATTACH:
963 return wined3d_dll_init(inst);
965 case DLL_PROCESS_DETACH:
966 if (!reserved)
967 return wined3d_dll_destroy(inst);
968 break;
970 case DLL_THREAD_DETACH:
971 if (!wined3d_context_gl_set_current(NULL))
973 ERR("Failed to clear current context.\n");
975 return TRUE;
977 return TRUE;