dinput: SetActionMap setting the device buffer.
[wine] / dlls / wined3d / wined3d_main.c
1 /*
2  * Direct3D wine internal interface main
3  *
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
9  *
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.
14  *
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.
19  *
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
23  */
24
25 #include "config.h"
26
27 #include "initguid.h"
28 #include "wined3d_private.h"
29
30 WINE_DEFAULT_DEBUG_CHANNEL(d3d);
31
32 struct wined3d_wndproc
33 {
34     HWND window;
35     BOOL unicode;
36     WNDPROC proc;
37     struct wined3d_device *device;
38 };
39
40 struct wined3d_wndproc_table
41 {
42     struct wined3d_wndproc *entries;
43     unsigned int count;
44     unsigned int size;
45 };
46
47 static struct wined3d_wndproc_table wndproc_table;
48
49 int num_lock = 0;
50 void (CDECL *wine_tsx11_lock_ptr)(void) = NULL;
51 void (CDECL *wine_tsx11_unlock_ptr)(void) = NULL;
52
53 static CRITICAL_SECTION wined3d_cs;
54 static CRITICAL_SECTION_DEBUG wined3d_cs_debug =
55 {
56     0, 0, &wined3d_cs,
57     {&wined3d_cs_debug.ProcessLocksList,
58     &wined3d_cs_debug.ProcessLocksList},
59     0, 0, {(DWORD_PTR)(__FILE__ ": wined3d_cs")}
60 };
61 static CRITICAL_SECTION wined3d_cs = {&wined3d_cs_debug, -1, 0, 0, 0, 0};
62
63 static CRITICAL_SECTION wined3d_wndproc_cs;
64 static CRITICAL_SECTION_DEBUG wined3d_wndproc_cs_debug =
65 {
66     0, 0, &wined3d_wndproc_cs,
67     {&wined3d_wndproc_cs_debug.ProcessLocksList,
68     &wined3d_wndproc_cs_debug.ProcessLocksList},
69     0, 0, {(DWORD_PTR)(__FILE__ ": wined3d_wndproc_cs")}
70 };
71 static CRITICAL_SECTION wined3d_wndproc_cs = {&wined3d_wndproc_cs_debug, -1, 0, 0, 0, 0};
72
73 /* When updating default value here, make sure to update winecfg as well,
74  * where appropriate. */
75 struct wined3d_settings wined3d_settings =
76 {
77     VS_HW,          /* Hardware by default */
78     PS_HW,          /* Hardware by default */
79     TRUE,           /* Use of GLSL enabled by default */
80     ORM_FBO,        /* Use FBOs to do offscreen rendering */
81     RTL_READTEX,    /* Default render target locking method */
82     PCI_VENDOR_NONE,/* PCI Vendor ID */
83     PCI_DEVICE_NONE,/* PCI Device ID */
84     0,              /* The default of memory is set in init_driver_info */
85     NULL,           /* No wine logo by default */
86     FALSE,          /* Disable multisampling for now due to Nvidia driver bugs which happens for some users */
87     FALSE,          /* No strict draw ordering. */
88 };
89
90 /* Do not call while under the GL lock. */
91 struct wined3d * CDECL wined3d_create(UINT version, DWORD flags, void *parent)
92 {
93     struct wined3d *object;
94     HRESULT hr;
95
96     object = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(*object));
97     if (!object)
98     {
99         ERR("Failed to allocate wined3d object memory.\n");
100         return NULL;
101     }
102
103     hr = wined3d_init(object, version, flags, parent);
104     if (FAILED(hr))
105     {
106         WARN("Failed to initialize wined3d object, hr %#x.\n", hr);
107         HeapFree(GetProcessHeap(), 0, object);
108         return NULL;
109     }
110
111     TRACE("Created wined3d object %p for d3d%d support.\n", object, version);
112
113     return object;
114 }
115
116 static DWORD get_config_key(HKEY defkey, HKEY appkey, const char *name, char *buffer, DWORD size)
117 {
118     if (appkey && !RegQueryValueExA(appkey, name, 0, NULL, (BYTE *)buffer, &size)) return 0;
119     if (defkey && !RegQueryValueExA(defkey, name, 0, NULL, (BYTE *)buffer, &size)) return 0;
120     return ERROR_FILE_NOT_FOUND;
121 }
122
123 static DWORD get_config_key_dword(HKEY defkey, HKEY appkey, const char *name, DWORD *data)
124 {
125     DWORD type;
126     DWORD size = sizeof(DWORD);
127     if (appkey && !RegQueryValueExA(appkey, name, 0, &type, (BYTE *)data, &size) && (type == REG_DWORD)) return 0;
128     if (defkey && !RegQueryValueExA(defkey, name, 0, &type, (BYTE *)data, &size) && (type == REG_DWORD)) return 0;
129     return ERROR_FILE_NOT_FOUND;
130 }
131
132 static void CDECL wined3d_do_nothing(void)
133 {
134 }
135
136 static BOOL wined3d_dll_init(HINSTANCE hInstDLL)
137 {
138     DWORD wined3d_context_tls_idx;
139     HMODULE mod;
140     char buffer[MAX_PATH+10];
141     DWORD size = sizeof(buffer);
142     HKEY hkey = 0;
143     HKEY appkey = 0;
144     DWORD len, tmpvalue;
145     WNDCLASSA wc;
146
147     wined3d_context_tls_idx = TlsAlloc();
148     if (wined3d_context_tls_idx == TLS_OUT_OF_INDEXES)
149     {
150         DWORD err = GetLastError();
151         ERR("Failed to allocate context TLS index, err %#x.\n", err);
152         return FALSE;
153     }
154     context_set_tls_idx(wined3d_context_tls_idx);
155
156     /* We need our own window class for a fake window which we use to retrieve GL capabilities */
157     /* We might need CS_OWNDC in the future if we notice strange things on Windows.
158      * Various articles/posts about OpenGL problems on Windows recommend this. */
159     wc.style                = CS_HREDRAW | CS_VREDRAW;
160     wc.lpfnWndProc          = DefWindowProcA;
161     wc.cbClsExtra           = 0;
162     wc.cbWndExtra           = 0;
163     wc.hInstance            = hInstDLL;
164     wc.hIcon                = LoadIconA(NULL, (LPCSTR)IDI_WINLOGO);
165     wc.hCursor              = LoadCursorA(NULL, (LPCSTR)IDC_ARROW);
166     wc.hbrBackground        = NULL;
167     wc.lpszMenuName         = NULL;
168     wc.lpszClassName        = WINED3D_OPENGL_WINDOW_CLASS_NAME;
169
170     if (!RegisterClassA(&wc))
171     {
172         ERR("Failed to register window class 'WineD3D_OpenGL'!\n");
173         if (!TlsFree(wined3d_context_tls_idx))
174         {
175             DWORD err = GetLastError();
176             ERR("Failed to free context TLS index, err %#x.\n", err);
177         }
178         return FALSE;
179     }
180
181     DisableThreadLibraryCalls(hInstDLL);
182
183     mod = GetModuleHandleA( "winex11.drv" );
184     if (mod)
185     {
186         wine_tsx11_lock_ptr   = (void *)GetProcAddress( mod, "wine_tsx11_lock" );
187         wine_tsx11_unlock_ptr = (void *)GetProcAddress( mod, "wine_tsx11_unlock" );
188     }
189     else /* We are most likely on Windows */
190     {
191         wine_tsx11_lock_ptr   = wined3d_do_nothing;
192         wine_tsx11_unlock_ptr = wined3d_do_nothing;
193     }
194     /* @@ Wine registry key: HKCU\Software\Wine\Direct3D */
195     if ( RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\Direct3D", &hkey ) ) hkey = 0;
196
197     len = GetModuleFileNameA( 0, buffer, MAX_PATH );
198     if (len && len < MAX_PATH)
199     {
200         HKEY tmpkey;
201         /* @@ Wine registry key: HKCU\Software\Wine\AppDefaults\app.exe\Direct3D */
202         if (!RegOpenKeyA( HKEY_CURRENT_USER, "Software\\Wine\\AppDefaults", &tmpkey ))
203         {
204             char *p, *appname = buffer;
205             if ((p = strrchr( appname, '/' ))) appname = p + 1;
206             if ((p = strrchr( appname, '\\' ))) appname = p + 1;
207             strcat( appname, "\\Direct3D" );
208             TRACE("appname = [%s]\n", appname);
209             if (RegOpenKeyA( tmpkey, appname, &appkey )) appkey = 0;
210             RegCloseKey( tmpkey );
211         }
212     }
213
214     if (hkey || appkey)
215     {
216         if ( !get_config_key( hkey, appkey, "VertexShaderMode", buffer, size) )
217         {
218             if (!strcmp(buffer,"none"))
219             {
220                 TRACE("Disable vertex shaders\n");
221                 wined3d_settings.vs_mode = VS_NONE;
222             }
223         }
224         if ( !get_config_key( hkey, appkey, "PixelShaderMode", buffer, size) )
225         {
226             if (!strcmp(buffer,"enabled"))
227             {
228                 TRACE("Allow pixel shaders\n");
229                 wined3d_settings.ps_mode = PS_HW;
230             }
231             if (!strcmp(buffer,"disabled"))
232             {
233                 TRACE("Disable pixel shaders\n");
234                 wined3d_settings.ps_mode = PS_NONE;
235             }
236         }
237         if ( !get_config_key( hkey, appkey, "UseGLSL", buffer, size) )
238         {
239             if (!strcmp(buffer,"disabled"))
240             {
241                 TRACE("Use of GL Shading Language disabled\n");
242                 wined3d_settings.glslRequested = FALSE;
243             }
244         }
245         if ( !get_config_key( hkey, appkey, "OffscreenRenderingMode", buffer, size) )
246         {
247             if (!strcmp(buffer,"backbuffer"))
248             {
249                 TRACE("Using the backbuffer for offscreen rendering\n");
250                 wined3d_settings.offscreen_rendering_mode = ORM_BACKBUFFER;
251             }
252             else if (!strcmp(buffer,"fbo"))
253             {
254                 TRACE("Using FBOs for offscreen rendering\n");
255                 wined3d_settings.offscreen_rendering_mode = ORM_FBO;
256             }
257         }
258         if ( !get_config_key( hkey, appkey, "RenderTargetLockMode", buffer, size) )
259         {
260             if (!strcmp(buffer,"disabled"))
261             {
262                 TRACE("Disabling render target locking\n");
263                 wined3d_settings.rendertargetlock_mode = RTL_DISABLE;
264             }
265             else if (!strcmp(buffer,"readdraw"))
266             {
267                 TRACE("Using glReadPixels for render target reading and glDrawPixels for writing\n");
268                 wined3d_settings.rendertargetlock_mode = RTL_READDRAW;
269             }
270             else if (!strcmp(buffer,"readtex"))
271             {
272                 TRACE("Using glReadPixels for render target reading and textures for writing\n");
273                 wined3d_settings.rendertargetlock_mode = RTL_READTEX;
274             }
275         }
276         if ( !get_config_key_dword( hkey, appkey, "VideoPciDeviceID", &tmpvalue) )
277         {
278             int pci_device_id = tmpvalue;
279
280             /* A pci device id is 16-bit */
281             if(pci_device_id > 0xffff)
282             {
283                 ERR("Invalid value for VideoPciDeviceID. The value should be smaller or equal to 65535 or 0xffff\n");
284             }
285             else
286             {
287                 TRACE("Using PCI Device ID %04x\n", pci_device_id);
288                 wined3d_settings.pci_device_id = pci_device_id;
289             }
290         }
291         if ( !get_config_key_dword( hkey, appkey, "VideoPciVendorID", &tmpvalue) )
292         {
293             int pci_vendor_id = tmpvalue;
294
295             /* A pci device id is 16-bit */
296             if(pci_vendor_id > 0xffff)
297             {
298                 ERR("Invalid value for VideoPciVendorID. The value should be smaller or equal to 65535 or 0xffff\n");
299             }
300             else
301             {
302                 TRACE("Using PCI Vendor ID %04x\n", pci_vendor_id);
303                 wined3d_settings.pci_vendor_id = pci_vendor_id;
304             }
305         }
306         if ( !get_config_key( hkey, appkey, "VideoMemorySize", buffer, size) )
307         {
308             int TmpVideoMemorySize = atoi(buffer);
309             if(TmpVideoMemorySize > 0)
310             {
311                 wined3d_settings.emulated_textureram = TmpVideoMemorySize *1024*1024;
312                 TRACE("Use %iMB = %d byte for emulated_textureram\n",
313                         TmpVideoMemorySize,
314                         wined3d_settings.emulated_textureram);
315             }
316             else
317                 ERR("VideoMemorySize is %i but must be >0\n", TmpVideoMemorySize);
318         }
319         if ( !get_config_key( hkey, appkey, "WineLogo", buffer, size) )
320         {
321             size_t len = strlen(buffer) + 1;
322
323             wined3d_settings.logo = HeapAlloc(GetProcessHeap(), 0, len);
324             if (!wined3d_settings.logo) ERR("Failed to allocate logo path memory.\n");
325             else memcpy(wined3d_settings.logo, buffer, len);
326         }
327         if ( !get_config_key( hkey, appkey, "Multisampling", buffer, size) )
328         {
329             if (!strcmp(buffer,"enabled"))
330             {
331                 TRACE("Allow multisampling\n");
332                 wined3d_settings.allow_multisampling = TRUE;
333             }
334         }
335         if (!get_config_key(hkey, appkey, "StrictDrawOrdering", buffer, size)
336                 && !strcmp(buffer,"enabled"))
337         {
338             TRACE("Enforcing strict draw ordering.\n");
339             wined3d_settings.strict_draw_ordering = TRUE;
340         }
341     }
342     if (wined3d_settings.vs_mode == VS_HW)
343         TRACE("Allow HW vertex shaders\n");
344     if (wined3d_settings.ps_mode == PS_NONE)
345         TRACE("Disable pixel shaders\n");
346     if (wined3d_settings.glslRequested)
347         TRACE("If supported by your system, GL Shading Language will be used\n");
348
349     if (appkey) RegCloseKey( appkey );
350     if (hkey) RegCloseKey( hkey );
351
352     return TRUE;
353 }
354
355 static BOOL wined3d_dll_destroy(HINSTANCE hInstDLL)
356 {
357     DWORD wined3d_context_tls_idx = context_get_tls_idx();
358     unsigned int i;
359
360     if (!TlsFree(wined3d_context_tls_idx))
361     {
362         DWORD err = GetLastError();
363         ERR("Failed to free context TLS index, err %#x.\n", err);
364     }
365
366     for (i = 0; i < wndproc_table.count; ++i)
367     {
368         /* Trying to unregister these would be futile. These entries can only
369          * exist if either we skipped them in wined3d_unregister_window() due
370          * to the application replacing the wndproc after the entry was
371          * registered, or if the application still has an active wined3d
372          * device. In the latter case the application has bigger problems than
373          * these entries. */
374         WARN("Leftover wndproc table entry %p.\n", &wndproc_table.entries[i]);
375     }
376     HeapFree(GetProcessHeap(), 0, wndproc_table.entries);
377
378     HeapFree(GetProcessHeap(), 0, wined3d_settings.logo);
379     UnregisterClassA(WINED3D_OPENGL_WINDOW_CLASS_NAME, hInstDLL);
380
381     return TRUE;
382 }
383
384 void WINAPI wined3d_mutex_lock(void)
385 {
386     EnterCriticalSection(&wined3d_cs);
387 }
388
389 void WINAPI wined3d_mutex_unlock(void)
390 {
391     LeaveCriticalSection(&wined3d_cs);
392 }
393
394 static void wined3d_wndproc_mutex_lock(void)
395 {
396     EnterCriticalSection(&wined3d_wndproc_cs);
397 }
398
399 static void wined3d_wndproc_mutex_unlock(void)
400 {
401     LeaveCriticalSection(&wined3d_wndproc_cs);
402 }
403
404 static struct wined3d_wndproc *wined3d_find_wndproc(HWND window)
405 {
406     unsigned int i;
407
408     for (i = 0; i < wndproc_table.count; ++i)
409     {
410         if (wndproc_table.entries[i].window == window)
411         {
412             return &wndproc_table.entries[i];
413         }
414     }
415
416     return NULL;
417 }
418
419 static LRESULT CALLBACK wined3d_wndproc(HWND window, UINT message, WPARAM wparam, LPARAM lparam)
420 {
421     struct wined3d_wndproc *entry;
422     struct wined3d_device *device;
423     BOOL unicode;
424     WNDPROC proc;
425
426     wined3d_wndproc_mutex_lock();
427     entry = wined3d_find_wndproc(window);
428
429     if (!entry)
430     {
431         wined3d_wndproc_mutex_unlock();
432         ERR("Window %p is not registered with wined3d.\n", window);
433         return DefWindowProcW(window, message, wparam, lparam);
434     }
435
436     device = entry->device;
437     unicode = entry->unicode;
438     proc = entry->proc;
439     wined3d_wndproc_mutex_unlock();
440
441     return device_process_message(device, window, unicode, message, wparam, lparam, proc);
442 }
443
444 BOOL wined3d_register_window(HWND window, struct wined3d_device *device)
445 {
446     struct wined3d_wndproc *entry;
447
448     wined3d_wndproc_mutex_lock();
449
450     if (wined3d_find_wndproc(window))
451     {
452         wined3d_wndproc_mutex_unlock();
453         WARN("Window %p is already registered with wined3d.\n", window);
454         return TRUE;
455     }
456
457     if (wndproc_table.size == wndproc_table.count)
458     {
459         unsigned int new_size = max(1, wndproc_table.size * 2);
460         struct wined3d_wndproc *new_entries;
461
462         if (!wndproc_table.entries) new_entries = HeapAlloc(GetProcessHeap(), 0, new_size * sizeof(*new_entries));
463         else new_entries = HeapReAlloc(GetProcessHeap(), 0, wndproc_table.entries, new_size * sizeof(*new_entries));
464
465         if (!new_entries)
466         {
467             wined3d_wndproc_mutex_unlock();
468             ERR("Failed to grow table.\n");
469             return FALSE;
470         }
471
472         wndproc_table.entries = new_entries;
473         wndproc_table.size = new_size;
474     }
475
476     entry = &wndproc_table.entries[wndproc_table.count++];
477     entry->window = window;
478     entry->unicode = IsWindowUnicode(window);
479     /* Set a window proc that matches the window. Some applications (e.g. NoX)
480      * replace the window proc after we've set ours, and expect to be able to
481      * call the previous one (ours) directly, without using CallWindowProc(). */
482     if (entry->unicode)
483         entry->proc = (WNDPROC)SetWindowLongPtrW(window, GWLP_WNDPROC, (LONG_PTR)wined3d_wndproc);
484     else
485         entry->proc = (WNDPROC)SetWindowLongPtrA(window, GWLP_WNDPROC, (LONG_PTR)wined3d_wndproc);
486     entry->device = device;
487
488     wined3d_wndproc_mutex_unlock();
489
490     return TRUE;
491 }
492
493 void wined3d_unregister_window(HWND window)
494 {
495     struct wined3d_wndproc *entry, *last;
496     LONG_PTR proc;
497
498     wined3d_wndproc_mutex_lock();
499
500     if (!(entry = wined3d_find_wndproc(window)))
501     {
502         wined3d_wndproc_mutex_unlock();
503         ERR("Window %p is not registered with wined3d.\n", window);
504         return;
505     }
506
507     if (entry->unicode)
508     {
509         proc = GetWindowLongPtrW(window, GWLP_WNDPROC);
510         if (proc != (LONG_PTR)wined3d_wndproc)
511         {
512             wined3d_wndproc_mutex_unlock();
513             WARN("Not unregistering window %p, window proc %#lx doesn't match wined3d window proc %p.\n",
514                     window, proc, wined3d_wndproc);
515             return;
516         }
517
518         SetWindowLongPtrW(window, GWLP_WNDPROC, (LONG_PTR)entry->proc);
519     }
520     else
521     {
522         proc = GetWindowLongPtrA(window, GWLP_WNDPROC);
523         if (proc != (LONG_PTR)wined3d_wndproc)
524         {
525             wined3d_wndproc_mutex_unlock();
526             WARN("Not unregistering window %p, window proc %#lx doesn't match wined3d window proc %p.\n",
527                     window, proc, wined3d_wndproc);
528             return;
529         }
530
531         SetWindowLongPtrA(window, GWLP_WNDPROC, (LONG_PTR)entry->proc);
532     }
533
534     last = &wndproc_table.entries[--wndproc_table.count];
535     if (entry != last) *entry = *last;
536
537     wined3d_wndproc_mutex_unlock();
538 }
539
540 /* At process attach */
541 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
542 {
543     TRACE("WineD3D DLLMain Reason=%u\n", fdwReason);
544
545     switch (fdwReason)
546     {
547         case DLL_PROCESS_ATTACH:
548             return wined3d_dll_init(hInstDLL);
549
550         case DLL_PROCESS_DETACH:
551             return wined3d_dll_destroy(hInstDLL);
552
553         case DLL_THREAD_DETACH:
554         {
555             if (!context_set_current(NULL))
556             {
557                 ERR("Failed to clear current context.\n");
558             }
559             return TRUE;
560         }
561
562         default:
563             return TRUE;
564     }
565 }