ntdll: Store memory views in a binary tree instead of a list.
[wine.git] / dlls / dmcompos / chordmap.c
blobe3259e6824fd13abe2b79189b8725e42063575a1
1 /* IDirectMusicChordMap 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 "dmcompos_private.h"
21 #include "dmobject.h"
23 WINE_DEFAULT_DEBUG_CHANNEL(dmcompos);
24 WINE_DECLARE_DEBUG_CHANNEL(dmfile);
26 /*****************************************************************************
27 * IDirectMusicChordMapImpl implementation
29 typedef struct IDirectMusicChordMapImpl {
30 IDirectMusicChordMap IDirectMusicChordMap_iface;
31 struct dmobject dmobj;
32 LONG ref;
33 } IDirectMusicChordMapImpl;
35 /* IDirectMusicChordMapImpl IDirectMusicChordMap part: */
36 static inline IDirectMusicChordMapImpl *impl_from_IDirectMusicChordMap(IDirectMusicChordMap *iface)
38 return CONTAINING_RECORD(iface, IDirectMusicChordMapImpl, IDirectMusicChordMap_iface);
41 static HRESULT WINAPI IDirectMusicChordMapImpl_QueryInterface(IDirectMusicChordMap *iface,
42 REFIID riid, void **ret_iface)
44 IDirectMusicChordMapImpl *This = impl_from_IDirectMusicChordMap(iface);
46 TRACE("(%p, %s, %p)\n", This, debugstr_dmguid(riid), ret_iface);
48 *ret_iface = NULL;
50 if (IsEqualIID(riid, &IID_IUnknown) || IsEqualIID(riid, &IID_IDirectMusicChordMap))
51 *ret_iface = iface;
52 else if (IsEqualIID(riid, &IID_IDirectMusicObject))
53 *ret_iface = &This->dmobj.IDirectMusicObject_iface;
54 else if (IsEqualIID(riid, &IID_IPersistStream))
55 *ret_iface = &This->dmobj.IPersistStream_iface;
56 else {
57 WARN("(%p, %s, %p): not found\n", This, debugstr_dmguid(riid), ret_iface);
58 return E_NOINTERFACE;
61 IUnknown_AddRef((IUnknown*)*ret_iface);
62 return S_OK;
65 static ULONG WINAPI IDirectMusicChordMapImpl_AddRef(IDirectMusicChordMap *iface)
67 IDirectMusicChordMapImpl *This = impl_from_IDirectMusicChordMap(iface);
68 LONG ref = InterlockedIncrement(&This->ref);
70 TRACE("(%p) ref=%d\n", This, ref);
72 return ref;
75 static ULONG WINAPI IDirectMusicChordMapImpl_Release(IDirectMusicChordMap *iface)
77 IDirectMusicChordMapImpl *This = impl_from_IDirectMusicChordMap(iface);
78 LONG ref = InterlockedDecrement(&This->ref);
80 TRACE("(%p) ref=%d\n", This, ref);
82 if (!ref) {
83 HeapFree(GetProcessHeap(), 0, This);
84 DMCOMPOS_UnlockModule();
87 return ref;
90 static HRESULT WINAPI IDirectMusicChordMapImpl_GetScale(IDirectMusicChordMap *iface,
91 DWORD *pdwScale)
93 IDirectMusicChordMapImpl *This = impl_from_IDirectMusicChordMap(iface);
94 FIXME("(%p, %p): stub\n", This, pdwScale);
95 return S_OK;
98 static const IDirectMusicChordMapVtbl dmchordmap_vtbl = {
99 IDirectMusicChordMapImpl_QueryInterface,
100 IDirectMusicChordMapImpl_AddRef,
101 IDirectMusicChordMapImpl_Release,
102 IDirectMusicChordMapImpl_GetScale
105 /* IDirectMusicChordMapImpl IDirectMusicObject part: */
106 static HRESULT WINAPI IDirectMusicObjectImpl_ParseDescriptor(IDirectMusicObject *iface,
107 IStream *pStream, DMUS_OBJECTDESC *pDesc)
109 DMUS_PRIVATE_CHUNK Chunk;
110 DWORD StreamSize, StreamCount, ListSize[1], ListCount[1];
111 LARGE_INTEGER liMove; /* used when skipping chunks */
113 TRACE("(%p, %p)\n", pStream, pDesc);
115 /* FIXME: should this be determined from stream? */
116 pDesc->dwValidData |= DMUS_OBJ_CLASS;
117 pDesc->guidClass = CLSID_DirectMusicChordMap;
119 IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
120 TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
121 switch (Chunk.fccID) {
122 case FOURCC_RIFF: {
123 IStream_Read (pStream, &Chunk.fccID, sizeof(FOURCC), NULL);
124 TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(Chunk.fccID));
125 StreamSize = Chunk.dwSize - sizeof(FOURCC);
126 StreamCount = 0;
127 if (Chunk.fccID == DMUS_FOURCC_CHORDMAP_FORM) {
128 TRACE_(dmfile)(": chord map form\n");
129 do {
130 IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
131 StreamCount += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
132 TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
133 switch (Chunk.fccID) {
134 case DMUS_FOURCC_GUID_CHUNK: {
135 TRACE_(dmfile)(": GUID chunk\n");
136 pDesc->dwValidData |= DMUS_OBJ_OBJECT;
137 IStream_Read (pStream, &pDesc->guidObject, Chunk.dwSize, NULL);
138 break;
140 case DMUS_FOURCC_VERSION_CHUNK: {
141 TRACE_(dmfile)(": version chunk\n");
142 pDesc->dwValidData |= DMUS_OBJ_VERSION;
143 IStream_Read (pStream, &pDesc->vVersion, Chunk.dwSize, NULL);
144 break;
146 case DMUS_FOURCC_CATEGORY_CHUNK: {
147 TRACE_(dmfile)(": category chunk\n");
148 pDesc->dwValidData |= DMUS_OBJ_CATEGORY;
149 IStream_Read (pStream, pDesc->wszCategory, Chunk.dwSize, NULL);
150 break;
152 case FOURCC_LIST: {
153 IStream_Read (pStream, &Chunk.fccID, sizeof(FOURCC), NULL);
154 TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(Chunk.fccID));
155 ListSize[0] = Chunk.dwSize - sizeof(FOURCC);
156 ListCount[0] = 0;
157 switch (Chunk.fccID) {
158 /* evil M$ UNFO list, which can (!?) contain INFO elements */
159 case DMUS_FOURCC_UNFO_LIST: {
160 TRACE_(dmfile)(": UNFO list\n");
161 do {
162 IStream_Read (pStream, &Chunk, sizeof(FOURCC)+sizeof(DWORD), NULL);
163 ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + Chunk.dwSize;
164 TRACE_(dmfile)(": %s chunk (size = 0x%04x)", debugstr_fourcc (Chunk.fccID), Chunk.dwSize);
165 switch (Chunk.fccID) {
166 /* don't ask me why, but M$ puts INFO elements in UNFO list sometimes
167 (though strings seem to be valid unicode) */
168 case mmioFOURCC('I','N','A','M'):
169 case DMUS_FOURCC_UNAM_CHUNK: {
170 TRACE_(dmfile)(": name chunk\n");
171 pDesc->dwValidData |= DMUS_OBJ_NAME;
172 IStream_Read (pStream, pDesc->wszName, Chunk.dwSize, NULL);
173 break;
175 case mmioFOURCC('I','A','R','T'):
176 case DMUS_FOURCC_UART_CHUNK: {
177 TRACE_(dmfile)(": artist chunk (ignored)\n");
178 liMove.QuadPart = Chunk.dwSize;
179 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
180 break;
182 case mmioFOURCC('I','C','O','P'):
183 case DMUS_FOURCC_UCOP_CHUNK: {
184 TRACE_(dmfile)(": copyright chunk (ignored)\n");
185 liMove.QuadPart = Chunk.dwSize;
186 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
187 break;
189 case mmioFOURCC('I','S','B','J'):
190 case DMUS_FOURCC_USBJ_CHUNK: {
191 TRACE_(dmfile)(": subject chunk (ignored)\n");
192 liMove.QuadPart = Chunk.dwSize;
193 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
194 break;
196 case mmioFOURCC('I','C','M','T'):
197 case DMUS_FOURCC_UCMT_CHUNK: {
198 TRACE_(dmfile)(": comment chunk (ignored)\n");
199 liMove.QuadPart = Chunk.dwSize;
200 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
201 break;
203 default: {
204 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
205 liMove.QuadPart = Chunk.dwSize;
206 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
207 break;
210 TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
211 } while (ListCount[0] < ListSize[0]);
212 break;
214 default: {
215 TRACE_(dmfile)(": unknown (skipping)\n");
216 liMove.QuadPart = Chunk.dwSize - sizeof(FOURCC);
217 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
218 break;
221 break;
223 default: {
224 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
225 liMove.QuadPart = Chunk.dwSize;
226 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL);
227 break;
230 TRACE_(dmfile)(": StreamCount[0] = %d < StreamSize[0] = %d\n", StreamCount, StreamSize);
231 } while (StreamCount < StreamSize);
232 } else {
233 TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
234 liMove.QuadPart = StreamSize;
235 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
236 return E_FAIL;
239 TRACE_(dmfile)(": reading finished\n");
240 break;
242 default: {
243 TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
244 liMove.QuadPart = Chunk.dwSize;
245 IStream_Seek (pStream, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
246 return DMUS_E_INVALIDFILE;
250 TRACE(": returning descriptor:\n%s\n", debugstr_DMUS_OBJECTDESC (pDesc));
252 return S_OK;
255 static const IDirectMusicObjectVtbl dmobject_vtbl = {
256 dmobj_IDirectMusicObject_QueryInterface,
257 dmobj_IDirectMusicObject_AddRef,
258 dmobj_IDirectMusicObject_Release,
259 dmobj_IDirectMusicObject_GetDescriptor,
260 dmobj_IDirectMusicObject_SetDescriptor,
261 IDirectMusicObjectImpl_ParseDescriptor
264 /* IDirectMusicChordMapImpl IPersistStream part: */
265 static inline IDirectMusicChordMapImpl *impl_from_IPersistStream(IPersistStream *iface)
267 return CONTAINING_RECORD(iface, IDirectMusicChordMapImpl, dmobj.IPersistStream_iface);
270 static HRESULT WINAPI IPersistStreamImpl_Load(IPersistStream *iface, IStream *pStm)
272 IDirectMusicChordMapImpl *This = impl_from_IPersistStream(iface);
273 FOURCC chunkID;
274 DWORD chunkSize, StreamSize, StreamCount, ListSize[3], ListCount[3];
275 LARGE_INTEGER liMove; /* used when skipping chunks */
277 FIXME("(%p, %p): Loading not implemented yet\n", This, pStm);
278 IStream_Read (pStm, &chunkID, sizeof(FOURCC), NULL);
279 IStream_Read (pStm, &chunkSize, sizeof(DWORD), NULL);
280 TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (chunkID), chunkSize);
281 switch (chunkID) {
282 case FOURCC_RIFF: {
283 IStream_Read (pStm, &chunkID, sizeof(FOURCC), NULL);
284 TRACE_(dmfile)(": RIFF chunk of type %s", debugstr_fourcc(chunkID));
285 StreamSize = chunkSize - sizeof(FOURCC);
286 StreamCount = 0;
287 switch (chunkID) {
288 case DMUS_FOURCC_CHORDMAP_FORM: {
289 TRACE_(dmfile)(": chordmap form\n");
290 do {
291 IStream_Read (pStm, &chunkID, sizeof(FOURCC), NULL);
292 IStream_Read (pStm, &chunkSize, sizeof(FOURCC), NULL);
293 StreamCount += sizeof(FOURCC) + sizeof(DWORD) + chunkSize;
294 TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (chunkID), chunkSize);
295 switch (chunkID) {
296 case DMUS_FOURCC_GUID_CHUNK: {
297 TRACE_(dmfile)(": GUID chunk\n");
298 This->dmobj.desc.dwValidData |= DMUS_OBJ_OBJECT;
299 IStream_Read (pStm, &This->dmobj.desc.guidObject, chunkSize, NULL);
300 break;
302 case DMUS_FOURCC_VERSION_CHUNK: {
303 TRACE_(dmfile)(": version chunk\n");
304 This->dmobj.desc.dwValidData |= DMUS_OBJ_VERSION;
305 IStream_Read (pStm, &This->dmobj.desc.vVersion, chunkSize, NULL);
306 break;
308 case DMUS_FOURCC_CATEGORY_CHUNK: {
309 TRACE_(dmfile)(": category chunk\n");
310 This->dmobj.desc.dwValidData |= DMUS_OBJ_CATEGORY;
311 IStream_Read (pStm, This->dmobj.desc.wszCategory, chunkSize, NULL);
312 break;
314 case FOURCC_LIST: {
315 IStream_Read (pStm, &chunkID, sizeof(FOURCC), NULL);
316 TRACE_(dmfile)(": LIST chunk of type %s", debugstr_fourcc(chunkID));
317 ListSize[0] = chunkSize - sizeof(FOURCC);
318 ListCount[0] = 0;
319 switch (chunkID) {
320 case DMUS_FOURCC_UNFO_LIST: {
321 TRACE_(dmfile)(": UNFO list\n");
322 do {
323 IStream_Read (pStm, &chunkID, sizeof(FOURCC), NULL);
324 IStream_Read (pStm, &chunkSize, sizeof(FOURCC), NULL);
325 ListCount[0] += sizeof(FOURCC) + sizeof(DWORD) + chunkSize;
326 TRACE_(dmfile)(": %s chunk (size = %d)", debugstr_fourcc (chunkID), chunkSize);
327 switch (chunkID) {
328 /* don't ask me why, but M$ puts INFO elements in UNFO list sometimes
329 (though strings seem to be valid unicode) */
330 case mmioFOURCC('I','N','A','M'):
331 case DMUS_FOURCC_UNAM_CHUNK: {
332 TRACE_(dmfile)(": name chunk\n");
333 This->dmobj.desc.dwValidData |= DMUS_OBJ_NAME;
334 IStream_Read (pStm, This->dmobj.desc.wszName, chunkSize, NULL);
335 break;
337 case mmioFOURCC('I','A','R','T'):
338 case DMUS_FOURCC_UART_CHUNK: {
339 TRACE_(dmfile)(": artist chunk (ignored)\n");
340 liMove.QuadPart = chunkSize;
341 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
342 break;
344 case mmioFOURCC('I','C','O','P'):
345 case DMUS_FOURCC_UCOP_CHUNK: {
346 TRACE_(dmfile)(": copyright chunk (ignored)\n");
347 liMove.QuadPart = chunkSize;
348 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
349 break;
351 case mmioFOURCC('I','S','B','J'):
352 case DMUS_FOURCC_USBJ_CHUNK: {
353 TRACE_(dmfile)(": subject chunk (ignored)\n");
354 liMove.QuadPart = chunkSize;
355 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
356 break;
358 case mmioFOURCC('I','C','M','T'):
359 case DMUS_FOURCC_UCMT_CHUNK: {
360 TRACE_(dmfile)(": comment chunk (ignored)\n");
361 liMove.QuadPart = chunkSize;
362 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
363 break;
365 default: {
366 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
367 liMove.QuadPart = chunkSize;
368 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
369 break;
372 TRACE_(dmfile)(": ListCount[0] = %d < ListSize[0] = %d\n", ListCount[0], ListSize[0]);
373 } while (ListCount[0] < ListSize[0]);
374 break;
376 default: {
377 TRACE_(dmfile)(": unknown (skipping)\n");
378 liMove.QuadPart = chunkSize - sizeof(FOURCC);
379 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
380 break;
383 break;
385 default: {
386 TRACE_(dmfile)(": unknown chunk (irrelevant & skipping)\n");
387 liMove.QuadPart = chunkSize;
388 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL);
389 break;
392 TRACE_(dmfile)(": StreamCount[0] = %d < StreamSize[0] = %d\n", StreamCount, StreamSize);
393 } while (StreamCount < StreamSize);
394 break;
396 default: {
397 TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
398 liMove.QuadPart = StreamSize;
399 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
400 return E_FAIL;
403 TRACE_(dmfile)(": reading finished\n");
404 break;
406 default: {
407 TRACE_(dmfile)(": unexpected chunk; loading failed)\n");
408 liMove.QuadPart = chunkSize;
409 IStream_Seek (pStm, liMove, STREAM_SEEK_CUR, NULL); /* skip the rest of the chunk */
410 return E_FAIL;
414 return S_OK;
417 static const IPersistStreamVtbl persiststream_vtbl = {
418 dmobj_IPersistStream_QueryInterface,
419 dmobj_IPersistStream_AddRef,
420 dmobj_IPersistStream_Release,
421 dmobj_IPersistStream_GetClassID,
422 unimpl_IPersistStream_IsDirty,
423 IPersistStreamImpl_Load,
424 unimpl_IPersistStream_Save,
425 unimpl_IPersistStream_GetSizeMax
428 /* for ClassFactory */
429 HRESULT WINAPI create_dmchordmap(REFIID lpcGUID, void **ppobj)
431 IDirectMusicChordMapImpl* obj;
432 HRESULT hr;
434 obj = HeapAlloc(GetProcessHeap(), HEAP_ZERO_MEMORY, sizeof(IDirectMusicChordMapImpl));
435 if (NULL == obj) {
436 *ppobj = NULL;
437 return E_OUTOFMEMORY;
439 obj->IDirectMusicChordMap_iface.lpVtbl = &dmchordmap_vtbl;
440 obj->ref = 1;
441 dmobject_init(&obj->dmobj, &CLSID_DirectMusicChordMap,
442 (IUnknown *)&obj->IDirectMusicChordMap_iface);
443 obj->dmobj.IDirectMusicObject_iface.lpVtbl = &dmobject_vtbl;
444 obj->dmobj.IPersistStream_iface.lpVtbl = &persiststream_vtbl;
446 DMCOMPOS_LockModule();
447 hr = IDirectMusicChordMap_QueryInterface(&obj->IDirectMusicChordMap_iface, lpcGUID, ppobj);
448 IDirectMusicChordMap_Release(&obj->IDirectMusicChordMap_iface);
450 return hr;