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