include: New ddk/hidtypes.h include file.
[wine.git] / dlls / dswave / dswave.c
blob012763457045971026eb776c6aa68fefe4240a9d
1 /* IDirectMusicWave Implementation
3 * Copyright (C) 2003-2004 Rok Mandeljc
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU Lesser General Public
7 * License as published by the Free Software Foundation; either
8 * version 2.1 of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13 * Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public
16 * License along with this program; if not, write to the Free Software
17 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
20 #include "dswave_private.h"
21 #include "dmobject.h"
23 WINE_DEFAULT_DEBUG_CHANNEL(dswave);
24 WINE_DECLARE_DEBUG_CHANNEL(dmfile);
26 /* an interface that is, according to my tests, obtained by loader after loading object; is it acting
27 as some sort of bridge between object and loader? */
28 static const GUID IID_IDirectMusicWavePRIVATE = {0x69e934e4,0x97f1,0x4f1d,{0x88,0xe8,0xf2,0xac,0x88,0x67,0x13,0x27}};
30 /*****************************************************************************
31 * IDirectMusicWaveImpl implementation
33 typedef struct IDirectMusicWaveImpl {
34 IUnknown IUnknown_iface;
35 struct dmobject dmobj;
36 LONG ref;
37 } IDirectMusicWaveImpl;
39 /* IDirectMusicWaveImpl IUnknown part: */
40 static inline IDirectMusicWaveImpl *impl_from_IUnknown(IUnknown *iface)
42 return CONTAINING_RECORD(iface, IDirectMusicWaveImpl, IUnknown_iface);
45 static HRESULT WINAPI IUnknownImpl_QueryInterface(IUnknown *iface, REFIID riid, void **ret_iface)
47 IDirectMusicWaveImpl *This = impl_from_IUnknown(iface);
49 TRACE("(%p, %s, %p)\n", This, debugstr_dmguid(riid), ret_iface);
51 *ret_iface = NULL;
53 if (IsEqualIID(riid, &IID_IUnknown))
54 *ret_iface = iface;
55 else if (IsEqualIID(riid, &IID_IDirectMusicObject))
56 *ret_iface = &This->dmobj.IDirectMusicObject_iface;
57 else if (IsEqualIID(riid, &IID_IPersistStream))
58 *ret_iface = &This->dmobj.IPersistStream_iface;
59 else if (IsEqualIID(riid, &IID_IDirectMusicWavePRIVATE)) {
60 FIXME("(%p, %s, %p): Unsupported private interface\n", This, debugstr_guid(riid), ret_iface);
61 return E_NOINTERFACE;
62 } else {
63 WARN("(%p, %s, %p): not found\n", This, debugstr_dmguid(riid), ret_iface);
64 return E_NOINTERFACE;
67 IUnknown_AddRef((IUnknown*)*ret_iface);
68 return S_OK;
71 static ULONG WINAPI IUnknownImpl_AddRef(IUnknown *iface)
73 IDirectMusicWaveImpl *This = impl_from_IUnknown(iface);
74 LONG ref = InterlockedIncrement(&This->ref);
76 TRACE("(%p) ref=%d\n", This, ref);
78 return ref;
81 static ULONG WINAPI IUnknownImpl_Release(IUnknown *iface)
83 IDirectMusicWaveImpl *This = impl_from_IUnknown(iface);
84 LONG ref = InterlockedDecrement(&This->ref);
86 TRACE("(%p) ref=%d\n", This, ref);
88 if (!ref) {
89 HeapFree(GetProcessHeap(), 0, This);
90 DSWAVE_UnlockModule();
93 return ref;
96 static const IUnknownVtbl unknown_vtbl = {
97 IUnknownImpl_QueryInterface,
98 IUnknownImpl_AddRef,
99 IUnknownImpl_Release
102 /* IDirectMusicWaveImpl IDirectMusicObject part: */
103 static HRESULT WINAPI IDirectMusicObjectImpl_ParseDescriptor(IDirectMusicObject *iface,
104 IStream *pStream, DMUS_OBJECTDESC *pDesc)
106 DMUS_PRIVATE_CHUNK Chunk;
107 DWORD StreamSize, StreamCount, ListSize[1], ListCount[1];
108 LARGE_INTEGER liMove; /* used when skipping chunks */
110 TRACE("(%p, %p)\n", pStream, pDesc);
112 /* FIXME: should this be determined from stream? */
113 pDesc->dwValidData |= DMUS_OBJ_CLASS;
114 pDesc->guidClass = CLSID_DirectMusicSegment;
116 IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
117 TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
118 switch (Chunk.fccID) {
119 case FOURCC_RIFF: {
120 IStream_Read (pStream, &Chunk.fccID, sizeof(FOURCC), NULL);
121 TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(Chunk.fccID));
122 StreamSize = Chunk.dwSize - sizeof(FOURCC);
123 StreamCount = 0;
124 if (Chunk.fccID == mmioFOURCC('W','A','V','E')) {
125 TRACE_(dmfile)(": wave form\n");
126 do {
127 IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
128 StreamCount += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
129 TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
130 switch (Chunk.fccID) {
131 case DMUS_FOURCC_GUID_CHUNK: {
132 TRACE_(dmfile)(": GUID chunk\n");
133 pDesc->dwValidData |= DMUS_OBJ_OBJECT;
134 IStream_Read (pStream, &pDesc->guidObject, Chunk.dwSize, NULL);
135 break;
137 case DMUS_FOURCC_VERSION_CHUNK: {
138 TRACE_(dmfile)(": version chunk\n");
139 pDesc->dwValidData |= DMUS_OBJ_VERSION;
140 IStream_Read (pStream, &pDesc->vVersion, Chunk.dwSize, NULL);
141 break;
143 case DMUS_FOURCC_CATEGORY_CHUNK: {
144 TRACE_(dmfile)(": category chunk\n");
145 pDesc->dwValidData |= DMUS_OBJ_CATEGORY;
146 IStream_Read (pStream, pDesc->wszCategory, Chunk.dwSize, NULL);
147 break;
149 case FOURCC_LIST: {
150 IStream_Read (pStream, &Chunk.fccID, sizeof(FOURCC), NULL);
151 TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
152 ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
153 ListCount[0] = 0;
154 switch (Chunk.fccID) {
155 /* evil M$ UNFO list, which can (!?) contain INFO elements */
156 case DMUS_FOURCC_UNFO_LIST: {
157 TRACE_(dmfile)(": UNFO list\n");
158 do {
159 IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
160 ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
161 TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
162 switch (Chunk.fccID) {
163 /* don't ask me why, but M$ puts INFO elements in UNFO list sometimes
164 (though strings seem to be valid unicode) */
165 case mmioFOURCC('I','N','A','M'):
166 case DMUS_FOURCC_UNAM_CHUNK: {
167 TRACE_(dmfile)(": name chunk\n");
168 pDesc->dwValidData |= DMUS_OBJ_NAME;
169 IStream_Read (pStream, pDesc->wszName, Chunk.dwSize, NULL);
170 break;
172 case mmioFOURCC('I','A','R','T'):
173 case DMUS_FOURCC_UART_CHUNK: {
174 TRACE_(dmfile)(": artist chunk (ignored)\n");
175 liMove.QuadPart = Chunk.dwSize;
176 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
177 break;
179 case mmioFOURCC('I','C','O','P'):
180 case DMUS_FOURCC_UCOP_CHUNK: {
181 TRACE_(dmfile)(": copyright chunk (ignored)\n");
182 liMove.QuadPart = Chunk.dwSize;
183 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
184 break;
186 case mmioFOURCC('I','S','B','J'):
187 case DMUS_FOURCC_USBJ_CHUNK: {
188 TRACE_(dmfile)(": subject chunk (ignored)\n");
189 liMove.QuadPart = Chunk.dwSize;
190 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
191 break;
193 case mmioFOURCC('I','C','M','T'):
194 case DMUS_FOURCC_UCMT_CHUNK: {
195 TRACE_(dmfile)(": comment chunk (ignored)\n");
196 liMove.QuadPart = Chunk.dwSize;
197 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
198 break;
200 default: {
201 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
202 liMove.QuadPart = Chunk.dwSize;
203 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
204 break;
207 TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
208 } while (ListCount[0] < ListSize[0]);
209 break;
211 default: {
212 TRACE_(dmfile)(": unknown (skipping)\n");
213 liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
214 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
215 break;
218 break;
220 default: {
221 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
222 liMove.QuadPart = Chunk.dwSize;
223 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
224 break;
227 TRACE_(dmfile)(": StreamCount[0] = %d < StreamSize[0] = %d\n", StreamCount, StreamSize);
228 } while (StreamCount < StreamSize);
229 } else {
230 TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
231 liMove.QuadPart = StreamSize;
232 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
233 return E_FAIL;
236 TRACE_(dmfile)(": reading finished\n");
237 break;
239 default: {
240 TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
241 liMove.QuadPart = Chunk.dwSize;
242 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
243 return DMUS_E_INVALIDFILE;
247 TRACE(": returning descriptor: %s\n", debugstr_DMUS_OBJECTDESC (pDesc));
249 return S_OK;
252 static const IDirectMusicObjectVtbl dmobject_vtbl = {
253 dmobj_IDirectMusicObject_QueryInterface,
254 dmobj_IDirectMusicObject_AddRef,
255 dmobj_IDirectMusicObject_Release,
256 dmobj_IDirectMusicObject_GetDescriptor,
257 dmobj_IDirectMusicObject_SetDescriptor,
258 IDirectMusicObjectImpl_ParseDescriptor
261 /* IDirectMusicWaveImpl IPersistStream part: */
262 static inline IDirectMusicWaveImpl *impl_from_IPersistStream(IPersistStream *iface)
264 return CONTAINING_RECORD(iface, IDirectMusicWaveImpl, dmobj.IPersistStream_iface);
267 static HRESULT WINAPI IPersistStreamImpl_Load(IPersistStream *iface, IStream *pStm)
269 IDirectMusicWaveImpl *This = impl_from_IPersistStream(iface);
270 DMUS_PRIVATE_CHUNK Chunk;
271 DWORD StreamSize, StreamCount, ListSize[1], ListCount[1];
272 LARGE_INTEGER liMove; /* used when skipping chunks */
274 FIXME("(%p, %p): loading not implemented yet (only descriptor is loaded)\n", This, pStm);
276 /* FIXME: should this be determined from stream? */
277 This->dmobj.desc.dwValidData |= DMUS_OBJ_CLASS;
278 This->dmobj.desc.guidClass = CLSID_DirectMusicSegment;
280 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
281 TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
282 switch (Chunk.fccID) {
283 case FOURCC_RIFF: {
284 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
285 TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(Chunk.fccID));
286 StreamSize = Chunk.dwSize - sizeof(FOURCC);
287 StreamCount = 0;
288 if (Chunk.fccID == mmioFOURCC('W','A','V','E')) {
289 TRACE_(dmfile)(": wave form\n");
290 do {
291 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
292 StreamCount += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
293 TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
294 switch (Chunk.fccID) {
295 case DMUS_FOURCC_GUID_CHUNK: {
296 TRACE_(dmfile)(": GUID chunk\n");
297 This->dmobj.desc.dwValidData |= DMUS_OBJ_OBJECT;
298 IStream_Read (pStm, &This->dmobj.desc.guidObject, Chunk.dwSize, NULL);
299 break;
301 case DMUS_FOURCC_VERSION_CHUNK: {
302 TRACE_(dmfile)(": version chunk\n");
303 This->dmobj.desc.dwValidData |= DMUS_OBJ_VERSION;
304 IStream_Read (pStm, &This->dmobj.desc.vVersion, Chunk.dwSize, NULL);
305 break;
307 case DMUS_FOURCC_CATEGORY_CHUNK: {
308 TRACE_(dmfile)(": category chunk\n");
309 This->dmobj.desc.dwValidData |= DMUS_OBJ_CATEGORY;
310 IStream_Read (pStm, This->dmobj.desc.wszCategory, Chunk.dwSize, NULL);
311 break;
313 case FOURCC_LIST: {
314 IStream_Read (pStm, &Chunk.fccID, sizeof(FOURCC), NULL);
315 TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
316 ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
317 ListCount[0] = 0;
318 switch (Chunk.fccID) {
319 /* evil M$ UNFO list, which can (!?) contain INFO elements */
320 case DMUS_FOURCC_UNFO_LIST: {
321 TRACE_(dmfile)(": UNFO list\n");
322 do {
323 IStream_Read (pStm, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
324 ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
325 TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
326 switch (Chunk.fccID) {
327 /* don't ask me why, but M$ puts INFO elements in UNFO list sometimes
328 (though strings seem to be valid unicode) */
329 case mmioFOURCC('I','N','A','M'):
330 case DMUS_FOURCC_UNAM_CHUNK: {
331 TRACE_(dmfile)(": name chunk\n");
332 This->dmobj.desc.dwValidData |= DMUS_OBJ_NAME;
333 IStream_Read (pStm, This->dmobj.desc.wszName, Chunk.dwSize, NULL);
334 break;
336 case mmioFOURCC('I','A','R','T'):
337 case DMUS_FOURCC_UART_CHUNK: {
338 TRACE_(dmfile)(": artist chunk (ignored)\n");
339 liMove.QuadPart = Chunk.dwSize;
340 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
341 break;
343 case mmioFOURCC('I','C','O','P'):
344 case DMUS_FOURCC_UCOP_CHUNK: {
345 TRACE_(dmfile)(": copyright chunk (ignored)\n");
346 liMove.QuadPart = Chunk.dwSize;
347 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
348 break;
350 case mmioFOURCC('I','S','B','J'):
351 case DMUS_FOURCC_USBJ_CHUNK: {
352 TRACE_(dmfile)(": subject chunk (ignored)\n");
353 liMove.QuadPart = Chunk.dwSize;
354 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
355 break;
357 case mmioFOURCC('I','C','M','T'):
358 case DMUS_FOURCC_UCMT_CHUNK: {
359 TRACE_(dmfile)(": comment chunk (ignored)\n");
360 liMove.QuadPart = Chunk.dwSize;
361 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
362 break;
364 default: {
365 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
366 liMove.QuadPart = Chunk.dwSize;
367 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
368 break;
371 TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
372 } while (ListCount[0] < ListSize[0]);
373 break;
375 default: {
376 TRACE_(dmfile)(": unknown (skipping)\n");
377 liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
378 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
379 break;
382 break;
384 default: {
385 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
386 liMove.QuadPart = Chunk.dwSize;
387 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
388 break;
391 TRACE_(dmfile)(": StreamCount[0] = %d < StreamSize[0] = %d\n", StreamCount, StreamSize);
392 } while (StreamCount < StreamSize);
393 } else {
394 TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
395 liMove.QuadPart = StreamSize;
396 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
397 return E_FAIL;
400 TRACE_(dmfile)(": reading finished\n");
401 break;
403 default: {
404 TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
405 liMove.QuadPart = Chunk.dwSize;
406 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
407 return DMUS_E_INVALIDFILE;
411 return S_OK;
414 static const IPersistStreamVtbl persiststream_vtbl = {
415 dmobj_IPersistStream_QueryInterface,
416 dmobj_IPersistStream_AddRef,
417 dmobj_IPersistStream_Release,
418 dmobj_IPersistStream_GetClassID,
419 unimpl_IPersistStream_IsDirty,
420 IPersistStreamImpl_Load,
421 unimpl_IPersistStream_Save,
422 unimpl_IPersistStream_GetSizeMax
425 /* for ClassFactory */
426 HRESULT WINAPI create_dswave(REFIID lpcGUID, void **ppobj)
428 IDirectMusicWaveImpl *obj;
429 HRESULT hr;
431 obj = HeapAlloc(GetProcessHeap(), 0, sizeof(IDirectMusicWaveImpl));
432 if (!obj) {
433 *ppobj = NULL;
434 return E_OUTOFMEMORY;
436 obj->IUnknown_iface.lpVtbl = &unknown_vtbl;
437 obj->ref = 1;
438 dmobject_init(&obj->dmobj, &CLSID_DirectSoundWave, &obj->IUnknown_iface);
439 obj->dmobj.IDirectMusicObject_iface.lpVtbl = &dmobject_vtbl;
440 obj->dmobj.IPersistStream_iface.lpVtbl = &persiststream_vtbl;
442 DSWAVE_LockModule();
443 hr = IUnknown_QueryInterface(&obj->IUnknown_iface, lpcGUID, ppobj);
444 IUnknown_Release(&obj->IUnknown_iface);
445 return hr;