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