winmm: The 64KB buffer limit on MIDIHDR buffers applies to streams only.
[wine] / dlls / wineoss.drv / midi.c
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
2
3 /*
4  * Sample MIDI Wine Driver for Open Sound System (basically Linux)
5  *
6  * Copyright 1994       Martin Ayotte
7  * Copyright 1998       Luiz Otavio L. Zorzella (init procedures)
8  * Copyright 1998/1999  Eric POUECH :
9  *              98/7    changes for making this MIDI driver work on OSS
10  *                      current support is limited to MIDI ports of OSS systems
11  *              98/9    rewriting MCI code for MIDI
12  *              98/11   split in midi.c and mcimidi.c
13  *
14  * This library is free software; you can redistribute it and/or
15  * modify it under the terms of the GNU Lesser General Public
16  * License as published by the Free Software Foundation; either
17  * version 2.1 of the License, or (at your option) any later version.
18  *
19  * This library is distributed in the hope that it will be useful,
20  * but WITHOUT ANY WARRANTY; without even the implied warranty of
21  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
22  * Lesser General Public License for more details.
23  *
24  * You should have received a copy of the GNU Lesser General Public
25  * License along with this library; if not, write to the Free Software
26  * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27  */
28
29 /* TODO:
30  *    + use better instrument definition for OPL/2 (midiPatch.c) or
31  *      use existing instrument definition (from playmidi or kmid)
32  *      with a .winerc option 
33  *    + have a look at OPL/3 ?
34  *    + implement asynchronous playback of MidiHdr
35  *    + implement STREAM'ed MidiHdr (question: how shall we share the
36  *      code between the midiStream functions in MMSYSTEM/WINMM and
37  *      the code for the low level driver)
38  *    + use a more accurate read mechanism than the one of snooping on
39  *      timers (like select on fd)
40  */
41
42 #include "config.h"
43 #include "wine/port.h"
44
45 #include <stdlib.h>
46 #include <string.h>
47 #include <stdarg.h>
48 #include <stdio.h>
49 #ifdef HAVE_UNISTD_H
50 # include <unistd.h>
51 #endif
52 #include <fcntl.h>
53 #include <errno.h>
54 #ifdef HAVE_SYS_IOCTL_H
55 # include <sys/ioctl.h>
56 #endif
57 #ifdef HAVE_POLL_H
58 #include <poll.h>
59 #endif
60 #ifdef HAVE_SYS_POLL_H
61 #include <sys/poll.h>
62 #endif
63
64 #include "windef.h"
65 #include "winbase.h"
66 #include "wingdi.h"
67 #include "winuser.h"
68 #include "winnls.h"
69 #include "mmddk.h"
70 #include "oss.h"
71 #include "wine/unicode.h"
72 #include "wine/debug.h"
73
74 WINE_DEFAULT_DEBUG_CHANNEL(midi);
75
76 #ifdef SNDCTL_SEQ_NRMIDIS
77 #define HAVE_OSS_MIDI
78 #endif
79
80 #ifdef HAVE_OSS_MIDI
81
82 typedef struct {
83     int                 state;                  /* -1 disabled, 0 is no recording started, 1 in recording, bit 2 set if in sys exclusive recording */
84     DWORD               bufsize;
85     MIDIOPENDESC        midiDesc;
86     WORD                wFlags;
87     LPMIDIHDR           lpQueueHdr;
88     DWORD               dwTotalPlayed;
89     unsigned char       incoming[3];
90     unsigned char       incPrev;
91     char                incLen;
92     DWORD               startTime;
93     MIDIINCAPSW         caps;
94 } WINE_MIDIIN;
95
96 typedef struct {
97     BOOL                bEnabled;
98     DWORD               bufsize;
99     MIDIOPENDESC        midiDesc;
100     WORD                wFlags;
101     LPMIDIHDR           lpQueueHdr;
102     DWORD               dwTotalPlayed;
103     void*               lpExtra;                /* according to port type (MIDI, FM...), extra data when needed */
104     MIDIOUTCAPSW        caps;
105 } WINE_MIDIOUT;
106
107 static WINE_MIDIIN      MidiInDev [MAX_MIDIINDRV ];
108 static WINE_MIDIOUT     MidiOutDev[MAX_MIDIOUTDRV];
109
110 /* this is the total number of MIDI out devices found (synth and port) */
111 static  int             MODM_NumDevs = 0;
112 /* this is the number of FM synthesizers (index from 0 to NUMFMSYNTHDEVS - 1) */
113 static  int             MODM_NumFMSynthDevs = 0;
114 /* the Midi ports have index from NUMFMSYNTHDEVS to NumDevs - 1 */
115
116 /* this is the total number of MIDI out devices found */
117 static  int             MIDM_NumDevs = 0;
118
119 static  int             midiSeqFD = -1;
120 static  int             numOpenMidiSeq = 0;
121 static  int             numStartedMidiIn = 0;
122
123 static CRITICAL_SECTION crit_sect;   /* protects all MidiIn buffers queues */
124 static CRITICAL_SECTION_DEBUG critsect_debug =
125 {
126     0, 0, &crit_sect,
127     { &critsect_debug.ProcessLocksList, &critsect_debug.ProcessLocksList },
128       0, 0, { (DWORD_PTR)(__FILE__ ": crit_sect") }
129 };
130 static CRITICAL_SECTION crit_sect = { &critsect_debug, -1, 0, 0, 0, 0 };
131
132 static int end_thread;
133 static HANDLE hThread;
134
135 /*======================================================================*
136  *                  Low level MIDI implementation                       *
137  *======================================================================*/
138
139 static int midiOpenSeq(void);
140 static int midiCloseSeq(void);
141
142 /**************************************************************************
143  *                      MIDI_unixToWindowsDeviceType            [internal]
144  *
145  * return the Windows equivalent to a Unix Device Type
146  *
147  */
148 static  int     MIDI_UnixToWindowsDeviceType(int type)
149 {
150     /* MOD_MIDIPORT     output port
151      * MOD_SYNTH        generic internal synth
152      * MOD_SQSYNTH      square wave internal synth
153      * MOD_FMSYNTH      FM internal synth
154      * MOD_MAPPER       MIDI mapper
155      * MOD_WAVETABLE    hardware watetable internal synth
156      * MOD_SWSYNTH      software internal synth
157      */
158
159     /* FIXME Is this really the correct equivalence from UNIX to
160        Windows Sound type */
161
162     switch (type) {
163     case SYNTH_TYPE_FM:     return MOD_FMSYNTH;
164     case SYNTH_TYPE_SAMPLE: return MOD_SYNTH;
165     case SYNTH_TYPE_MIDI:   return MOD_MIDIPORT;
166     default:
167         ERR("Cannot determine the type of this midi device. "
168             "Assuming FM Synth\n");
169         return MOD_FMSYNTH;
170     }
171 }
172
173 /**************************************************************************
174  *                      OSS_MidiInit                            [internal]
175  *
176  * Initializes the MIDI devices information variables
177  */
178 LRESULT OSS_MidiInit(void)
179 {
180     int                 i, status, numsynthdevs = 255, nummididevs = 255;
181     struct synth_info   sinfo;
182     struct midi_info    minfo;
183     static      BOOL    bInitDone = FALSE;
184
185     if (bInitDone)
186         return 0;
187
188     TRACE("Initializing the MIDI variables.\n");
189     bInitDone = TRUE;
190
191     /* try to open device */
192     if (midiOpenSeq() == -1) {
193         return -1;
194     }
195
196     /* find how many Synth devices are there in the system */
197     status = ioctl(midiSeqFD, SNDCTL_SEQ_NRSYNTHS, &numsynthdevs);
198
199     if (status == -1) {
200         ERR("ioctl for nr synth failed.\n");
201         midiCloseSeq();
202         return -1;
203     }
204
205     if (numsynthdevs > MAX_MIDIOUTDRV) {
206         ERR("MAX_MIDIOUTDRV (%d) was enough for the number of devices (%d). "
207             "Some FM devices will not be available.\n",MAX_MIDIOUTDRV,numsynthdevs);
208         numsynthdevs = MAX_MIDIOUTDRV;
209     }
210
211     for (i = 0; i < numsynthdevs; i++) {
212         /* Manufac ID. We do not have access to this with soundcard.h
213          * Does not seem to be a problem, because in mmsystem.h only
214          * Microsoft's ID is listed.
215          */
216         MidiOutDev[i].caps.wMid = 0x00FF;
217         MidiOutDev[i].caps.wPid = 0x0001;       /* FIXME Product ID  */
218         /* Product Version. We simply say "1" */
219         MidiOutDev[i].caps.vDriverVersion = 0x001;
220         MidiOutDev[i].caps.wChannelMask   = 0xFFFF;
221
222         /* FIXME Do we have this information?
223          * Assuming the soundcards can handle
224          * MIDICAPS_VOLUME and MIDICAPS_LRVOLUME but
225          * not MIDICAPS_CACHE.
226          */
227         MidiOutDev[i].caps.dwSupport      = MIDICAPS_VOLUME|MIDICAPS_LRVOLUME;
228
229         sinfo.device = i;
230         status = ioctl(midiSeqFD, SNDCTL_SYNTH_INFO, &sinfo);
231         if (status == -1) {
232             static const WCHAR fmt[] = {'W','i','n','e',' ','O','S','S',' ','M','i','d','i',' ','O','u','t',' ','(','#','%','d',')',' ','d','i','s','a','b','l','e','d',0};
233             ERR("ioctl for synth info failed on %d, disabling it.\n", i);
234
235             wsprintfW(MidiOutDev[i].caps.szPname, fmt, i);
236
237             MidiOutDev[i].caps.wTechnology = MOD_MIDIPORT;
238             MidiOutDev[i].caps.wVoices     = 16;
239             MidiOutDev[i].caps.wNotes      = 16;
240             MidiOutDev[i].bEnabled = FALSE;
241         } else {
242             MultiByteToWideChar( CP_UNIXCP, 0, sinfo.name, -1,
243                                  MidiOutDev[i].caps.szPname,
244                                  sizeof(MidiOutDev[i].caps.szPname)/sizeof(WCHAR) );
245
246             MidiOutDev[i].caps.wTechnology = MIDI_UnixToWindowsDeviceType(sinfo.synth_type);
247             MidiOutDev[i].caps.wVoices     = sinfo.nr_voices;
248
249             /* FIXME Is it possible to know the maximum
250              * number of simultaneous notes of a soundcard ?
251              * I believe we don't have this information, but
252              * it's probably equal or more than wVoices
253              */
254             MidiOutDev[i].caps.wNotes      = sinfo.nr_voices;
255             MidiOutDev[i].bEnabled = TRUE;
256         }
257
258         /* We also have the information sinfo.synth_subtype, not used here
259          */
260
261         if (sinfo.capabilities & SYNTH_CAP_INPUT) {
262             FIXME("Synthesizer supports MIDI in. Not yet supported.\n");
263         }
264
265         TRACE("SynthOut[%d]\tname='%s' techn=%d voices=%d notes=%d chnMsk=%04x support=%d\n"
266               "\tOSS info: synth subtype=%d capa=%lx\n",
267               i, wine_dbgstr_w(MidiOutDev[i].caps.szPname), 
268               MidiOutDev[i].caps.wTechnology, 
269               MidiOutDev[i].caps.wVoices, MidiOutDev[i].caps.wNotes, 
270               MidiOutDev[i].caps.wChannelMask, MidiOutDev[i].caps.dwSupport,
271               sinfo.synth_subtype, (long)sinfo.capabilities);
272     }
273
274     /* find how many MIDI devices are there in the system */
275     status = ioctl(midiSeqFD, SNDCTL_SEQ_NRMIDIS, &nummididevs);
276     if (status == -1) {
277         ERR("ioctl on nr midi failed.\n");
278         nummididevs = 0;
279         goto wrapup;
280     }
281
282     /* FIXME: the two restrictions below could be loosened in some cases */
283     if (numsynthdevs + nummididevs > MAX_MIDIOUTDRV) {
284         ERR("MAX_MIDIOUTDRV was not enough for the number of devices. "
285             "Some MIDI devices will not be available.\n");
286         nummididevs = MAX_MIDIOUTDRV - numsynthdevs;
287     }
288
289     if (nummididevs > MAX_MIDIINDRV) {
290         ERR("MAX_MIDIINDRV (%d) was not enough for the number of devices (%d). "
291             "Some MIDI devices will not be available.\n",MAX_MIDIINDRV,nummididevs);
292         nummididevs = MAX_MIDIINDRV;
293     }
294
295     for (i = 0; i < nummididevs; i++) {
296         minfo.device = i;
297         status = ioctl(midiSeqFD, SNDCTL_MIDI_INFO, &minfo);
298         if (status == -1) WARN("ioctl on midi info for device %d failed.\n", i);
299
300         /* This whole part is somewhat obscure to me. I'll keep trying to dig
301            info about it. If you happen to know, please tell us. The very
302            descriptive minfo.dev_type was not used here.
303         */
304         /* Manufacturer ID. We do not have access to this with soundcard.h
305            Does not seem to be a problem, because in mmsystem.h only
306            Microsoft's ID is listed */
307         MidiOutDev[numsynthdevs + i].caps.wMid = 0x00FF;
308         MidiOutDev[numsynthdevs + i].caps.wPid = 0x0001;        /* FIXME Product ID */
309         /* Product Version. We simply say "1" */
310         MidiOutDev[numsynthdevs + i].caps.vDriverVersion = 0x001;
311         if (status == -1) {
312             static const WCHAR fmt[] = {'W','i','n','e',' ','O','S','S',' ','M','i','d','i',' ','O','u','t',' ','(','#','%','d',')',' ','d','i','s','a','b','l','e','d',0};
313             wsprintfW(MidiOutDev[numsynthdevs + i].caps.szPname, fmt, numsynthdevs + i);
314             MidiOutDev[numsynthdevs + i].bEnabled = FALSE;
315         } else {
316             MultiByteToWideChar(CP_UNIXCP, 0, minfo.name, -1,
317                                 MidiOutDev[numsynthdevs + i].caps.szPname,
318                                 sizeof(MidiOutDev[numsynthdevs + i].caps.szPname) / sizeof(WCHAR));
319             MidiOutDev[numsynthdevs + i].bEnabled = TRUE;
320         }
321         MidiOutDev[numsynthdevs + i].caps.wTechnology = MOD_MIDIPORT; /* FIXME Is this right? */
322         /* Does it make any difference? */
323         MidiOutDev[numsynthdevs + i].caps.wVoices     = 16;
324         /* Does it make any difference? */
325         MidiOutDev[numsynthdevs + i].caps.wNotes      = 16;
326         MidiOutDev[numsynthdevs + i].caps.wChannelMask= 0xFFFF;
327
328         /* FIXME Does it make any difference? */
329         MidiOutDev[numsynthdevs + i].caps.dwSupport   = MIDICAPS_VOLUME|MIDICAPS_LRVOLUME;
330
331         /* This whole part is somewhat obscure to me. I'll keep trying to dig
332            info about it. If you happen to know, please tell us. The very
333            descriptive minfo.dev_type was not used here.
334         */
335         /* Manufac ID. We do not have access to this with soundcard.h
336            Does not seem to be a problem, because in mmsystem.h only
337            Microsoft's ID is listed */
338         MidiInDev[i].caps.wMid = 0x00FF;
339         MidiInDev[i].caps.wPid = 0x0001;        /* FIXME Product ID */
340         /* Product Version. We simply say "1" */
341         MidiInDev[i].caps.vDriverVersion = 0x001;
342         if (status == -1) {
343             static const WCHAR fmt[] = {'W','i','n','e',' ','O','S','S',' ','M','i','d','i',' ','I','n',' ','(','#','%','d',')',' ','d','i','s','a','b','l','e','d',0};
344             wsprintfW(MidiInDev[i].caps.szPname, fmt, numsynthdevs + i);
345             MidiInDev[i].state = -1;
346         } else {
347             MultiByteToWideChar(CP_UNIXCP, 0, minfo.name, -1,
348                                 MidiInDev[i].caps.szPname,
349                                 sizeof(MidiInDev[i].caps.szPname) / sizeof(WCHAR));
350             MidiInDev[i].state = 0;
351         }
352         /* FIXME : could we get better information than that ? */
353         MidiInDev[i].caps.dwSupport   = MIDICAPS_VOLUME|MIDICAPS_LRVOLUME;
354
355         TRACE("MidiOut[%d]\tname='%s' techn=%d voices=%d notes=%d chnMsk=%04x support=%d\n"
356               "MidiIn [%d]\tname='%s' support=%d\n"
357               "\tOSS info: midi dev-type=%d, capa=%lx\n",
358               i, wine_dbgstr_w(MidiOutDev[numsynthdevs + i].caps.szPname), 
359               MidiOutDev[numsynthdevs + i].caps.wTechnology,
360               MidiOutDev[numsynthdevs + i].caps.wVoices, MidiOutDev[numsynthdevs + i].caps.wNotes,
361               MidiOutDev[numsynthdevs + i].caps.wChannelMask, MidiOutDev[numsynthdevs + i].caps.dwSupport,
362               i, wine_dbgstr_w(MidiInDev[i].caps.szPname), MidiInDev[i].caps.dwSupport,
363               minfo.dev_type, (long)minfo.capabilities);
364     }
365
366  wrapup:
367     /* windows does not seem to differentiate Synth from MIDI devices */
368     MODM_NumFMSynthDevs = numsynthdevs;
369     MODM_NumDevs        = numsynthdevs + nummididevs;
370
371     MIDM_NumDevs        = nummididevs;
372
373     /* close file and exit */
374     midiCloseSeq();
375
376     return 0;
377 }
378
379 /**************************************************************************
380  *                      OSS_MidiExit                            [internal]
381  *
382  * Release the MIDI devices information variables
383  */
384 LRESULT OSS_MidiExit(void)
385 {
386     TRACE("()\n");
387
388     ZeroMemory(MidiInDev, sizeof(MidiInDev));
389     ZeroMemory(MidiOutDev, sizeof(MidiOutDev));
390
391     MODM_NumDevs = 0;
392     MODM_NumFMSynthDevs = 0;
393     MIDM_NumDevs = 0;
394
395     return 0;
396 }
397
398 /**************************************************************************
399  *                      MIDI_NotifyClient                       [internal]
400  */
401 static DWORD MIDI_NotifyClient(UINT wDevID, WORD wMsg,
402                                DWORD dwParam1, DWORD dwParam2)
403 {
404     DWORD               dwCallBack;
405     UINT                uFlags;
406     HANDLE              hDev;
407     DWORD               dwInstance;
408
409     TRACE("wDevID = %04X wMsg = %d dwParm1 = %04X dwParam2 = %04X\n",
410           wDevID, wMsg, dwParam1, dwParam2);
411
412     switch (wMsg) {
413     case MOM_OPEN:
414     case MOM_CLOSE:
415     case MOM_DONE:
416     case MOM_POSITIONCB:
417         if (wDevID > MODM_NumDevs)
418             return MMSYSERR_BADDEVICEID;
419
420         dwCallBack = MidiOutDev[wDevID].midiDesc.dwCallback;
421         uFlags = MidiOutDev[wDevID].wFlags;
422         hDev = MidiOutDev[wDevID].midiDesc.hMidi;
423         dwInstance = MidiOutDev[wDevID].midiDesc.dwInstance;
424         break;
425
426     case MIM_OPEN:
427     case MIM_CLOSE:
428     case MIM_DATA:
429     case MIM_LONGDATA:
430     case MIM_ERROR:
431     case MIM_LONGERROR:
432     case MIM_MOREDATA:
433         if (wDevID > MIDM_NumDevs)
434             return MMSYSERR_BADDEVICEID;
435
436         dwCallBack = MidiInDev[wDevID].midiDesc.dwCallback;
437         uFlags = MidiInDev[wDevID].wFlags;
438         hDev = MidiInDev[wDevID].midiDesc.hMidi;
439         dwInstance = MidiInDev[wDevID].midiDesc.dwInstance;
440         break;
441     default:
442         WARN("Unsupported MSW-MIDI message %u\n", wMsg);
443         return MMSYSERR_ERROR;
444     }
445
446     return DriverCallback(dwCallBack, uFlags, hDev, wMsg, dwInstance, dwParam1, dwParam2) ?
447         0 : MMSYSERR_ERROR;
448 }
449
450 static int midi_warn = 1;
451 /**************************************************************************
452  *                      midiOpenSeq                             [internal]
453  */
454 static int midiOpenSeq(void)
455 {
456     if (numOpenMidiSeq == 0) {
457         const char* device;
458         device=getenv("MIDIDEV");
459         if (!device) device="/dev/sequencer";
460         midiSeqFD = open(device, O_RDWR, 0);
461         if (midiSeqFD == -1) {
462             if (midi_warn)
463             {
464                 WARN("Can't open MIDI device '%s' ! (%s). If your "
465                         "program needs this (probably not): %s\n",
466                         device, strerror(errno),
467                         errno == ENOENT ?
468                         "create it ! (\"man MAKEDEV\" ?)" :
469                         errno == ENODEV ?
470                         "load MIDI sequencer kernel driver !" :
471                         errno == EACCES ?
472                         "grant access ! (\"man chmod\")" : ""
473                 );
474             }
475             midi_warn = 0;
476             return -1;
477         }
478 #if 0
479         if (fcntl(midiSeqFD, F_SETFL, O_NONBLOCK) < 0) {
480             WARN("can't set sequencer fd to non-blocking, errno %d (%s)\n", errno, strerror(errno));
481             close(midiSeqFD);
482             midiSeqFD = -1;
483             return -1;
484         }
485 #endif
486         fcntl(midiSeqFD, F_SETFD, 1); /* set close on exec flag */
487         ioctl(midiSeqFD, SNDCTL_SEQ_RESET);
488     }
489     numOpenMidiSeq++;
490     return 0;
491 }
492
493 /**************************************************************************
494  *                      midiCloseSeq                            [internal]
495  */
496 static int midiCloseSeq(void)
497 {
498     if (--numOpenMidiSeq == 0) {
499         close(midiSeqFD);
500         midiSeqFD = -1;
501     }
502     return 0;
503 }
504
505 /* FIXME: this is a bad idea, it's even not static... */
506 SEQ_DEFINEBUF(1024);
507
508 /* FIXME: this is not reentrant, not static - because of global variable
509  * _seqbuf and al.
510  */
511 /**************************************************************************
512  *                      seqbuf_dump                             [internal]
513  *
514  * Used by SEQ_DUMPBUF to flush the buffer.
515  *
516  */
517 void seqbuf_dump(void)
518 {
519     if (_seqbufptr) {
520         if (write(midiSeqFD, _seqbuf, _seqbufptr) == -1) {
521             WARN("Can't write data to sequencer %d, errno %d (%s)!\n",
522                  midiSeqFD, errno, strerror(errno));
523         }
524         /* FIXME:
525          *      in any case buffer is lost so that if many errors occur the buffer
526          * will not overrun
527          */
528         _seqbufptr = 0;
529     }
530 }
531
532 /**************************************************************************
533  *                      midReceiveChar                          [internal]
534  */
535 static void midReceiveChar(WORD wDevID, unsigned char value, DWORD dwTime)
536 {
537     DWORD               toSend = 0;
538
539     TRACE("Adding %02xh to %d[%d]\n", value, wDevID, MidiInDev[wDevID].incLen);
540
541     if (wDevID >= MIDM_NumDevs) {
542         WARN("bad devID\n");
543         return;
544     }
545     if (MidiInDev[wDevID].state <= 0) {
546         TRACE("disabled or input not started, thrown away\n");
547         return;
548     }
549
550     if (MidiInDev[wDevID].state & 2) { /* system exclusive */
551         LPMIDIHDR       lpMidiHdr;
552         WORD            sbfb = FALSE;
553
554         EnterCriticalSection(&crit_sect);
555         if ((lpMidiHdr = MidiInDev[wDevID].lpQueueHdr) != NULL) {
556             LPBYTE      lpData = (LPBYTE) lpMidiHdr->lpData;
557
558             lpData[lpMidiHdr->dwBytesRecorded++] = value;
559             if (lpMidiHdr->dwBytesRecorded == lpMidiHdr->dwBufferLength) {
560                 sbfb = TRUE;
561             }
562         }
563         if (value == 0xF7) { /* then end */
564             MidiInDev[wDevID].state &= ~2;
565             sbfb = TRUE;
566         }
567         if (sbfb && lpMidiHdr != NULL) {
568             lpMidiHdr = MidiInDev[wDevID].lpQueueHdr;
569             lpMidiHdr->dwFlags &= ~MHDR_INQUEUE;
570             lpMidiHdr->dwFlags |= MHDR_DONE;
571             MidiInDev[wDevID].lpQueueHdr = lpMidiHdr->lpNext;
572             if (MIDI_NotifyClient(wDevID, MIM_LONGDATA, (DWORD_PTR)lpMidiHdr, dwTime) != MMSYSERR_NOERROR) {
573                 WARN("Couldn't notify client\n");
574             }
575         }
576         LeaveCriticalSection(&crit_sect);
577         return;
578     }
579
580 #define IS_CMD(_x)      (((_x) & 0x80) == 0x80)
581 #define IS_SYS_CMD(_x)  (((_x) & 0xF0) == 0xF0)
582
583     if (!IS_CMD(value) && MidiInDev[wDevID].incLen == 0) { /* try to reuse old cmd */
584         if (IS_CMD(MidiInDev[wDevID].incPrev) && !IS_SYS_CMD(MidiInDev[wDevID].incPrev)) {
585             MidiInDev[wDevID].incoming[0] = MidiInDev[wDevID].incPrev;
586             MidiInDev[wDevID].incLen = 1;
587             TRACE("Reusing old command %02xh\n", MidiInDev[wDevID].incPrev);
588         } else {
589             FIXME("error for midi-in, should generate MIM_ERROR notification:"
590                   " prev=%02Xh, incLen=%02Xh\n",
591                   MidiInDev[wDevID].incPrev, MidiInDev[wDevID].incLen);
592             return;
593         }
594     }
595     MidiInDev[wDevID].incoming[(int)(MidiInDev[wDevID].incLen++)] = value;
596     if (MidiInDev[wDevID].incLen == 1 && !IS_SYS_CMD(MidiInDev[wDevID].incoming[0])) {
597         /* store new cmd, just in case */
598         MidiInDev[wDevID].incPrev = MidiInDev[wDevID].incoming[0];
599     }
600
601 #undef IS_CMD
602 #undef IS_SYS_CMD
603
604     switch (MidiInDev[wDevID].incoming[0] & 0xF0) {
605     case MIDI_NOTEOFF:
606     case MIDI_NOTEON:
607     case MIDI_KEY_PRESSURE:
608     case MIDI_CTL_CHANGE:
609     case MIDI_PITCH_BEND:
610         if (MidiInDev[wDevID].incLen == 3) {
611             toSend = (MidiInDev[wDevID].incoming[2] << 16) |
612                 (MidiInDev[wDevID].incoming[1] <<  8) |
613                 (MidiInDev[wDevID].incoming[0] <<  0);
614         }
615         break;
616     case MIDI_PGM_CHANGE:
617     case MIDI_CHN_PRESSURE:
618         if (MidiInDev[wDevID].incLen == 2) {
619             toSend = (MidiInDev[wDevID].incoming[1] <<  8) |
620                 (MidiInDev[wDevID].incoming[0] <<  0);
621         }
622         break;
623     case MIDI_SYSTEM_PREFIX:
624         if (MidiInDev[wDevID].incoming[0] == 0xF0) {
625             MidiInDev[wDevID].state |= 2;
626             MidiInDev[wDevID].incLen = 0;
627         } else {
628             if (MidiInDev[wDevID].incLen == 1) {
629                 toSend = (MidiInDev[wDevID].incoming[0] <<  0);
630             }
631         }
632         break;
633     default:
634         WARN("This shouldn't happen (%02X)\n", MidiInDev[wDevID].incoming[0]);
635     }
636     if (toSend != 0) {
637         TRACE("Sending event %08x\n", toSend);
638         MidiInDev[wDevID].incLen =      0;
639         dwTime -= MidiInDev[wDevID].startTime;
640         if (MIDI_NotifyClient(wDevID, MIM_DATA, toSend, dwTime) != MMSYSERR_NOERROR) {
641             WARN("Couldn't notify client\n");
642         }
643     }
644 }
645
646 static DWORD WINAPI midRecThread(LPVOID arg)
647 {
648     unsigned char buffer[256];
649     int len, idx;
650     DWORD dwTime;
651     struct pollfd pfd;
652
653     TRACE("Thread startup\n");
654
655     pfd.fd = midiSeqFD;
656     pfd.fd = POLLIN;
657     
658     while(!end_thread) {
659         TRACE("Thread loop\n");
660
661         /* Check if an event is present */
662         if (poll(&pfd, 1, 250) <= 0)
663             continue;
664         
665         len = read(midiSeqFD, buffer, sizeof(buffer));
666         TRACE("Received %d bytes\n", len);
667
668         if (len < 0) continue;
669         if ((len % 4) != 0) {
670             WARN("Bad length %d, errno %d (%s)\n", len, errno, strerror(errno));
671             continue;
672         }
673
674         dwTime = GetTickCount();
675         
676         for (idx = 0; idx < len; ) {
677             if (buffer[idx] & 0x80) {
678                 TRACE(
679                       "Reading<8> %02x %02x %02x %02x %02x %02x %02x %02x\n",
680                       buffer[idx + 0], buffer[idx + 1],
681                       buffer[idx + 2], buffer[idx + 3],
682                       buffer[idx + 4], buffer[idx + 5],
683                       buffer[idx + 6], buffer[idx + 7]);
684                       idx += 8;
685             } else {
686                 switch (buffer[idx + 0]) {
687                 case SEQ_WAIT:
688                 case SEQ_ECHO:
689                     break;
690                 case SEQ_MIDIPUTC:
691                     midReceiveChar(buffer[idx + 2], buffer[idx + 1], dwTime);
692                     break;
693                 default:
694                     TRACE("Unsupported event %d\n", buffer[idx + 0]);
695                     break;
696                 }
697                 idx += 4;
698             }
699         }
700     }
701     return 0;
702 }
703
704 /**************************************************************************
705  *                              midGetDevCaps                   [internal]
706  */
707 static DWORD midGetDevCaps(WORD wDevID, LPMIDIINCAPSW lpCaps, DWORD dwSize)
708 {
709     TRACE("(%04X, %p, %08X);\n", wDevID, lpCaps, dwSize);
710
711     if (wDevID >= MIDM_NumDevs) return MMSYSERR_BADDEVICEID;
712     if (lpCaps == NULL)         return MMSYSERR_INVALPARAM;
713
714     memcpy(lpCaps, &MidiInDev[wDevID].caps, min(dwSize, sizeof(*lpCaps)));
715
716     return MMSYSERR_NOERROR;
717 }
718
719 /**************************************************************************
720  *                      midOpen                                 [internal]
721  */
722 static DWORD midOpen(WORD wDevID, LPMIDIOPENDESC lpDesc, DWORD dwFlags)
723 {
724     TRACE("(%04X, %p, %08X);\n", wDevID, lpDesc, dwFlags);
725
726     if (lpDesc == NULL) {
727         WARN("Invalid Parameter !\n");
728         return MMSYSERR_INVALPARAM;
729     }
730
731     /* FIXME :
732      *  how to check that content of lpDesc is correct ?
733      */
734     if (wDevID >= MIDM_NumDevs) {
735         WARN("wDevID too large (%u) !\n", wDevID);
736         return MMSYSERR_BADDEVICEID;
737     }
738     if (MidiInDev[wDevID].state == -1) {        
739         WARN("device disabled\n");
740         return MIDIERR_NODEVICE;
741     }
742     if (MidiInDev[wDevID].midiDesc.hMidi != 0) {
743         WARN("device already open !\n");
744         return MMSYSERR_ALLOCATED;
745     }
746     if ((dwFlags & MIDI_IO_STATUS) != 0) {
747         WARN("No support for MIDI_IO_STATUS in dwFlags yet, ignoring it\n");
748         dwFlags &= ~MIDI_IO_STATUS;
749     }
750     if ((dwFlags & ~CALLBACK_TYPEMASK) != 0) {
751         FIXME("Bad dwFlags\n");
752         return MMSYSERR_INVALFLAG;
753     }
754
755     if (midiOpenSeq() < 0) {
756         return MMSYSERR_ERROR;
757     }
758
759     if (numStartedMidiIn++ == 0) {
760         end_thread = 0;
761         hThread = CreateThread(NULL, 0, midRecThread, NULL, 0, NULL);
762         if (!hThread) {
763             numStartedMidiIn = 0;
764             WARN("Couldn't create thread for midi-in\n");
765             midiCloseSeq();
766             return MMSYSERR_ERROR;
767         }
768         SetThreadPriority(hThread, THREAD_PRIORITY_TIME_CRITICAL);
769         TRACE("Created thread for midi-in\n");
770     }
771
772     MidiInDev[wDevID].wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
773
774     MidiInDev[wDevID].lpQueueHdr = NULL;
775     MidiInDev[wDevID].dwTotalPlayed = 0;
776     MidiInDev[wDevID].bufsize = 0x3FFF;
777     MidiInDev[wDevID].midiDesc = *lpDesc;
778     MidiInDev[wDevID].state = 0;
779     MidiInDev[wDevID].incLen = 0;
780     MidiInDev[wDevID].startTime = 0;
781
782     if (MIDI_NotifyClient(wDevID, MIM_OPEN, 0L, 0L) != MMSYSERR_NOERROR) {
783         WARN("can't notify client !\n");
784         return MMSYSERR_INVALPARAM;
785     }
786     return MMSYSERR_NOERROR;
787 }
788
789 /**************************************************************************
790  *                      midClose                                [internal]
791  */
792 static DWORD midClose(WORD wDevID)
793 {
794     int         ret = MMSYSERR_NOERROR;
795
796     TRACE("(%04X);\n", wDevID);
797
798     if (wDevID >= MIDM_NumDevs) {
799         WARN("wDevID too big (%u) !\n", wDevID);
800         return MMSYSERR_BADDEVICEID;
801     }
802     if (MidiInDev[wDevID].midiDesc.hMidi == 0) {
803         WARN("device not opened !\n");
804         return MMSYSERR_ERROR;
805     }
806     if (MidiInDev[wDevID].lpQueueHdr != 0) {
807         return MIDIERR_STILLPLAYING;
808     }
809
810     if (midiSeqFD == -1) {
811         WARN("ooops !\n");
812         return MMSYSERR_ERROR;
813     }
814     if (--numStartedMidiIn == 0) {
815         TRACE("Stopping thread for midi-in\n");
816         end_thread = 1;
817         if (WaitForSingleObject(hThread, 5000) != WAIT_OBJECT_0) {
818             WARN("Thread end not signaled, force termination\n");
819             TerminateThread(hThread, 0);
820         }
821         TRACE("Stopped thread for midi-in\n");
822     }
823     midiCloseSeq();
824
825     MidiInDev[wDevID].bufsize = 0;
826     if (MIDI_NotifyClient(wDevID, MIM_CLOSE, 0L, 0L) != MMSYSERR_NOERROR) {
827         WARN("can't notify client !\n");
828         ret = MMSYSERR_INVALPARAM;
829     }
830     MidiInDev[wDevID].midiDesc.hMidi = 0;
831     return ret;
832 }
833
834 /**************************************************************************
835  *                              midAddBuffer                    [internal]
836  */
837 static DWORD midAddBuffer(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
838 {
839     TRACE("(%04X, %p, %08X);\n", wDevID, lpMidiHdr, dwSize);
840
841     if (wDevID >= MIDM_NumDevs) return MMSYSERR_BADDEVICEID;
842     if (MidiInDev[wDevID].state == -1) return MIDIERR_NODEVICE;
843
844     if (lpMidiHdr == NULL)      return MMSYSERR_INVALPARAM;
845     if (sizeof(MIDIHDR) > dwSize) return MMSYSERR_INVALPARAM;
846     if (lpMidiHdr->dwBufferLength == 0) return MMSYSERR_INVALPARAM;
847     if (lpMidiHdr->dwFlags & MHDR_INQUEUE) return MIDIERR_STILLPLAYING;
848     if (!(lpMidiHdr->dwFlags & MHDR_PREPARED)) return MIDIERR_UNPREPARED;
849
850     EnterCriticalSection(&crit_sect);
851     lpMidiHdr->dwFlags &= ~WHDR_DONE;
852     lpMidiHdr->dwFlags |= MHDR_INQUEUE;
853     lpMidiHdr->dwBytesRecorded = 0;
854     lpMidiHdr->lpNext = 0;
855     if (MidiInDev[wDevID].lpQueueHdr == 0) {
856         MidiInDev[wDevID].lpQueueHdr = lpMidiHdr;
857     } else {
858         LPMIDIHDR       ptr;
859
860         for (ptr = MidiInDev[wDevID].lpQueueHdr;
861              ptr->lpNext != 0;
862              ptr = ptr->lpNext);
863         ptr->lpNext = lpMidiHdr;
864     }
865     LeaveCriticalSection(&crit_sect);
866
867     return MMSYSERR_NOERROR;
868 }
869
870 /**************************************************************************
871  *                              midPrepare                      [internal]
872  */
873 static DWORD midPrepare(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
874 {
875     TRACE("(%04X, %p, %08X);\n", wDevID, lpMidiHdr, dwSize);
876
877     if (dwSize < sizeof(MIDIHDR) || lpMidiHdr == 0 ||
878         lpMidiHdr->lpData == 0 || (lpMidiHdr->dwFlags & MHDR_INQUEUE) != 0)
879         return MMSYSERR_INVALPARAM;
880
881     lpMidiHdr->lpNext = 0;
882     lpMidiHdr->dwFlags |= MHDR_PREPARED;
883     lpMidiHdr->dwBytesRecorded = 0;
884
885     return MMSYSERR_NOERROR;
886 }
887
888 /**************************************************************************
889  *                              midUnprepare                    [internal]
890  */
891 static DWORD midUnprepare(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
892 {
893     TRACE("(%04X, %p, %08X);\n", wDevID, lpMidiHdr, dwSize);
894
895     if (wDevID >= MIDM_NumDevs) return MMSYSERR_BADDEVICEID;
896     if (MidiInDev[wDevID].state == -1) return MIDIERR_NODEVICE;
897
898     if (dwSize < sizeof(MIDIHDR) || lpMidiHdr == 0 ||
899         lpMidiHdr->lpData == 0)
900         return MMSYSERR_INVALPARAM;
901
902     if (!(lpMidiHdr->dwFlags & MHDR_PREPARED)) return MIDIERR_UNPREPARED;
903     if (lpMidiHdr->dwFlags & MHDR_INQUEUE) return MIDIERR_STILLPLAYING;
904
905     lpMidiHdr->dwFlags &= ~MHDR_PREPARED;
906
907     return MMSYSERR_NOERROR;
908 }
909
910 /**************************************************************************
911  *                      midReset                                [internal]
912  */
913 static DWORD midReset(WORD wDevID)
914 {
915     DWORD               dwTime = GetTickCount();
916
917     TRACE("(%04X);\n", wDevID);
918
919     if (wDevID >= MIDM_NumDevs) return MMSYSERR_BADDEVICEID;
920     if (MidiInDev[wDevID].state == -1) return MIDIERR_NODEVICE;
921
922     EnterCriticalSection(&crit_sect);
923     while (MidiInDev[wDevID].lpQueueHdr) {
924         MidiInDev[wDevID].lpQueueHdr->dwFlags &= ~MHDR_INQUEUE;
925         MidiInDev[wDevID].lpQueueHdr->dwFlags |= MHDR_DONE;
926         /* FIXME: when called from 16 bit, lpQueueHdr needs to be a segmented ptr */
927         if (MIDI_NotifyClient(wDevID, MIM_LONGDATA,
928                               (DWORD_PTR)MidiInDev[wDevID].lpQueueHdr, dwTime) != MMSYSERR_NOERROR) {
929             WARN("Couldn't notify client\n");
930         }
931         MidiInDev[wDevID].lpQueueHdr = MidiInDev[wDevID].lpQueueHdr->lpNext;
932     }
933     LeaveCriticalSection(&crit_sect);
934
935     return MMSYSERR_NOERROR;
936 }
937
938
939 /**************************************************************************
940  *                      midStart                                [internal]
941  */
942 static DWORD midStart(WORD wDevID)
943 {
944     TRACE("(%04X);\n", wDevID);
945
946     if (wDevID >= MIDM_NumDevs) return MMSYSERR_BADDEVICEID;
947     if (MidiInDev[wDevID].state == -1) return MIDIERR_NODEVICE;
948
949     MidiInDev[wDevID].state = 1;
950     MidiInDev[wDevID].startTime = GetTickCount();
951     return MMSYSERR_NOERROR;
952 }
953
954 /**************************************************************************
955  *                      midStop                                 [internal]
956  */
957 static DWORD midStop(WORD wDevID)
958 {
959     TRACE("(%04X);\n", wDevID);
960
961     if (wDevID >= MIDM_NumDevs) return MMSYSERR_BADDEVICEID;
962     if (MidiInDev[wDevID].state == -1) return MIDIERR_NODEVICE;
963
964     MidiInDev[wDevID].state = 0;
965     return MMSYSERR_NOERROR;
966 }
967
968 /*-----------------------------------------------------------------------*/
969
970 typedef struct sVoice {
971     int                 note;                   /* 0 means not used */
972     int                 channel;
973     unsigned            cntMark : 30,
974                         status : 2;
975 #define sVS_UNUSED      0
976 #define sVS_PLAYING     1
977 #define sVS_SUSTAINED   2
978 } sVoice;
979
980 typedef struct sChannel {
981     int                 program;
982
983     int                 bender;
984     int                 benderRange;
985     /* controllers */
986     int                 bank;           /* CTL_BANK_SELECT */
987     int                 volume;         /* CTL_MAIN_VOLUME */
988     int                 balance;        /* CTL_BALANCE     */
989     int                 expression;     /* CTL_EXPRESSION  */
990     int                 sustain;        /* CTL_SUSTAIN     */
991
992     unsigned char       nrgPmtMSB;      /* Non register Parameters */
993     unsigned char       nrgPmtLSB;
994     unsigned char       regPmtMSB;      /* Non register Parameters */
995     unsigned char       regPmtLSB;
996 } sChannel;
997
998 typedef struct sFMextra {
999     unsigned            counter;
1000     int                 drumSetMask;
1001     sChannel            channel[16];    /* MIDI has only 16 channels */
1002     sVoice              voice[1];       /* dyn allocated according to sound card */
1003     /* do not append fields below voice[1] since the size of this structure
1004      * depends on the number of available voices on the FM synth...
1005      */
1006 } sFMextra;
1007
1008 extern const unsigned char midiFMInstrumentPatches[16 * 128];
1009 extern const unsigned char midiFMDrumsPatches     [16 * 128];
1010
1011 /**************************************************************************
1012  *                      modFMLoad                               [internal]
1013  */
1014 static int modFMLoad(int dev)
1015 {
1016     int                         i;
1017     struct sbi_instrument       sbi;
1018
1019     sbi.device = dev;
1020     sbi.key = FM_PATCH;
1021
1022     memset(sbi.operators + 16, 0, 16);
1023     for (i = 0; i < 128; i++) {
1024         sbi.channel = i;
1025         memcpy(sbi.operators, midiFMInstrumentPatches + i * 16, 16);
1026
1027         if (write(midiSeqFD, &sbi, sizeof(sbi)) == -1) {
1028             WARN("Couldn't write patch for instrument %d, errno %d (%s)!\n", sbi.channel, errno, strerror(errno));
1029             return -1;
1030         }
1031     }
1032     for (i = 0; i < 128; i++) {
1033         sbi.channel = 128 + i;
1034         memcpy(sbi.operators, midiFMDrumsPatches + i * 16, 16);
1035
1036         if (write(midiSeqFD, &sbi, sizeof(sbi)) == -1) {
1037             WARN("Couldn't write patch for drum %d, errno %d (%s)!\n", sbi.channel, errno, strerror(errno));
1038             return -1;
1039         }
1040     }
1041     return 0;
1042 }
1043
1044 /**************************************************************************
1045  *                      modFMReset                              [internal]
1046  */
1047 static  void modFMReset(WORD wDevID)
1048 {
1049     sFMextra*   extra   = MidiOutDev[wDevID].lpExtra;
1050     sVoice*     voice   = extra->voice;
1051     sChannel*   channel = extra->channel;
1052     int         i;
1053
1054     for (i = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1055         if (voice[i].status != sVS_UNUSED) {
1056             SEQ_STOP_NOTE(wDevID, i, voice[i].note, 64);
1057         }
1058         SEQ_KEY_PRESSURE(wDevID, i, 127, 0);
1059         SEQ_CONTROL(wDevID, i, SEQ_VOLMODE, VOL_METHOD_LINEAR);
1060         voice[i].note = 0;
1061         voice[i].channel = -1;
1062         voice[i].cntMark = 0;
1063         voice[i].status = sVS_UNUSED;
1064     }
1065     for (i = 0; i < 16; i++) {
1066         channel[i].program = 0;
1067         channel[i].bender = 8192;
1068         channel[i].benderRange = 2;
1069         channel[i].bank = 0;
1070         channel[i].volume = 127;
1071         channel[i].balance = 64;
1072         channel[i].expression = 0;
1073         channel[i].sustain = 0;
1074     }
1075     extra->counter = 0;
1076     extra->drumSetMask = 1 << 9; /* channel 10 is normally drums, sometimes 16 also */
1077     SEQ_DUMPBUF();
1078 }
1079
1080 #define         IS_DRUM_CHANNEL(_xtra, _chn)    ((_xtra)->drumSetMask & (1 << (_chn)))
1081
1082 /**************************************************************************
1083  *                              modGetDevCaps                   [internal]
1084  */
1085 static DWORD modGetDevCaps(WORD wDevID, LPMIDIOUTCAPSW lpCaps, DWORD dwSize)
1086 {
1087     TRACE("(%04X, %p, %08X);\n", wDevID, lpCaps, dwSize);
1088
1089     if (wDevID >= MODM_NumDevs) return MMSYSERR_BADDEVICEID;
1090     if (lpCaps == NULL)         return MMSYSERR_INVALPARAM;
1091
1092     memcpy(lpCaps, &MidiOutDev[wDevID].caps, min(dwSize, sizeof(*lpCaps)));
1093
1094     return MMSYSERR_NOERROR;
1095 }
1096
1097 /**************************************************************************
1098  *                      modOpen                                 [internal]
1099  */
1100 static DWORD modOpen(WORD wDevID, LPMIDIOPENDESC lpDesc, DWORD dwFlags)
1101 {
1102     TRACE("(%04X, %p, %08X);\n", wDevID, lpDesc, dwFlags);
1103     if (lpDesc == NULL) {
1104         WARN("Invalid Parameter !\n");
1105         return MMSYSERR_INVALPARAM;
1106     }
1107     if (wDevID >= MODM_NumDevs) {
1108         TRACE("MAX_MIDIOUTDRV reached !\n");
1109         return MMSYSERR_BADDEVICEID;
1110     }
1111     if (MidiOutDev[wDevID].midiDesc.hMidi != 0) {
1112         WARN("device already open !\n");
1113         return MMSYSERR_ALLOCATED;
1114     }
1115     if (!MidiOutDev[wDevID].bEnabled) {
1116         WARN("device disabled !\n");
1117         return MIDIERR_NODEVICE;
1118     }
1119     if ((dwFlags & ~CALLBACK_TYPEMASK) != 0) {
1120         WARN("bad dwFlags\n");
1121         return MMSYSERR_INVALFLAG;
1122     }
1123     if (!MidiOutDev[wDevID].bEnabled) {
1124         TRACE("disabled wDevID\n");
1125         return MMSYSERR_NOTENABLED;
1126     }
1127
1128     MidiOutDev[wDevID].lpExtra = 0;
1129
1130     switch (MidiOutDev[wDevID].caps.wTechnology) {
1131     case MOD_FMSYNTH:
1132         {
1133             void*       extra;
1134
1135             extra = HeapAlloc(GetProcessHeap(), 0,
1136                               sizeof(struct sFMextra) +
1137                               sizeof(struct sVoice) * (MidiOutDev[wDevID].caps.wVoices - 1));
1138
1139             if (extra == 0) {
1140                 WARN("can't alloc extra data !\n");
1141                 return MMSYSERR_NOMEM;
1142             }
1143             MidiOutDev[wDevID].lpExtra = extra;
1144             if (midiOpenSeq() < 0) {
1145                 MidiOutDev[wDevID].lpExtra = 0;
1146                 HeapFree(GetProcessHeap(), 0, extra);
1147                 return MMSYSERR_ERROR;
1148             }
1149             if (modFMLoad(wDevID) < 0) {
1150                 midiCloseSeq();
1151                 MidiOutDev[wDevID].lpExtra = 0;
1152                 HeapFree(GetProcessHeap(), 0, extra);
1153                 return MMSYSERR_ERROR;
1154             }
1155             modFMReset(wDevID);
1156         }
1157         break;
1158     case MOD_MIDIPORT:
1159     case MOD_SYNTH:
1160         if (midiOpenSeq() < 0) {
1161             return MMSYSERR_ALLOCATED;
1162         }
1163         break;
1164     default:
1165         WARN("Technology not supported (yet) %d !\n",
1166              MidiOutDev[wDevID].caps.wTechnology);
1167         return MMSYSERR_NOTENABLED;
1168     }
1169
1170     MidiOutDev[wDevID].wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
1171
1172     MidiOutDev[wDevID].lpQueueHdr = NULL;
1173     MidiOutDev[wDevID].dwTotalPlayed = 0;
1174     MidiOutDev[wDevID].bufsize = 0x3FFF;
1175     MidiOutDev[wDevID].midiDesc = *lpDesc;
1176
1177     if (MIDI_NotifyClient(wDevID, MOM_OPEN, 0L, 0L) != MMSYSERR_NOERROR) {
1178         WARN("can't notify client !\n");
1179         return MMSYSERR_INVALPARAM;
1180     }
1181     TRACE("Successful !\n");
1182     return MMSYSERR_NOERROR;
1183 }
1184
1185
1186 /**************************************************************************
1187  *                      modClose                                [internal]
1188  */
1189 static DWORD modClose(WORD wDevID)
1190 {
1191     int ret = MMSYSERR_NOERROR;
1192
1193     TRACE("(%04X);\n", wDevID);
1194
1195     if (MidiOutDev[wDevID].midiDesc.hMidi == 0) {
1196         WARN("device not opened !\n");
1197         return MMSYSERR_ERROR;
1198     }
1199     /* FIXME: should test that no pending buffer is still in the queue for
1200      * playing */
1201
1202     if (midiSeqFD == -1) {
1203         WARN("can't close !\n");
1204         return MMSYSERR_ERROR;
1205     }
1206
1207     switch (MidiOutDev[wDevID].caps.wTechnology) {
1208     case MOD_FMSYNTH:
1209     case MOD_MIDIPORT:
1210         midiCloseSeq();
1211         break;
1212     default:
1213         WARN("Technology not supported (yet) %d !\n",
1214              MidiOutDev[wDevID].caps.wTechnology);
1215         return MMSYSERR_NOTENABLED;
1216     }
1217
1218     HeapFree(GetProcessHeap(), 0, MidiOutDev[wDevID].lpExtra);
1219     MidiOutDev[wDevID].lpExtra = 0;
1220
1221     MidiOutDev[wDevID].bufsize = 0;
1222     if (MIDI_NotifyClient(wDevID, MOM_CLOSE, 0L, 0L) != MMSYSERR_NOERROR) {
1223         WARN("can't notify client !\n");
1224         ret = MMSYSERR_INVALPARAM;
1225     }
1226     MidiOutDev[wDevID].midiDesc.hMidi = 0;
1227     return ret;
1228 }
1229
1230 /**************************************************************************
1231  *                      modData                                 [internal]
1232  */
1233 static DWORD modData(WORD wDevID, DWORD dwParam)
1234 {
1235     WORD        evt = LOBYTE(LOWORD(dwParam));
1236     WORD        d1  = HIBYTE(LOWORD(dwParam));
1237     WORD        d2  = LOBYTE(HIWORD(dwParam));
1238
1239     TRACE("(%04X, %08X);\n", wDevID, dwParam);
1240
1241     if (wDevID >= MODM_NumDevs) return MMSYSERR_BADDEVICEID;
1242     if (!MidiOutDev[wDevID].bEnabled) return MIDIERR_NODEVICE;
1243
1244     if (midiSeqFD == -1) {
1245         WARN("can't play !\n");
1246         return MIDIERR_NODEVICE;
1247     }
1248     switch (MidiOutDev[wDevID].caps.wTechnology) {
1249     case MOD_FMSYNTH:
1250         /* FIXME:
1251          *      - chorus depth controller is not used
1252          */
1253         {
1254             sFMextra*   extra   = MidiOutDev[wDevID].lpExtra;
1255             sVoice*     voice   = extra->voice;
1256             sChannel*   channel = extra->channel;
1257             int         chn = (evt & 0x0F);
1258             int         i, nv;
1259
1260             switch (evt & 0xF0) {
1261             case MIDI_NOTEOFF:
1262                 for (i = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1263                                 /* don't stop sustained notes */
1264                     if (voice[i].status == sVS_PLAYING && voice[i].channel == chn && voice[i].note == d1) {
1265                         voice[i].status = sVS_UNUSED;
1266                         SEQ_STOP_NOTE(wDevID, i, d1, d2);
1267                     }
1268                 }
1269                 break;
1270             case MIDI_NOTEON:
1271                 if (d2 == 0) { /* note off if velocity == 0 */
1272                     for (i = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1273                         /* don't stop sustained notes */
1274                         if (voice[i].status == sVS_PLAYING && voice[i].channel == chn && voice[i].note == d1) {
1275                             voice[i].status = sVS_UNUSED;
1276                             SEQ_STOP_NOTE(wDevID, i, d1, 64);
1277                         }
1278                     }
1279                     break;
1280                 }
1281                 /* finding out in this order :
1282                  *      - an empty voice
1283                  *      - if replaying the same note on the same channel
1284                  *      - the older voice (LRU)
1285                  */
1286                 for (i = nv = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1287                     if (voice[i].status == sVS_UNUSED ||
1288                         (voice[i].note == d1 && voice[i].channel == chn)) {
1289                         nv = i;
1290                         break;
1291                     }
1292                     if (voice[i].cntMark < voice[0].cntMark) {
1293                         nv = i;
1294                     }
1295                 }
1296                 TRACE(
1297                       "playing on voice=%d, pgm=%d, pan=0x%02X, vol=0x%02X, "
1298                       "bender=0x%02X, note=0x%02X, vel=0x%02X\n",
1299                       nv, channel[chn].program,
1300                       channel[chn].balance,
1301                       channel[chn].volume,
1302                       channel[chn].bender, d1, d2);
1303
1304                 SEQ_SET_PATCH(wDevID, nv, IS_DRUM_CHANNEL(extra, chn) ?
1305                               (128 + d1) : channel[chn].program);
1306                 SEQ_BENDER_RANGE(wDevID, nv, channel[chn].benderRange * 100);
1307                 SEQ_BENDER(wDevID, nv, channel[chn].bender);
1308                 SEQ_CONTROL(wDevID, nv, CTL_PAN, channel[chn].balance);
1309                 SEQ_CONTROL(wDevID, nv, CTL_EXPRESSION, channel[chn].expression);
1310 #if 0
1311                 /* FIXME: does not really seem to work on my SB card and
1312                  * screws everything up... so lay it down
1313                  */
1314                 SEQ_CONTROL(wDevID, nv, CTL_MAIN_VOLUME, channel[chn].volume);
1315 #endif
1316                 SEQ_START_NOTE(wDevID, nv, d1, d2);
1317                 voice[nv].status = channel[chn].sustain ? sVS_SUSTAINED : sVS_PLAYING;
1318                 voice[nv].note = d1;
1319                 voice[nv].channel = chn;
1320                 voice[nv].cntMark = extra->counter++;
1321                 break;
1322             case MIDI_KEY_PRESSURE:
1323                 for (i = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1324                     if (voice[i].status != sVS_UNUSED && voice[i].channel == chn && voice[i].note == d1) {
1325                         SEQ_KEY_PRESSURE(wDevID, i, d1, d2);
1326                     }
1327                 }
1328                 break;
1329             case MIDI_CTL_CHANGE:
1330                 switch (d1) {
1331                 case CTL_BANK_SELECT:   channel[chn].bank = d2;         break;
1332                 case CTL_MAIN_VOLUME:   channel[chn].volume = d2;       break;
1333                 case CTL_PAN:           channel[chn].balance = d2;      break;
1334                 case CTL_EXPRESSION:    channel[chn].expression = d2;   break;
1335                 case CTL_SUSTAIN:       channel[chn].sustain = d2;
1336                     if (d2) {
1337                         for (i = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1338                             if (voice[i].status == sVS_PLAYING && voice[i].channel == chn) {
1339                                 voice[i].status = sVS_SUSTAINED;
1340                             }
1341                         }
1342                     } else {
1343                         for (i = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1344                             if (voice[i].status == sVS_SUSTAINED && voice[i].channel == chn) {
1345                                 voice[i].status = sVS_UNUSED;
1346                                 SEQ_STOP_NOTE(wDevID, i, voice[i].note, 64);
1347                             }
1348                         }
1349                     }
1350                     break;
1351                 case CTL_NONREG_PARM_NUM_LSB:   channel[chn].nrgPmtLSB = d2;    break;
1352                 case CTL_NONREG_PARM_NUM_MSB:   channel[chn].nrgPmtMSB = d2;    break;
1353                 case CTL_REGIST_PARM_NUM_LSB:   channel[chn].regPmtLSB = d2;    break;
1354                 case CTL_REGIST_PARM_NUM_MSB:   channel[chn].regPmtMSB = d2;    break;
1355                 case CTL_DATA_ENTRY:
1356                     switch ((channel[chn].regPmtMSB << 8) | channel[chn].regPmtLSB) {
1357                     case 0x0000:
1358                         if (channel[chn].benderRange != d2) {
1359                             channel[chn].benderRange = d2;
1360                             for (i = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1361                                 if (voice[i].channel == chn) {
1362                                     SEQ_BENDER_RANGE(wDevID, i, channel[chn].benderRange);
1363                                 }
1364                             }
1365                         }
1366                         break;
1367
1368                     case 0x7F7F:
1369                         channel[chn].benderRange = 2;
1370                         for (i = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1371                             if (voice[i].channel == chn) {
1372                                 SEQ_BENDER_RANGE(wDevID, i, channel[chn].benderRange);
1373                             }
1374                         }
1375                         break;
1376                     default:
1377                         TRACE("Data entry: regPmt=0x%02x%02x, nrgPmt=0x%02x%02x with %x\n",
1378                               channel[chn].regPmtMSB, channel[chn].regPmtLSB,
1379                               channel[chn].nrgPmtMSB, channel[chn].nrgPmtLSB,
1380                               d2);
1381                         break;
1382                     }
1383                     break;
1384
1385                 case 0x78: /* all sounds off */
1386                     /* FIXME: I don't know if I have to take care of the channel
1387                      * for this control ?
1388                      */
1389                     for (i = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1390                         if (voice[i].status != sVS_UNUSED && voice[i].channel == chn) {
1391                             voice[i].status = sVS_UNUSED;
1392                             SEQ_STOP_NOTE(wDevID, i, voice[i].note, 64);
1393                         }
1394                     }
1395                     break;
1396                 case 0x7B: /* all notes off */
1397                     /* FIXME: I don't know if I have to take care of the channel
1398                      * for this control ?
1399                      */
1400                     for (i = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1401                         if (voice[i].status == sVS_PLAYING && voice[i].channel == chn) {
1402                             voice[i].status = sVS_UNUSED;
1403                             SEQ_STOP_NOTE(wDevID, i, voice[i].note, 64);
1404                         }
1405                     }
1406                     break;
1407                 default:
1408                     TRACE("Dropping MIDI control event 0x%02x(%02x) on channel %d\n",
1409                           d1, d2, chn);
1410                     break;
1411                 }
1412                 break;
1413             case MIDI_PGM_CHANGE:
1414                 channel[chn].program = d1;
1415                 break;
1416             case MIDI_CHN_PRESSURE:
1417                 for (i = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1418                     if (voice[i].status != sVS_UNUSED && voice[i].channel == chn) {
1419                         SEQ_KEY_PRESSURE(wDevID, i, voice[i].note, d1);
1420                     }
1421                 }
1422                 break;
1423             case MIDI_PITCH_BEND:
1424                 channel[chn].bender = (d2 << 7) + d1;
1425                 for (i = 0; i < MidiOutDev[wDevID].caps.wVoices; i++) {
1426                     if (voice[i].channel == chn) {
1427                         SEQ_BENDER(wDevID, i, channel[chn].bender);
1428                     }
1429                 }
1430                 break;
1431             case MIDI_SYSTEM_PREFIX:
1432                 switch (evt & 0x0F) {
1433                 case 0x0F:      /* Reset */
1434                     modFMReset(wDevID);
1435                     break;
1436                 default:
1437                     WARN("Unsupported (yet) system event %02x\n", evt & 0x0F);
1438                 }
1439                 break;
1440             default:
1441                 WARN("Internal error, shouldn't happen (event=%08x)\n", evt & 0xF0);
1442                 return MMSYSERR_NOTENABLED;
1443             }
1444         }
1445         break;
1446     case MOD_MIDIPORT:
1447         {
1448             int dev = wDevID - MODM_NumFMSynthDevs;
1449             if (dev < 0) {
1450                 WARN("Internal error on devID (%u) !\n", wDevID);
1451                 return MIDIERR_NODEVICE;
1452             }
1453
1454             switch (evt & 0xF0) {
1455             case MIDI_NOTEOFF:
1456             case MIDI_NOTEON:
1457             case MIDI_KEY_PRESSURE:
1458             case MIDI_CTL_CHANGE:
1459             case MIDI_PITCH_BEND:
1460                 SEQ_MIDIOUT(dev, evt);
1461                 SEQ_MIDIOUT(dev, d1);
1462                 SEQ_MIDIOUT(dev, d2);
1463                 break;
1464             case MIDI_PGM_CHANGE:
1465             case MIDI_CHN_PRESSURE:
1466                 SEQ_MIDIOUT(dev, evt);
1467                 SEQ_MIDIOUT(dev, d1);
1468                 break;
1469             case MIDI_SYSTEM_PREFIX:
1470                 switch (evt & 0x0F) {
1471                 case 0x00:      /* System Exclusive, don't do it on modData,
1472                                  * should require modLongData*/
1473                 case 0x01:      /* Undefined */
1474                 case 0x04:      /* Undefined. */
1475                 case 0x05:      /* Undefined. */
1476                 case 0x07:      /* End of Exclusive. */
1477                 case 0x09:      /* Undefined. */
1478                 case 0x0D:      /* Undefined. */
1479                     break;
1480                 case 0x06:      /* Tune Request */
1481                 case 0x08:      /* Timing Clock. */
1482                 case 0x0A:      /* Start. */
1483                 case 0x0B:      /* Continue */
1484                 case 0x0C:      /* Stop */
1485                 case 0x0E:      /* Active Sensing. */
1486                     SEQ_MIDIOUT(dev, evt);
1487                     break;
1488                 case 0x0F:      /* Reset */
1489                                 /* SEQ_MIDIOUT(dev, evt);
1490                                    this other way may be better */
1491                     SEQ_MIDIOUT(dev, MIDI_SYSTEM_PREFIX);
1492                     SEQ_MIDIOUT(dev, 0x7e);
1493                     SEQ_MIDIOUT(dev, 0x7f);
1494                     SEQ_MIDIOUT(dev, 0x09);
1495                     SEQ_MIDIOUT(dev, 0x01);
1496                     SEQ_MIDIOUT(dev, 0xf7);
1497                     break;
1498                 case 0x03:      /* Song Select. */
1499                     SEQ_MIDIOUT(dev, evt);
1500                     SEQ_MIDIOUT(dev, d1);
1501                 case 0x02:      /* Song Position Pointer. */
1502                     SEQ_MIDIOUT(dev, evt);
1503                     SEQ_MIDIOUT(dev, d1);
1504                     SEQ_MIDIOUT(dev, d2);
1505                 }
1506                 break;
1507             }
1508         }
1509         break;
1510     default:
1511         WARN("Technology not supported (yet) %d !\n",
1512              MidiOutDev[wDevID].caps.wTechnology);
1513         return MMSYSERR_NOTENABLED;
1514     }
1515
1516     SEQ_DUMPBUF();
1517
1518     return MMSYSERR_NOERROR;
1519 }
1520
1521 /**************************************************************************
1522  *              modLongData                                     [internal]
1523  */
1524 static DWORD modLongData(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
1525 {
1526     int         count;
1527     LPBYTE      lpData;
1528
1529     TRACE("(%04X, %p, %08X);\n", wDevID, lpMidiHdr, dwSize);
1530
1531     /* Note: MS doc does not say much about the dwBytesRecorded member of the MIDIHDR structure
1532      * but it seems to be used only for midi input.
1533      * Taking a look at the WAVEHDR structure (which is quite similar) confirms this assumption.
1534      */
1535     
1536     if (wDevID >= MODM_NumDevs) return MMSYSERR_BADDEVICEID;
1537     if (!MidiOutDev[wDevID].bEnabled) return MIDIERR_NODEVICE;
1538
1539     if (midiSeqFD == -1) {
1540         WARN("can't play !\n");
1541         return MIDIERR_NODEVICE;
1542     }
1543
1544     lpData = (LPBYTE) lpMidiHdr->lpData;
1545
1546     if (lpData == NULL)
1547         return MIDIERR_UNPREPARED;
1548     if (!(lpMidiHdr->dwFlags & MHDR_PREPARED))
1549         return MIDIERR_UNPREPARED;
1550     if (lpMidiHdr->dwFlags & MHDR_INQUEUE)
1551         return MIDIERR_STILLPLAYING;
1552     lpMidiHdr->dwFlags &= ~MHDR_DONE;
1553     lpMidiHdr->dwFlags |= MHDR_INQUEUE;
1554
1555     /* FIXME: MS doc is not 100% clear. Will lpData only contain system exclusive
1556      * data, or can it also contain raw MIDI data, to be split up and sent to
1557      * modShortData() ?
1558      * If the latest is true, then the following WARNing will fire up
1559      */
1560     if (lpData[0] != 0xF0 || lpData[lpMidiHdr->dwBufferLength - 1] != 0xF7) {
1561         WARN("Alledged system exclusive buffer is not correct\n\tPlease report with MIDI file\n");
1562     }
1563
1564     TRACE("dwBufferLength=%u !\n", lpMidiHdr->dwBufferLength);
1565     TRACE("                 %02X %02X %02X ... %02X %02X %02X\n",
1566           lpData[0], lpData[1], lpData[2], lpData[lpMidiHdr->dwBufferLength-3],
1567           lpData[lpMidiHdr->dwBufferLength-2], lpData[lpMidiHdr->dwBufferLength-1]);
1568
1569     switch (MidiOutDev[wDevID].caps.wTechnology) {
1570     case MOD_FMSYNTH:
1571         /* FIXME: I don't think there is much to do here */
1572         break;
1573     case MOD_MIDIPORT:
1574         if (lpData[0] != 0xF0) {
1575             /* Send end of System Exclusive */
1576             SEQ_MIDIOUT(wDevID - MODM_NumFMSynthDevs, 0xF0);
1577             WARN("Adding missing 0xF0 marker at the beginning of "
1578                  "system exclusive byte stream\n");
1579         }
1580         for (count = 0; count < lpMidiHdr->dwBufferLength; count++) {
1581             SEQ_MIDIOUT(wDevID - MODM_NumFMSynthDevs, lpData[count]);
1582         }
1583         if (lpData[count - 1] != 0xF7) {
1584             /* Send end of System Exclusive */
1585             SEQ_MIDIOUT(wDevID - MODM_NumFMSynthDevs, 0xF7);
1586             WARN("Adding missing 0xF7 marker at the end of "
1587                  "system exclusive byte stream\n");
1588         }
1589         SEQ_DUMPBUF();
1590         break;
1591     default:
1592         WARN("Technology not supported (yet) %d !\n",
1593              MidiOutDev[wDevID].caps.wTechnology);
1594         return MMSYSERR_NOTENABLED;
1595     }
1596
1597     lpMidiHdr->dwFlags &= ~MHDR_INQUEUE;
1598     lpMidiHdr->dwFlags |= MHDR_DONE;
1599     if (MIDI_NotifyClient(wDevID, MOM_DONE, (DWORD_PTR)lpMidiHdr, 0L) != MMSYSERR_NOERROR) {
1600         WARN("can't notify client !\n");
1601         return MMSYSERR_INVALPARAM;
1602     }
1603     return MMSYSERR_NOERROR;
1604 }
1605
1606 /**************************************************************************
1607  *                      modPrepare                              [internal]
1608  */
1609 static DWORD modPrepare(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
1610 {
1611     TRACE("(%04X, %p, %08X);\n", wDevID, lpMidiHdr, dwSize);
1612
1613     if (midiSeqFD == -1) {
1614         WARN("can't prepare !\n");
1615         return MMSYSERR_NOTENABLED;
1616     }
1617
1618     /* MS doc says that dwFlags must be set to zero, but (kinda funny) MS mciseq drivers
1619      * asks to prepare MIDIHDR which dwFlags != 0.
1620      * So at least check for the inqueue flag
1621      */
1622     if (dwSize < sizeof(MIDIHDR) || lpMidiHdr == 0 ||
1623         lpMidiHdr->lpData == 0 || (lpMidiHdr->dwFlags & MHDR_INQUEUE) != 0) {
1624         WARN("%p %p %08x %d\n", lpMidiHdr, lpMidiHdr ? lpMidiHdr->lpData : NULL,
1625                    lpMidiHdr ? lpMidiHdr->dwFlags : 0, dwSize);
1626         return MMSYSERR_INVALPARAM;
1627     }
1628
1629     lpMidiHdr->lpNext = 0;
1630     lpMidiHdr->dwFlags |= MHDR_PREPARED;
1631     lpMidiHdr->dwFlags &= ~MHDR_DONE;
1632     return MMSYSERR_NOERROR;
1633 }
1634
1635 /**************************************************************************
1636  *                              modUnprepare                    [internal]
1637  */
1638 static DWORD modUnprepare(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
1639 {
1640     TRACE("(%04X, %p, %08X);\n", wDevID, lpMidiHdr, dwSize);
1641
1642     if (midiSeqFD == -1) {
1643         WARN("can't unprepare !\n");
1644         return MMSYSERR_NOTENABLED;
1645     }
1646
1647     if (dwSize < sizeof(MIDIHDR) || lpMidiHdr == 0)
1648         return MMSYSERR_INVALPARAM;
1649     if (lpMidiHdr->dwFlags & MHDR_INQUEUE)
1650         return MIDIERR_STILLPLAYING;
1651     lpMidiHdr->dwFlags &= ~MHDR_PREPARED;
1652     return MMSYSERR_NOERROR;
1653 }
1654
1655 /**************************************************************************
1656  *                      modReset                                [internal]
1657  */
1658 static DWORD modReset(WORD wDevID)
1659 {
1660     unsigned chn;
1661
1662     TRACE("(%04X);\n", wDevID);
1663
1664     if (wDevID >= MODM_NumDevs) return MMSYSERR_BADDEVICEID;
1665     if (!MidiOutDev[wDevID].bEnabled) return MIDIERR_NODEVICE;
1666
1667     /* stop all notes */
1668     /* FIXME: check if 0x78B0 is channel dependent or not. I coded it so that
1669      * it's channel dependent...
1670      */
1671     for (chn = 0; chn < 16; chn++) {
1672         /* turn off every note */
1673         modData(wDevID, 0x7800 | MIDI_CTL_CHANGE | chn);
1674         /* remove sustain on all channels */
1675         modData(wDevID, (CTL_SUSTAIN << 8) | MIDI_CTL_CHANGE | chn);
1676     }
1677     /* FIXME: the LongData buffers must also be returned to the app */
1678     return MMSYSERR_NOERROR;
1679 }
1680
1681 #else /* HAVE_OSS_MIDI */
1682
1683 LRESULT OSS_MidiInit(void)
1684 {
1685     TRACE("()\n");
1686     return FALSE;
1687 }
1688
1689 LRESULT OSS_MidiExit(void)
1690 {
1691     TRACE("()\n");
1692     return 0;
1693 }
1694
1695
1696 #endif /* HAVE_OSS_MIDI */
1697
1698 /*======================================================================*
1699  *                          MIDI entry points                           *
1700  *======================================================================*/
1701
1702 /**************************************************************************
1703  *                      midMessage (WINEOSS.@)
1704  */
1705 DWORD WINAPI OSS_midMessage(UINT wDevID, UINT wMsg, DWORD_PTR dwUser,
1706                             DWORD_PTR dwParam1, DWORD_PTR dwParam2)
1707 {
1708     TRACE("(%04X, %04X, %08lX, %08lX, %08lX);\n",
1709           wDevID, wMsg, dwUser, dwParam1, dwParam2);
1710     switch (wMsg) {
1711 #ifdef HAVE_OSS_MIDI
1712     case DRVM_INIT:
1713     case DRVM_EXIT:
1714     case DRVM_ENABLE:
1715     case DRVM_DISABLE:
1716         /* FIXME: Pretend this is supported */
1717         return 0;
1718     case MIDM_OPEN:
1719         return midOpen(wDevID, (LPMIDIOPENDESC)dwParam1, dwParam2);
1720     case MIDM_CLOSE:
1721         return midClose(wDevID);
1722     case MIDM_ADDBUFFER:
1723         return midAddBuffer(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1724     case MIDM_PREPARE:
1725         return midPrepare(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1726     case MIDM_UNPREPARE:
1727         return midUnprepare(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1728     case MIDM_GETDEVCAPS:
1729         return midGetDevCaps(wDevID, (LPMIDIINCAPSW)dwParam1,dwParam2);
1730     case MIDM_GETNUMDEVS:
1731         return MIDM_NumDevs;
1732     case MIDM_RESET:
1733         return midReset(wDevID);
1734     case MIDM_START:
1735         return midStart(wDevID);
1736     case MIDM_STOP:
1737         return midStop(wDevID);
1738 #endif
1739     default:
1740         TRACE("Unsupported message\n");
1741     }
1742     return MMSYSERR_NOTSUPPORTED;
1743 }
1744
1745 /**************************************************************************
1746  *                              modMessage (WINEOSS.@)
1747  */
1748 DWORD WINAPI OSS_modMessage(UINT wDevID, UINT wMsg, DWORD_PTR dwUser,
1749                             DWORD_PTR dwParam1, DWORD_PTR dwParam2)
1750 {
1751     TRACE("(%04X, %04X, %08lX, %08lX, %08lX);\n",
1752           wDevID, wMsg, dwUser, dwParam1, dwParam2);
1753
1754     switch (wMsg) {
1755 #ifdef HAVE_OSS_MIDI
1756     case DRVM_INIT:
1757     case DRVM_EXIT:
1758     case DRVM_ENABLE:
1759     case DRVM_DISABLE:
1760         /* FIXME: Pretend this is supported */
1761         return 0;
1762     case MODM_OPEN:
1763         return modOpen(wDevID, (LPMIDIOPENDESC)dwParam1, dwParam2);
1764     case MODM_CLOSE:
1765         return modClose(wDevID);
1766     case MODM_DATA:
1767         return modData(wDevID, dwParam1);
1768     case MODM_LONGDATA:
1769         return modLongData(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1770     case MODM_PREPARE:
1771         return modPrepare(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1772     case MODM_UNPREPARE:
1773         return modUnprepare(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1774     case MODM_GETDEVCAPS:
1775         return modGetDevCaps(wDevID, (LPMIDIOUTCAPSW)dwParam1, dwParam2);
1776     case MODM_GETNUMDEVS:
1777         return MODM_NumDevs;
1778     case MODM_GETVOLUME:
1779         return 0;
1780     case MODM_SETVOLUME:
1781         return 0;
1782     case MODM_RESET:
1783         return modReset(wDevID);
1784 #endif
1785     default:
1786         TRACE("Unsupported message\n");
1787     }
1788     return MMSYSERR_NOTSUPPORTED;
1789 }
1790
1791 /*-----------------------------------------------------------------------*/