Debian Codegraph @

src/gui/midi_alsa.h

raw symbols SCIP
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 /* $Id: midi_alsa.h,v 1.20 2009-04-27 17:33:12 qbix79 Exp $ */
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) /*nop */
32 #define my_snd_seq_open(seqp) snd_seq_open(seqp, "hw", SND_SEQ_OPEN_OUTPUT, 0)
33 #else
34 /* SND_SEQ_OPEN_OUT causes oops on early version of ALSA */
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; //try 17:0. Seems to be default nowadays
132
133 // try to use port specified in config file
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 // default port if none specified
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 /* subscribe to MIDI port */
171 if (snd_seq_connect_to(seq_handle, my_port, seq_client, seq_port) < 0) {
172 if (defaultport) { //if port "65:0" (default) try "17:0" as well
173 seq_client = 17; seq_port = 0; //Update reported values
174 if(snd_seq_connect_to(seq_handle,my_port,seq_client,seq_port) < 0) { //Try 128:0 Timidity port as well
175 // seq_client = 128; seq_port = 0; //Update reported values
176 // if(snd_seq_connect_to(seq_handle,my_port,seq_client,seq_port) < 0) {
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;