Add support for more than one sound card.
[wine] / dlls / itss / itss.c
1 /*
2  *    ITSS Class Factory
3  *
4  * Copyright 2002 Lionel Ulmer
5  * Copyright 2004 Mike McCormack
6  *
7  *  see http://bonedaddy.net/pabs3/hhm/#chmspec
8  *
9  * This library is free software; you can redistribute it and/or
10  * modify it under the terms of the GNU Lesser General Public
11  * License as published by the Free Software Foundation; either
12  * version 2.1 of the License, or (at your option) any later version.
13  *
14  * This library is distributed in the hope that it will be useful,
15  * but WITHOUT ANY WARRANTY; without even the implied warranty of
16  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
17  * Lesser General Public License for more details.
18  *
19  * You should have received a copy of the GNU Lesser General Public
20  * License along with this library; if not, write to the Free Software
21  * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
22  */
23
24 #include "config.h"
25
26 #include <stdarg.h>
27 #include <stdio.h>
28
29 #define COBJMACROS
30
31 #include "windef.h"
32 #include "winbase.h"
33 #include "winuser.h"
34 #include "winnls.h"
35 #include "winreg.h"
36 #include "ole2.h"
37
38 #include "itss.h"
39 #include "uuids.h"
40
41 #include "wine/unicode.h"
42 #include "wine/debug.h"
43
44 #include "itsstor.h"
45
46 WINE_DEFAULT_DEBUG_CHANNEL(itss);
47
48 #include "initguid.h"
49
50 DEFINE_GUID(CLSID_ITStorage,0x5d02926a,0x212e,0x11d0,0x9d,0xf9,0x00,0xa0,0xc9,0x22,0xe6,0xec );
51 DEFINE_GUID(CLSID_ITSProtocol,0x9d148290,0xb9c8,0x11d0,0xa4,0xcc,0x00,0x00,0xf8,0x01,0x49,0xf6);
52 DEFINE_GUID(IID_IITStorage, 0x88cc31de, 0x27ab, 0x11d0, 0x9d, 0xf9, 0x0, 0xa0, 0xc9, 0x22, 0xe6, 0xec);
53
54 static HRESULT ITSS_create(IUnknown *pUnkOuter, LPVOID *ppObj);
55
56 ULONG dll_count = 0;
57
58 BOOL WINAPI DllMain(HINSTANCE hInstDLL, DWORD fdwReason, LPVOID lpv)
59 {
60     switch(fdwReason) {
61     case DLL_PROCESS_ATTACH:
62         DisableThreadLibraryCalls(hInstDLL);
63         break;
64     case DLL_PROCESS_DETACH:
65         break;
66     }
67     return TRUE;
68 }
69
70 /******************************************************************************
71  * ITSS ClassFactory
72  */
73 typedef struct {
74     IClassFactory ITF_IClassFactory;
75
76     DWORD ref;
77     HRESULT (*pfnCreateInstance)(IUnknown *pUnkOuter, LPVOID *ppObj);
78 } IClassFactoryImpl;
79
80 struct object_creation_info
81 {
82     const CLSID *clsid;
83     LPCSTR szClassName;
84     HRESULT (*pfnCreateInstance)(IUnknown *pUnkOuter, LPVOID *ppObj);
85 };
86
87 static const struct object_creation_info object_creation[] =
88 {
89     { &CLSID_ITStorage, "ITStorage", ITSS_create },
90     { &CLSID_ITSProtocol, "ITSProtocol", ITS_IParseDisplayName_create },
91 };
92
93 static HRESULT WINAPI
94 ITSSCF_QueryInterface(LPCLASSFACTORY iface,REFIID riid,LPVOID *ppobj)
95 {
96     IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
97
98     if (IsEqualGUID(riid, &IID_IUnknown)
99         || IsEqualGUID(riid, &IID_IClassFactory))
100     {
101         IClassFactory_AddRef(iface);
102         *ppobj = This;
103         return S_OK;
104     }
105
106     WARN("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppobj);
107     return E_NOINTERFACE;
108 }
109
110 static ULONG WINAPI ITSSCF_AddRef(LPCLASSFACTORY iface)
111 {
112     IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
113     return InterlockedIncrement(&This->ref);
114 }
115
116 static ULONG WINAPI ITSSCF_Release(LPCLASSFACTORY iface)
117 {
118     IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
119
120     ULONG ref = InterlockedDecrement(&This->ref);
121
122     if (ref == 0) {
123         HeapFree(GetProcessHeap(), 0, This);
124         InterlockedDecrement(&dll_count);
125     }
126
127     return ref;
128 }
129
130
131 static HRESULT WINAPI ITSSCF_CreateInstance(LPCLASSFACTORY iface, LPUNKNOWN pOuter,
132                                           REFIID riid, LPVOID *ppobj)
133 {
134     IClassFactoryImpl *This = (IClassFactoryImpl *)iface;
135     HRESULT hres;
136     LPUNKNOWN punk;
137     
138     TRACE("(%p)->(%p,%s,%p)\n",This,pOuter,debugstr_guid(riid),ppobj);
139
140     *ppobj = NULL;
141     hres = This->pfnCreateInstance(pOuter, (LPVOID *) &punk);
142     if (SUCCEEDED(hres)) {
143         hres = IUnknown_QueryInterface(punk, riid, ppobj);
144         IUnknown_Release(punk);
145     }
146     return hres;
147 }
148
149 static HRESULT WINAPI ITSSCF_LockServer(LPCLASSFACTORY iface, BOOL dolock)
150 {
151     TRACE("(%p)->(%d)\n", iface, dolock);
152
153     if(dolock)
154         InterlockedIncrement(&dll_count);
155     else
156         InterlockedDecrement(&dll_count);
157
158     return S_OK;
159 }
160
161 static IClassFactoryVtbl ITSSCF_Vtbl =
162 {
163     ITSSCF_QueryInterface,
164     ITSSCF_AddRef,
165     ITSSCF_Release,
166     ITSSCF_CreateInstance,
167     ITSSCF_LockServer
168 };
169
170
171 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID iid, LPVOID *ppv)
172 {
173     DWORD i;
174     IClassFactoryImpl *factory;
175
176     TRACE("%s %s %p\n",debugstr_guid(rclsid), debugstr_guid(iid), ppv);
177     
178     if ( !IsEqualGUID( &IID_IClassFactory, iid )
179          && ! IsEqualGUID( &IID_IUnknown, iid) )
180         return E_NOINTERFACE;
181
182     for (i=0; i < sizeof(object_creation)/sizeof(object_creation[0]); i++)
183     {
184         if (IsEqualGUID(object_creation[i].clsid, rclsid))
185             break;
186     }
187
188     if (i == sizeof(object_creation)/sizeof(object_creation[0]))
189     {
190         FIXME("%s: no class found.\n", debugstr_guid(rclsid));
191         return CLASS_E_CLASSNOTAVAILABLE;
192     }
193
194     TRACE("Creating a class factory for %s\n",object_creation[i].szClassName);
195
196     factory = HeapAlloc(GetProcessHeap(), 0, sizeof(*factory));
197     if (factory == NULL) return E_OUTOFMEMORY;
198
199     factory->ITF_IClassFactory.lpVtbl = &ITSSCF_Vtbl;
200     factory->ref = 1;
201
202     factory->pfnCreateInstance = object_creation[i].pfnCreateInstance;
203
204     *ppv = &(factory->ITF_IClassFactory);
205     InterlockedIncrement(&dll_count);
206
207     TRACE("(%p) <- %p\n", ppv, &(factory->ITF_IClassFactory) );
208
209     return S_OK;
210 }
211
212 /*****************************************************************************/
213
214 typedef struct {
215     IITStorageVtbl *vtbl_IITStorage;
216     DWORD ref;
217 } ITStorageImpl;
218
219
220 HRESULT WINAPI ITStorageImpl_QueryInterface(
221     IITStorage* iface,
222     REFIID riid,
223     void** ppvObject)
224 {
225     ITStorageImpl *This = (ITStorageImpl *)iface;
226     if (IsEqualGUID(riid, &IID_IUnknown)
227         || IsEqualGUID(riid, &IID_IITStorage))
228     {
229         IClassFactory_AddRef(iface);
230         *ppvObject = This;
231         return S_OK;
232     }
233
234     WARN("(%p)->(%s,%p),not found\n",This,debugstr_guid(riid),ppvObject);
235     return E_NOINTERFACE;
236 }
237
238 ULONG WINAPI ITStorageImpl_AddRef(
239     IITStorage* iface)
240 {
241     ITStorageImpl *This = (ITStorageImpl *)iface;
242     TRACE("%p\n", This);
243     return InterlockedIncrement(&This->ref);
244 }
245
246 ULONG WINAPI ITStorageImpl_Release(
247     IITStorage* iface)
248 {
249     ITStorageImpl *This = (ITStorageImpl *)iface;
250     ULONG ref = InterlockedDecrement(&This->ref);
251
252     if (ref == 0) {
253         HeapFree(GetProcessHeap(), 0, This);
254         InterlockedDecrement(&dll_count);
255     }
256
257     return ref;
258 }
259
260 HRESULT WINAPI ITStorageImpl_StgCreateDocfile(
261     IITStorage* iface,
262     const WCHAR* pwcsName,
263     DWORD grfMode,
264     DWORD reserved,
265     IStorage** ppstgOpen)
266 {
267     ITStorageImpl *This = (ITStorageImpl *)iface;
268
269     TRACE("%p %s %lu %lu %p\n", This,
270           debugstr_w(pwcsName), grfMode, reserved, ppstgOpen );
271
272     return ITSS_StgOpenStorage( pwcsName, NULL, grfMode,
273                                 0, reserved, ppstgOpen);
274 }
275
276 HRESULT WINAPI ITStorageImpl_StgCreateDocfileOnILockBytes(
277     IITStorage* iface,
278     ILockBytes* plkbyt,
279     DWORD grfMode,
280     DWORD reserved,
281     IStorage** ppstgOpen)
282 {
283     ITStorageImpl *This = (ITStorageImpl *)iface;
284     FIXME("%p\n", This);
285     return E_NOTIMPL;
286 }
287
288 HRESULT WINAPI ITStorageImpl_StgIsStorageFile(
289     IITStorage* iface,
290     const WCHAR* pwcsName)
291 {
292     ITStorageImpl *This = (ITStorageImpl *)iface;
293     FIXME("%p\n", This);
294     return E_NOTIMPL;
295 }
296
297 HRESULT WINAPI ITStorageImpl_StgIsStorageILockBytes(
298     IITStorage* iface,
299     ILockBytes* plkbyt)
300 {
301     ITStorageImpl *This = (ITStorageImpl *)iface;
302     FIXME("%p\n", This);
303     return E_NOTIMPL;
304 }
305
306 HRESULT WINAPI ITStorageImpl_StgOpenStorage(
307     IITStorage* iface,
308     const WCHAR* pwcsName,
309     IStorage* pstgPriority,
310     DWORD grfMode,
311     SNB snbExclude,
312     DWORD reserved,
313     IStorage** ppstgOpen)
314 {
315     ITStorageImpl *This = (ITStorageImpl *)iface;
316
317     TRACE("%p %s %p %ld %p\n", This, debugstr_w( pwcsName ),
318            pstgPriority, grfMode, snbExclude );
319
320     return ITSS_StgOpenStorage( pwcsName, pstgPriority, grfMode,
321                                 snbExclude, reserved, ppstgOpen);
322 }
323
324 HRESULT WINAPI ITStorageImpl_StgOpenStorageOnILockBytes(
325     IITStorage* iface,
326     ILockBytes* plkbyt,
327     IStorage* pStgPriority,
328     DWORD grfMode,
329     SNB snbExclude,
330     DWORD reserved,
331     IStorage** ppstgOpen)
332 {
333     ITStorageImpl *This = (ITStorageImpl *)iface;
334     FIXME("%p\n", This);
335     return E_NOTIMPL;
336 }
337
338 HRESULT WINAPI ITStorageImpl_StgSetTimes(
339     IITStorage* iface,
340     WCHAR* lpszName,
341     FILETIME* pctime,
342     FILETIME* patime,
343     FILETIME* pmtime)
344 {
345     ITStorageImpl *This = (ITStorageImpl *)iface;
346     FIXME("%p\n", This);
347     return E_NOTIMPL;
348 }
349
350 HRESULT WINAPI ITStorageImpl_SetControlData(
351     IITStorage* iface,
352     PITS_Control_Data pControlData)
353 {
354     ITStorageImpl *This = (ITStorageImpl *)iface;
355     FIXME("%p\n", This);
356     return E_NOTIMPL;
357 }
358
359 HRESULT WINAPI ITStorageImpl_DefaultControlData(
360     IITStorage* iface,
361     PITS_Control_Data* ppControlData)
362 {
363     ITStorageImpl *This = (ITStorageImpl *)iface;
364     FIXME("%p\n", This);
365     return E_NOTIMPL;
366 }
367
368 HRESULT WINAPI ITStorageImpl_Compact(
369     IITStorage* iface,
370     const WCHAR* pwcsName,
371     ECompactionLev iLev)
372 {
373     ITStorageImpl *This = (ITStorageImpl *)iface;
374     FIXME("%p\n", This);
375     return E_NOTIMPL;
376 }
377
378 static IITStorageVtbl ITStorageImpl_Vtbl =
379 {
380     ITStorageImpl_QueryInterface,
381     ITStorageImpl_AddRef,
382     ITStorageImpl_Release,
383     ITStorageImpl_StgCreateDocfile,
384     ITStorageImpl_StgCreateDocfileOnILockBytes,
385     ITStorageImpl_StgIsStorageFile,
386     ITStorageImpl_StgIsStorageILockBytes,
387     ITStorageImpl_StgOpenStorage,
388     ITStorageImpl_StgOpenStorageOnILockBytes,
389     ITStorageImpl_StgSetTimes,
390     ITStorageImpl_SetControlData,
391     ITStorageImpl_DefaultControlData,
392     ITStorageImpl_Compact,
393 };
394
395 static HRESULT ITSS_create(IUnknown *pUnkOuter, LPVOID *ppObj)
396 {
397     ITStorageImpl *its;
398
399     if( pUnkOuter )
400         return CLASS_E_NOAGGREGATION;
401
402     its = HeapAlloc( GetProcessHeap(), 0, sizeof(ITStorageImpl) );
403     its->vtbl_IITStorage = &ITStorageImpl_Vtbl;
404     its->ref = 1;
405
406     TRACE("-> %p\n", its);
407     *ppObj = (LPVOID) its;
408     InterlockedIncrement(&dll_count);
409
410     return S_OK;
411 }
412
413 /*****************************************************************************/
414
415 HRESULT WINAPI DllRegisterServer(void)
416 {
417     FIXME("\n");
418     return S_OK;
419 }
420
421 HRESULT WINAPI DllCanUnloadNow(void)
422 {
423     TRACE("dll_count = %lu\n", dll_count);
424     return dll_count ? S_FALSE : S_OK;
425 }