Source code of Windows XP (NT5)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

314 lines
9.4 KiB

  1. /**********************************************************************
  2. Copyright (c) 1992-1999 Microsoft Corporation
  3. drvproc.c
  4. DESCRIPTION:
  5. Driver procedure for the Midi Mapper.
  6. HISTORY:
  7. 06/09/93 [t-kyleb] created.
  8. *********************************************************************/
  9. #include "preclude.h"
  10. #include <windows.h>
  11. #include <windowsx.h>
  12. #include <mmsystem.h>
  13. #include <mmddk.h>
  14. #include "idf.h"
  15. #include <memory.h>
  16. #include "midimap.h"
  17. #include "debug.h"
  18. //=========================== Globals ======================================
  19. //
  20. PCHANNEL gapChannel[MAX_CHANNELS] = {NULL};
  21. WORD gwConfigWhere = 0;
  22. UINT gcPorts = 0;
  23. WORD gwFlags = 0;
  24. HINSTANCE ghinst = NULL;
  25. PPORT gpportList = NULL;
  26. PINSTANCE gpinstanceList = NULL;
  27. PINSTANCE gpIoctlInstance = NULL;
  28. PINSTRUMENT gpinstrumentList = NULL;
  29. QUEUE gqFreeSyncObjs;
  30. HMIDISTRM ghMidiStrm = NULL;
  31. TCHAR szVersion[] = TEXT (__DATE__)
  32. TEXT ("@")
  33. TEXT (__TIME__);
  34. DWORD gdwVolume = 0xFFFFFFFFL;
  35. HANDLE hMutexRefCnt = NULL;
  36. static const TCHAR gszRefCnt[] = TEXT ("MidiMapper_modLongMessage_RefCnt");
  37. HANDLE hMutexConfig = NULL;
  38. static const TCHAR gszConfigure[] = TEXT ("MidiMapper_Configure");
  39. static const TCHAR gszReconfigure[] = TEXT("MidiMapper_Reconfig");
  40. //=========================== Prototypes ===================================
  41. //
  42. PRIVATE LRESULT FNLOCAL GetMapperStatus(
  43. LPMAPPERSTATUS lpStat);
  44. /***************************************************************************
  45. @doc INTERNAL
  46. @api LRESULT | DriverProc | The entry point for an installable driver.
  47. @parm DWORD | dwID | For most messages, <p dwID> is the DWORD value
  48. that the driver returns in response to a <m DRV_OPEN> message.
  49. Each time that the driver is opened, through the <f DrvOpen> API,
  50. the driver receives a <m DRV_OPEN> message and can return an
  51. arbitrary, non-zero value. The installable driver interface
  52. saves this value and returns a unique driver handle to the
  53. application. Whenever the application sends a message to the
  54. driver using the driver handle, the interface routes the message
  55. to this entry point and passes the corresponding <p dwID>.
  56. This mechanism allows the driver to use the same or different
  57. identifiers for multiple opens but ensures that driver handles
  58. are unique at the application interface layer.
  59. The following messages are not related to a particular open
  60. instance of the driver. For these messages, the dwID will always
  61. be zero.
  62. DRV_LOAD, DRV_FREE, DRV_ENABLE, DRV_DISABLE, DRV_OPEN
  63. @parm HDRVR | hdrvr | This is the handle returned to the application
  64. by the driver interface.
  65. @parm UINT | umsg | The requested action to be performed. Message
  66. values below <m DRV_RESERVED> are used for globally defined messages.
  67. Message values from <m DRV_RESERVED> to <m DRV_USER> are used for
  68. defined driver protocols. Messages above <m DRV_USER> are used
  69. for driver specific messages.
  70. @parm LPARAM | lParam1 | Data for this message. Defined separately for
  71. each message
  72. @parm LPARAM | lParam2 | Data for this message. Defined separately for
  73. each message
  74. @rdesc Defined separately for each message.
  75. ****************************************************************************/
  76. LRESULT FNEXPORT DriverProc(
  77. DWORD_PTR dwID,
  78. HDRVR hdrvr,
  79. UINT umsg,
  80. LPARAM lParam1,
  81. LPARAM lParam2)
  82. {
  83. //
  84. // NOTE DS is not valid here.
  85. //
  86. switch (umsg)
  87. {
  88. case DRV_LOAD:
  89. return(1L);
  90. case DRV_FREE:
  91. return(0L);
  92. case DRV_OPEN:
  93. case DRV_CLOSE:
  94. return(1L);
  95. case DRV_ENABLE:
  96. // Prevent Synchronization problems
  97. // During Configuration
  98. if (NULL != hMutexConfig)
  99. WaitForSingleObject (hMutexConfig, INFINITE);
  100. SET_ENABLED;
  101. DPF(1, TEXT ("Enable!"));
  102. Configure(0);
  103. if (NULL != hMutexConfig)
  104. ReleaseMutex (hMutexConfig);
  105. return(0L);
  106. case DRV_DISABLE:
  107. CLR_ENABLED;
  108. return(0L);
  109. case DRV_INSTALL:
  110. case DRV_REMOVE:
  111. // If the user installs or removes the driver then let them
  112. // know that they will have to restart.
  113. //
  114. return((LRESULT)DRVCNF_RESTART);
  115. case DRV_ENABLE_DEBUG:
  116. return(DbgEnable((BOOL)lParam1));
  117. case DRV_SET_DEBUG_LEVEL:
  118. return(DbgSetLevel((UINT)lParam1));
  119. case DRV_GETMAPPERSTATUS:
  120. return GetMapperStatus((LPMAPPERSTATUS)lParam1);
  121. #ifdef DEBUG
  122. case DRV_REGISTERDEBUGCB:
  123. DbgRegisterCallback((HWND)lParam1);
  124. return 1L;
  125. case DRV_GETNEXTLOGENTRY:
  126. return (LRESULT)DbgGetNextLogEntry((LPTSTR)lParam1, (UINT)lParam2);
  127. #endif
  128. // Let the default handler handle everything else.
  129. //
  130. default:
  131. DPF(1, TEXT ("DriverProc unsupported=%08lX"), (DWORD)umsg);
  132. return(DefDriverProc(dwID, hdrvr, umsg, lParam1, lParam2));
  133. }
  134. } //** DriverProc()
  135. /***************************************************************************
  136. @doc internal
  137. @api LRESULT | GetMapperStatus | Return information about the current
  138. mapper setup to a debug application.
  139. @parm LPMAPPERSTATUS | lpStat | Pointer to a structure to receive
  140. the mapper information. lpStat->cbStruct must be filled in.
  141. @rdesc | Always returns 0;
  142. ***************************************************************************/
  143. PRIVATE LRESULT FNLOCAL GetMapperStatus(
  144. LPMAPPERSTATUS lpStat)
  145. {
  146. MAPPERSTATUS stat;
  147. if (lpStat->cbStruct < sizeof(stat))
  148. DPF(1, TEXT ("MAPPERSTATUS struct too small -- recompile MAPWATCH!!!"));
  149. stat.cbStruct = lpStat->cbStruct;
  150. // stat.DS = __segname("_DATA");
  151. stat.ghinst = ghinst;
  152. stat.gwFlags = gwFlags;
  153. stat.gwConfigWhere = gwConfigWhere;
  154. stat.pgapChannel = gapChannel;
  155. stat.gpportList = gpportList;
  156. stat.gpinstanceList = gpinstanceList;
  157. stat.gpinstrumentList = gpinstrumentList;
  158. stat.lpszVersion = szVersion;
  159. hmemcpy(lpStat, &stat, (UINT)lpStat->cbStruct);
  160. return 0;
  161. }
  162. /***************************************************************************
  163. @doc internal
  164. @api BOOL | DllEntryPoint | Entry point for the DLL
  165. @parm HINSTANCE | hinstDLL | handle of DLL module
  166. @parm DWORD | fdwReason | Why function was called
  167. @parm LPVOID | lpvReserved | Reserved = must be NULL
  168. @rdesc | Should always return TRUE for success.
  169. ***************************************************************************/
  170. BOOL WINAPI DllEntryPoint(
  171. HINSTANCE hinstDLL,
  172. DWORD fdwReason,
  173. LPVOID lpvReserved)
  174. {
  175. static TCHAR BCODE szSection[] = TEXT ("MIDIMAP");
  176. static TCHAR BCODE szSystemIni[] = TEXT ("system.ini");
  177. static HANDLE hThread = NULL;
  178. DWORD dw;
  179. switch (fdwReason)
  180. {
  181. case DLL_PROCESS_ATTACH:
  182. #ifdef DEBUG
  183. {
  184. UINT uDebugLevel;
  185. uDebugLevel = DbgInitialize(TRUE);
  186. DPF(0, TEXT ("DllEntryPoint - DLL_PROCESS_ATTACH (hinstDLL=%.4Xh, lpvReserved=%.08lXh)"),
  187. hinstDLL, lpvReserved);
  188. DPF(0, TEXT ("Debug Level = %u"), uDebugLevel);
  189. if (2 <= uDebugLevel)
  190. DebugBreak();
  191. }
  192. #endif
  193. // Save our instance handle in a global.
  194. //
  195. ghinst = hinstDLL;
  196. // Initialize Synchronization object
  197. //
  198. QueueInit(&gqFreeSyncObjs);
  199. // Initialize modLongMessage RefCnt Synchronization Object
  200. hMutexRefCnt = CreateMutex (NULL, FALSE, gszRefCnt);
  201. // Initialize Configure Synchronization Object
  202. hMutexConfig = CreateMutex (NULL, FALSE, gszConfigure);
  203. break;
  204. case DLL_THREAD_ATTACH:
  205. #ifdef DEBUG
  206. DPF(0, TEXT ("DllEntryPoint - DLL_THREAD_ATTACH (hinstDLL=%.4Xh, lpvReserved=%.08lXh)"),
  207. hinstDLL, lpvReserved);
  208. #endif
  209. break;
  210. case DLL_THREAD_DETACH:
  211. #ifdef DEBUG
  212. DPF(0, TEXT ("DllEntryPoint - DLL_THREAD_DETACH (hinstDLL=%.4Xh, lpvReserved=%.08lXh)"),
  213. hinstDLL, lpvReserved);
  214. #endif
  215. break;
  216. case DLL_PROCESS_DETACH:
  217. // Cleanup Configure Synchronization Object
  218. //
  219. if (NULL != hMutexConfig) CloseHandle (hMutexConfig);
  220. // Cleanup modLongMessage RefCnt Synchronization Object
  221. //
  222. if (NULL != hMutexRefCnt) CloseHandle (hMutexRefCnt);
  223. // Cleanup Synchronization object
  224. //
  225. QueueCleanup(&gqFreeSyncObjs);
  226. // cleanup instance handle
  227. //
  228. ghinst = NULL;
  229. #ifdef DEBUG
  230. DPF(0, TEXT ("DllEntryPoint - DLL_PROCESS_DETACH (hinstDLL=%.4Xh, lpvReserved=%.08lXh)"),
  231. hinstDLL, lpvReserved);
  232. #endif
  233. break;
  234. } // End switch (fdwReason)
  235. return TRUE;
  236. } // End DllEntryPoint