/* Automatically generated from Squeak on #(19 July 2000 3:58:37 pm) */

#include <math.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <time.h>

/* Default EXPORT macro that does nothing (see comment in sq.h): */
#define EXPORT(returnType) returnType

/* Do not include the entire sq.h file but just those parts needed. */
/*  The virtual machine proxy definition */
#include "sqVirtualMachine.h"
/* Configuration options */
#include "sqConfig.h"
/* Platform specific definitions */
#include "sqPlatformSpecific.h"

#define true 1
#define false 0
#define null 0  /* using 'null' because nil is predefined in Think C */

/* memory access macros */
#define byteAt(i) (*((unsigned char *) (i)))
#define byteAtput(i, val) (*((unsigned char *) (i)) = val)
#define longAt(i) (*((int *) (i)))
#define longAtput(i, val) (*((int *) (i)) = val)

/*** Variables ***/
struct VirtualMachine* interpreterProxy;
const char *moduleName = "SirenMIDIPlugin 19 July 2000 (e)";

/*** Function Prototypes ***/
#pragma export on
EXPORT(int) closeMIDI(void);
EXPORT(int) getMIDIDeviceName(void);
EXPORT(const char*) getModuleName(void);
EXPORT(int) ioctlMIDI(void);
EXPORT(int) openMIDI(void);
EXPORT(int) putMIDIOn(void);
EXPORT(int) readMIDIControl(void);
EXPORT(int) readMIDIPacket(void);
EXPORT(int) setInterpreter(struct VirtualMachine* anInterpreter);
#pragma export off

EXPORT(int) closeMIDI(void) {
    int err;

	err = sqCloseMIDI ();
	if (interpreterProxy->failed()) {
		return null;
	}
	interpreterProxy->pop(1);
	interpreterProxy->push(((err << 1) | 1));
}

EXPORT(int) getMIDIDeviceName(void) {
    int name;
    int answer;
    int index;

	name = interpreterProxy->stackValue(0);
	index = interpreterProxy->stackIntegerValue(1);
	interpreterProxy->success(interpreterProxy->isBytes(name));
	if (interpreterProxy->failed()) {
		return null;
	}
	answer = sqGetMIDIDeviceName(index, name + 4);
	if (interpreterProxy->failed()) {
		return null;
	}
	interpreterProxy->pop(3);
	interpreterProxy->push(((answer << 1) | 1));
}

EXPORT(const char*) getModuleName(void) {
	return moduleName;
}

EXPORT(int) ioctlMIDI(void) {
    int which;
    int toggle;
    int answer;

	which = interpreterProxy->stackIntegerValue(1);
	toggle = interpreterProxy->stackIntegerValue(0);
	answer = sqMIDIioctl (which, toggle);
	if (interpreterProxy->failed()) {
		return null;
	}
	interpreterProxy->pop(3);
	if (toggle == 0) {
		if (answer == 0) {
			interpreterProxy->push(interpreterProxy->falseObject());
		} else {
			interpreterProxy->push(interpreterProxy->trueObject());
		}
	} else {
		interpreterProxy->push(((answer << 1) | 1));
	}
}

EXPORT(int) openMIDI(void) {
    int theMRSemaphore;
    int theMRSocket;
    int answer;

	theMRSemaphore = interpreterProxy->stackIntegerValue(1);
	theMRSocket = interpreterProxy->stackIntegerValue(0);
	answer = sqOpenMIDI (theMRSemaphore, theMRSocket);
	if (interpreterProxy->failed()) {
		return null;
	}
	interpreterProxy->pop(3);
	interpreterProxy->push(((answer << 1) | 1));
}

EXPORT(int) putMIDIOn(void) {
    int interface;
    int message;
    int length;
    int answer;

	message = interpreterProxy->stackValue(2);
	length = interpreterProxy->stackIntegerValue(1);
	interface = interpreterProxy->stackIntegerValue(0);
	interpreterProxy->success(interpreterProxy->isBytes(message));
	if (interpreterProxy->failed()) {
		return null;
	}
	answer = sqMIDIPlaySizeOn (message + 4, length, interface);
	if (interpreterProxy->failed()) {
		return null;
	}
	interpreterProxy->pop(4);
	interpreterProxy->push(((answer << 1) | 1));
}

EXPORT(int) readMIDIControl(void) {
    int fromC;
    int toC;
    int answer;
    int results;

	fromC = interpreterProxy->stackIntegerValue(2);
	toC = interpreterProxy->stackIntegerValue(1);
	if (fromC > 271) {
		results = interpreterProxy->stackIntegerValue(0);
	} else {
		results = interpreterProxy->stackValue(0);
	}
	answer = sqReadMIDIControl (fromC, toC, results);
	if (interpreterProxy->failed()) {
		return null;
	}
	interpreterProxy->pop(4);
	interpreterProxy->push(((answer << 1) | 1));
}

EXPORT(int) readMIDIPacket(void) {
    int data;
    int packet;
    int dsize;
    int answer;

	data = interpreterProxy->stackValue(0);
	dsize = interpreterProxy->stackIntegerValue(1);
	packet = interpreterProxy->stackValue(2);
	interpreterProxy->success(interpreterProxy->isBytes(data));
	if (interpreterProxy->failed()) {
		return null;
	}
	answer = sqReadMIDIPacket (packet, dsize, data + 4);
	if (interpreterProxy->failed()) {
		return null;
	}
	interpreterProxy->pop(4);
	interpreterProxy->push(((answer << 1) | 1));
}

EXPORT(int) setInterpreter(struct VirtualMachine* anInterpreter) {
    int ok;

	interpreterProxy = anInterpreter;
	ok = interpreterProxy->majorVersion() == VM_PROXY_MAJOR;
	if (ok == 0) {
		return 0;
	}
	ok = interpreterProxy->minorVersion() >= VM_PROXY_MINOR;
	return ok;
}
