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
28 #include "wined3d_private.h"
30 WINE_DEFAULT_DEBUG_CHANNEL(d3d
);
31 WINE_DECLARE_DEBUG_CHANNEL(winediag
);
33 struct wined3d_wndproc
35 struct wined3d
*wined3d
;
40 struct wined3d_device
*device
;
44 struct wined3d_wndproc_table
46 struct wined3d_wndproc
*entries
;
51 struct wined3d_window_hook
58 struct wined3d_registered_swapchain_state
60 struct wined3d_swapchain_state
*state
;
64 struct wined3d_swapchain_state_table
66 struct wined3d_window_hook
*hooks
;
70 struct wined3d_registered_swapchain_state
*states
;
75 static struct wined3d_wndproc_table wndproc_table
;
76 static struct wined3d_swapchain_state_table swapchain_state_table
;
78 static CRITICAL_SECTION wined3d_cs
;
79 static CRITICAL_SECTION_DEBUG wined3d_cs_debug
=
82 {&wined3d_cs_debug
.ProcessLocksList
,
83 &wined3d_cs_debug
.ProcessLocksList
},
84 0, 0, {(DWORD_PTR
)(__FILE__
": wined3d_cs")}
86 static CRITICAL_SECTION wined3d_cs
= {&wined3d_cs_debug
, -1, 0, 0, 0, 0};
88 static CRITICAL_SECTION wined3d_wndproc_cs
;
89 static CRITICAL_SECTION_DEBUG wined3d_wndproc_cs_debug
=
91 0, 0, &wined3d_wndproc_cs
,
92 {&wined3d_wndproc_cs_debug
.ProcessLocksList
,
93 &wined3d_wndproc_cs_debug
.ProcessLocksList
},
94 0, 0, {(DWORD_PTR
)(__FILE__
": wined3d_wndproc_cs")}
96 static CRITICAL_SECTION wined3d_wndproc_cs
= {&wined3d_wndproc_cs_debug
, -1, 0, 0, 0, 0};
98 CRITICAL_SECTION wined3d_command_cs
;
99 static CRITICAL_SECTION_DEBUG wined3d_command_cs_debug
=
101 0, 0, &wined3d_command_cs
,
102 {&wined3d_command_cs_debug
.ProcessLocksList
,
103 &wined3d_command_cs_debug
.ProcessLocksList
},
104 0, 0, {(DWORD_PTR
)(__FILE__
": wined3d_command_cs")}
106 CRITICAL_SECTION wined3d_command_cs
= {&wined3d_command_cs_debug
, -1, 0, 0, 0, 0};
108 /* When updating default value here, make sure to update winecfg as well,
109 * where appropriate. */
110 struct wined3d_settings wined3d_settings
=
112 .cs_multithreaded
= WINED3D_CSMT_ENABLE
,
113 .max_gl_version
= MAKEDWORD_VERSION(4, 4),
114 .offscreen_rendering_mode
= ORM_FBO
,
115 .pci_vendor_id
= PCI_VENDOR_NONE
,
116 .pci_device_id
= PCI_DEVICE_NONE
,
117 .multisample_textures
= TRUE
,
119 .max_sm_vs
= UINT_MAX
,
120 .max_sm_ps
= UINT_MAX
,
121 .max_sm_ds
= UINT_MAX
,
122 .max_sm_hs
= UINT_MAX
,
123 .max_sm_gs
= UINT_MAX
,
124 .max_sm_cs
= UINT_MAX
,
125 .renderer
= WINED3D_RENDERER_AUTO
,
126 .shader_backend
= WINED3D_SHADER_BACKEND_AUTO
,
129 struct wined3d
* CDECL
wined3d_create(DWORD flags
)
131 struct wined3d
*object
;
134 if (!(object
= heap_alloc_zero(FIELD_OFFSET(struct wined3d
, adapters
[1]))))
136 ERR("Failed to allocate wined3d object memory.\n");
140 if (wined3d_settings
.renderer
== WINED3D_RENDERER_NO3D
)
141 flags
|= WINED3D_NO3D
;
143 if (FAILED(hr
= wined3d_init(object
, flags
)))
145 WARN("Failed to initialize wined3d object, hr %#x.\n", hr
);
150 TRACE("Created wined3d object %p.\n", object
);
155 static DWORD
get_config_key(HKEY defkey
, HKEY appkey
, const char *name
, char *buffer
, DWORD size
)
157 if (appkey
&& !RegQueryValueExA(appkey
, name
, 0, NULL
, (BYTE
*)buffer
, &size
)) return 0;
158 if (defkey
&& !RegQueryValueExA(defkey
, name
, 0, NULL
, (BYTE
*)buffer
, &size
)) return 0;
159 return ERROR_FILE_NOT_FOUND
;
162 static DWORD
get_config_key_dword(HKEY defkey
, HKEY appkey
, const char *name
, DWORD
*value
)
164 DWORD type
, data
, size
;
167 if (appkey
&& !RegQueryValueExA(appkey
, name
, 0, &type
, (BYTE
*)&data
, &size
) && type
== REG_DWORD
) goto success
;
169 if (defkey
&& !RegQueryValueExA(defkey
, name
, 0, &type
, (BYTE
*)&data
, &size
) && type
== REG_DWORD
) goto success
;
171 return ERROR_FILE_NOT_FOUND
;
178 BOOL
wined3d_get_app_name(char *app_name
, unsigned int app_name_size
)
180 char buffer
[MAX_PATH
];
184 len
= GetModuleFileNameA(0, buffer
, ARRAY_SIZE(buffer
));
185 if (!(len
&& len
< MAX_PATH
))
189 if ((p
= strrchr(name
, '/' )))
191 if ((p
= strrchr(name
, '\\')))
194 len
= strlen(name
) + 1;
195 if (app_name_size
< len
)
198 memcpy(app_name
, name
, len
);
202 static BOOL
wined3d_dll_init(HINSTANCE hInstDLL
)
204 DWORD wined3d_context_tls_idx
;
205 char buffer
[MAX_PATH
+10];
206 DWORD size
= sizeof(buffer
);
212 wined3d_context_tls_idx
= TlsAlloc();
213 if (wined3d_context_tls_idx
== TLS_OUT_OF_INDEXES
)
215 DWORD err
= GetLastError();
216 ERR("Failed to allocate context TLS index, err %#x.\n", err
);
219 context_set_tls_idx(wined3d_context_tls_idx
);
221 /* We need our own window class for a fake window which we use to retrieve GL capabilities */
222 /* We might need CS_OWNDC in the future if we notice strange things on Windows.
223 * Various articles/posts about OpenGL problems on Windows recommend this. */
224 wc
.style
= CS_HREDRAW
| CS_VREDRAW
;
225 wc
.lpfnWndProc
= DefWindowProcA
;
228 wc
.hInstance
= hInstDLL
;
229 wc
.hIcon
= LoadIconA(NULL
, (const char *)IDI_WINLOGO
);
230 wc
.hCursor
= LoadCursorA(NULL
, (const char *)IDC_ARROW
);
231 wc
.hbrBackground
= NULL
;
232 wc
.lpszMenuName
= NULL
;
233 wc
.lpszClassName
= WINED3D_OPENGL_WINDOW_CLASS_NAME
;
235 if (!RegisterClassA(&wc
))
237 ERR("Failed to register window class 'WineD3D_OpenGL'!\n");
238 if (!TlsFree(wined3d_context_tls_idx
))
240 DWORD err
= GetLastError();
241 ERR("Failed to free context TLS index, err %#x.\n", err
);
246 DisableThreadLibraryCalls(hInstDLL
);
248 /* @@ Wine registry key: HKCU\Software\Wine\Direct3D */
249 if ( RegOpenKeyA( HKEY_CURRENT_USER
, "Software\\Wine\\Direct3D", &hkey
) ) hkey
= 0;
251 if (wined3d_get_app_name(buffer
, ARRAY_SIZE(buffer
)))
254 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\Direct3D */
255 if (!RegOpenKeyA(HKEY_CURRENT_USER
, "Software\\Wine\\AppDefaults", &tmpkey
))
257 strcat(buffer
, "\\Direct3D");
258 TRACE("Application name %s.\n", buffer
);
259 if (RegOpenKeyA(tmpkey
, buffer
, &appkey
)) appkey
= 0;
266 if (!get_config_key_dword(hkey
, appkey
, "csmt", &wined3d_settings
.cs_multithreaded
))
267 ERR_(winediag
)("Setting multithreaded command stream to %#x.\n", wined3d_settings
.cs_multithreaded
);
268 if (!get_config_key_dword(hkey
, appkey
, "MaxVersionGL", &tmpvalue
))
270 ERR_(winediag
)("Setting maximum allowed wined3d GL version to %u.%u.\n",
271 tmpvalue
>> 16, tmpvalue
& 0xffff);
272 wined3d_settings
.max_gl_version
= tmpvalue
;
274 if (!get_config_key(hkey
, appkey
, "shader_backend", buffer
, size
))
276 if (!_strnicmp(buffer
, "glsl", -1))
278 ERR_(winediag
)("Using the GLSL shader backend.\n");
279 wined3d_settings
.shader_backend
= WINED3D_SHADER_BACKEND_GLSL
;
281 else if (!_strnicmp(buffer
, "arb", -1))
283 ERR_(winediag
)("Using the ARB shader backend.\n");
284 wined3d_settings
.shader_backend
= WINED3D_SHADER_BACKEND_ARB
;
286 else if (!_strnicmp(buffer
, "none", -1))
288 ERR_(winediag
)("Disabling shader backends.\n");
289 wined3d_settings
.shader_backend
= WINED3D_SHADER_BACKEND_NONE
;
292 if (wined3d_settings
.shader_backend
== WINED3D_SHADER_BACKEND_ARB
293 || wined3d_settings
.shader_backend
== WINED3D_SHADER_BACKEND_NONE
)
295 ERR_(winediag
)("The GLSL shader backend has been disabled. You get to keep all the pieces if it breaks.\n");
296 TRACE("Use of GL Shading Language disabled.\n");
298 if (!get_config_key(hkey
, appkey
, "OffscreenRenderingMode", buffer
, size
)
299 && !strcmp(buffer
,"backbuffer"))
300 wined3d_settings
.offscreen_rendering_mode
= ORM_BACKBUFFER
;
301 if ( !get_config_key_dword( hkey
, appkey
, "VideoPciDeviceID", &tmpvalue
) )
303 int pci_device_id
= tmpvalue
;
305 /* A pci device id is 16-bit */
306 if(pci_device_id
> 0xffff)
308 ERR("Invalid value for VideoPciDeviceID. The value should be smaller or equal to 65535 or 0xffff\n");
312 TRACE("Using PCI Device ID %04x\n", pci_device_id
);
313 wined3d_settings
.pci_device_id
= pci_device_id
;
316 if ( !get_config_key_dword( hkey
, appkey
, "VideoPciVendorID", &tmpvalue
) )
318 int pci_vendor_id
= tmpvalue
;
320 /* A pci device id is 16-bit */
321 if(pci_vendor_id
> 0xffff)
323 ERR("Invalid value for VideoPciVendorID. The value should be smaller or equal to 65535 or 0xffff\n");
327 TRACE("Using PCI Vendor ID %04x\n", pci_vendor_id
);
328 wined3d_settings
.pci_vendor_id
= pci_vendor_id
;
331 if ( !get_config_key( hkey
, appkey
, "VideoMemorySize", buffer
, size
) )
333 int TmpVideoMemorySize
= atoi(buffer
);
334 if(TmpVideoMemorySize
> 0)
336 wined3d_settings
.emulated_textureram
= (UINT64
)TmpVideoMemorySize
*1024*1024;
337 TRACE("Use %iMiB = 0x%s bytes for emulated_textureram\n",
339 wine_dbgstr_longlong(wined3d_settings
.emulated_textureram
));
342 ERR("VideoMemorySize is %i but must be >0\n", TmpVideoMemorySize
);
344 if ( !get_config_key( hkey
, appkey
, "WineLogo", buffer
, size
) )
346 size_t len
= strlen(buffer
) + 1;
348 if (!(wined3d_settings
.logo
= heap_alloc(len
)))
349 ERR("Failed to allocate logo path memory.\n");
351 memcpy(wined3d_settings
.logo
, buffer
, len
);
353 if (!get_config_key_dword(hkey
, appkey
, "MultisampleTextures", &wined3d_settings
.multisample_textures
))
354 ERR_(winediag
)("Setting multisample textures to %#x.\n", wined3d_settings
.multisample_textures
);
355 if (!get_config_key_dword(hkey
, appkey
, "SampleCount", &wined3d_settings
.sample_count
))
356 ERR_(winediag
)("Forcing sample count to %u. This may not be compatible with all applications.\n",
357 wined3d_settings
.sample_count
);
358 if (!get_config_key(hkey
, appkey
, "CheckFloatConstants", buffer
, size
)
359 && !strcmp(buffer
, "enabled"))
361 TRACE("Checking relative addressing indices in float constants.\n");
362 wined3d_settings
.check_float_constants
= TRUE
;
364 if (!get_config_key_dword(hkey
, appkey
, "strict_shader_math", &wined3d_settings
.strict_shader_math
))
365 ERR_(winediag
)("Setting strict shader math to %#x.\n", wined3d_settings
.strict_shader_math
);
366 if (!get_config_key_dword(hkey
, appkey
, "MaxShaderModelVS", &wined3d_settings
.max_sm_vs
))
367 TRACE("Limiting VS shader model to %u.\n", wined3d_settings
.max_sm_vs
);
368 if (!get_config_key_dword(hkey
, appkey
, "MaxShaderModelHS", &wined3d_settings
.max_sm_hs
))
369 TRACE("Limiting HS shader model to %u.\n", wined3d_settings
.max_sm_hs
);
370 if (!get_config_key_dword(hkey
, appkey
, "MaxShaderModelDS", &wined3d_settings
.max_sm_ds
))
371 TRACE("Limiting DS shader model to %u.\n", wined3d_settings
.max_sm_ds
);
372 if (!get_config_key_dword(hkey
, appkey
, "MaxShaderModelGS", &wined3d_settings
.max_sm_gs
))
373 TRACE("Limiting GS shader model to %u.\n", wined3d_settings
.max_sm_gs
);
374 if (!get_config_key_dword(hkey
, appkey
, "MaxShaderModelPS", &wined3d_settings
.max_sm_ps
))
375 TRACE("Limiting PS shader model to %u.\n", wined3d_settings
.max_sm_ps
);
376 if (!get_config_key_dword(hkey
, appkey
, "MaxShaderModelCS", &wined3d_settings
.max_sm_cs
))
377 TRACE("Limiting CS shader model to %u.\n", wined3d_settings
.max_sm_cs
);
378 if (!get_config_key(hkey
, appkey
, "renderer", buffer
, size
))
380 if (!strcmp(buffer
, "vulkan"))
382 ERR_(winediag
)("Using the Vulkan renderer.\n");
383 wined3d_settings
.renderer
= WINED3D_RENDERER_VULKAN
;
385 else if (!strcmp(buffer
, "gl"))
387 ERR_(winediag
)("Using the OpenGL renderer.\n");
388 wined3d_settings
.renderer
= WINED3D_RENDERER_OPENGL
;
390 else if (!strcmp(buffer
, "gdi") || !strcmp(buffer
, "no3d"))
392 ERR_(winediag
)("Disabling 3D support.\n");
393 wined3d_settings
.renderer
= WINED3D_RENDERER_NO3D
;
396 if (!get_config_key_dword(hkey
, appkey
, "cb_access_map_w", &tmpvalue
) && tmpvalue
)
398 TRACE("Forcing all constant buffers to be write-mappable.\n");
399 wined3d_settings
.cb_access_map_w
= TRUE
;
403 if (appkey
) RegCloseKey( appkey
);
404 if (hkey
) RegCloseKey( hkey
);
409 static BOOL
wined3d_dll_destroy(HINSTANCE hInstDLL
)
411 DWORD wined3d_context_tls_idx
= context_get_tls_idx();
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
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
);
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
,
475 unsigned int adapter_idx
, output_idx
;
476 struct wined3d_adapter
*adapter
;
477 MONITORINFOEXW monitor_info
;
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());
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
];
503 static struct wined3d_wndproc
*wined3d_find_wndproc(HWND window
, struct wined3d
*wined3d
)
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
)
518 BOOL
wined3d_filter_messages(HWND window
, BOOL filter
)
520 struct wined3d_wndproc
*entry
;
523 wined3d_wndproc_mutex_lock();
525 if (!(entry
= wined3d_find_wndproc(window
, NULL
)))
527 wined3d_wndproc_mutex_unlock();
532 entry
->filter
= filter
;
534 wined3d_wndproc_mutex_unlock();
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
;
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
;
559 wined3d_wndproc_mutex_unlock();
563 if (filter
&& message
!= WM_DISPLAYCHANGE
)
565 TRACE("Filtering message: window %p, message %#x, wparam %#lx, lparam %#lx.\n",
566 window
, message
, wparam
, lparam
);
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
);
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
;
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
)
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
)))
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
);
614 swapchain_desc
.output
= output
;
615 wined3d_swapchain_state_set_fullscreen(state
, &swapchain_desc
, NULL
);
617 wined3d_wndproc_mutex_unlock();
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
)))
640 WARN("Window %p is already registered with wined3d.\n", window
);
641 entry
->flags
= flags
;
642 wined3d_wndproc_mutex_unlock();
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");
654 entry
= &wndproc_table
.entries
[wndproc_table
.count
++];
655 entry
->window
= window
;
656 entry
->unicode
= IsWindowUnicode(window
);
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(). */
664 entry
->proc
= (WNDPROC
)SetWindowLongPtrW(window
, GWLP_WNDPROC
, (LONG_PTR
)wined3d_wndproc
);
666 entry
->proc
= (WNDPROC
)SetWindowLongPtrA(window
, GWLP_WNDPROC
, (LONG_PTR
)wined3d_wndproc
);
672 entry
->device
= device
;
673 entry
->wined3d
= wined3d
;
674 entry
->flags
= flags
;
676 wined3d_wndproc_mutex_unlock();
681 static BOOL
restore_wndproc(struct wined3d_wndproc
*entry
)
687 proc
= GetWindowLongPtrW(entry
->window
, GWLP_WNDPROC
);
688 if (proc
!= (LONG_PTR
)wined3d_wndproc
)
690 SetWindowLongPtrW(entry
->window
, GWLP_WNDPROC
, (LONG_PTR
)entry
->proc
);
694 proc
= GetWindowLongPtrA(entry
->window
, GWLP_WNDPROC
);
695 if (proc
!= (LONG_PTR
)wined3d_wndproc
)
697 SetWindowLongPtrA(entry
->window
, GWLP_WNDPROC
, (LONG_PTR
)entry
->proc
);
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
);
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();
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
;
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
)
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",
758 last
= &wndproc_table
.entries
[--wndproc_table
.count
];
765 wined3d_wndproc_mutex_unlock();
768 static struct wined3d_window_hook
*wined3d_find_hook(DWORD thread_id
)
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
];
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();
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
)))
802 wined3d_wndproc_mutex_unlock();
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();
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
);
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
;
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
)
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
)
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
;
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
)
865 case DLL_PROCESS_ATTACH
:
866 return wined3d_dll_init(inst
);
868 case DLL_PROCESS_DETACH
:
870 return wined3d_dll_destroy(inst
);
873 case DLL_THREAD_DETACH
:
874 if (!wined3d_context_gl_set_current(NULL
))
876 ERR("Failed to clear current context.\n");