[DMUSIC]
[reactos.git] / reactos / dll / directx / wine / 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 }
124
125 return TRUE;
126 }
127
128
129 /******************************************************************
130 * DllCanUnloadNow (DMUSIC.@)
131 *
132 *
133 */
134 HRESULT WINAPI DllCanUnloadNow(void)
135 {
136 return DMUSIC_refCount != 0 ? S_FALSE : S_OK;
137 }
138
139
140 /******************************************************************
141 * DllGetClassObject (DMUSIC.@)
142 *
143 *
144 */
145 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
146 {
147 TRACE("(%s, %s, %p)\n", debugstr_dmguid(rclsid), debugstr_dmguid(riid), ppv);
148 if (IsEqualCLSID (rclsid, &CLSID_DirectMusic) && IsEqualIID (riid, &IID_IClassFactory)) {
149 *ppv = &DirectMusic_CF;
150 IClassFactory_AddRef((IClassFactory*)*ppv);
151 return S_OK;
152 } else if (IsEqualCLSID (rclsid, &CLSID_DirectMusicCollection) && IsEqualIID (riid, &IID_IClassFactory)) {
153 *ppv = &Collection_CF;
154 IClassFactory_AddRef((IClassFactory*)*ppv);
155 return S_OK;
156 }
157
158 WARN("(%s, %s, %p): no interface found.\n", debugstr_dmguid(rclsid), debugstr_dmguid(riid), ppv);
159 return CLASS_E_CLASSNOTAVAILABLE;
160 }
161
162 /***********************************************************************
163 * DllRegisterServer (DMUSIC.@)
164 */
165 HRESULT WINAPI DllRegisterServer(void)
166 {
167 return __wine_register_resources( instance );
168 }
169
170 /***********************************************************************
171 * DllUnregisterServer (DMUSIC.@)
172 */
173 HRESULT WINAPI DllUnregisterServer(void)
174 {
175 return __wine_unregister_resources( instance );
176 }
177
178 /******************************************************************
179 * Helper functions
180 *
181 *
182 */
183 /* dwPatch from MIDILOCALE */
184 DWORD MIDILOCALE2Patch (const MIDILOCALE *pLocale) {
185 DWORD dwPatch = 0;
186 if (!pLocale) return 0;
187 dwPatch |= (pLocale->ulBank & F_INSTRUMENT_DRUMS); /* set drum bit */
188 dwPatch |= ((pLocale->ulBank & 0x00007F7F) << 8); /* set MIDI bank location */
189 dwPatch |= (pLocale->ulInstrument & 0x0000007F); /* set PC value */
190 return dwPatch;
191 }
192
193 /* MIDILOCALE from dwPatch */
194 void Patch2MIDILOCALE (DWORD dwPatch, LPMIDILOCALE pLocale) {
195 memset (pLocale, 0, sizeof(MIDILOCALE));
196
197 pLocale->ulInstrument = (dwPatch & 0x7F); /* get PC value */
198 pLocale->ulBank = ((dwPatch & 0x007F7F00) >> 8); /* get MIDI bank location */
199 pLocale->ulBank |= (dwPatch & F_INSTRUMENT_DRUMS); /* get drum bit */
200 }
201
202 /* check whether the given DWORD is even (return 0) or odd (return 1) */
203 int even_or_odd (DWORD number) {
204 return (number & 0x1); /* basically, check if bit 0 is set ;) */
205 }
206
207 /* FOURCC to string conversion for debug messages */
208 const char *debugstr_fourcc (DWORD fourcc) {
209 if (!fourcc) return "'null'";
210 return wine_dbg_sprintf ("\'%c%c%c%c\'",
211 (char)(fourcc), (char)(fourcc >> 8),
212 (char)(fourcc >> 16), (char)(fourcc >> 24));
213 }
214
215 /* DMUS_VERSION struct to string conversion for debug messages */
216 static const char *debugstr_dmversion (const DMUS_VERSION *version) {
217 if (!version) return "'null'";
218 return wine_dbg_sprintf ("\'%i,%i,%i,%i\'",
219 (int)((version->dwVersionMS & 0xFFFF0000) >> 8), (int)(version->dwVersionMS & 0x0000FFFF),
220 (int)((version->dwVersionLS & 0xFFFF0000) >> 8), (int)(version->dwVersionLS & 0x0000FFFF));
221 }
222
223 /* returns name of given GUID */
224 const char *debugstr_dmguid (const GUID *id) {
225 static const guid_info guids[] = {
226 /* CLSIDs */
227 GE(CLSID_AudioVBScript),
228 GE(CLSID_DirectMusic),
229 GE(CLSID_DirectMusicAudioPath),
230 GE(CLSID_DirectMusicAudioPathConfig),
231 GE(CLSID_DirectMusicAuditionTrack),
232 GE(CLSID_DirectMusicBand),
233 GE(CLSID_DirectMusicBandTrack),
234 GE(CLSID_DirectMusicChordMapTrack),
235 GE(CLSID_DirectMusicChordMap),
236 GE(CLSID_DirectMusicChordTrack),
237 GE(CLSID_DirectMusicCollection),
238 GE(CLSID_DirectMusicCommandTrack),
239 GE(CLSID_DirectMusicComposer),
240 GE(CLSID_DirectMusicContainer),
241 GE(CLSID_DirectMusicGraph),
242 GE(CLSID_DirectMusicLoader),
243 GE(CLSID_DirectMusicLyricsTrack),
244 GE(CLSID_DirectMusicMarkerTrack),
245 GE(CLSID_DirectMusicMelodyFormulationTrack),
246 GE(CLSID_DirectMusicMotifTrack),
247 GE(CLSID_DirectMusicMuteTrack),
248 GE(CLSID_DirectMusicParamControlTrack),
249 GE(CLSID_DirectMusicPatternTrack),
250 GE(CLSID_DirectMusicPerformance),
251 GE(CLSID_DirectMusicScript),
252 GE(CLSID_DirectMusicScriptAutoImpSegment),
253 GE(CLSID_DirectMusicScriptAutoImpPerformance),
254 GE(CLSID_DirectMusicScriptAutoImpSegmentState),
255 GE(CLSID_DirectMusicScriptAutoImpAudioPathConfig),
256 GE(CLSID_DirectMusicScriptAutoImpAudioPath),
257 GE(CLSID_DirectMusicScriptAutoImpSong),
258 GE(CLSID_DirectMusicScriptSourceCodeLoader),
259 GE(CLSID_DirectMusicScriptTrack),
260 GE(CLSID_DirectMusicSection),
261 GE(CLSID_DirectMusicSegment),
262 GE(CLSID_DirectMusicSegmentState),
263 GE(CLSID_DirectMusicSegmentTriggerTrack),
264 GE(CLSID_DirectMusicSegTriggerTrack),
265 GE(CLSID_DirectMusicSeqTrack),
266 GE(CLSID_DirectMusicSignPostTrack),
267 GE(CLSID_DirectMusicSong),
268 GE(CLSID_DirectMusicStyle),
269 GE(CLSID_DirectMusicStyleTrack),
270 GE(CLSID_DirectMusicSynth),
271 GE(CLSID_DirectMusicSynthSink),
272 GE(CLSID_DirectMusicSysExTrack),
273 GE(CLSID_DirectMusicTemplate),
274 GE(CLSID_DirectMusicTempoTrack),
275 GE(CLSID_DirectMusicTimeSigTrack),
276 GE(CLSID_DirectMusicWaveTrack),
277 GE(CLSID_DirectSoundWave),
278 /* IIDs */
279 GE(IID_IDirectMusic),
280 GE(IID_IDirectMusic2),
281 GE(IID_IDirectMusic8),
282 GE(IID_IDirectMusicAudioPath),
283 GE(IID_IDirectMusicBand),
284 GE(IID_IDirectMusicBuffer),
285 GE(IID_IDirectMusicChordMap),
286 GE(IID_IDirectMusicCollection),
287 GE(IID_IDirectMusicComposer),
288 GE(IID_IDirectMusicContainer),
289 GE(IID_IDirectMusicDownload),
290 GE(IID_IDirectMusicDownloadedInstrument),
291 GE(IID_IDirectMusicGetLoader),
292 GE(IID_IDirectMusicGraph),
293 GE(IID_IDirectMusicInstrument),
294 GE(IID_IDirectMusicLoader),
295 GE(IID_IDirectMusicLoader8),
296 GE(IID_IDirectMusicObject),
297 GE(IID_IDirectMusicPatternTrack),
298 GE(IID_IDirectMusicPerformance),
299 GE(IID_IDirectMusicPerformance2),
300 GE(IID_IDirectMusicPerformance8),
301 GE(IID_IDirectMusicPort),
302 GE(IID_IDirectMusicPortDownload),
303 GE(IID_IDirectMusicScript),
304 GE(IID_IDirectMusicSegment),
305 GE(IID_IDirectMusicSegment2),
306 GE(IID_IDirectMusicSegment8),
307 GE(IID_IDirectMusicSegmentState),
308 GE(IID_IDirectMusicSegmentState8),
309 GE(IID_IDirectMusicStyle),
310 GE(IID_IDirectMusicStyle8),
311 GE(IID_IDirectMusicSynth),
312 GE(IID_IDirectMusicSynth8),
313 GE(IID_IDirectMusicSynthSink),
314 GE(IID_IDirectMusicThru),
315 GE(IID_IDirectMusicTool),
316 GE(IID_IDirectMusicTool8),
317 GE(IID_IDirectMusicTrack),
318 GE(IID_IDirectMusicTrack8),
319 GE(IID_IUnknown),
320 GE(IID_IPersistStream),
321 GE(IID_IStream),
322 GE(IID_IClassFactory),
323 /* GUIDs */
324 GE(GUID_DirectMusicAllTypes),
325 GE(GUID_NOTIFICATION_CHORD),
326 GE(GUID_NOTIFICATION_COMMAND),
327 GE(GUID_NOTIFICATION_MEASUREANDBEAT),
328 GE(GUID_NOTIFICATION_PERFORMANCE),
329 GE(GUID_NOTIFICATION_RECOMPOSE),
330 GE(GUID_NOTIFICATION_SEGMENT),
331 GE(GUID_BandParam),
332 GE(GUID_ChordParam),
333 GE(GUID_CommandParam),
334 GE(GUID_CommandParam2),
335 GE(GUID_CommandParamNext),
336 GE(GUID_IDirectMusicBand),
337 GE(GUID_IDirectMusicChordMap),
338 GE(GUID_IDirectMusicStyle),
339 GE(GUID_MuteParam),
340 GE(GUID_Play_Marker),
341 GE(GUID_RhythmParam),
342 GE(GUID_TempoParam),
343 GE(GUID_TimeSignature),
344 GE(GUID_Valid_Start_Time),
345 GE(GUID_Clear_All_Bands),
346 GE(GUID_ConnectToDLSCollection),
347 GE(GUID_Disable_Auto_Download),
348 GE(GUID_DisableTempo),
349 GE(GUID_DisableTimeSig),
350 GE(GUID_Download),
351 GE(GUID_DownloadToAudioPath),
352 GE(GUID_Enable_Auto_Download),
353 GE(GUID_EnableTempo),
354 GE(GUID_EnableTimeSig),
355 GE(GUID_IgnoreBankSelectForGM),
356 GE(GUID_SeedVariations),
357 GE(GUID_StandardMIDIFile),
358 GE(GUID_Unload),
359 GE(GUID_UnloadFromAudioPath),
360 GE(GUID_Variations),
361 GE(GUID_PerfMasterTempo),
362 GE(GUID_PerfMasterVolume),
363 GE(GUID_PerfMasterGrooveLevel),
364 GE(GUID_PerfAutoDownload),
365 GE(GUID_DefaultGMCollection),
366 GE(GUID_Synth_Default),
367 GE(GUID_Buffer_Reverb),
368 GE(GUID_Buffer_EnvReverb),
369 GE(GUID_Buffer_Stereo),
370 GE(GUID_Buffer_3D_Dry),
371 GE(GUID_Buffer_Mono),
372 GE(GUID_DMUS_PROP_GM_Hardware),
373 GE(GUID_DMUS_PROP_GS_Capable),
374 GE(GUID_DMUS_PROP_GS_Hardware),
375 GE(GUID_DMUS_PROP_DLS1),
376 GE(GUID_DMUS_PROP_DLS2),
377 GE(GUID_DMUS_PROP_Effects),
378 GE(GUID_DMUS_PROP_INSTRUMENT2),
379 GE(GUID_DMUS_PROP_LegacyCaps),
380 GE(GUID_DMUS_PROP_MemorySize),
381 GE(GUID_DMUS_PROP_SampleMemorySize),
382 GE(GUID_DMUS_PROP_SamplePlaybackRate),
383 GE(GUID_DMUS_PROP_SetSynthSink),
384 GE(GUID_DMUS_PROP_SinkUsesDSound),
385 GE(GUID_DMUS_PROP_SynthSink_DSOUND),
386 GE(GUID_DMUS_PROP_SynthSink_WAVE),
387 GE(GUID_DMUS_PROP_Volume),
388 GE(GUID_DMUS_PROP_WavesReverb),
389 GE(GUID_DMUS_PROP_WriteLatency),
390 GE(GUID_DMUS_PROP_WritePeriod),
391 GE(GUID_DMUS_PROP_XG_Capable),
392 GE(GUID_DMUS_PROP_XG_Hardware)
393 };
394
395 unsigned int i;
396
397 if (!id) return "(null)";
398
399 for (i = 0; i < sizeof(guids)/sizeof(guids[0]); i++) {
400 if (IsEqualGUID(id, guids[i].guid))
401 return guids[i].name;
402 }
403 /* if we didn't find it, act like standard debugstr_guid */
404 return debugstr_guid(id);
405 }
406
407 /* generic flag-dumping function */
408 static const char* debugstr_flags (DWORD flags, const flag_info* names, size_t num_names){
409 char buffer[128] = "", *ptr = &buffer[0];
410 unsigned int i;
411 int size = sizeof(buffer);
412
413 for (i=0; i < num_names; i++)
414 {
415 if ((flags & names[i].val) || /* standard flag*/
416 ((!flags) && (!names[i].val))) { /* zero value only */
417 int cnt = snprintf(ptr, size, "%s ", names[i].name);
418 if (cnt < 0 || cnt >= size) break;
419 size -= cnt;
420 ptr += cnt;
421 }
422 }
423
424 return wine_dbg_sprintf("%s", buffer);
425 }
426
427 /* dump DMUS_OBJ flags */
428 static const char *debugstr_DMUS_OBJ_FLAGS (DWORD flagmask) {
429 static const flag_info flags[] = {
430 FE(DMUS_OBJ_OBJECT),
431 FE(DMUS_OBJ_CLASS),
432 FE(DMUS_OBJ_NAME),
433 FE(DMUS_OBJ_CATEGORY),
434 FE(DMUS_OBJ_FILENAME),
435 FE(DMUS_OBJ_FULLPATH),
436 FE(DMUS_OBJ_URL),
437 FE(DMUS_OBJ_VERSION),
438 FE(DMUS_OBJ_DATE),
439 FE(DMUS_OBJ_LOADED),
440 FE(DMUS_OBJ_MEMORY),
441 FE(DMUS_OBJ_STREAM)
442 };
443 return debugstr_flags (flagmask, flags, sizeof(flags)/sizeof(flags[0]));
444 }
445
446 /* Dump whole DMUS_OBJECTDESC struct */
447 void dump_DMUS_OBJECTDESC(LPDMUS_OBJECTDESC desc)
448 {
449 TRACE("DMUS_OBJECTDESC (%p):\n", desc);
450 TRACE(" - dwSize = %d\n", desc->dwSize);
451 TRACE(" - dwValidData = %s\n", debugstr_DMUS_OBJ_FLAGS (desc->dwValidData));
452 if (desc->dwValidData & DMUS_OBJ_CLASS) TRACE(" - guidClass = %s\n", debugstr_dmguid(&desc->guidClass));
453 if (desc->dwValidData & DMUS_OBJ_OBJECT) TRACE(" - guidObject = %s\n", debugstr_guid(&desc->guidObject));
454 if (desc->dwValidData & DMUS_OBJ_DATE) TRACE(" - ftDate = FIXME\n");
455 if (desc->dwValidData & DMUS_OBJ_VERSION) TRACE(" - vVersion = %s\n", debugstr_dmversion(&desc->vVersion));
456 if (desc->dwValidData & DMUS_OBJ_NAME) TRACE(" - wszName = %s\n", debugstr_w(desc->wszName));
457 if (desc->dwValidData & DMUS_OBJ_CATEGORY) TRACE(" - wszCategory = %s\n", debugstr_w(desc->wszCategory));
458 if (desc->dwValidData & DMUS_OBJ_FILENAME) TRACE(" - wszFileName = %s\n", debugstr_w(desc->wszFileName));
459 if (desc->dwValidData & DMUS_OBJ_MEMORY) TRACE(" - llMemLength = 0x%s\n - pbMemData = %p\n",
460 wine_dbgstr_longlong(desc->llMemLength), desc->pbMemData);
461 if (desc->dwValidData & DMUS_OBJ_STREAM) TRACE(" - pStream = %p\n", desc->pStream);
462 }
463
464 /* Dump DMUS_PORTPARAMS flags */
465 static const char* debugstr_DMUS_PORTPARAMS_FLAGS(DWORD flagmask)
466 {
467 static const flag_info flags[] = {
468 FE(DMUS_PORTPARAMS_VOICES),
469 FE(DMUS_PORTPARAMS_CHANNELGROUPS),
470 FE(DMUS_PORTPARAMS_AUDIOCHANNELS),
471 FE(DMUS_PORTPARAMS_SAMPLERATE),
472 FE(DMUS_PORTPARAMS_EFFECTS),
473 FE(DMUS_PORTPARAMS_SHARE)
474 };
475 return debugstr_flags(flagmask, flags, sizeof(flags)/sizeof(flags[0]));
476 }
477
478 /* Dump whole DMUS_PORTPARAMS struct */
479 void dump_DMUS_PORTPARAMS(LPDMUS_PORTPARAMS params)
480 {
481 TRACE("DMUS_PORTPARAMS (%p):\n", params);
482 TRACE(" - dwSize = %d\n", params->dwSize);
483 TRACE(" - dwValidParams = %s\n", debugstr_DMUS_PORTPARAMS_FLAGS(params->dwValidParams));
484 if (params->dwValidParams & DMUS_PORTPARAMS_VOICES) TRACE(" - dwVoices = %u\n", params->dwVoices);
485 if (params->dwValidParams & DMUS_PORTPARAMS_CHANNELGROUPS) TRACE(" - dwChannelGroup = %u\n", params->dwChannelGroups);
486 if (params->dwValidParams & DMUS_PORTPARAMS_AUDIOCHANNELS) TRACE(" - dwAudioChannels = %u\n", params->dwAudioChannels);
487 if (params->dwValidParams & DMUS_PORTPARAMS_SAMPLERATE) TRACE(" - dwSampleRate = %u\n", params->dwSampleRate);
488 if (params->dwValidParams & DMUS_PORTPARAMS_EFFECTS) TRACE(" - dwEffectFlags = %x\n", params->dwEffectFlags);
489 if (params->dwValidParams & DMUS_PORTPARAMS_SHARE) TRACE(" - fShare = %u\n", params->fShare);
490 }