1 /*
2  * Device Enumeration
3  *
4  * Copyright (C) 2002 John K. Hohm
5  * Copyright (C) 2002 Robert Shearman
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Lesser General Public
9  * License as published by the Free Software Foundation; either
10  * version 2.1 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser General Public
18  * License along with this library; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20  */
21 
22 #include "devenum_private.h"
23 #include "rpcproxy.h"
24 #include "wine/debug.h"
25 
26 WINE_DEFAULT_DEBUG_CHANNEL(devenum);
27 
28 DECLSPEC_HIDDEN LONG dll_refs;
29 static HINSTANCE devenum_instance;
30 
31 #ifdef __REACTOS__
32 static void DEVENUM_RegisterQuartz(void);
33 #endif
34 
35 /***********************************************************************
36  *		DllEntryPoint
37  */
38 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID fImpLoad)
39 {
40     TRACE("%p 0x%x %p\n", hinstDLL, fdwReason, fImpLoad);
41 
42     switch(fdwReason) {
43     case DLL_PROCESS_ATTACH:
44         devenum_instance = hinstDLL;
45         DisableThreadLibraryCalls(hinstDLL);
46 	break;
47     }
48     return TRUE;
49 }
50 
51 struct class_factory
52 {
53     IClassFactory IClassFactory_iface;
54     IUnknown *obj;
55 };
56 
57 static inline struct class_factory *impl_from_IClassFactory( IClassFactory *iface )
58 {
59     return CONTAINING_RECORD( iface, struct class_factory, IClassFactory_iface );
60 }
61 
62 static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID iid, void **obj)
63 {
64     TRACE("(%p, %s, %p)\n", iface, debugstr_guid(iid), obj);
65 
66     if (IsEqualGUID(iid, &IID_IUnknown) || IsEqualGUID(iid, &IID_IClassFactory))
67     {
68         IClassFactory_AddRef(iface);
69         *obj = iface;
70         return S_OK;
71     }
72 
73     *obj = NULL;
74     WARN("no interface for %s\n", debugstr_guid(iid));
75     return E_NOINTERFACE;
76 }
77 
78 static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
79 {
80     DEVENUM_LockModule();
81     return 2;
82 }
83 
84 static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
85 {
86     DEVENUM_UnlockModule();
87     return 1;
88 }
89 
90 static HRESULT WINAPI ClassFactory_CreateInstance(IClassFactory *iface,
91     IUnknown *outer, REFIID iid, void **obj)
92 {
93     struct class_factory *This = impl_from_IClassFactory( iface );
94 
95     TRACE("(%p, %s, %p)\n", outer, debugstr_guid(iid), obj);
96 
97     if (!obj) return E_POINTER;
98 
99     if (outer) return CLASS_E_NOAGGREGATION;
100 
101     return IUnknown_QueryInterface(This->obj, iid, obj);
102 }
103 
104 static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL lock)
105 {
106     if (lock)
107         DEVENUM_LockModule();
108     else
109         DEVENUM_UnlockModule();
110     return S_OK;
111 }
112 
113 static const IClassFactoryVtbl ClassFactory_vtbl = {
114     ClassFactory_QueryInterface,
115     ClassFactory_AddRef,
116     ClassFactory_Release,
117     ClassFactory_CreateInstance,
118     ClassFactory_LockServer
119 };
120 
121 static struct class_factory create_devenum_cf = { { &ClassFactory_vtbl }, (IUnknown *)&DEVENUM_CreateDevEnum };
122 static struct class_factory device_moniker_cf = { { &ClassFactory_vtbl }, (IUnknown *)&DEVENUM_ParseDisplayName };
123 
124 /***********************************************************************
125  *		DllGetClassObject (DEVENUM.@)
126  */
127 HRESULT WINAPI DllGetClassObject(REFCLSID clsid, REFIID iid, void **obj)
128 {
129     TRACE("(%s, %s, %p)\n", debugstr_guid(clsid), debugstr_guid(iid), obj);
130 
131     *obj = NULL;
132 
133     if (IsEqualGUID(clsid, &CLSID_SystemDeviceEnum))
134         return IClassFactory_QueryInterface(&create_devenum_cf.IClassFactory_iface, iid, obj);
135     else if (IsEqualGUID(clsid, &CLSID_CDeviceMoniker))
136         return IClassFactory_QueryInterface(&device_moniker_cf.IClassFactory_iface, iid, obj);
137 
138     FIXME("class %s not available\n", debugstr_guid(clsid));
139     return CLASS_E_CLASSNOTAVAILABLE;
140 }
141 
142 /***********************************************************************
143  *		DllCanUnloadNow (DEVENUM.@)
144  */
145 HRESULT WINAPI DllCanUnloadNow(void)
146 {
147     return dll_refs != 0 ? S_FALSE : S_OK;
148 }
149 
150 /***********************************************************************
151  *		DllRegisterServer (DEVENUM.@)
152  */
153 HRESULT WINAPI DllRegisterServer(void)
154 {
155     HRESULT res;
156     IFilterMapper2 * pMapper = NULL;
157     LPVOID mapvptr;
158 
159     TRACE("\n");
160 
161     res = __wine_register_resources( devenum_instance );
162     if (FAILED(res))
163         return res;
164 
165 #ifdef __REACTOS__
166     /* Quartz is needed for IFilterMapper2 */
167     DEVENUM_RegisterQuartz();
168 #endif
169 
170 /*** ActiveMovieFilter Categories ***/
171 
172     CoInitialize(NULL);
173 
174     res = CoCreateInstance(&CLSID_FilterMapper2, NULL, CLSCTX_INPROC,
175                            &IID_IFilterMapper2,  &mapvptr);
176     if (SUCCEEDED(res))
177     {
178         static const WCHAR friendlyvidcap[] = {'V','i','d','e','o',' ','C','a','p','t','u','r','e',' ','S','o','u','r','c','e','s',0};
179         static const WCHAR friendlydshow[] = {'D','i','r','e','c','t','S','h','o','w',' ','F','i','l','t','e','r','s',0};
180         static const WCHAR friendlyvidcomp[] = {'V','i','d','e','o',' ','C','o','m','p','r','e','s','s','o','r','s',0};
181         static const WCHAR friendlyaudcap[] = {'A','u','d','i','o',' ','C','a','p','t','u','r','e',' ','S','o','u','r','c','e','s',0};
182         static const WCHAR friendlyaudcomp[] = {'A','u','d','i','o',' ','C','o','m','p','r','e','s','s','o','r','s',0};
183         static const WCHAR friendlyaudrend[] = {'A','u','d','i','o',' ','R','e','n','d','e','r','e','r','s',0};
184         static const WCHAR friendlymidirend[] = {'M','i','d','i',' ','R','e','n','d','e','r','e','r','s',0};
185         static const WCHAR friendlyextrend[] = {'E','x','t','e','r','n','a','l',' ','R','e','n','d','e','r','e','r','s',0};
186         static const WCHAR friendlydevctrl[] = {'D','e','v','i','c','e',' ','C','o','n','t','r','o','l',' ','F','i','l','t','e','r','s',0};
187 
188         pMapper = mapvptr;
189 
190         IFilterMapper2_CreateCategory(pMapper, &CLSID_VideoInputDeviceCategory, MERIT_DO_NOT_USE, friendlyvidcap);
191         IFilterMapper2_CreateCategory(pMapper, &CLSID_LegacyAmFilterCategory, MERIT_NORMAL, friendlydshow);
192         IFilterMapper2_CreateCategory(pMapper, &CLSID_VideoCompressorCategory, MERIT_DO_NOT_USE, friendlyvidcomp);
193         IFilterMapper2_CreateCategory(pMapper, &CLSID_AudioInputDeviceCategory, MERIT_DO_NOT_USE, friendlyaudcap);
194         IFilterMapper2_CreateCategory(pMapper, &CLSID_AudioCompressorCategory, MERIT_DO_NOT_USE, friendlyaudcomp);
195         IFilterMapper2_CreateCategory(pMapper, &CLSID_AudioRendererCategory, MERIT_NORMAL, friendlyaudrend);
196         IFilterMapper2_CreateCategory(pMapper, &CLSID_MidiRendererCategory, MERIT_NORMAL, friendlymidirend);
197         IFilterMapper2_CreateCategory(pMapper, &CLSID_TransmitCategory, MERIT_DO_NOT_USE, friendlyextrend);
198         IFilterMapper2_CreateCategory(pMapper, &CLSID_DeviceControlCategory, MERIT_DO_NOT_USE, friendlydevctrl);
199 
200         IFilterMapper2_Release(pMapper);
201     }
202 
203     CoUninitialize();
204 
205     return res;
206 }
207 
208 /***********************************************************************
209  *		DllUnregisterServer (DEVENUM.@)
210  */
211 HRESULT WINAPI DllUnregisterServer(void)
212 {
213     FIXME("stub!\n");
214     return __wine_unregister_resources( devenum_instance );
215 }
216 
217 #ifdef __REACTOS__
218 
219 typedef HRESULT (WINAPI *DllRegisterServer_func)(void);
220 
221 /* calls DllRegisterServer() for the Quartz DLL */
222 static void DEVENUM_RegisterQuartz(void)
223 {
224     HANDLE hDLL = LoadLibraryA("quartz.dll");
225     DllRegisterServer_func pDllRegisterServer = NULL;
226     if (hDLL)
227         pDllRegisterServer = (DllRegisterServer_func)GetProcAddress(hDLL, "DllRegisterServer");
228     if (pDllRegisterServer)
229     {
230         HRESULT hr = pDllRegisterServer();
231         if (FAILED(hr))
232             ERR("Failed to register Quartz. Error was 0x%x)\n", hr);
233     }
234 }
235 
236 #endif
237