Release 970525
[wine] / multimedia / midi.c
1 /*
2  * Sample MIDI Wine Driver for Linux
3  *
4  * Copyright 1994 Martin Ayotte
5  */
6
7 #include <stdio.h>
8 #include <stdlib.h>
9 #include <unistd.h>
10 #include <fcntl.h>
11 #include <sys/ioctl.h>
12 #include "windows.h"
13 #include "ldt.h"
14 #include "user.h"
15 #include "driver.h"
16 #include "mmsystem.h"
17 #include "xmalloc.h"
18
19 #include "stddebug.h"
20 #include "debug.h"
21
22 #ifdef linux
23 #include <linux/soundcard.h>
24 #elif __FreeBSD__
25 #include <machine/soundcard.h>
26 #endif
27
28 #if defined(linux) || defined(__FreeBSD__)
29 #define MIDI_DEV "/dev/sequencer"
30
31 #ifdef SOUND_VERSION
32 #define IOCTL(a,b,c)            ioctl(a,b,&c)
33 #else
34 #define IOCTL(a,b,c)            (c = ioctl(a,b,c) )
35 #endif
36
37 #define MAX_MIDIINDRV   (1)
38 #define MAX_MIDIOUTDRV  (1)
39 #define MAX_MCIMIDIDRV  (1)
40
41 typedef struct {
42         int                             unixdev;
43         int                             state;
44         DWORD                   bufsize;
45         MIDIOPENDESC    midiDesc;
46         WORD                    wFlags;
47         LPMIDIHDR               lpQueueHdr;
48         DWORD                   dwTotalPlayed;
49         } LINUX_MIDIIN;
50
51 typedef struct {
52         int                             unixdev;
53         int                             state;
54         DWORD                   bufsize;
55         MIDIOPENDESC    midiDesc;
56         WORD                    wFlags;
57         LPMIDIHDR               lpQueueHdr;
58         DWORD                   dwTotalPlayed;
59         } LINUX_MIDIOUT;
60
61 typedef struct {
62         int     nUseCount;          /* Incremented for each shared open */
63         BOOL16  fShareable;         /* TRUE if first open was shareable */
64         WORD    wNotifyDeviceID;    /* MCI device ID with a pending notification */
65         HANDLE16 hCallback;         /* Callback handle for pending notification */
66         HMMIO16 hFile;                          /* mmio file handle open as Element             */
67         DWORD   dwBeginData;
68         DWORD   dwTotalLen;
69         WORD    wFormat;
70         WORD    nTracks;
71         WORD    nTempo;
72         MCI_OPEN_PARMS openParms;
73 /*      MIDIHDR         MidiHdr; */
74         HLOCAL16        hMidiHdr;
75         WORD            dwStatus;
76         } LINUX_MCIMIDI;
77
78 static LINUX_MIDIIN             MidiInDev[MAX_MIDIINDRV];
79 static LINUX_MIDIOUT    MidiOutDev[MAX_MIDIOUTDRV];
80 static LINUX_MCIMIDI    MCIMidiDev[MAX_MCIMIDIDRV];
81 #endif
82
83
84 /**************************************************************************
85 *                               MIDI_NotifyClient                       [internal]
86 */
87 static DWORD MIDI_NotifyClient(UINT16 wDevID, WORD wMsg, 
88                                 DWORD dwParam1, DWORD dwParam2)
89 {
90 #if defined(linux) || defined(__FreeBSD__)
91         if (MidiInDev[wDevID].wFlags != DCB_NULL && !DriverCallback(
92                 MidiInDev[wDevID].midiDesc.dwCallback, MidiInDev[wDevID].wFlags, 
93                 MidiInDev[wDevID].midiDesc.hMidi, wMsg, 
94                 MidiInDev[wDevID].midiDesc.dwInstance, dwParam1, dwParam2)) {
95                 dprintf_midi(stddeb, "MIDI_NotifyClient // can't notify client !\n");
96                 return MMSYSERR_NOERROR;
97                 }
98         return 0;
99 #else
100         return MMSYSERR_NOTENABLED;
101 #endif
102 }
103
104
105 /**************************************************************************
106 *                               MIDI_ReadByte                   [internal]      
107 */
108 static DWORD MIDI_ReadByte(UINT16 wDevID, BYTE *lpbyt)
109 {
110 #if defined(linux) || defined(__FreeBSD__)
111         if (lpbyt != NULL) {
112                 if (mmioRead(MCIMidiDev[wDevID].hFile, (HPSTR)lpbyt,
113                         (long) sizeof(BYTE)) == (long) sizeof(BYTE)) {
114                         return 0;
115                         }
116                 }
117         dprintf_midi(stddeb, "MIDI_ReadByte // error reading wDevID=%04X\n", wDevID);
118         return MCIERR_INTERNAL;
119
120 #else
121         return MMSYSERR_NOTENABLED;
122 #endif
123 }
124
125
126 /**************************************************************************
127 *                               MIDI_ReadWord                   [internal]      
128 */
129 static DWORD MIDI_ReadWord(UINT16 wDevID, LPWORD lpw)
130 {
131         BYTE    hibyte, lobyte;
132         if (lpw != NULL) {
133                 if (MIDI_ReadByte(wDevID, &hibyte) == 0) {
134                         if (MIDI_ReadByte(wDevID, &lobyte) == 0) {
135                                 *lpw = ((WORD)hibyte << 8) + lobyte;
136                                 return 0;
137                                 }
138                         }
139                 }
140         dprintf_midi(stddeb, "MIDI_ReadWord // error reading wDevID=%04X\n", wDevID);
141         return MCIERR_INTERNAL;
142 }
143
144
145 /**************************************************************************
146 *                               MIDI_ReadLong                   [internal]      
147 */
148 static DWORD MIDI_ReadLong(UINT16 wDevID, LPDWORD lpdw)
149 {
150         WORD    hiword, loword;
151         if (lpdw != NULL) {
152                 if (MIDI_ReadWord(wDevID, &hiword) == 0) {
153                         if (MIDI_ReadWord(wDevID, &loword) == 0) {
154                                 *lpdw = MAKELONG(loword, hiword);
155                                 return 0;
156                                 }
157                         }
158                 }
159         dprintf_midi(stddeb, "MIDI_ReadLong // error reading wDevID=%04X\n", wDevID);
160         return MCIERR_INTERNAL;
161 }
162
163
164 /**************************************************************************
165 *                               MIDI_ReadVaryLen                [internal]      
166 */
167 static DWORD MIDI_ReadVaryLen(UINT16 wDevID, LPDWORD lpdw)
168 {
169         BYTE    byte;
170         DWORD   value;
171         if (lpdw == NULL) return MCIERR_INTERNAL;
172         if (MIDI_ReadByte(wDevID, &byte) != 0) {
173                 dprintf_midi(stddeb, "MIDI_ReadVaryLen // error reading wDevID=%04X\n", wDevID);
174                 return MCIERR_INTERNAL;
175                 }
176         value = (DWORD)(byte & 0x7F);
177         while (byte & 0x80) {
178                 if (MIDI_ReadByte(wDevID, &byte) != 0) {
179                         dprintf_midi(stddeb, "MIDI_ReadVaryLen // error reading wDevID=%04X\n", wDevID);
180                         return MCIERR_INTERNAL;
181                         }
182                 value = (value << 7) + (byte & 0x7F);
183                 }
184         *lpdw = value;
185 /*
186         dprintf_midi(stddeb, "MIDI_ReadVaryLen // val=%08lX \n", value);
187 */
188         return 0;
189 }
190
191
192 /**************************************************************************
193 *                               MIDI_ReadMThd                   [internal]      
194 */
195 static DWORD MIDI_ReadMThd(UINT16 wDevID, DWORD dwOffset)
196 {
197 #if defined(linux) || defined(__FreeBSD__)
198         DWORD   toberead;
199         FOURCC  fourcc;
200         dprintf_midi(stddeb, "MIDI_ReadMThd(%04X, %08lX);\n", wDevID, dwOffset);
201         if (mmioSeek(MCIMidiDev[wDevID].hFile, dwOffset, SEEK_SET) != dwOffset) {
202                 dprintf_midi(stddeb, "MIDI_ReadMThd // can't seek at %08lX begin of 'MThd' \n", dwOffset);
203                 return MCIERR_INTERNAL;
204                 }
205         if (mmioRead(MCIMidiDev[wDevID].hFile, (HPSTR)&fourcc,
206                 (long) sizeof(FOURCC)) != (long) sizeof(FOURCC)) {
207                 return MCIERR_INTERNAL;
208                 }
209         if (MIDI_ReadLong(wDevID, &toberead) != 0) {
210                 return MCIERR_INTERNAL;
211                 }
212         if (MIDI_ReadWord(wDevID, &MCIMidiDev[wDevID].wFormat) != 0) {
213                 return MCIERR_INTERNAL;
214                 }
215         if (MIDI_ReadWord(wDevID, &MCIMidiDev[wDevID].nTracks) != 0) {
216                 return MCIERR_INTERNAL;
217                 }
218         if (MIDI_ReadWord(wDevID, &MCIMidiDev[wDevID].nTempo) != 0) {
219                 return MCIERR_INTERNAL;
220                 }
221         dprintf_midi(stddeb, "MIDI_ReadMThd // toberead=%08lX, wFormat=%04X nTracks=%04X nTempo=%04X\n",
222                 toberead, MCIMidiDev[wDevID].wFormat,
223                 MCIMidiDev[wDevID].nTracks,
224                 MCIMidiDev[wDevID].nTempo);
225         toberead -= 3 * sizeof(WORD);
226 /*
227                 ntrks = read16bit ();
228                 Mf_division = division = read16bit ();
229 */
230         return 0;
231
232 #else
233          return MMSYSERR_NOTENABLED;
234 #endif
235 }
236
237
238 static DWORD MIDI_ReadMTrk(UINT16 wDevID, DWORD dwOffset)
239 {
240 #if defined(linux) || defined(__FreeBSD__)
241         DWORD   toberead;
242         FOURCC  fourcc;
243         if (mmioSeek(MCIMidiDev[wDevID].hFile, dwOffset, SEEK_SET) != dwOffset) {
244                 dprintf_midi(stddeb, "MIDI_ReadMTrk // can't seek at %08lX begin of 'MThd' \n", dwOffset);
245                 }
246         if (mmioRead(MCIMidiDev[wDevID].hFile, (HPSTR)&fourcc,
247                 (long) sizeof(FOURCC)) != (long) sizeof(FOURCC)) {
248                 return MCIERR_INTERNAL;
249                 }
250         if (MIDI_ReadLong(wDevID, &toberead) != 0) {
251                 return MCIERR_INTERNAL;
252                 }
253         dprintf_midi(stddeb, "MIDI_ReadMTrk // toberead=%08lX\n", toberead);
254         toberead -= 3 * sizeof(WORD);
255         MCIMidiDev[wDevID].dwTotalLen = toberead;
256         return 0;
257 #else
258          return MMSYSERR_NOTENABLED;
259 #endif
260 }
261
262
263 /**************************************************************************
264 *                               MIDI_mciOpen                    [internal]      
265 */
266 static DWORD MIDI_mciOpen(UINT16 wDevID, DWORD dwFlags, LPMCI_OPEN_PARMS lpParms)
267 {
268 #if defined(linux) || defined(__FreeBSD__)
269         MIDIOPENDESC    MidiDesc;
270         DWORD           dwRet;
271         DWORD           dwOffset;
272         LPSTR           lpstrElementName;
273         char            str[128];
274
275         dprintf_midi(stddeb, "MIDI_mciOpen(%08lX, %p)\n", dwFlags, lpParms);
276         if (lpParms == NULL) return MCIERR_INTERNAL;
277
278         if (MCIMidiDev[wDevID].nUseCount > 0) {
279                 /* The driver already open on this channel */
280                 /* If the driver was opened shareable before and this open specifies */
281                 /* shareable then increment the use count */
282                 if (MCIMidiDev[wDevID].fShareable && (dwFlags & MCI_OPEN_SHAREABLE))
283                         ++MCIMidiDev[wDevID].nUseCount;
284                 else
285                         return MCIERR_MUST_USE_SHAREABLE;
286                 }
287         else {
288                 MCIMidiDev[wDevID].nUseCount = 1;
289                 MCIMidiDev[wDevID].fShareable = dwFlags & MCI_OPEN_SHAREABLE;
290                 MCIMidiDev[wDevID].hMidiHdr = USER_HEAP_ALLOC(sizeof(MIDIHDR));
291                 }
292         dprintf_midi(stddeb, "MIDI_mciOpen // wDevID=%04X\n", wDevID);
293 /*      lpParms->wDeviceID = wDevID;*/
294         dprintf_midi(stddeb, "MIDI_mciOpen // lpParms->wDevID=%04X\n", lpParms->wDeviceID);
295         dprintf_midi(stddeb, "MIDI_mciOpen // before OPEN_ELEMENT\n");
296         if (dwFlags & MCI_OPEN_ELEMENT) {
297                 lpstrElementName = (LPSTR)PTR_SEG_TO_LIN(lpParms->lpstrElementName);
298                 dprintf_midi(stddeb, "MIDI_mciOpen // MCI_OPEN_ELEMENT '%s' !\n", lpstrElementName);
299                 if (strlen(lpstrElementName) > 0) {
300                         strcpy(str, lpstrElementName);
301                         CharUpper32A(str);
302                         MCIMidiDev[wDevID].hFile = mmioOpen(str, NULL, 
303                                 MMIO_ALLOCBUF | MMIO_READWRITE | MMIO_EXCLUSIVE);
304                         if (MCIMidiDev[wDevID].hFile == 0) {
305                                 dprintf_midi(stddeb, "MIDI_mciOpen // can't find file='%s' !\n", str);
306                                 return MCIERR_FILE_NOT_FOUND;
307                                 }
308                         }
309                 else 
310                         MCIMidiDev[wDevID].hFile = 0;
311                 }
312         dprintf_midi(stddeb, "MIDI_mciOpen // hFile=%u\n", MCIMidiDev[wDevID].hFile);
313         memcpy(&MCIMidiDev[wDevID].openParms, lpParms, sizeof(MCI_OPEN_PARMS));
314         MCIMidiDev[wDevID].wNotifyDeviceID = lpParms->wDeviceID;
315         MCIMidiDev[wDevID].dwStatus = MCI_MODE_STOP;
316         MCIMidiDev[wDevID].dwBeginData = 0;
317         MCIMidiDev[wDevID].dwTotalLen = 0;
318         MidiDesc.hMidi = 0;
319         if (MCIMidiDev[wDevID].hFile != 0) {
320                 MMCKINFO        ckMainRIFF;
321                 if (mmioDescend(MCIMidiDev[wDevID].hFile, &ckMainRIFF, NULL, 0) != 0) {
322                         return MCIERR_INTERNAL;
323                         }
324                 dprintf_midi(stddeb,"MIDI_mciOpen // ParentChunk ckid=%.4s fccType=%.4s cksize=%08lX \n",
325                                 (LPSTR)&ckMainRIFF.ckid, (LPSTR)&ckMainRIFF.fccType,
326                                 ckMainRIFF.cksize);
327                 dwOffset = 0;
328                 if (ckMainRIFF.ckid == mmioFOURCC('R', 'M', 'I', 'D')) {
329                         dprintf_midi(stddeb, "MIDI_mciOpen // is a 'RMID' file \n");
330                         dwOffset = ckMainRIFF.dwDataOffset;
331                         }
332                 if (ckMainRIFF.ckid != mmioFOURCC('M', 'T', 'h', 'd')) {
333                         dprintf_midi(stddeb, "MIDI_mciOpen // unknown format !\n");
334                         return MCIERR_INTERNAL;
335                         }
336                 if (MIDI_ReadMThd(wDevID, dwOffset) != 0) {
337                         dprintf_midi(stddeb, "MIDI_mciOpen // can't read 'MThd' header \n");
338                         return MCIERR_INTERNAL;
339                         }
340                 dwOffset = mmioSeek(MCIMidiDev[wDevID].hFile, 0, SEEK_CUR);
341                 if (MIDI_ReadMTrk(wDevID, dwOffset) != 0) {
342                         dprintf_midi(stddeb, "MIDI_mciOpen // can't read 'MTrk' header \n");
343                         return MCIERR_INTERNAL;
344                         }
345                 dwOffset = mmioSeek(MCIMidiDev[wDevID].hFile, 0, SEEK_CUR);
346                 MCIMidiDev[wDevID].dwBeginData = dwOffset;
347                 dprintf_midi(stddeb, "MIDI_mciOpen // Chunk Found ckid=%.4s fccType=%.4s cksize=%08lX \n",
348                                 (LPSTR)&ckMainRIFF.ckid, (LPSTR)&ckMainRIFF.fccType,
349                                 ckMainRIFF.cksize);
350                 }
351         dwRet = modMessage(wDevID, MODM_OPEN, 0, (DWORD)&MidiDesc, CALLBACK_NULL);
352 /*      dwRet = midMessage(wDevID, MIDM_OPEN, 0, (DWORD)&MidiDesc, CALLBACK_NULL); */
353         return 0;
354 #else
355         return MMSYSERR_NOTENABLED;
356 #endif
357 }
358
359
360 /**************************************************************************
361 *                               MIDI_mciStop                    [internal]
362 */
363 static DWORD MIDI_mciStop(UINT16 wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
364 {
365 #if defined(linux) || defined(__FreeBSD__)
366         dprintf_midi(stddeb, "MIDI_mciStop(%04X, %08lX, %p);\n", wDevID, dwFlags, lpParms);
367         if (lpParms == NULL) return MCIERR_INTERNAL;
368         MCIMidiDev[wDevID].dwStatus = MCI_MODE_STOP;
369         dprintf_midi(stddeb, "MIDI_mciStop // MCIMidiDev[wDevID].dwStatus=%p %d\n",
370                         &MCIMidiDev[wDevID].dwStatus, MCIMidiDev[wDevID].dwStatus);
371         return 0;
372 #else
373         return MCIERR_INTERNAL;
374 #endif
375 }
376
377
378 /**************************************************************************
379 *                               MIDI_mciClose           [internal]
380 */
381 static DWORD MIDI_mciClose(UINT16 wDevID, DWORD dwParam, LPMCI_GENERIC_PARMS lpParms)
382 {
383 #if defined(linux) || defined(__FreeBSD__)
384         DWORD           dwRet;
385
386         dprintf_midi(stddeb, "MIDI_mciClose(%04X, %08lX, %p);\n", wDevID, dwParam, lpParms);
387         if (MCIMidiDev[wDevID].dwStatus != MCI_MODE_STOP) {
388                 MIDI_mciStop(wDevID, MCI_WAIT, lpParms);
389                 }
390         MCIMidiDev[wDevID].dwStatus = MCI_MODE_STOP;
391         MCIMidiDev[wDevID].nUseCount--;
392         if (MCIMidiDev[wDevID].nUseCount == 0) {
393                 if (MCIMidiDev[wDevID].hFile != 0) {
394                         mmioClose(MCIMidiDev[wDevID].hFile, 0);
395                         MCIMidiDev[wDevID].hFile = 0;
396                         dprintf_midi(stddeb, "MIDI_mciClose // hFile closed !\n");
397                         }
398                 USER_HEAP_FREE(MCIMidiDev[wDevID].hMidiHdr);
399                 dwRet = modMessage(wDevID, MODM_CLOSE, 0, 0L, 0L);
400                 if (dwRet != MMSYSERR_NOERROR) return MCIERR_INTERNAL;
401 /*
402                 dwRet = midMessage(wDevID, MIDM_CLOSE, 0, 0L, 0L);
403                 if (dwRet != MMSYSERR_NOERROR) return MCIERR_INTERNAL;
404 */
405                 }
406         return 0;
407 #else
408         return 0;
409 #endif
410 }
411
412
413 /**************************************************************************
414 *                               MIDI_mciPlay            [internal]
415 */
416 static DWORD MIDI_mciPlay(UINT16 wDevID, DWORD dwFlags, LPMCI_PLAY_PARMS lpParms)
417 {
418 #if defined(linux) || defined(__FreeBSD__)
419         int                     count;
420         int                     start, end;
421         LPMIDIHDR       lpMidiHdr;
422         DWORD           lp16MidiHdr;
423         DWORD           dwData;
424         LPWORD          ptr;
425         DWORD           dwRet;
426
427         dprintf_midi(stddeb, "MIDI_mciPlay(%04X, %08lX, %p);\n", wDevID, dwFlags, lpParms);
428         if (MCIMidiDev[wDevID].hFile == 0) {
429                 dprintf_midi(stddeb, "MIDI_mciPlay // can't find file='%08lx' !\n", 
430                                 MCIMidiDev[wDevID].openParms.lpstrElementName);
431                 return MCIERR_FILE_NOT_FOUND;
432                 }
433         start = 1;              end = 99999;
434         if (dwFlags & MCI_FROM) {
435                 start = lpParms->dwFrom; 
436                 dprintf_midi(stddeb, "MIDI_mciPlay // MCI_FROM=%d \n", start);
437                 }
438         if (dwFlags & MCI_TO) {
439                 end = lpParms->dwTo;
440                 dprintf_midi(stddeb, "MIDI_mciPlay // MCI_TO=%d \n", end);
441                 }
442 #if 0
443         if (dwFlags & MCI_NOTIFY) {
444                 dprintf_midi(stddeb, "MIDI_mciPlay // MCI_NOTIFY %08lX !\n", lpParms->dwCallback);
445                 switch(fork()) {
446                         case -1:
447                                 dprintf_midi(stddeb, "MIDI_mciPlay // Can't 'fork' process !\n");
448                                 break;
449                         case 0:
450                                 dprintf_midi(stddeb, "MIDI_mciPlay // process started ! play in background ...\n");
451                                 break;
452                         default:
453                                 dprintf_midi(stddeb, "MIDI_mciPlay // process started ! return to caller...\n");
454                                 return 0;
455                         }
456                 }
457 #endif
458
459         lpMidiHdr = USER_HEAP_LIN_ADDR(MCIMidiDev[wDevID].hMidiHdr);
460         lp16MidiHdr = USER_HEAP_SEG_ADDR(MCIMidiDev[wDevID].hMidiHdr);
461
462         lpMidiHdr->lpData = (LPSTR) malloc(1200);
463         if (lpMidiHdr->lpData == NULL) return MCIERR_INTERNAL;
464         lpMidiHdr->dwBufferLength = 1024;
465         lpMidiHdr->dwUser = 0L;
466         lpMidiHdr->dwFlags = 0L;
467         dwRet = modMessage(wDevID, MODM_PREPARE, 0, (DWORD)lp16MidiHdr, sizeof(MIDIHDR));
468
469 /*      dprintf_midi(stddeb, "MIDI_mciPlay // after MODM_PREPARE \n"); */
470
471         MCIMidiDev[wDevID].dwStatus = MCI_MODE_PLAY;
472         while(MCIMidiDev[wDevID].dwStatus != MCI_MODE_STOP) {
473                 dprintf_midi(stddeb, "MIDI_mciPlay // MCIMidiDev[wDevID].dwStatus=%p %d\n",
474                         &MCIMidiDev[wDevID].dwStatus, MCIMidiDev[wDevID].dwStatus);
475
476                 ptr = (LPWORD)lpMidiHdr->lpData;
477                 for (count = 0; count < lpMidiHdr->dwBufferLength; count++) {
478                         if (MIDI_ReadVaryLen(wDevID, &dwData) != 0) break;
479                         *ptr = LOWORD(dwData);
480                         }
481 /*
482                 count = mmioRead(MCIMidiDev[wDevID].hFile, lpMidiHdr->lpData, lpMidiHdr->dwBufferLength);
483 */
484                 dprintf_midi(stddeb, "MIDI_mciPlay // after read count = %d\n",count);
485
486                 if (count < 1) break;
487                 lpMidiHdr->dwBytesRecorded = count;
488                 dprintf_midi(stddeb, "MIDI_mciPlay // before MODM_LONGDATA lpMidiHdr=%p dwBytesRecorded=%lu\n",
489                                         lpMidiHdr, lpMidiHdr->dwBytesRecorded);
490                 dwRet = modMessage(wDevID, MODM_LONGDATA, 0, (DWORD)lp16MidiHdr, sizeof(MIDIHDR));
491                 if (dwRet != MMSYSERR_NOERROR) {
492                   switch (dwRet) {
493                   case MMSYSERR_NOTENABLED:
494                     return MCIERR_DEVICE_NOT_READY;
495                     
496                   case MIDIERR_NODEVICE:
497                     return MCIERR_INVALID_DEVICE_ID;
498
499                   case MIDIERR_UNPREPARED:
500                     return MCIERR_DRIVER_INTERNAL;
501
502                   case MIDIERR_STILLPLAYING:
503                     return MCIERR_SEQ_PORT_INUSE;
504
505                   case MMSYSERR_INVALPARAM:
506                     return MCIERR_CANNOT_LOAD_DRIVER;
507                   
508                   default:
509                     return MCIERR_DRIVER;
510                   }     
511                 }
512               }
513         dwRet = modMessage(wDevID, MODM_UNPREPARE, 0, (DWORD)lp16MidiHdr, sizeof(MIDIHDR));
514         if (lpMidiHdr->lpData != NULL) {
515                 free(lpMidiHdr->lpData);
516                 lpMidiHdr->lpData = NULL;
517               }
518         MCIMidiDev[wDevID].dwStatus = MCI_MODE_STOP;
519         if (dwFlags & MCI_NOTIFY) {
520                 dprintf_midi(stddeb, "MIDI_mciPlay // MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
521                 mciDriverNotify((HWND16)LOWORD(lpParms->dwCallback), 
522                         MCIMidiDev[wDevID].wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
523 #if 0
524                 exit(1);
525 #endif
526               }
527         return 0;
528 #else
529         return MMSYSERR_NOTENABLED;
530 #endif
531       }
532
533
534 /**************************************************************************
535 *                               MIDI_mciRecord                  [internal]
536 */
537 static DWORD MIDI_mciRecord(UINT16 wDevID, DWORD dwFlags, LPMCI_RECORD_PARMS lpParms)
538 {
539 #if defined(linux) || defined(__FreeBSD__)
540         int                     start, end;
541         LPMIDIHDR       lpMidiHdr;
542         DWORD           dwRet;
543
544         dprintf_midi(stddeb, "MIDI_mciRecord(%04X, %08lX, %p);\n", wDevID, dwFlags, lpParms);
545         if (MCIMidiDev[wDevID].hFile == 0) {
546                 dprintf_midi(stddeb, "MIDI_mciRecord // can't find file='%08lx' !\n", 
547                                 MCIMidiDev[wDevID].openParms.lpstrElementName);
548                 return MCIERR_FILE_NOT_FOUND;
549                 }
550         start = 1;              end = 99999;
551         if (dwFlags & MCI_FROM) {
552                 start = lpParms->dwFrom; 
553                 dprintf_midi(stddeb, "MIDI_mciRecord // MCI_FROM=%d \n", start);
554                 }
555         if (dwFlags & MCI_TO) {
556                 end = lpParms->dwTo;
557                 dprintf_midi(stddeb, "MIDI_mciRecord // MCI_TO=%d \n", end);
558                 }
559         lpMidiHdr = USER_HEAP_LIN_ADDR(MCIMidiDev[wDevID].hMidiHdr);
560         lpMidiHdr->lpData = (LPSTR) xmalloc(1200);
561         lpMidiHdr->dwBufferLength = 1024;
562         lpMidiHdr->dwUser = 0L;
563         lpMidiHdr->dwFlags = 0L;
564         dwRet = midMessage(wDevID, MIDM_PREPARE, 0, (DWORD)lpMidiHdr, sizeof(MIDIHDR));
565         dprintf_midi(stddeb, "MIDI_mciRecord // after MIDM_PREPARE \n");
566         MCIMidiDev[wDevID].dwStatus = MCI_MODE_RECORD;
567         while(MCIMidiDev[wDevID].dwStatus != MCI_MODE_STOP) {
568                 dprintf_midi(stddeb, "MIDI_mciRecord // MCIMidiDev[wDevID].dwStatus=%p %d\n",
569                         &MCIMidiDev[wDevID].dwStatus, MCIMidiDev[wDevID].dwStatus);
570                 lpMidiHdr->dwBytesRecorded = 0;
571                 dwRet = midMessage(wDevID, MIDM_START, 0, 0L, 0L);
572                 dprintf_midi(stddeb, "MIDI_mciRecord // after MIDM_START lpMidiHdr=%p dwBytesRecorded=%lu\n",
573                                         lpMidiHdr, lpMidiHdr->dwBytesRecorded);
574                 if (lpMidiHdr->dwBytesRecorded == 0) break;
575                 }
576         dprintf_midi(stddeb, "MIDI_mciRecord // before MIDM_UNPREPARE \n");
577         dwRet = midMessage(wDevID, MIDM_UNPREPARE, 0, (DWORD)lpMidiHdr, sizeof(MIDIHDR));
578         dprintf_midi(stddeb, "MIDI_mciRecord // after MIDM_UNPREPARE \n");
579         if (lpMidiHdr->lpData != NULL) {
580                 free(lpMidiHdr->lpData);
581                 lpMidiHdr->lpData = NULL;
582                 }
583         MCIMidiDev[wDevID].dwStatus = MCI_MODE_STOP;
584         if (dwFlags & MCI_NOTIFY) {
585                 dprintf_midi(stddeb, "MIDI_mciRecord // MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
586                 mciDriverNotify((HWND16)LOWORD(lpParms->dwCallback), 
587                         MCIMidiDev[wDevID].wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
588                 }
589         return 0;
590 #else
591         return MMSYSERR_NOTENABLED;
592 #endif
593 }
594
595
596 /**************************************************************************
597 *                               MIDI_mciPause                   [internal]
598 */
599 static DWORD MIDI_mciPause(UINT16 wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
600 {
601 #if defined(linux) || defined(__FreeBSD__)
602         dprintf_midi(stddeb, "MIDI_mciPause(%04X, %08lX, %p);\n", wDevID, dwFlags, lpParms);
603         if (lpParms == NULL) return MCIERR_INTERNAL;
604         return 0;
605 #else
606         return MCIERR_INTERNAL;
607 #endif
608 }
609
610
611 /**************************************************************************
612 *                               MIDI_mciResume                  [internal]
613 */
614 static DWORD MIDI_mciResume(UINT16 wDevID, DWORD dwFlags, LPMCI_GENERIC_PARMS lpParms)
615 {
616 #if defined(linux) || defined(__FreeBSD__)
617         dprintf_midi(stddeb, "MIDI_mciResume(%04X, %08lX, %p);\n", wDevID, dwFlags, lpParms);
618         if (lpParms == NULL) return MCIERR_INTERNAL;
619         return 0;
620 #else
621         return MCIERR_INTERNAL;
622 #endif
623 }
624
625
626 /**************************************************************************
627 *                               MIDI_mciSet                     [internal]
628 */
629 static DWORD MIDI_mciSet(UINT16 wDevID, DWORD dwFlags, LPMCI_SET_PARMS lpParms)
630 {
631 #if defined(linux) || defined(__FreeBSD__)
632         dprintf_midi(stddeb, "MIDI_mciSet(%04X, %08lX, %p);\n", wDevID, dwFlags, lpParms);
633         if (lpParms == NULL) return MCIERR_INTERNAL;
634         dprintf_midi(stddeb, "MIDI_mciSet // dwTimeFormat=%08lX\n", lpParms->dwTimeFormat);
635         dprintf_midi(stddeb, "MIDI_mciSet // dwAudio=%08lX\n", lpParms->dwAudio);
636         if (dwFlags & MCI_SET_TIME_FORMAT) {
637                 switch (lpParms->dwTimeFormat) {
638                         case MCI_FORMAT_MILLISECONDS:
639                                 dprintf_midi(stddeb, "MIDI_mciSet // MCI_FORMAT_MILLISECONDS !\n");
640                                 break;
641                         case MCI_FORMAT_BYTES:
642                                 dprintf_midi(stddeb, "MIDI_mciSet // MCI_FORMAT_BYTES !\n");
643                                 break;
644                         case MCI_FORMAT_SAMPLES:
645                                 dprintf_midi(stddeb, "MIDI_mciSet // MCI_FORMAT_SAMPLES !\n");
646                                 break;
647                         default:
648                                 dprintf_midi(stddeb, "MIDI_mciSet // bad time format !\n");
649                                 return MCIERR_BAD_TIME_FORMAT;
650                         }
651                 }
652         if (dwFlags & MCI_SET_VIDEO) return MCIERR_UNSUPPORTED_FUNCTION;
653         if (dwFlags & MCI_SET_DOOR_OPEN) return MCIERR_UNSUPPORTED_FUNCTION;
654         if (dwFlags & MCI_SET_DOOR_CLOSED) return MCIERR_UNSUPPORTED_FUNCTION;
655         if (dwFlags & MCI_SET_AUDIO) {
656                 dprintf_midi(stddeb, "MIDI_mciSet // MCI_SET_AUDIO !\n");
657                 }
658         if (dwFlags && MCI_SET_ON) {
659                 dprintf_midi(stddeb, "MIDI_mciSet // MCI_SET_ON !\n");
660                 if (dwFlags && MCI_SET_AUDIO_LEFT) {
661                         dprintf_midi(stddeb, "MIDI_mciSet // MCI_SET_AUDIO_LEFT !\n");
662                         }
663                 if (dwFlags && MCI_SET_AUDIO_RIGHT) {
664                         dprintf_midi(stddeb, "MIDI_mciSet // MCI_SET_AUDIO_RIGHT !\n");
665                         }
666                 }
667         if (dwFlags & MCI_SET_OFF) {
668                 dprintf_midi(stddeb, "MIDI_mciSet // MCI_SET_OFF !\n");
669                 }
670         if (dwFlags & MCI_SEQ_SET_MASTER) {
671                 dprintf_midi(stddeb, "MIDI_mciSet // MCI_SEQ_SET_MASTER !\n");
672                 }
673         if (dwFlags & MCI_SEQ_SET_SLAVE) {
674                 dprintf_midi(stddeb, "MIDI_mciSet // MCI_SEQ_SET_SLAVE !\n");
675                 }
676         if (dwFlags & MCI_SEQ_SET_OFFSET) {
677                 dprintf_midi(stddeb, "MIDI_mciSet // MCI_SEQ_SET_OFFSET !\n");
678                 }
679         if (dwFlags & MCI_SEQ_SET_PORT) {
680                 dprintf_midi(stddeb, "MIDI_mciSet // MCI_SEQ_SET_PORT !\n");
681                 }
682         if (dwFlags & MCI_SEQ_SET_TEMPO) {
683                 dprintf_midi(stddeb, "MIDI_mciSet // MCI_SEQ_SET_TEMPO !\n");
684                 }
685         return 0;
686 #else
687         return MCIERR_INTERNAL;
688 #endif
689 }
690
691
692 /**************************************************************************
693 *                               MIDI_mciStatus          [internal]
694 */
695 static DWORD MIDI_mciStatus(UINT16 wDevID, DWORD dwFlags, LPMCI_STATUS_PARMS lpParms)
696 {
697 #if defined(linux) || defined(__FreeBSD__)
698         dprintf_midi(stddeb, "MIDI_mciStatus(%04X, %08lX, %p);\n", wDevID, dwFlags, lpParms);
699         if (lpParms == NULL) return MCIERR_INTERNAL;
700         if (dwFlags & MCI_STATUS_ITEM) {
701                 switch(lpParms->dwItem) {
702                         case MCI_STATUS_CURRENT_TRACK:
703                                 lpParms->dwReturn = 1;
704                                 break;
705                         case MCI_STATUS_LENGTH:
706                                 lpParms->dwReturn = 5555;
707                                 if (dwFlags & MCI_TRACK) {
708                                         lpParms->dwTrack = 1;
709                                         lpParms->dwReturn = 2222;
710                                         }
711                                 break;
712                         case MCI_STATUS_MODE:
713                                 lpParms->dwReturn = MCI_MODE_STOP;
714                                 break;
715                         case MCI_STATUS_MEDIA_PRESENT:
716                                 dprintf_midi(stddeb, "MIDI_mciStatus // MCI_STATUS_MEDIA_PRESENT !\n");
717                                 lpParms->dwReturn = TRUE;
718                                 break;
719                         case MCI_STATUS_NUMBER_OF_TRACKS:
720                                 lpParms->dwReturn = 1;
721                                 break;
722                         case MCI_STATUS_POSITION:
723                                 lpParms->dwReturn = 3333;
724                                 if (dwFlags & MCI_STATUS_START) {
725                                         lpParms->dwItem = 1;
726                                         }
727                                 if (dwFlags & MCI_TRACK) {
728                                         lpParms->dwTrack = 1;
729                                         lpParms->dwReturn = 777;
730                                         }
731                                 break;
732                         case MCI_STATUS_READY:
733                                 dprintf_midi(stddeb, "MIDI_mciStatus // MCI_STATUS_READY !\n");
734                                 lpParms->dwReturn = TRUE;
735                                 break;
736                         case MCI_STATUS_TIME_FORMAT:
737                                 dprintf_midi(stddeb, "MIDI_mciStatus // MCI_STATUS_TIME_FORMAT !\n");
738                                 lpParms->dwReturn = MCI_FORMAT_MILLISECONDS;
739                                 break;
740                         case MCI_SEQ_STATUS_DIVTYPE:
741                                 dprintf_midi(stddeb, "MIDI_mciStatus // MCI_SEQ_STATUS_DIVTYPE !\n");
742                                 lpParms->dwReturn = 0;
743                                 break;
744                         case MCI_SEQ_STATUS_MASTER:
745                                 dprintf_midi(stddeb, "MIDI_mciStatus // MCI_SEQ_STATUS_MASTER !\n");
746                                 lpParms->dwReturn = 0;
747                                 break;
748                         case MCI_SEQ_STATUS_SLAVE:
749                                 dprintf_midi(stddeb, "MIDI_mciStatus // MCI_SEQ_STATUS_SLAVE !\n");
750                                 lpParms->dwReturn = 0;
751                                 break;
752                         case MCI_SEQ_STATUS_OFFSET:
753                                 dprintf_midi(stddeb, "MIDI_mciStatus // MCI_SEQ_STATUS_OFFSET !\n");
754                                 lpParms->dwReturn = 0;
755                                 break;
756                         case MCI_SEQ_STATUS_PORT:
757                                 dprintf_midi(stddeb, "MIDI_mciStatus // MCI_SEQ_STATUS_PORT !\n");
758                                 lpParms->dwReturn = 0;
759                                 break;
760                         case MCI_SEQ_STATUS_TEMPO:
761                                 dprintf_midi(stddeb, "MIDI_mciStatus // MCI_SEQ_STATUS_TEMPO !\n");
762                                 lpParms->dwReturn = 0;
763                                 break;
764                         default:
765                                 dprintf_midi(stddeb, "MIDI_mciStatus // unknowm command %08lX !\n", lpParms->dwItem);
766                                 return MCIERR_UNRECOGNIZED_COMMAND;
767                         }
768                 }
769         if (dwFlags & MCI_NOTIFY) {
770                 dprintf_midi(stddeb, "MIDI_mciStatus // MCI_NOTIFY_SUCCESSFUL %08lX !\n", lpParms->dwCallback);
771                 mciDriverNotify((HWND16)LOWORD(lpParms->dwCallback), 
772                         MCIMidiDev[wDevID].wNotifyDeviceID, MCI_NOTIFY_SUCCESSFUL);
773                 }
774         return 0;
775 #else
776         return MCIERR_INTERNAL;
777 #endif
778 }
779
780 /**************************************************************************
781 *                               MIDI_mciGetDevCaps              [internal]
782 */
783 static DWORD MIDI_mciGetDevCaps(UINT16 wDevID, DWORD dwFlags, 
784                                         LPMCI_GETDEVCAPS_PARMS lpParms)
785 {
786 #if defined(linux) || defined(__FreeBSD__)
787         dprintf_midi(stddeb, "MIDI_mciGetDevCaps(%04X, %08lX, %p);\n", wDevID, dwFlags, lpParms);
788         if (lpParms == NULL) return MCIERR_INTERNAL;
789         if (dwFlags & MCI_GETDEVCAPS_ITEM) {
790                 switch(lpParms->dwItem) {
791                         case MCI_GETDEVCAPS_CAN_RECORD:
792                                 lpParms->dwReturn = TRUE;
793                                 break;
794                         case MCI_GETDEVCAPS_HAS_AUDIO:
795                                 lpParms->dwReturn = TRUE;
796                                 break;
797                         case MCI_GETDEVCAPS_HAS_VIDEO:
798                                 lpParms->dwReturn = FALSE;
799                                 break;
800                         case MCI_GETDEVCAPS_DEVICE_TYPE:
801                                 lpParms->dwReturn = MCI_DEVTYPE_SEQUENCER;
802                                 break;
803                         case MCI_GETDEVCAPS_USES_FILES:
804                                 lpParms->dwReturn = TRUE;
805                                 break;
806                         case MCI_GETDEVCAPS_COMPOUND_DEVICE:
807                                 lpParms->dwReturn = TRUE;
808                                 break;
809                         case MCI_GETDEVCAPS_CAN_EJECT:
810                                 lpParms->dwReturn = FALSE;
811                                 break;
812                         case MCI_GETDEVCAPS_CAN_PLAY:
813                                 lpParms->dwReturn = TRUE;
814                                 break;
815                         case MCI_GETDEVCAPS_CAN_SAVE:
816                                 lpParms->dwReturn = FALSE;
817                                 break;
818                         default:
819                                 return MCIERR_UNRECOGNIZED_COMMAND;
820                         }
821                 }
822         return 0;
823 #else
824         return MCIERR_INTERNAL;
825 #endif
826 }
827
828 /**************************************************************************
829 *                               MIDI_mciInfo                    [internal]
830 */
831 static DWORD MIDI_mciInfo(UINT16 wDevID, DWORD dwFlags, LPMCI_INFO_PARMS lpParms)
832 {
833 #if defined(linux) || defined(__FreeBSD__)
834         dprintf_midi(stddeb, "MIDI_mciInfo(%04X, %08lX, %p);\n", wDevID, dwFlags, lpParms);
835         if (lpParms == NULL) return MCIERR_INTERNAL;
836         lpParms->lpstrReturn = NULL;
837         switch(dwFlags) {
838                 case MCI_INFO_PRODUCT:
839                         lpParms->lpstrReturn = "Linux Sound System 0.5";
840                         break;
841                 case MCI_INFO_FILE:
842                         lpParms->lpstrReturn = "FileName";
843                         break;
844                 default:
845                         return MCIERR_UNRECOGNIZED_COMMAND;
846                 }
847         if (lpParms->lpstrReturn != NULL)
848                 lpParms->dwRetSize = strlen(lpParms->lpstrReturn);
849         else
850                 lpParms->dwRetSize = 0;
851         return 0;
852 #else
853         return MCIERR_INTERNAL;
854 #endif
855 }
856
857
858 /*-----------------------------------------------------------------------*/
859
860
861 /**************************************************************************
862 *                               midGetDevCaps                   [internal]
863 */
864 static DWORD midGetDevCaps(WORD wDevID, LPMIDIINCAPS lpCaps, DWORD dwSize)
865 {
866         dprintf_midi(stddeb, "midGetDevCaps(%04X, %p, %08lX);\n", wDevID, lpCaps, dwSize);
867         return MMSYSERR_NOTENABLED;
868 }
869
870 /**************************************************************************
871 *                               midOpen                                 [internal]
872 */
873 static DWORD midOpen(WORD wDevID, LPMIDIOPENDESC lpDesc, DWORD dwFlags)
874 {
875 #if defined(linux) || defined(__FreeBSD__)
876         int             midi;
877         dprintf_midi(stddeb,
878                 "midOpen(%04X, %p, %08lX);\n", wDevID, lpDesc, dwFlags);
879         if (lpDesc == NULL) {
880                 dprintf_midi(stddeb,"Linux 'midOpen' // Invalid Parameter !\n");
881                 return MMSYSERR_INVALPARAM;
882                 }
883         if (wDevID >= MAX_MIDIINDRV) {
884                 dprintf_midi(stddeb,"Linux 'midOpen' // MAX_MIDIINDRV reached !\n");
885                 return MMSYSERR_ALLOCATED;
886                 }
887         MidiInDev[wDevID].unixdev = 0;
888         midi = open (MIDI_DEV, O_RDONLY, 0);
889         if (midi == -1) {
890                 dprintf_midi(stddeb,"Linux 'midOpen' // can't open !\n");
891                 return MMSYSERR_NOTENABLED;
892                 }
893         MidiInDev[wDevID].wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
894         switch(MidiInDev[wDevID].wFlags) {
895                 case DCB_NULL:
896                         dprintf_midi(stddeb,"Linux 'midOpen' // CALLBACK_NULL !\n");
897                         break;
898                 case DCB_WINDOW:
899                         dprintf_midi(stddeb,
900                                 "Linux 'midOpen' // CALLBACK_WINDOW !\n");
901                         break;
902                 case DCB_TASK:
903                         dprintf_midi(stddeb,
904                                    "Linux 'midOpen' // CALLBACK_TASK !\n");
905                         break;
906                 case DCB_FUNCTION:
907                         dprintf_midi(stddeb,
908                                    "Linux 'midOpen' // CALLBACK_FUNCTION !\n");
909                         break;
910                 }
911         MidiInDev[wDevID].lpQueueHdr = NULL;
912         MidiInDev[wDevID].unixdev = midi;
913         MidiInDev[wDevID].dwTotalPlayed = 0;
914         MidiInDev[wDevID].bufsize = 0x3FFF;
915         if (MIDI_NotifyClient(wDevID, MIM_OPEN, 0L, 0L) != MMSYSERR_NOERROR) {
916                 dprintf_midi(stddeb,"Linux 'midOpen' // can't notify client !\n");
917                 return MMSYSERR_INVALPARAM;
918                 }
919         return MMSYSERR_NOERROR;
920 #else
921         return MMSYSERR_NOTENABLED;
922 #endif
923 }
924
925 /**************************************************************************
926 *                               midClose                                [internal]
927 */
928 static DWORD midClose(WORD wDevID)
929 {
930 #if defined(linux) || defined(__FreeBSD__)
931         dprintf_midi(stddeb, "midClose(%04X);\n", wDevID);
932         if (MidiInDev[wDevID].unixdev == 0) {
933                 dprintf_midi(stddeb,"Linux 'midClose' // can't close !\n");
934                 return MMSYSERR_NOTENABLED;
935                 }
936         close(MidiInDev[wDevID].unixdev);
937         MidiInDev[wDevID].unixdev = 0;
938         MidiInDev[wDevID].bufsize = 0;
939         if (MIDI_NotifyClient(wDevID, MIM_CLOSE, 0L, 0L) != MMSYSERR_NOERROR) {
940                 dprintf_midi(stddeb,"Linux 'midClose' // can't notify client !\n");
941                 return MMSYSERR_INVALPARAM;
942                 }
943         return MMSYSERR_NOERROR;
944 #else
945         return MMSYSERR_NOTENABLED;
946 #endif
947 }
948
949 /**************************************************************************
950 *                               midAddBuffer            [internal]
951 */
952 static DWORD midAddBuffer(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
953 {
954         dprintf_midi(stddeb, "midAddBuffer(%04X, %p, %08lX);\n", wDevID, lpMidiHdr, dwSize);
955         return MMSYSERR_NOTENABLED;
956 }
957
958 /**************************************************************************
959 *                               midPrepare                      [internal]
960 */
961 static DWORD midPrepare(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
962 {
963         dprintf_midi(stddeb, "midPrepare(%04X, %p, %08lX);\n", wDevID, lpMidiHdr, dwSize);
964         return MMSYSERR_NOTENABLED;
965 }
966
967 /**************************************************************************
968 *                               midUnprepare                    [internal]
969 */
970 static DWORD midUnprepare(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
971 {
972         dprintf_midi(stddeb, "midUnprepare(%04X, %p, %08lX);\n", wDevID, lpMidiHdr, dwSize);
973         return MMSYSERR_NOTENABLED;
974 }
975
976 /**************************************************************************
977 *                               midReset                                [internal]
978 */
979 static DWORD midReset(WORD wDevID)
980 {
981         dprintf_midi(stddeb, "midReset(%04X);\n", wDevID);
982         return MMSYSERR_NOTENABLED;
983 }
984
985
986 /**************************************************************************
987 *                               midStart                                [internal]
988 */
989 static DWORD midStart(WORD wDevID)
990 {
991         dprintf_midi(stddeb, "midStart(%04X);\n", wDevID);
992         return MMSYSERR_NOTENABLED;
993 }
994
995
996 /**************************************************************************
997 *                               midStop                                 [internal]
998 */
999 static DWORD midStop(WORD wDevID)
1000 {
1001         dprintf_midi(stddeb, "midStop(%04X);\n", wDevID);
1002         return MMSYSERR_NOTENABLED;
1003 }
1004
1005
1006 /**************************************************************************
1007 *                               midMessage                              [sample driver]
1008 */
1009 DWORD midMessage(WORD wDevID, WORD wMsg, DWORD dwUser, 
1010                                         DWORD dwParam1, DWORD dwParam2)
1011 {
1012         dprintf_midi(stddeb, "midMessage(%04X, %04X, %08lX, %08lX, %08lX);\n", 
1013                         wDevID, wMsg, dwUser, dwParam1, dwParam2);
1014         switch(wMsg) {
1015                 case MIDM_OPEN:
1016                         return midOpen(wDevID, (LPMIDIOPENDESC)PTR_SEG_TO_LIN(dwParam1), dwParam2);
1017                 case MIDM_CLOSE:
1018                         return midClose(wDevID);
1019                 case MIDM_ADDBUFFER:
1020                         return midAddBuffer(wDevID, (LPMIDIHDR)PTR_SEG_TO_LIN(dwParam1), dwParam2);
1021                 case MIDM_PREPARE:
1022                         return midPrepare(wDevID, (LPMIDIHDR)PTR_SEG_TO_LIN(dwParam1), dwParam2);
1023                 case MIDM_UNPREPARE:
1024                         return midUnprepare(wDevID, (LPMIDIHDR)PTR_SEG_TO_LIN(dwParam1), dwParam2);
1025                 case MIDM_GETDEVCAPS:
1026                         return midGetDevCaps(wDevID, (LPMIDIINCAPS)PTR_SEG_TO_LIN(dwParam1), dwParam2);
1027                 case MIDM_GETNUMDEVS:
1028                         return 0;
1029                 case MIDM_RESET:
1030                         return midReset(wDevID);
1031                 case MIDM_START:
1032                         return midStart(wDevID);
1033                 case MIDM_STOP:
1034                         return midStop(wDevID);
1035                 }
1036         return MMSYSERR_NOTSUPPORTED;
1037 }
1038
1039
1040
1041 /*-----------------------------------------------------------------------*/
1042
1043
1044 /**************************************************************************
1045 *                               modGetDevCaps                   [internal]
1046 */
1047 static DWORD modGetDevCaps(WORD wDevID, LPMIDIOUTCAPS lpCaps, DWORD dwSize)
1048 {
1049         dprintf_midi(stddeb, "modGetDevCaps(%04X, %p, %08lX);\n", wDevID, lpCaps, dwSize);
1050         return MMSYSERR_NOTENABLED;
1051 }
1052
1053
1054 /**************************************************************************
1055 *                               modOpen                                 [internal]
1056 */
1057 static DWORD modOpen(WORD wDevID, LPMIDIOPENDESC lpDesc, DWORD dwFlags)
1058 {
1059 #if defined(linux) || defined(__FreeBSD__)
1060         int             midi;
1061
1062         dprintf_midi(stddeb,
1063                 "modOpen(%04X, %p, %08lX);\n", wDevID, lpDesc, dwFlags);
1064         if (lpDesc == NULL) {
1065                 dprintf_midi(stddeb,"Linux 'modOpen' // Invalid Parameter !\n");
1066                 return MMSYSERR_INVALPARAM;
1067                 }
1068         if (wDevID>= MAX_MIDIOUTDRV) {
1069                 dprintf_midi(stddeb,"Linux 'modOpen' // MAX_MIDIOUTDRV reached !\n");
1070                 return MMSYSERR_ALLOCATED;
1071                 }
1072         MidiOutDev[wDevID].unixdev = 0;
1073         midi = open (MIDI_DEV, O_WRONLY, 0);
1074         if (midi == -1) {
1075                 dprintf_midi(stddeb,"Linux 'modOpen' // can't open !\n");
1076                 return MMSYSERR_NOTENABLED;
1077                 }
1078         MidiOutDev[wDevID].wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
1079         switch(MidiOutDev[wDevID].wFlags) {
1080                 case DCB_NULL:
1081                         dprintf_midi(stddeb,"Linux 'modOpen' // CALLBACK_NULL !\n");
1082                         break;
1083                 case DCB_WINDOW:
1084                         dprintf_midi(stddeb,
1085                                 "Linux 'modOpen' // CALLBACK_WINDOW !\n");
1086                         break;
1087                 case DCB_TASK:
1088                         dprintf_midi(stddeb,
1089                                 "Linux 'modOpen' // CALLBACK_TASK !\n");
1090                         break;
1091                 case DCB_FUNCTION:
1092                         dprintf_midi(stddeb,
1093                                 "Linux 'modOpen' // CALLBACK_FUNCTION !\n");
1094                         break;
1095                 }
1096         MidiOutDev[wDevID].lpQueueHdr = NULL;
1097         MidiOutDev[wDevID].unixdev = midi;
1098         MidiOutDev[wDevID].dwTotalPlayed = 0;
1099         MidiOutDev[wDevID].bufsize = 0x3FFF;
1100         if (MIDI_NotifyClient(wDevID, MOM_OPEN, 0L, 0L) != MMSYSERR_NOERROR) {
1101                 dprintf_midi(stddeb,"Linux 'modOpen' // can't notify client !\n");
1102                 return MMSYSERR_INVALPARAM;
1103                 }
1104         dprintf_midi(stddeb,
1105                 "Linux 'modOpen' // Succesful unixdev=%d !\n", midi);
1106         return MMSYSERR_NOERROR;
1107 #else
1108         return MMSYSERR_NOTENABLED;
1109 #endif
1110 }
1111
1112
1113 /**************************************************************************
1114 *                               modClose                                [internal]
1115 */
1116 static DWORD modClose(WORD wDevID)
1117 {
1118 #if defined(linux) || defined(__FreeBSD__)
1119         dprintf_midi(stddeb, "modClose(%04X);\n", wDevID);
1120         if (MidiOutDev[wDevID].unixdev == 0) {
1121                 dprintf_midi(stddeb,"Linux 'modClose' // can't close !\n");
1122                 return MMSYSERR_NOTENABLED;
1123                 }
1124         close(MidiOutDev[wDevID].unixdev);
1125         MidiOutDev[wDevID].unixdev = 0;
1126         MidiOutDev[wDevID].bufsize = 0;
1127         if (MIDI_NotifyClient(wDevID, MOM_CLOSE, 0L, 0L) != MMSYSERR_NOERROR) {
1128                 dprintf_midi(stddeb,"Linux 'modClose' // can't notify client !\n");
1129                 return MMSYSERR_INVALPARAM;
1130                 }
1131         return MMSYSERR_NOERROR;
1132 #else
1133         return MMSYSERR_NOTENABLED;
1134 #endif
1135 }
1136
1137 /**************************************************************************
1138 *                               modData                                 [internal]
1139 */
1140 static DWORD modData(WORD wDevID, DWORD dwParam)
1141 {
1142 #if defined(linux) || defined(__FreeBSD__)
1143         WORD    event;
1144
1145         dprintf_midi(stddeb,    
1146                 "modData(%04X, %08lX);\n", wDevID, dwParam);
1147         if (MidiOutDev[wDevID].unixdev == 0) {
1148         dprintf_midi(stddeb,"Linux 'modData' // can't play !\n");
1149                 return MIDIERR_NODEVICE;
1150                 }
1151         event = LOWORD(dwParam);
1152         if (write (MidiOutDev[wDevID].unixdev, 
1153                 &event, sizeof(WORD)) != sizeof(WORD)) {
1154                 dprintf_midi(stddeb,
1155                         "modData() // error writting unixdev !\n");
1156                 }
1157         return MMSYSERR_NOTENABLED;
1158 #else
1159         return MMSYSERR_NOTENABLED;
1160 #endif
1161 }
1162
1163 /**************************************************************************
1164 *                               modLongData                                     [internal]
1165 */
1166 static DWORD modLongData(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
1167 {
1168 #if defined(linux) || defined(__FreeBSD__)
1169         int             count;
1170         LPWORD  ptr;
1171         int     en;
1172
1173         dprintf_midi(stddeb,    
1174                 "modLongData(%04X, %p, %08lX);\n", wDevID, lpMidiHdr, dwSize);
1175         if (MidiOutDev[wDevID].unixdev == 0) {
1176         dprintf_midi(stddeb,"Linux 'modLongData' // can't play !\n");
1177                 return MIDIERR_NODEVICE;
1178                 }
1179         if (lpMidiHdr->lpData == NULL) return MIDIERR_UNPREPARED;
1180         if (!(lpMidiHdr->dwFlags & MHDR_PREPARED)) return MIDIERR_UNPREPARED;
1181         if (lpMidiHdr->dwFlags & MHDR_INQUEUE) return MIDIERR_STILLPLAYING;
1182         lpMidiHdr->dwFlags &= ~MHDR_DONE;
1183         lpMidiHdr->dwFlags |= MHDR_INQUEUE;
1184         dprintf_midi(stddeb,
1185                 "modLongData() // dwBytesRecorded %lu !\n", lpMidiHdr->dwBytesRecorded);
1186         dprintf_midi(stddeb,
1187                 "                 %02X %02X %02X %02X\n",lpMidiHdr->lpData[0],
1188                                                          lpMidiHdr->lpData[1],
1189                                                          lpMidiHdr->lpData[2],
1190                                                          lpMidiHdr->lpData[3]);
1191 /*
1192         count = write (MidiOutDev[wDevID].unixdev, 
1193                 lpMidiHdr->lpData, lpMidiHdr->dwBytesRecorded);
1194 */
1195         ptr = (LPWORD)lpMidiHdr->lpData;
1196         for (count = 0; count < lpMidiHdr->dwBytesRecorded; count++) {
1197                 if (write (MidiOutDev[wDevID].unixdev, ptr, 
1198                         sizeof(WORD)) != sizeof(WORD)) break;
1199                 ptr++;
1200                 }
1201
1202         en = errno;
1203         dprintf_midi(stddeb, "Linux 'modLongData' // after write count = %d\n",count);
1204         if (count != lpMidiHdr->dwBytesRecorded) {
1205                 dprintf_midi(stddeb,
1206                         "modLongData() // error writting unixdev #%d ! (%d != %ld)\n",
1207                         MidiOutDev[wDevID].unixdev, count, lpMidiHdr->dwBytesRecorded);
1208                 dprintf_midi(stddeb,
1209                         "                 errno = %d error = %s\n",en,strerror(en));
1210                 return MMSYSERR_NOTENABLED;
1211                 }
1212         lpMidiHdr->dwFlags &= ~MHDR_INQUEUE;
1213         lpMidiHdr->dwFlags |= MHDR_DONE;
1214         if (MIDI_NotifyClient(wDevID, MOM_DONE, 0L, 0L) != MMSYSERR_NOERROR) {
1215                 dprintf_midi(stddeb,"Linux 'modLongData' // can't notify client !\n");
1216                 return MMSYSERR_INVALPARAM;
1217                 }
1218         return MMSYSERR_NOERROR;
1219 #else
1220         return MMSYSERR_NOTENABLED;
1221 #endif
1222 }
1223
1224 /**************************************************************************
1225 *                               modPrepare                              [internal]
1226 */
1227 static DWORD modPrepare(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
1228 {
1229 #if defined(linux) || defined(__FreeBSD__)
1230         dprintf_midi(stddeb,
1231                   "modPrepare(%04X, %p, %08lX);\n", wDevID, lpMidiHdr, dwSize);
1232         if (MidiOutDev[wDevID].unixdev == 0) {
1233                 dprintf_midi(stddeb,"Linux 'modPrepare' // can't prepare !\n");
1234                 return MMSYSERR_NOTENABLED;
1235                 }
1236         if (MidiOutDev[wDevID].lpQueueHdr != NULL) {
1237                 dprintf_midi(stddeb,"Linux 'modPrepare' // already prepare !\n");
1238                 return MMSYSERR_NOTENABLED;
1239                 }
1240         MidiOutDev[wDevID].dwTotalPlayed = 0;
1241         MidiOutDev[wDevID].lpQueueHdr = PTR_SEG_TO_LIN(lpMidiHdr);
1242         if (lpMidiHdr->dwFlags & MHDR_INQUEUE) return MIDIERR_STILLPLAYING;
1243         lpMidiHdr->dwFlags |= MHDR_PREPARED;
1244         lpMidiHdr->dwFlags &= ~MHDR_DONE;
1245         return MMSYSERR_NOERROR;
1246 #else
1247         return MMSYSERR_NOTENABLED;
1248 #endif
1249 }
1250
1251 /**************************************************************************
1252 *                               modUnprepare                    [internal]
1253 */
1254 static DWORD modUnprepare(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
1255 {
1256 #if defined(linux) || defined(__FreeBSD__)
1257         dprintf_midi(stddeb,
1258                 "modUnprepare(%04X, %p, %08lX);\n", wDevID, lpMidiHdr, dwSize);
1259         if (MidiOutDev[wDevID].unixdev == 0) {
1260                 dprintf_midi(stddeb,"Linux 'modUnprepare' // can't unprepare !\n");
1261                 return MMSYSERR_NOTENABLED;
1262                 }
1263         return MMSYSERR_NOERROR;
1264 #else
1265         return MMSYSERR_NOTENABLED;
1266 #endif
1267 }
1268
1269 /**************************************************************************
1270 *                               modReset                                [internal]
1271 */
1272 static DWORD modReset(WORD wDevID)
1273 {
1274         dprintf_midi(stddeb, "modReset(%04X);\n", wDevID);
1275         return MMSYSERR_NOTENABLED;
1276 }
1277
1278
1279 /**************************************************************************
1280 *                               modMessage                      [sample driver]
1281 */
1282 DWORD modMessage(WORD wDevID, WORD wMsg, DWORD dwUser, 
1283                                         DWORD dwParam1, DWORD dwParam2)
1284 {
1285         dprintf_midi(stddeb, "modMessage(%04X, %04X, %08lX, %08lX, %08lX);\n", 
1286                         wDevID, wMsg, dwUser, dwParam1, dwParam2);
1287         switch(wMsg) {
1288                 case MODM_OPEN:
1289                         return modOpen(wDevID, (LPMIDIOPENDESC)PTR_SEG_TO_LIN(dwParam1), dwParam2);
1290                 case MODM_CLOSE:
1291                         return modClose(wDevID);
1292                 case MODM_DATA:
1293                         return modData(wDevID, dwParam1);
1294                 case MODM_LONGDATA:
1295                         return modLongData(wDevID, (LPMIDIHDR)PTR_SEG_TO_LIN(dwParam1), dwParam2);
1296                 case MODM_PREPARE:
1297                         return modPrepare(wDevID, (LPMIDIHDR)PTR_SEG_TO_LIN(dwParam1), dwParam2);
1298                 case MODM_UNPREPARE:
1299                         return modUnprepare(wDevID, (LPMIDIHDR)PTR_SEG_TO_LIN(dwParam1), dwParam2);
1300                 case MODM_GETDEVCAPS:
1301                         return modGetDevCaps(wDevID, (LPMIDIOUTCAPS)PTR_SEG_TO_LIN(dwParam1), dwParam2);
1302                 case MODM_GETNUMDEVS:
1303                         return 1;
1304                 case MODM_GETVOLUME:
1305                         return 0;
1306                 case MODM_SETVOLUME:
1307                         return 0;
1308                 case MODM_RESET:
1309                         return modReset(wDevID);
1310                 }
1311         return MMSYSERR_NOTSUPPORTED;
1312 }
1313
1314
1315 /**************************************************************************
1316 *                               MIDI_DriverProc         [sample driver]
1317 */
1318 LONG MIDI_DriverProc(DWORD dwDevID, HDRVR16 hDriv, WORD wMsg, 
1319                                                 DWORD dwParam1, DWORD dwParam2)
1320 {
1321 #if defined(linux) || defined(__FreeBSD__)
1322         switch(wMsg) {
1323                 case DRV_LOAD:
1324                         return 1;
1325                 case DRV_FREE:
1326                         return 1;
1327                 case DRV_OPEN:
1328                         return 1;
1329                 case DRV_CLOSE:
1330                         return 1;
1331                 case DRV_ENABLE:
1332                         return 1;
1333                 case DRV_DISABLE:
1334                         return 1;
1335                 case DRV_QUERYCONFIGURE:
1336                         return 1;
1337                 case DRV_CONFIGURE:
1338                         MessageBox16(0, "Sample Midi Linux Driver !", 
1339                                      "MMLinux Driver", MB_OK);
1340                         return 1;
1341                 case DRV_INSTALL:
1342                         return DRVCNF_RESTART;
1343                 case DRV_REMOVE:
1344                         return DRVCNF_RESTART;
1345                 case MCI_OPEN_DRIVER:
1346                 case MCI_OPEN:
1347                         return MIDI_mciOpen(dwDevID, dwParam1, (LPMCI_OPEN_PARMS)PTR_SEG_TO_LIN(dwParam2));
1348                 case MCI_CLOSE_DRIVER:
1349                 case MCI_CLOSE:
1350                         return MIDI_mciClose(dwDevID, dwParam1, (LPMCI_GENERIC_PARMS)PTR_SEG_TO_LIN(dwParam2));
1351                 case MCI_PLAY:
1352                         return MIDI_mciPlay(dwDevID, dwParam1, (LPMCI_PLAY_PARMS)PTR_SEG_TO_LIN(dwParam2));
1353                 case MCI_RECORD:
1354                         return MIDI_mciRecord(dwDevID, dwParam1, (LPMCI_RECORD_PARMS)PTR_SEG_TO_LIN(dwParam2));
1355                 case MCI_STOP:
1356                         return MIDI_mciStop(dwDevID, dwParam1, (LPMCI_GENERIC_PARMS)PTR_SEG_TO_LIN(dwParam2));
1357                 case MCI_SET:
1358                         return MIDI_mciSet(dwDevID, dwParam1, (LPMCI_SET_PARMS)PTR_SEG_TO_LIN(dwParam2));
1359                 case MCI_PAUSE:
1360                         return MIDI_mciPause(dwDevID, dwParam1, (LPMCI_GENERIC_PARMS)PTR_SEG_TO_LIN(dwParam2));
1361                 case MCI_RESUME:
1362                         return MIDI_mciResume(dwDevID, dwParam1, (LPMCI_GENERIC_PARMS)PTR_SEG_TO_LIN(dwParam2));
1363                 case MCI_STATUS:
1364                         return MIDI_mciStatus(dwDevID, dwParam1, (LPMCI_STATUS_PARMS)PTR_SEG_TO_LIN(dwParam2));
1365                 case MCI_GETDEVCAPS:
1366                         return MIDI_mciGetDevCaps(dwDevID, dwParam1, (LPMCI_GETDEVCAPS_PARMS)PTR_SEG_TO_LIN(dwParam2));
1367                 case MCI_INFO:
1368                         return MIDI_mciInfo(dwDevID, dwParam1, (LPMCI_INFO_PARMS)PTR_SEG_TO_LIN(dwParam2));
1369                 default:
1370                         return DefDriverProc(dwDevID, hDriv, wMsg, dwParam1, dwParam2);
1371                 }
1372 #else
1373         return MMSYSERR_NOTENABLED;
1374 #endif
1375 }
1376
1377
1378 /*-----------------------------------------------------------------------*/