dinput8_main.c 6.09 KB
Newer Older
1 2 3
/* DirectInput 8
 *
 * Copyright 2002 TransGaming Technologies Inc.
4
 * Copyright 2006 Roderick Colenbrander
5 6 7 8 9 10 11 12 13 14 15 16 17
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * 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 22
 */

#include "config.h"
#include <assert.h>
23
#include <stdarg.h>
24 25
#include <string.h>

26 27
#define COBJMACROS

28
#include "wine/debug.h"
29
#include "windef.h"
30 31
#include "winbase.h"
#include "winerror.h"
32 33
#include "objbase.h"
#include "rpcproxy.h"
34 35 36
#include "dinput.h"

WINE_DEFAULT_DEBUG_CHANNEL(dinput);
37 38

static HINSTANCE instance;
39 40 41 42 43 44 45 46 47 48 49 50 51 52
static LONG dll_count;

/*
 * Dll lifetime tracking declaration
 */
static void LockModule(void)
{
    InterlockedIncrement(&dll_count);
}

static void UnlockModule(void)
{
    InterlockedDecrement(&dll_count);
}
53 54 55 56

/******************************************************************************
 *	DirectInput8Create (DINPUT8.@)
 */
57
HRESULT WINAPI DECLSPEC_HOTPATCH DirectInput8Create(HINSTANCE hinst, DWORD dwVersion, REFIID riid, LPVOID *ppDI, LPUNKNOWN punkOuter) {
58
    HRESULT hr, hrCo;
59

60
    TRACE("hInst (%p), dwVersion: %d, riid (%s), punkOuter (%p))\n", hinst, dwVersion, debugstr_guid(riid), punkOuter);
61 62 63 64 65 66 67 68

    /* The specified version needs to be dinput8 (0x800) or higher */
    if(dwVersion < 0x800)
        return DIERR_OLDDIRECTINPUTVERSION;

    if( !(IsEqualGUID(&IID_IDirectInput8A, riid) || IsEqualGUID(&IID_IDirectInput8W, riid) || IsEqualGUID(&IID_IUnknown, riid)) )
        return DIERR_INVALIDPARAM;

69
    hrCo = CoInitialize(NULL);
70 71 72
    
    hr = CoCreateInstance( &CLSID_DirectInput8, punkOuter, CLSCTX_INPROC_SERVER, riid, ppDI);
    if(FAILED(hr)) {
73
        ERR("CoCreateInstance failed with hr = %d\n", hr);
74 75 76
        return DIERR_INVALIDPARAM;
    }

77 78 79
    /*  ensure balance of calls */
    if(hrCo == S_OK || hrCo == S_FALSE)
        CoUninitialize();
80 81 82

    /* When aggregation is used (punkOuter!=NULL) the application needs to manually call Initialize. */
    if(punkOuter == NULL && IsEqualGUID(&IID_IDirectInput8A, riid)) {
83
        LPDIRECTINPUTA DI = *ppDI;
84 85 86 87
        IDirectInput8_Initialize(DI, hinst, dwVersion);
    }

    if(punkOuter == NULL && IsEqualGUID(&IID_IDirectInput8W, riid)) {
88
        LPDIRECTINPUTW DI = *ppDI;
89 90 91 92
        IDirectInput8_Initialize(DI, hinst, dwVersion);
    }

    return S_OK;
93 94 95 96 97 98 99 100
}

/*******************************************************************************
 * DirectInput8 ClassFactory
 */
typedef struct
{
    /* IUnknown fields */
101
    IClassFactory IClassFactory_iface;
102 103
} IClassFactoryImpl;

104 105 106 107 108
static inline IClassFactoryImpl *impl_from_IClassFactory(IClassFactory *iface)
{
    return CONTAINING_RECORD(iface, IClassFactoryImpl, IClassFactory_iface);
}

109
static HRESULT WINAPI DI8CF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj) {
110
    IClassFactoryImpl *This = impl_from_IClassFactory(iface);
111 112 113 114 115 116 117
    FIXME("%p %s %p\n",This,debugstr_guid(riid),ppobj);
    return E_NOINTERFACE;
}

static ULONG WINAPI DI8CF_AddRef(LPCLASSFACTORY iface) {
    LockModule();
    return 2;
118 119
}

120 121 122 123 124 125
static ULONG WINAPI DI8CF_Release(LPCLASSFACTORY iface) {
    UnlockModule();
    return 1;
}

static HRESULT WINAPI DI8CF_CreateInstance(LPCLASSFACTORY iface,LPUNKNOWN pOuter,REFIID riid,LPVOID *ppobj) {
126
    IClassFactoryImpl *This = impl_from_IClassFactory(iface);
127 128

    TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,debugstr_guid(riid),ppobj);
129 130
    if( IsEqualGUID( &IID_IDirectInput8A, riid ) || IsEqualGUID( &IID_IDirectInput8W, riid ) || IsEqualGUID( &IID_IUnknown, riid )) {
        return DirectInputCreateEx(0, DIRECTINPUT_VERSION, riid, ppobj, pOuter);
131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154
    }

    ERR("(%p,%p,%s,%p) Interface not found!\n",This,pOuter,debugstr_guid(riid),ppobj);    
    return E_NOINTERFACE;
}

static HRESULT WINAPI DI8CF_LockServer(LPCLASSFACTORY iface,BOOL dolock) {
    TRACE("(%p)->(%d)\n", iface, dolock);

    if(dolock)
        LockModule();
    else
        UnlockModule();

    return S_OK;
}

static const IClassFactoryVtbl DI8CF_Vtbl = {
    DI8CF_QueryInterface,
    DI8CF_AddRef,
    DI8CF_Release,
    DI8CF_CreateInstance,
    DI8CF_LockServer
};
155
static IClassFactoryImpl DINPUT8_CF = { { &DI8CF_Vtbl } };
156 157


158 159 160
/***********************************************************************
 *		DllCanUnloadNow (DINPUT8.@)
 */
161
HRESULT WINAPI DllCanUnloadNow(void)
162
{
163
    return dll_count == 0 ? S_OK : S_FALSE;
164 165 166 167 168
}

/***********************************************************************
 *		DllGetClassObject (DINPUT8.@)
 */
169
HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
170
{
171 172
    TRACE("(%s,%s,%p)\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
    if ( IsEqualCLSID( &IID_IClassFactory, riid ) ) {
173
        *ppv = &DINPUT8_CF;
174 175 176
        IClassFactory_AddRef((IClassFactory*)*ppv);
        return S_OK;
    }
177

178
    FIXME("(%s,%s,%p): no interface found.\n", debugstr_guid(rclsid), debugstr_guid(riid), ppv);
179 180
    return CLASS_E_CLASSNOTAVAILABLE;
}
181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211

/***********************************************************************
 *		DllMain
 */
BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD reason, LPVOID lpv)
{
    switch (reason)
    {
    case DLL_PROCESS_ATTACH:
        instance = hInstDLL;
        DisableThreadLibraryCalls( hInstDLL );
        break;
    }
    return TRUE;
}

/***********************************************************************
 *		DllRegisterServer (DINPUT8.@)
 */
HRESULT WINAPI DllRegisterServer(void)
{
    return __wine_register_resources( instance, NULL );
}

/***********************************************************************
 *		DllUnregisterServer (DINPUT8.@)
 */
HRESULT WINAPI DllUnregisterServer(void)
{
    return __wine_unregister_resources( instance, NULL );
}