msxml3: Avoid BAD_CAST in schemasInit.
[wine] / dlls / dmusic / dmusic_main.c
1 /* DirectMusic Main
2  *
3  * Copyright (C) 2003-2004 Rok Mandeljc
4  *
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.
9  *
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.
14  *
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
18  */
19
20 #include "config.h"
21 #include "wine/port.h"
22
23 #include <stdio.h>
24
25 #include "dmusic_private.h"
26 #include "rpcproxy.h"
27
28 WINE_DEFAULT_DEBUG_CHANNEL(dmusic);
29
30 static HINSTANCE instance;
31 LONG DMUSIC_refCount = 0;
32
33 typedef struct {
34         IClassFactory IClassFactory_iface;
35         HRESULT WINAPI (*fnCreateInstance)(REFIID riid, void **ppv, IUnknown *pUnkOuter);
36 } IClassFactoryImpl;
37
38 /******************************************************************
39  *      IClassFactory implementation
40  */
41 static inline IClassFactoryImpl *impl_from_IClassFactory(IClassFactory *iface)
42 {
43         return CONTAINING_RECORD(iface, IClassFactoryImpl, IClassFactory_iface);
44 }
45
46 static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID riid, void **ppv)
47 {
48         if (ppv == NULL)
49                 return E_POINTER;
50
51         if (IsEqualGUID(&IID_IUnknown, riid))
52                 TRACE("(%p)->(IID_IUnknown %p)\n", iface, ppv);
53         else if (IsEqualGUID(&IID_IClassFactory, riid))
54                 TRACE("(%p)->(IID_IClassFactory %p)\n", iface, ppv);
55         else {
56                 FIXME("(%p)->(%s %p)\n", iface, debugstr_guid(riid), ppv);
57                 *ppv = NULL;
58                 return E_NOINTERFACE;
59         }
60
61         *ppv = iface;
62         IUnknown_AddRef((IUnknown*)*ppv);
63         return S_OK;
64 }
65
66 static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
67 {
68         DMUSIC_LockModule();
69
70         return 2; /* non-heap based object */
71 }
72
73 static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
74 {
75         DMUSIC_UnlockModule();
76
77         return 1; /* non-heap based object */
78 }
79
80 static HRESULT WINAPI ClassFactory_CreateInstance(IClassFactory *iface, IUnknown *pUnkOuter,
81         REFIID riid, void **ppv)
82 {
83         IClassFactoryImpl *This = impl_from_IClassFactory(iface);
84
85         TRACE ("(%p, %s, %p)\n", pUnkOuter, debugstr_dmguid(riid), ppv);
86
87         return This->fnCreateInstance(riid, ppv, pUnkOuter);
88 }
89
90 static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL dolock)
91 {
92         TRACE("(%d)\n", dolock);
93
94         if (dolock)
95                 DMUSIC_LockModule();
96         else
97                 DMUSIC_UnlockModule();
98
99         return S_OK;
100 }
101
102 static const IClassFactoryVtbl classfactory_vtbl = {
103         ClassFactory_QueryInterface,
104         ClassFactory_AddRef,
105         ClassFactory_Release,
106         ClassFactory_CreateInstance,
107         ClassFactory_LockServer
108 };
109
110 static IClassFactoryImpl DirectMusic_CF = {{&classfactory_vtbl}, DMUSIC_CreateDirectMusicImpl};
111 static IClassFactoryImpl Collection_CF = {{&classfactory_vtbl},
112                                           DMUSIC_CreateDirectMusicCollectionImpl};
113
114 /******************************************************************
115  *              DllMain
116  *
117  *
118  */
119 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {
120         if (fdwReason == DLL_PROCESS_ATTACH) {
121             instance = hinstDLL;
122             DisableThreadLibraryCalls(hinstDLL);
123                 /* FIXME: Initialisation */
124         } else if (fdwReason == DLL_PROCESS_DETACH) {
125                 /* FIXME: Cleanup */
126         }
127
128         return TRUE;
129 }
130
131
132 /******************************************************************
133  *              DllCanUnloadNow (DMUSIC.@)
134  *
135  *
136  */
137 HRESULT WINAPI DllCanUnloadNow(void)
138 {
139         return DMUSIC_refCount != 0 ? S_FALSE : S_OK;
140 }
141
142
143 /******************************************************************
144  *              DllGetClassObject (DMUSIC.@)
145  *
146  *
147  */
148 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
149 {
150         TRACE("(%s, %s, %p)\n", debugstr_dmguid(rclsid), debugstr_dmguid(riid), ppv);
151         if (IsEqualCLSID (rclsid, &CLSID_DirectMusic) && IsEqualIID (riid, &IID_IClassFactory)) {
152                 *ppv = &DirectMusic_CF;
153                 IClassFactory_AddRef((IClassFactory*)*ppv);
154                 return S_OK;
155         } else if (IsEqualCLSID (rclsid, &CLSID_DirectMusicCollection) && IsEqualIID (riid, &IID_IClassFactory)) {
156                 *ppv = &Collection_CF;
157                 IClassFactory_AddRef((IClassFactory*)*ppv);
158                 return S_OK;
159         }
160         
161     WARN("(%s, %s, %p): no interface found.\n", debugstr_dmguid(rclsid), debugstr_dmguid(riid), ppv);
162     return CLASS_E_CLASSNOTAVAILABLE;
163 }
164
165 /***********************************************************************
166  *              DllRegisterServer (DMUSIC.@)
167  */
168 HRESULT WINAPI DllRegisterServer(void)
169 {
170     return __wine_register_resources( instance );
171 }
172
173 /***********************************************************************
174  *              DllUnregisterServer (DMUSIC.@)
175  */
176 HRESULT WINAPI DllUnregisterServer(void)
177 {
178     return __wine_unregister_resources( instance );
179 }
180
181 /******************************************************************
182  *              Helper functions
183  *
184  *
185  */
186 /* dwPatch from MIDILOCALE */
187 DWORD MIDILOCALE2Patch (const MIDILOCALE *pLocale) {
188         DWORD dwPatch = 0;
189         if (!pLocale) return 0;
190         dwPatch |= (pLocale->ulBank & F_INSTRUMENT_DRUMS); /* set drum bit */
191         dwPatch |= ((pLocale->ulBank & 0x00007F7F) << 8); /* set MIDI bank location */
192         dwPatch |= (pLocale->ulInstrument & 0x0000007F); /* set PC value */
193         return dwPatch; 
194 }
195
196 /* MIDILOCALE from dwPatch */
197 void Patch2MIDILOCALE (DWORD dwPatch, LPMIDILOCALE pLocale) {
198         memset (pLocale, 0, sizeof(MIDILOCALE));
199         
200         pLocale->ulInstrument = (dwPatch & 0x7F); /* get PC value */
201         pLocale->ulBank = ((dwPatch & 0x007F7F00) >> 8); /* get MIDI bank location */
202         pLocale->ulBank |= (dwPatch & F_INSTRUMENT_DRUMS); /* get drum bit */
203 }
204
205 /* check whether the given DWORD is even (return 0) or odd (return 1) */
206 int even_or_odd (DWORD number) {
207         return (number & 0x1); /* basically, check if bit 0 is set ;) */
208 }
209
210 /* FOURCC to string conversion for debug messages */
211 const char *debugstr_fourcc (DWORD fourcc) {
212     if (!fourcc) return "'null'";
213     return wine_dbg_sprintf ("\'%c%c%c%c\'",
214                 (char)(fourcc), (char)(fourcc >> 8),
215         (char)(fourcc >> 16), (char)(fourcc >> 24));
216 }
217
218 /* DMUS_VERSION struct to string conversion for debug messages */
219 static const char *debugstr_dmversion (const DMUS_VERSION *version) {
220         if (!version) return "'null'";
221         return wine_dbg_sprintf ("\'%i,%i,%i,%i\'",
222                 (int)((version->dwVersionMS & 0xFFFF0000) >> 8), (int)(version->dwVersionMS & 0x0000FFFF), 
223                 (int)((version->dwVersionLS & 0xFFFF0000) >> 8), (int)(version->dwVersionLS & 0x0000FFFF));
224 }
225
226 /* returns name of given GUID */
227 const char *debugstr_dmguid (const GUID *id) {
228         static const guid_info guids[] = {
229                 /* CLSIDs */
230                 GE(CLSID_AudioVBScript),
231                 GE(CLSID_DirectMusic),
232                 GE(CLSID_DirectMusicAudioPath),
233                 GE(CLSID_DirectMusicAudioPathConfig),
234                 GE(CLSID_DirectMusicAuditionTrack),
235                 GE(CLSID_DirectMusicBand),
236                 GE(CLSID_DirectMusicBandTrack),
237                 GE(CLSID_DirectMusicChordMapTrack),
238                 GE(CLSID_DirectMusicChordMap),
239                 GE(CLSID_DirectMusicChordTrack),
240                 GE(CLSID_DirectMusicCollection),
241                 GE(CLSID_DirectMusicCommandTrack),
242                 GE(CLSID_DirectMusicComposer),
243                 GE(CLSID_DirectMusicContainer),
244                 GE(CLSID_DirectMusicGraph),
245                 GE(CLSID_DirectMusicLoader),
246                 GE(CLSID_DirectMusicLyricsTrack),
247                 GE(CLSID_DirectMusicMarkerTrack),
248                 GE(CLSID_DirectMusicMelodyFormulationTrack),
249                 GE(CLSID_DirectMusicMotifTrack),
250                 GE(CLSID_DirectMusicMuteTrack),
251                 GE(CLSID_DirectMusicParamControlTrack),
252                 GE(CLSID_DirectMusicPatternTrack),
253                 GE(CLSID_DirectMusicPerformance),
254                 GE(CLSID_DirectMusicScript),
255                 GE(CLSID_DirectMusicScriptAutoImpSegment),
256                 GE(CLSID_DirectMusicScriptAutoImpPerformance),
257                 GE(CLSID_DirectMusicScriptAutoImpSegmentState),
258                 GE(CLSID_DirectMusicScriptAutoImpAudioPathConfig),
259                 GE(CLSID_DirectMusicScriptAutoImpAudioPath),
260                 GE(CLSID_DirectMusicScriptAutoImpSong),
261                 GE(CLSID_DirectMusicScriptSourceCodeLoader),
262                 GE(CLSID_DirectMusicScriptTrack),
263                 GE(CLSID_DirectMusicSection),
264                 GE(CLSID_DirectMusicSegment),
265                 GE(CLSID_DirectMusicSegmentState),
266                 GE(CLSID_DirectMusicSegmentTriggerTrack),
267                 GE(CLSID_DirectMusicSegTriggerTrack),
268                 GE(CLSID_DirectMusicSeqTrack),
269                 GE(CLSID_DirectMusicSignPostTrack),
270                 GE(CLSID_DirectMusicSong),
271                 GE(CLSID_DirectMusicStyle),
272                 GE(CLSID_DirectMusicStyleTrack),
273                 GE(CLSID_DirectMusicSynth),
274                 GE(CLSID_DirectMusicSynthSink),
275                 GE(CLSID_DirectMusicSysExTrack),
276                 GE(CLSID_DirectMusicTemplate),
277                 GE(CLSID_DirectMusicTempoTrack),
278                 GE(CLSID_DirectMusicTimeSigTrack),
279                 GE(CLSID_DirectMusicWaveTrack),
280                 GE(CLSID_DirectSoundWave),
281                 /* IIDs */
282                 GE(IID_IDirectMusic),
283                 GE(IID_IDirectMusic2),
284                 GE(IID_IDirectMusic8),
285                 GE(IID_IDirectMusicAudioPath),
286                 GE(IID_IDirectMusicBand),
287                 GE(IID_IDirectMusicBuffer),
288                 GE(IID_IDirectMusicChordMap),
289                 GE(IID_IDirectMusicCollection),
290                 GE(IID_IDirectMusicComposer),
291                 GE(IID_IDirectMusicContainer),
292                 GE(IID_IDirectMusicDownload),
293                 GE(IID_IDirectMusicDownloadedInstrument),
294                 GE(IID_IDirectMusicGetLoader),
295                 GE(IID_IDirectMusicGraph),
296                 GE(IID_IDirectMusicInstrument),
297                 GE(IID_IDirectMusicLoader),
298                 GE(IID_IDirectMusicLoader8),
299                 GE(IID_IDirectMusicObject),
300                 GE(IID_IDirectMusicPatternTrack),
301                 GE(IID_IDirectMusicPerformance),
302                 GE(IID_IDirectMusicPerformance2),
303                 GE(IID_IDirectMusicPerformance8),
304                 GE(IID_IDirectMusicPort),
305                 GE(IID_IDirectMusicPortDownload),
306                 GE(IID_IDirectMusicScript),
307                 GE(IID_IDirectMusicSegment),
308                 GE(IID_IDirectMusicSegment2),
309                 GE(IID_IDirectMusicSegment8),
310                 GE(IID_IDirectMusicSegmentState),
311                 GE(IID_IDirectMusicSegmentState8),
312                 GE(IID_IDirectMusicStyle),
313                 GE(IID_IDirectMusicStyle8),
314                 GE(IID_IDirectMusicSynth),
315                 GE(IID_IDirectMusicSynth8),
316                 GE(IID_IDirectMusicSynthSink),
317                 GE(IID_IDirectMusicThru),
318                 GE(IID_IDirectMusicTool),
319                 GE(IID_IDirectMusicTool8),
320                 GE(IID_IDirectMusicTrack),
321                 GE(IID_IDirectMusicTrack8),
322                 GE(IID_IUnknown),
323                 GE(IID_IPersistStream),
324                 GE(IID_IStream),
325                 GE(IID_IClassFactory),
326                 /* GUIDs */
327                 GE(GUID_DirectMusicAllTypes),
328                 GE(GUID_NOTIFICATION_CHORD),
329                 GE(GUID_NOTIFICATION_COMMAND),
330                 GE(GUID_NOTIFICATION_MEASUREANDBEAT),
331                 GE(GUID_NOTIFICATION_PERFORMANCE),
332                 GE(GUID_NOTIFICATION_RECOMPOSE),
333                 GE(GUID_NOTIFICATION_SEGMENT),
334                 GE(GUID_BandParam),
335                 GE(GUID_ChordParam),
336                 GE(GUID_CommandParam),
337                 GE(GUID_CommandParam2),
338                 GE(GUID_CommandParamNext),
339                 GE(GUID_IDirectMusicBand),
340                 GE(GUID_IDirectMusicChordMap),
341                 GE(GUID_IDirectMusicStyle),
342                 GE(GUID_MuteParam),
343                 GE(GUID_Play_Marker),
344                 GE(GUID_RhythmParam),
345                 GE(GUID_TempoParam),
346                 GE(GUID_TimeSignature),
347                 GE(GUID_Valid_Start_Time),
348                 GE(GUID_Clear_All_Bands),
349                 GE(GUID_ConnectToDLSCollection),
350                 GE(GUID_Disable_Auto_Download),
351                 GE(GUID_DisableTempo),
352                 GE(GUID_DisableTimeSig),
353                 GE(GUID_Download),
354                 GE(GUID_DownloadToAudioPath),
355                 GE(GUID_Enable_Auto_Download),
356                 GE(GUID_EnableTempo),
357                 GE(GUID_EnableTimeSig),
358                 GE(GUID_IgnoreBankSelectForGM),
359                 GE(GUID_SeedVariations),
360                 GE(GUID_StandardMIDIFile),
361                 GE(GUID_Unload),
362                 GE(GUID_UnloadFromAudioPath),
363                 GE(GUID_Variations),
364                 GE(GUID_PerfMasterTempo),
365                 GE(GUID_PerfMasterVolume),
366                 GE(GUID_PerfMasterGrooveLevel),
367                 GE(GUID_PerfAutoDownload),
368                 GE(GUID_DefaultGMCollection),
369                 GE(GUID_Synth_Default),
370                 GE(GUID_Buffer_Reverb),
371                 GE(GUID_Buffer_EnvReverb),
372                 GE(GUID_Buffer_Stereo),
373                 GE(GUID_Buffer_3D_Dry),
374                 GE(GUID_Buffer_Mono),
375                 GE(GUID_DMUS_PROP_GM_Hardware),
376                 GE(GUID_DMUS_PROP_GS_Capable),
377                 GE(GUID_DMUS_PROP_GS_Hardware),
378                 GE(GUID_DMUS_PROP_DLS1),
379                 GE(GUID_DMUS_PROP_DLS2),
380                 GE(GUID_DMUS_PROP_Effects),
381                 GE(GUID_DMUS_PROP_INSTRUMENT2),
382                 GE(GUID_DMUS_PROP_LegacyCaps),
383                 GE(GUID_DMUS_PROP_MemorySize),
384                 GE(GUID_DMUS_PROP_SampleMemorySize),
385                 GE(GUID_DMUS_PROP_SamplePlaybackRate),
386                 GE(GUID_DMUS_PROP_SetSynthSink),
387                 GE(GUID_DMUS_PROP_SinkUsesDSound),
388                 GE(GUID_DMUS_PROP_SynthSink_DSOUND),
389                 GE(GUID_DMUS_PROP_SynthSink_WAVE),
390                 GE(GUID_DMUS_PROP_Volume),
391                 GE(GUID_DMUS_PROP_WavesReverb),
392                 GE(GUID_DMUS_PROP_WriteLatency),
393                 GE(GUID_DMUS_PROP_WritePeriod),
394                 GE(GUID_DMUS_PROP_XG_Capable),
395                 GE(GUID_DMUS_PROP_XG_Hardware)
396         };
397
398         unsigned int i;
399
400         if (!id) return "(null)";
401
402         for (i = 0; i < sizeof(guids)/sizeof(guids[0]); i++) {
403                 if (IsEqualGUID(id, guids[i].guid))
404                         return guids[i].name;
405         }
406         /* if we didn't find it, act like standard debugstr_guid */     
407         return debugstr_guid(id);
408 }       
409
410 /* generic flag-dumping function */
411 static const char* debugstr_flags (DWORD flags, const flag_info* names, size_t num_names){
412         char buffer[128] = "", *ptr = &buffer[0];
413         unsigned int i;
414         int size = sizeof(buffer);
415         
416         for (i=0; i < num_names; i++)
417         {
418                 if ((flags & names[i].val) ||   /* standard flag*/
419                         ((!flags) && (!names[i].val))) { /* zero value only */
420                                 int cnt = snprintf(ptr, size, "%s ", names[i].name);
421                                 if (cnt < 0 || cnt >= size) break;
422                                 size -= cnt;
423                                 ptr += cnt;
424                 }
425         }
426         
427         return wine_dbg_sprintf("%s", buffer);
428 }
429
430 /* dump DMUS_OBJ flags */
431 static const char *debugstr_DMUS_OBJ_FLAGS (DWORD flagmask) {
432     static const flag_info flags[] = {
433             FE(DMUS_OBJ_OBJECT),
434             FE(DMUS_OBJ_CLASS),
435             FE(DMUS_OBJ_NAME),
436             FE(DMUS_OBJ_CATEGORY),
437             FE(DMUS_OBJ_FILENAME),
438             FE(DMUS_OBJ_FULLPATH),
439             FE(DMUS_OBJ_URL),
440             FE(DMUS_OBJ_VERSION),
441             FE(DMUS_OBJ_DATE),
442             FE(DMUS_OBJ_LOADED),
443             FE(DMUS_OBJ_MEMORY),
444             FE(DMUS_OBJ_STREAM)
445         };
446     return debugstr_flags (flagmask, flags, sizeof(flags)/sizeof(flags[0]));
447 }
448
449 /* dump whole DMUS_OBJECTDESC struct */
450 const char *debugstr_DMUS_OBJECTDESC (LPDMUS_OBJECTDESC pDesc) {
451         if (pDesc) {
452                 char buffer[1024] = "", *ptr = &buffer[0];
453                 
454                 ptr += sprintf(ptr, "DMUS_OBJECTDESC (%p):\n", pDesc);
455                 ptr += sprintf(ptr, " - dwSize = %d\n", pDesc->dwSize);
456                 ptr += sprintf(ptr, " - dwValidData = %s\n", debugstr_DMUS_OBJ_FLAGS (pDesc->dwValidData));
457                 if (pDesc->dwValidData & DMUS_OBJ_CLASS) ptr += sprintf(ptr, " - guidClass = %s\n", debugstr_dmguid(&pDesc->guidClass));
458                 if (pDesc->dwValidData & DMUS_OBJ_OBJECT) ptr += sprintf(ptr, " - guidObject = %s\n", debugstr_guid(&pDesc->guidObject));
459                 if (pDesc->dwValidData & DMUS_OBJ_DATE) ptr += sprintf(ptr, " - ftDate = FIXME\n");
460                 if (pDesc->dwValidData & DMUS_OBJ_VERSION) ptr += sprintf(ptr, " - vVersion = %s\n", debugstr_dmversion(&pDesc->vVersion));
461                 if (pDesc->dwValidData & DMUS_OBJ_NAME) ptr += sprintf(ptr, " - wszName = %s\n", debugstr_w(pDesc->wszName));
462                 if (pDesc->dwValidData & DMUS_OBJ_CATEGORY) ptr += sprintf(ptr, " - wszCategory = %s\n", debugstr_w(pDesc->wszCategory));
463                 if (pDesc->dwValidData & DMUS_OBJ_FILENAME) ptr += sprintf(ptr, " - wszFileName = %s\n", debugstr_w(pDesc->wszFileName));
464                 if (pDesc->dwValidData & DMUS_OBJ_MEMORY) ptr += sprintf(ptr, " - llMemLength = 0x%s\n  - pbMemData = %p\n",
465                                                                      wine_dbgstr_longlong(pDesc->llMemLength), pDesc->pbMemData);
466                 if (pDesc->dwValidData & DMUS_OBJ_STREAM) ptr += sprintf(ptr, " - pStream = %p", pDesc->pStream);
467
468                 return wine_dbg_sprintf("%s", buffer);
469         } else {
470                 return wine_dbg_sprintf("(NULL)");
471         }
472 }