dinput8: Use an iface instead of a vtbl pointer in IClassFactoryImpl.
[wine] / dlls / 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
28 #include "wine/debug.h"
29 #include "windef.h"
30 #include "winbase.h"
31 #include "winerror.h"
32 #include "dinput.h"
33
34 WINE_DEFAULT_DEBUG_CHANNEL(dinput);
35 static LONG dll_count;
36
37 /*
38  * Dll lifetime tracking declaration
39  */
40 static void LockModule(void)
41 {
42     InterlockedIncrement(&dll_count);
43 }
44
45 static void UnlockModule(void)
46 {
47     InterlockedDecrement(&dll_count);
48 }
49
50 /******************************************************************************
51  *      DirectInput8Create (DINPUT8.@)
52  */
53 HRESULT WINAPI DECLSPEC_HOTPATCH DirectInput8Create(HINSTANCE hinst, DWORD dwVersion, REFIID riid, LPVOID *ppDI, LPUNKNOWN punkOuter) {
54     HRESULT hr, hrCo;
55
56     TRACE("hInst (%p), dwVersion: %d, riid (%s), punkOuter (%p))\n", hinst, dwVersion, debugstr_guid(riid), punkOuter);
57
58     /* The specified version needs to be dinput8 (0x800) or higher */
59     if(dwVersion < 0x800)
60         return DIERR_OLDDIRECTINPUTVERSION;
61
62     if( !(IsEqualGUID(&IID_IDirectInput8A, riid) || IsEqualGUID(&IID_IDirectInput8W, riid) || IsEqualGUID(&IID_IUnknown, riid)) )
63         return DIERR_INVALIDPARAM;
64
65     hrCo = CoInitialize(NULL);
66     
67     hr = CoCreateInstance( &CLSID_DirectInput8, punkOuter, CLSCTX_INPROC_SERVER, riid, ppDI);
68     if(FAILED(hr)) {
69         ERR("CoCreateInstance failed with hr = %d\n", hr);
70         return DIERR_INVALIDPARAM;
71     }
72
73     /*  ensure balance of calls */
74     if(hrCo == S_OK || hrCo == S_FALSE)
75         CoUninitialize();
76
77     /* When aggregation is used (punkOuter!=NULL) the application needs to manually call Initialize. */
78     if(punkOuter == NULL && IsEqualGUID(&IID_IDirectInput8A, riid)) {
79         LPDIRECTINPUTA DI = *ppDI;
80         IDirectInput8_Initialize(DI, hinst, dwVersion);
81     }
82
83     if(punkOuter == NULL && IsEqualGUID(&IID_IDirectInput8W, riid)) {
84         LPDIRECTINPUTW DI = *ppDI;
85         IDirectInput8_Initialize(DI, hinst, dwVersion);
86     }
87
88     return S_OK;
89 }
90
91 /*******************************************************************************
92  * DirectInput8 ClassFactory
93  */
94 typedef struct
95 {
96     /* IUnknown fields */
97     IClassFactory IClassFactory_iface;
98 } IClassFactoryImpl;
99
100 static inline IClassFactoryImpl *impl_from_IClassFactory(IClassFactory *iface)
101 {
102     return CONTAINING_RECORD(iface, IClassFactoryImpl, IClassFactory_iface);
103 }
104
105 static HRESULT WINAPI DI8CF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj) {
106     IClassFactoryImpl *This = impl_from_IClassFactory(iface);
107     FIXME("%p %s %p\n",This,debugstr_guid(riid),ppobj);
108     return E_NOINTERFACE;
109 }
110
111 static ULONG WINAPI DI8CF_AddRef(LPCLASSFACTORY iface) {
112     LockModule();
113     return 2;
114 }
115
116 static ULONG WINAPI DI8CF_Release(LPCLASSFACTORY iface) {
117     UnlockModule();
118     return 1;
119 }
120
121 static HRESULT WINAPI DI8CF_CreateInstance(LPCLASSFACTORY iface,LPUNKNOWN pOuter,REFIID riid,LPVOID *ppobj) {
122     IClassFactoryImpl *This = impl_from_IClassFactory(iface);
123
124     TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,debugstr_guid(riid),ppobj);
125     if( IsEqualGUID( &IID_IDirectInput8A, riid ) || IsEqualGUID( &IID_IDirectInput8W, riid ) || IsEqualGUID( &IID_IUnknown, riid )) {
126         return DirectInputCreateEx(0, DIRECTINPUT_VERSION, riid, ppobj, pOuter);
127     }
128
129     ERR("(%p,%p,%s,%p) Interface not found!\n",This,pOuter,debugstr_guid(riid),ppobj);    
130     return E_NOINTERFACE;
131 }
132
133 static HRESULT WINAPI DI8CF_LockServer(LPCLASSFACTORY iface,BOOL dolock) {
134     TRACE("(%p)->(%d)\n", iface, dolock);
135
136     if(dolock)
137         LockModule();
138     else
139         UnlockModule();
140
141     return S_OK;
142 }
143
144 static const IClassFactoryVtbl DI8CF_Vtbl = {
145     DI8CF_QueryInterface,
146     DI8CF_AddRef,
147     DI8CF_Release,
148     DI8CF_CreateInstance,
149     DI8CF_LockServer
150 };
151 static IClassFactoryImpl DINPUT8_CF = { { &DI8CF_Vtbl } };
152
153
154 /***********************************************************************
155  *              DllCanUnloadNow (DINPUT8.@)
156  */
157 HRESULT WINAPI DllCanUnloadNow(void)
158 {
159     return dll_count == 0 ? S_OK : S_FALSE;
160 }
161
162 /***********************************************************************
163  *              DllGetClassObject (DINPUT8.@)
164  */
165 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
166 {
167     TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
168     if ( IsEqualCLSID( &IID_IClassFactory, riid ) ) {
169         *ppv = &DINPUT8_CF;
170         IClassFactory_AddRef((IClassFactory*)*ppv);
171         return S_OK;
172     }
173
174     FIXME("(%s,%s,%p): no interface found.\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
175     return CLASS_E_CLASSNOTAVAILABLE;
176 }