DOSBox-X
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator
src/gui/midi_alsa.h
00001 /*
00002  *  Copyright (C) 2002-2013  The DOSBox Team
00003  *
00004  *  This program is free software; you can redistribute it and/or modify
00005  *  it under the terms of the GNU General Public License as published by
00006  *  the Free Software Foundation; either version 2 of the License, or
00007  *  (at your option) any later version.
00008  *
00009  *  This program is distributed in the hope that it will be useful,
00010  *  but WITHOUT ANY WARRANTY; without even the implied warranty of
00011  *  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
00012  *  GNU General Public License for more details.
00013  *
00014  *  You should have received a copy of the GNU General Public License
00015  *  along with this program; if not, write to the Free Software
00016  *  Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307, USA.
00017  */
00018 
00019 
00020 #define ALSA_PCM_OLD_HW_PARAMS_API
00021 #define ALSA_PCM_OLD_SW_PARAMS_API
00022 #include <alsa/asoundlib.h>
00023 #include <ctype.h>
00024 #include <string>
00025 #include <sstream>
00026 #define ADDR_DELIM      ".:"
00027 
00028 #if ((SND_LIB_MINOR >= 6) && (SND_LIB_MAJOR == 0)) || (SND_LIB_MAJOR >= 1)
00029 #define snd_seq_flush_output(x) snd_seq_drain_output(x)
00030 #define snd_seq_set_client_group(x,name)        /*nop */
00031 #define my_snd_seq_open(seqp) snd_seq_open(seqp, "hw", SND_SEQ_OPEN_OUTPUT, 0)
00032 #else
00033 /* SND_SEQ_OPEN_OUT causes oops on early version of ALSA */
00034 #define my_snd_seq_open(seqp) snd_seq_open(seqp, SND_SEQ_OPEN)
00035 #endif
00036 
00037 class MidiHandler_alsa : public MidiHandler {
00038 private:
00039         snd_seq_event_t ev;
00040         snd_seq_t *seq_handle;
00041         int seq_client, seq_port;
00042         int my_client, my_port;
00043         void send_event(int do_flush) {
00044                 snd_seq_ev_set_direct(&ev);
00045                 snd_seq_ev_set_source(&ev, my_port);
00046                 snd_seq_ev_set_dest(&ev, seq_client, seq_port);
00047 
00048                 snd_seq_event_output(seq_handle, &ev);
00049                 if (do_flush)
00050                         snd_seq_flush_output(seq_handle);
00051         }
00052 
00053         bool parse_addr(const char *arg, int *client, int *port) {
00054                 std::string in(arg);
00055                 if(in.empty()) return false;
00056 
00057                 if(in[0] == 's' || in[0] == 'S') {
00058                         *client = SND_SEQ_ADDRESS_SUBSCRIBERS;
00059                         *port = 0;
00060                         return true;
00061                 }
00062 
00063                 if(in.find_first_of(ADDR_DELIM) == std::string::npos) return false;
00064                 std::istringstream inp(in);
00065                 int val1, val2; char c;
00066                 if(!(inp >> val1)) return false;
00067                 if(!(inp >> c   )) return false;
00068                 if(!(inp >> val2)) return false;
00069                 *client = val1; *port = val2;
00070                 return true;
00071         }
00072 public:
00073         MidiHandler_alsa() : MidiHandler() {};
00074         const char* GetName(void) { return "alsa"; }
00075         void PlaySysex(Bit8u * sysex,Bitu len) {
00076                 snd_seq_ev_set_sysex(&ev, len, sysex);
00077                 send_event(1);
00078         }
00079 
00080         void PlayMsg(Bit8u * msg) {
00081                 ev.type = SND_SEQ_EVENT_OSS;
00082 
00083                 ev.data.raw32.d[0] = msg[0];
00084                 ev.data.raw32.d[1] = msg[1];
00085                 ev.data.raw32.d[2] = msg[2];
00086 
00087                 unsigned char chanID = msg[0] & 0x0F;
00088                 switch (msg[0] & 0xF0) {
00089                 case 0x80:
00090                         snd_seq_ev_set_noteoff(&ev, chanID, msg[1], msg[2]);
00091                         send_event(1);
00092                         break;
00093                 case 0x90:
00094                         snd_seq_ev_set_noteon(&ev, chanID, msg[1], msg[2]);
00095                         send_event(1);
00096                         break;
00097                 case 0xB0:
00098                         snd_seq_ev_set_controller(&ev, chanID, msg[1], msg[2]);
00099                         send_event(1);
00100                         break;
00101                 case 0xC0:
00102                         snd_seq_ev_set_pgmchange(&ev, chanID, msg[1]);
00103                         send_event(0);
00104                         break;
00105                 case 0xD0:
00106                         snd_seq_ev_set_chanpress(&ev, chanID, msg[1]);
00107                         send_event(0);
00108                         break;
00109                 case 0xE0:{
00110                                 long theBend = ((long)msg[1] + (long)(msg[2] << 7)) - 0x2000;
00111                                 snd_seq_ev_set_pitchbend(&ev, chanID, theBend);
00112                                 send_event(1);
00113                         }
00114                         break;
00115                 default:
00116                         LOG(LOG_MISC,LOG_DEBUG)("ALSA:Unknown Command: %08lx", (long)msg);
00117                         send_event(1);
00118                         break;
00119                 }
00120         }       
00121 
00122         void Close(void) {
00123                 if (seq_handle)
00124                         snd_seq_close(seq_handle);
00125         }
00126 
00127         bool Open(const char * conf) {
00128                 char var[10];
00129                 unsigned int caps;
00130                 bool defaultport = true; //try 17:0. Seems to be default nowadays
00131 
00132                 // try to use port specified in config file
00133                 if (conf && conf[0]) { 
00134                         safe_strncpy(var, conf, 10);
00135                         if (!parse_addr(var, &seq_client, &seq_port)) {
00136                                 LOG(LOG_MISC,LOG_WARN)("ALSA:Invalid alsa port %s", var);
00137                                 return false;
00138                         }
00139                         defaultport = false;
00140                 }
00141                 // default port if none specified
00142                 else if (!parse_addr("65:0", &seq_client, &seq_port)) {
00143                         LOG(LOG_MISC,LOG_WARN)("ALSA:Invalid alsa port 65:0");
00144                         return false;
00145                 }
00146 
00147                 if (my_snd_seq_open(&seq_handle)) {
00148                         LOG(LOG_MISC,LOG_WARN)("ALSA:Can't open sequencer");
00149                         return false;
00150                 }
00151         
00152                 my_client = snd_seq_client_id(seq_handle);
00153                 snd_seq_set_client_name(seq_handle, "DOSBOX");
00154                 snd_seq_set_client_group(seq_handle, "input");
00155         
00156                 caps = SND_SEQ_PORT_CAP_READ;
00157                 if (seq_client == SND_SEQ_ADDRESS_SUBSCRIBERS)
00158                         caps = ~((unsigned int)SND_SEQ_PORT_CAP_SUBS_READ);
00159                 my_port =
00160                           snd_seq_create_simple_port(seq_handle, "DOSBOX", caps,
00161                           SND_SEQ_PORT_TYPE_MIDI_GENERIC | SND_SEQ_PORT_TYPE_APPLICATION);
00162                 if (my_port < 0) {
00163                         snd_seq_close(seq_handle);
00164                         LOG(LOG_MISC,LOG_WARN)("ALSA:Can't create ALSA port");
00165                         return false;
00166                 }
00167         
00168                 if (seq_client != SND_SEQ_ADDRESS_SUBSCRIBERS) {
00169                         /* subscribe to MIDI port */
00170                         if (snd_seq_connect_to(seq_handle, my_port, seq_client, seq_port) < 0) {
00171                                 if (defaultport) { //if port "65:0" (default) try "17:0" as well
00172                                         seq_client = 17; seq_port = 0; //Update reported values
00173                                         if(snd_seq_connect_to(seq_handle,my_port,seq_client,seq_port) < 0) { //Try 128:0 Timidity port as well
00174 //                                              seq_client = 128; seq_port = 0; //Update reported values
00175 //                                              if(snd_seq_connect_to(seq_handle,my_port,seq_client,seq_port) < 0) {
00176                                                         snd_seq_close(seq_handle);
00177                                                         LOG(LOG_MISC,LOG_WARN)("ALSA:Can't subscribe to MIDI port (65:0) nor (17:0)");
00178                                                         return false;
00179 //                                              }
00180                                         }
00181                                 } else {
00182                                         snd_seq_close(seq_handle);
00183                                         LOG(LOG_MISC,LOG_WARN)("ALSA:Can't subscribe to MIDI port (%d:%d)", seq_client, seq_port);
00184                                         return false;
00185                                 }
00186                         }
00187                 }
00188 
00189                 LOG(LOG_MISC,LOG_DEBUG)("ALSA:Client initialised [%d:%d]", seq_client, seq_port);
00190                 return true;
00191         }
00192 
00193 };
00194 
00195 MidiHandler_alsa Midi_alsa;