| 1 | /* $Id: initdsound.cpp,v 1.1 2002-09-14 08:31:25 sandervl Exp $ | 
|---|
| 2 | * | 
|---|
| 3 | * DLL entry point | 
|---|
| 4 | * | 
|---|
| 5 | * Copyright 1998 Sander van Leeuwen | 
|---|
| 6 | * Copyright 1998 Peter Fitzsimmons | 
|---|
| 7 | * | 
|---|
| 8 | * | 
|---|
| 9 | * Project Odin Software License can be found in LICENSE.TXT | 
|---|
| 10 | * | 
|---|
| 11 | */ | 
|---|
| 12 |  | 
|---|
| 13 | /*-------------------------------------------------------------*/ | 
|---|
| 14 | /* INITERM.C -- Source for a custom dynamic link library       */ | 
|---|
| 15 | /*              initialization and termination (_DLL_InitTerm) */ | 
|---|
| 16 | /*              function.                                      */ | 
|---|
| 17 | /*                                                             */ | 
|---|
| 18 | /* When called to perform initialization, this sample function */ | 
|---|
| 19 | /* gets storage for an array of integers, and initializes its  */ | 
|---|
| 20 | /* elements with random integers.  At termination time, it     */ | 
|---|
| 21 | /* frees the array.  Substitute your own special processing.   */ | 
|---|
| 22 | /*-------------------------------------------------------------*/ | 
|---|
| 23 |  | 
|---|
| 24 |  | 
|---|
| 25 | /* Include files */ | 
|---|
| 26 | #define  INCL_DOSMODULEMGR | 
|---|
| 27 | #define  INCL_DOSPROCESS | 
|---|
| 28 | #define  INCL_DOSERRORS | 
|---|
| 29 | #define  INCL_OS2MM | 
|---|
| 30 | #include <os2wrap.h>    //Odin32 OS/2 api wrappers | 
|---|
| 31 | #include <os2mewrap.h> //Odin32 OS/2 MMPM/2 api wrappers | 
|---|
| 32 | #include <stdlib.h> | 
|---|
| 33 | #include <stdio.h> | 
|---|
| 34 | #include <string.h> | 
|---|
| 35 | #include <odin.h> | 
|---|
| 36 | #include <win32type.h> | 
|---|
| 37 | #include <win32api.h> | 
|---|
| 38 | #include <winconst.h> | 
|---|
| 39 | #include <odinlx.h> | 
|---|
| 40 | #include <misc.h>       /*PLF Wed  98-03-18 23:18:15*/ | 
|---|
| 41 | #include <initdll.h> | 
|---|
| 42 | #include <custombuild.h> | 
|---|
| 43 | #include "initdsound.h" | 
|---|
| 44 |  | 
|---|
| 45 | extern "C" { | 
|---|
| 46 | //Win32 resource table (produced by wrc) | 
|---|
| 47 | extern DWORD _Resource_PEResTab; | 
|---|
| 48 | } | 
|---|
| 49 |  | 
|---|
| 50 | static HMODULE dllHandle = 0; | 
|---|
| 51 | static HMODULE MMPMLibraryHandle = 0; | 
|---|
| 52 |  | 
|---|
| 53 | BOOL fdsMMPMAvailable = TRUE; | 
|---|
| 54 |  | 
|---|
| 55 | DWORD (APIENTRY *pfnDSmciSendCommand)(WORD   wDeviceID, | 
|---|
| 56 | WORD   wMessage, | 
|---|
| 57 | DWORD  dwParam1, | 
|---|
| 58 | PVOID  dwParam2, | 
|---|
| 59 | WORD   wUserParm) = NULL; | 
|---|
| 60 | DWORD (APIENTRY *pfnDSmciGetErrorString)(DWORD   dwError, | 
|---|
| 61 | LPSTR   lpstrBuffer, | 
|---|
| 62 | WORD    wLength) = NULL; | 
|---|
| 63 |  | 
|---|
| 64 |  | 
|---|
| 65 | //****************************************************************************** | 
|---|
| 66 | //****************************************************************************** | 
|---|
| 67 | BOOL WINAPI LibMainDSound(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID fImpLoad) | 
|---|
| 68 | { | 
|---|
| 69 | char   szError[CCHMAXPATH]; | 
|---|
| 70 | HKEY   hKey; | 
|---|
| 71 |  | 
|---|
| 72 | switch (fdwReason) | 
|---|
| 73 | { | 
|---|
| 74 | case DLL_PROCESS_ATTACH: | 
|---|
| 75 |  | 
|---|
| 76 | if(fdsMMPMAvailable == TRUE) | 
|---|
| 77 | {//if audio access wasn't disabled already, check if mmpm2 is installed | 
|---|
| 78 | // try to load the MDM library, not MMPM directly!!! | 
|---|
| 79 | if (DosLoadModule(szError, sizeof(szError), | 
|---|
| 80 | "MDM.DLL", &MMPMLibraryHandle) != NO_ERROR) | 
|---|
| 81 | { | 
|---|
| 82 | // this system has no MMPM :-( | 
|---|
| 83 | fdsMMPMAvailable = FALSE; | 
|---|
| 84 | } | 
|---|
| 85 | else | 
|---|
| 86 | { | 
|---|
| 87 | /* detect if MMPM is available */ | 
|---|
| 88 | if (DosQueryProcAddr(MMPMLibraryHandle, | 
|---|
| 89 | 1, /* ORD_MCISENDCOMMAND */ | 
|---|
| 90 | NULL, | 
|---|
| 91 | (PFN*)&pfnDSmciSendCommand) != NO_ERROR) | 
|---|
| 92 | { | 
|---|
| 93 | fdsMMPMAvailable = FALSE; | 
|---|
| 94 | } | 
|---|
| 95 | else | 
|---|
| 96 | { | 
|---|
| 97 | fdsMMPMAvailable = TRUE; | 
|---|
| 98 | } | 
|---|
| 99 |  | 
|---|
| 100 | /* see if we can get the address for the mciGetErrorString function */ | 
|---|
| 101 | if (fdsMMPMAvailable == TRUE) | 
|---|
| 102 | { | 
|---|
| 103 | if (DosQueryProcAddr(MMPMLibraryHandle, | 
|---|
| 104 | 3, /* ORD_MCIGETERRORSTRING */ | 
|---|
| 105 | NULL, | 
|---|
| 106 | (PFN*)&pfnDSmciGetErrorString) != NO_ERROR) | 
|---|
| 107 | pfnDSmciGetErrorString = NULL; | 
|---|
| 108 | } | 
|---|
| 109 | dprintf(("MMPM/2 is available; hmod %x", MMPMLibraryHandle)); | 
|---|
| 110 | dprintf(("mciSendCommand    %x", pfnDSmciSendCommand)); | 
|---|
| 111 | dprintf(("mciGetErrorString %x", pfnDSmciGetErrorString)); | 
|---|
| 112 | } | 
|---|
| 113 | } | 
|---|
| 114 |  | 
|---|
| 115 | if(fdsMMPMAvailable && RegOpenKeyA(HKEY_LOCAL_MACHINE, CUSTOM_BUILD_OPTIONS_KEY, &hKey) == 0) | 
|---|
| 116 | { | 
|---|
| 117 | DWORD dwSize, dwType; | 
|---|
| 118 | DWORD dwFlag; | 
|---|
| 119 |  | 
|---|
| 120 | dwSize = sizeof(dwFlag); | 
|---|
| 121 | LONG rc = RegQueryValueExA(hKey, DISABLE_AUDIO_KEY, | 
|---|
| 122 | NULL, &dwType, | 
|---|
| 123 | (LPBYTE)&dwFlag, | 
|---|
| 124 | &dwSize); | 
|---|
| 125 |  | 
|---|
| 126 | if(rc == 0 && dwType == REG_DWORD) { | 
|---|
| 127 | if(dwFlag) { | 
|---|
| 128 | fdsMMPMAvailable = FALSE; | 
|---|
| 129 | pfnDSmciGetErrorString = NULL; | 
|---|
| 130 | pfnDSmciSendCommand = NULL; | 
|---|
| 131 | } | 
|---|
| 132 | } | 
|---|
| 133 | RegCloseKey(hKey); | 
|---|
| 134 | } | 
|---|
| 135 | return TRUE; | 
|---|
| 136 |  | 
|---|
| 137 | case DLL_THREAD_ATTACH: | 
|---|
| 138 | case DLL_THREAD_DETACH: | 
|---|
| 139 | return TRUE; | 
|---|
| 140 |  | 
|---|
| 141 | case DLL_PROCESS_DETACH: | 
|---|
| 142 | if(MMPMLibraryHandle) DosFreeModule(MMPMLibraryHandle); | 
|---|
| 143 | return TRUE; | 
|---|
| 144 | } | 
|---|
| 145 | return FALSE; | 
|---|
| 146 | } | 
|---|
| 147 | /****************************************************************************/ | 
|---|
| 148 | /* _DLL_InitTerm is the function that gets called by the operating system   */ | 
|---|
| 149 | /* loader when it loads and frees this DLL for each process that accesses   */ | 
|---|
| 150 | /* this DLL.  However, it only gets called the first time the DLL is loaded */ | 
|---|
| 151 | /* and the last time it is freed for a particular process.  The system      */ | 
|---|
| 152 | /* linkage convention MUST be used because the operating system loader is   */ | 
|---|
| 153 | /* calling this function.                                                   */ | 
|---|
| 154 | /****************************************************************************/ | 
|---|
| 155 | ULONG APIENTRY inittermDSound(ULONG hModule, ULONG ulFlag) | 
|---|
| 156 | { | 
|---|
| 157 |  | 
|---|
| 158 | /*-------------------------------------------------------------------------*/ | 
|---|
| 159 | /* If ulFlag is zero then the DLL is being loaded so initialization should */ | 
|---|
| 160 | /* be performed.  If ulFlag is 1 then the DLL is being freed so            */ | 
|---|
| 161 | /* termination should be performed.                                        */ | 
|---|
| 162 | /*-------------------------------------------------------------------------*/ | 
|---|
| 163 |  | 
|---|
| 164 | switch (ulFlag) { | 
|---|
| 165 | case 0 : | 
|---|
| 166 | { | 
|---|
| 167 | dllHandle = RegisterLxDll(hModule, LibMainDSound, (PVOID)&_Resource_PEResTab); | 
|---|
| 168 | if(dllHandle == 0) | 
|---|
| 169 | return 0UL; | 
|---|
| 170 |  | 
|---|
| 171 | dprintf(("dsound init %s %s (%x)", __DATE__, __TIME__, inittermDSound)); | 
|---|
| 172 | break; | 
|---|
| 173 | } | 
|---|
| 174 |  | 
|---|
| 175 | case 1 : | 
|---|
| 176 | if(dllHandle) { | 
|---|
| 177 | UnregisterLxDll(dllHandle); | 
|---|
| 178 | } | 
|---|
| 179 | break; | 
|---|
| 180 | default  : | 
|---|
| 181 | return 0UL; | 
|---|
| 182 | } | 
|---|
| 183 |  | 
|---|
| 184 | /***********************************************************/ | 
|---|
| 185 | /* A non-zero value must be returned to indicate success.  */ | 
|---|
| 186 | /***********************************************************/ | 
|---|
| 187 | return 1UL; | 
|---|
| 188 | } | 
|---|
| 189 | //****************************************************************************** | 
|---|
| 190 | //****************************************************************************** | 
|---|
| 191 |  | 
|---|
| 192 | DWORD APIENTRY dsmciSendCommand(WORD   wDeviceID, | 
|---|
| 193 | WORD   wMessage, | 
|---|
| 194 | DWORD  dwParam1, | 
|---|
| 195 | PVOID  dwParam2, | 
|---|
| 196 | WORD   wUserParm) | 
|---|
| 197 | { | 
|---|
| 198 | if(pfnDSmciSendCommand == NULL) { | 
|---|
| 199 | DebugInt3(); | 
|---|
| 200 | return MCIERR_CANNOT_LOAD_DRIVER; | 
|---|
| 201 | } | 
|---|
| 202 | USHORT sel = RestoreOS2FS(); | 
|---|
| 203 | DWORD ret = pfnDSmciSendCommand(wDeviceID, wMessage, dwParam1, dwParam2, wUserParm); | 
|---|
| 204 | SetFS(sel); | 
|---|
| 205 | return ret; | 
|---|
| 206 | } | 
|---|
| 207 | //****************************************************************************** | 
|---|
| 208 | //****************************************************************************** | 
|---|
| 209 | DWORD APIENTRY dsmciGetErrorString(DWORD   dwError, | 
|---|
| 210 | LPSTR   lpstrBuffer, | 
|---|
| 211 | WORD    wLength) | 
|---|
| 212 | { | 
|---|
| 213 | if(pfnDSmciGetErrorString == NULL) { | 
|---|
| 214 | DebugInt3(); | 
|---|
| 215 | return MCIERR_CANNOT_LOAD_DRIVER; | 
|---|
| 216 | } | 
|---|
| 217 | USHORT sel = RestoreOS2FS(); | 
|---|
| 218 | DWORD ret = pfnDSmciGetErrorString(dwError, lpstrBuffer, wLength); | 
|---|
| 219 | SetFS(sel); | 
|---|
| 220 | return ret; | 
|---|
| 221 | } | 
|---|
| 222 | //****************************************************************************** | 
|---|
| 223 | //****************************************************************************** | 
|---|