| 1 | /* |
| 2 | * Copyright (C) 2002-2010 The DOSBox Team |
| 3 | * |
| 4 | * This program is free software; you can redistribute it and/or modify |
| 5 | * it under the terms of the GNU General Public License as published by |
| 6 | * the Free Software Foundation; either version 2 of the License, or |
| 7 | * (at your option) any later version. |
| 8 | * |
| 9 | * This program is distributed in the hope that it will be useful, |
| 10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 12 | * GNU General Public License for more details. |
| 13 | * |
| 14 | * You should have received a copy of the GNU General Public License |
| 15 | * along with this program; if not, write to the Free Software |
| 16 | * Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA. |
| 17 | */ |
| 18 | |
| 19 | #include <assert.h> |
| 20 | #include <string.h> |
| 21 | #include <stdlib.h> |
| 22 | |
| 23 | #include "dosbox.h" |
| 24 | #include "cross.h" |
| 25 | #include "support.h" |
| 26 | #include "setup.h" |
| 27 | #include "mapper.h" |
| 28 | #include "pic.h" |
| 29 | #include "hardware.h" |
| 30 | |
| 31 | #define SYSEX_SIZE 1024 |
| 32 | #define RAWBUF 1024 |
| 33 | |
| 34 | Bit8u MIDI_evt_len[256] = { |
| 35 | 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, // 0x00 |
| 36 | 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, // 0x10 |
| 37 | 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, // 0x20 |
| 38 | 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, // 0x30 |
| 39 | 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, // 0x40 |
| 40 | 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, // 0x50 |
| 41 | 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, // 0x60 |
| 42 | 0,0,0,0, 0,0,0,0, 0,0,0,0, 0,0,0,0, // 0x70 |
| 43 | |
| 44 | 3,3,3,3, 3,3,3,3, 3,3,3,3, 3,3,3,3, // 0x80 |
| 45 | 3,3,3,3, 3,3,3,3, 3,3,3,3, 3,3,3,3, // 0x90 |
| 46 | 3,3,3,3, 3,3,3,3, 3,3,3,3, 3,3,3,3, // 0xa0 |
| 47 | 3,3,3,3, 3,3,3,3, 3,3,3,3, 3,3,3,3, // 0xb0 |
| 48 | |
| 49 | 2,2,2,2, 2,2,2,2, 2,2,2,2, 2,2,2,2, // 0xc0 |
| 50 | 2,2,2,2, 2,2,2,2, 2,2,2,2, 2,2,2,2, // 0xd0 |
| 51 | |
| 52 | 3,3,3,3, 3,3,3,3, 3,3,3,3, 3,3,3,3, // 0xe0 |
| 53 | |
| 54 | 0,2,3,2, 0,0,1,0, 1,0,1,1, 1,0,1,0 // 0xf0 |
| 55 | }; |
| 56 | |
| 57 | class MidiHandler; |
| 58 | |
| 59 | MidiHandler * handler_list=0; |
| 60 | |
| 61 | class MidiHandler { |
| 62 | public: |
| 63 | MidiHandler() { |
| 64 | next=handler_list; |
| 65 | handler_list=this; |
| 66 | }; |
| 67 | virtual bool Open(const char * /*conf*/) { return true; }; |
| 68 | virtual void Close(void) {}; |
| 69 | virtual void PlayMsg(Bit8u * /*msg*/) {}; |
| 70 | virtual void PlaySysex(Bit8u * /*sysex*/,Bitu /*len*/) {}; |
| 71 | virtual const char * GetName(void) { return "none"; }; |
| 72 | virtual ~MidiHandler() { }; |
| 73 | MidiHandler * next; |
| 74 | }; |
| 75 | |
| 76 | MidiHandler Midi_none; |
| 77 | |
| 78 | /* Include different midi drivers, lowest ones get checked first for default */ |
| 79 | |
| 80 | #if defined(MACOSX) |
| 81 | |
| 82 | #include "midi_coremidi.h" |
| 83 | #include "midi_coreaudio.h" |
| 84 | |
| 85 | #elif defined (WIN32) |
| 86 | |
| 87 | #include "midi_win32.h" |
| 88 | |
| 89 | #else |
| 90 | |
| 91 | #include "midi_oss.h" |
| 92 | |
| 93 | #endif |
| 94 | |
| 95 | #if defined (HAVE_ALSA) |
| 96 | |
| 97 | #include "midi_alsa.h" |
| 98 | |
| 99 | #endif |
| 100 | |
| 101 | static struct { |
| 102 | Bitu status; |
| 103 | Bitu cmd_len; |
| 104 | Bitu cmd_pos; |
| 105 | Bit8u cmd_buf[8]; |
| 106 | Bit8u rt_buf[8]; |
| 107 | struct { |
| 108 | Bit8u buf[SYSEX_SIZE]; |
| 109 | Bitu used; |
| 110 | } sysex; |
| 111 | bool available; |
| 112 | MidiHandler * handler; |
| 113 | } midi; |
| 114 | |
| 115 | void MIDI_RawOutByte(Bit8u data) { |
| 116 | /* Test for a realtime MIDI message */ |
| 117 | if (data>=0xf8) { |
| 118 | midi.rt_buf[0]=data; |
| 119 | midi.handler->PlayMsg(midi.rt_buf); |
| 120 | return; |
| 121 | } |
| 122 | /* Test for a active sysex tranfer */ |
| 123 | if (midi.status==0xf0) { |
| 124 | if (!(data&0x80)) { |
| 125 | if (midi.sysex.used<(SYSEX_SIZE-1)) midi.sysex.buf[midi.sysex.used++]=data; |
| 126 | return; |
| 127 | } else { |
| 128 | midi.sysex.buf[midi.sysex.used++]=0xf7; |
| 129 | midi.handler->PlaySysex(midi.sysex.buf,midi.sysex.used); |
| 130 | LOG(LOG_ALL,LOG_NORMAL)("Sysex message size %d",midi.sysex.used); |
| 131 | if (CaptureState & CAPTURE_MIDI) { |
| 132 | CAPTURE_AddMidi( true, midi.sysex.used-1, &midi.sysex.buf[1]); |
| 133 | } |
| 134 | } |
| 135 | } |
| 136 | if (data&0x80) { |
| 137 | midi.status=data; |
| 138 | midi.cmd_pos=0; |
| 139 | midi.cmd_len=MIDI_evt_len[data]; |
| 140 | if (midi.status==0xf0) { |
| 141 | midi.sysex.buf[0]=0xf0; |
| 142 | midi.sysex.used=1; |
| 143 | } |
| 144 | } |
| 145 | if (midi.cmd_len) { |
| 146 | midi.cmd_buf[midi.cmd_pos++]=data; |
| 147 | if (midi.cmd_pos >= midi.cmd_len) { |
| 148 | if (CaptureState & CAPTURE_MIDI) { |
| 149 | CAPTURE_AddMidi(false, midi.cmd_len, midi.cmd_buf); |
| 150 | } |
| 151 | midi.handler->PlayMsg(midi.cmd_buf); |
| 152 | midi.cmd_pos=1; //Use Running status |
| 153 | } |
| 154 | } |
| 155 | } |
| 156 | |
| 157 | bool MIDI_Available(void) { |
| 158 | return midi.available; |
| 159 | } |
| 160 | |
| 161 | class MIDI:public Module_base{ |
| 162 | public: |
| 163 | MIDI(Section* configuration):Module_base(configuration){ |
| 164 | Section_prop * section=static_cast<Section_prop *>(configuration); |
| 165 | const char * dev=section->Get_string("mididevice"); |
| 166 | const char * conf=section->Get_string("midiconfig"); |
| 167 | /* If device = "default" go for first handler that works */ |
| 168 | MidiHandler * handler; |
| 169 | // MAPPER_AddHandler(MIDI_SaveRawEvent,MK_f8,MMOD1|MMOD2,"caprawmidi","Cap MIDI"); |
| 170 | midi.status=0x00; |
| 171 | midi.cmd_pos=0; |
| 172 | midi.cmd_len=0; |
| 173 | if (!strcasecmp(dev,"default")) goto getdefault; |
| 174 | handler=handler_list; |
| 175 | while (handler) { |
| 176 | if (!strcasecmp(dev,handler->GetName())) { |
| 177 | if (!handler->Open(conf)) { |
| 178 | LOG_MSG("MIDI:Can't open device:%s with config:%s.",dev,conf); |
| 179 | goto getdefault; |
| 180 | } |
| 181 | midi.handler=handler; |
| 182 | midi.available=true; |
| 183 | LOG_MSG("MIDI:Opened device:%s",handler->GetName()); |
| 184 | return; |
| 185 | } |
| 186 | handler=handler->next; |
| 187 | } |
| 188 | LOG_MSG("MIDI:Can't find device:%s, finding default handler.",dev); |
| 189 | getdefault: |
| 190 | handler=handler_list; |
| 191 | while (handler) { |
| 192 | if (handler->Open(conf)) { |
| 193 | midi.available=true; |
| 194 | midi.handler=handler; |
| 195 | LOG_MSG("MIDI:Opened device:%s",handler->GetName()); |
| 196 | return; |
| 197 | } |
| 198 | handler=handler->next; |
| 199 | } |
| 200 | /* This shouldn't be possible */ |
| 201 | } |
| 202 | ~MIDI(){ |
| 203 | if(midi.available) midi.handler->Close(); |
| 204 | midi.available = false; |
| 205 | midi.handler = 0; |
| 206 | } |
| 207 | }; |
| 208 | |
| 209 | |
| 210 | static MIDI* test; |
| 211 | void MIDI_Destroy(Section* /*sec*/){ |
| 212 | delete test; |
| 213 | } |
| 214 | void MIDI_Init(Section * sec) { |
| 215 | test = new MIDI(sec); |
| 216 | sec->AddDestroyFunction(&MIDI_Destroy,true); |
| 217 | } |