| 1 | /* $Id: initwinmm.cpp,v 1.6 2001-12-23 09:43:12 sandervl Exp $ | 
|---|
| 2 | * | 
|---|
| 3 | * WINMM DLL entry point | 
|---|
| 4 | * | 
|---|
| 5 | * Copyright 1998 Sander van Leeuwen (sandervl@xs4all.nl) | 
|---|
| 6 | * Copyright 1998 Peter Fitzsimmons | 
|---|
| 7 | * Copyright 2000 Chris Wohlgemuth | 
|---|
| 8 | * | 
|---|
| 9 | * | 
|---|
| 10 | * Project Odin Software License can be found in LICENSE.TXT | 
|---|
| 11 | * | 
|---|
| 12 | */ | 
|---|
| 13 |  | 
|---|
| 14 | /*-------------------------------------------------------------*/ | 
|---|
| 15 | /* INITERM.C -- Source for a custom dynamic link library       */ | 
|---|
| 16 | /*              initialization and termination (_DLL_InitTerm) */ | 
|---|
| 17 | /*              function.                                      */ | 
|---|
| 18 | /*                                                             */ | 
|---|
| 19 | /* When called to perform initialization, this sample function */ | 
|---|
| 20 | /* gets storage for an array of integers, and initializes its  */ | 
|---|
| 21 | /* elements with random integers.  At termination time, it     */ | 
|---|
| 22 | /* frees the array.  Substitute your own special processing.   */ | 
|---|
| 23 | /*-------------------------------------------------------------*/ | 
|---|
| 24 |  | 
|---|
| 25 |  | 
|---|
| 26 | /* Include files */ | 
|---|
| 27 | #define  INCL_DOSMODULEMGR | 
|---|
| 28 | #define  INCL_DOSPROCESS | 
|---|
| 29 | #define  INCL_DOSSEMAPHORES | 
|---|
| 30 | #define  INCL_DOSERRORS | 
|---|
| 31 | #define  INCL_OS2MM | 
|---|
| 32 | #include <os2wrap.h>   //Odin32 OS/2 api wrappers | 
|---|
| 33 | #include <os2mewrap.h> //Odin32 OS/2 MMPM/2 api wrappers | 
|---|
| 34 | #include <stdlib.h> | 
|---|
| 35 | #include <stdio.h> | 
|---|
| 36 | #include <string.h> | 
|---|
| 37 | #include <builtin.h> | 
|---|
| 38 | #include <misc.h>       /*PLF Wed  98-03-18 23:19:26*/ | 
|---|
| 39 | #include <odin.h> | 
|---|
| 40 | #include <win32type.h> | 
|---|
| 41 | #include <win32api.h> | 
|---|
| 42 | #include <winconst.h> | 
|---|
| 43 | #include <odinlx.h> | 
|---|
| 44 | #include <initdll.h> | 
|---|
| 45 | #include "auxiliary.h" | 
|---|
| 46 | #include "winmmtype.h" | 
|---|
| 47 | #include "waveoutbase.h" | 
|---|
| 48 | #include <win\options.h> | 
|---|
| 49 | #include "initwinmm.h" | 
|---|
| 50 | #include <custombuild.h> | 
|---|
| 51 |  | 
|---|
| 52 | #define DBG_LOCALLOG    DBG_initterm | 
|---|
| 53 | #include "dbglocal.h" | 
|---|
| 54 |  | 
|---|
| 55 | BOOL MULTIMEDIA_MciInit(void); | 
|---|
| 56 | BOOL MULTIMEDIA_CreateIData(HINSTANCE hinstDLL); | 
|---|
| 57 | void MULTIMEDIA_DeleteIData(void); | 
|---|
| 58 |  | 
|---|
| 59 | extern "C" { | 
|---|
| 60 | void IRTMidiShutdown();  // IRTMidi shutdown routine | 
|---|
| 61 |  | 
|---|
| 62 | //Win32 resource table (produced by wrc) | 
|---|
| 63 | extern DWORD winmm_PEResTab; | 
|---|
| 64 | } | 
|---|
| 65 | static HMODULE dllHandle = 0; | 
|---|
| 66 | static HMODULE MMPMLibraryHandle = 0; | 
|---|
| 67 |  | 
|---|
| 68 | BOOL fMMPMAvailable = FALSE; | 
|---|
| 69 |  | 
|---|
| 70 | DWORD (APIENTRY *pfnmciSendCommand)(WORD   wDeviceID, | 
|---|
| 71 | WORD   wMessage, | 
|---|
| 72 | DWORD  dwParam1, | 
|---|
| 73 | PVOID  dwParam2, | 
|---|
| 74 | WORD   wUserParm) = NULL; | 
|---|
| 75 | DWORD (APIENTRY *pfnmciGetErrorString)(DWORD   dwError, | 
|---|
| 76 | LPSTR   lpstrBuffer, | 
|---|
| 77 | WORD    wLength) = NULL; | 
|---|
| 78 |  | 
|---|
| 79 | //****************************************************************************** | 
|---|
| 80 | //****************************************************************************** | 
|---|
| 81 | void WIN32API DisableWaveAudio() | 
|---|
| 82 | { | 
|---|
| 83 | fMMPMAvailable = FALSE; | 
|---|
| 84 | pfnmciGetErrorString = NULL; | 
|---|
| 85 | pfnmciSendCommand = NULL; | 
|---|
| 86 | } | 
|---|
| 87 | //****************************************************************************** | 
|---|
| 88 | //****************************************************************************** | 
|---|
| 89 | BOOL WINAPI LibMainWinmm(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID fImpLoad) | 
|---|
| 90 | { | 
|---|
| 91 | static BOOL bInitDone = FALSE; | 
|---|
| 92 | char   szError[CCHMAXPATH]; | 
|---|
| 93 | HKEY   hKey; | 
|---|
| 94 |  | 
|---|
| 95 | switch (fdwReason) | 
|---|
| 96 | { | 
|---|
| 97 | case DLL_PROCESS_ATTACH: | 
|---|
| 98 | { | 
|---|
| 99 | if (!MULTIMEDIA_CreateIData(hinstDLL)) | 
|---|
| 100 | return FALSE; | 
|---|
| 101 |  | 
|---|
| 102 | if (!bInitDone) { /* to be done only once */ | 
|---|
| 103 | if (!MULTIMEDIA_MciInit() /*|| !MMDRV_Init() */ ) { | 
|---|
| 104 | MULTIMEDIA_DeleteIData(); | 
|---|
| 105 | return FALSE; | 
|---|
| 106 | } | 
|---|
| 107 | bInitDone = TRUE; | 
|---|
| 108 | } | 
|---|
| 109 | DWORD dwVolume; | 
|---|
| 110 |  | 
|---|
| 111 | dwVolume = PROFILE_GetOdinIniInt(WINMM_SECTION, DEFVOL_KEY, 100); | 
|---|
| 112 | dwVolume = (dwVolume*0xFFFF)/100; | 
|---|
| 113 | dwVolume = (dwVolume << 16) | dwVolume; | 
|---|
| 114 | WaveOut::setDefaultVolume(dwVolume); | 
|---|
| 115 |  | 
|---|
| 116 | // try to load the MDM library, not MMPM directly!!! | 
|---|
| 117 | if (DosLoadModule(szError, sizeof(szError), | 
|---|
| 118 | "MDM.DLL", &MMPMLibraryHandle) != NO_ERROR) | 
|---|
| 119 | { | 
|---|
| 120 | // this system has no MMPM :-( | 
|---|
| 121 | fMMPMAvailable = FALSE; | 
|---|
| 122 | } | 
|---|
| 123 | else | 
|---|
| 124 | { | 
|---|
| 125 | /* detect if MMPM is available */ | 
|---|
| 126 | if (DosQueryProcAddr(MMPMLibraryHandle, | 
|---|
| 127 | 1, /* ORD_MCISENDCOMMAND */ | 
|---|
| 128 | NULL, | 
|---|
| 129 | (PFN*)&pfnmciSendCommand) != NO_ERROR) | 
|---|
| 130 | { | 
|---|
| 131 | fMMPMAvailable = FALSE; | 
|---|
| 132 | } | 
|---|
| 133 | else | 
|---|
| 134 | { | 
|---|
| 135 | fMMPMAvailable = TRUE; | 
|---|
| 136 | } | 
|---|
| 137 |  | 
|---|
| 138 | /* see if we can get the address for the mciGetErrorString function */ | 
|---|
| 139 | if (fMMPMAvailable == TRUE) | 
|---|
| 140 | { | 
|---|
| 141 | if (DosQueryProcAddr(MMPMLibraryHandle, | 
|---|
| 142 | 3, /* ORD_MCIGETERRORSTRING */ | 
|---|
| 143 | NULL, | 
|---|
| 144 | (PFN*)&pfnmciGetErrorString) != NO_ERROR) | 
|---|
| 145 | pfnmciGetErrorString = NULL; | 
|---|
| 146 | } | 
|---|
| 147 | dprintf(("MMPM/2 is available; hmod %x", MMPMLibraryHandle)); | 
|---|
| 148 | dprintf(("mciSendCommand    %x", pfnmciSendCommand)); | 
|---|
| 149 | dprintf(("mciGetErrorString %x", pfnmciGetErrorString)); | 
|---|
| 150 | } | 
|---|
| 151 | if(fMMPMAvailable && RegOpenKeyA(HKEY_LOCAL_MACHINE, CUSTOM_BUILD_OPTIONS_KEY, &hKey) == 0) | 
|---|
| 152 | { | 
|---|
| 153 | DWORD dwSize, dwType; | 
|---|
| 154 | DWORD dwFlag; | 
|---|
| 155 |  | 
|---|
| 156 | dwSize = sizeof(dwFlag); | 
|---|
| 157 | LONG rc = RegQueryValueExA(hKey, DISABLE_AUDIO_KEY, | 
|---|
| 158 | NULL, &dwType, | 
|---|
| 159 | (LPBYTE)&dwFlag, | 
|---|
| 160 | &dwSize); | 
|---|
| 161 |  | 
|---|
| 162 | if(rc == 0 && dwType == REG_DWORD) { | 
|---|
| 163 | if(dwFlag) { | 
|---|
| 164 | fMMPMAvailable = FALSE; | 
|---|
| 165 | pfnmciGetErrorString = NULL; | 
|---|
| 166 | pfnmciSendCommand = NULL; | 
|---|
| 167 | } | 
|---|
| 168 | } | 
|---|
| 169 | RegCloseKey(hKey); | 
|---|
| 170 | } | 
|---|
| 171 | return TRUE; | 
|---|
| 172 | } | 
|---|
| 173 |  | 
|---|
| 174 | case DLL_THREAD_ATTACH: | 
|---|
| 175 | case DLL_THREAD_DETACH: | 
|---|
| 176 | return TRUE; | 
|---|
| 177 |  | 
|---|
| 178 | case DLL_PROCESS_DETACH: | 
|---|
| 179 | MULTIMEDIA_DeleteIData(); | 
|---|
| 180 | auxOS2Close(); /* SvL: Close aux device if necessary */ | 
|---|
| 181 | IRTMidiShutdown;  /* JT: Shutdown RT Midi subsystem, if running. */ | 
|---|
| 182 |  | 
|---|
| 183 | if(MMPMLibraryHandle) DosFreeModule(MMPMLibraryHandle); | 
|---|
| 184 | return TRUE; | 
|---|
| 185 | } | 
|---|
| 186 | return FALSE; | 
|---|
| 187 | } | 
|---|
| 188 | /****************************************************************************/ | 
|---|
| 189 | /* _DLL_InitTerm is the function that gets called by the operating system   */ | 
|---|
| 190 | /* loader when it loads and frees this DLL for each process that accesses   */ | 
|---|
| 191 | /* this DLL.  However, it only gets called the first time the DLL is loaded */ | 
|---|
| 192 | /* and the last time it is freed for a particular process.  The system      */ | 
|---|
| 193 | /* linkage convention MUST be used because the operating system loader is   */ | 
|---|
| 194 | /* calling this function.                                                   */ | 
|---|
| 195 | /****************************************************************************/ | 
|---|
| 196 | ULONG APIENTRY inittermWinmm(ULONG hModule, ULONG ulFlag) | 
|---|
| 197 | { | 
|---|
| 198 | /*-------------------------------------------------------------------------*/ | 
|---|
| 199 | /* If ulFlag is zero then the DLL is being loaded so initialization should */ | 
|---|
| 200 | /* be performed.  If ulFlag is 1 then the DLL is being freed so            */ | 
|---|
| 201 | /* termination should be performed.                                        */ | 
|---|
| 202 | /*-------------------------------------------------------------------------*/ | 
|---|
| 203 |  | 
|---|
| 204 | switch (ulFlag) | 
|---|
| 205 | { | 
|---|
| 206 | case 0 : | 
|---|
| 207 | ParseLogStatusWINMM(); | 
|---|
| 208 |  | 
|---|
| 209 | dllHandle = RegisterLxDll(hModule, LibMainWinmm, (PVOID)&winmm_PEResTab); | 
|---|
| 210 | if(dllHandle == 0) | 
|---|
| 211 | return 0UL;/* Error */ | 
|---|
| 212 |  | 
|---|
| 213 | dprintf(("winmm init %s %s (%x)", __DATE__, __TIME__, inittermWinmm)); | 
|---|
| 214 | break; | 
|---|
| 215 | case 1 : | 
|---|
| 216 | auxOS2Close(); /* SvL: Close aux device if necessary */ | 
|---|
| 217 | if(dllHandle) { | 
|---|
| 218 | UnregisterLxDll(dllHandle); | 
|---|
| 219 | } | 
|---|
| 220 | break; | 
|---|
| 221 | default  : | 
|---|
| 222 | return 0UL; | 
|---|
| 223 | } | 
|---|
| 224 |  | 
|---|
| 225 | /***********************************************************/ | 
|---|
| 226 | /* A non-zero value must be returned to indicate success.  */ | 
|---|
| 227 | /***********************************************************/ | 
|---|
| 228 | return 1UL; | 
|---|
| 229 | } | 
|---|
| 230 | //****************************************************************************** | 
|---|
| 231 | //****************************************************************************** | 
|---|
| 232 | DWORD APIENTRY mymciSendCommand(WORD   wDeviceID, | 
|---|
| 233 | WORD   wMessage, | 
|---|
| 234 | DWORD  dwParam1, | 
|---|
| 235 | PVOID  dwParam2, | 
|---|
| 236 | WORD   wUserParm) | 
|---|
| 237 | { | 
|---|
| 238 | if(pfnmciSendCommand == NULL) { | 
|---|
| 239 | DebugInt3(); | 
|---|
| 240 | return MCIERR_CANNOT_LOAD_DRIVER; | 
|---|
| 241 | } | 
|---|
| 242 | USHORT sel = GetFS(); | 
|---|
| 243 | DWORD ret = pfnmciSendCommand(wDeviceID, wMessage, dwParam1, dwParam2, wUserParm); | 
|---|
| 244 | SetFS(sel); | 
|---|
| 245 | return ret; | 
|---|
| 246 | } | 
|---|
| 247 | //****************************************************************************** | 
|---|
| 248 | //****************************************************************************** | 
|---|
| 249 | DWORD APIENTRY mymciGetErrorString(DWORD   dwError, | 
|---|
| 250 | LPSTR   lpstrBuffer, | 
|---|
| 251 | WORD    wLength) | 
|---|
| 252 | { | 
|---|
| 253 | if(pfnmciGetErrorString == NULL) { | 
|---|
| 254 | DebugInt3(); | 
|---|
| 255 | return MCIERR_CANNOT_LOAD_DRIVER; | 
|---|
| 256 | } | 
|---|
| 257 | USHORT sel = GetFS(); | 
|---|
| 258 | DWORD ret = pfnmciGetErrorString(dwError, lpstrBuffer, wLength); | 
|---|
| 259 | SetFS(sel); | 
|---|
| 260 | return ret; | 
|---|
| 261 | } | 
|---|
| 262 | //****************************************************************************** | 
|---|
| 263 | //****************************************************************************** | 
|---|