wined3d: Return the correct length from config_list_get_value().
[wine.git] / dlls / wined3d / wined3d_main.c
blob4f50e8aeb7df59c3b6dd34e1f43504334a64cb6d
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 "initguid.h"
26 #include "wined3d_private.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
29 WINE_DECLARE_DEBUG_CHANNEL(winediag);
31 struct wined3d_wndproc
33 struct wined3d *wined3d;
34 HWND window;
35 BOOL unicode;
36 BOOL filter;
37 WNDPROC proc;
38 struct wined3d_device *device;
39 uint32_t flags;
42 struct wined3d_wndproc_table
44 struct wined3d_wndproc *entries;
45 SIZE_T count;
46 SIZE_T size;
49 struct wined3d_window_hook
51 HHOOK hook;
52 DWORD thread_id;
53 unsigned int count;
56 struct wined3d_registered_swapchain_state
58 struct wined3d_swapchain_state *state;
59 DWORD thread_id;
62 struct wined3d_swapchain_state_table
64 struct wined3d_window_hook *hooks;
65 SIZE_T hooks_size;
66 SIZE_T hook_count;
68 struct wined3d_registered_swapchain_state *states;
69 SIZE_T states_size;
70 SIZE_T state_count;
73 static struct wined3d_wndproc_table wndproc_table;
74 static struct wined3d_swapchain_state_table swapchain_state_table;
76 static CRITICAL_SECTION wined3d_cs;
77 static CRITICAL_SECTION_DEBUG wined3d_cs_debug =
79 0, 0, &wined3d_cs,
80 {&wined3d_cs_debug.ProcessLocksList,
81 &wined3d_cs_debug.ProcessLocksList},
82 0, 0, {(DWORD_PTR)(__FILE__ ": wined3d_cs")}
84 static CRITICAL_SECTION wined3d_cs = {&wined3d_cs_debug, -1, 0, 0, 0, 0};
86 static CRITICAL_SECTION wined3d_wndproc_cs;
87 static CRITICAL_SECTION_DEBUG wined3d_wndproc_cs_debug =
89 0, 0, &wined3d_wndproc_cs,
90 {&wined3d_wndproc_cs_debug.ProcessLocksList,
91 &wined3d_wndproc_cs_debug.ProcessLocksList},
92 0, 0, {(DWORD_PTR)(__FILE__ ": wined3d_wndproc_cs")}
94 static CRITICAL_SECTION wined3d_wndproc_cs = {&wined3d_wndproc_cs_debug, -1, 0, 0, 0, 0};
96 CRITICAL_SECTION wined3d_command_cs;
97 static CRITICAL_SECTION_DEBUG wined3d_command_cs_debug =
99 0, 0, &wined3d_command_cs,
100 {&wined3d_command_cs_debug.ProcessLocksList,
101 &wined3d_command_cs_debug.ProcessLocksList},
102 0, 0, {(DWORD_PTR)(__FILE__ ": wined3d_command_cs")}
104 CRITICAL_SECTION wined3d_command_cs = {&wined3d_command_cs_debug, -1, 0, 0, 0, 0};
106 /* When updating default value here, make sure to update winecfg as well,
107 * where appropriate. */
108 struct wined3d_settings wined3d_settings =
110 .cs_multithreaded = WINED3D_CSMT_ENABLE,
111 .max_gl_version = MAKEDWORD_VERSION(4, 4),
112 .offscreen_rendering_mode = ORM_FBO,
113 .pci_vendor_id = PCI_VENDOR_NONE,
114 .pci_device_id = PCI_DEVICE_NONE,
115 .multisample_textures = TRUE,
116 .sample_count = ~0u,
117 .max_sm_vs = UINT_MAX,
118 .max_sm_ps = UINT_MAX,
119 .max_sm_ds = UINT_MAX,
120 .max_sm_hs = UINT_MAX,
121 .max_sm_gs = UINT_MAX,
122 .max_sm_cs = UINT_MAX,
123 .renderer = WINED3D_RENDERER_AUTO,
124 .shader_backend = WINED3D_SHADER_BACKEND_AUTO,
127 struct wined3d * CDECL wined3d_create(DWORD flags)
129 struct wined3d *object;
130 HRESULT hr;
132 if (!(object = heap_alloc_zero(FIELD_OFFSET(struct wined3d, adapters[1]))))
134 ERR("Failed to allocate wined3d object memory.\n");
135 return NULL;
138 if (wined3d_settings.renderer == WINED3D_RENDERER_NO3D)
139 flags |= WINED3D_NO3D;
141 if (FAILED(hr = wined3d_init(object, flags)))
143 WARN("Failed to initialize wined3d object, hr %#x.\n", hr);
144 heap_free(object);
145 return NULL;
148 TRACE("Created wined3d object %p.\n", object);
150 return object;
153 static bool is_option_separator(char c)
155 return c == ',' || c == ';' || c == '\0';
158 static const char *config_list_get_value(const char *string, const char *key, size_t *len)
160 const char *p, *end;
161 char prev_char;
163 p = string;
164 while (p)
166 if ((p = strstr(p, key)))
168 prev_char = p > string ? p[-1] : 0;
169 p += strlen(key);
171 if (is_option_separator(prev_char) && *p == '=')
173 if ((end = strpbrk(p + 1, ",;")))
174 *len = end - (p + 1);
175 else
176 *len = strlen(p + 1);
177 return p + 1;
182 return NULL;
185 static DWORD get_config_key(HKEY defkey, HKEY appkey, const char *env, const char *name, char *buffer, DWORD size)
187 const char *env_value;
188 size_t env_len;
190 if ((env_value = config_list_get_value(env, name, &env_len)) && env_len < size)
192 memcpy(buffer, env_value, env_len);
193 buffer[env_len] = 0;
194 return 0;
196 if (appkey && !RegQueryValueExA(appkey, name, 0, NULL, (BYTE *)buffer, &size)) return 0;
197 if (defkey && !RegQueryValueExA(defkey, name, 0, NULL, (BYTE *)buffer, &size)) return 0;
198 return ERROR_FILE_NOT_FOUND;
201 static DWORD get_config_key_dword(HKEY defkey, HKEY appkey, const char *env, const char *name, DWORD *value)
203 DWORD type, data, size;
204 const char *env_value;
205 size_t env_len;
206 char *end;
208 if ((env_value = config_list_get_value(env, name, &env_len)))
210 *value = strtoul(env_value, &end, 0);
211 if (end != env_value)
212 return 0;
214 size = sizeof(data);
215 if (appkey && !RegQueryValueExA(appkey, name, 0, &type, (BYTE *)&data, &size) && type == REG_DWORD) goto success;
216 size = sizeof(data);
217 if (defkey && !RegQueryValueExA(defkey, name, 0, &type, (BYTE *)&data, &size) && type == REG_DWORD) goto success;
219 return ERROR_FILE_NOT_FOUND;
221 success:
222 *value = data;
223 return 0;
226 BOOL wined3d_get_app_name(char *app_name, unsigned int app_name_size)
228 char buffer[MAX_PATH];
229 unsigned int len;
230 char *p, *name;
232 len = GetModuleFileNameA(0, buffer, ARRAY_SIZE(buffer));
233 if (!(len && len < MAX_PATH))
234 return FALSE;
236 name = buffer;
237 if ((p = strrchr(name, '/' )))
238 name = p + 1;
239 if ((p = strrchr(name, '\\')))
240 name = p + 1;
242 len = strlen(name) + 1;
243 if (app_name_size < len)
244 return FALSE;
246 memcpy(app_name, name, len);
247 return TRUE;
250 static BOOL wined3d_dll_init(HINSTANCE hInstDLL)
252 DWORD wined3d_context_tls_idx;
253 char buffer[MAX_PATH+10];
254 DWORD size = sizeof(buffer);
255 const char *env;
256 HKEY hkey = 0;
257 HKEY appkey = 0;
258 DWORD tmpvalue;
259 WNDCLASSA wc;
261 wined3d_context_tls_idx = TlsAlloc();
262 if (wined3d_context_tls_idx == TLS_OUT_OF_INDEXES)
264 DWORD err = GetLastError();
265 ERR("Failed to allocate context TLS index, err %#x.\n", err);
266 return FALSE;
268 context_set_tls_idx(wined3d_context_tls_idx);
270 /* We need our own window class for a fake window which we use to retrieve GL capabilities */
271 /* We might need CS_OWNDC in the future if we notice strange things on Windows.
272 * Various articles/posts about OpenGL problems on Windows recommend this. */
273 wc.style = CS_HREDRAW | CS_VREDRAW;
274 wc.lpfnWndProc = DefWindowProcA;
275 wc.cbClsExtra = 0;
276 wc.cbWndExtra = 0;
277 wc.hInstance = hInstDLL;
278 wc.hIcon = LoadIconA(NULL, (const char *)IDI_WINLOGO);
279 wc.hCursor = LoadCursorA(NULL, (const char *)IDC_ARROW);
280 wc.hbrBackground = NULL;
281 wc.lpszMenuName = NULL;
282 wc.lpszClassName = WINED3D_OPENGL_WINDOW_CLASS_NAME;
284 if (!RegisterClassA(&wc))
286 ERR("Failed to register window class 'WineD3D_OpenGL'!\n");
287 if (!TlsFree(wined3d_context_tls_idx))
289 DWORD err = GetLastError();
290 ERR("Failed to free context TLS index, err %#x.\n", err);
292 return FALSE;
295 DisableThreadLibraryCalls(hInstDLL);
297 /* @@ Wine registry key: HKCU\Software\Wine\Direct3D */
298 if ( RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Direct3D", &hkey ) ) hkey = 0;
300 if (wined3d_get_app_name(buffer, ARRAY_SIZE(buffer)))
302 HKEY tmpkey;
303 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\Direct3D */
304 if (!RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmpkey))
306 strcat(buffer, "\\Direct3D");
307 TRACE("Application name %s.\n", buffer);
308 if (RegOpenKeyA(tmpkey, buffer, &appkey)) appkey = 0;
309 RegCloseKey(tmpkey);
313 /* Allow modifying settings using the WINE_D3D_CONFIG environment variable,
314 * which takes precedence over registry keys. An example is as follows:
316 * WINE_D3D_CONFIG=csmt=0x1,shader_backend=glsl
318 env = getenv("WINE_D3D_CONFIG");
320 if (hkey || appkey || env)
322 if (!get_config_key_dword(hkey, appkey, env, "csmt", &wined3d_settings.cs_multithreaded))
323 ERR_(winediag)("Setting multithreaded command stream to %#x.\n", wined3d_settings.cs_multithreaded);
324 if (!get_config_key_dword(hkey, appkey, env, "MaxVersionGL", &tmpvalue))
326 ERR_(winediag)("Setting maximum allowed wined3d GL version to %u.%u.\n",
327 tmpvalue >> 16, tmpvalue & 0xffff);
328 wined3d_settings.max_gl_version = tmpvalue;
330 if (!get_config_key(hkey, appkey, env, "shader_backend", buffer, size))
332 if (!_strnicmp(buffer, "glsl", -1))
334 ERR_(winediag)("Using the GLSL shader backend.\n");
335 wined3d_settings.shader_backend = WINED3D_SHADER_BACKEND_GLSL;
337 else if (!_strnicmp(buffer, "arb", -1))
339 ERR_(winediag)("Using the ARB shader backend.\n");
340 wined3d_settings.shader_backend = WINED3D_SHADER_BACKEND_ARB;
342 else if (!_strnicmp(buffer, "none", -1))
344 ERR_(winediag)("Disabling shader backends.\n");
345 wined3d_settings.shader_backend = WINED3D_SHADER_BACKEND_NONE;
348 if (wined3d_settings.shader_backend == WINED3D_SHADER_BACKEND_ARB
349 || wined3d_settings.shader_backend == WINED3D_SHADER_BACKEND_NONE)
351 ERR_(winediag)("The GLSL shader backend has been disabled. You get to keep all the pieces if it breaks.\n");
352 TRACE("Use of GL Shading Language disabled.\n");
354 if (!get_config_key(hkey, appkey, env, "OffscreenRenderingMode", buffer, size)
355 && !strcmp(buffer,"backbuffer"))
356 wined3d_settings.offscreen_rendering_mode = ORM_BACKBUFFER;
357 if (!get_config_key_dword(hkey, appkey, env, "VideoPciDeviceID", &tmpvalue))
359 int pci_device_id = tmpvalue;
361 /* A pci device id is 16-bit */
362 if(pci_device_id > 0xffff)
364 ERR("Invalid value for VideoPciDeviceID. The value should be smaller or equal to 65535 or 0xffff\n");
366 else
368 TRACE("Using PCI Device ID %04x\n", pci_device_id);
369 wined3d_settings.pci_device_id = pci_device_id;
372 if (!get_config_key_dword(hkey, appkey, env, "VideoPciVendorID", &tmpvalue))
374 int pci_vendor_id = tmpvalue;
376 /* A pci device id is 16-bit */
377 if(pci_vendor_id > 0xffff)
379 ERR("Invalid value for VideoPciVendorID. The value should be smaller or equal to 65535 or 0xffff\n");
381 else
383 TRACE("Using PCI Vendor ID %04x\n", pci_vendor_id);
384 wined3d_settings.pci_vendor_id = pci_vendor_id;
387 if (!get_config_key(hkey, appkey, env, "VideoMemorySize", buffer, size))
389 int TmpVideoMemorySize = atoi(buffer);
390 if(TmpVideoMemorySize > 0)
392 wined3d_settings.emulated_textureram = (UINT64)TmpVideoMemorySize *1024*1024;
393 TRACE("Use %iMiB = 0x%s bytes for emulated_textureram\n",
394 TmpVideoMemorySize,
395 wine_dbgstr_longlong(wined3d_settings.emulated_textureram));
397 else
398 ERR("VideoMemorySize is %i but must be >0\n", TmpVideoMemorySize);
400 if (!get_config_key(hkey, appkey, env, "WineLogo", buffer, size))
402 size_t len = strlen(buffer) + 1;
404 if (!(wined3d_settings.logo = heap_alloc(len)))
405 ERR("Failed to allocate logo path memory.\n");
406 else
407 memcpy(wined3d_settings.logo, buffer, len);
409 if (!get_config_key_dword(hkey, appkey, env, "MultisampleTextures", &wined3d_settings.multisample_textures))
410 ERR_(winediag)("Setting multisample textures to %#x.\n", wined3d_settings.multisample_textures);
411 if (!get_config_key_dword(hkey, appkey, env, "SampleCount", &wined3d_settings.sample_count))
412 ERR_(winediag)("Forcing sample count to %u. This may not be compatible with all applications.\n",
413 wined3d_settings.sample_count);
414 if (!get_config_key(hkey, appkey, env, "CheckFloatConstants", buffer, size)
415 && !strcmp(buffer, "enabled"))
417 TRACE("Checking relative addressing indices in float constants.\n");
418 wined3d_settings.check_float_constants = TRUE;
420 if (!get_config_key_dword(hkey, appkey, env, "strict_shader_math", &wined3d_settings.strict_shader_math))
421 ERR_(winediag)("Setting strict shader math to %#x.\n", wined3d_settings.strict_shader_math);
422 if (!get_config_key_dword(hkey, appkey, env, "MaxShaderModelVS", &wined3d_settings.max_sm_vs))
423 TRACE("Limiting VS shader model to %u.\n", wined3d_settings.max_sm_vs);
424 if (!get_config_key_dword(hkey, appkey, env, "MaxShaderModelHS", &wined3d_settings.max_sm_hs))
425 TRACE("Limiting HS shader model to %u.\n", wined3d_settings.max_sm_hs);
426 if (!get_config_key_dword(hkey, appkey, env, "MaxShaderModelDS", &wined3d_settings.max_sm_ds))
427 TRACE("Limiting DS shader model to %u.\n", wined3d_settings.max_sm_ds);
428 if (!get_config_key_dword(hkey, appkey, env, "MaxShaderModelGS", &wined3d_settings.max_sm_gs))
429 TRACE("Limiting GS shader model to %u.\n", wined3d_settings.max_sm_gs);
430 if (!get_config_key_dword(hkey, appkey, env, "MaxShaderModelPS", &wined3d_settings.max_sm_ps))
431 TRACE("Limiting PS shader model to %u.\n", wined3d_settings.max_sm_ps);
432 if (!get_config_key_dword(hkey, appkey, env, "MaxShaderModelCS", &wined3d_settings.max_sm_cs))
433 TRACE("Limiting CS shader model to %u.\n", wined3d_settings.max_sm_cs);
434 if (!get_config_key(hkey, appkey, env, "renderer", buffer, size))
436 if (!strcmp(buffer, "vulkan"))
438 ERR_(winediag)("Using the Vulkan renderer.\n");
439 wined3d_settings.renderer = WINED3D_RENDERER_VULKAN;
441 else if (!strcmp(buffer, "gl"))
443 ERR_(winediag)("Using the OpenGL renderer.\n");
444 wined3d_settings.renderer = WINED3D_RENDERER_OPENGL;
446 else if (!strcmp(buffer, "gdi") || !strcmp(buffer, "no3d"))
448 ERR_(winediag)("Disabling 3D support.\n");
449 wined3d_settings.renderer = WINED3D_RENDERER_NO3D;
452 if (!get_config_key_dword(hkey, appkey, env, "cb_access_map_w", &tmpvalue) && tmpvalue)
454 TRACE("Forcing all constant buffers to be write-mappable.\n");
455 wined3d_settings.cb_access_map_w = TRUE;
459 if (appkey) RegCloseKey( appkey );
460 if (hkey) RegCloseKey( hkey );
462 return TRUE;
465 static BOOL wined3d_dll_destroy(HINSTANCE hInstDLL)
467 DWORD wined3d_context_tls_idx = context_get_tls_idx();
468 unsigned int i;
470 if (!TlsFree(wined3d_context_tls_idx))
472 DWORD err = GetLastError();
473 ERR("Failed to free context TLS index, err %#x.\n", err);
476 for (i = 0; i < wndproc_table.count; ++i)
478 /* Trying to unregister these would be futile. These entries can only
479 * exist if either we skipped them in wined3d_unregister_window() due
480 * to the application replacing the wndproc after the entry was
481 * registered, or if the application still has an active wined3d
482 * device. In the latter case the application has bigger problems than
483 * these entries. */
484 WARN("Leftover wndproc table entry %p.\n", &wndproc_table.entries[i]);
486 heap_free(wndproc_table.entries);
488 heap_free(swapchain_state_table.states);
489 for (i = 0; i < swapchain_state_table.hook_count; ++i)
491 WARN("Leftover swapchain state hook %p.\n", &swapchain_state_table.hooks[i]);
492 UnhookWindowsHookEx(swapchain_state_table.hooks[i].hook);
494 heap_free(swapchain_state_table.hooks);
496 heap_free(wined3d_settings.logo);
497 UnregisterClassA(WINED3D_OPENGL_WINDOW_CLASS_NAME, hInstDLL);
499 DeleteCriticalSection(&wined3d_command_cs);
501 DeleteCriticalSection(&wined3d_wndproc_cs);
502 DeleteCriticalSection(&wined3d_cs);
503 return TRUE;
506 void WINAPI wined3d_mutex_lock(void)
508 EnterCriticalSection(&wined3d_cs);
511 void WINAPI wined3d_mutex_unlock(void)
513 LeaveCriticalSection(&wined3d_cs);
516 static void wined3d_wndproc_mutex_lock(void)
518 EnterCriticalSection(&wined3d_wndproc_cs);
521 static void wined3d_wndproc_mutex_unlock(void)
523 LeaveCriticalSection(&wined3d_wndproc_cs);
526 static struct wined3d_output * wined3d_get_output_from_window(const struct wined3d *wined3d,
527 HWND hwnd)
529 unsigned int adapter_idx, output_idx;
530 struct wined3d_adapter *adapter;
531 MONITORINFOEXW monitor_info;
532 HMONITOR monitor;
534 TRACE("wined3d %p, hwnd %p.\n", wined3d, hwnd);
536 monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
537 monitor_info.cbSize = sizeof(monitor_info);
538 if (!GetMonitorInfoW(monitor, (MONITORINFO *)&monitor_info))
540 ERR("GetMonitorInfoW failed, error %#x.\n", GetLastError());
541 return NULL;
544 for (adapter_idx = 0; adapter_idx < wined3d->adapter_count; ++adapter_idx)
546 adapter = wined3d->adapters[adapter_idx];
547 for (output_idx = 0; output_idx < adapter->output_count; ++output_idx)
549 if (!lstrcmpiW(adapter->outputs[output_idx].device_name, monitor_info.szDevice))
550 return &adapter->outputs[output_idx];
554 return NULL;
557 static struct wined3d_wndproc *wined3d_find_wndproc(HWND window, struct wined3d *wined3d)
559 unsigned int i;
561 for (i = 0; i < wndproc_table.count; ++i)
563 struct wined3d_wndproc *entry = &wndproc_table.entries[i];
565 if (entry->window == window && entry->wined3d == wined3d)
566 return entry;
569 return NULL;
572 BOOL wined3d_filter_messages(HWND window, BOOL filter)
574 struct wined3d_wndproc *entry;
575 BOOL ret;
577 wined3d_wndproc_mutex_lock();
579 if (!(entry = wined3d_find_wndproc(window, NULL)))
581 wined3d_wndproc_mutex_unlock();
582 return FALSE;
585 ret = entry->filter;
586 entry->filter = filter;
588 wined3d_wndproc_mutex_unlock();
590 return ret;
593 static LRESULT CALLBACK wined3d_wndproc(HWND window, UINT message, WPARAM wparam, LPARAM lparam)
595 struct wined3d_wndproc *entry;
596 struct wined3d_device *device;
597 BOOL unicode, filter;
598 WNDPROC proc;
600 wined3d_wndproc_mutex_lock();
602 if (!(entry = wined3d_find_wndproc(window, NULL)))
604 wined3d_wndproc_mutex_unlock();
605 ERR("Window %p is not registered with wined3d.\n", window);
606 return DefWindowProcW(window, message, wparam, lparam);
609 device = entry->device;
610 unicode = entry->unicode;
611 filter = entry->filter;
612 proc = entry->proc;
613 wined3d_wndproc_mutex_unlock();
615 if (device)
617 if (filter && message != WM_DISPLAYCHANGE)
619 TRACE("Filtering message: window %p, message %#x, wparam %#lx, lparam %#lx.\n",
620 window, message, wparam, lparam);
622 if (unicode)
623 return DefWindowProcW(window, message, wparam, lparam);
624 return DefWindowProcA(window, message, wparam, lparam);
627 return device_process_message(device, window, unicode, message, wparam, lparam, proc);
629 if (unicode)
630 return CallWindowProcW(proc, window, message, wparam, lparam);
631 return CallWindowProcA(proc, window, message, wparam, lparam);
634 static LRESULT CALLBACK wined3d_hook_proc(int code, WPARAM wparam, LPARAM lparam)
636 struct wined3d_swapchain_desc swapchain_desc;
637 struct wined3d_swapchain_state *state;
638 struct wined3d_wndproc *entry;
639 struct wined3d_output *output;
640 MSG *msg = (MSG *)lparam;
641 unsigned int i;
643 /* Handle Alt+Enter. */
644 if (code == HC_ACTION && msg->message == WM_SYSKEYDOWN
645 && msg->wParam == VK_RETURN && (msg->lParam & (KF_ALTDOWN << 16)))
647 wined3d_wndproc_mutex_lock();
649 for (i = 0; i < swapchain_state_table.state_count; ++i)
651 state = swapchain_state_table.states[i].state;
653 if (state->device_window != msg->hwnd)
654 continue;
656 if ((entry = wined3d_find_wndproc(msg->hwnd, state->wined3d))
657 && (entry->flags & (WINED3D_REGISTER_WINDOW_NO_WINDOW_CHANGES
658 | WINED3D_REGISTER_WINDOW_NO_ALT_ENTER)))
659 continue;
661 swapchain_desc = state->desc;
662 swapchain_desc.windowed = !swapchain_desc.windowed;
663 if (!(output = wined3d_get_output_from_window(state->wined3d, state->device_window)))
665 ERR("Failed to get output from window %p.\n", state->device_window);
666 break;
668 swapchain_desc.output = output;
669 wined3d_swapchain_state_set_fullscreen(state, &swapchain_desc, NULL);
671 wined3d_wndproc_mutex_unlock();
673 return 1;
676 wined3d_wndproc_mutex_unlock();
679 return CallNextHookEx(0, code, wparam, lparam);
682 BOOL CDECL wined3d_register_window(struct wined3d *wined3d, HWND window,
683 struct wined3d_device *device, unsigned int flags)
685 struct wined3d_wndproc *entry;
687 TRACE("wined3d %p, window %p, device %p, flags %#x.\n", wined3d, window, device, flags);
689 wined3d_wndproc_mutex_lock();
691 if ((entry = wined3d_find_wndproc(window, wined3d)))
693 if (!wined3d)
694 WARN("Window %p is already registered with wined3d.\n", window);
695 entry->flags = flags;
696 wined3d_wndproc_mutex_unlock();
697 return TRUE;
700 if (!wined3d_array_reserve((void **)&wndproc_table.entries, &wndproc_table.size,
701 wndproc_table.count + 1, sizeof(*entry)))
703 wined3d_wndproc_mutex_unlock();
704 ERR("Failed to grow table.\n");
705 return FALSE;
708 entry = &wndproc_table.entries[wndproc_table.count++];
709 entry->window = window;
710 entry->unicode = IsWindowUnicode(window);
711 if (!wined3d)
713 /* Set a window proc that matches the window. Some applications (e.g.
714 * NoX) replace the window proc after we've set ours, and expect to be
715 * able to call the previous one (ours) directly, without using
716 * CallWindowProc(). */
717 if (entry->unicode)
718 entry->proc = (WNDPROC)SetWindowLongPtrW(window, GWLP_WNDPROC, (LONG_PTR)wined3d_wndproc);
719 else
720 entry->proc = (WNDPROC)SetWindowLongPtrA(window, GWLP_WNDPROC, (LONG_PTR)wined3d_wndproc);
722 else
724 entry->proc = NULL;
726 entry->device = device;
727 entry->wined3d = wined3d;
728 entry->flags = flags;
730 wined3d_wndproc_mutex_unlock();
732 return TRUE;
735 static BOOL restore_wndproc(struct wined3d_wndproc *entry)
737 LONG_PTR proc;
739 if (entry->unicode)
741 proc = GetWindowLongPtrW(entry->window, GWLP_WNDPROC);
742 if (proc != (LONG_PTR)wined3d_wndproc)
743 return FALSE;
744 SetWindowLongPtrW(entry->window, GWLP_WNDPROC, (LONG_PTR)entry->proc);
746 else
748 proc = GetWindowLongPtrA(entry->window, GWLP_WNDPROC);
749 if (proc != (LONG_PTR)wined3d_wndproc)
750 return FALSE;
751 SetWindowLongPtrA(entry->window, GWLP_WNDPROC, (LONG_PTR)entry->proc);
754 return TRUE;
757 void wined3d_unregister_window(HWND window)
759 struct wined3d_wndproc *entry, *last;
761 wined3d_wndproc_mutex_lock();
763 if (!(entry = wined3d_find_wndproc(window, NULL)))
765 wined3d_wndproc_mutex_unlock();
766 ERR("Window %p is not registered with wined3d.\n", window);
767 return;
770 if (entry->proc && !restore_wndproc(entry))
772 entry->device = NULL;
773 WARN("Not unregistering window %p, current window proc doesn't match wined3d window proc.\n", window);
774 wined3d_wndproc_mutex_unlock();
775 return;
778 last = &wndproc_table.entries[--wndproc_table.count];
779 if (entry != last) *entry = *last;
781 wined3d_wndproc_mutex_unlock();
784 void CDECL wined3d_unregister_windows(struct wined3d *wined3d)
786 struct wined3d_wndproc *entry, *last;
787 unsigned int i = 0;
789 TRACE("wined3d %p.\n", wined3d);
791 wined3d_wndproc_mutex_lock();
793 while (i < wndproc_table.count)
795 entry = &wndproc_table.entries[i];
797 if (entry->wined3d != wined3d)
799 ++i;
800 continue;
803 if (entry->proc && !restore_wndproc(entry))
805 entry->device = NULL;
806 WARN("Not unregistering window %p, current window proc doesn't match wined3d window proc.\n",
807 entry->window);
808 ++i;
809 continue;
812 last = &wndproc_table.entries[--wndproc_table.count];
813 if (entry != last)
814 *entry = *last;
815 else
816 ++i;
819 wined3d_wndproc_mutex_unlock();
822 static struct wined3d_window_hook *wined3d_find_hook(DWORD thread_id)
824 unsigned int i;
826 for (i = 0; i < swapchain_state_table.hook_count; ++i)
828 if (swapchain_state_table.hooks[i].thread_id == thread_id)
829 return &swapchain_state_table.hooks[i];
832 return NULL;
835 void wined3d_swapchain_state_register(struct wined3d_swapchain_state *state)
837 struct wined3d_registered_swapchain_state *state_entry;
838 struct wined3d_window_hook *hook;
840 wined3d_wndproc_mutex_lock();
842 if (!wined3d_array_reserve((void **)&swapchain_state_table.states, &swapchain_state_table.states_size,
843 swapchain_state_table.state_count + 1, sizeof(*state_entry)))
845 wined3d_wndproc_mutex_unlock();
846 return;
849 state_entry = &swapchain_state_table.states[swapchain_state_table.state_count++];
850 state_entry->state = state;
851 state_entry->thread_id = GetWindowThreadProcessId(state->device_window, NULL);
853 if ((hook = wined3d_find_hook(state_entry->thread_id)))
855 ++hook->count;
856 wined3d_wndproc_mutex_unlock();
857 return;
860 if (!wined3d_array_reserve((void **)&swapchain_state_table.hooks, &swapchain_state_table.hooks_size,
861 swapchain_state_table.hook_count + 1, sizeof(*hook)))
863 --swapchain_state_table.state_count;
864 wined3d_wndproc_mutex_unlock();
865 return;
868 hook = &swapchain_state_table.hooks[swapchain_state_table.hook_count++];
869 hook->thread_id = state_entry->thread_id;
870 hook->hook = SetWindowsHookExW(WH_GETMESSAGE, wined3d_hook_proc, 0, hook->thread_id);
871 hook->count = 1;
873 wined3d_wndproc_mutex_unlock();
876 static void wined3d_swapchain_state_unregister(struct wined3d_swapchain_state *state)
878 struct wined3d_registered_swapchain_state *state_entry, *last_state_entry;
879 struct wined3d_window_hook *hook, *last_hook;
880 unsigned int i;
882 wined3d_wndproc_mutex_lock();
884 for (i = 0; i < swapchain_state_table.state_count; ++i)
886 state_entry = &swapchain_state_table.states[i];
888 if (state_entry->state != state)
889 continue;
891 if ((hook = wined3d_find_hook(state_entry->thread_id)) && !--hook->count)
893 UnhookWindowsHookEx(hook->hook);
894 last_hook = &swapchain_state_table.hooks[--swapchain_state_table.hook_count];
895 if (hook != last_hook)
896 *hook = *last_hook;
899 last_state_entry = &swapchain_state_table.states[--swapchain_state_table.state_count];
900 if (state_entry != last_state_entry)
901 *state_entry = *last_state_entry;
903 break;
906 wined3d_wndproc_mutex_unlock();
909 void wined3d_swapchain_state_cleanup(struct wined3d_swapchain_state *state)
911 wined3d_swapchain_state_unregister(state);
914 /* At process attach */
915 BOOL WINAPI DllMain(HINSTANCE inst, DWORD reason, void *reserved)
917 switch (reason)
919 case DLL_PROCESS_ATTACH:
920 return wined3d_dll_init(inst);
922 case DLL_PROCESS_DETACH:
923 if (!reserved)
924 return wined3d_dll_destroy(inst);
925 break;
927 case DLL_THREAD_DETACH:
928 if (!wined3d_context_gl_set_current(NULL))
930 ERR("Failed to clear current context.\n");
932 return TRUE;
934 return TRUE;