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