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