netapi32: Convert the Unix library to the __wine_unix_call interface.
[wine.git] / dlls / wined3d / wined3d_main.c
blob6e93b8dc683a6e5e16b2af6440cf83d6bce893c5
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 .cs_multithreaded = WINED3D_CSMT_ENABLE,
114 .max_gl_version = MAKEDWORD_VERSION(4, 4),
115 .offscreen_rendering_mode = ORM_FBO,
116 .pci_vendor_id = PCI_VENDOR_NONE,
117 .pci_device_id = PCI_DEVICE_NONE,
118 .multisample_textures = TRUE,
119 .sample_count = ~0u,
120 .max_sm_vs = UINT_MAX,
121 .max_sm_ps = UINT_MAX,
122 .max_sm_ds = UINT_MAX,
123 .max_sm_hs = UINT_MAX,
124 .max_sm_gs = UINT_MAX,
125 .max_sm_cs = UINT_MAX,
126 .renderer = WINED3D_RENDERER_AUTO,
127 .shader_backend = WINED3D_SHADER_BACKEND_AUTO,
130 struct wined3d * CDECL wined3d_create(DWORD flags)
132 struct wined3d *object;
133 HRESULT hr;
135 if (!(object = heap_alloc_zero(FIELD_OFFSET(struct wined3d, adapters[1]))))
137 ERR("Failed to allocate wined3d object memory.\n");
138 return NULL;
141 if (wined3d_settings.renderer == WINED3D_RENDERER_NO3D)
142 flags |= WINED3D_NO3D;
144 if (FAILED(hr = wined3d_init(object, flags)))
146 WARN("Failed to initialize wined3d object, hr %#x.\n", hr);
147 heap_free(object);
148 return NULL;
151 TRACE("Created wined3d object %p.\n", object);
153 return object;
156 static DWORD get_config_key(HKEY defkey, HKEY appkey, const char *name, char *buffer, DWORD size)
158 if (appkey && !RegQueryValueExA(appkey, name, 0, NULL, (BYTE *)buffer, &size)) return 0;
159 if (defkey && !RegQueryValueExA(defkey, name, 0, NULL, (BYTE *)buffer, &size)) return 0;
160 return ERROR_FILE_NOT_FOUND;
163 static DWORD get_config_key_dword(HKEY defkey, HKEY appkey, const char *name, DWORD *value)
165 DWORD type, data, size;
167 size = sizeof(data);
168 if (appkey && !RegQueryValueExA(appkey, name, 0, &type, (BYTE *)&data, &size) && type == REG_DWORD) goto success;
169 size = sizeof(data);
170 if (defkey && !RegQueryValueExA(defkey, name, 0, &type, (BYTE *)&data, &size) && type == REG_DWORD) goto success;
172 return ERROR_FILE_NOT_FOUND;
174 success:
175 *value = data;
176 return 0;
179 BOOL wined3d_get_app_name(char *app_name, unsigned int app_name_size)
181 char buffer[MAX_PATH];
182 unsigned int len;
183 char *p, *name;
185 len = GetModuleFileNameA(0, buffer, ARRAY_SIZE(buffer));
186 if (!(len && len < MAX_PATH))
187 return FALSE;
189 name = buffer;
190 if ((p = strrchr(name, '/' )))
191 name = p + 1;
192 if ((p = strrchr(name, '\\')))
193 name = p + 1;
195 len = strlen(name) + 1;
196 if (app_name_size < len)
197 return FALSE;
199 memcpy(app_name, name, len);
200 return TRUE;
203 static BOOL wined3d_dll_init(HINSTANCE hInstDLL)
205 DWORD wined3d_context_tls_idx;
206 char buffer[MAX_PATH+10];
207 DWORD size = sizeof(buffer);
208 HKEY hkey = 0;
209 HKEY appkey = 0;
210 DWORD tmpvalue;
211 WNDCLASSA wc;
213 wined3d_context_tls_idx = TlsAlloc();
214 if (wined3d_context_tls_idx == TLS_OUT_OF_INDEXES)
216 DWORD err = GetLastError();
217 ERR("Failed to allocate context TLS index, err %#x.\n", err);
218 return FALSE;
220 context_set_tls_idx(wined3d_context_tls_idx);
222 /* We need our own window class for a fake window which we use to retrieve GL capabilities */
223 /* We might need CS_OWNDC in the future if we notice strange things on Windows.
224 * Various articles/posts about OpenGL problems on Windows recommend this. */
225 wc.style = CS_HREDRAW | CS_VREDRAW;
226 wc.lpfnWndProc = DefWindowProcA;
227 wc.cbClsExtra = 0;
228 wc.cbWndExtra = 0;
229 wc.hInstance = hInstDLL;
230 wc.hIcon = LoadIconA(NULL, (const char *)IDI_WINLOGO);
231 wc.hCursor = LoadCursorA(NULL, (const char *)IDC_ARROW);
232 wc.hbrBackground = NULL;
233 wc.lpszMenuName = NULL;
234 wc.lpszClassName = WINED3D_OPENGL_WINDOW_CLASS_NAME;
236 if (!RegisterClassA(&wc))
238 ERR("Failed to register window class 'WineD3D_OpenGL'!\n");
239 if (!TlsFree(wined3d_context_tls_idx))
241 DWORD err = GetLastError();
242 ERR("Failed to free context TLS index, err %#x.\n", err);
244 return FALSE;
247 DisableThreadLibraryCalls(hInstDLL);
249 /* @@ Wine registry key: HKCU\Software\Wine\Direct3D */
250 if ( RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Direct3D", &hkey ) ) hkey = 0;
252 if (wined3d_get_app_name(buffer, ARRAY_SIZE(buffer)))
254 HKEY tmpkey;
255 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\Direct3D */
256 if (!RegOpenKeyA(HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmpkey))
258 strcat(buffer, "\\Direct3D");
259 TRACE("Application name %s.\n", buffer);
260 if (RegOpenKeyA(tmpkey, buffer, &appkey)) appkey = 0;
261 RegCloseKey(tmpkey);
265 if (hkey || appkey)
267 if (!get_config_key_dword(hkey, appkey, "csmt", &wined3d_settings.cs_multithreaded))
268 ERR_(winediag)("Setting multithreaded command stream to %#x.\n", wined3d_settings.cs_multithreaded);
269 if (!get_config_key_dword(hkey, appkey, "MaxVersionGL", &tmpvalue))
271 ERR_(winediag)("Setting maximum allowed wined3d GL version to %u.%u.\n",
272 tmpvalue >> 16, tmpvalue & 0xffff);
273 wined3d_settings.max_gl_version = tmpvalue;
275 if (!get_config_key(hkey, appkey, "shader_backend", buffer, size))
277 if (!_strnicmp(buffer, "glsl", -1))
279 ERR_(winediag)("Using the GLSL shader backend.\n");
280 wined3d_settings.shader_backend = WINED3D_SHADER_BACKEND_GLSL;
282 else if (!_strnicmp(buffer, "arb", -1))
284 ERR_(winediag)("Using the ARB shader backend.\n");
285 wined3d_settings.shader_backend = WINED3D_SHADER_BACKEND_ARB;
287 else if (!_strnicmp(buffer, "none", -1))
289 ERR_(winediag)("Disabling shader backends.\n");
290 wined3d_settings.shader_backend = WINED3D_SHADER_BACKEND_NONE;
293 if (wined3d_settings.shader_backend == WINED3D_SHADER_BACKEND_ARB
294 || wined3d_settings.shader_backend == WINED3D_SHADER_BACKEND_NONE)
296 ERR_(winediag)("The GLSL shader backend has been disabled. You get to keep all the pieces if it breaks.\n");
297 TRACE("Use of GL Shading Language disabled.\n");
299 if (!get_config_key(hkey, appkey, "OffscreenRenderingMode", buffer, size)
300 && !strcmp(buffer,"backbuffer"))
301 wined3d_settings.offscreen_rendering_mode = ORM_BACKBUFFER;
302 if ( !get_config_key_dword( hkey, appkey, "VideoPciDeviceID", &tmpvalue) )
304 int pci_device_id = tmpvalue;
306 /* A pci device id is 16-bit */
307 if(pci_device_id > 0xffff)
309 ERR("Invalid value for VideoPciDeviceID. The value should be smaller or equal to 65535 or 0xffff\n");
311 else
313 TRACE("Using PCI Device ID %04x\n", pci_device_id);
314 wined3d_settings.pci_device_id = pci_device_id;
317 if ( !get_config_key_dword( hkey, appkey, "VideoPciVendorID", &tmpvalue) )
319 int pci_vendor_id = tmpvalue;
321 /* A pci device id is 16-bit */
322 if(pci_vendor_id > 0xffff)
324 ERR("Invalid value for VideoPciVendorID. The value should be smaller or equal to 65535 or 0xffff\n");
326 else
328 TRACE("Using PCI Vendor ID %04x\n", pci_vendor_id);
329 wined3d_settings.pci_vendor_id = pci_vendor_id;
332 if ( !get_config_key( hkey, appkey, "VideoMemorySize", buffer, size) )
334 int TmpVideoMemorySize = atoi(buffer);
335 if(TmpVideoMemorySize > 0)
337 wined3d_settings.emulated_textureram = (UINT64)TmpVideoMemorySize *1024*1024;
338 TRACE("Use %iMiB = 0x%s bytes for emulated_textureram\n",
339 TmpVideoMemorySize,
340 wine_dbgstr_longlong(wined3d_settings.emulated_textureram));
342 else
343 ERR("VideoMemorySize is %i but must be >0\n", TmpVideoMemorySize);
345 if ( !get_config_key( hkey, appkey, "WineLogo", buffer, size) )
347 size_t len = strlen(buffer) + 1;
349 if (!(wined3d_settings.logo = heap_alloc(len)))
350 ERR("Failed to allocate logo path memory.\n");
351 else
352 memcpy(wined3d_settings.logo, buffer, len);
354 if (!get_config_key_dword(hkey, appkey, "MultisampleTextures", &wined3d_settings.multisample_textures))
355 ERR_(winediag)("Setting multisample textures to %#x.\n", wined3d_settings.multisample_textures);
356 if (!get_config_key_dword(hkey, appkey, "SampleCount", &wined3d_settings.sample_count))
357 ERR_(winediag)("Forcing sample count to %u. This may not be compatible with all applications.\n",
358 wined3d_settings.sample_count);
359 if (!get_config_key(hkey, appkey, "CheckFloatConstants", buffer, size)
360 && !strcmp(buffer, "enabled"))
362 TRACE("Checking relative addressing indices in float constants.\n");
363 wined3d_settings.check_float_constants = TRUE;
365 if (!get_config_key_dword(hkey, appkey, "strict_shader_math", &wined3d_settings.strict_shader_math))
366 ERR_(winediag)("Setting strict shader math to %#x.\n", wined3d_settings.strict_shader_math);
367 if (!get_config_key_dword(hkey, appkey, "MaxShaderModelVS", &wined3d_settings.max_sm_vs))
368 TRACE("Limiting VS shader model to %u.\n", wined3d_settings.max_sm_vs);
369 if (!get_config_key_dword(hkey, appkey, "MaxShaderModelHS", &wined3d_settings.max_sm_hs))
370 TRACE("Limiting HS shader model to %u.\n", wined3d_settings.max_sm_hs);
371 if (!get_config_key_dword(hkey, appkey, "MaxShaderModelDS", &wined3d_settings.max_sm_ds))
372 TRACE("Limiting DS shader model to %u.\n", wined3d_settings.max_sm_ds);
373 if (!get_config_key_dword(hkey, appkey, "MaxShaderModelGS", &wined3d_settings.max_sm_gs))
374 TRACE("Limiting GS shader model to %u.\n", wined3d_settings.max_sm_gs);
375 if (!get_config_key_dword(hkey, appkey, "MaxShaderModelPS", &wined3d_settings.max_sm_ps))
376 TRACE("Limiting PS shader model to %u.\n", wined3d_settings.max_sm_ps);
377 if (!get_config_key_dword(hkey, appkey, "MaxShaderModelCS", &wined3d_settings.max_sm_cs))
378 TRACE("Limiting CS shader model to %u.\n", wined3d_settings.max_sm_cs);
379 if (!get_config_key(hkey, appkey, "renderer", buffer, size))
381 if (!strcmp(buffer, "vulkan"))
383 ERR_(winediag)("Using the Vulkan renderer.\n");
384 wined3d_settings.renderer = WINED3D_RENDERER_VULKAN;
386 else if (!strcmp(buffer, "gl"))
388 ERR_(winediag)("Using the OpenGL renderer.\n");
389 wined3d_settings.renderer = WINED3D_RENDERER_OPENGL;
391 else if (!strcmp(buffer, "gdi") || !strcmp(buffer, "no3d"))
393 ERR_(winediag)("Disabling 3D support.\n");
394 wined3d_settings.renderer = WINED3D_RENDERER_NO3D;
399 if (appkey) RegCloseKey( appkey );
400 if (hkey) RegCloseKey( hkey );
402 return TRUE;
405 static BOOL wined3d_dll_destroy(HINSTANCE hInstDLL)
407 DWORD wined3d_context_tls_idx = context_get_tls_idx();
408 unsigned int i;
410 wined3d_spirv_shader_backend_cleanup();
412 if (!TlsFree(wined3d_context_tls_idx))
414 DWORD err = GetLastError();
415 ERR("Failed to free context TLS index, err %#x.\n", err);
418 for (i = 0; i < wndproc_table.count; ++i)
420 /* Trying to unregister these would be futile. These entries can only
421 * exist if either we skipped them in wined3d_unregister_window() due
422 * to the application replacing the wndproc after the entry was
423 * registered, or if the application still has an active wined3d
424 * device. In the latter case the application has bigger problems than
425 * these entries. */
426 WARN("Leftover wndproc table entry %p.\n", &wndproc_table.entries[i]);
428 heap_free(wndproc_table.entries);
430 heap_free(swapchain_state_table.states);
431 for (i = 0; i < swapchain_state_table.hook_count; ++i)
433 WARN("Leftover swapchain state hook %p.\n", &swapchain_state_table.hooks[i]);
434 UnhookWindowsHookEx(swapchain_state_table.hooks[i].hook);
436 heap_free(swapchain_state_table.hooks);
438 heap_free(wined3d_settings.logo);
439 UnregisterClassA(WINED3D_OPENGL_WINDOW_CLASS_NAME, hInstDLL);
441 DeleteCriticalSection(&wined3d_command_cs);
443 DeleteCriticalSection(&wined3d_wndproc_cs);
444 DeleteCriticalSection(&wined3d_cs);
445 return TRUE;
448 void WINAPI wined3d_mutex_lock(void)
450 EnterCriticalSection(&wined3d_cs);
453 void WINAPI wined3d_mutex_unlock(void)
455 LeaveCriticalSection(&wined3d_cs);
458 static void wined3d_wndproc_mutex_lock(void)
460 EnterCriticalSection(&wined3d_wndproc_cs);
463 static void wined3d_wndproc_mutex_unlock(void)
465 LeaveCriticalSection(&wined3d_wndproc_cs);
468 static struct wined3d_output * wined3d_get_output_from_window(const struct wined3d *wined3d,
469 HWND hwnd)
471 unsigned int adapter_idx, output_idx;
472 struct wined3d_adapter *adapter;
473 MONITORINFOEXW monitor_info;
474 HMONITOR monitor;
476 TRACE("wined3d %p, hwnd %p.\n", wined3d, hwnd);
478 monitor = MonitorFromWindow(hwnd, MONITOR_DEFAULTTONEAREST);
479 monitor_info.cbSize = sizeof(monitor_info);
480 if (!GetMonitorInfoW(monitor, (MONITORINFO *)&monitor_info))
482 ERR("GetMonitorInfoW failed, error %#x.\n", GetLastError());
483 return NULL;
486 for (adapter_idx = 0; adapter_idx < wined3d->adapter_count; ++adapter_idx)
488 adapter = wined3d->adapters[adapter_idx];
489 for (output_idx = 0; output_idx < adapter->output_count; ++output_idx)
491 if (!lstrcmpiW(adapter->outputs[output_idx].device_name, monitor_info.szDevice))
492 return &adapter->outputs[output_idx];
496 return NULL;
499 static struct wined3d_wndproc *wined3d_find_wndproc(HWND window, struct wined3d *wined3d)
501 unsigned int i;
503 for (i = 0; i < wndproc_table.count; ++i)
505 struct wined3d_wndproc *entry = &wndproc_table.entries[i];
507 if (entry->window == window && entry->wined3d == wined3d)
508 return entry;
511 return NULL;
514 BOOL wined3d_filter_messages(HWND window, BOOL filter)
516 struct wined3d_wndproc *entry;
517 BOOL ret;
519 wined3d_wndproc_mutex_lock();
521 if (!(entry = wined3d_find_wndproc(window, NULL)))
523 wined3d_wndproc_mutex_unlock();
524 return FALSE;
527 ret = entry->filter;
528 entry->filter = filter;
530 wined3d_wndproc_mutex_unlock();
532 return ret;
535 static LRESULT CALLBACK wined3d_wndproc(HWND window, UINT message, WPARAM wparam, LPARAM lparam)
537 struct wined3d_wndproc *entry;
538 struct wined3d_device *device;
539 BOOL unicode, filter;
540 WNDPROC proc;
542 wined3d_wndproc_mutex_lock();
544 if (!(entry = wined3d_find_wndproc(window, NULL)))
546 wined3d_wndproc_mutex_unlock();
547 ERR("Window %p is not registered with wined3d.\n", window);
548 return DefWindowProcW(window, message, wparam, lparam);
551 device = entry->device;
552 unicode = entry->unicode;
553 filter = entry->filter;
554 proc = entry->proc;
555 wined3d_wndproc_mutex_unlock();
557 if (device)
559 if (filter && message != WM_DISPLAYCHANGE)
561 TRACE("Filtering message: window %p, message %#x, wparam %#lx, lparam %#lx.\n",
562 window, message, wparam, lparam);
564 if (unicode)
565 return DefWindowProcW(window, message, wparam, lparam);
566 return DefWindowProcA(window, message, wparam, lparam);
569 return device_process_message(device, window, unicode, message, wparam, lparam, proc);
571 if (unicode)
572 return CallWindowProcW(proc, window, message, wparam, lparam);
573 return CallWindowProcA(proc, window, message, wparam, lparam);
576 static LRESULT CALLBACK wined3d_hook_proc(int code, WPARAM wparam, LPARAM lparam)
578 struct wined3d_swapchain_desc swapchain_desc;
579 struct wined3d_swapchain_state *state;
580 struct wined3d_wndproc *entry;
581 struct wined3d_output *output;
582 MSG *msg = (MSG *)lparam;
583 unsigned int i;
585 /* Handle Alt+Enter. */
586 if (code == HC_ACTION && msg->message == WM_SYSKEYDOWN
587 && msg->wParam == VK_RETURN && (msg->lParam & (KF_ALTDOWN << 16)))
589 wined3d_wndproc_mutex_lock();
591 for (i = 0; i < swapchain_state_table.state_count; ++i)
593 state = swapchain_state_table.states[i].state;
595 if (state->device_window != msg->hwnd)
596 continue;
598 if ((entry = wined3d_find_wndproc(msg->hwnd, state->wined3d))
599 && (entry->flags & (WINED3D_REGISTER_WINDOW_NO_WINDOW_CHANGES
600 | WINED3D_REGISTER_WINDOW_NO_ALT_ENTER)))
601 continue;
603 swapchain_desc = state->desc;
604 swapchain_desc.windowed = !swapchain_desc.windowed;
605 if (!(output = wined3d_get_output_from_window(state->wined3d, state->device_window)))
607 ERR("Failed to get output from window %p.\n", state->device_window);
608 break;
610 swapchain_desc.output = output;
611 wined3d_swapchain_state_set_fullscreen(state, &swapchain_desc, NULL);
613 wined3d_wndproc_mutex_unlock();
615 return 1;
618 wined3d_wndproc_mutex_unlock();
621 return CallNextHookEx(0, code, wparam, lparam);
624 BOOL CDECL wined3d_register_window(struct wined3d *wined3d, HWND window,
625 struct wined3d_device *device, unsigned int flags)
627 struct wined3d_wndproc *entry;
629 TRACE("wined3d %p, window %p, device %p, flags %#x.\n", wined3d, window, device, flags);
631 wined3d_wndproc_mutex_lock();
633 if ((entry = wined3d_find_wndproc(window, wined3d)))
635 if (!wined3d)
636 WARN("Window %p is already registered with wined3d.\n", window);
637 entry->flags = flags;
638 wined3d_wndproc_mutex_unlock();
639 return TRUE;
642 if (!wined3d_array_reserve((void **)&wndproc_table.entries, &wndproc_table.size,
643 wndproc_table.count + 1, sizeof(*entry)))
645 wined3d_wndproc_mutex_unlock();
646 ERR("Failed to grow table.\n");
647 return FALSE;
650 entry = &wndproc_table.entries[wndproc_table.count++];
651 entry->window = window;
652 entry->unicode = IsWindowUnicode(window);
653 if (!wined3d)
655 /* Set a window proc that matches the window. Some applications (e.g.
656 * NoX) replace the window proc after we've set ours, and expect to be
657 * able to call the previous one (ours) directly, without using
658 * CallWindowProc(). */
659 if (entry->unicode)
660 entry->proc = (WNDPROC)SetWindowLongPtrW(window, GWLP_WNDPROC, (LONG_PTR)wined3d_wndproc);
661 else
662 entry->proc = (WNDPROC)SetWindowLongPtrA(window, GWLP_WNDPROC, (LONG_PTR)wined3d_wndproc);
664 else
666 entry->proc = NULL;
668 entry->device = device;
669 entry->wined3d = wined3d;
670 entry->flags = flags;
672 wined3d_wndproc_mutex_unlock();
674 return TRUE;
677 static BOOL restore_wndproc(struct wined3d_wndproc *entry)
679 LONG_PTR proc;
681 if (entry->unicode)
683 proc = GetWindowLongPtrW(entry->window, GWLP_WNDPROC);
684 if (proc != (LONG_PTR)wined3d_wndproc)
685 return FALSE;
686 SetWindowLongPtrW(entry->window, GWLP_WNDPROC, (LONG_PTR)entry->proc);
688 else
690 proc = GetWindowLongPtrA(entry->window, GWLP_WNDPROC);
691 if (proc != (LONG_PTR)wined3d_wndproc)
692 return FALSE;
693 SetWindowLongPtrA(entry->window, GWLP_WNDPROC, (LONG_PTR)entry->proc);
696 return TRUE;
699 void wined3d_unregister_window(HWND window)
701 struct wined3d_wndproc *entry, *last;
703 wined3d_wndproc_mutex_lock();
705 if (!(entry = wined3d_find_wndproc(window, NULL)))
707 wined3d_wndproc_mutex_unlock();
708 ERR("Window %p is not registered with wined3d.\n", window);
709 return;
712 if (entry->proc && !restore_wndproc(entry))
714 entry->device = NULL;
715 WARN("Not unregistering window %p, current window proc doesn't match wined3d window proc.\n", window);
716 wined3d_wndproc_mutex_unlock();
717 return;
720 last = &wndproc_table.entries[--wndproc_table.count];
721 if (entry != last) *entry = *last;
723 wined3d_wndproc_mutex_unlock();
726 void CDECL wined3d_unregister_windows(struct wined3d *wined3d)
728 struct wined3d_wndproc *entry, *last;
729 unsigned int i = 0;
731 TRACE("wined3d %p.\n", wined3d);
733 wined3d_wndproc_mutex_lock();
735 while (i < wndproc_table.count)
737 entry = &wndproc_table.entries[i];
739 if (entry->wined3d != wined3d)
741 ++i;
742 continue;
745 if (entry->proc && !restore_wndproc(entry))
747 entry->device = NULL;
748 WARN("Not unregistering window %p, current window proc doesn't match wined3d window proc.\n",
749 entry->window);
750 ++i;
751 continue;
754 last = &wndproc_table.entries[--wndproc_table.count];
755 if (entry != last)
756 *entry = *last;
757 else
758 ++i;
761 wined3d_wndproc_mutex_unlock();
764 static struct wined3d_window_hook *wined3d_find_hook(DWORD thread_id)
766 unsigned int i;
768 for (i = 0; i < swapchain_state_table.hook_count; ++i)
770 if (swapchain_state_table.hooks[i].thread_id == thread_id)
771 return &swapchain_state_table.hooks[i];
774 return NULL;
777 void wined3d_swapchain_state_register(struct wined3d_swapchain_state *state)
779 struct wined3d_registered_swapchain_state *state_entry;
780 struct wined3d_window_hook *hook;
782 wined3d_wndproc_mutex_lock();
784 if (!wined3d_array_reserve((void **)&swapchain_state_table.states, &swapchain_state_table.states_size,
785 swapchain_state_table.state_count + 1, sizeof(*state_entry)))
787 wined3d_wndproc_mutex_unlock();
788 return;
791 state_entry = &swapchain_state_table.states[swapchain_state_table.state_count++];
792 state_entry->state = state;
793 state_entry->thread_id = GetWindowThreadProcessId(state->device_window, NULL);
795 if ((hook = wined3d_find_hook(state_entry->thread_id)))
797 ++hook->count;
798 wined3d_wndproc_mutex_unlock();
799 return;
802 if (!wined3d_array_reserve((void **)&swapchain_state_table.hooks, &swapchain_state_table.hooks_size,
803 swapchain_state_table.hook_count + 1, sizeof(*hook)))
805 --swapchain_state_table.state_count;
806 wined3d_wndproc_mutex_unlock();
807 return;
810 hook = &swapchain_state_table.hooks[swapchain_state_table.hook_count++];
811 hook->thread_id = state_entry->thread_id;
812 hook->hook = SetWindowsHookExW(WH_GETMESSAGE, wined3d_hook_proc, 0, hook->thread_id);
813 hook->count = 1;
815 wined3d_wndproc_mutex_unlock();
818 static void wined3d_swapchain_state_unregister(struct wined3d_swapchain_state *state)
820 struct wined3d_registered_swapchain_state *state_entry, *last_state_entry;
821 struct wined3d_window_hook *hook, *last_hook;
822 unsigned int i;
824 wined3d_wndproc_mutex_lock();
826 for (i = 0; i < swapchain_state_table.state_count; ++i)
828 state_entry = &swapchain_state_table.states[i];
830 if (state_entry->state != state)
831 continue;
833 if ((hook = wined3d_find_hook(state_entry->thread_id)) && !--hook->count)
835 UnhookWindowsHookEx(hook->hook);
836 last_hook = &swapchain_state_table.hooks[--swapchain_state_table.hook_count];
837 if (hook != last_hook)
838 *hook = *last_hook;
841 last_state_entry = &swapchain_state_table.states[--swapchain_state_table.state_count];
842 if (state_entry != last_state_entry)
843 *state_entry = *last_state_entry;
845 break;
848 wined3d_wndproc_mutex_unlock();
851 void wined3d_swapchain_state_cleanup(struct wined3d_swapchain_state *state)
853 wined3d_swapchain_state_unregister(state);
856 /* At process attach */
857 BOOL WINAPI DllMain(HINSTANCE inst, DWORD reason, void *reserved)
859 switch (reason)
861 case DLL_PROCESS_ATTACH:
862 return wined3d_dll_init(inst);
864 case DLL_PROCESS_DETACH:
865 if (!reserved)
866 return wined3d_dll_destroy(inst);
867 break;
869 case DLL_THREAD_DETACH:
870 if (!wined3d_context_gl_set_current(NULL))
872 ERR("Failed to clear current context.\n");
874 return TRUE;
876 return TRUE;