| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
|
| 10 |
|
| 11 |
|
| 12 |
|
| 13 |
|
| 14 |
|
| 15 |
|
| 16 |
|
| 17 |
|
| 18 |
|
| 19 |
|
| 20 |
|
| 21 |
#define ALSA_PCM_OLD_HW_PARAMS_API |
| 22 |
#define ALSA_PCM_OLD_SW_PARAMS_API |
| 23 |
#include <alsa/asoundlib.h> |
| 24 |
#include <ctype.h> |
| 25 |
#include <string> |
| 26 |
#include <sstream> |
| 27 |
#define ADDR_DELIM ".:" |
| 28 |
|
| 29 |
#if ((SND_LIB_MINOR >= 6) && (SND_LIB_MAJOR == 0)) || (SND_LIB_MAJOR >= 1) |
| 30 |
#define snd_seq_flush_output(x) snd_seq_drain_output(x) |
| 31 |
#define snd_seq_set_client_group(x,name) |
| 32 |
#define my_snd_seq_open(seqp) snd_seq_open(seqp, "hw", SND_SEQ_OPEN_OUTPUT, 0) |
| 33 |
#else |
| 34 |
|
| 35 |
#define my_snd_seq_open(seqp) snd_seq_open(seqp, SND_SEQ_OPEN) |
| 36 |
#endif |
| 37 |
|
| 38 |
class MidiHandler_alsa : public MidiHandler { |
| 39 |
private: |
| 40 |
snd_seq_event_t ev; |
| 41 |
snd_seq_t *seq_handle; |
| 42 |
int seq_client, seq_port; |
| 43 |
int my_client, my_port; |
| 44 |
void send_event(int do_flush) { |
| 45 |
snd_seq_ev_set_direct(&ev); |
| 46 |
snd_seq_ev_set_source(&ev, my_port); |
| 47 |
snd_seq_ev_set_dest(&ev, seq_client, seq_port); |
| 48 |
|
| 49 |
snd_seq_event_output(seq_handle, &ev); |
| 50 |
if (do_flush) |
| 51 |
snd_seq_flush_output(seq_handle); |
| 52 |
} |
| 53 |
|
| 54 |
bool parse_addr(const char *arg, int *client, int *port) { |
| 55 |
std::string in(arg); |
| 56 |
if(in.empty()) return false; |
| 57 |
|
| 58 |
if(in[0] == 's' || in[0] == 'S') { |
| 59 |
*client = SND_SEQ_ADDRESS_SUBSCRIBERS; |
| 60 |
*port = 0; |
| 61 |
return true; |
| 62 |
} |
| 63 |
|
| 64 |
if(in.find_first_of(ADDR_DELIM) == std::string::npos) return false; |
| 65 |
std::istringstream inp(in); |
| 66 |
int val1, val2; char c; |
| 67 |
if(!(inp >> val1)) return false; |
| 68 |
if(!(inp >> c )) return false; |
| 69 |
if(!(inp >> val2)) return false; |
| 70 |
*client = val1; *port = val2; |
| 71 |
return true; |
| 72 |
} |
| 73 |
public: |
| 74 |
MidiHandler_alsa() : MidiHandler() {}; |
| 75 |
const char* GetName(void) { return "alsa"; } |
| 76 |
void PlaySysex(Bit8u * sysex,Bitu len) { |
| 77 |
snd_seq_ev_set_sysex(&ev, len, sysex); |
| 78 |
send_event(1); |
| 79 |
} |
| 80 |
|
| 81 |
void PlayMsg(Bit8u * msg) { |
| 82 |
ev.type = SND_SEQ_EVENT_OSS; |
| 83 |
|
| 84 |
ev.data.raw32.d[0] = msg[0]; |
| 85 |
ev.data.raw32.d[1] = msg[1]; |
| 86 |
ev.data.raw32.d[2] = msg[2]; |
| 87 |
|
| 88 |
unsigned char chanID = msg[0] & 0x0F; |
| 89 |
switch (msg[0] & 0xF0) { |
| 90 |
case 0x80: |
| 91 |
snd_seq_ev_set_noteoff(&ev, chanID, msg[1], msg[2]); |
| 92 |
send_event(1); |
| 93 |
break; |
| 94 |
case 0x90: |
| 95 |
snd_seq_ev_set_noteon(&ev, chanID, msg[1], msg[2]); |
| 96 |
send_event(1); |
| 97 |
break; |
| 98 |
case 0xB0: |
| 99 |
snd_seq_ev_set_controller(&ev, chanID, msg[1], msg[2]); |
| 100 |
send_event(1); |
| 101 |
break; |
| 102 |
case 0xC0: |
| 103 |
snd_seq_ev_set_pgmchange(&ev, chanID, msg[1]); |
| 104 |
send_event(0); |
| 105 |
break; |
| 106 |
case 0xD0: |
| 107 |
snd_seq_ev_set_chanpress(&ev, chanID, msg[1]); |
| 108 |
send_event(0); |
| 109 |
break; |
| 110 |
case 0xE0:{ |
| 111 |
long theBend = ((long)msg[1] + (long)(msg[2] << 7)) - 0x2000; |
| 112 |
snd_seq_ev_set_pitchbend(&ev, chanID, theBend); |
| 113 |
send_event(1); |
| 114 |
} |
| 115 |
break; |
| 116 |
default: |
| 117 |
LOG(LOG_MISC,LOG_WARN)("ALSA:Unknown Command: %08lx", (long)msg); |
| 118 |
send_event(1); |
| 119 |
break; |
| 120 |
} |
| 121 |
} |
| 122 |
|
| 123 |
void Close(void) { |
| 124 |
if (seq_handle) |
| 125 |
snd_seq_close(seq_handle); |
| 126 |
} |
| 127 |
|
| 128 |
bool Open(const char * conf) { |
| 129 |
char var[10]; |
| 130 |
unsigned int caps; |
| 131 |
bool defaultport = true; |
| 132 |
|
| 133 |
|
| 134 |
if (conf && conf[0]) { |
| 135 |
safe_strncpy(var, conf, 10); |
| 136 |
if (!parse_addr(var, &seq_client, &seq_port)) { |
| 137 |
LOG_MSG("ALSA:Invalid alsa port %s", var); |
| 138 |
return false; |
| 139 |
} |
| 140 |
defaultport = false; |
| 141 |
} |
| 142 |
|
| 143 |
else if (!parse_addr("65:0", &seq_client, &seq_port)) { |
| 144 |
LOG_MSG("ALSA:Invalid alsa port 65:0"); |
| 145 |
return false; |
| 146 |
} |
| 147 |
|
| 148 |
if (my_snd_seq_open(&seq_handle)) { |
| 149 |
LOG_MSG("ALSA:Can't open sequencer"); |
| 150 |
return false; |
| 151 |
} |
| 152 |
|
| 153 |
my_client = snd_seq_client_id(seq_handle); |
| 154 |
snd_seq_set_client_name(seq_handle, "DOSBOX"); |
| 155 |
snd_seq_set_client_group(seq_handle, "input"); |
| 156 |
|
| 157 |
caps = SND_SEQ_PORT_CAP_READ; |
| 158 |
if (seq_client == SND_SEQ_ADDRESS_SUBSCRIBERS) |
| 159 |
caps = ~SND_SEQ_PORT_CAP_SUBS_READ; |
| 160 |
my_port = |
| 161 |
snd_seq_create_simple_port(seq_handle, "DOSBOX", caps, |
| 162 |
SND_SEQ_PORT_TYPE_MIDI_GENERIC | SND_SEQ_PORT_TYPE_APPLICATION); |
| 163 |
if (my_port < 0) { |
| 164 |
snd_seq_close(seq_handle); |
| 165 |
LOG_MSG("ALSA:Can't create ALSA port"); |
| 166 |
return false; |
| 167 |
} |
| 168 |
|
| 169 |
if (seq_client != SND_SEQ_ADDRESS_SUBSCRIBERS) { |
| 170 |
|
| 171 |
if (snd_seq_connect_to(seq_handle, my_port, seq_client, seq_port) < 0) { |
| 172 |
if (defaultport) { |
| 173 |
seq_client = 17; seq_port = 0; |
| 174 |
if(snd_seq_connect_to(seq_handle,my_port,seq_client,seq_port) < 0) { |
| 175 |
|
| 176 |
|
| 177 |
snd_seq_close(seq_handle); |
| 178 |
LOG_MSG("ALSA:Can't subscribe to MIDI port (65:0) nor (17:0)"); |
| 179 |
return false; |
| 180 |
|
| 181 |
} |
| 182 |
} else { |
| 183 |
snd_seq_close(seq_handle); |
| 184 |
LOG_MSG("ALSA:Can't subscribe to MIDI port (%d:%d)", seq_client, seq_port); |
| 185 |
return false; |
| 186 |
} |
| 187 |
} |
| 188 |
} |
| 189 |
|
| 190 |
LOG_MSG("ALSA:Client initialised [%d:%d]", seq_client, seq_port); |
| 191 |
return true; |
| 192 |
} |
| 193 |
|
| 194 |
}; |
| 195 |
|
| 196 |
MidiHandler_alsa Midi_alsa; |