/*
 * Schism Tracker - a cross-platform Impulse Tracker clone
 * copyright (c) 2003-2005 Storlek <storlek@rigelseven.com>
 * copyright (c) 2005-2008 Mrs. Brisby <mrs.brisby@nimh.org>
 * copyright (c) 2009 Storlek & Mrs. Brisby
 * copyright (c) 2010-2012 Storlek
 * URL: http://schismtracker.org/
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA
 */

#include "headers.h"

#include "midi.h"
#include "util.h"
#include "mem.h"

#include <sys/types.h>
#include <sys/stat.h>
#include <sys/ioctl.h>
#include <sys/soundcard.h>

#ifdef HAVE_POLL_H
# include <poll.h>
#elif HAVE_SYS_POLL_H
# include <sys/poll.h>
#endif

#include <fcntl.h>

/* this is stupid; oss doesn't have a concept of ports... */
#define MAX_OSS_MIDI    64

#define MAX_MIDI_PORTS  (MAX_OSS_MIDI + 2)

#define MIDIBUF_SIZE (65536)

struct oss_midi_provider {
	int opened[MAX_MIDI_PORTS];

	unsigned char midi_buf[MIDIBUF_SIZE];
};

static void _oss_send(struct midi_port *p, const unsigned char *data, uint32_t len,
	SCHISM_UNUSED uint32_t delay)
{
	int fd, n;
	ssize_t r;
	struct oss_midi_provider *omp = p->provider->userdata;

	n = INT_SHAPED_PTR(p->userdata);
	if (n < 0) /* this can happen if write() returns size of 0 */
		return;

	fd = omp->opened[n];
	if (fd < 0)
		return;

	while (len > 0) {
		r = write(fd, data, len);
		if (r < -1 && errno == EINTR)
			continue;

		if (r < 1) {
			/* err, can't happen? */
			(void)close(omp->opened[n]);
			omp->opened[n] = -1;
			p->userdata = PTR_SHAPED_INT(-1);
			p->io = 0; /* failure! */
			return;
		}

		data += r;
		len -= r;
	}
}

static int _oss_start_stop(SCHISM_UNUSED struct midi_port *p) { return 1; /* do nothing */ }

static int _oss_work(struct midi_provider *p)
{
	struct pollfd pfd[MAX_MIDI_PORTS];
	struct midi_port *ptr, *src;
	int i, j;
	ssize_t r;
	struct oss_midi_provider *omp = p->userdata;

	ptr = NULL;
	j = 0;
	while (midi_port_foreach(p, &ptr)) {
		i = INT_SHAPED_PTR(ptr->userdata);
		if (i == -1)
			continue; /* err... */

		if (!(ptr->io & MIDI_INPUT))
			continue;

		SCHISM_RUNTIME_ASSERT(i < MAX_MIDI_PORTS, "somethings very wrong");

		/* umm, this was just setting it to 'i' before, but that's totally
		 * wrong. how the hell did this work? does anyone actually use OSS
		 * MIDI anymore? lmao... */
		pfd[j].fd = omp->opened[i];
		pfd[j].events = POLLIN;
		pfd[j].revents = 0; /* RH 5 bug */ /* LOL WOW THIS CODE IS OLD */
		j++;
	}

	if (!j || poll(pfd, j, 0) <= 0)
		return 0;

	for (i = 0; i < j; i++) {
		if (!(pfd[i].revents & POLLIN))
			continue;

		do {
			r = read(pfd[i].fd, omp->midi_buf, sizeof(omp->midi_buf));
		} while (r == -1 && errno == EINTR);

		if (r > 0) {
			ptr = src = NULL;
			while (midi_port_foreach(p, &ptr)) {
				if (INT_SHAPED_PTR(ptr->userdata) == pfd[i].fd) {
					src = ptr;
				}
			}
			midi_received_cb(src, omp->midi_buf, r);
		}
	}

	return 0;
}

static int _tryopen(int n, const char *name, struct midi_provider *_oss_provider)
{
	int io;
	char *ptr;
	struct oss_midi_provider *omp = _oss_provider->userdata;

	if (omp->opened[n] != -1)
		return 1;

	if ((omp->opened[n] = open(name, O_RDWR|O_NOCTTY|O_NONBLOCK)) != -1) {
		io = MIDI_INPUT | MIDI_OUTPUT;
	} else if ((omp->opened[n] = open(name, O_RDONLY|O_NOCTTY|O_NONBLOCK)) != -1) {
		io = MIDI_INPUT;
	} else if ((omp->opened[n] = open(name, O_WRONLY|O_NOCTTY|O_NONBLOCK)) != -1) {
		io = MIDI_OUTPUT;
	} else {
		return 2;
	}

	ptr = NULL;
	if (asprintf(&ptr, " %-16s (OSS)", name) == -1)
		return 3;

	midi_port_register(_oss_provider, io, ptr, PTR_SHAPED_INT(n), NULL);
	free(ptr);
	return 0;
}

static void _oss_poll(struct midi_provider *_oss_provider)
{
	int i;

	_tryopen(0, "/dev/midi", _oss_provider);
	for (i = 0; i < MAX_OSS_MIDI; i++) {
		char sbuf[64];

		snprintf(sbuf, sizeof(sbuf), "/dev/midi%d", i);
		if (!_tryopen(i + 1, sbuf, _oss_provider))
			continue;

		snprintf(sbuf, sizeof(sbuf), "/dev/midi%02d", i);
		if (!_tryopen(i + 1, sbuf, _oss_provider))
			continue;
	}
}

static void _oss_destroy_userdata(void *x)
{
	struct oss_midi_provider *omp = x;
	int i;

	for (i = 0; i < MAX_MIDI_PORTS; i++)
		if (omp->opened[i] >= 0)
			close(omp->opened[i]);

	free(omp);
}

int oss_midi_setup(void)
{
	static const struct midi_driver driver = {
		.flags = 0,
		.poll = _oss_poll,
		.work = _oss_work,
		.enable = _oss_start_stop,
		.disable = _oss_start_stop,
		.send = _oss_send,
		.destroy_userdata = _oss_destroy_userdata,
	};
	struct oss_midi_provider *omp;
	int i;

	omp = mem_calloc(1, sizeof(*omp));

	for (i = 0; i < MAX_MIDI_PORTS; i++)
		omp->opened[i] = -1;

	if (!midi_provider_register("OSS", &driver, omp))
		return 0;

	return 1;
}
