| 1 | /* $Id: initterm.cpp,v 1.18 2002-09-14 08:31:25 sandervl Exp $ | 
|---|
| 2 | * | 
|---|
| 3 | * DSOUND DLL entry point | 
|---|
| 4 | * | 
|---|
| 5 | * Copyright 1998 Sander van Leeuwen | 
|---|
| 6 | * Copyright 1998 Peter Fitzsimmons | 
|---|
| 7 | * | 
|---|
| 8 | * Project Odin Software License can be found in LICENSE.TXT | 
|---|
| 9 | */ | 
|---|
| 10 |  | 
|---|
| 11 | #define  INCL_DOSMODULEMGR | 
|---|
| 12 | #define  INCL_DOSPROCESS | 
|---|
| 13 | #define  INCL_DOSERRORS | 
|---|
| 14 | #define  INCL_OS2MM | 
|---|
| 15 | #include <os2wrap.h>    //Odin32 OS/2 api wrappers | 
|---|
| 16 | #include <os2mewrap.h> //Odin32 OS/2 MMPM/2 api wrappers | 
|---|
| 17 | #include <stdlib.h> | 
|---|
| 18 | #include <stdio.h> | 
|---|
| 19 | #include <string.h> | 
|---|
| 20 | #include <odin.h> | 
|---|
| 21 | #include <win32type.h> | 
|---|
| 22 | #include <win32api.h> | 
|---|
| 23 | #include <winconst.h> | 
|---|
| 24 | #include <odinlx.h> | 
|---|
| 25 | #include <misc.h>       /*PLF Wed  98-03-18 23:18:15*/ | 
|---|
| 26 | #include <initdll.h> | 
|---|
| 27 | #include <custombuild.h> | 
|---|
| 28 | #include "initdsound.h" | 
|---|
| 29 |  | 
|---|
| 30 | // Win32 resource table (produced by wrc) | 
|---|
| 31 | extern DWORD dsound_PEResTab; | 
|---|
| 32 |  | 
|---|
| 33 | static HMODULE dllHandle = 0; | 
|---|
| 34 | static HMODULE MMPMLibraryHandle = 0; | 
|---|
| 35 |  | 
|---|
| 36 | char dsoundPath[CCHMAXPATH] = ""; | 
|---|
| 37 |  | 
|---|
| 38 | BOOL fdsMMPMAvailable = TRUE; | 
|---|
| 39 |  | 
|---|
| 40 | DWORD (APIENTRY *pfnDSmciSendCommand)(WORD   wDeviceID, | 
|---|
| 41 | WORD   wMessage, | 
|---|
| 42 | DWORD  dwParam1, | 
|---|
| 43 | PVOID  dwParam2, | 
|---|
| 44 | WORD   wUserParm) = NULL; | 
|---|
| 45 | DWORD (APIENTRY *pfnDSmciGetErrorString)(DWORD   dwError, | 
|---|
| 46 | LPSTR   lpstrBuffer, | 
|---|
| 47 | WORD    wLength) = NULL; | 
|---|
| 48 |  | 
|---|
| 49 | BOOL WINAPI LibMainDSound(HINSTANCE hinstDLL, DWORD fdwReason, LPVOID fImpLoad) | 
|---|
| 50 | { | 
|---|
| 51 | char   szError[CCHMAXPATH]; | 
|---|
| 52 | HKEY   hKey; | 
|---|
| 53 |  | 
|---|
| 54 | switch (fdwReason) | 
|---|
| 55 | { | 
|---|
| 56 | case DLL_PROCESS_ATTACH: | 
|---|
| 57 |  | 
|---|
| 58 | if(fdsMMPMAvailable == TRUE) | 
|---|
| 59 | { | 
|---|
| 60 | // if audio access wasn't disabled already, check if mmpm2 is installed | 
|---|
| 61 | // try to load the MDM library, not MMPM directly!!! | 
|---|
| 62 | if (DosLoadModule(szError, sizeof(szError), | 
|---|
| 63 | "MDM", &MMPMLibraryHandle) != NO_ERROR) | 
|---|
| 64 | { | 
|---|
| 65 | // this system has no MMPM :-( | 
|---|
| 66 | fdsMMPMAvailable = FALSE; | 
|---|
| 67 | } | 
|---|
| 68 | else | 
|---|
| 69 | { | 
|---|
| 70 | /* detect if MMPM is available */ | 
|---|
| 71 | if (DosQueryProcAddr(MMPMLibraryHandle, | 
|---|
| 72 | 1, /* ORD_MCISENDCOMMAND */ | 
|---|
| 73 | NULL, | 
|---|
| 74 | (PFN*)&pfnDSmciSendCommand) != NO_ERROR) | 
|---|
| 75 | { | 
|---|
| 76 | fdsMMPMAvailable = FALSE; | 
|---|
| 77 | } | 
|---|
| 78 | else | 
|---|
| 79 | { | 
|---|
| 80 | fdsMMPMAvailable = TRUE; | 
|---|
| 81 | } | 
|---|
| 82 |  | 
|---|
| 83 | /* see if we can get the address for the mciGetErrorString function */ | 
|---|
| 84 | if (fdsMMPMAvailable == TRUE) | 
|---|
| 85 | { | 
|---|
| 86 | if (DosQueryProcAddr(MMPMLibraryHandle, | 
|---|
| 87 | 3, /* ORD_MCIGETERRORSTRING */ | 
|---|
| 88 | NULL, | 
|---|
| 89 | (PFN*)&pfnDSmciGetErrorString) != NO_ERROR) | 
|---|
| 90 | pfnDSmciGetErrorString = NULL; | 
|---|
| 91 | } | 
|---|
| 92 | dprintf(("MMPM/2 is available; hmod %x", MMPMLibraryHandle)); | 
|---|
| 93 | dprintf(("mciSendCommand    %x", pfnDSmciSendCommand)); | 
|---|
| 94 | dprintf(("mciGetErrorString %x", pfnDSmciGetErrorString)); | 
|---|
| 95 | } | 
|---|
| 96 | } | 
|---|
| 97 |  | 
|---|
| 98 | if(fdsMMPMAvailable && RegOpenKeyA(HKEY_LOCAL_MACHINE, CUSTOM_BUILD_OPTIONS_KEY, &hKey) == 0) | 
|---|
| 99 | { | 
|---|
| 100 | DWORD dwSize, dwType; | 
|---|
| 101 | DWORD dwFlag; | 
|---|
| 102 |  | 
|---|
| 103 | dwSize = sizeof(dwFlag); | 
|---|
| 104 | LONG rc = RegQueryValueExA(hKey, DISABLE_AUDIO_KEY, | 
|---|
| 105 | NULL, &dwType, | 
|---|
| 106 | (LPBYTE)&dwFlag, | 
|---|
| 107 | &dwSize); | 
|---|
| 108 |  | 
|---|
| 109 | if(rc == 0 && dwType == REG_DWORD) { | 
|---|
| 110 | if(dwFlag) { | 
|---|
| 111 | fdsMMPMAvailable = FALSE; | 
|---|
| 112 | pfnDSmciGetErrorString = NULL; | 
|---|
| 113 | pfnDSmciSendCommand = NULL; | 
|---|
| 114 | } | 
|---|
| 115 | } | 
|---|
| 116 | RegCloseKey(hKey); | 
|---|
| 117 | } | 
|---|
| 118 | return TRUE; | 
|---|
| 119 |  | 
|---|
| 120 | case DLL_THREAD_ATTACH: | 
|---|
| 121 | case DLL_THREAD_DETACH: | 
|---|
| 122 | return TRUE; | 
|---|
| 123 |  | 
|---|
| 124 | case DLL_PROCESS_DETACH: | 
|---|
| 125 | if(MMPMLibraryHandle) DosFreeModule(MMPMLibraryHandle); | 
|---|
| 126 | return TRUE; | 
|---|
| 127 | } | 
|---|
| 128 | return FALSE; | 
|---|
| 129 | } | 
|---|
| 130 |  | 
|---|
| 131 | DWORD APIENTRY dsmciSendCommand(WORD   wDeviceID, | 
|---|
| 132 | WORD   wMessage, | 
|---|
| 133 | DWORD  dwParam1, | 
|---|
| 134 | PVOID  dwParam2, | 
|---|
| 135 | WORD   wUserParm) | 
|---|
| 136 | { | 
|---|
| 137 | if(pfnDSmciSendCommand == NULL) { | 
|---|
| 138 | DebugInt3(); | 
|---|
| 139 | return MCIERR_CANNOT_LOAD_DRIVER; | 
|---|
| 140 | } | 
|---|
| 141 | USHORT sel = RestoreOS2FS(); | 
|---|
| 142 | DWORD ret = pfnDSmciSendCommand(wDeviceID, wMessage, dwParam1, dwParam2, wUserParm); | 
|---|
| 143 | SetFS(sel); | 
|---|
| 144 | return ret; | 
|---|
| 145 | } | 
|---|
| 146 |  | 
|---|
| 147 | DWORD APIENTRY dsmciGetErrorString(DWORD   dwError, | 
|---|
| 148 | LPSTR   lpstrBuffer, | 
|---|
| 149 | WORD    wLength) | 
|---|
| 150 | { | 
|---|
| 151 | if(pfnDSmciGetErrorString == NULL) { | 
|---|
| 152 | DebugInt3(); | 
|---|
| 153 | return MCIERR_CANNOT_LOAD_DRIVER; | 
|---|
| 154 | } | 
|---|
| 155 | USHORT sel = RestoreOS2FS(); | 
|---|
| 156 | DWORD ret = pfnDSmciGetErrorString(dwError, lpstrBuffer, wLength); | 
|---|
| 157 | SetFS(sel); | 
|---|
| 158 | return ret; | 
|---|
| 159 | } | 
|---|
| 160 |  | 
|---|
| 161 | ULONG SYSTEM DLL_InitDSound(ULONG hModule) | 
|---|
| 162 | { | 
|---|
| 163 | DosQueryModuleName(hModule, CCHMAXPATH, dsoundPath); | 
|---|
| 164 | char *endofpath = strrchr(dsoundPath, '\\'); | 
|---|
| 165 | if (endofpath) | 
|---|
| 166 | *(endofpath+1) = 0; | 
|---|
| 167 |  | 
|---|
| 168 | CheckVersionFromHMOD(PE2LX_VERSION, hModule); /*PLF Wed  98-03-18 05:28:48*/ | 
|---|
| 169 |  | 
|---|
| 170 | dllHandle = RegisterLxDll(hModule, LibMainDSound, (PVOID)&dsound_PEResTab); | 
|---|
| 171 | if(dllHandle == 0) | 
|---|
| 172 | return -1; | 
|---|
| 173 |  | 
|---|
| 174 | dprintf(("dsound init %s %s (%x)", __DATE__, __TIME__, DLL_InitDSound)); | 
|---|
| 175 |  | 
|---|
| 176 | return 0; | 
|---|
| 177 | } | 
|---|
| 178 |  | 
|---|
| 179 | void SYSTEM DLL_TermDSound(ULONG hModule) | 
|---|
| 180 | { | 
|---|
| 181 | if (dllHandle) | 
|---|
| 182 | UnregisterLxDll(dllHandle); | 
|---|
| 183 | } | 
|---|
| 184 |  | 
|---|
| 185 | ULONG SYSTEM DLL_Init(ULONG hModule) | 
|---|
| 186 | { | 
|---|
| 187 | if (DLL_InitDefault(hModule) == -1) | 
|---|
| 188 | return -1; | 
|---|
| 189 | return DLL_InitDSound(hModule); | 
|---|
| 190 | } | 
|---|
| 191 |  | 
|---|
| 192 | void SYSTEM DLL_Term(ULONG hModule) | 
|---|
| 193 | { | 
|---|
| 194 | DLL_TermDSound(hModule); | 
|---|
| 195 | DLL_TermDefault(hModule); | 
|---|
| 196 | } | 
|---|