winebus.sys: Add missing keyboard free_device callback.
[wine.git] / dlls / wined3d / wined3d_main.c
blob293359714c35d56fe3267b2e16a659d68c187423
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"
26 #include "wine/port.h"
28 #include "initguid.h"
29 #include "wined3d_private.h"
31 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
32 WINE_DECLARE_DEBUG_CHANNEL(winediag);
34 struct wined3d_wndproc
36 struct wined3d *wined3d;
37 HWND window;
38 BOOL unicode;
39 BOOL filter;
40 WNDPROC proc;
41 struct wined3d_device *device;
42 uint32_t flags;
45 struct wined3d_wndproc_table
47 struct wined3d_wndproc *entries;
48 SIZE_T count;
49 SIZE_T size;
52 struct wined3d_window_hook
54 HHOOK hook;
55 DWORD thread_id;
56 unsigned int count;
59 struct wined3d_registered_swapchain_state
61 struct wined3d_swapchain_state *state;
62 DWORD thread_id;
65 struct wined3d_swapchain_state_table
67 struct wined3d_window_hook *hooks;
68 SIZE_T hooks_size;
69 SIZE_T hook_count;
71 struct wined3d_registered_swapchain_state *states;
72 SIZE_T states_size;
73 SIZE_T state_count;
76 static struct wined3d_wndproc_table wndproc_table;
77 static struct wined3d_swapchain_state_table swapchain_state_table;
79 static CRITICAL_SECTION wined3d_cs;
80 static CRITICAL_SECTION_DEBUG wined3d_cs_debug =
82 0, 0, &wined3d_cs,
83 {&wined3d_cs_debug.ProcessLocksList,
84 &wined3d_cs_debug.ProcessLocksList},
85 0, 0, {(DWORD_PTR)(__FILE__ ": wined3d_cs")}
87 static CRITICAL_SECTION wined3d_cs = {&wined3d_cs_debug, -1, 0, 0, 0, 0};
89 static CRITICAL_SECTION wined3d_wndproc_cs;
90 static CRITICAL_SECTION_DEBUG wined3d_wndproc_cs_debug =
92 0, 0, &wined3d_wndproc_cs,
93 {&wined3d_wndproc_cs_debug.ProcessLocksList,
94 &wined3d_wndproc_cs_debug.ProcessLocksList},
95 0, 0, {(DWORD_PTR)(__FILE__ ": wined3d_wndproc_cs")}
97 static CRITICAL_SECTION wined3d_wndproc_cs = {&wined3d_wndproc_cs_debug, -1, 0, 0, 0, 0};
99 CRITICAL_SECTION wined3d_command_cs;
100 static CRITICAL_SECTION_DEBUG wined3d_command_cs_debug =
102 0, 0, &wined3d_command_cs,
103 {&wined3d_command_cs_debug.ProcessLocksList,
104 &wined3d_command_cs_debug.ProcessLocksList},
105 0, 0, {(DWORD_PTR)(__FILE__ ": wined3d_command_cs")}
107 CRITICAL_SECTION wined3d_command_cs = {&wined3d_command_cs_debug, -1, 0, 0, 0, 0};
109 /* When updating default value here, make sure to update winecfg as well,
110 * where appropriate. */
111 struct wined3d_settings wined3d_settings =
113 WINED3D_CSMT_ENABLE, /* Multithreaded CS by default. */
114 MAKEDWORD_VERSION(4, 4), /* Default to OpenGL 4.4 */
115 ORM_FBO, /* Use FBOs to do offscreen rendering */
116 PCI_VENDOR_NONE,/* PCI Vendor ID */
117 PCI_DEVICE_NONE,/* PCI Device ID */
118 0, /* The default of memory is set in init_driver_info */
119 NULL, /* No wine logo by default */
120 TRUE, /* Prefer multisample textures to multisample renderbuffers. */
121 ~0u, /* Don't force a specific sample count by default. */
122 FALSE, /* Don't range check relative addressing indices in float constants. */
123 FALSE, /* No strict shader math by default. */
124 ~0U, /* No VS shader model limit by default. */
125 ~0U, /* No HS shader model limit by default. */
126 ~0U, /* No DS shader model limit by default. */
127 ~0U, /* No GS shader model limit by default. */
128 ~0U, /* No PS shader model limit by default. */
129 ~0u, /* No CS shader model limit by default. */
130 WINED3D_RENDERER_AUTO,
131 WINED3D_SHADER_BACKEND_AUTO,
134 struct wined3d * CDECL wined3d_create(DWORD flags)
136 struct wined3d *object;
137 HRESULT hr;
139 if (!(object = heap_alloc_zero(FIELD_OFFSET(struct wined3d, adapters[1]))))
141 ERR("Failed to allocate wined3d object memory.\n");
142 return NULL;
145 if (wined3d_settings.renderer == WINED3D_RENDERER_NO3D)
146 flags |= WINED3D_NO3D;
148 if (FAILED(hr = wined3d_init(object, flags)))
150 WARN("Failed to initialize wined3d object, hr %#x.\n", hr);
151 heap_free(object);
152 return NULL;
155 TRACE("Created wined3d object %p.\n", object);
157 return object;
160 static DWORD get_config_key(HKEY defkey, HKEY appkey, const char *name, char *buffer, DWORD size)
162 if (appkey && !RegQueryValueExA(appkey, name, 0, NULL, (BYTE *)buffer, &size)) return 0;
163 if (defkey && !RegQueryValueExA(defkey, name, 0, NULL, (BYTE *)buffer, &size)) return 0;
164 return ERROR_FILE_NOT_FOUND;
167 static DWORD get_config_key_dword(HKEY defkey, HKEY appkey, const char *name, DWORD *value)
169 DWORD type, data, size;
171 size = sizeof(data);
172 if (appkey && !RegQueryValueExA(appkey, name, 0, &type, (BYTE *)&data, &size) && type == REG_DWORD) goto success;
173 size = sizeof(data);
174 if (defkey && !RegQueryValueExA(defkey, name, 0, &type, (BYTE *)&data, &size) && type == REG_DWORD) goto success;
176 return ERROR_FILE_NOT_FOUND;
178 success:
179 *value = data;
180 return 0;
183 BOOL wined3d_get_app_name(char *app_name, unsigned int app_name_size)
185 char buffer[MAX_PATH];
186 unsigned int len;
187 char *p, *name;
189 len = GetModuleFileNameA(0, buffer, ARRAY_SIZE(buffer));
190 if (!(len && len < MAX_PATH))
191 return FALSE;
193 name = buffer;
194 if ((p = strrchr(name, '/' )))
195 name = p + 1;
196 if ((p = strrchr(name, '\\')))
197 name = p + 1;
199 len = strlen(name) + 1;
200 if (app_name_size < len)
201 return FALSE;
203 memcpy(app_name, name, len);
204 return TRUE;
207 static BOOL wined3d_dll_init(HINSTANCE hInstDLL)
209 DWORD wined3d_context_tls_idx;
210 char buffer[MAX_PATH+10];
211 DWORD size = sizeof(buffer);
212 HKEY hkey = 0;
213 HKEY appkey = 0;
214 DWORD tmpvalue;
215 WNDCLASSA wc;
217 wined3d_context_tls_idx = TlsAlloc();
218 if (wined3d_context_tls_idx == TLS_OUT_OF_INDEXES)
220 DWORD err = GetLastError();
221 ERR("Failed to allocate context TLS index, err %#x.\n", err);
222 return FALSE;
224 context_set_tls_idx(wined3d_context_tls_idx);
226 /* We need our own window class for a fake window which we use to retrieve GL capabilities */
227 /* We might need CS_OWNDC in the future if we notice strange things on Windows.
228 * Various articles/posts about OpenGL problems on Windows recommend this. */
229 wc.style = CS_HREDRAW | CS_VREDRAW;
230 wc.lpfnWndProc = DefWindowProcA;
231 wc.cbClsExtra = 0;
232 wc.cbWndExtra = 0;
233 wc.hInstance = hInstDLL;
234 wc.hIcon = LoadIconA(NULL, (const char *)IDI_WINLOGO);
235 wc.hCursor = LoadCursorA(NULL, (const char *)IDC_ARROW);
236 wc.hbrBackground = NULL;
237 wc.lpszMenuName = NULL;
238 wc.lpszClassName = WINED3D_OPENGL_WINDOW_CLASS_NAME;
240 if (!RegisterClassA(&wc))
242 ERR("Failed to register window class 'WineD3D_OpenGL'!\n");
243 if (!TlsFree(wined3d_context_tls_idx))
245 DWORD err = GetLastError();
246 ERR("Failed to free context TLS index, err %#x.\n", err);
248 return FALSE;
251 DisableThreadLibraryCalls(hInstDLL);
253 /* @@ Wine registry key: HKCU\Software\Wine\Direct3D */
254 if ( RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Direct3D", &hkey ) ) hkey = 0;
256 if (wined3d_get_app_name(buffer, ARRAY_SIZE(buffer)))
258 HKEY tmpkey;
259 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\Direct3D */
260 if (!RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmpkey))
262 strcat(buffer, "\\Direct3D");
263 TRACE("Application name %s.\n", buffer);
264 if (RegOpenKeyA(tmpkey, buffer, &appkey)) appkey = 0;
265 RegCloseKey(tmpkey);
269 if (hkey || appkey)
271 if (!get_config_key_dword(hkey, appkey, "csmt", &wined3d_settings.cs_multithreaded))
272 ERR_(winediag)("Setting multithreaded command stream to %#x.\n", wined3d_settings.cs_multithreaded);
273 if (!get_config_key_dword(hkey, appkey, "MaxVersionGL", &tmpvalue))
275 ERR_(winediag)("Setting maximum allowed wined3d GL version to %u.%u.\n",
276 tmpvalue >> 16, tmpvalue & 0xffff);
277 wined3d_settings.max_gl_version = tmpvalue;
279 if (!get_config_key(hkey, appkey, "shader_backend", buffer, size))
281 if (!_strnicmp(buffer, "glsl", -1))
283 ERR_(winediag)("Using the GLSL shader backend.\n");
284 wined3d_settings.shader_backend = WINED3D_SHADER_BACKEND_GLSL;
286 else if (!_strnicmp(buffer, "arb", -1))
288 ERR_(winediag)("Using the ARB shader backend.\n");
289 wined3d_settings.shader_backend = WINED3D_SHADER_BACKEND_ARB;
291 else if (!_strnicmp(buffer, "none", -1))
293 ERR_(winediag)("Disabling shader backends.\n");
294 wined3d_settings.shader_backend = WINED3D_SHADER_BACKEND_NONE;
297 if (wined3d_settings.shader_backend == WINED3D_SHADER_BACKEND_ARB
298 || wined3d_settings.shader_backend == WINED3D_SHADER_BACKEND_NONE)
300 ERR_(winediag)("The GLSL shader backend has been disabled. You get to keep all the pieces if it breaks.\n");
301 TRACE("Use of GL Shading Language disabled.\n");
303 if (!get_config_key(hkey, appkey, "OffscreenRenderingMode", buffer, size)
304 && !strcmp(buffer,"backbuffer"))
305 wined3d_settings.offscreen_rendering_mode = ORM_BACKBUFFER;
306 if ( !get_config_key_dword( hkey, appkey, "VideoPciDeviceID", &tmpvalue) )
308 int pci_device_id = tmpvalue;
310 /* A pci device id is 16-bit */
311 if(pci_device_id > 0xffff)
313 ERR("Invalid value for VideoPciDeviceID. The value should be smaller or equal to 65535 or 0xffff\n");
315 else
317 TRACE("Using PCI Device ID %04x\n", pci_device_id);
318 wined3d_settings.pci_device_id = pci_device_id;
321 if ( !get_config_key_dword( hkey, appkey, "VideoPciVendorID", &tmpvalue) )
323 int pci_vendor_id = tmpvalue;
325 /* A pci device id is 16-bit */
326 if(pci_vendor_id > 0xffff)
328 ERR("Invalid value for VideoPciVendorID. The value should be smaller or equal to 65535 or 0xffff\n");
330 else
332 TRACE("Using PCI Vendor ID %04x\n", pci_vendor_id);
333 wined3d_settings.pci_vendor_id = pci_vendor_id;
336 if ( !get_config_key( hkey, appkey, "VideoMemorySize", buffer, size) )
338 int TmpVideoMemorySize = atoi(buffer);
339 if(TmpVideoMemorySize > 0)
341 wined3d_settings.emulated_textureram = (UINT64)TmpVideoMemorySize *1024*1024;
342 TRACE("Use %iMiB = 0x%s bytes for emulated_textureram\n",
343 TmpVideoMemorySize,
344 wine_dbgstr_longlong(wined3d_settings.emulated_textureram));
346 else
347 ERR("VideoMemorySize is %i but must be >0\n", TmpVideoMemorySize);
349 if ( !get_config_key( hkey, appkey, "WineLogo", buffer, size) )
351 size_t len = strlen(buffer) + 1;
353 if (!(wined3d_settings.logo = heap_alloc(len)))
354 ERR("Failed to allocate logo path memory.\n");
355 else
356 memcpy(wined3d_settings.logo, buffer, len);
358 if (!get_config_key_dword(hkey, appkey, "MultisampleTextures", &wined3d_settings.multisample_textures))
359 ERR_(winediag)("Setting multisample textures to %#x.\n", wined3d_settings.multisample_textures);
360 if (!get_config_key_dword(hkey, appkey, "SampleCount", &wined3d_settings.sample_count))
361 ERR_(winediag)("Forcing sample count to %u. This may not be compatible with all applications.\n",
362 wined3d_settings.sample_count);
363 if (!get_config_key(hkey, appkey, "CheckFloatConstants", buffer, size)
364 && !strcmp(buffer, "enabled"))
366 TRACE("Checking relative addressing indices in float constants.\n");
367 wined3d_settings.check_float_constants = TRUE;
369 if (!get_config_key_dword(hkey, appkey, "strict_shader_math", &wined3d_settings.strict_shader_math))
370 ERR_(winediag)("Setting strict shader math to %#x.\n", wined3d_settings.strict_shader_math);
371 if (!get_config_key_dword(hkey, appkey, "MaxShaderModelVS", &wined3d_settings.max_sm_vs))
372 TRACE("Limiting VS shader model to %u.\n", wined3d_settings.max_sm_vs);
373 if (!get_config_key_dword(hkey, appkey, "MaxShaderModelHS", &wined3d_settings.max_sm_hs))
374 TRACE("Limiting HS shader model to %u.\n", wined3d_settings.max_sm_hs);
375 if (!get_config_key_dword(hkey, appkey, "MaxShaderModelDS", &wined3d_settings.max_sm_ds))
376 TRACE("Limiting DS shader model to %u.\n", wined3d_settings.max_sm_ds);
377 if (!get_config_key_dword(hkey, appkey, "MaxShaderModelGS", &wined3d_settings.max_sm_gs))
378 TRACE("Limiting GS shader model to %u.\n", wined3d_settings.max_sm_gs);
379 if (!get_config_key_dword(hkey, appkey, "MaxShaderModelPS", &wined3d_settings.max_sm_ps))
380 TRACE("Limiting PS shader model to %u.\n", wined3d_settings.max_sm_ps);
381 if (!get_config_key_dword(hkey, appkey, "MaxShaderModelCS", &wined3d_settings.max_sm_cs))
382 TRACE("Limiting CS shader model to %u.\n", wined3d_settings.max_sm_cs);
383 if (!get_config_key(hkey, appkey, "renderer", buffer, size))
385 if (!strcmp(buffer, "vulkan"))
387 ERR_(winediag)("Using the Vulkan renderer.\n");
388 wined3d_settings.renderer = WINED3D_RENDERER_VULKAN;
390 else if (!strcmp(buffer, "gl"))
392 ERR_(winediag)("Using the OpenGL renderer.\n");
393 wined3d_settings.renderer = WINED3D_RENDERER_OPENGL;
395 else if (!strcmp(buffer, "gdi") || !strcmp(buffer, "no3d"))
397 ERR_(winediag)("Disabling 3D support.\n");
398 wined3d_settings.renderer = WINED3D_RENDERER_NO3D;
403 if (appkey) RegCloseKey( appkey );
404 if (hkey) RegCloseKey( hkey );
406 return TRUE;
409 static BOOL wined3d_dll_destroy(HINSTANCE hInstDLL)
411 DWORD wined3d_context_tls_idx = context_get_tls_idx();
412 unsigned int i;
414 wined3d_spirv_shader_backend_cleanup();
416 if (!TlsFree(wined3d_context_tls_idx))
418 DWORD err = GetLastError();
419 ERR("Failed to free context TLS index, err %#x.\n", err);
422 for (i = 0; i < wndproc_table.count; ++i)
424 /* Trying to unregister these would be futile. These entries can only
425 * exist if either we skipped them in wined3d_unregister_window() due
426 * to the application replacing the wndproc after the entry was
427 * registered, or if the application still has an active wined3d
428 * device. In the latter case the application has bigger problems than
429 * these entries. */
430 WARN("Leftover wndproc table entry %p.\n", &wndproc_table.entries[i]);
432 heap_free(wndproc_table.entries);
434 heap_free(swapchain_state_table.states);
435 for (i = 0; i < swapchain_state_table.hook_count; ++i)
437 WARN("Leftover swapchain state hook %p.\n", &swapchain_state_table.hooks[i]);
438 UnhookWindowsHookEx(swapchain_state_table.hooks[i].hook);
440 heap_free(swapchain_state_table.hooks);
442 heap_free(wined3d_settings.logo);
443 UnregisterClassA(WINED3D_OPENGL_WINDOW_CLASS_NAME, hInstDLL);
445 DeleteCriticalSection(&wined3d_command_cs);
447 DeleteCriticalSection(&wined3d_wndproc_cs);
448 DeleteCriticalSection(&wined3d_cs);
449 return TRUE;
452 void WINAPI wined3d_mutex_lock(void)
454 EnterCriticalSection(&wined3d_cs);
457 void WINAPI wined3d_mutex_unlock(void)
459 LeaveCriticalSection(&wined3d_cs);
462 static void wined3d_wndproc_mutex_lock(void)
464 EnterCriticalSection(&wined3d_wndproc_cs);
467 static void wined3d_wndproc_mutex_unlock(void)
469 LeaveCriticalSection(&wined3d_wndproc_cs);
472 static struct wined3d_output * wined3d_get_output_from_window(const struct wined3d *wined3d,
473 HWND hwnd)
475 unsigned int adapter_idx, output_idx;
476 struct wined3d_adapter *adapter;
477 MONITORINFOEXW monitor_info;
478 HMONITOR monitor;
480 TRACE("wined3d %p, hwnd %p.\n", wined3d, hwnd);
482 monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
483 monitor_info.cbSize = sizeof(monitor_info);
484 if (!GetMonitorInfoW(monitor, (MONITORINFO *)&monitor_info))
486 ERR("GetMonitorInfoW failed, error %#x.\n", GetLastError());
487 return NULL;
490 for (adapter_idx = 0; adapter_idx < wined3d->adapter_count; ++adapter_idx)
492 adapter = wined3d->adapters[adapter_idx];
493 for (output_idx = 0; output_idx < adapter->output_count; ++output_idx)
495 if (!lstrcmpiW(adapter->outputs[output_idx].device_name, monitor_info.szDevice))
496 return &adapter->outputs[output_idx];
500 return NULL;
503 static struct wined3d_wndproc *wined3d_find_wndproc(HWND window, struct wined3d *wined3d)
505 unsigned int i;
507 for (i = 0; i < wndproc_table.count; ++i)
509 struct wined3d_wndproc *entry = &wndproc_table.entries[i];
511 if (entry->window == window && entry->wined3d == wined3d)
512 return entry;
515 return NULL;
518 BOOL wined3d_filter_messages(HWND window, BOOL filter)
520 struct wined3d_wndproc *entry;
521 BOOL ret;
523 wined3d_wndproc_mutex_lock();
525 if (!(entry = wined3d_find_wndproc(window, NULL)))
527 wined3d_wndproc_mutex_unlock();
528 return FALSE;
531 ret = entry->filter;
532 entry->filter = filter;
534 wined3d_wndproc_mutex_unlock();
536 return ret;
539 static LRESULT CALLBACK wined3d_wndproc(HWND window, UINT message, WPARAM wparam, LPARAM lparam)
541 struct wined3d_wndproc *entry;
542 struct wined3d_device *device;
543 BOOL unicode, filter;
544 WNDPROC proc;
546 wined3d_wndproc_mutex_lock();
548 if (!(entry = wined3d_find_wndproc(window, NULL)))
550 wined3d_wndproc_mutex_unlock();
551 ERR("Window %p is not registered with wined3d.\n", window);
552 return DefWindowProcW(window, message, wparam, lparam);
555 device = entry->device;
556 unicode = entry->unicode;
557 filter = entry->filter;
558 proc = entry->proc;
559 wined3d_wndproc_mutex_unlock();
561 if (device)
563 if (filter && message != WM_DISPLAYCHANGE)
565 TRACE("Filtering message: window %p, message %#x, wparam %#lx, lparam %#lx.\n",
566 window, message, wparam, lparam);
568 if (unicode)
569 return DefWindowProcW(window, message, wparam, lparam);
570 return DefWindowProcA(window, message, wparam, lparam);
573 return device_process_message(device, window, unicode, message, wparam, lparam, proc);
575 if (unicode)
576 return CallWindowProcW(proc, window, message, wparam, lparam);
577 return CallWindowProcA(proc, window, message, wparam, lparam);
580 static LRESULT CALLBACK wined3d_hook_proc(int code, WPARAM wparam, LPARAM lparam)
582 struct wined3d_swapchain_desc swapchain_desc;
583 struct wined3d_swapchain_state *state;
584 struct wined3d_wndproc *entry;
585 struct wined3d_output *output;
586 MSG *msg = (MSG *)lparam;
587 unsigned int i;
589 /* Handle Alt+Enter. */
590 if (code == HC_ACTION && msg->message == WM_SYSKEYDOWN
591 && msg->wParam == VK_RETURN && (msg->lParam & (KF_ALTDOWN << 16)))
593 wined3d_wndproc_mutex_lock();
595 for (i = 0; i < swapchain_state_table.state_count; ++i)
597 state = swapchain_state_table.states[i].state;
599 if (state->device_window != msg->hwnd)
600 continue;
602 if ((entry = wined3d_find_wndproc(msg->hwnd, state->wined3d))
603 && (entry->flags & (WINED3D_REGISTER_WINDOW_NO_WINDOW_CHANGES
604 | WINED3D_REGISTER_WINDOW_NO_ALT_ENTER)))
605 continue;
607 swapchain_desc = state->desc;
608 swapchain_desc.windowed = !swapchain_desc.windowed;
609 if (!(output = wined3d_get_output_from_window(state->wined3d, state->device_window)))
611 ERR("Failed to get output from window %p.\n", state->device_window);
612 break;
614 swapchain_desc.output = output;
615 wined3d_swapchain_state_set_fullscreen(state, &swapchain_desc, NULL);
617 wined3d_wndproc_mutex_unlock();
619 return 1;
622 wined3d_wndproc_mutex_unlock();
625 return CallNextHookEx(0, code, wparam, lparam);
628 BOOL CDECL wined3d_register_window(struct wined3d *wined3d, HWND window,
629 struct wined3d_device *device, unsigned int flags)
631 struct wined3d_wndproc *entry;
633 TRACE("wined3d %p, window %p, device %p, flags %#x.\n", wined3d, window, device, flags);
635 wined3d_wndproc_mutex_lock();
637 if ((entry = wined3d_find_wndproc(window, wined3d)))
639 if (!wined3d)
640 WARN("Window %p is already registered with wined3d.\n", window);
641 entry->flags = flags;
642 wined3d_wndproc_mutex_unlock();
643 return TRUE;
646 if (!wined3d_array_reserve((void **)&wndproc_table.entries, &wndproc_table.size,
647 wndproc_table.count + 1, sizeof(*entry)))
649 wined3d_wndproc_mutex_unlock();
650 ERR("Failed to grow table.\n");
651 return FALSE;
654 entry = &wndproc_table.entries[wndproc_table.count++];
655 entry->window = window;
656 entry->unicode = IsWindowUnicode(window);
657 if (!wined3d)
659 /* Set a window proc that matches the window. Some applications (e.g.
660 * NoX) replace the window proc after we've set ours, and expect to be
661 * able to call the previous one (ours) directly, without using
662 * CallWindowProc(). */
663 if (entry->unicode)
664 entry->proc = (WNDPROC)SetWindowLongPtrW(window, GWLP_WNDPROC, (LONG_PTR)wined3d_wndproc);
665 else
666 entry->proc = (WNDPROC)SetWindowLongPtrA(window, GWLP_WNDPROC, (LONG_PTR)wined3d_wndproc);
668 else
670 entry->proc = NULL;
672 entry->device = device;
673 entry->wined3d = wined3d;
674 entry->flags = flags;
676 wined3d_wndproc_mutex_unlock();
678 return TRUE;
681 static BOOL restore_wndproc(struct wined3d_wndproc *entry)
683 LONG_PTR proc;
685 if (entry->unicode)
687 proc = GetWindowLongPtrW(entry->window, GWLP_WNDPROC);
688 if (proc != (LONG_PTR)wined3d_wndproc)
689 return FALSE;
690 SetWindowLongPtrW(entry->window, GWLP_WNDPROC, (LONG_PTR)entry->proc);
692 else
694 proc = GetWindowLongPtrA(entry->window, GWLP_WNDPROC);
695 if (proc != (LONG_PTR)wined3d_wndproc)
696 return FALSE;
697 SetWindowLongPtrA(entry->window, GWLP_WNDPROC, (LONG_PTR)entry->proc);
700 return TRUE;
703 void wined3d_unregister_window(HWND window)
705 struct wined3d_wndproc *entry, *last;
707 wined3d_wndproc_mutex_lock();
709 if (!(entry = wined3d_find_wndproc(window, NULL)))
711 wined3d_wndproc_mutex_unlock();
712 ERR("Window %p is not registered with wined3d.\n", window);
713 return;
716 if (entry->proc && !restore_wndproc(entry))
718 entry->device = NULL;
719 WARN("Not unregistering window %p, current window proc doesn't match wined3d window proc.\n", window);
720 wined3d_wndproc_mutex_unlock();
721 return;
724 last = &wndproc_table.entries[--wndproc_table.count];
725 if (entry != last) *entry = *last;
727 wined3d_wndproc_mutex_unlock();
730 void CDECL wined3d_unregister_windows(struct wined3d *wined3d)
732 struct wined3d_wndproc *entry, *last;
733 unsigned int i = 0;
735 TRACE("wined3d %p.\n", wined3d);
737 wined3d_wndproc_mutex_lock();
739 while (i < wndproc_table.count)
741 entry = &wndproc_table.entries[i];
743 if (entry->wined3d != wined3d)
745 ++i;
746 continue;
749 if (entry->proc && !restore_wndproc(entry))
751 entry->device = NULL;
752 WARN("Not unregistering window %p, current window proc doesn't match wined3d window proc.\n",
753 entry->window);
754 ++i;
755 continue;
758 last = &wndproc_table.entries[--wndproc_table.count];
759 if (entry != last)
760 *entry = *last;
761 else
762 ++i;
765 wined3d_wndproc_mutex_unlock();
768 static struct wined3d_window_hook *wined3d_find_hook(DWORD thread_id)
770 unsigned int i;
772 for (i = 0; i < swapchain_state_table.hook_count; ++i)
774 if (swapchain_state_table.hooks[i].thread_id == thread_id)
775 return &swapchain_state_table.hooks[i];
778 return NULL;
781 void wined3d_swapchain_state_register(struct wined3d_swapchain_state *state)
783 struct wined3d_registered_swapchain_state *state_entry;
784 struct wined3d_window_hook *hook;
786 wined3d_wndproc_mutex_lock();
788 if (!wined3d_array_reserve((void **)&swapchain_state_table.states, &swapchain_state_table.states_size,
789 swapchain_state_table.state_count + 1, sizeof(*state_entry)))
791 wined3d_wndproc_mutex_unlock();
792 return;
795 state_entry = &swapchain_state_table.states[swapchain_state_table.state_count++];
796 state_entry->state = state;
797 state_entry->thread_id = GetWindowThreadProcessId(state->device_window, NULL);
799 if ((hook = wined3d_find_hook(state_entry->thread_id)))
801 ++hook->count;
802 wined3d_wndproc_mutex_unlock();
803 return;
806 if (!wined3d_array_reserve((void **)&swapchain_state_table.hooks, &swapchain_state_table.hooks_size,
807 swapchain_state_table.hook_count + 1, sizeof(*hook)))
809 --swapchain_state_table.state_count;
810 wined3d_wndproc_mutex_unlock();
811 return;
814 hook = &swapchain_state_table.hooks[swapchain_state_table.hook_count++];
815 hook->thread_id = state_entry->thread_id;
816 hook->hook = SetWindowsHookExW(WH_GETMESSAGE, wined3d_hook_proc, 0, hook->thread_id);
817 hook->count = 1;
819 wined3d_wndproc_mutex_unlock();
822 static void wined3d_swapchain_state_unregister(struct wined3d_swapchain_state *state)
824 struct wined3d_registered_swapchain_state *state_entry, *last_state_entry;
825 struct wined3d_window_hook *hook, *last_hook;
826 unsigned int i;
828 wined3d_wndproc_mutex_lock();
830 for (i = 0; i < swapchain_state_table.state_count; ++i)
832 state_entry = &swapchain_state_table.states[i];
834 if (state_entry->state != state)
835 continue;
837 if ((hook = wined3d_find_hook(state_entry->thread_id)) && !--hook->count)
839 UnhookWindowsHookEx(hook->hook);
840 last_hook = &swapchain_state_table.hooks[--swapchain_state_table.hook_count];
841 if (hook != last_hook)
842 *hook = *last_hook;
845 last_state_entry = &swapchain_state_table.states[--swapchain_state_table.state_count];
846 if (state_entry != last_state_entry)
847 *state_entry = *last_state_entry;
849 break;
852 wined3d_wndproc_mutex_unlock();
855 void wined3d_swapchain_state_cleanup(struct wined3d_swapchain_state *state)
857 wined3d_swapchain_state_unregister(state);
860 /* At process attach */
861 BOOL WINAPI DllMain(HINSTANCE inst, DWORD reason, void *reserved)
863 switch (reason)
865 case DLL_PROCESS_ATTACH:
866 return wined3d_dll_init(inst);
868 case DLL_PROCESS_DETACH:
869 if (!reserved)
870 return wined3d_dll_destroy(inst);
871 break;
873 case DLL_THREAD_DETACH:
874 if (!wined3d_context_gl_set_current(NULL))
876 ERR("Failed to clear current context.\n");
878 return TRUE;
880 return TRUE;