Initialize the output buffer parameters to 0 in RegQueryValue*
[wine.git] / multimedia / midi.c
blob872852974f555be73e2e50703b0e65c080af7145
1 /* -*- tab-width: 8; c-basic-offset: 4 -*- */
3 /*
4 * Sample MIDI Wine Driver for Linux
6 * Copyright 1994 Martin Ayotte
7 */
9 /*
10 * Eric POUECH :
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
17 #include "config.h"
18 #include <errno.h>
19 #include <string.h>
20 #include <stdlib.h>
21 #include <string.h>
22 #include <unistd.h>
23 #include <fcntl.h>
24 #include <sys/ioctl.h>
25 #include "wine/winuser16.h"
26 #include "multimedia.h"
27 #include "driver.h"
28 #include "xmalloc.h"
29 #include "debug.h"
30 #include "heap.h"
31 #include "ldt.h"
33 DEFAULT_DEBUG_CHANNEL(midi)
35 typedef struct {
36 #ifndef HAVE_OSS
37 int unixdev;
38 #endif
39 int state;
40 DWORD bufsize;
41 LPMIDIOPENDESC midiDesc;
42 WORD wFlags;
43 LPMIDIHDR16 lpQueueHdr;
44 DWORD dwTotalPlayed;
45 #ifdef HAVE_OSS
46 unsigned char incoming[3];
47 unsigned char incPrev;
48 char incLen;
49 DWORD startTime;
50 #endif
51 } WINE_MIDIIN;
53 typedef struct {
54 #ifndef HAVE_OSS
55 int unixdev;
56 #endif
57 int state;
58 DWORD bufsize;
59 LPMIDIOPENDESC midiDesc;
60 WORD wFlags;
61 LPMIDIHDR16 lpQueueHdr;
62 DWORD dwTotalPlayed;
63 #ifdef HAVE_OSS
64 void* lpExtra; /* according to port type (MIDI, FM...), extra data when needed */
65 #endif
66 } WINE_MIDIOUT;
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 */
72 int MODM_NUMDEVS = 0;
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 */
81 int MIDM_NUMDEVS = 0;
83 #ifdef HAVE_OSS
84 static int midiSeqFD = -1;
85 static int numOpenMidiSeq = 0;
86 static UINT midiInTimerID = 0;
87 static int numStartedMidiIn = 0;
88 #endif /* HAVE_OSS */
90 /* this structure holds pointers with information for each MIDI
91 * out device found.
93 LPMIDIOUTCAPS16 midiOutDevices[MAX_MIDIOUTDRV];
95 /* this structure holds pointers with information for each MIDI
96 * in device found.
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)
117 DWORD dwCallBack;
118 UINT16 uFlags;
119 HANDLE16 hDev;
120 DWORD dwInstance;
122 TRACE(midi,"wDevID = %04X wMsg = %d dwParm1 = %04lX dwParam2 = %04lX\n",
123 wDevID, wMsg, dwParam1, dwParam2);
125 switch (wMsg) {
126 case MOM_OPEN:
127 case MOM_CLOSE:
128 case MOM_DONE:
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;
136 break;
138 case MIM_OPEN:
139 case MIM_CLOSE:
140 case MIM_DATA:
141 case MIM_ERROR:
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;
149 break;
150 default:
151 WARN(midi, "Unsupported MSW-MIDI message %u\n", wMsg);
152 return MCIERR_INTERNAL;
155 return DriverCallback16(dwCallBack, uFlags, hDev, wMsg, dwInstance, dwParam1, dwParam2) ?
156 0 : MCIERR_INTERNAL;
159 #ifdef HAVE_OSS
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);
169 return -1;
171 if (fcntl(midiSeqFD, F_SETFL, O_NONBLOCK) < 0) {
172 WARN(midi, "can't set sequencer fd to non blocking (%d)\n", errno);
173 close(midiSeqFD);
174 midiSeqFD = -1;
175 return -1;
177 ioctl(midiSeqFD, SNDCTL_SEQ_RESET);
179 numOpenMidiSeq++;
180 return 0;
183 /**************************************************************************
184 * midiCloseSeq [internal]
186 static int midiCloseSeq(void)
188 if (--numOpenMidiSeq == 0) {
189 close(midiSeqFD);
190 midiSeqFD = -1;
192 return 0;
195 /* FIXME: this is a bad idea, it's even not static... */
196 SEQ_DEFINEBUF(1024);
198 /* FIXME: this is not reentrant, not static - because of global variable
199 * _seqbuf and al. */
200 /**************************************************************************
201 * seqbuf_dump [internal]
203 void seqbuf_dump(void)
205 if (_seqbufptr) {
206 if (write(midiSeqFD, _seqbuf, _seqbufptr) == -1) {
207 WARN(midi, "Can't write data to sequencer (%d/%d)!\n",
208 midiSeqFD, errno);
210 /* FIXME:
211 * in any case buffer is lost so that if many errors occur the buffer
212 * will not overrun
214 _seqbufptr = 0;
217 #endif /* HAVE_OSS */
219 #ifdef HAVE_OSS
221 static void midReceiveChar(WORD wDevID, unsigned char value, DWORD dwTime)
223 DWORD toSend = 0;
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");
229 return;
231 if (MidiInDev[wDevID].state == 0) {
232 TRACE(midi, "input not started, thrown away\n");
233 return;
236 if (MidiInDev[wDevID].state & 2) { /* system exclusive */
237 LPMIDIHDR16 lpMidiHdr = MidiInDev[wDevID].lpQueueHdr;
238 WORD sbfb = FALSE;
240 if (lpMidiHdr) {
241 LPBYTE lpData = ((DWORD)lpMidiHdr == lpMidiHdr->reserved) ?
242 (LPBYTE)lpMidiHdr->lpData : (LPBYTE)PTR_SEG_TO_LIN(lpMidiHdr->lpData);
244 lpData[lpMidiHdr->dwBytesRecorded++] = value;
245 if (lpMidiHdr->dwBytesRecorded == lpMidiHdr->dwBufferLength) {
246 sbfb = TRUE;
249 if (value == 0xF7) { /* then end */
250 MidiInDev[wDevID].state &= ~2;
251 sbfb = TRUE;
253 if (sbfb && lpMidiHdr != NULL) {
254 lpMidiHdr = MidiInDev[wDevID].lpQueueHdr;
255 lpMidiHdr->dwFlags &= ~MHDR_INQUEUE;
256 lpMidiHdr->dwFlags |= MHDR_DONE;
257 MidiInDev[wDevID].lpQueueHdr = (LPMIDIHDR16)lpMidiHdr->lpNext;
258 if (MIDI_NotifyClient(wDevID, MIM_LONGDATA, (DWORD)lpMidiHdr->reserved, dwTime) != MMSYSERR_NOERROR) {
259 WARN(midi, "Couldn't notify client\n");
262 return;
265 #define IS_CMD(_x) (((_x) & 0x80) == 0x80)
266 #define IS_SYS_CMD(_x) (((_x) & 0xF0) == 0xF0)
268 if (!IS_CMD(value) && MidiInDev[wDevID].incLen == 0) { /* try to reuse old cmd */
269 if (IS_CMD(MidiInDev[wDevID].incPrev) && !IS_SYS_CMD(MidiInDev[wDevID].incPrev)) {
270 MidiInDev[wDevID].incoming[0] = MidiInDev[wDevID].incPrev;
271 MidiInDev[wDevID].incLen = 1;
272 TRACE(midi, "Reusing old command %02xh\n", MidiInDev[wDevID].incPrev);
273 } else {
274 FIXME(midi, "error for midi-in, should generate MIM_ERROR notification:"
275 " prev=%02Xh, incLen=%02Xh\n",
276 MidiInDev[wDevID].incPrev, MidiInDev[wDevID].incLen);
277 return;
280 MidiInDev[wDevID].incoming[(int)(MidiInDev[wDevID].incLen++)] = value;
281 if (MidiInDev[wDevID].incLen == 1 && !IS_SYS_CMD(MidiInDev[wDevID].incoming[0])) {
282 /* store new cmd, just in case */
283 MidiInDev[wDevID].incPrev = MidiInDev[wDevID].incoming[0];
286 #undef IS_CMD(_x)
287 #undef IS_SYS_CMD(_x)
289 switch (MidiInDev[wDevID].incoming[0] & 0xF0) {
290 case MIDI_NOTEOFF:
291 case MIDI_NOTEON:
292 case MIDI_KEY_PRESSURE:
293 case MIDI_CTL_CHANGE:
294 case MIDI_PITCH_BEND:
295 if (MidiInDev[wDevID].incLen == 3) {
296 toSend = (MidiInDev[wDevID].incoming[2] << 16) |
297 (MidiInDev[wDevID].incoming[1] << 8) |
298 (MidiInDev[wDevID].incoming[0] << 0);
300 break;
301 case MIDI_PGM_CHANGE:
302 case MIDI_CHN_PRESSURE:
303 if (MidiInDev[wDevID].incLen == 2) {
304 toSend = (MidiInDev[wDevID].incoming[1] << 8) |
305 (MidiInDev[wDevID].incoming[0] << 0);
307 break;
308 case MIDI_SYSTEM_PREFIX:
309 if (MidiInDev[wDevID].incoming[0] == 0xF0) {
310 MidiInDev[wDevID].state |= 2;
311 MidiInDev[wDevID].incLen = 0;
312 } else {
313 if (MidiInDev[wDevID].incLen == 1) {
314 toSend = (MidiInDev[wDevID].incoming[0] << 0);
317 break;
318 default:
319 WARN(midi, "This shouldn't happen (%02X)\n", MidiInDev[wDevID].incoming[0]);
321 if (toSend != 0) {
322 TRACE(midi, "Sending event %08lx\n", toSend);
323 MidiInDev[wDevID].incLen = 0;
324 dwTime -= MidiInDev[wDevID].startTime;
325 if (MIDI_NotifyClient(wDevID, MIM_DATA, toSend, dwTime) != MMSYSERR_NOERROR) {
326 WARN(midi, "Couldn't notify client\n");
331 static VOID WINAPI midTimeCallback(HWND hwnd, UINT msg, UINT id, DWORD dwTime)
333 unsigned char buffer[256];
334 int len, idx;
336 TRACE(midi, "(%04X, %d, %d, %lu)\n", hwnd, msg, id, dwTime);
338 len = read(midiSeqFD, buffer, sizeof(buffer));
340 if ((len % 4) != 0) {
341 WARN(midi, "bad length %d (%d)\n", len, errno);
342 return;
345 for (idx = 0; idx < len; ) {
346 if (buffer[idx] & 0x80) {
347 TRACE(midi,
348 "reading<8> %02x %02x %02x %02x %02x %02x %02x %02x\n",
349 buffer[idx + 0], buffer[idx + 1],
350 buffer[idx + 2], buffer[idx + 3],
351 buffer[idx + 4], buffer[idx + 5],
352 buffer[idx + 6], buffer[idx + 7]);
353 idx += 8;
354 } else {
355 switch (buffer[idx + 0]) {
356 case SEQ_WAIT:
357 case SEQ_ECHO:
358 break;
359 case SEQ_MIDIPUTC:
360 midReceiveChar(buffer[idx + 2], buffer[idx + 1], dwTime);
361 break;
362 default:
363 TRACE(midi, "Unsupported event %d\n", buffer[idx + 0]);
364 break;
366 idx += 4;
370 #endif /* HAVE_OSS */
372 /**************************************************************************
373 * midGetDevCaps [internal]
375 static DWORD midGetDevCaps(WORD wDevID, LPMIDIINCAPS16 lpCaps, DWORD dwSize)
377 LPMIDIINCAPS16 tmplpCaps;
379 TRACE(midi, "(%04X, %p, %08lX);\n", wDevID, lpCaps, dwSize);
381 if (wDevID >= MIDM_NUMDEVS) {
382 return MMSYSERR_BADDEVICEID;
384 if (lpCaps == NULL) {
385 return MMSYSERR_INVALPARAM;
388 tmplpCaps = midiInDevices[wDevID];
389 lpCaps->wMid = tmplpCaps->wMid;
390 lpCaps->wPid = tmplpCaps->wPid;
391 lpCaps->vDriverVersion = tmplpCaps->vDriverVersion;
392 strcpy(lpCaps->szPname, tmplpCaps->szPname);
393 if (dwSize == sizeof(MIDIINCAPS16)) {
394 /* we should run win 95, so make use of dwSupport */
395 lpCaps->dwSupport = tmplpCaps->dwSupport;
396 } else if (dwSize != sizeof(MIDIINCAPS16) - sizeof(DWORD)) {
397 TRACE(midi, "bad size for lpCaps\n");
398 return MMSYSERR_INVALPARAM;
401 return MMSYSERR_NOERROR;
404 /**************************************************************************
405 * midOpen [internal]
407 static DWORD midOpen(WORD wDevID, LPMIDIOPENDESC lpDesc, DWORD dwFlags)
409 TRACE(midi, "(%04X, %p, %08lX);\n", wDevID, lpDesc, dwFlags);
411 if (lpDesc == NULL) {
412 WARN(midi, "Invalid Parameter !\n");
413 return MMSYSERR_INVALPARAM;
415 /* FIXME :
416 * how to check that content of lpDesc is correct ?
418 if (wDevID >= MAX_MIDIINDRV) {
419 WARN(midi,"wDevID too large (%u) !\n", wDevID);
420 return MMSYSERR_BADDEVICEID;
422 if (MidiInDev[wDevID].midiDesc != 0) {
423 WARN(midi, "device already open !\n");
424 return MMSYSERR_ALLOCATED;
426 if ((dwFlags & ~CALLBACK_TYPEMASK) != 0) {
427 FIXME(midi, "No support for MIDI_IO_STATUS in dwFlags\n");
428 return MMSYSERR_INVALFLAG;
431 #ifdef HAVE_OSS
432 if (midiOpenSeq() < 0) {
433 return MMSYSERR_ERROR;
436 if (numStartedMidiIn++ == 0) {
437 midiInTimerID = SetTimer(0, 0, 250, midTimeCallback);
438 if (!midiInTimerID) {
439 numStartedMidiIn = 0;
440 WARN(midi, "Couldn't start timer for midi-in\n");
441 midiCloseSeq();
442 return MMSYSERR_ERROR;
444 TRACE(midi, "Starting timer (%u) for midi-in\n", midiInTimerID);
446 #else /* HAVE_OSS */
448 int midi = open(MIDI_DEV, O_WRONLY, 0);
450 MidiInDev[wDevID].unixdev = 0;
451 if (midi == -1) {
452 WARN(midi,"can't open '%s' (%d)!\n", MIDI_DEV, errno);
453 return MMSYSERR_ALLOCATED;
455 MidiInDev[wDevID].unixdev = midi;
457 #endif /* HAVE_OSS */
459 MidiInDev[wDevID].wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
461 MidiInDev[wDevID].lpQueueHdr = NULL;
462 MidiInDev[wDevID].dwTotalPlayed = 0;
463 MidiInDev[wDevID].bufsize = 0x3FFF;
464 MidiInDev[wDevID].midiDesc = lpDesc;
465 MidiInDev[wDevID].state = 0;
466 #ifdef HAVE_OSS
467 MidiInDev[wDevID].incLen = 0;
468 MidiInDev[wDevID].startTime = 0;
469 #endif /* HAVE_OSS */
470 if (MIDI_NotifyClient(wDevID, MIM_OPEN, 0L, 0L) != MMSYSERR_NOERROR) {
471 WARN(midi,"can't notify client !\n");
472 return MMSYSERR_INVALPARAM;
474 return MMSYSERR_NOERROR;
477 /**************************************************************************
478 * midClose [internal]
480 static DWORD midClose(WORD wDevID)
482 int ret = MMSYSERR_NOERROR;
484 TRACE(midi, "(%04X);\n", wDevID);
486 if (wDevID >= MAX_MIDIINDRV) {
487 WARN(midi,"wDevID too bif (%u) !\n", wDevID);
488 return MMSYSERR_BADDEVICEID;
490 if (MidiInDev[wDevID].midiDesc == 0) {
491 WARN(midi, "device not opened !\n");
492 return MMSYSERR_ERROR;
494 if (MidiInDev[wDevID].lpQueueHdr != 0) {
495 return MIDIERR_STILLPLAYING;
498 #ifdef HAVE_OSS
499 if (midiSeqFD == -1) {
500 WARN(midi,"ooops !\n");
501 return MMSYSERR_ERROR;
503 if (--numStartedMidiIn == 0) {
504 TRACE(midi, "Stopping timer for midi-in\n");
505 if (!KillTimer(0, midiInTimerID)) {
506 WARN(midi, "Couldn't stop timer for midi-in\n");
508 midiInTimerID = 0;
510 midiCloseSeq();
511 #else /* HAVE_OSS */
512 if (MidiInDev[wDevID].unixdev == 0) {
513 WARN(midi,"ooops !\n");
514 return MMSYSERR_ERROR;
516 close(MidiInDev[wDevID].unixdev);
517 MidiInDev[wDevID].unixdev = 0;
518 #endif /* HAVE_OSS */
519 MidiInDev[wDevID].bufsize = 0;
520 if (MIDI_NotifyClient(wDevID, MIM_CLOSE, 0L, 0L) != MMSYSERR_NOERROR) {
521 WARN(midi,"can't notify client !\n");
522 ret = MMSYSERR_INVALPARAM;
524 MidiInDev[wDevID].midiDesc = 0;
525 return ret;
528 /**************************************************************************
529 * midAddBuffer [internal]
531 static DWORD midAddBuffer(WORD wDevID, LPMIDIHDR16 lpMidiHdr, DWORD dwSize)
533 TRACE(midi, "(%04X, %p, %08lX);\n", wDevID, lpMidiHdr, dwSize);
535 if (lpMidiHdr == NULL) return MMSYSERR_INVALPARAM;
536 if (sizeof(MIDIHDR16) > dwSize) return MMSYSERR_INVALPARAM;
537 if (lpMidiHdr->dwBufferLength == 0) return MMSYSERR_INVALPARAM;
538 if (lpMidiHdr->dwFlags & MHDR_INQUEUE) return MIDIERR_STILLPLAYING;
539 if (!(lpMidiHdr->dwFlags & MHDR_PREPARED)) return MIDIERR_UNPREPARED;
541 if (MidiInDev[wDevID].lpQueueHdr == 0) {
542 MidiInDev[wDevID].lpQueueHdr = lpMidiHdr;
543 } else {
544 LPMIDIHDR16 ptr;
546 for (ptr = MidiInDev[wDevID].lpQueueHdr;
547 ptr->lpNext != 0;
548 ptr = (LPMIDIHDR16)ptr->lpNext);
549 ptr->lpNext = (struct midihdr_tag*)lpMidiHdr;
551 return MMSYSERR_NOERROR;
554 /**************************************************************************
555 * midPrepare [internal]
557 static DWORD midPrepare(WORD wDevID, LPMIDIHDR16 lpMidiHdr, DWORD dwSize)
559 TRACE(midi, "(%04X, %p, %08lX);\n", wDevID, lpMidiHdr, dwSize);
561 if (dwSize < sizeof(MIDIHDR16) || lpMidiHdr == 0 ||
562 lpMidiHdr->lpData == 0 || lpMidiHdr->dwFlags != 0 ||
563 lpMidiHdr->dwBufferLength >= 0x10000ul)
564 return MMSYSERR_INVALPARAM;
566 lpMidiHdr->lpNext = 0;
567 lpMidiHdr->dwFlags |= MHDR_PREPARED;
568 lpMidiHdr->dwBytesRecorded = 0;
570 return MMSYSERR_NOERROR;
573 /**************************************************************************
574 * midUnprepare [internal]
576 static DWORD midUnprepare(WORD wDevID, LPMIDIHDR16 lpMidiHdr, DWORD dwSize)
578 TRACE(midi, "(%04X, %p, %08lX);\n", wDevID, lpMidiHdr, dwSize);
580 if (dwSize < sizeof(MIDIHDR16) || lpMidiHdr == 0 ||
581 lpMidiHdr->lpData == 0 || lpMidiHdr->dwBufferLength >= 0x10000ul)
582 return MMSYSERR_INVALPARAM;
584 if (!(lpMidiHdr->dwFlags & MHDR_PREPARED)) return MIDIERR_UNPREPARED;
585 if (lpMidiHdr->dwFlags & MHDR_INQUEUE) return MIDIERR_STILLPLAYING;
587 lpMidiHdr->dwFlags &= ~MHDR_PREPARED;
589 return MMSYSERR_NOERROR;
592 /**************************************************************************
593 * midReset [internal]
595 static DWORD midReset(WORD wDevID)
597 DWORD dwTime = GetTickCount();
599 TRACE(midi, "(%04X);\n", wDevID);
601 while (MidiInDev[wDevID].lpQueueHdr) {
602 MidiInDev[wDevID].lpQueueHdr->dwFlags &= ~MHDR_INQUEUE;
603 MidiInDev[wDevID].lpQueueHdr->dwFlags |= MHDR_DONE;
604 /* FIXME: when called from 16 bit, lpQueueHdr needs to be a segmented ptr */
605 if (MIDI_NotifyClient(wDevID, MIM_LONGDATA,
606 (DWORD)MidiInDev[wDevID].lpQueueHdr, dwTime) != MMSYSERR_NOERROR) {
607 WARN(midi, "Couldn't notify client\n");
609 MidiInDev[wDevID].lpQueueHdr = (LPMIDIHDR16)MidiInDev[wDevID].lpQueueHdr->lpNext;
612 return MMSYSERR_NOERROR;
616 /**************************************************************************
617 * midStart [internal]
619 static DWORD midStart(WORD wDevID)
621 TRACE(midi, "(%04X);\n", wDevID);
623 /* FIXME : should test value of wDevID */
625 #ifdef HAVE_OSS
626 MidiInDev[wDevID].state = 1;
627 MidiInDev[wDevID].startTime = GetTickCount();
628 return MMSYSERR_NOERROR;
629 #else
630 return MMSYSERR_NOTENABLED;
631 #endif /* HAVE_OSS */
634 /**************************************************************************
635 * midStop [internal]
637 static DWORD midStop(WORD wDevID)
639 TRACE(midi, "(%04X);\n", wDevID);
641 /* FIXME : should test value of wDevID */
643 #ifdef HAVE_OSS
644 MidiInDev[wDevID].state = 0;
645 return MMSYSERR_NOERROR;
646 #else /* HAVE_OSS */
647 return MMSYSERR_NOTENABLED;
648 #endif /* HAVE_OSS */
651 /*-----------------------------------------------------------------------*/
653 #ifdef HAVE_OSS
655 typedef struct sVoice {
656 int note; /* 0 means not used */
657 int channel;
658 unsigned cntMark : 30,
659 status : 2;
660 #define sVS_UNUSED 0
661 #define sVS_PLAYING 1
662 #define sVS_SUSTAINED 2
663 } sVoice;
665 typedef struct sChannel {
666 int program;
668 int bender;
669 int benderRange;
670 /* controlers */
671 int bank; /* CTL_BANK_SELECT */
672 int volume; /* CTL_MAIN_VOLUME */
673 int balance; /* CTL_BALANCE */
674 int expression; /* CTL_EXPRESSION */
675 int sustain; /* CTL_SUSTAIN */
677 unsigned char nrgPmtMSB; /* Non register Parameters */
678 unsigned char nrgPmtLSB;
679 unsigned char regPmtMSB; /* Non register Parameters */
680 unsigned char regPmtLSB;
681 } sChannel;
683 typedef struct sFMextra {
684 unsigned counter;
685 int drumSetMask;
686 sChannel channel[16]; /* MIDI has only 16 channels */
687 sVoice voice[1]; /* dyn allocated according to sound card */
688 /* do not append fields below voice[1] since the size of this structure
689 * depends on the number of available voices on the FM synth...
691 } sFMextra;
693 extern unsigned char midiFMInstrumentPatches[16 * 128];
694 extern unsigned char midiFMDrumsPatches [16 * 128];
696 /**************************************************************************
697 * modFMLoad [internal]
699 static int modFMLoad(int dev)
701 int i;
702 struct sbi_instrument sbi;
704 sbi.device = dev;
705 sbi.key = FM_PATCH;
707 memset(sbi.operators + 16, 0, 16);
708 for (i = 0; i < 128; i++) {
709 sbi.channel = i;
710 memcpy(sbi.operators, midiFMInstrumentPatches + i * 16, 16);
712 if (write(midiSeqFD, (char*)&sbi, sizeof(sbi)) == -1) {
713 WARN(midi, "Couldn't write patch for instrument %d (%d)!\n", sbi.channel, errno);
714 return -1;
717 for (i = 0; i < 128; i++) {
718 sbi.channel = 128 + i;
719 memcpy(sbi.operators, midiFMDrumsPatches + i * 16, 16);
721 if (write(midiSeqFD, (char*)&sbi, sizeof(sbi)) == -1) {
722 WARN(midi, "Couldn't write patch for drum %d (%d)!\n", sbi.channel, errno);
723 return -1;
726 return 0;
729 /**************************************************************************
730 * modFMReset [internal]
732 static void modFMReset(WORD wDevID)
734 sFMextra* extra = (sFMextra*)MidiOutDev[wDevID].lpExtra;
735 sVoice* voice = extra->voice;
736 sChannel* channel = extra->channel;
737 int i;
739 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
740 if (voice[i].status != sVS_UNUSED) {
741 SEQ_STOP_NOTE(wDevID, i, voice[i].note, 64);
743 SEQ_KEY_PRESSURE(wDevID, i, 127, 0);
744 SEQ_CONTROL(wDevID, i, SEQ_VOLMODE, VOL_METHOD_LINEAR);
745 voice[i].note = 0;
746 voice[i].channel = -1;
747 voice[i].cntMark = 0;
748 voice[i].status = sVS_UNUSED;
750 for (i = 0; i < 16; i++) {
751 channel[i].program = 0;
752 channel[i].bender = 8192;
753 channel[i].benderRange = 2;
754 channel[i].bank = 0;
755 channel[i].volume = 127;
756 channel[i].balance = 64;
757 channel[i].expression = 0;
758 channel[i].sustain = 0;
760 extra->counter = 0;
761 extra->drumSetMask = 1 << 9; /* channel 10 is normally drums, sometimes 16 also */
762 SEQ_DUMPBUF();
765 #define IS_DRUM_CHANNEL(_xtra, _chn) ((_xtra)->drumSetMask & (1 << (_chn)))
767 #endif /* HAVE_OSS */
769 /**************************************************************************
770 * modGetDevCaps [internal]
772 static DWORD modGetDevCaps(WORD wDevID, LPMIDIOUTCAPS16 lpCaps,
773 DWORD dwSize)
775 TRACE(midi, "(%04X, %p, %08lX);\n", wDevID, lpCaps, dwSize);
776 if (wDevID == (WORD) MIDI_MAPPER) {
777 lpCaps->wMid = 0x00FF; /* Manufac ID */
778 lpCaps->wPid = 0x0001; /* Product ID */
779 lpCaps->vDriverVersion = 0x001; /* Product Version */
780 strcpy(lpCaps->szPname, "MIDI Mapper (not functional yet)");
781 /* FIXME Does it make any difference ? */
782 lpCaps->wTechnology = MOD_FMSYNTH;
783 lpCaps->wVoices = 14; /* FIXME */
784 lpCaps->wNotes = 14; /* FIXME */
785 /* FIXME Does it make any difference ? */
786 lpCaps->dwSupport = MIDICAPS_VOLUME|MIDICAPS_LRVOLUME;
787 } else {
788 LPMIDIOUTCAPS16 tmplpCaps;
790 if (wDevID >= MODM_NUMDEVS) {
791 TRACE(midi, "MAX_MIDIOUTDRV reached !\n");
792 return MMSYSERR_BADDEVICEID;
794 /* FIXME There is a way to do it so easily, but I'm too
795 * sleepy to think and I want to test
798 tmplpCaps = midiOutDevices[wDevID];
799 lpCaps->wMid = tmplpCaps->wMid;
800 lpCaps->wPid = tmplpCaps->wPid;
801 lpCaps->vDriverVersion = tmplpCaps->vDriverVersion;
802 strcpy(lpCaps->szPname, tmplpCaps->szPname);
803 lpCaps->wTechnology = tmplpCaps->wTechnology;
804 lpCaps->wVoices = tmplpCaps->wVoices;
805 lpCaps->wNotes = tmplpCaps->wNotes;
806 lpCaps->dwSupport = tmplpCaps->dwSupport;
808 return MMSYSERR_NOERROR;
811 /**************************************************************************
812 * modOpen [internal]
814 static DWORD modOpen(WORD wDevID, LPMIDIOPENDESC lpDesc, DWORD dwFlags)
816 TRACE(midi, "(%04X, %p, %08lX);\n", wDevID, lpDesc, dwFlags);
817 if (lpDesc == NULL) {
818 WARN(midi, "Invalid Parameter !\n");
819 return MMSYSERR_INVALPARAM;
821 if (wDevID >= MAX_MIDIOUTDRV) {
822 TRACE(midi,"MAX_MIDIOUTDRV reached !\n");
823 return MMSYSERR_BADDEVICEID;
825 if (MidiOutDev[wDevID].midiDesc != 0) {
826 WARN(midi, "device already open !\n");
827 return MMSYSERR_ALLOCATED;
829 if ((dwFlags & ~CALLBACK_TYPEMASK) != 0) {
830 WARN(midi, "bad dwFlags\n");
831 return MMSYSERR_INVALFLAG;
833 if (midiOutDevices[wDevID] == NULL) {
834 TRACE(midi, "un-allocated wDevID\n");
835 return MMSYSERR_BADDEVICEID;
838 #ifdef HAVE_OSS
839 MidiOutDev[wDevID].lpExtra = 0;
841 switch (midiOutDevices[wDevID]->wTechnology) {
842 case MOD_FMSYNTH:
844 void* extra = xmalloc(sizeof(struct sFMextra) +
845 sizeof(struct sVoice) *
846 (midiOutDevices[wDevID]->wVoices - 1));
848 if (extra == 0) {
849 WARN(midi, "can't alloc extra data !\n");
850 return MMSYSERR_NOMEM;
852 MidiOutDev[wDevID].lpExtra = extra;
853 if (midiOpenSeq() < 0) {
854 MidiOutDev[wDevID].lpExtra = 0;
855 free(extra);
856 return MMSYSERR_ERROR;
858 if (modFMLoad(wDevID) < 0) {
859 midiCloseSeq();
860 MidiOutDev[wDevID].lpExtra = 0;
861 free(extra);
862 return MMSYSERR_ERROR;
864 modFMReset(wDevID);
866 break;
867 case MOD_MIDIPORT:
868 if (midiOpenSeq() < 0) {
869 return MMSYSERR_ALLOCATED;
871 break;
872 default:
873 WARN(midi,"Technology not supported (yet) %d !\n",
874 midiOutDevices[wDevID]->wTechnology);
875 return MMSYSERR_NOTENABLED;
877 #else /* HAVE_OSS */
879 int midi = open (MIDI_DEV, O_WRONLY, 0);
880 MidiOutDev[wDevID].unixdev = 0;
881 if (midi == -1) {
882 WARN(midi, "can't open !\n");
883 return MMSYSERR_ALLOCATED;
885 MidiOutDev[wDevID].unixdev = midi;
887 #endif /* HAVE_OSS */
889 MidiOutDev[wDevID].wFlags = HIWORD(dwFlags & CALLBACK_TYPEMASK);
891 MidiOutDev[wDevID].lpQueueHdr = NULL;
892 MidiOutDev[wDevID].dwTotalPlayed = 0;
893 MidiOutDev[wDevID].bufsize = 0x3FFF;
894 MidiOutDev[wDevID].midiDesc = lpDesc;
896 if (MIDI_NotifyClient(wDevID, MOM_OPEN, 0L, 0L) != MMSYSERR_NOERROR) {
897 WARN(midi,"can't notify client !\n");
898 return MMSYSERR_INVALPARAM;
900 TRACE(midi, "Succesful !\n");
901 return MMSYSERR_NOERROR;
905 /**************************************************************************
906 * modClose [internal]
908 static DWORD modClose(WORD wDevID)
910 int ret = MMSYSERR_NOERROR;
912 TRACE(midi, "(%04X);\n", wDevID);
914 if (MidiOutDev[wDevID].midiDesc == 0) {
915 WARN(midi, "device not opened !\n");
916 return MMSYSERR_ERROR;
918 /* FIXME: should test that no pending buffer is still in the queue for
919 * playing */
921 #ifdef HAVE_OSS
922 if (midiSeqFD == -1) {
923 WARN(midi,"can't close !\n");
924 return MMSYSERR_ERROR;
927 switch (midiOutDevices[wDevID]->wTechnology) {
928 case MOD_FMSYNTH:
929 case MOD_MIDIPORT:
930 midiCloseSeq();
931 break;
932 default:
933 WARN(midi,"Technology not supported (yet) %d !\n",
934 midiOutDevices[wDevID]->wTechnology);
935 return MMSYSERR_NOTENABLED;
938 if (MidiOutDev[wDevID].lpExtra != 0) {
939 free(MidiOutDev[wDevID].lpExtra);
940 MidiOutDev[wDevID].lpExtra = 0;
942 #else
943 if (MidiOutDev[wDevID].unixdev == 0) {
944 WARN(midi,"can't close !\n");
945 return MMSYSERR_NOTENABLED;
947 close(MidiOutDev[wDevID].unixdev);
948 MidiOutDev[wDevID].unixdev = 0;
949 #endif /* HAVE_OSS */
951 MidiOutDev[wDevID].bufsize = 0;
952 if (MIDI_NotifyClient(wDevID, MOM_CLOSE, 0L, 0L) != MMSYSERR_NOERROR) {
953 WARN(midi,"can't notify client !\n");
954 ret = MMSYSERR_INVALPARAM;
956 MidiOutDev[wDevID].midiDesc = 0;
957 return ret;
960 /**************************************************************************
961 * modData [internal]
963 static DWORD modData(WORD wDevID, DWORD dwParam)
965 WORD evt = LOBYTE(LOWORD(dwParam));
966 WORD d1 = HIBYTE(LOWORD(dwParam));
967 WORD d2 = LOBYTE(HIWORD(dwParam));
969 TRACE(midi, "(%04X, %08lX);\n", wDevID, dwParam);
971 #ifdef HAVE_OSS
972 if (midiSeqFD == -1) {
973 WARN(midi,"can't play !\n");
974 return MIDIERR_NODEVICE;
976 switch (midiOutDevices[wDevID]->wTechnology) {
977 case MOD_FMSYNTH:
978 /* FIXME:
979 * - chorus depth controller is not used
982 sFMextra* extra = (sFMextra*)MidiOutDev[wDevID].lpExtra;
983 sVoice* voice = extra->voice;
984 sChannel* channel = extra->channel;
985 int chn = (evt & 0x0F);
986 int i, nv;
988 switch (evt & 0xF0) {
989 case MIDI_NOTEOFF:
990 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
991 /* don't stop sustained notes */
992 if (voice[i].status == sVS_PLAYING && voice[i].channel == chn && voice[i].note == d1) {
993 voice[i].status = sVS_UNUSED;
994 SEQ_STOP_NOTE(wDevID, i, d1, d2);
997 break;
998 case MIDI_NOTEON:
999 if (d2 == 0) { /* note off if velocity == 0 */
1000 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1001 /* don't stop sustained notes */
1002 if (voice[i].status == sVS_PLAYING && voice[i].channel == chn && voice[i].note == d1) {
1003 voice[i].status = sVS_UNUSED;
1004 SEQ_STOP_NOTE(wDevID, i, d1, 64);
1007 break;
1009 /* finding out in this order :
1010 * - an empty voice
1011 * - if replaying the same note on the same channel
1012 * - the older voice (LRU)
1014 for (i = nv = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1015 if (voice[i].status == sVS_UNUSED ||
1016 (voice[i].note == d1 && voice[i].channel == chn)) {
1017 nv = i;
1018 break;
1020 if (voice[i].cntMark < voice[0].cntMark) {
1021 nv = i;
1024 TRACE(midi,
1025 "playing on voice=%d, pgm=%d, pan=0x%02X, vol=0x%02X, "
1026 "bender=0x%02X, note=0x%02X, vel=0x%02X\n",
1027 nv, channel[chn].program,
1028 channel[chn].balance,
1029 channel[chn].volume,
1030 channel[chn].bender, d1, d2);
1032 SEQ_SET_PATCH(wDevID, nv, IS_DRUM_CHANNEL(extra, chn) ?
1033 (128 + d1) : channel[chn].program);
1034 SEQ_BENDER_RANGE(wDevID, nv, channel[chn].benderRange * 100);
1035 SEQ_BENDER(wDevID, nv, channel[chn].bender);
1036 SEQ_CONTROL(wDevID, nv, CTL_PAN, channel[chn].balance);
1037 SEQ_CONTROL(wDevID, nv, CTL_EXPRESSION, channel[chn].expression);
1038 #if 0
1039 /* FIXME: does not really seem to work on my SB card and
1040 * screws everything up... so lay it down
1042 SEQ_CONTROL(wDevID, nv, CTL_MAIN_VOLUME, channel[chn].volume);
1043 #endif
1044 SEQ_START_NOTE(wDevID, nv, d1, d2);
1045 voice[nv].status = channel[chn].sustain ? sVS_SUSTAINED : sVS_PLAYING;
1046 voice[nv].note = d1;
1047 voice[nv].channel = chn;
1048 voice[nv].cntMark = extra->counter++;
1049 break;
1050 case MIDI_KEY_PRESSURE:
1051 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1052 if (voice[i].status != sVS_UNUSED && voice[i].channel == chn && voice[i].note == d1) {
1053 SEQ_KEY_PRESSURE(wDevID, i, d1, d2);
1056 break;
1057 case MIDI_CTL_CHANGE:
1058 switch (d1) {
1059 case CTL_BANK_SELECT: channel[chn].bank = d2; break;
1060 case CTL_MAIN_VOLUME: channel[chn].volume = d2; break;
1061 case CTL_PAN: channel[chn].balance = d2; break;
1062 case CTL_EXPRESSION: channel[chn].expression = d2; break;
1063 case CTL_SUSTAIN: channel[chn].sustain = d2;
1064 if (d2) {
1065 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1066 if (voice[i].status == sVS_PLAYING && voice[i].channel == chn) {
1067 voice[i].status = sVS_SUSTAINED;
1070 } else {
1071 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1072 if (voice[i].status == sVS_SUSTAINED && voice[i].channel == chn) {
1073 voice[i].status = sVS_UNUSED;
1074 SEQ_STOP_NOTE(wDevID, i, voice[i].note, 64);
1078 break;
1079 case CTL_NONREG_PARM_NUM_LSB: channel[chn].nrgPmtLSB = d2; break;
1080 case CTL_NONREG_PARM_NUM_MSB: channel[chn].nrgPmtMSB = d2; break;
1081 case CTL_REGIST_PARM_NUM_LSB: channel[chn].regPmtLSB = d2; break;
1082 case CTL_REGIST_PARM_NUM_MSB: channel[chn].regPmtMSB = d2; break;
1084 case CTL_DATA_ENTRY:
1085 switch ((channel[chn].regPmtMSB << 8) | channel[chn].regPmtLSB) {
1086 case 0x0000:
1087 if (channel[chn].benderRange != d2) {
1088 channel[chn].benderRange = d2;
1089 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1090 if (voice[i].channel == chn) {
1091 SEQ_BENDER_RANGE(wDevID, i, channel[chn].benderRange);
1095 break;
1097 case 0x7F7F:
1098 channel[chn].benderRange = 2;
1099 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1100 if (voice[i].channel == chn) {
1101 SEQ_BENDER_RANGE(wDevID, i, channel[chn].benderRange);
1104 break;
1105 default:
1106 TRACE(midi, "Data entry: regPmt=0x%02x%02x, nrgPmt=0x%02x%02x with %x\n",
1107 channel[chn].regPmtMSB, channel[chn].regPmtLSB,
1108 channel[chn].nrgPmtMSB, channel[chn].nrgPmtLSB,
1109 d2);
1110 break;
1112 break;
1114 case 0x78: /* all sounds off */
1115 /* FIXME: I don't know if I have to take care of the channel
1116 * for this control ?
1118 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1119 if (voice[i].status != sVS_UNUSED && voice[i].channel == chn) {
1120 voice[i].status = sVS_UNUSED;
1121 SEQ_STOP_NOTE(wDevID, i, voice[i].note, 0);
1124 break;
1125 case 0x7B: /* all notes off */
1126 /* FIXME: I don't know if I have to take care of the channel
1127 * for this control ?
1129 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1130 if (voice[i].status == sVS_PLAYING && voice[i].channel == chn) {
1131 voice[i].status = sVS_UNUSED;
1132 SEQ_STOP_NOTE(wDevID, i, voice[i].note, 0);
1135 break;
1136 default:
1137 TRACE(midi, "Dropping MIDI control event 0x%02x(%02x) on channel %d\n",
1138 d1, d2, chn);
1139 break;
1141 break;
1142 case MIDI_PGM_CHANGE:
1143 channel[chn].program = d1;
1144 break;
1145 case MIDI_CHN_PRESSURE:
1146 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1147 if (voice[i].status != sVS_UNUSED && voice[i].channel == chn) {
1148 SEQ_KEY_PRESSURE(wDevID, i, voice[i].note, d1);
1151 break;
1152 case MIDI_PITCH_BEND:
1153 channel[chn].bender = (d2 << 7) + d1;
1154 for (i = 0; i < midiOutDevices[wDevID]->wVoices; i++) {
1155 if (voice[i].channel == chn) {
1156 SEQ_BENDER(wDevID, i, channel[chn].bender);
1159 break;
1160 case MIDI_SYSTEM_PREFIX:
1161 switch (evt & 0x0F) {
1162 case 0x0F: /* Reset */
1163 modFMReset(wDevID);
1164 break;
1165 default:
1166 WARN(midi, "Unsupported (yet) system event %02x\n", evt & 0x0F);
1168 break;
1169 default:
1170 WARN(midi, "Internal error, shouldn't happen (event=%08x)\n", evt & 0xF0);
1171 return MMSYSERR_NOTENABLED;
1174 break;
1175 case MOD_MIDIPORT:
1177 int dev = wDevID - MODM_NUMFMSYNTHDEVS;
1178 if (dev < 0) {
1179 WARN(midi, "Internal error on devID (%u) !\n", wDevID);
1180 return MIDIERR_NODEVICE;
1183 switch (evt & 0xF0) {
1184 case MIDI_NOTEOFF:
1185 case MIDI_NOTEON:
1186 case MIDI_KEY_PRESSURE:
1187 case MIDI_CTL_CHANGE:
1188 case MIDI_PITCH_BEND:
1189 SEQ_MIDIOUT(dev, evt);
1190 SEQ_MIDIOUT(dev, d1);
1191 SEQ_MIDIOUT(dev, d2);
1192 break;
1193 case MIDI_PGM_CHANGE:
1194 case MIDI_CHN_PRESSURE:
1195 SEQ_MIDIOUT(dev, evt);
1196 SEQ_MIDIOUT(dev, d1);
1197 break;
1198 case MIDI_SYSTEM_PREFIX:
1199 switch (evt & 0x0F) {
1200 case 0x00: /* System Exclusive, don't do it on modData,
1201 * should require modLongData*/
1202 case 0x01: /* Undefined */
1203 case 0x04: /* Undefined. */
1204 case 0x05: /* Undefined. */
1205 case 0x07: /* End of Exclusive. */
1206 case 0x09: /* Undefined. */
1207 case 0x0D: /* Undefined. */
1208 break;
1209 case 0x06: /* Tune Request */
1210 case 0x08: /* Timing Clock. */
1211 case 0x0A: /* Start. */
1212 case 0x0B: /* Continue */
1213 case 0x0C: /* Stop */
1214 case 0x0E: /* Active Sensing. */
1215 SEQ_MIDIOUT(dev, evt);
1216 break;
1217 case 0x0F: /* Reset */
1218 /* SEQ_MIDIOUT(dev, evt);
1219 this other way may be better */
1220 SEQ_MIDIOUT(dev, MIDI_SYSTEM_PREFIX);
1221 SEQ_MIDIOUT(dev, 0x7e);
1222 SEQ_MIDIOUT(dev, 0x7f);
1223 SEQ_MIDIOUT(dev, 0x09);
1224 SEQ_MIDIOUT(dev, 0x01);
1225 SEQ_MIDIOUT(dev, 0xf7);
1226 break;
1227 case 0x03: /* Song Select. */
1228 SEQ_MIDIOUT(dev, evt);
1229 SEQ_MIDIOUT(dev, d1);
1230 case 0x02: /* Song Position Pointer. */
1231 SEQ_MIDIOUT(dev, evt);
1232 SEQ_MIDIOUT(dev, d1);
1233 SEQ_MIDIOUT(dev, d2);
1235 break;
1238 break;
1239 default:
1240 WARN(midi, "Technology not supported (yet) %d !\n",
1241 midiOutDevices[wDevID]->wTechnology);
1242 return MMSYSERR_NOTENABLED;
1245 SEQ_DUMPBUF();
1246 #else
1247 if (MidiOutDev[wDevID].unixdev == 0) {
1248 WARN(midi,"can't play !\n");
1249 return MIDIERR_NODEVICE;
1252 WORD event = LOWORD(dwParam);
1253 if (write (MidiOutDev[wDevID].unixdev,
1254 &event, sizeof(event)) != sizeof(WORD)) {
1255 WARN(midi, "error writting unixdev !\n");
1258 #endif /* HAVE_OSS */
1259 return MMSYSERR_NOERROR;
1262 /**************************************************************************
1263 * modLongData [internal]
1265 static DWORD modLongData(WORD wDevID, LPMIDIHDR16 lpMidiHdr, DWORD dwSize)
1267 int count;
1268 LPBYTE lpData;
1270 TRACE(midi, "(%04X, %p, %08lX);\n", wDevID, lpMidiHdr, dwSize);
1272 #ifdef HAVE_OSS
1273 if (midiSeqFD == -1) {
1274 WARN(midi,"can't play !\n");
1275 return MIDIERR_NODEVICE;
1277 #else /* HAVE_OSS */
1278 if (MidiOutDev[wDevID].unixdev == 0) {
1279 WARN(midi,"can't play !\n");
1280 return MIDIERR_NODEVICE;
1282 #endif /* HAVE_OSS */
1284 lpData = ((DWORD)lpMidiHdr == lpMidiHdr->reserved) ?
1285 (LPBYTE)lpMidiHdr->lpData : (LPBYTE)PTR_SEG_TO_LIN(lpMidiHdr->lpData);
1287 if (lpData == NULL)
1288 return MIDIERR_UNPREPARED;
1289 if (!(lpMidiHdr->dwFlags & MHDR_PREPARED))
1290 return MIDIERR_UNPREPARED;
1291 if (lpMidiHdr->dwFlags & MHDR_INQUEUE)
1292 return MIDIERR_STILLPLAYING;
1293 lpMidiHdr->dwFlags &= ~MHDR_DONE;
1294 lpMidiHdr->dwFlags |= MHDR_INQUEUE;
1296 /* FIXME: MS doc is not 100% clear. Will lpData only contain system exclusive
1297 * data, or can it also contain raw MIDI data, to be split up and sent to
1298 * modShortData() ?
1299 * If the latest is true, then the following WARNing will fire up
1301 if (lpData[0] != 0xF0 || lpData[lpMidiHdr->dwBytesRecorded - 1] != 0xF7) {
1302 WARN(midi, "Alledged system exclusive buffer is not correct\nPlease report with MIDI file\n");
1305 TRACE(midi, "dwBytesRecorded %lu !\n", lpMidiHdr->dwBytesRecorded);
1306 TRACE(midi, " %02X %02X %02X %02X\n",
1307 lpData[0], lpData[1], lpData[2], lpData[3]);
1309 #ifdef HAVE_OSS
1310 switch (midiOutDevices[wDevID]->wTechnology) {
1311 case MOD_FMSYNTH:
1312 /* FIXME: I don't think there is much to do here */
1313 break;
1314 case MOD_MIDIPORT:
1315 if (lpData[0] != 0xF0) {
1316 /* Send end of System Exclusive */
1317 SEQ_MIDIOUT(wDevID - MODM_NUMFMSYNTHDEVS, 0xF0);
1318 WARN(midi, "Adding missing 0xF0 marker at the begining of "
1319 "system exclusive byte stream\n");
1321 for (count = 0; count < lpMidiHdr->dwBytesRecorded; count++) {
1322 SEQ_MIDIOUT(wDevID - MODM_NUMFMSYNTHDEVS, lpData[count]);
1324 if (lpData[count - 1] != 0xF7) {
1325 /* Send end of System Exclusive */
1326 SEQ_MIDIOUT(wDevID - MODM_NUMFMSYNTHDEVS, 0xF7);
1327 WARN(midi, "Adding missing 0xF7 marker at the end of "
1328 "system exclusive byte stream\n");
1330 SEQ_DUMPBUF();
1331 break;
1332 default:
1333 WARN(midi, "Technology not supported (yet) %d !\n",
1334 midiOutDevices[wDevID]->wTechnology);
1335 return MMSYSERR_NOTENABLED;
1337 #else /* HAVE_OSS */
1339 int en;
1341 for (count = 0; count < lpMidiHdr->dwBytesRecorded; count++) {
1342 if (write(MidiOutDev[wDevID].unixdev,
1343 lpData, sizeof(WORD)) != sizeof(WORD))
1344 break;
1345 ptr += 2;
1348 en = errno;
1349 TRACE(midi, "after write count = %d\n",count);
1350 if (count != lpMidiHdr->dwBytesRecorded) {
1351 WARN(midi, "error writting unixdev #%d ! (%d != %ld)\n",
1352 MidiOutDev[wDevID].unixdev, count,
1353 lpMidiHdr->dwBytesRecorded);
1354 TRACE(midi, "\terrno = %d error = %s\n",en,strerror(en));
1355 return MMSYSERR_NOTENABLED;
1358 #endif /* HAVE_OSS */
1360 lpMidiHdr->dwFlags &= ~MHDR_INQUEUE;
1361 lpMidiHdr->dwFlags |= MHDR_DONE;
1362 if (MIDI_NotifyClient(wDevID, MOM_DONE, lpMidiHdr->reserved, 0L) != MMSYSERR_NOERROR) {
1363 WARN(midi,"can't notify client !\n");
1364 return MMSYSERR_INVALPARAM;
1366 return MMSYSERR_NOERROR;
1369 /**************************************************************************
1370 * modPrepare [internal]
1372 static DWORD modPrepare(WORD wDevID, LPMIDIHDR16 lpMidiHdr, DWORD dwSize)
1374 TRACE(midi, "(%04X, %p, %08lX);\n", wDevID, lpMidiHdr, dwSize);
1376 #ifdef HAVE_OSS
1377 if (midiSeqFD == -1) {
1378 WARN(midi,"can't prepare !\n");
1379 return MMSYSERR_NOTENABLED;
1381 #else /* HAVE_OSS */
1382 if (MidiOutDev[wDevID].unixdev == 0) {
1383 WARN(midi,"can't prepare !\n");
1384 return MMSYSERR_NOTENABLED;
1386 #endif /* HAVE_OSS */
1387 if (dwSize < sizeof(MIDIHDR16) || lpMidiHdr == 0 ||
1388 lpMidiHdr->lpData == 0 || lpMidiHdr->dwFlags != 0 ||
1389 lpMidiHdr->dwBufferLength >= 0x10000ul) {
1390 WARN(midi, "%p %p %08lx %d/%ld\n", lpMidiHdr, lpMidiHdr->lpData,
1391 lpMidiHdr->dwFlags, sizeof(MIDIHDR16), dwSize);
1392 return MMSYSERR_INVALPARAM;
1395 lpMidiHdr->lpNext = 0;
1396 lpMidiHdr->dwFlags |= MHDR_PREPARED;
1397 lpMidiHdr->dwFlags &= ~MHDR_DONE;
1398 return MMSYSERR_NOERROR;
1401 /**************************************************************************
1402 * modUnprepare [internal]
1404 static DWORD modUnprepare(WORD wDevID, LPMIDIHDR16 lpMidiHdr, DWORD dwSize)
1406 TRACE(midi, "(%04X, %p, %08lX);\n", wDevID, lpMidiHdr, dwSize);
1408 #ifdef HAVE_OSS
1409 if (midiSeqFD == -1) {
1410 WARN(midi,"can't unprepare !\n");
1411 return MMSYSERR_NOTENABLED;
1413 #else /* HAVE_OSS */
1414 if (MidiOutDev[wDevID].unixdev == 0) {
1415 WARN(midi,"can't unprepare !\n");
1416 return MMSYSERR_NOTENABLED;
1418 #endif /* HAVE_OSS */
1419 if (dwSize < sizeof(MIDIHDR16) || lpMidiHdr == 0)
1420 return MMSYSERR_INVALPARAM;
1421 if (lpMidiHdr->dwFlags & MHDR_INQUEUE)
1422 return MIDIERR_STILLPLAYING;
1423 lpMidiHdr->dwFlags &= ~MHDR_PREPARED;
1424 return MMSYSERR_NOERROR;
1427 /**************************************************************************
1428 * modReset [internal]
1430 static DWORD modReset(WORD wDevID)
1432 TRACE(midi, "(%04X);\n", wDevID);
1433 /* FIXME: this function should :
1434 * turn off every note, remove sustain on all channels
1435 * remove any pending buffers
1437 return MMSYSERR_NOTENABLED;
1439 #endif
1441 /*======================================================================*
1442 * MIDI entry points *
1443 *======================================================================*/
1445 /**************************************************************************
1446 * midMessage [sample driver]
1448 DWORD WINAPI midMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
1449 DWORD dwParam1, DWORD dwParam2)
1451 TRACE(midi, "(%04X, %04X, %08lX, %08lX, %08lX);\n",
1452 wDevID, wMsg, dwUser, dwParam1, dwParam2);
1453 switch (wMsg) {
1454 #ifdef SNDCTL_MIDI_INFO
1455 case MIDM_OPEN:
1456 return midOpen(wDevID,(LPMIDIOPENDESC)dwParam1, dwParam2);
1457 case MIDM_CLOSE:
1458 return midClose(wDevID);
1459 case MIDM_ADDBUFFER:
1460 return midAddBuffer(wDevID,(LPMIDIHDR16)dwParam1, dwParam2);
1461 case MIDM_PREPARE:
1462 return midPrepare(wDevID,(LPMIDIHDR16)dwParam1, dwParam2);
1463 case MIDM_UNPREPARE:
1464 return midUnprepare(wDevID,(LPMIDIHDR16)dwParam1, dwParam2);
1465 case MIDM_GETDEVCAPS:
1466 return midGetDevCaps(wDevID,(LPMIDIINCAPS16)dwParam1,dwParam2);
1467 case MIDM_GETNUMDEVS:
1468 return MIDM_NUMDEVS;
1469 case MIDM_RESET:
1470 return midReset(wDevID);
1471 case MIDM_START:
1472 return midStart(wDevID);
1473 case MIDM_STOP:
1474 return midStop(wDevID);
1475 #endif
1476 default:
1477 TRACE(midi, "Unsupported message\n");
1479 return MMSYSERR_NOTSUPPORTED;
1482 /**************************************************************************
1483 * modMessage [sample driver]
1485 DWORD WINAPI modMessage(WORD wDevID, WORD wMsg, DWORD dwUser,
1486 DWORD dwParam1, DWORD dwParam2)
1488 TRACE(midi, "(%04X, %04X, %08lX, %08lX, %08lX);\n",
1489 wDevID, wMsg, dwUser, dwParam1, dwParam2);
1491 switch (wMsg) {
1492 #ifdef SNDCTL_MIDI_INFO
1493 case MODM_OPEN:
1494 return modOpen(wDevID, (LPMIDIOPENDESC)dwParam1, dwParam2);
1495 case MODM_CLOSE:
1496 return modClose(wDevID);
1497 case MODM_DATA:
1498 return modData(wDevID, dwParam1);
1499 case MODM_LONGDATA:
1500 return modLongData(wDevID, (LPMIDIHDR16)dwParam1, dwParam2);
1501 case MODM_PREPARE:
1502 return modPrepare(wDevID, (LPMIDIHDR16)dwParam1, dwParam2);
1503 case MODM_UNPREPARE:
1504 return modUnprepare(wDevID, (LPMIDIHDR16)dwParam1, dwParam2);
1505 case MODM_GETDEVCAPS:
1506 return modGetDevCaps(wDevID, (LPMIDIOUTCAPS16)dwParam1, dwParam2);
1507 case MODM_GETNUMDEVS:
1508 return MODM_NUMDEVS;
1509 case MODM_GETVOLUME:
1510 return 0;
1511 case MODM_SETVOLUME:
1512 return 0;
1513 case MODM_RESET:
1514 return modReset(wDevID);
1515 #endif
1516 default:
1517 TRACE(midi, "Unsupported message\n");
1519 return MMSYSERR_NOTSUPPORTED;
1522 /**************************************************************************
1523 * MIDI_DriverProc32 [sample driver]
1525 LONG MIDI_DriverProc(DWORD dwDevID, HDRVR16 hDriv, DWORD wMsg,
1526 DWORD dwParam1, DWORD dwParam2)
1528 switch (wMsg) {
1529 case DRV_LOAD: return 1;
1530 case DRV_FREE: return 1;
1531 case DRV_OPEN: return 1;
1532 case DRV_CLOSE: return 1;
1533 case DRV_ENABLE: return 1;
1534 case DRV_DISABLE: return 1;
1535 case DRV_QUERYCONFIGURE: return 1;
1536 case DRV_CONFIGURE: MessageBoxA(0, "Sample Midi Linux Driver !", "MMLinux Driver", MB_OK); return 1;
1537 case DRV_INSTALL: return DRVCNF_RESTART;
1538 case DRV_REMOVE: return DRVCNF_RESTART;
1539 default:
1540 TRACE(midi, "Sending msg=%lu to default driver proc\n", wMsg);
1541 return DefDriverProc16(dwDevID, hDriv, wMsg, dwParam1, dwParam2);
1545 /**************************************************************************
1546 * MIDI_DriverProc16 [sample driver]
1548 LONG MIDI_DriverProc16(DWORD dwDevID, HDRVR16 hDriv, WORD wMsg,
1549 DWORD dwParam1, DWORD dwParam2)
1551 switch (wMsg) {
1552 case DRV_LOAD: return 1;
1553 case DRV_FREE: return 1;
1554 case DRV_OPEN: return 1;
1555 case DRV_CLOSE: return 1;
1556 case DRV_ENABLE: return 1;
1557 case DRV_DISABLE: return 1;
1558 case DRV_QUERYCONFIGURE: return 1;
1559 case DRV_CONFIGURE: MessageBoxA(0, "Sample Midi Linux Driver !", "MMLinux Driver", MB_OK); return 1;
1560 case DRV_INSTALL: return DRVCNF_RESTART;
1561 case DRV_REMOVE: return DRVCNF_RESTART;
1562 default:
1563 TRACE(midi, "Sending msg=%u to default driver proc\n", wMsg);
1564 return DefDriverProc(dwDevID, hDriv, wMsg, dwParam1, dwParam2);
1568 /*-----------------------------------------------------------------------*/