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