ddraw: User memory surfaces should be in the system memory pool.
[wine/multimedia.git] / dlls / dmusic / dmusic_main.c
blob8708787e733ab7c17db42ff4467663482fee5d98
1 /* DirectMusic Main
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 "config.h"
21 #include "wine/port.h"
23 #include <stdio.h>
25 #include "dmusic_private.h"
26 #include "rpcproxy.h"
28 WINE_DEFAULT_DEBUG_CHANNEL(dmusic);
30 static HINSTANCE instance;
31 LONG DMUSIC_refCount = 0;
33 typedef struct {
34 IClassFactory IClassFactory_iface;
35 HRESULT WINAPI (*fnCreateInstance)(REFIID riid, void **ppv, IUnknown *pUnkOuter);
36 } IClassFactoryImpl;
38 /******************************************************************
39 * IClassFactory implementation
41 static inline IClassFactoryImpl *impl_from_IClassFactory(IClassFactory *iface)
43 return CONTAINING_RECORD(iface, IClassFactoryImpl, IClassFactory_iface);
46 static HRESULT WINAPI ClassFactory_QueryInterface(IClassFactory *iface, REFIID riid, void **ppv)
48 if (ppv == NULL)
49 return E_POINTER;
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;
61 *ppv = iface;
62 IUnknown_AddRef((IUnknown*)*ppv);
63 return S_OK;
66 static ULONG WINAPI ClassFactory_AddRef(IClassFactory *iface)
68 DMUSIC_LockModule();
70 return 2; /* non-heap based object */
73 static ULONG WINAPI ClassFactory_Release(IClassFactory *iface)
75 DMUSIC_UnlockModule();
77 return 1; /* non-heap based object */
80 static HRESULT WINAPI ClassFactory_CreateInstance(IClassFactory *iface, IUnknown *pUnkOuter,
81 REFIID riid, void **ppv)
83 IClassFactoryImpl *This = impl_from_IClassFactory(iface);
85 TRACE ("(%p, %s, %p)\n", pUnkOuter, debugstr_dmguid(riid), ppv);
87 return This->fnCreateInstance(riid, ppv, pUnkOuter);
90 static HRESULT WINAPI ClassFactory_LockServer(IClassFactory *iface, BOOL dolock)
92 TRACE("(%d)\n", dolock);
94 if (dolock)
95 DMUSIC_LockModule();
96 else
97 DMUSIC_UnlockModule();
99 return S_OK;
102 static const IClassFactoryVtbl classfactory_vtbl = {
103 ClassFactory_QueryInterface,
104 ClassFactory_AddRef,
105 ClassFactory_Release,
106 ClassFactory_CreateInstance,
107 ClassFactory_LockServer
110 static IClassFactoryImpl DirectMusic_CF = {{&classfactory_vtbl}, DMUSIC_CreateDirectMusicImpl};
111 static IClassFactoryImpl Collection_CF = {{&classfactory_vtbl},
112 DMUSIC_CreateDirectMusicCollectionImpl};
114 /******************************************************************
115 * DllMain
119 BOOL WINAPI DllMain(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID lpvReserved) {
120 if (fdwReason == DLL_PROCESS_ATTACH) {
121 instance = hinstDLL;
122 DisableThreadLibraryCalls(hinstDLL);
125 return TRUE;
129 /******************************************************************
130 * DllCanUnloadNow (DMUSIC.@)
134 HRESULT WINAPI DllCanUnloadNow(void)
136 return DMUSIC_refCount != 0 ? S_FALSE : S_OK;
140 /******************************************************************
141 * DllGetClassObject (DMUSIC.@)
145 HRESULT WINAPI DllGetClassObject(REFCLSID rclsid, REFIID riid, LPVOID *ppv)
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;
158 WARN("(%s, %s, %p): no interface found.\n", debugstr_dmguid(rclsid), debugstr_dmguid(riid), ppv);
159 return CLASS_E_CLASSNOTAVAILABLE;
162 /***********************************************************************
163 * DllRegisterServer (DMUSIC.@)
165 HRESULT WINAPI DllRegisterServer(void)
167 return __wine_register_resources( instance );
170 /***********************************************************************
171 * DllUnregisterServer (DMUSIC.@)
173 HRESULT WINAPI DllUnregisterServer(void)
175 return __wine_unregister_resources( instance );
178 /******************************************************************
179 * Helper functions
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;
193 /* MIDILOCALE from dwPatch */
194 void Patch2MIDILOCALE (DWORD dwPatch, LPMIDILOCALE pLocale) {
195 memset (pLocale, 0, sizeof(MIDILOCALE));
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 */
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 ;) */
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));
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));
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_DirectMusicAudioPathConfig),
230 GE(CLSID_DirectMusicAuditionTrack),
231 GE(CLSID_DirectMusicBand),
232 GE(CLSID_DirectMusicBandTrack),
233 GE(CLSID_DirectMusicChordMapTrack),
234 GE(CLSID_DirectMusicChordMap),
235 GE(CLSID_DirectMusicChordTrack),
236 GE(CLSID_DirectMusicCollection),
237 GE(CLSID_DirectMusicCommandTrack),
238 GE(CLSID_DirectMusicComposer),
239 GE(CLSID_DirectMusicContainer),
240 GE(CLSID_DirectMusicGraph),
241 GE(CLSID_DirectMusicLoader),
242 GE(CLSID_DirectMusicLyricsTrack),
243 GE(CLSID_DirectMusicMarkerTrack),
244 GE(CLSID_DirectMusicMelodyFormulationTrack),
245 GE(CLSID_DirectMusicMotifTrack),
246 GE(CLSID_DirectMusicMuteTrack),
247 GE(CLSID_DirectMusicParamControlTrack),
248 GE(CLSID_DirectMusicPatternTrack),
249 GE(CLSID_DirectMusicPerformance),
250 GE(CLSID_DirectMusicScript),
251 GE(CLSID_DirectMusicScriptAutoImpSegment),
252 GE(CLSID_DirectMusicScriptAutoImpPerformance),
253 GE(CLSID_DirectMusicScriptAutoImpSegmentState),
254 GE(CLSID_DirectMusicScriptAutoImpAudioPathConfig),
255 GE(CLSID_DirectMusicScriptAutoImpAudioPath),
256 GE(CLSID_DirectMusicScriptAutoImpSong),
257 GE(CLSID_DirectMusicScriptSourceCodeLoader),
258 GE(CLSID_DirectMusicScriptTrack),
259 GE(CLSID_DirectMusicSection),
260 GE(CLSID_DirectMusicSegment),
261 GE(CLSID_DirectMusicSegmentState),
262 GE(CLSID_DirectMusicSegmentTriggerTrack),
263 GE(CLSID_DirectMusicSegTriggerTrack),
264 GE(CLSID_DirectMusicSeqTrack),
265 GE(CLSID_DirectMusicSignPostTrack),
266 GE(CLSID_DirectMusicSong),
267 GE(CLSID_DirectMusicStyle),
268 GE(CLSID_DirectMusicStyleTrack),
269 GE(CLSID_DirectMusicSynth),
270 GE(CLSID_DirectMusicSynthSink),
271 GE(CLSID_DirectMusicSysExTrack),
272 GE(CLSID_DirectMusicTemplate),
273 GE(CLSID_DirectMusicTempoTrack),
274 GE(CLSID_DirectMusicTimeSigTrack),
275 GE(CLSID_DirectMusicWaveTrack),
276 GE(CLSID_DirectSoundWave),
277 /* IIDs */
278 GE(IID_IDirectMusic),
279 GE(IID_IDirectMusic2),
280 GE(IID_IDirectMusic8),
281 GE(IID_IDirectMusicAudioPath),
282 GE(IID_IDirectMusicBand),
283 GE(IID_IDirectMusicBuffer),
284 GE(IID_IDirectMusicChordMap),
285 GE(IID_IDirectMusicCollection),
286 GE(IID_IDirectMusicComposer),
287 GE(IID_IDirectMusicContainer),
288 GE(IID_IDirectMusicDownload),
289 GE(IID_IDirectMusicDownloadedInstrument),
290 GE(IID_IDirectMusicGetLoader),
291 GE(IID_IDirectMusicGraph),
292 GE(IID_IDirectMusicInstrument),
293 GE(IID_IDirectMusicLoader),
294 GE(IID_IDirectMusicLoader8),
295 GE(IID_IDirectMusicObject),
296 GE(IID_IDirectMusicPatternTrack),
297 GE(IID_IDirectMusicPerformance),
298 GE(IID_IDirectMusicPerformance2),
299 GE(IID_IDirectMusicPerformance8),
300 GE(IID_IDirectMusicPort),
301 GE(IID_IDirectMusicPortDownload),
302 GE(IID_IDirectMusicScript),
303 GE(IID_IDirectMusicSegment),
304 GE(IID_IDirectMusicSegment2),
305 GE(IID_IDirectMusicSegment8),
306 GE(IID_IDirectMusicSegmentState),
307 GE(IID_IDirectMusicSegmentState8),
308 GE(IID_IDirectMusicStyle),
309 GE(IID_IDirectMusicStyle8),
310 GE(IID_IDirectMusicSynth),
311 GE(IID_IDirectMusicSynth8),
312 GE(IID_IDirectMusicSynthSink),
313 GE(IID_IDirectMusicThru),
314 GE(IID_IDirectMusicTool),
315 GE(IID_IDirectMusicTool8),
316 GE(IID_IDirectMusicTrack),
317 GE(IID_IDirectMusicTrack8),
318 GE(IID_IUnknown),
319 GE(IID_IPersistStream),
320 GE(IID_IStream),
321 GE(IID_IClassFactory),
322 /* GUIDs */
323 GE(GUID_DirectMusicAllTypes),
324 GE(GUID_NOTIFICATION_CHORD),
325 GE(GUID_NOTIFICATION_COMMAND),
326 GE(GUID_NOTIFICATION_MEASUREANDBEAT),
327 GE(GUID_NOTIFICATION_PERFORMANCE),
328 GE(GUID_NOTIFICATION_RECOMPOSE),
329 GE(GUID_NOTIFICATION_SEGMENT),
330 GE(GUID_BandParam),
331 GE(GUID_ChordParam),
332 GE(GUID_CommandParam),
333 GE(GUID_CommandParam2),
334 GE(GUID_CommandParamNext),
335 GE(GUID_IDirectMusicBand),
336 GE(GUID_IDirectMusicChordMap),
337 GE(GUID_IDirectMusicStyle),
338 GE(GUID_MuteParam),
339 GE(GUID_Play_Marker),
340 GE(GUID_RhythmParam),
341 GE(GUID_TempoParam),
342 GE(GUID_TimeSignature),
343 GE(GUID_Valid_Start_Time),
344 GE(GUID_Clear_All_Bands),
345 GE(GUID_ConnectToDLSCollection),
346 GE(GUID_Disable_Auto_Download),
347 GE(GUID_DisableTempo),
348 GE(GUID_DisableTimeSig),
349 GE(GUID_Download),
350 GE(GUID_DownloadToAudioPath),
351 GE(GUID_Enable_Auto_Download),
352 GE(GUID_EnableTempo),
353 GE(GUID_EnableTimeSig),
354 GE(GUID_IgnoreBankSelectForGM),
355 GE(GUID_SeedVariations),
356 GE(GUID_StandardMIDIFile),
357 GE(GUID_Unload),
358 GE(GUID_UnloadFromAudioPath),
359 GE(GUID_Variations),
360 GE(GUID_PerfMasterTempo),
361 GE(GUID_PerfMasterVolume),
362 GE(GUID_PerfMasterGrooveLevel),
363 GE(GUID_PerfAutoDownload),
364 GE(GUID_DefaultGMCollection),
365 GE(GUID_Synth_Default),
366 GE(GUID_Buffer_Reverb),
367 GE(GUID_Buffer_EnvReverb),
368 GE(GUID_Buffer_Stereo),
369 GE(GUID_Buffer_3D_Dry),
370 GE(GUID_Buffer_Mono),
371 GE(GUID_DMUS_PROP_GM_Hardware),
372 GE(GUID_DMUS_PROP_GS_Capable),
373 GE(GUID_DMUS_PROP_GS_Hardware),
374 GE(GUID_DMUS_PROP_DLS1),
375 GE(GUID_DMUS_PROP_DLS2),
376 GE(GUID_DMUS_PROP_Effects),
377 GE(GUID_DMUS_PROP_INSTRUMENT2),
378 GE(GUID_DMUS_PROP_LegacyCaps),
379 GE(GUID_DMUS_PROP_MemorySize),
380 GE(GUID_DMUS_PROP_SampleMemorySize),
381 GE(GUID_DMUS_PROP_SamplePlaybackRate),
382 GE(GUID_DMUS_PROP_SetSynthSink),
383 GE(GUID_DMUS_PROP_SinkUsesDSound),
384 GE(GUID_DMUS_PROP_SynthSink_DSOUND),
385 GE(GUID_DMUS_PROP_SynthSink_WAVE),
386 GE(GUID_DMUS_PROP_Volume),
387 GE(GUID_DMUS_PROP_WavesReverb),
388 GE(GUID_DMUS_PROP_WriteLatency),
389 GE(GUID_DMUS_PROP_WritePeriod),
390 GE(GUID_DMUS_PROP_XG_Capable),
391 GE(GUID_DMUS_PROP_XG_Hardware)
394 unsigned int i;
396 if (!id) return "(null)";
398 for (i = 0; i < sizeof(guids)/sizeof(guids[0]); i++) {
399 if (IsEqualGUID(id, guids[i].guid))
400 return guids[i].name;
402 /* if we didn't find it, act like standard debugstr_guid */
403 return debugstr_guid(id);
406 /* generic flag-dumping function */
407 static const char* debugstr_flags (DWORD flags, const flag_info* names, size_t num_names){
408 char buffer[128] = "", *ptr = &buffer[0];
409 unsigned int i;
410 int size = sizeof(buffer);
412 for (i=0; i < num_names; i++)
414 if ((flags & names[i].val) || /* standard flag*/
415 ((!flags) && (!names[i].val))) { /* zero value only */
416 int cnt = snprintf(ptr, size, "%s ", names[i].name);
417 if (cnt < 0 || cnt >= size) break;
418 size -= cnt;
419 ptr += cnt;
423 return wine_dbg_sprintf("%s", buffer);
426 /* dump DMUS_OBJ flags */
427 static const char *debugstr_DMUS_OBJ_FLAGS (DWORD flagmask) {
428 static const flag_info flags[] = {
429 FE(DMUS_OBJ_OBJECT),
430 FE(DMUS_OBJ_CLASS),
431 FE(DMUS_OBJ_NAME),
432 FE(DMUS_OBJ_CATEGORY),
433 FE(DMUS_OBJ_FILENAME),
434 FE(DMUS_OBJ_FULLPATH),
435 FE(DMUS_OBJ_URL),
436 FE(DMUS_OBJ_VERSION),
437 FE(DMUS_OBJ_DATE),
438 FE(DMUS_OBJ_LOADED),
439 FE(DMUS_OBJ_MEMORY),
440 FE(DMUS_OBJ_STREAM)
442 return debugstr_flags (flagmask, flags, sizeof(flags)/sizeof(flags[0]));
445 /* Dump whole DMUS_OBJECTDESC struct */
446 void dump_DMUS_OBJECTDESC(LPDMUS_OBJECTDESC desc)
448 TRACE("DMUS_OBJECTDESC (%p):\n", desc);
449 TRACE(" - dwSize = %d\n", desc->dwSize);
450 TRACE(" - dwValidData = %s\n", debugstr_DMUS_OBJ_FLAGS (desc->dwValidData));
451 if (desc->dwValidData & DMUS_OBJ_CLASS) TRACE(" - guidClass = %s\n", debugstr_dmguid(&desc->guidClass));
452 if (desc->dwValidData & DMUS_OBJ_OBJECT) TRACE(" - guidObject = %s\n", debugstr_guid(&desc->guidObject));
453 if (desc->dwValidData & DMUS_OBJ_DATE) TRACE(" - ftDate = FIXME\n");
454 if (desc->dwValidData & DMUS_OBJ_VERSION) TRACE(" - vVersion = %s\n", debugstr_dmversion(&desc->vVersion));
455 if (desc->dwValidData & DMUS_OBJ_NAME) TRACE(" - wszName = %s\n", debugstr_w(desc->wszName));
456 if (desc->dwValidData & DMUS_OBJ_CATEGORY) TRACE(" - wszCategory = %s\n", debugstr_w(desc->wszCategory));
457 if (desc->dwValidData & DMUS_OBJ_FILENAME) TRACE(" - wszFileName = %s\n", debugstr_w(desc->wszFileName));
458 if (desc->dwValidData & DMUS_OBJ_MEMORY) TRACE(" - llMemLength = 0x%s\n - pbMemData = %p\n",
459 wine_dbgstr_longlong(desc->llMemLength), desc->pbMemData);
460 if (desc->dwValidData & DMUS_OBJ_STREAM) TRACE(" - pStream = %p\n", desc->pStream);
463 /* Dump DMUS_PORTPARAMS flags */
464 static const char* debugstr_DMUS_PORTPARAMS_FLAGS(DWORD flagmask)
466 static const flag_info flags[] = {
467 FE(DMUS_PORTPARAMS_VOICES),
468 FE(DMUS_PORTPARAMS_CHANNELGROUPS),
469 FE(DMUS_PORTPARAMS_AUDIOCHANNELS),
470 FE(DMUS_PORTPARAMS_SAMPLERATE),
471 FE(DMUS_PORTPARAMS_EFFECTS),
472 FE(DMUS_PORTPARAMS_SHARE)
474 return debugstr_flags(flagmask, flags, sizeof(flags)/sizeof(flags[0]));
477 /* Dump whole DMUS_PORTPARAMS struct */
478 void dump_DMUS_PORTPARAMS(LPDMUS_PORTPARAMS params)
480 TRACE("DMUS_PORTPARAMS (%p):\n", params);
481 TRACE(" - dwSize = %d\n", params->dwSize);
482 TRACE(" - dwValidParams = %s\n", debugstr_DMUS_PORTPARAMS_FLAGS(params->dwValidParams));
483 if (params->dwValidParams & DMUS_PORTPARAMS_VOICES) TRACE(" - dwVoices = %u\n", params->dwVoices);
484 if (params->dwValidParams & DMUS_PORTPARAMS_CHANNELGROUPS) TRACE(" - dwChannelGroup = %u\n", params->dwChannelGroups);
485 if (params->dwValidParams & DMUS_PORTPARAMS_AUDIOCHANNELS) TRACE(" - dwAudioChannels = %u\n", params->dwAudioChannels);
486 if (params->dwValidParams & DMUS_PORTPARAMS_SAMPLERATE) TRACE(" - dwSampleRate = %u\n", params->dwSampleRate);
487 if (params->dwValidParams & DMUS_PORTPARAMS_EFFECTS) TRACE(" - dwEffectFlags = %x\n", params->dwEffectFlags);
488 if (params->dwValidParams & DMUS_PORTPARAMS_SHARE) TRACE(" - fShare = %u\n", params->fShare);