--- gameDetector.cpp	28 Apr 2002 00:31:31 -0000	1.46
+++ gameDetector.cpp	28 Apr 2002 01:55:43 -0000
@@ -568,6 +568,9 @@
 #if defined(UNIX) && !defined(__BEOS__)
     case MD_SEQ:        return MidiDriver_SEQ_create();
 #endif
+#if defined(TIMIDITY)
+		case MD_TIMIDITY:		return MidiDriver_TIMIDITY_create();
+#endif
 #if defined(__APPLE__)
 	case MD_QTMUSIC:		return MidiDriver_QT_create();
 	case MD_COREAUDIO:		return MidiDriver_CORE_create();
--- sound/mididrv.cpp	28 Apr 2002 00:32:00 -0000	1.16
+++ sound/mididrv.cpp	28 Apr 2002 01:55:44 -0000
@@ -29,6 +29,10 @@
 #include "stdafx.h"
 #include "scumm.h"
 #include "mididrv.h"
+#if defined(TIMIDITY)
+#include <sys/socket.h>
+#include <netdb.h>
+#endif
 
 #if defined(WIN32) && !defined(_WIN32_WCE)
 
@@ -786,15 +790,66 @@
 
 
 
-#if 0
 
-/* Old code for timidity support, maybe somebody can rewrite this for the 
-   new midi driver system?
- */
+#if defined(TIMIDITY)
+/* TIMIDITY driver */
+class MidiDriver_TIMIDITY : public MidiDriver {
+public:
+	MidiDriver_TIMIDITY();
+	int open(int mode);
+	int open_socket(int *socketptr, int port);
+	void close();
+	void send(uint32 b);
+	void pause(bool pause);
+	void set_stream_callback(void *param, StreamCallback *sc);
+private:
+	StreamCallback *_stream_proc;
+	void *_stream_param;
+	int _mode;
+	int device;
+};
+
+MidiDriver_TIMIDITY::MidiDriver_TIMIDITY() {
+  _mode=0;
+	device=0;
+}
 
 
-/*********** Timidity		*/
-int MidiDriver::connect_to_timidity(int port)
+int MidiDriver_TIMIDITY::open(int mode)
+{	
+	int s,s2,err;
+	int len;
+	int dummy,newport;
+	char buf[256];
+
+	if (_mode != 0 )
+		return MERR_ALREADY_OPEN;
+	device = 0;
+	_mode = mode;
+	if (mode!=MO_SIMPLE) return MERR_STREAMING_NOT_AVAILABLE;
+
+	if ((err = open_socket(&s,7777))!=0)
+		return err;
+
+	len = ::read(s, buf, 256);
+	sprintf(buf,"SETBUF %f %f\n",0.1,0.15);
+	write(s, buf, strlen(buf));
+
+	len = ::read(s, buf, 256);
+	sprintf(buf,"OPEN lsb\n");
+	write(s, buf, strlen(buf));
+
+	len = ::read(s, buf, 256);
+	sscanf(buf,"%d %d",&dummy, &newport);
+	write(s, buf, strlen(buf));
+
+	if ((err = open_socket(&s2,newport))!=0)
+		return err;
+	device = s2;
+	return 0;
+}
+
+int MidiDriver_TIMIDITY::open_socket(int *socketptr, int port)
 {
 	int s = 0;
 #if !defined(__APPLE__CW) && !defined(__MORPHOS__)	// No socket support on Apple Carbon or Morphos
@@ -803,7 +858,8 @@
 
 	serverhost = gethostbyname("localhost");
 	if (serverhost == NULL)
-		error("Could not resolve Timidity host ('localhost')");
+		return MERR_CANNOT_CONNECT;
+		//warning("Could not resolve Timidity host ('localhost')");
 
 	sadd.sin_family = serverhost->h_addrtype;
 	sadd.sin_port = htons(port);
@@ -811,37 +867,82 @@
 
 	s = socket(AF_INET, SOCK_STREAM, 0);
 	if (s < 0)
-		error("Could not open Timidity socket");
+		return MERR_CANNOT_CONNECT;
+		//error("Could not open Timidity socket");
 
 	if (connect(s, (struct sockaddr *)&sadd, sizeof(struct sockaddr_in)) < 0)
-		error("Could not connect to Timidity server");
+		return MERR_CANNOT_CONNECT;
+		//error("Could not connect to Timidity server");
+	*socketptr = s;
+	return 0;
 #endif
-	return s;
+	return MERR_DEVICE_NOT_AVAILABLE;
 }
 
-void MidiDriver::midiInitTimidity()
+void MidiDriver_TIMIDITY::close()
 {
-	int s, s2;
-	int len;
-	int dummy, newport;
-	char buf[256];
+	::close(device);
+	_mode = 0;
+}
 
-	s = connect_to_timidity(7777);
-	len = read(s, buf, 256);			// buf[len] = '\0'; printf("%s", buf);
-	sprintf(buf, "SETBUF %f %f\n", 0.1, 0.15);
-	write(s, buf, strlen(buf));
-	len = read(s, buf, 256);			// buf[len] = '\0'; printf("%s", buf); 
+void MidiDriver_TIMIDITY::send(uint32 b)
+{
+	unsigned char buf[256];
+	int position = 0;
 
-	sprintf(buf, "OPEN lsb\n");
-	write(s, buf, strlen(buf));
-	len = read(s, buf, 256);			// buf[len] = '\0'; printf("%s", buf); 
+	switch (b & 0xF0) {
+	case 0x80:
+	case 0x90:
+	case 0xA0:
+	case 0xB0:
+	case 0xE0:
+		buf[position++] = SEQ_MIDIPUTC;
+		buf[position++] = (unsigned char)b;
+		buf[position++] = DEVICE_NUM;
+		buf[position++] = 0;
+		buf[position++] = SEQ_MIDIPUTC;
+		buf[position++] = (unsigned char)((b >> 8) & 0x7F);
+		buf[position++] = DEVICE_NUM;
+		buf[position++] = 0;
+		buf[position++] = SEQ_MIDIPUTC;
+		buf[position++] = (unsigned char)((b >> 16) & 0x7F);
+		buf[position++] = DEVICE_NUM;
+		buf[position++] = 0;
+		break;
+	case 0xC0:
+	case 0xD0:
+		buf[position++] = SEQ_MIDIPUTC;
+		buf[position++] = (unsigned char)b;
+		buf[position++] = DEVICE_NUM;
+		buf[position++] = 0;
+		buf[position++] = SEQ_MIDIPUTC;
+		buf[position++] = (unsigned char)((b >> 8) & 0x7F);
+		buf[position++] = DEVICE_NUM;
+		buf[position++] = 0;
+		break;
+	default:
+		fprintf(stderr, "Unknown : %08x\n", (int) b);
+		break;
+	}
+	write(device, buf, position);
+}
 
-	sscanf(buf, "%d %d", &dummy, &newport);
-	printf("	 => port = %d\n", newport);
+void MidiDriver_TIMIDITY::pause(bool pause)
+{
+	debug(1,"pause: %d",pause);
+	if (_mode == MO_STREAMING) {
+	}
+}
 
-	s2 = connect_to_timidity(newport);
-	_mo = (void *)s2;
+void MidiDriver_TIMIDITY::set_stream_callback(void *param, StreamCallback *sc)
+{
+	_stream_param = param;
+	_stream_proc = sc;
 }
 
+MidiDriver *MidiDriver_TIMIDITY_create()
+{
+	return new MidiDriver_TIMIDITY();
+}
 
-#endif /* 0 */
+#endif /* TIMIDITY */

