1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
4 * Sample MIDI Wine Driver for Linux
6 * Copyright 1994 Martin Ayotte
11 * 98/7 changes for making this MIDI driver work on OSS
12 * current support is limited to MIDI ports of OSS systems
13 * 98/9 rewriting MCI code for MIDI
14 * 98/11 splitted in midi.c and mcimidi.c
23 #include <sys/ioctl.h>
24 #include "wine/winuser16.h"
26 #include "multimedia.h"
41 LPMIDIOPENDESC midiDesc;
46 unsigned char incoming[3];
47 unsigned char incPrev;
59 LPMIDIOPENDESC midiDesc;
64 void* lpExtra; /* according to port type (MIDI, FM...), extra data when needed */
68 static WINE_MIDIIN MidiInDev [MAX_MIDIINDRV ];
69 static WINE_MIDIOUT MidiOutDev[MAX_MIDIOUTDRV];
71 /* this is the total number of MIDI out devices found */
73 /* this is the number of FM synthetizers (index from 0 to
74 NUMFMSYNTHDEVS - 1) */
75 int MODM_NUMFMSYNTHDEVS = 0;
76 /* this is the number of Midi ports (index from NUMFMSYNTHDEVS to
77 NUMFMSYNTHDEVS + NUMMIDIDEVS - 1) */
78 int MODM_NUMMIDIDEVS = 0;
80 /* this is the total number of MIDI out devices found */
84 static int midiSeqFD = -1;
85 static int numOpenMidiSeq = 0;
86 static UINT32 midiInTimerID = 0;
87 static int numStartedMidiIn = 0;
90 /* this structure holds pointers with information for each MIDI
93 LPMIDIOUTCAPS16 midiOutDevices[MAX_MIDIOUTDRV];
95 /* this structure holds pointers with information for each MIDI
98 LPMIDIINCAPS16 midiInDevices [MAX_MIDIINDRV];
101 * FIXME : all tests on device ID for midXXX and modYYY are made against
102 * MAX_MIDIxxDRV (when they are made) but should be done against the actual
103 * number of midi devices found...
104 * check also when HAVE_OSS is defined that midiDesc is not NULL
107 /*======================================================================*
108 * Low level MIDI implemantation *
109 *======================================================================*/
110 #ifdef SNDCTL_MIDI_INFO
111 /**************************************************************************
112 * MIDI_NotifyClient [internal]
114 static DWORD MIDI_NotifyClient(UINT16 wDevID, WORD wMsg,
115 DWORD dwParam1, DWORD dwParam2)
122 TRACE(midi,"wDevID = %04X wMsg = %d dwParm1 = %04lX dwParam2 = %04lX\n",
123 wDevID, wMsg, dwParam1, dwParam2);
129 if (wDevID > MAX_MIDIOUTDRV)
130 return MCIERR_INTERNAL;
132 dwCallBack = MidiOutDev[wDevID].midiDesc->dwCallback;
133 uFlags = MidiOutDev[wDevID].wFlags;
134 hDev = MidiOutDev[wDevID].midiDesc->hMidi;
135 dwInstance = MidiOutDev[wDevID].midiDesc->dwInstance;
142 if (wDevID > MAX_MIDIINDRV)
143 return MCIERR_INTERNAL;
145 dwCallBack = MidiInDev[wDevID].midiDesc->dwCallback;
146 uFlags = MidiInDev[wDevID].wFlags;
147 hDev = MidiInDev[wDevID].midiDesc->hMidi;
148 dwInstance = MidiInDev[wDevID].midiDesc->dwInstance;
151 WARN(midi, "Unsupported MSW-MIDI message %u\n", wMsg);
152 return MCIERR_INTERNAL;
155 return DriverCallback(dwCallBack, uFlags, hDev, wMsg, dwInstance, dwParam1, dwParam2) ?
160 /**************************************************************************
161 * midiOpenSeq [internal]
163 static int midiOpenSeq(void)
165 if (numOpenMidiSeq == 0) {
166 midiSeqFD = open(MIDI_SEQ, O_RDWR, 0);
167 if (midiSeqFD == -1) {
168 ERR(midi, "can't open '%s' ! (%d)\n", MIDI_SEQ, errno);
171 if (fcntl(midiSeqFD, F_SETFL, O_NONBLOCK) < 0) {
172 WARN(midi, "can't set sequencer fd to non blocking (%d)\n", errno);
177 ioctl(midiSeqFD, SNDCTL_SEQ_RESET);
183 /**************************************************************************
184 * midiCloseSeq [internal]
186 static int midiCloseSeq(void)
188 if (--numOpenMidiSeq == 0) {
195 /* FIXME: this is a bad idea, it's even not static... */
198 /* FIXME: this is not reentrant, not static - because of global variable
200 /**************************************************************************
201 * seqbuf_dump [internal]
203 void seqbuf_dump(void)
206 if (write(midiSeqFD, _seqbuf, _seqbufptr) == -1) {
207 WARN(midi, "Can't write data to sequencer (%d/%d)!\n",
211 * in any case buffer is lost so that if many errors occur the buffer
217 #endif /* HAVE_OSS */
221 static void midReceiveChar(WORD wDevID, unsigned char value, DWORD dwTime)
225 TRACE(midi, "Adding %02xh to %d[%d]\n", value, wDevID, MidiInDev[wDevID].incLen);
227 if (wDevID >= MAX_MIDIINDRV) {
228 WARN(midi, "bad devID\n");
231 if (MidiInDev[wDevID].state == 0) {
232 TRACE(midi, "input not started, thrown away\n");
236 if (MidiInDev[wDevID].state & 2) { /* system exclusive */
237 LPMIDIHDR ptr = MidiInDev[wDevID].lpQueueHdr;
241 ((LPSTR)(ptr->reserved))[ptr->dwBytesRecorded++] = value;
242 if (ptr->dwBytesRecorded == ptr->dwBufferLength) {
246 if (value == 0xF7) { /* then end */
247 MidiInDev[wDevID].state &= ~2;
250 if (sbfb && ptr != NULL) {
251 ptr = MidiInDev[wDevID].lpQueueHdr;
252 ptr->dwFlags &= ~MHDR_INQUEUE;
253 ptr->dwFlags |= MHDR_DONE;
254 MidiInDev[wDevID].lpQueueHdr = (LPMIDIHDR)ptr->lpNext;
255 if (MIDI_NotifyClient(wDevID, MIM_LONGDATA, (DWORD)ptr, dwTime) != MMSYSERR_NOERROR) {
256 WARN(midi, "Couldn't notify client\n");
262 #define IS_CMD(_x) (((_x) & 0x80) == 0x80)
263 #define IS_SYS_CMD(_x) (((_x) & 0xF0) == 0xF0)
265 if (!IS_CMD(value) && MidiInDev[wDevID].incLen == 0) { /* try to reuse old cmd */
266 if (IS_CMD(MidiInDev[wDevID].incPrev) && !IS_SYS_CMD(MidiInDev[wDevID].incPrev)) {
267 MidiInDev[wDevID].incoming[0] = MidiInDev[wDevID].incPrev;
268 MidiInDev[wDevID].incLen = 1;
269 TRACE(midi, "Reusing old command %02xh\n", MidiInDev[wDevID].incPrev);
271 FIXME(midi, "error for midi-in, should generate MIM_ERROR notification:"
272 " prev=%02Xh, incLen=%02Xh\n",
273 MidiInDev[wDevID].incPrev, MidiInDev[wDevID].incLen);
277 MidiInDev[wDevID].incoming[(int)(MidiInDev[wDevID].incLen++)] = value;
278 if (MidiInDev[wDevID].incLen == 1 && !IS_SYS_CMD(MidiInDev[wDevID].incoming[0])) {
279 /* store new cmd, just in case */
280 MidiInDev[wDevID].incPrev = MidiInDev[wDevID].incoming[0];
284 #undef IS_SYS_CMD(_x)
286 switch (MidiInDev[wDevID].incoming[0] & 0xF0) {
289 case MIDI_KEY_PRESSURE:
290 case MIDI_CTL_CHANGE:
291 case MIDI_PITCH_BEND:
292 if (MidiInDev[wDevID].incLen == 3) {
293 toSend = (MidiInDev[wDevID].incoming[2] << 16) |
294 (MidiInDev[wDevID].incoming[1] << 8) |
295 (MidiInDev[wDevID].incoming[0] << 0);
298 case MIDI_PGM_CHANGE:
299 case MIDI_CHN_PRESSURE:
300 if (MidiInDev[wDevID].incLen == 2) {
301 toSend = (MidiInDev[wDevID].incoming[1] << 8) |
302 (MidiInDev[wDevID].incoming[0] << 0);
305 case MIDI_SYSTEM_PREFIX:
306 if (MidiInDev[wDevID].incoming[0] == 0xF0) {
307 MidiInDev[wDevID].state |= 2;
308 MidiInDev[wDevID].incLen = 0;
310 if (MidiInDev[wDevID].incLen == 1) {
311 toSend = (MidiInDev[wDevID].incoming[0] << 0);
316 WARN(midi, "This shouldn't happen (%02X)\n", MidiInDev[wDevID].incoming[0]);
319 TRACE(midi, "Sending event %08lx\n", toSend);
320 MidiInDev[wDevID].incLen = 0;
321 dwTime -= MidiInDev[wDevID].startTime;
322 if (MIDI_NotifyClient(wDevID, MIM_DATA, toSend, dwTime) != MMSYSERR_NOERROR) {
323 WARN(midi, "Couldn't notify client\n");
328 static VOID WINAPI midTimeCallback(HWND32 hwnd, UINT32 msg, UINT32 id, DWORD dwTime)
330 unsigned char buffer[256];
333 TRACE(midi, "(%04X, %d, %d, %lu)\n", hwnd, msg, id, dwTime);
335 len = read(midiSeqFD, buffer, sizeof(buffer));
337 if ((len % 4) != 0) {
338 WARN(midi, "bad length %d (%d)\n", len, errno);
342 for (idx = 0; idx < len; ) {
343 if (buffer[idx] & 0x80) {
345 "reading<8> %02x %02x %02x %02x %02x %02x %02x %02x\n",
346 buffer[idx + 0], buffer[idx + 1],
347 buffer[idx + 2], buffer[idx + 3],
348 buffer[idx + 4], buffer[idx + 5],
349 buffer[idx + 6], buffer[idx + 7]);
352 switch (buffer[idx + 0]) {
357 midReceiveChar(buffer[idx + 2], buffer[idx + 1], dwTime);
360 TRACE(midi, "Unsupported event %d\n", buffer[idx + 0]);
367 #endif /* HAVE_OSS */
369 /**************************************************************************
370 * midGetDevCaps [internal]
372 static DWORD midGetDevCaps(WORD wDevID, LPMIDIINCAPS16 lpCaps, DWORD dwSize)
374 LPMIDIINCAPS16 tmplpCaps;
376 TRACE(midi, "(%04X, %p, %08lX);\n", wDevID, lpCaps, dwSize);
378 if (wDevID >= MIDM_NUMDEVS) {
379 return MMSYSERR_BADDEVICEID;
381 if (lpCaps == NULL) {
382 return MMSYSERR_INVALPARAM;
385 tmplpCaps = midiInDevices[wDevID];
386 lpCaps->wMid = tmplpCaps->wMid;
387 lpCaps->wPid = tmplpCaps->wPid;
388 lpCaps->vDriverVersion = tmplpCaps->vDriverVersion;
389 strcpy(lpCaps->szPname, tmplpCaps->szPname);
390 if (dwSize == sizeof(MIDIINCAPS16)) {
391 /* we should run win 95, so make use of dwSupport */
392 lpCaps->dwSupport = tmplpCaps->dwSupport;
393 } else if (dwSize != sizeof(MIDIINCAPS16) - sizeof(DWORD)) {
394 TRACE(midi, "bad size for lpCaps\n");
395 return MMSYSERR_INVALPARAM;
398 return MMSYSERR_NOERROR;
401 /**************************************************************************
404 static DWORD midOpen(WORD wDevID, LPMIDIOPENDESC lpDesc, DWORD dwFlags)
406 TRACE(midi, "(%04X, %p, %08lX);\n", wDevID, lpDesc, dwFlags);
408 if (lpDesc == NULL) {
409 WARN(midi, "Invalid Parameter !\n");
410 return MMSYSERR_INVALPARAM;
413 * how to check that content of lpDesc is correct ?
415 if (wDevID >= MAX_MIDIINDRV) {
416 WARN(midi,"wDevID too large (%u) !\n", wDevID);
417 return MMSYSERR_BADDEVICEID;
419 if (MidiInDev[wDevID].midiDesc != 0) {
420 WARN(midi, "device already open !\n");
421 return MMSYSERR_ALLOCATED;
423 if ((dwFlags & ~CALLBACK_TYPEMASK) != 0) {
424 FIXME(midi, "No support for MIDI_IO_STATUS in dwFlags\n");
425 return MMSYSERR_INVALFLAG;
429 if (midiOpenSeq() < 0) {
430 return MMSYSERR_ERROR;
433 if (numStartedMidiIn++ == 0) {
434 midiInTimerID = SetTimer32(0, 0, 250, midTimeCallback);
435 if (!midiInTimerID) {
436 numStartedMidiIn = 0;
437 WARN(midi, "Couldn't start timer for midi-in\n");
439 return MMSYSERR_ERROR;
441 TRACE(midi, "Starting timer (%u) for midi-in\n", midiInTimerID);
445 int midi = open(MIDI_DEV, O_WRONLY, 0);
447 MidiInDev[wDevID].unixdev = 0;
449 WARN(midi,"can't open '%s' (%d)!\n", MIDI_DEV, errno);
450 return MMSYSERR_ALLOCATED;
452 MidiInDev[wDevID].unixdev = midi;
454 #endif /* HAVE_OSS */
456 MidiInDev[wDevID].wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
457 switch (MidiInDev[wDevID].wFlags) {
459 TRACE(midi,"CALLBACK_NULL !\n");
462 TRACE(midi, "CALLBACK_WINDOW !\n");
465 TRACE(midi, "CALLBACK_TASK !\n");
468 TRACE(midi, "CALLBACK_FUNCTION (%08lX)!\n", lpDesc->dwCallback);
471 MidiInDev[wDevID].lpQueueHdr = NULL;
472 MidiInDev[wDevID].dwTotalPlayed = 0;
473 MidiInDev[wDevID].bufsize = 0x3FFF;
474 MidiInDev[wDevID].midiDesc = lpDesc;
475 MidiInDev[wDevID].state = 0;
477 MidiInDev[wDevID].incLen = 0;
478 MidiInDev[wDevID].startTime = 0;
479 #endif /* HAVE_OSS */
480 if (MIDI_NotifyClient(wDevID, MIM_OPEN, 0L, 0L) != MMSYSERR_NOERROR) {
481 WARN(midi,"can't notify client !\n");
482 return MMSYSERR_INVALPARAM;
484 return MMSYSERR_NOERROR;
487 /**************************************************************************
488 * midClose [internal]
490 static DWORD midClose(WORD wDevID)
492 int ret = MMSYSERR_NOERROR;
494 TRACE(midi, "(%04X);\n", wDevID);
496 if (wDevID >= MAX_MIDIINDRV) {
497 WARN(midi,"wDevID too bif (%u) !\n", wDevID);
498 return MMSYSERR_BADDEVICEID;
500 if (MidiInDev[wDevID].midiDesc == 0) {
501 WARN(midi, "device not opened !\n");
502 return MMSYSERR_ERROR;
504 if (MidiInDev[wDevID].lpQueueHdr != 0) {
505 return MIDIERR_STILLPLAYING;
509 if (midiSeqFD == -1) {
510 WARN(midi,"ooops !\n");
511 return MMSYSERR_ERROR;
513 if (--numStartedMidiIn == 0) {
514 TRACE(midi, "Stopping timer for midi-in\n");
515 if (!KillTimer32(0, midiInTimerID)) {
516 WARN(midi, "Couldn't stop timer for midi-in\n");
522 if (MidiInDev[wDevID].unixdev == 0) {
523 WARN(midi,"ooops !\n");
524 return MMSYSERR_ERROR;
526 close(MidiInDev[wDevID].unixdev);
527 MidiInDev[wDevID].unixdev = 0;
528 #endif /* HAVE_OSS */
529 MidiInDev[wDevID].bufsize = 0;
530 if (MIDI_NotifyClient(wDevID, MIM_CLOSE, 0L, 0L) != MMSYSERR_NOERROR) {
531 WARN(midi,"can't notify client !\n");
532 ret = MMSYSERR_INVALPARAM;
534 MidiInDev[wDevID].midiDesc = 0;
538 /**************************************************************************
539 * midAddBuffer [internal]
541 static DWORD midAddBuffer(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
543 TRACE(midi, "(%04X, %p, %08lX);\n", wDevID, lpMidiHdr, dwSize);
545 if (lpMidiHdr == NULL) return MMSYSERR_INVALPARAM;
546 if (sizeof(MIDIHDR) != dwSize) return MMSYSERR_INVALPARAM;
547 if (lpMidiHdr->dwBufferLength == 0) return MMSYSERR_INVALPARAM;
548 if (lpMidiHdr->dwFlags & MHDR_INQUEUE) return MIDIERR_STILLPLAYING;
549 if (!(lpMidiHdr->dwFlags & MHDR_PREPARED)) return MIDIERR_UNPREPARED;
551 if (MidiInDev[wDevID].lpQueueHdr == 0) {
552 MidiInDev[wDevID].lpQueueHdr = lpMidiHdr;
556 for (ptr = MidiInDev[wDevID].lpQueueHdr;
558 ptr = (LPMIDIHDR)ptr->lpNext);
559 ptr->lpNext = (struct midihdr_tag*)lpMidiHdr;
561 return MMSYSERR_NOERROR;
564 /**************************************************************************
565 * midPrepare [internal]
567 static DWORD midPrepare(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
569 TRACE(midi, "(%04X, %p, %08lX);\n", wDevID, lpMidiHdr, dwSize);
571 if (dwSize != sizeof(MIDIHDR) || lpMidiHdr == 0 ||
572 lpMidiHdr->lpData == 0 || lpMidiHdr->dwFlags != 0 ||
573 lpMidiHdr->dwBufferLength >= 0x10000ul)
574 return MMSYSERR_INVALPARAM;
576 lpMidiHdr->lpNext = 0;
577 lpMidiHdr->dwFlags |= MHDR_PREPARED;
578 lpMidiHdr->dwBytesRecorded = 0;
580 return MMSYSERR_NOERROR;
583 /**************************************************************************
584 * midUnprepare [internal]
586 static DWORD midUnprepare(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
588 TRACE(midi, "(%04X, %p, %08lX);\n", wDevID, lpMidiHdr, dwSize);
590 if (dwSize != sizeof(MIDIHDR) || lpMidiHdr == 0 ||
591 lpMidiHdr->lpData == 0 || lpMidiHdr->dwBufferLength >= 0x10000ul)
592 return MMSYSERR_INVALPARAM;
594 if (!(lpMidiHdr->dwFlags & MHDR_PREPARED)) return MIDIERR_UNPREPARED;
595 if (lpMidiHdr->dwFlags & MHDR_INQUEUE) return MIDIERR_STILLPLAYING;
597 lpMidiHdr->dwFlags &= ~MHDR_PREPARED;
599 return MMSYSERR_NOERROR;
602 /**************************************************************************
603 * midReset [internal]
605 static DWORD midReset(WORD wDevID)
607 DWORD dwTime = GetTickCount();
609 TRACE(midi, "(%04X);\n", wDevID);
611 while (MidiInDev[wDevID].lpQueueHdr) {
612 MidiInDev[wDevID].lpQueueHdr->dwFlags &= ~MHDR_INQUEUE;
613 MidiInDev[wDevID].lpQueueHdr->dwFlags |= MHDR_DONE;
614 if (MIDI_NotifyClient(wDevID, MIM_LONGDATA,
615 (DWORD)MidiInDev[wDevID].lpQueueHdr, dwTime) != MMSYSERR_NOERROR) {
616 WARN(midi, "Couldn't notify client\n");
618 MidiInDev[wDevID].lpQueueHdr = (LPMIDIHDR)MidiInDev[wDevID].lpQueueHdr->lpNext;
621 return MMSYSERR_NOERROR;
625 /**************************************************************************
626 * midStart [internal]
628 static DWORD midStart(WORD wDevID)
630 TRACE(midi, "(%04X);\n", wDevID);
632 /* FIXME : should test value of wDevID */
635 MidiInDev[wDevID].state = 1;
636 MidiInDev[wDevID].startTime = GetTickCount();
637 return MMSYSERR_NOERROR;
639 return MMSYSERR_NOTENABLED;
640 #endif /* HAVE_OSS */
643 /**************************************************************************
646 static DWORD midStop(WORD wDevID)
648 TRACE(midi, "(%04X);\n", wDevID);
650 /* FIXME : should test value of wDevID */
653 MidiInDev[wDevID].state = 0;
654 return MMSYSERR_NOERROR;
656 return MMSYSERR_NOTENABLED;
657 #endif /* HAVE_OSS */
660 /**************************************************************************
661 * midMessage [sample driver]
663 DWORD WINAPI midMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
664 DWORD dwParam1, DWORD dwParam2)
666 TRACE(midi, "(%04X, %04X, %08lX, %08lX, %08lX);\n",
667 wDevID, wMsg, dwUser, dwParam1, dwParam2);
670 return midOpen(wDevID,(LPMIDIOPENDESC)dwParam1, dwParam2);
672 return midClose(wDevID);
674 return midAddBuffer(wDevID,(LPMIDIHDR)dwParam1, dwParam2);
676 return midPrepare(wDevID,(LPMIDIHDR)dwParam1, dwParam2);
678 return midUnprepare(wDevID,(LPMIDIHDR)dwParam1, dwParam2);
679 case MIDM_GETDEVCAPS:
680 return midGetDevCaps(wDevID,(LPMIDIINCAPS16)dwParam1,dwParam2);
681 case MIDM_GETNUMDEVS:
684 return midReset(wDevID);
686 return midStart(wDevID);
688 return midStop(wDevID);
690 TRACE(midi, "Unsupported message\n");
692 return MMSYSERR_NOTSUPPORTED;
695 /*-----------------------------------------------------------------------*/
699 typedef struct sVoice {
700 int note; /* 0 means not used */
702 unsigned cntMark : 30,
705 #define sVS_PLAYING 1
706 #define sVS_SUSTAINED 2
709 typedef struct sChannel {
715 int bank; /* CTL_BANK_SELECT */
716 int volume; /* CTL_MAIN_VOLUME */
717 int balance; /* CTL_BALANCE */
718 int expression; /* CTL_EXPRESSION */
719 int sustain; /* CTL_SUSTAIN */
721 unsigned char nrgPmtMSB; /* Non register Parameters */
722 unsigned char nrgPmtLSB;
723 unsigned char regPmtMSB; /* Non register Parameters */
724 unsigned char regPmtLSB;
727 typedef struct sFMextra {
730 sChannel channel[16]; /* MIDI has only 16 channels */
731 sVoice voice[1]; /* dyn allocated according to sound card */
732 /* do not append fields below voice[1] since the size of this structure
733 * depends on the number of available voices on the FM synth...
737 extern unsigned char midiFMInstrumentPatches[16 * 128];
738 extern unsigned char midiFMDrumsPatches [16 * 128];
740 /**************************************************************************
741 * modFMLoad [internal]
743 static int modFMLoad(int dev)
746 struct sbi_instrument sbi;
751 memset(sbi.operators + 16, 0, 16);
752 for (i = 0; i < 128; i++) {
754 memcpy(sbi.operators, midiFMInstrumentPatches + i * 16, 16);
756 if (write(midiSeqFD, (char*)&sbi, sizeof(sbi)) == -1) {
757 WARN(midi, "Couldn't write patch for instrument %d (%d)!\n", sbi.channel, errno);
761 for (i = 0; i < 128; i++) {
762 sbi.channel = 128 + i;
763 memcpy(sbi.operators, midiFMDrumsPatches + i * 16, 16);
765 if (write(midiSeqFD, (char*)&sbi, sizeof(sbi)) == -1) {
766 WARN(midi, "Couldn't write patch for drum %d (%d)!\n", sbi.channel, errno);
773 /**************************************************************************
774 * modFMReset [internal]
776 static void modFMReset(WORD wDevID)
778 sFMextra* extra = (sFMextra*)MidiOutDev[wDevID].lpExtra;
779 sVoice* voice = extra->voice;
780 sChannel* channel = extra->channel;
783 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
784 if (voice[i].status != sVS_UNUSED) {
785 SEQ_STOP_NOTE(wDevID, i, voice[i].note, 64);
787 SEQ_KEY_PRESSURE(wDevID, i, 127, 0);
788 SEQ_CONTROL(wDevID, i, SEQ_VOLMODE, VOL_METHOD_LINEAR);
790 voice[i].channel = -1;
791 voice[i].cntMark = 0;
792 voice[i].status = sVS_UNUSED;
794 for (i = 0; i < 16; i++) {
795 channel[i].program = 0;
796 channel[i].bender = 8192;
797 channel[i].benderRange = 2;
799 channel[i].volume = 127;
800 channel[i].balance = 64;
801 channel[i].expression = 0;
802 channel[i].sustain = 0;
805 extra->drumSetMask = 1 << 9; /* channel 10 is normally drums, sometimes 16 also */
809 #define IS_DRUM_CHANNEL(_xtra, _chn) ((_xtra)->drumSetMask & (1 << (_chn)))
811 #endif /* HAVE_OSS */
813 /**************************************************************************
814 * modGetDevCaps [internal]
816 static DWORD modGetDevCaps(WORD wDevID, LPMIDIOUTCAPS16 lpCaps,
819 TRACE(midi, "(%04X, %p, %08lX);\n", wDevID, lpCaps, dwSize);
820 if (wDevID == (WORD) MIDI_MAPPER) {
821 lpCaps->wMid = 0x00FF; /* Manufac ID */
822 lpCaps->wPid = 0x0001; /* Product ID */
823 lpCaps->vDriverVersion = 0x001; /* Product Version */
824 strcpy(lpCaps->szPname, "MIDI Mapper (not functional yet)");
825 /* FIXME Does it make any difference ? */
826 lpCaps->wTechnology = MOD_FMSYNTH;
827 lpCaps->wVoices = 14; /* FIXME */
828 lpCaps->wNotes = 14; /* FIXME */
829 /* FIXME Does it make any difference ? */
830 lpCaps->dwSupport = MIDICAPS_VOLUME|MIDICAPS_LRVOLUME;
832 LPMIDIOUTCAPS16 tmplpCaps;
834 if (wDevID >= MODM_NUMDEVS) {
835 TRACE(midi, "MAX_MIDIOUTDRV reached !\n");
836 return MMSYSERR_BADDEVICEID;
838 /* FIXME There is a way to do it so easily, but I'm too
839 * sleepy to think and I want to test
842 tmplpCaps = midiOutDevices[wDevID];
843 lpCaps->wMid = tmplpCaps->wMid;
844 lpCaps->wPid = tmplpCaps->wPid;
845 lpCaps->vDriverVersion = tmplpCaps->vDriverVersion;
846 strcpy(lpCaps->szPname, tmplpCaps->szPname);
847 lpCaps->wTechnology = tmplpCaps->wTechnology;
848 lpCaps->wVoices = tmplpCaps->wVoices;
849 lpCaps->wNotes = tmplpCaps->wNotes;
850 lpCaps->dwSupport = tmplpCaps->dwSupport;
852 return MMSYSERR_NOERROR;
855 /**************************************************************************
858 static DWORD modOpen(WORD wDevID, LPMIDIOPENDESC lpDesc, DWORD dwFlags)
860 TRACE(midi, "(%04X, %p, %08lX);\n", wDevID, lpDesc, dwFlags);
861 if (lpDesc == NULL) {
862 WARN(midi, "Invalid Parameter !\n");
863 return MMSYSERR_INVALPARAM;
865 if (wDevID >= MAX_MIDIOUTDRV) {
866 TRACE(midi,"MAX_MIDIOUTDRV reached !\n");
867 return MMSYSERR_BADDEVICEID;
869 if (MidiOutDev[wDevID].midiDesc != 0) {
870 WARN(midi, "device already open !\n");
871 return MMSYSERR_ALLOCATED;
873 if ((dwFlags & ~CALLBACK_TYPEMASK) != 0) {
874 WARN(midi, "bad dwFlags\n");
875 return MMSYSERR_INVALFLAG;
877 if (midiOutDevices[wDevID] == NULL) {
878 TRACE(midi, "un-allocated wDevID\n");
879 return MMSYSERR_BADDEVICEID;
883 MidiOutDev[wDevID].lpExtra = 0;
885 switch (midiOutDevices[wDevID]->wTechnology) {
888 void* extra = xmalloc(sizeof(struct sFMextra) +
889 sizeof(struct sVoice) *
890 (midiOutDevices[wDevID]->wVoices - 1));
893 WARN(midi, "can't alloc extra data !\n");
894 return MMSYSERR_NOMEM;
896 MidiOutDev[wDevID].lpExtra = extra;
897 if (midiOpenSeq() < 0) {
898 MidiOutDev[wDevID].lpExtra = 0;
900 return MMSYSERR_ERROR;
902 if (modFMLoad(wDevID) < 0) {
904 MidiOutDev[wDevID].lpExtra = 0;
906 return MMSYSERR_ERROR;
912 if (midiOpenSeq() < 0) {
913 return MMSYSERR_ALLOCATED;
917 WARN(midi,"Technology not supported (yet) %d !\n",
918 midiOutDevices[wDevID]->wTechnology);
919 return MMSYSERR_NOTENABLED;
923 int midi = open (MIDI_DEV, O_WRONLY, 0);
924 MidiOutDev[wDevID].unixdev = 0;
926 WARN(midi, "can't open !\n");
927 return MMSYSERR_ALLOCATED;
929 MidiOutDev[wDevID].unixdev = midi;
931 #endif /* HAVE_OSS */
933 MidiOutDev[wDevID].wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
934 switch (MidiOutDev[wDevID].wFlags) {
936 TRACE(midi,"CALLBACK_NULL !\n");
939 TRACE(midi, "CALLBACK_WINDOW !\n");
942 TRACE(midi, "CALLBACK_TASK !\n");
945 TRACE(midi, "CALLBACK_FUNCTION !\n");
948 MidiOutDev[wDevID].lpQueueHdr = NULL;
949 MidiOutDev[wDevID].dwTotalPlayed = 0;
950 MidiOutDev[wDevID].bufsize = 0x3FFF;
951 MidiOutDev[wDevID].midiDesc = lpDesc;
953 if (MIDI_NotifyClient(wDevID, MOM_OPEN, 0L, 0L) != MMSYSERR_NOERROR) {
954 WARN(midi,"can't notify client !\n");
955 return MMSYSERR_INVALPARAM;
957 TRACE(midi, "Succesful !\n");
958 return MMSYSERR_NOERROR;
962 /**************************************************************************
963 * modClose [internal]
965 static DWORD modClose(WORD wDevID)
967 int ret = MMSYSERR_NOERROR;
969 TRACE(midi, "(%04X);\n", wDevID);
971 if (MidiOutDev[wDevID].midiDesc == 0) {
972 WARN(midi, "device not opened !\n");
973 return MMSYSERR_ERROR;
975 /* FIXME: should test that no pending buffer is still in the queue for
979 if (midiSeqFD == -1) {
980 WARN(midi,"can't close !\n");
981 return MMSYSERR_ERROR;
984 switch (midiOutDevices[wDevID]->wTechnology) {
990 WARN(midi,"Technology not supported (yet) %d !\n",
991 midiOutDevices[wDevID]->wTechnology);
992 return MMSYSERR_NOTENABLED;
995 if (MidiOutDev[wDevID].lpExtra != 0) {
996 free(MidiOutDev[wDevID].lpExtra);
997 MidiOutDev[wDevID].lpExtra = 0;
1000 if (MidiOutDev[wDevID].unixdev == 0) {
1001 WARN(midi,"can't close !\n");
1002 return MMSYSERR_NOTENABLED;
1004 close(MidiOutDev[wDevID].unixdev);
1005 MidiOutDev[wDevID].unixdev = 0;
1006 #endif /* HAVE_OSS */
1008 MidiOutDev[wDevID].bufsize = 0;
1009 if (MIDI_NotifyClient(wDevID, MOM_CLOSE, 0L, 0L) != MMSYSERR_NOERROR) {
1010 WARN(midi,"can't notify client !\n");
1011 ret = MMSYSERR_INVALPARAM;
1013 MidiOutDev[wDevID].midiDesc = 0;
1017 /**************************************************************************
1018 * modData [internal]
1020 static DWORD modData(WORD wDevID, DWORD dwParam)
1022 WORD evt = LOBYTE(LOWORD(dwParam));
1023 WORD d1 = HIBYTE(LOWORD(dwParam));
1024 WORD d2 = LOBYTE(HIWORD(dwParam));
1026 TRACE(midi, "(%04X, %08lX);\n", wDevID, dwParam);
1029 if (midiSeqFD == -1) {
1030 WARN(midi,"can't play !\n");
1031 return MIDIERR_NODEVICE;
1033 switch (midiOutDevices[wDevID]->wTechnology) {
1036 * - chorus depth controller is not used
1039 sFMextra* extra = (sFMextra*)MidiOutDev[wDevID].lpExtra;
1040 sVoice* voice = extra->voice;
1041 sChannel* channel = extra->channel;
1042 int chn = (evt & 0x0F);
1045 switch (evt & 0xF0) {
1047 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1048 /* don't stop sustained notes */
1049 if (voice[i].status == sVS_PLAYING && voice[i].channel == chn && voice[i].note == d1) {
1050 voice[i].status = sVS_UNUSED;
1051 SEQ_STOP_NOTE(wDevID, i, d1, d2);
1056 if (d2 == 0) { /* note off if velocity == 0 */
1057 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1058 /* don't stop sustained notes */
1059 if (voice[i].status == sVS_PLAYING && voice[i].channel == chn && voice[i].note == d1) {
1060 voice[i].status = sVS_UNUSED;
1061 SEQ_STOP_NOTE(wDevID, i, d1, 64);
1066 /* finding out in this order :
1068 * - if replaying the same note on the same channel
1069 * - the older voice (LRU)
1071 for (i = nv = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1072 if (voice[i].status == sVS_UNUSED ||
1073 (voice[i].note == d1 && voice[i].channel == chn)) {
1077 if (voice[i].cntMark < voice[0].cntMark) {
1082 "playing on voice=%d, pgm=%d, pan=0x%02X, vol=0x%02X, "
1083 "bender=0x%02X, note=0x%02X, vel=0x%02X\n",
1084 nv, channel[chn].program,
1085 channel[chn].balance,
1086 channel[chn].volume,
1087 channel[chn].bender, d1, d2);
1089 SEQ_SET_PATCH(wDevID, nv, IS_DRUM_CHANNEL(extra, chn) ?
1090 (128 + d1) : channel[chn].program);
1091 SEQ_BENDER_RANGE(wDevID, nv, channel[chn].benderRange * 100);
1092 SEQ_BENDER(wDevID, nv, channel[chn].bender);
1093 SEQ_CONTROL(wDevID, nv, CTL_PAN, channel[chn].balance);
1094 SEQ_CONTROL(wDevID, nv, CTL_EXPRESSION, channel[chn].expression);
1096 /* FIXME: does not really seem to work on my SB card and
1097 * screws everything up... so lay it down
1099 SEQ_CONTROL(wDevID, nv, CTL_MAIN_VOLUME, channel[chn].volume);
1101 SEQ_START_NOTE(wDevID, nv, d1, d2);
1102 voice[nv].status = channel[chn].sustain ? sVS_SUSTAINED : sVS_PLAYING;
1103 voice[nv].note = d1;
1104 voice[nv].channel = chn;
1105 voice[nv].cntMark = extra->counter++;
1107 case MIDI_KEY_PRESSURE:
1108 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1109 if (voice[i].status != sVS_UNUSED && voice[i].channel == chn && voice[i].note == d1) {
1110 SEQ_KEY_PRESSURE(wDevID, i, d1, d2);
1114 case MIDI_CTL_CHANGE:
1116 case CTL_BANK_SELECT: channel[chn].bank = d2; break;
1117 case CTL_MAIN_VOLUME: channel[chn].volume = d2; break;
1118 case CTL_PAN: channel[chn].balance = d2; break;
1119 case CTL_EXPRESSION: channel[chn].expression = d2; break;
1120 case CTL_SUSTAIN: channel[chn].sustain = d2;
1122 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1123 if (voice[i].status == sVS_PLAYING && voice[i].channel == chn) {
1124 voice[i].status = sVS_SUSTAINED;
1128 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1129 if (voice[i].status == sVS_SUSTAINED && voice[i].channel == chn) {
1130 voice[i].status = sVS_UNUSED;
1131 SEQ_STOP_NOTE(wDevID, i, voice[i].note, 64);
1136 case CTL_NONREG_PARM_NUM_LSB: channel[chn].nrgPmtLSB = d2; break;
1137 case CTL_NONREG_PARM_NUM_MSB: channel[chn].nrgPmtMSB = d2; break;
1138 case CTL_REGIST_PARM_NUM_LSB: channel[chn].regPmtLSB = d2; break;
1139 case CTL_REGIST_PARM_NUM_MSB: channel[chn].regPmtMSB = d2; break;
1141 case CTL_DATA_ENTRY:
1142 switch ((channel[chn].regPmtMSB << 8) | channel[chn].regPmtLSB) {
1144 if (channel[chn].benderRange != d2) {
1145 channel[chn].benderRange = d2;
1146 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1147 if (voice[i].channel == chn) {
1148 SEQ_BENDER_RANGE(wDevID, i, channel[chn].benderRange);
1155 channel[chn].benderRange = 2;
1156 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1157 if (voice[i].channel == chn) {
1158 SEQ_BENDER_RANGE(wDevID, i, channel[chn].benderRange);
1163 TRACE(midi, "Data entry: regPmt=0x%02x%02x, nrgPmt=0x%02x%02x with %x\n",
1164 channel[chn].regPmtMSB, channel[chn].regPmtLSB,
1165 channel[chn].nrgPmtMSB, channel[chn].nrgPmtLSB,
1171 case 0x78: /* all sounds off */
1172 /* FIXME: I don't know if I have to take care of the channel
1173 * for this control ?
1175 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1176 if (voice[i].status != sVS_UNUSED && voice[i].channel == chn) {
1177 voice[i].status = sVS_UNUSED;
1178 SEQ_STOP_NOTE(wDevID, i, voice[i].note, 0);
1182 case 0x7B: /* all notes off */
1183 /* FIXME: I don't know if I have to take care of the channel
1184 * for this control ?
1186 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1187 if (voice[i].status == sVS_PLAYING && voice[i].channel == chn) {
1188 voice[i].status = sVS_UNUSED;
1189 SEQ_STOP_NOTE(wDevID, i, voice[i].note, 0);
1194 TRACE(midi, "Dropping MIDI control event 0x%02x(%02x) on channel %d\n",
1199 case MIDI_PGM_CHANGE:
1200 channel[chn].program = d1;
1202 case MIDI_CHN_PRESSURE:
1203 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1204 if (voice[i].status != sVS_UNUSED && voice[i].channel == chn) {
1205 SEQ_KEY_PRESSURE(wDevID, i, voice[i].note, d1);
1209 case MIDI_PITCH_BEND:
1210 channel[chn].bender = (d2 << 7) + d1;
1211 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1212 if (voice[i].channel == chn) {
1213 SEQ_BENDER(wDevID, i, channel[chn].bender);
1217 case MIDI_SYSTEM_PREFIX:
1218 switch (evt & 0x0F) {
1219 case 0x0F: /* Reset */
1223 WARN(midi, "Unsupported (yet) system event %02x\n", evt & 0x0F);
1227 WARN(midi, "Internal error, shouldn't happen (event=%08x)\n", evt & 0xF0);
1228 return MMSYSERR_NOTENABLED;
1234 int dev = wDevID - MODM_NUMFMSYNTHDEVS;
1236 WARN(midi, "Internal error on devID (%u) !\n", wDevID);
1237 return MIDIERR_NODEVICE;
1240 switch (evt & 0xF0) {
1243 case MIDI_KEY_PRESSURE:
1244 case MIDI_CTL_CHANGE:
1245 case MIDI_PITCH_BEND:
1246 SEQ_MIDIOUT(dev, evt);
1247 SEQ_MIDIOUT(dev, d1);
1248 SEQ_MIDIOUT(dev, d2);
1250 case MIDI_PGM_CHANGE:
1251 case MIDI_CHN_PRESSURE:
1252 SEQ_MIDIOUT(dev, evt);
1253 SEQ_MIDIOUT(dev, d1);
1255 case MIDI_SYSTEM_PREFIX:
1256 switch (evt & 0x0F) {
1257 case 0x00: /* System Exclusive, don't do it on modData,
1258 * should require modLongData*/
1259 case 0x01: /* Undefined */
1260 case 0x04: /* Undefined. */
1261 case 0x05: /* Undefined. */
1262 case 0x07: /* End of Exclusive. */
1263 case 0x09: /* Undefined. */
1264 case 0x0D: /* Undefined. */
1266 case 0x06: /* Tune Request */
1267 case 0x08: /* Timing Clock. */
1268 case 0x0A: /* Start. */
1269 case 0x0B: /* Continue */
1270 case 0x0C: /* Stop */
1271 case 0x0E: /* Active Sensing. */
1272 SEQ_MIDIOUT(dev, evt);
1274 case 0x0F: /* Reset */
1275 /* SEQ_MIDIOUT(dev, evt);
1276 this other way may be better */
1277 SEQ_MIDIOUT(dev, MIDI_SYSTEM_PREFIX);
1278 SEQ_MIDIOUT(dev, 0x7e);
1279 SEQ_MIDIOUT(dev, 0x7f);
1280 SEQ_MIDIOUT(dev, 0x09);
1281 SEQ_MIDIOUT(dev, 0x01);
1282 SEQ_MIDIOUT(dev, 0xf7);
1284 case 0x03: /* Song Select. */
1285 SEQ_MIDIOUT(dev, evt);
1286 SEQ_MIDIOUT(dev, d1);
1287 case 0x02: /* Song Position Pointer. */
1288 SEQ_MIDIOUT(dev, evt);
1289 SEQ_MIDIOUT(dev, d1);
1290 SEQ_MIDIOUT(dev, d2);
1297 WARN(midi, "Technology not supported (yet) %d !\n",
1298 midiOutDevices[wDevID]->wTechnology);
1299 return MMSYSERR_NOTENABLED;
1304 if (MidiOutDev[wDevID].unixdev == 0) {
1305 WARN(midi,"can't play !\n");
1306 return MIDIERR_NODEVICE;
1309 WORD event = LOWORD(dwParam);
1310 if (write (MidiOutDev[wDevID].unixdev,
1311 &event, sizeof(event)) != sizeof(WORD)) {
1312 WARN(midi, "error writting unixdev !\n");
1315 #endif /* HAVE_OSS */
1316 return MMSYSERR_NOERROR;
1319 /**************************************************************************
1320 * modLongData [internal]
1322 static DWORD modLongData(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
1326 TRACE(midi, "(%04X, %p, %08lX);\n", wDevID, lpMidiHdr, dwSize);
1329 if (midiSeqFD == -1) {
1330 WARN(midi,"can't play !\n");
1331 return MIDIERR_NODEVICE;
1333 #else /* HAVE_OSS */
1334 if (MidiOutDev[wDevID].unixdev == 0) {
1335 WARN(midi,"can't play !\n");
1336 return MIDIERR_NODEVICE;
1338 #endif /* HAVE_OSS */
1340 if (lpMidiHdr->lpData == NULL)
1341 return MIDIERR_UNPREPARED;
1342 if (!(lpMidiHdr->dwFlags & MHDR_PREPARED))
1343 return MIDIERR_UNPREPARED;
1344 if (lpMidiHdr->dwFlags & MHDR_INQUEUE)
1345 return MIDIERR_STILLPLAYING;
1346 lpMidiHdr->dwFlags &= ~MHDR_DONE;
1347 lpMidiHdr->dwFlags |= MHDR_INQUEUE;
1349 TRACE(midi, "dwBytesRecorded %lu !\n", lpMidiHdr->dwBytesRecorded);
1350 TRACE(midi, " %02X %02X %02X %02X\n",
1351 ((LPBYTE)(lpMidiHdr->reserved))[0],
1352 ((LPBYTE)(lpMidiHdr->reserved))[1],
1353 ((LPBYTE)(lpMidiHdr->reserved))[2],
1354 ((LPBYTE)(lpMidiHdr->reserved))[3]);
1356 switch (midiOutDevices[wDevID]->wTechnology) {
1358 /* FIXME: I don't think there is much to do here */
1361 if (((LPBYTE)lpMidiHdr->reserved)[0] != 0xF0) {
1362 /* Send end of System Exclusive */
1363 SEQ_MIDIOUT(wDevID - MODM_NUMFMSYNTHDEVS, 0xF0);
1364 TRACE(midi, "Adding missing 0xF0 marker at the begining of "
1365 "system exclusive byte stream\n");
1367 for (count = 0; count < lpMidiHdr->dwBytesRecorded; count++) {
1368 SEQ_MIDIOUT(wDevID - MODM_NUMFMSYNTHDEVS,
1369 ((LPBYTE)lpMidiHdr->reserved)[count]);
1371 if (((LPBYTE)lpMidiHdr->reserved)[count - 1] != 0xF7) {
1372 /* Send end of System Exclusive */
1373 SEQ_MIDIOUT(wDevID - MODM_NUMFMSYNTHDEVS, 0xF7);
1374 TRACE(midi, "Adding missing 0xF7 marker at the end of "
1375 "system exclusive byte stream\n");
1380 WARN(midi, "Technology not supported (yet) %d !\n",
1381 midiOutDevices[wDevID]->wTechnology);
1382 return MMSYSERR_NOTENABLED;
1384 #else /* HAVE_OSS */
1386 LPWORD ptr = (LPWORD)lpMidiHdr->reserved;
1389 for (count = 0; count < lpMidiHdr->dwBytesRecorded; count++) {
1390 if (write(MidiOutDev[wDevID].unixdev,
1391 ptr, sizeof(WORD)) != sizeof(WORD))
1397 TRACE(midi, "after write count = %d\n",count);
1398 if (count != lpMidiHdr->dwBytesRecorded) {
1399 WARN(midi, "error writting unixdev #%d ! (%d != %ld)\n",
1400 MidiOutDev[wDevID].unixdev, count,
1401 lpMidiHdr->dwBytesRecorded);
1402 TRACE(midi, "\terrno = %d error = %s\n",en,strerror(en));
1403 return MMSYSERR_NOTENABLED;
1406 #endif /* HAVE_OSS */
1408 lpMidiHdr->dwFlags &= ~MHDR_INQUEUE;
1409 lpMidiHdr->dwFlags |= MHDR_DONE;
1410 if (MIDI_NotifyClient(wDevID, MOM_DONE, (DWORD)lpMidiHdr, 0L) != MMSYSERR_NOERROR) {
1411 WARN(midi,"can't notify client !\n");
1412 return MMSYSERR_INVALPARAM;
1414 return MMSYSERR_NOERROR;
1417 /**************************************************************************
1418 * modPrepare [internal]
1420 static DWORD modPrepare(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
1422 TRACE(midi, "(%04X, %p, %08lX);\n", wDevID, lpMidiHdr, dwSize);
1425 if (midiSeqFD == -1) {
1426 WARN(midi,"can't prepare !\n");
1427 return MMSYSERR_NOTENABLED;
1429 #else /* HAVE_OSS */
1430 if (MidiOutDev[wDevID].unixdev == 0) {
1431 WARN(midi,"can't prepare !\n");
1432 return MMSYSERR_NOTENABLED;
1434 #endif /* HAVE_OSS */
1435 if (dwSize != sizeof(MIDIHDR) || lpMidiHdr == 0 ||
1436 lpMidiHdr->lpData == 0 || lpMidiHdr->dwFlags != 0 ||
1437 lpMidiHdr->dwBufferLength >= 0x10000ul)
1438 return MMSYSERR_INVALPARAM;
1440 lpMidiHdr->lpNext = 0;
1441 lpMidiHdr->dwFlags |= MHDR_PREPARED;
1442 lpMidiHdr->dwFlags &= ~MHDR_DONE;
1443 return MMSYSERR_NOERROR;
1446 /**************************************************************************
1447 * modUnprepare [internal]
1449 static DWORD modUnprepare(WORD wDevID, LPMIDIHDR lpMidiHdr, DWORD dwSize)
1451 TRACE(midi, "(%04X, %p, %08lX);\n", wDevID, lpMidiHdr, dwSize);
1454 if (midiSeqFD == -1) {
1455 WARN(midi,"can't unprepare !\n");
1456 return MMSYSERR_NOTENABLED;
1458 #else /* HAVE_OSS */
1459 if (MidiOutDev[wDevID].unixdev == 0) {
1460 WARN(midi,"can't unprepare !\n");
1461 return MMSYSERR_NOTENABLED;
1463 #endif /* HAVE_OSS */
1464 if (dwSize != sizeof(MIDIHDR) || lpMidiHdr == 0)
1465 return MMSYSERR_INVALPARAM;
1466 if (lpMidiHdr->dwFlags & MHDR_INQUEUE)
1467 return MIDIERR_STILLPLAYING;
1468 lpMidiHdr->dwFlags &= ~MHDR_PREPARED;
1469 return MMSYSERR_NOERROR;
1472 /**************************************************************************
1473 * modReset [internal]
1475 static DWORD modReset(WORD wDevID)
1477 TRACE(midi, "(%04X);\n", wDevID);
1478 /* FIXME: this function should :
1479 * turn off every note, remove sustain on all channels
1480 * remove any pending buffers
1482 return MMSYSERR_NOTENABLED;
1486 /*======================================================================*
1487 * MIDI entry points *
1488 *======================================================================*/
1490 /**************************************************************************
1491 * modMessage [sample driver]
1493 DWORD WINAPI modMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
1494 DWORD dwParam1, DWORD dwParam2)
1496 TRACE(midi, "(%04X, %04X, %08lX, %08lX, %08lX);\n",
1497 wDevID, wMsg, dwUser, dwParam1, dwParam2);
1500 #ifdef SNDCTL_MIDI_INFO
1502 return modOpen(wDevID, (LPMIDIOPENDESC)dwParam1, dwParam2);
1504 return modClose(wDevID);
1506 return modData(wDevID, dwParam1);
1508 return modLongData(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1510 return modPrepare(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1511 case MODM_UNPREPARE:
1512 return modUnprepare(wDevID, (LPMIDIHDR)dwParam1, dwParam2);
1513 case MODM_GETDEVCAPS:
1514 return modGetDevCaps(wDevID,(LPMIDIOUTCAPS16)dwParam1,dwParam2);
1515 case MODM_GETNUMDEVS:
1516 return MODM_NUMDEVS;
1517 case MODM_GETVOLUME:
1519 case MODM_SETVOLUME:
1522 return modReset(wDevID);
1525 TRACE(midi, "Unsupported message\n");
1527 return MMSYSERR_NOTSUPPORTED;
1530 /**************************************************************************
1531 * MIDI_DriverProc32 [sample driver]
1533 LONG MIDI_DriverProc32(DWORD dwDevID, HDRVR16 hDriv, DWORD wMsg,
1534 DWORD dwParam1, DWORD dwParam2)
1537 case DRV_LOAD: return 1;
1538 case DRV_FREE: return 1;
1539 case DRV_OPEN: return 1;
1540 case DRV_CLOSE: return 1;
1541 case DRV_ENABLE: return 1;
1542 case DRV_DISABLE: return 1;
1543 case DRV_QUERYCONFIGURE: return 1;
1544 case DRV_CONFIGURE: MessageBox16(0, "Sample Midi Linux Driver !", "MMLinux Driver", MB_OK); return 1;
1545 case DRV_INSTALL: return DRVCNF_RESTART;
1546 case DRV_REMOVE: return DRVCNF_RESTART;
1548 TRACE(midi, "Sending msg=%lu to default driver proc\n", wMsg);
1549 return DefDriverProc32(dwDevID, hDriv, wMsg, dwParam1, dwParam2);
1553 /**************************************************************************
1554 * MIDI_DriverProc16 [sample driver]
1556 LONG MIDI_DriverProc16(DWORD dwDevID, HDRVR16 hDriv, WORD wMsg,
1557 DWORD dwParam1, DWORD dwParam2)
1560 case DRV_LOAD: return 1;
1561 case DRV_FREE: return 1;
1562 case DRV_OPEN: return 1;
1563 case DRV_CLOSE: return 1;
1564 case DRV_ENABLE: return 1;
1565 case DRV_DISABLE: return 1;
1566 case DRV_QUERYCONFIGURE: return 1;
1567 case DRV_CONFIGURE: MessageBox16(0, "Sample Midi Linux Driver !", "MMLinux Driver", MB_OK); return 1;
1568 case DRV_INSTALL: return DRVCNF_RESTART;
1569 case DRV_REMOVE: return DRVCNF_RESTART;
1571 TRACE(midi, "Sending msg=%u to default driver proc\n", wMsg);
1572 return DefDriverProc32(dwDevID, hDriv, wMsg, dwParam1, dwParam2);
1576 /*-----------------------------------------------------------------------*/