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