1 /* ScummVM - Graphic Adventure Engine
2  *
3  * ScummVM is the legal property of its developers, whose names
4  * are too numerous to list here. Please refer to the COPYRIGHT
5  * file distributed with this source distribution.
6  *
7  * This program is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU General Public License
9  * as published by the Free Software Foundation; either version 2
10  * of the License, or (at your option) any later version.
11  *
12  * This program is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
15  * GNU General Public License for more details.
16  *
17  * You should have received a copy of the GNU General Public License
18  * along with this program; if not, write to the Free Software
19  * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
20  *
21  */
22 
23 // Disable symbol overrides so that we can use system headers.
24 #define FORBIDDEN_SYMBOL_ALLOW_ALL
25 
26 #include "common/scummsys.h"
27 
28 #if defined(USE_SNDIO)
29 
30 #include "common/error.h"
31 #include "common/textconsole.h"
32 #include "common/util.h"
33 #include "audio/musicplugin.h"
34 #include "audio/mpu401.h"
35 
36 #include <sndio.h>
37 
38 ////////////////////////////////////////
39 //
40 // sndio MIDI driver
41 //
42 ////////////////////////////////////////
43 
44 class MidiDriver_Sndio : public MidiDriver_MPU401 {
45 public:
46 	MidiDriver_Sndio();
47 	int open();
isOpen() const48 	bool isOpen() const { return hdl != NULL; }
49 	void close();
50 	void send(uint32 b);
51 	void sysEx(const byte *msg, uint16 length);
52 
53 private:
54 	struct mio_hdl *hdl;
55 };
56 
MidiDriver_Sndio()57 MidiDriver_Sndio::MidiDriver_Sndio() {
58 	hdl = NULL;
59 }
60 
open()61 int MidiDriver_Sndio::open() {
62 	if (hdl != NULL)
63 		return MERR_ALREADY_OPEN;
64 
65 	hdl = ::mio_open(NULL, MIO_OUT, 0);
66 	if (hdl == NULL)
67 		warning("Could open MIDI port (no music)");
68 	return 0;
69 }
70 
close()71 void MidiDriver_Sndio::close() {
72 	MidiDriver_MPU401::close();
73 	if (!hdl)
74 		return;
75 	mio_close(hdl);
76 	hdl = NULL;
77 }
78 
send(uint32 b)79 void MidiDriver_Sndio::send(uint32 b) {
80 	unsigned char buf[4];
81 	unsigned int len;
82 
83 	if (!hdl)
84 		return;
85 	buf[0] = b & 0xff;
86 	buf[1] = (b >> 8) & 0xff;
87 	buf[2] = (b >> 16) & 0xff;
88 	buf[3] = (b >> 24) & 0xff;
89 	switch (buf[0] & 0xf0) {
90 	case 0xf0:
91 		return;
92 	case 0xc0:
93 	case 0xd0:
94 		len = 2;
95 		break;
96 	default:
97 		len = 3;
98 	}
99 	mio_write(hdl, buf, len);
100 }
101 
sysEx(const byte * msg,uint16 length)102 void MidiDriver_Sndio::sysEx(const byte *msg, uint16 length) {
103 	if (!hdl)
104 		return;
105 
106 	unsigned char buf[266];
107 
108 	assert(length + 2 <= ARRAYSIZE(buf));
109 
110 	// Add SysEx frame
111 	buf[0] = 0xF0;
112 	memcpy(buf + 1, msg, length);
113 	buf[length + 1] = 0xF7;
114 
115 	mio_write(hdl, buf, length + 2);
116 }
117 
118 
119 // Plugin interface
120 
121 class SndioMusicPlugin : public MusicPluginObject {
122 public:
getName() const123 	const char *getName() const {
124 		return "Sndio";
125 	}
126 
getId() const127 	const char *getId() const {
128 		return "sndio";
129 	}
130 
131 	MusicDevices getDevices() const;
132 	Common::Error createInstance(MidiDriver **mididriver, MidiDriver::DeviceHandle = 0) const;
133 };
134 
getDevices() const135 MusicDevices SndioMusicPlugin::getDevices() const {
136 	MusicDevices devices;
137 	devices.push_back(MusicDevice(this, "", MT_GM));
138 	return devices;
139 }
140 
createInstance(MidiDriver ** mididriver,MidiDriver::DeviceHandle) const141 Common::Error SndioMusicPlugin::createInstance(MidiDriver **mididriver, MidiDriver::DeviceHandle) const {
142 	*mididriver = new MidiDriver_Sndio();
143 
144 	return Common::kNoError;
145 }
146 
147 //#if PLUGIN_ENABLED_DYNAMIC(Sndio)
148 	//REGISTER_PLUGIN_DYNAMIC(SNDIO, PLUGIN_TYPE_MUSIC, SndioMusicPlugin);
149 //#else
150 	REGISTER_PLUGIN_STATIC(SNDIO, PLUGIN_TYPE_MUSIC, SndioMusicPlugin);
151 //#endif
152 
153 #endif
154