[BRANCHES]
[reactos.git] / reactos / dll / directx / wine / dinput8 / dinput8_main.c
1 /* DirectInput 8
2 *
3 * Copyright 2002 TransGaming Technologies Inc.
4 * Copyright 2006 Roderick Colenbrander
5 *
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
19 */
20
21 #include <config.h>
22 //#include <assert.h>
23 //#include <stdarg.h>
24 //#include <string.h>
25
26 #define COBJMACROS
27 #define WIN32_NO_STATUS
28 #define _INC_WINDOWS
29 #define COM_NO_WINDOWS_H
30
31 #include <wine/debug.h>
32 //#include "windef.h"
33 #include <winbase.h>
34 //#include "winerror.h"
35 #include <objbase.h>
36 #include <rpcproxy.h>
37 #include <dinput.h>
38
39 WINE_DEFAULT_DEBUG_CHANNEL(dinput);
40
41 static HINSTANCE instance;
42 static LONG dll_count;
43
44 /*
45 * Dll lifetime tracking declaration
46 */
47 static void LockModule(void)
48 {
49 InterlockedIncrement(&dll_count);
50 }
51
52 static void UnlockModule(void)
53 {
54 InterlockedDecrement(&dll_count);
55 }
56
57 /******************************************************************************
58 * DirectInput8Create (DINPUT8.@)
59 */
60 HRESULT WINAPI DECLSPEC_HOTPATCH DirectInput8Create(HINSTANCE hinst, DWORD dwVersion, REFIID riid, LPVOID *ppDI, LPUNKNOWN punkOuter) {
61 IDirectInputA *pDI;
62 HRESULT hr, hrCo;
63
64 TRACE("hInst (%p), dwVersion: %d, riid (%s), punkOuter (%p)\n", hinst, dwVersion, debugstr_guid(riid), punkOuter);
65
66 if (!ppDI)
67 return E_POINTER;
68
69 if (!IsEqualGUID(&IID_IDirectInput8A, riid) &&
70 !IsEqualGUID(&IID_IDirectInput8W, riid) &&
71 !IsEqualGUID(&IID_IUnknown, riid))
72 {
73 *ppDI = NULL;
74 return DIERR_NOINTERFACE;
75 }
76
77 hrCo = CoInitialize(NULL);
78
79 hr = CoCreateInstance(&CLSID_DirectInput, NULL, CLSCTX_INPROC_SERVER, &IID_IDirectInputA, (void **)&pDI);
80
81 /* Ensure balance of calls. */
82 if (SUCCEEDED(hrCo))
83 CoUninitialize();
84
85 if (FAILED(hr)) {
86 ERR("CoCreateInstance failed with hr = 0x%08x\n", hr);
87 return hr;
88 }
89
90 hr = IDirectInput_QueryInterface(pDI, riid, ppDI);
91 IDirectInput_Release(pDI);
92
93 if (FAILED(hr))
94 return hr;
95
96 /* When aggregation is used (punkOuter!=NULL) the application needs to manually call Initialize. */
97 if(punkOuter == NULL && IsEqualGUID(&IID_IDirectInput8A, riid)) {
98 IDirectInput8A *DI = *ppDI;
99
100 hr = IDirectInput8_Initialize(DI, hinst, dwVersion);
101 if (FAILED(hr))
102 {
103 IDirectInput8_Release(DI);
104 *ppDI = NULL;
105 return hr;
106 }
107 }
108
109 if(punkOuter == NULL && IsEqualGUID(&IID_IDirectInput8W, riid)) {
110 IDirectInput8W *DI = *ppDI;
111
112 hr = IDirectInput8_Initialize(DI, hinst, dwVersion);
113 if (FAILED(hr))
114 {
115 IDirectInput8_Release(DI);
116 *ppDI = NULL;
117 return hr;
118 }
119 }
120
121 return S_OK;
122 }
123
124 /*******************************************************************************
125 * DirectInput8 ClassFactory
126 */
127 typedef struct
128 {
129 /* IUnknown fields */
130 IClassFactory IClassFactory_iface;
131 } IClassFactoryImpl;
132
133 static inline IClassFactoryImpl *impl_from_IClassFactory(IClassFactory *iface)
134 {
135 return CONTAINING_RECORD(iface, IClassFactoryImpl, IClassFactory_iface);
136 }
137
138 static HRESULT WINAPI DI8CF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj) {
139 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
140 FIXME("%p %s %p\n",This,debugstr_guid(riid),ppobj);
141 return E_NOINTERFACE;
142 }
143
144 static ULONG WINAPI DI8CF_AddRef(LPCLASSFACTORY iface) {
145 LockModule();
146 return 2;
147 }
148
149 static ULONG WINAPI DI8CF_Release(LPCLASSFACTORY iface) {
150 UnlockModule();
151 return 1;
152 }
153
154 static HRESULT WINAPI DI8CF_CreateInstance(LPCLASSFACTORY iface,LPUNKNOWN pOuter,REFIID riid,LPVOID *ppobj) {
155 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
156
157 TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,debugstr_guid(riid),ppobj);
158 if( IsEqualGUID( &IID_IDirectInput8A, riid ) || IsEqualGUID( &IID_IDirectInput8W, riid ) || IsEqualGUID( &IID_IUnknown, riid )) {
159 IDirectInputA *ppDI;
160 HRESULT hr;
161
162 hr = CoCreateInstance(&CLSID_DirectInput, NULL, CLSCTX_INPROC_SERVER, &IID_IDirectInputA, (void **)&ppDI);
163 if (FAILED(hr))
164 return hr;
165
166 hr = IDirectInput_QueryInterface(ppDI, riid, ppobj);
167 IDirectInput_Release(ppDI);
168
169 return hr;
170 }
171
172 ERR("(%p,%p,%s,%p) Interface not found!\n",This,pOuter,debugstr_guid(riid),ppobj);
173 return E_NOINTERFACE;
174 }
175
176 static HRESULT WINAPI DI8CF_LockServer(LPCLASSFACTORY iface,BOOL dolock) {
177 TRACE("(%p)->(%d)\n", iface, dolock);
178
179 if(dolock)
180 LockModule();
181 else
182 UnlockModule();
183
184 return S_OK;
185 }
186
187 static const IClassFactoryVtbl DI8CF_Vtbl = {
188 DI8CF_QueryInterface,
189 DI8CF_AddRef,
190 DI8CF_Release,
191 DI8CF_CreateInstance,
192 DI8CF_LockServer
193 };
194 static IClassFactoryImpl DINPUT8_CF = { { &DI8CF_Vtbl } };
195
196
197 /***********************************************************************
198 * DllCanUnloadNow (DINPUT8.@)
199 */
200 HRESULT WINAPI DllCanUnloadNow(void)
201 {
202 return dll_count == 0 ? S_OK : S_FALSE;
203 }
204
205 /***********************************************************************
206 * DllGetClassObject (DINPUT8.@)
207 */
208 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
209 {
210 TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
211 if ( IsEqualCLSID( &IID_IClassFactory, riid ) ) {
212 *ppv = &DINPUT8_CF;
213 IClassFactory_AddRef((IClassFactory*)*ppv);
214 return S_OK;
215 }
216
217 FIXME("(%s,%s,%p): no interface found.\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
218 return CLASS_E_CLASSNOTAVAILABLE;
219 }
220
221 /***********************************************************************
222 * DllMain
223 */
224 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD reason, LPVOID lpv)
225 {
226 switch (reason)
227 {
228 case DLL_PROCESS_ATTACH:
229 instance = hInstDLL;
230 DisableThreadLibraryCalls( hInstDLL );
231 break;
232 }
233 return TRUE;
234 }
235
236 /***********************************************************************
237 * DllRegisterServer (DINPUT8.@)
238 */
239 HRESULT WINAPI DllRegisterServer(void)
240 {
241 return __wine_register_resources( instance );
242 }
243
244 /***********************************************************************
245 * DllUnregisterServer (DINPUT8.@)
246 */
247 HRESULT WINAPI DllUnregisterServer(void)
248 {
249 return __wine_unregister_resources( instance );
250 }