2 * Direct3D wine internal interface main
4 * Copyright 2002-2003 The wine-d3d team
5 * Copyright 2002-2003 Raphael Junqueira
6 * Copyright 2004 Jason Edmeades
7 * Copyright 2007-2008 Stefan Dösinger for CodeWeavers
8 * Copyright 2009 Henri Verbeet for CodeWeavers
10 * This library is free software; you can redistribute it and/or
11 * modify it under the terms of the GNU Lesser General Public
12 * License as published by the Free Software Foundation; either
13 * version 2.1 of the License, or (at your option) any later version.
15 * This library is distributed in the hope that it will be useful,
16 * but WITHOUT ANY WARRANTY; without even the implied warranty of
17 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 * Lesser General Public License for more details.
20 * You should have received a copy of the GNU Lesser General Public
21 * License along with this library; if not, write to the Free Software
22 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
25 #define VKD3D_NO_VULKAN_H
26 #define VKD3D_NO_WIN32_TYPES
28 #include "wined3d_private.h"
32 WINE_DEFAULT_DEBUG_CHANNEL(d3d
);
33 WINE_DECLARE_DEBUG_CHANNEL(vkd3d
);
34 WINE_DECLARE_DEBUG_CHANNEL(winediag
);
36 struct wined3d_wndproc
38 struct wined3d
*wined3d
;
43 struct wined3d_device
*device
;
47 struct wined3d_wndproc_table
49 struct wined3d_wndproc
*entries
;
54 struct wined3d_window_hook
61 struct wined3d_registered_swapchain_state
63 struct wined3d_swapchain_state
*state
;
67 struct wined3d_swapchain_state_table
69 struct wined3d_window_hook
*hooks
;
73 struct wined3d_registered_swapchain_state
*states
;
78 static struct wined3d_wndproc_table wndproc_table
;
79 static struct wined3d_swapchain_state_table swapchain_state_table
;
81 static CRITICAL_SECTION wined3d_cs
;
82 static CRITICAL_SECTION_DEBUG wined3d_cs_debug
=
85 {&wined3d_cs_debug
.ProcessLocksList
,
86 &wined3d_cs_debug
.ProcessLocksList
},
87 0, 0, {(DWORD_PTR
)(__FILE__
": wined3d_cs")}
89 static CRITICAL_SECTION wined3d_cs
= {&wined3d_cs_debug
, -1, 0, 0, 0, 0};
91 static CRITICAL_SECTION wined3d_wndproc_cs
;
92 static CRITICAL_SECTION_DEBUG wined3d_wndproc_cs_debug
=
94 0, 0, &wined3d_wndproc_cs
,
95 {&wined3d_wndproc_cs_debug
.ProcessLocksList
,
96 &wined3d_wndproc_cs_debug
.ProcessLocksList
},
97 0, 0, {(DWORD_PTR
)(__FILE__
": wined3d_wndproc_cs")}
99 static CRITICAL_SECTION wined3d_wndproc_cs
= {&wined3d_wndproc_cs_debug
, -1, 0, 0, 0, 0};
101 CRITICAL_SECTION wined3d_command_cs
;
102 static CRITICAL_SECTION_DEBUG wined3d_command_cs_debug
=
104 0, 0, &wined3d_command_cs
,
105 {&wined3d_command_cs_debug
.ProcessLocksList
,
106 &wined3d_command_cs_debug
.ProcessLocksList
},
107 0, 0, {(DWORD_PTR
)(__FILE__
": wined3d_command_cs")}
109 CRITICAL_SECTION wined3d_command_cs
= {&wined3d_command_cs_debug
, -1, 0, 0, 0, 0};
111 /* When updating default value here, make sure to update winecfg as well,
112 * where appropriate. */
113 struct wined3d_settings wined3d_settings
=
115 .cs_multithreaded
= WINED3D_CSMT_ENABLE
,
116 .max_gl_version
= MAKEDWORD_VERSION(4, 4),
117 .offscreen_rendering_mode
= ORM_FBO
,
118 .pci_vendor_id
= PCI_VENDOR_NONE
,
119 .pci_device_id
= PCI_DEVICE_NONE
,
120 .multisample_textures
= TRUE
,
122 .max_sm_vs
= UINT_MAX
,
123 .max_sm_ps
= UINT_MAX
,
124 .max_sm_ds
= UINT_MAX
,
125 .max_sm_hs
= UINT_MAX
,
126 .max_sm_gs
= UINT_MAX
,
127 .max_sm_cs
= UINT_MAX
,
128 .renderer
= WINED3D_RENDERER_AUTO
,
129 .shader_backend
= WINED3D_SHADER_BACKEND_AUTO
,
132 struct wined3d
* CDECL
wined3d_create(uint32_t flags
)
134 struct wined3d
*object
;
137 if (!(object
= heap_alloc_zero(FIELD_OFFSET(struct wined3d
, adapters
[1]))))
139 ERR("Failed to allocate wined3d object memory.\n");
143 if (wined3d_settings
.renderer
== WINED3D_RENDERER_NO3D
)
144 flags
|= WINED3D_NO3D
;
146 if (FAILED(hr
= wined3d_init(object
, flags
)))
148 WARN("Failed to initialize wined3d object, hr %#lx.\n", hr
);
153 TRACE("Created wined3d object %p.\n", object
);
158 static bool is_option_separator(char c
)
160 return c
== ',' || c
== ';' || c
== '\0';
163 static const char *config_list_get_value(const char *string
, const char *key
, size_t *len
)
171 if ((p
= strstr(p
, key
)))
173 prev_char
= p
> string
? p
[-1] : 0;
176 if (is_option_separator(prev_char
) && *p
== '=')
178 if ((end
= strpbrk(p
+ 1, ",;")))
179 *len
= end
- (p
+ 1);
181 *len
= strlen(p
+ 1);
190 static DWORD
get_config_key(HKEY defkey
, HKEY appkey
, const char *env
, const char *name
, char *buffer
, DWORD size
)
192 const char *env_value
;
195 if ((env_value
= config_list_get_value(env
, name
, &env_len
)) && env_len
< size
)
197 memcpy(buffer
, env_value
, env_len
);
201 if (appkey
&& !RegQueryValueExA(appkey
, name
, 0, NULL
, (BYTE
*)buffer
, &size
)) return 0;
202 if (defkey
&& !RegQueryValueExA(defkey
, name
, 0, NULL
, (BYTE
*)buffer
, &size
)) return 0;
203 return ERROR_FILE_NOT_FOUND
;
206 static DWORD
get_config_key_dword(HKEY defkey
, HKEY appkey
, const char *env
, const char *name
, unsigned int *value
)
208 DWORD type
, data
, size
;
209 const char *env_value
;
213 if ((env_value
= config_list_get_value(env
, name
, &env_len
)))
215 *value
= strtoul(env_value
, &end
, 0);
216 if (end
!= env_value
)
220 if (appkey
&& !RegQueryValueExA(appkey
, name
, 0, &type
, (BYTE
*)&data
, &size
) && type
== REG_DWORD
) goto success
;
222 if (defkey
&& !RegQueryValueExA(defkey
, name
, 0, &type
, (BYTE
*)&data
, &size
) && type
== REG_DWORD
) goto success
;
224 return ERROR_FILE_NOT_FOUND
;
231 BOOL
wined3d_get_app_name(char *app_name
, unsigned int app_name_size
)
233 char buffer
[MAX_PATH
];
237 len
= GetModuleFileNameA(0, buffer
, ARRAY_SIZE(buffer
));
238 if (!(len
&& len
< MAX_PATH
))
242 if ((p
= strrchr(name
, '/' )))
244 if ((p
= strrchr(name
, '\\')))
247 len
= strlen(name
) + 1;
248 if (app_name_size
< len
)
251 memcpy(app_name
, name
, len
);
255 static void vkd3d_log_callback(const char *fmt
, va_list args
)
259 vsnprintf(buffer
, sizeof(buffer
), fmt
, args
);
260 __wine_dbg_output(buffer
);
263 static BOOL
wined3d_dll_init(HINSTANCE hInstDLL
)
265 DWORD wined3d_context_tls_idx
;
266 char buffer
[MAX_PATH
+10];
267 DWORD size
= sizeof(buffer
);
271 unsigned int tmpvalue
;
274 wined3d_context_tls_idx
= TlsAlloc();
275 if (wined3d_context_tls_idx
== TLS_OUT_OF_INDEXES
)
277 unsigned int err
= GetLastError();
278 ERR("Failed to allocate context TLS index, err %#x.\n", err
);
281 context_set_tls_idx(wined3d_context_tls_idx
);
283 /* We need our own window class for a fake window which we use to retrieve GL capabilities */
284 /* We might need CS_OWNDC in the future if we notice strange things on Windows.
285 * Various articles/posts about OpenGL problems on Windows recommend this. */
286 wc
.style
= CS_HREDRAW
| CS_VREDRAW
;
287 wc
.lpfnWndProc
= DefWindowProcA
;
290 wc
.hInstance
= hInstDLL
;
291 wc
.hIcon
= LoadIconA(NULL
, (const char *)IDI_WINLOGO
);
292 wc
.hCursor
= LoadCursorA(NULL
, (const char *)IDC_ARROW
);
293 wc
.hbrBackground
= NULL
;
294 wc
.lpszMenuName
= NULL
;
295 wc
.lpszClassName
= WINED3D_OPENGL_WINDOW_CLASS_NAME
;
297 if (!RegisterClassA(&wc
))
299 ERR("Failed to register window class 'WineD3D_OpenGL'!\n");
300 if (!TlsFree(wined3d_context_tls_idx
))
302 unsigned int err
= GetLastError();
303 ERR("Failed to free context TLS index, err %#x.\n", err
);
308 DisableThreadLibraryCalls(hInstDLL
);
310 /* @@ Wine registry key: HKCU\Software\Wine\Direct3D */
311 if ( RegOpenKeyA( HKEY_CURRENT_USER
, "Software\\Wine\\Direct3D", &hkey
) ) hkey
= 0;
313 if (wined3d_get_app_name(buffer
, ARRAY_SIZE(buffer
)))
316 /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\Direct3D */
317 if (!RegOpenKeyA(HKEY_CURRENT_USER
, "Software\\Wine\\AppDefaults", &tmpkey
))
319 strcat(buffer
, "\\Direct3D");
320 TRACE("Application name %s.\n", buffer
);
321 if (RegOpenKeyA(tmpkey
, buffer
, &appkey
)) appkey
= 0;
326 /* Allow modifying settings using the WINE_D3D_CONFIG environment variable,
327 * which takes precedence over registry keys. An example is as follows:
329 * WINE_D3D_CONFIG=csmt=0x1,shader_backend=glsl
331 env
= getenv("WINE_D3D_CONFIG");
333 if (hkey
|| appkey
|| env
)
335 if (!get_config_key_dword(hkey
, appkey
, env
, "csmt", &wined3d_settings
.cs_multithreaded
))
336 ERR_(winediag
)("Setting multithreaded command stream to %#x.\n", wined3d_settings
.cs_multithreaded
);
337 if (!get_config_key_dword(hkey
, appkey
, env
, "MaxVersionGL", &tmpvalue
))
339 ERR_(winediag
)("Setting maximum allowed wined3d GL version to %u.%u.\n",
340 tmpvalue
>> 16, tmpvalue
& 0xffff);
341 wined3d_settings
.max_gl_version
= tmpvalue
;
343 if (!get_config_key(hkey
, appkey
, env
, "shader_backend", buffer
, size
))
345 if (!stricmp(buffer
, "glsl"))
347 ERR_(winediag
)("Using the GLSL shader backend.\n");
348 wined3d_settings
.shader_backend
= WINED3D_SHADER_BACKEND_GLSL
;
350 else if (!stricmp(buffer
, "arb"))
352 ERR_(winediag
)("Using the ARB shader backend.\n");
353 wined3d_settings
.shader_backend
= WINED3D_SHADER_BACKEND_ARB
;
355 else if (!stricmp(buffer
, "none"))
357 ERR_(winediag
)("Disabling shader backends.\n");
358 wined3d_settings
.shader_backend
= WINED3D_SHADER_BACKEND_NONE
;
361 if (wined3d_settings
.shader_backend
== WINED3D_SHADER_BACKEND_ARB
362 || wined3d_settings
.shader_backend
== WINED3D_SHADER_BACKEND_NONE
)
364 ERR_(winediag
)("The GLSL shader backend has been disabled. You get to keep all the pieces if it breaks.\n");
365 TRACE("Use of GL Shading Language disabled.\n");
367 if (!get_config_key(hkey
, appkey
, env
, "OffscreenRenderingMode", buffer
, size
)
368 && !strcmp(buffer
,"backbuffer"))
369 wined3d_settings
.offscreen_rendering_mode
= ORM_BACKBUFFER
;
370 if (!get_config_key_dword(hkey
, appkey
, env
, "VideoPciDeviceID", &tmpvalue
))
372 int pci_device_id
= tmpvalue
;
374 /* A pci device id is 16-bit */
375 if(pci_device_id
> 0xffff)
377 ERR("Invalid value for VideoPciDeviceID. The value should be smaller or equal to 65535 or 0xffff\n");
381 TRACE("Using PCI Device ID %04x\n", pci_device_id
);
382 wined3d_settings
.pci_device_id
= pci_device_id
;
385 if (!get_config_key_dword(hkey
, appkey
, env
, "VideoPciVendorID", &tmpvalue
))
387 int pci_vendor_id
= tmpvalue
;
389 /* A pci device id is 16-bit */
390 if(pci_vendor_id
> 0xffff)
392 ERR("Invalid value for VideoPciVendorID. The value should be smaller or equal to 65535 or 0xffff\n");
396 TRACE("Using PCI Vendor ID %04x\n", pci_vendor_id
);
397 wined3d_settings
.pci_vendor_id
= pci_vendor_id
;
400 if (!get_config_key(hkey
, appkey
, env
, "VideoMemorySize", buffer
, size
))
402 int TmpVideoMemorySize
= atoi(buffer
);
403 if(TmpVideoMemorySize
> 0)
405 wined3d_settings
.emulated_textureram
= (UINT64
)TmpVideoMemorySize
*1024*1024;
406 TRACE("Use %iMiB = 0x%s bytes for emulated_textureram\n",
408 wine_dbgstr_longlong(wined3d_settings
.emulated_textureram
));
411 ERR("VideoMemorySize is %i but must be >0\n", TmpVideoMemorySize
);
413 if (!get_config_key(hkey
, appkey
, env
, "WineLogo", buffer
, size
))
415 size_t len
= strlen(buffer
) + 1;
417 if (!(wined3d_settings
.logo
= heap_alloc(len
)))
418 ERR("Failed to allocate logo path memory.\n");
420 memcpy(wined3d_settings
.logo
, buffer
, len
);
422 if (!get_config_key_dword(hkey
, appkey
, env
, "MultisampleTextures", &wined3d_settings
.multisample_textures
))
423 ERR_(winediag
)("Setting multisample textures to %#x.\n", wined3d_settings
.multisample_textures
);
424 if (!get_config_key_dword(hkey
, appkey
, env
, "SampleCount", &wined3d_settings
.sample_count
))
425 ERR_(winediag
)("Forcing sample count to %u. This may not be compatible with all applications.\n",
426 wined3d_settings
.sample_count
);
427 if (!get_config_key(hkey
, appkey
, env
, "CheckFloatConstants", buffer
, size
)
428 && !strcmp(buffer
, "enabled"))
430 TRACE("Checking relative addressing indices in float constants.\n");
431 wined3d_settings
.check_float_constants
= TRUE
;
433 if (!get_config_key_dword(hkey
, appkey
, env
, "strict_shader_math", &wined3d_settings
.strict_shader_math
))
434 ERR_(winediag
)("Setting strict shader math to %#x.\n", wined3d_settings
.strict_shader_math
);
435 if (!get_config_key_dword(hkey
, appkey
, env
, "MaxShaderModelVS", &wined3d_settings
.max_sm_vs
))
436 TRACE("Limiting VS shader model to %u.\n", wined3d_settings
.max_sm_vs
);
437 if (!get_config_key_dword(hkey
, appkey
, env
, "MaxShaderModelHS", &wined3d_settings
.max_sm_hs
))
438 TRACE("Limiting HS shader model to %u.\n", wined3d_settings
.max_sm_hs
);
439 if (!get_config_key_dword(hkey
, appkey
, env
, "MaxShaderModelDS", &wined3d_settings
.max_sm_ds
))
440 TRACE("Limiting DS shader model to %u.\n", wined3d_settings
.max_sm_ds
);
441 if (!get_config_key_dword(hkey
, appkey
, env
, "MaxShaderModelGS", &wined3d_settings
.max_sm_gs
))
442 TRACE("Limiting GS shader model to %u.\n", wined3d_settings
.max_sm_gs
);
443 if (!get_config_key_dword(hkey
, appkey
, env
, "MaxShaderModelPS", &wined3d_settings
.max_sm_ps
))
444 TRACE("Limiting PS shader model to %u.\n", wined3d_settings
.max_sm_ps
);
445 if (!get_config_key_dword(hkey
, appkey
, env
, "MaxShaderModelCS", &wined3d_settings
.max_sm_cs
))
446 TRACE("Limiting CS shader model to %u.\n", wined3d_settings
.max_sm_cs
);
447 if (!get_config_key(hkey
, appkey
, env
, "renderer", buffer
, size
))
449 if (!strcmp(buffer
, "vulkan"))
451 ERR_(winediag
)("Using the Vulkan renderer.\n");
452 wined3d_settings
.renderer
= WINED3D_RENDERER_VULKAN
;
454 else if (!strcmp(buffer
, "gl"))
456 ERR_(winediag
)("Using the OpenGL renderer.\n");
457 wined3d_settings
.renderer
= WINED3D_RENDERER_OPENGL
;
459 else if (!strcmp(buffer
, "gdi") || !strcmp(buffer
, "no3d"))
461 ERR_(winediag
)("Disabling 3D support.\n");
462 wined3d_settings
.renderer
= WINED3D_RENDERER_NO3D
;
465 if (!get_config_key_dword(hkey
, appkey
, env
, "cb_access_map_w", &tmpvalue
) && tmpvalue
)
467 TRACE("Forcing all constant buffers to be write-mappable.\n");
468 wined3d_settings
.cb_access_map_w
= TRUE
;
472 if (appkey
) RegCloseKey( appkey
);
473 if (hkey
) RegCloseKey( hkey
);
475 if (!getenv( "VKD3D_DEBUG" ))
477 if (TRACE_ON(vkd3d
)) putenv( "VKD3D_DEBUG=trace" );
478 else if (WARN_ON(vkd3d
)) putenv( "VKD3D_DEBUG=warn" );
479 else if (FIXME_ON(vkd3d
)) putenv( "VKD3D_DEBUG=fixme" );
480 else if (ERR_ON(vkd3d
)) putenv( "VKD3D_DEBUG=err" );
481 else putenv( "VKD3D_DEBUG=none" );
483 if (!getenv( "VKD3D_SHADER_DEBUG" ))
485 if (TRACE_ON(vkd3d
)) putenv( "VKD3D_SHADER_DEBUG=trace" );
486 else if (WARN_ON(vkd3d
)) putenv( "VKD3D_SHADER_DEBUG=warn" );
487 else if (FIXME_ON(vkd3d
)) putenv( "VKD3D_SHADER_DEBUG=fixme" );
488 else if (ERR_ON(vkd3d
)) putenv( "VKD3D_SHADER_DEBUG=err" );
489 else putenv( "VKD3D_SHADER_DEBUG=none" );
492 vkd3d_set_log_callback(vkd3d_log_callback
);
497 static BOOL
wined3d_dll_destroy(HINSTANCE hInstDLL
)
499 DWORD wined3d_context_tls_idx
= context_get_tls_idx();
502 if (!TlsFree(wined3d_context_tls_idx
))
504 unsigned int err
= GetLastError();
505 ERR("Failed to free context TLS index, err %#x.\n", err
);
508 for (i
= 0; i
< wndproc_table
.count
; ++i
)
510 /* Trying to unregister these would be futile. These entries can only
511 * exist if either we skipped them in wined3d_unregister_window() due
512 * to the application replacing the wndproc after the entry was
513 * registered, or if the application still has an active wined3d
514 * device. In the latter case the application has bigger problems than
516 WARN("Leftover wndproc table entry %p.\n", &wndproc_table
.entries
[i
]);
518 heap_free(wndproc_table
.entries
);
520 heap_free(swapchain_state_table
.states
);
521 for (i
= 0; i
< swapchain_state_table
.hook_count
; ++i
)
523 WARN("Leftover swapchain state hook %p.\n", &swapchain_state_table
.hooks
[i
]);
524 UnhookWindowsHookEx(swapchain_state_table
.hooks
[i
].hook
);
526 heap_free(swapchain_state_table
.hooks
);
528 heap_free(wined3d_settings
.logo
);
529 UnregisterClassA(WINED3D_OPENGL_WINDOW_CLASS_NAME
, hInstDLL
);
531 DeleteCriticalSection(&wined3d_command_cs
);
533 DeleteCriticalSection(&wined3d_wndproc_cs
);
534 DeleteCriticalSection(&wined3d_cs
);
538 void WINAPI
wined3d_mutex_lock(void)
540 EnterCriticalSection(&wined3d_cs
);
543 void WINAPI
wined3d_mutex_unlock(void)
545 LeaveCriticalSection(&wined3d_cs
);
548 static void wined3d_wndproc_mutex_lock(void)
550 EnterCriticalSection(&wined3d_wndproc_cs
);
553 static void wined3d_wndproc_mutex_unlock(void)
555 LeaveCriticalSection(&wined3d_wndproc_cs
);
558 static struct wined3d_output
* wined3d_get_output_from_window(const struct wined3d
*wined3d
,
561 unsigned int adapter_idx
, output_idx
;
562 struct wined3d_adapter
*adapter
;
563 MONITORINFOEXW monitor_info
;
566 TRACE("wined3d %p, hwnd %p.\n", wined3d
, hwnd
);
568 monitor
= MonitorFromWindow(hwnd
, MONITOR_DEFAULTTONEAREST
);
569 monitor_info
.cbSize
= sizeof(monitor_info
);
570 if (!GetMonitorInfoW(monitor
, (MONITORINFO
*)&monitor_info
))
572 ERR("GetMonitorInfoW failed, error %#lx.\n", GetLastError());
576 for (adapter_idx
= 0; adapter_idx
< wined3d
->adapter_count
; ++adapter_idx
)
578 adapter
= wined3d
->adapters
[adapter_idx
];
579 for (output_idx
= 0; output_idx
< adapter
->output_count
; ++output_idx
)
581 if (!lstrcmpiW(adapter
->outputs
[output_idx
].device_name
, monitor_info
.szDevice
))
582 return &adapter
->outputs
[output_idx
];
589 static struct wined3d_wndproc
*wined3d_find_wndproc(HWND window
, struct wined3d
*wined3d
)
593 for (i
= 0; i
< wndproc_table
.count
; ++i
)
595 struct wined3d_wndproc
*entry
= &wndproc_table
.entries
[i
];
597 if (entry
->window
== window
&& entry
->wined3d
== wined3d
)
604 BOOL
wined3d_filter_messages(HWND window
, BOOL filter
)
606 struct wined3d_wndproc
*entry
;
609 wined3d_wndproc_mutex_lock();
611 if (!(entry
= wined3d_find_wndproc(window
, NULL
)))
613 wined3d_wndproc_mutex_unlock();
618 entry
->filter
= filter
;
620 wined3d_wndproc_mutex_unlock();
625 static LRESULT CALLBACK
wined3d_wndproc(HWND window
, UINT message
, WPARAM wparam
, LPARAM lparam
)
627 struct wined3d_wndproc
*entry
;
628 struct wined3d_device
*device
;
629 BOOL unicode
, filter
;
632 wined3d_wndproc_mutex_lock();
634 if (!(entry
= wined3d_find_wndproc(window
, NULL
)))
636 wined3d_wndproc_mutex_unlock();
637 ERR("Window %p is not registered with wined3d.\n", window
);
638 return DefWindowProcW(window
, message
, wparam
, lparam
);
641 device
= entry
->device
;
642 unicode
= entry
->unicode
;
643 filter
= entry
->filter
;
645 wined3d_wndproc_mutex_unlock();
649 if (filter
&& message
!= WM_DISPLAYCHANGE
)
651 TRACE("Filtering message: window %p, message %#x, wparam %#Ix, lparam %#Ix.\n",
652 window
, message
, wparam
, lparam
);
655 return DefWindowProcW(window
, message
, wparam
, lparam
);
656 return DefWindowProcA(window
, message
, wparam
, lparam
);
659 return device_process_message(device
, window
, unicode
, message
, wparam
, lparam
, proc
);
662 return CallWindowProcW(proc
, window
, message
, wparam
, lparam
);
663 return CallWindowProcA(proc
, window
, message
, wparam
, lparam
);
666 static LRESULT CALLBACK
wined3d_hook_proc(int code
, WPARAM wparam
, LPARAM lparam
)
668 struct wined3d_swapchain_desc swapchain_desc
;
669 struct wined3d_swapchain_state
*state
;
670 struct wined3d_wndproc
*entry
;
671 struct wined3d_output
*output
;
672 MSG
*msg
= (MSG
*)lparam
;
675 /* Handle Alt+Enter. */
676 if (code
== HC_ACTION
&& msg
->message
== WM_SYSKEYDOWN
677 && msg
->wParam
== VK_RETURN
&& (msg
->lParam
& (KF_ALTDOWN
<< 16)))
679 wined3d_wndproc_mutex_lock();
681 for (i
= 0; i
< swapchain_state_table
.state_count
; ++i
)
683 state
= swapchain_state_table
.states
[i
].state
;
685 if (state
->device_window
!= msg
->hwnd
)
688 if ((entry
= wined3d_find_wndproc(msg
->hwnd
, state
->wined3d
))
689 && (entry
->flags
& (WINED3D_REGISTER_WINDOW_NO_WINDOW_CHANGES
690 | WINED3D_REGISTER_WINDOW_NO_ALT_ENTER
)))
693 swapchain_desc
= state
->desc
;
694 swapchain_desc
.windowed
= !swapchain_desc
.windowed
;
695 if (!(output
= wined3d_get_output_from_window(state
->wined3d
, state
->device_window
)))
697 ERR("Failed to get output from window %p.\n", state
->device_window
);
700 swapchain_desc
.output
= output
;
701 wined3d_swapchain_state_set_fullscreen(state
, &swapchain_desc
, NULL
);
703 wined3d_wndproc_mutex_unlock();
708 wined3d_wndproc_mutex_unlock();
711 return CallNextHookEx(0, code
, wparam
, lparam
);
714 BOOL CDECL
wined3d_register_window(struct wined3d
*wined3d
, HWND window
,
715 struct wined3d_device
*device
, unsigned int flags
)
717 struct wined3d_wndproc
*entry
;
719 TRACE("wined3d %p, window %p, device %p, flags %#x.\n", wined3d
, window
, device
, flags
);
721 wined3d_wndproc_mutex_lock();
723 if ((entry
= wined3d_find_wndproc(window
, wined3d
)))
726 WARN("Window %p is already registered with wined3d.\n", window
);
727 entry
->flags
= flags
;
728 wined3d_wndproc_mutex_unlock();
732 if (!wined3d_array_reserve((void **)&wndproc_table
.entries
, &wndproc_table
.size
,
733 wndproc_table
.count
+ 1, sizeof(*entry
)))
735 wined3d_wndproc_mutex_unlock();
736 ERR("Failed to grow table.\n");
740 entry
= &wndproc_table
.entries
[wndproc_table
.count
++];
741 entry
->window
= window
;
742 entry
->unicode
= IsWindowUnicode(window
);
745 /* Set a window proc that matches the window. Some applications (e.g.
746 * NoX) replace the window proc after we've set ours, and expect to be
747 * able to call the previous one (ours) directly, without using
748 * CallWindowProc(). */
750 entry
->proc
= (WNDPROC
)SetWindowLongPtrW(window
, GWLP_WNDPROC
, (LONG_PTR
)wined3d_wndproc
);
752 entry
->proc
= (WNDPROC
)SetWindowLongPtrA(window
, GWLP_WNDPROC
, (LONG_PTR
)wined3d_wndproc
);
758 entry
->device
= device
;
759 entry
->wined3d
= wined3d
;
760 entry
->flags
= flags
;
762 wined3d_wndproc_mutex_unlock();
767 static BOOL
restore_wndproc(struct wined3d_wndproc
*entry
)
773 proc
= GetWindowLongPtrW(entry
->window
, GWLP_WNDPROC
);
774 if (proc
!= (LONG_PTR
)wined3d_wndproc
)
776 SetWindowLongPtrW(entry
->window
, GWLP_WNDPROC
, (LONG_PTR
)entry
->proc
);
780 proc
= GetWindowLongPtrA(entry
->window
, GWLP_WNDPROC
);
781 if (proc
!= (LONG_PTR
)wined3d_wndproc
)
783 SetWindowLongPtrA(entry
->window
, GWLP_WNDPROC
, (LONG_PTR
)entry
->proc
);
789 void wined3d_unregister_window(HWND window
)
791 struct wined3d_wndproc
*entry
, *last
;
793 wined3d_wndproc_mutex_lock();
795 if (!(entry
= wined3d_find_wndproc(window
, NULL
)))
797 wined3d_wndproc_mutex_unlock();
798 ERR("Window %p is not registered with wined3d.\n", window
);
802 if (entry
->proc
&& !restore_wndproc(entry
))
804 entry
->device
= NULL
;
805 WARN("Not unregistering window %p, current window proc doesn't match wined3d window proc.\n", window
);
806 wined3d_wndproc_mutex_unlock();
810 last
= &wndproc_table
.entries
[--wndproc_table
.count
];
811 if (entry
!= last
) *entry
= *last
;
813 wined3d_wndproc_mutex_unlock();
816 void CDECL
wined3d_unregister_windows(struct wined3d
*wined3d
)
818 struct wined3d_wndproc
*entry
, *last
;
821 TRACE("wined3d %p.\n", wined3d
);
823 wined3d_wndproc_mutex_lock();
825 while (i
< wndproc_table
.count
)
827 entry
= &wndproc_table
.entries
[i
];
829 if (entry
->wined3d
!= wined3d
)
835 if (entry
->proc
&& !restore_wndproc(entry
))
837 entry
->device
= NULL
;
838 WARN("Not unregistering window %p, current window proc doesn't match wined3d window proc.\n",
844 last
= &wndproc_table
.entries
[--wndproc_table
.count
];
851 wined3d_wndproc_mutex_unlock();
854 static struct wined3d_window_hook
*wined3d_find_hook(DWORD thread_id
)
858 for (i
= 0; i
< swapchain_state_table
.hook_count
; ++i
)
860 if (swapchain_state_table
.hooks
[i
].thread_id
== thread_id
)
861 return &swapchain_state_table
.hooks
[i
];
867 void wined3d_swapchain_state_register(struct wined3d_swapchain_state
*state
)
869 struct wined3d_registered_swapchain_state
*state_entry
;
870 struct wined3d_window_hook
*hook
;
872 wined3d_wndproc_mutex_lock();
874 if (!wined3d_array_reserve((void **)&swapchain_state_table
.states
, &swapchain_state_table
.states_size
,
875 swapchain_state_table
.state_count
+ 1, sizeof(*state_entry
)))
877 wined3d_wndproc_mutex_unlock();
881 state_entry
= &swapchain_state_table
.states
[swapchain_state_table
.state_count
++];
882 state_entry
->state
= state
;
883 state_entry
->thread_id
= GetWindowThreadProcessId(state
->device_window
, NULL
);
885 if ((hook
= wined3d_find_hook(state_entry
->thread_id
)))
888 wined3d_wndproc_mutex_unlock();
892 if (!wined3d_array_reserve((void **)&swapchain_state_table
.hooks
, &swapchain_state_table
.hooks_size
,
893 swapchain_state_table
.hook_count
+ 1, sizeof(*hook
)))
895 --swapchain_state_table
.state_count
;
896 wined3d_wndproc_mutex_unlock();
900 hook
= &swapchain_state_table
.hooks
[swapchain_state_table
.hook_count
++];
901 hook
->thread_id
= state_entry
->thread_id
;
902 hook
->hook
= SetWindowsHookExW(WH_GETMESSAGE
, wined3d_hook_proc
, 0, hook
->thread_id
);
905 wined3d_wndproc_mutex_unlock();
908 static void wined3d_swapchain_state_unregister(struct wined3d_swapchain_state
*state
)
910 struct wined3d_registered_swapchain_state
*state_entry
, *last_state_entry
;
911 struct wined3d_window_hook
*hook
, *last_hook
;
914 wined3d_wndproc_mutex_lock();
916 for (i
= 0; i
< swapchain_state_table
.state_count
; ++i
)
918 state_entry
= &swapchain_state_table
.states
[i
];
920 if (state_entry
->state
!= state
)
923 if ((hook
= wined3d_find_hook(state_entry
->thread_id
)) && !--hook
->count
)
925 UnhookWindowsHookEx(hook
->hook
);
926 last_hook
= &swapchain_state_table
.hooks
[--swapchain_state_table
.hook_count
];
927 if (hook
!= last_hook
)
931 last_state_entry
= &swapchain_state_table
.states
[--swapchain_state_table
.state_count
];
932 if (state_entry
!= last_state_entry
)
933 *state_entry
= *last_state_entry
;
938 wined3d_wndproc_mutex_unlock();
941 void wined3d_swapchain_state_cleanup(struct wined3d_swapchain_state
*state
)
943 wined3d_swapchain_state_unregister(state
);
946 /* At process attach */
947 BOOL WINAPI
DllMain(HINSTANCE inst
, DWORD reason
, void *reserved
)
951 case DLL_PROCESS_ATTACH
:
952 return wined3d_dll_init(inst
);
954 case DLL_PROCESS_DETACH
:
956 return wined3d_dll_destroy(inst
);
959 case DLL_THREAD_DETACH
:
960 if (!wined3d_context_gl_set_current(NULL
))
962 ERR("Failed to clear current context.\n");