sndio/sndiod/miofile.c

195 lines
3.8 KiB
C
Raw Normal View History

2012-09-02 17:13:30 -05:00
/* $OpenBSD$ */
/*
2012-11-10 07:58:53 -06:00
* Copyright (c) 2008-2012 Alexandre Ratchov <alex@caoua.org>
2012-09-02 17:13:30 -05:00
*
* Permission to use, copy, modify, and distribute this software for any
* purpose with or without fee is hereby granted, provided that the above
* copyright notice and this permission notice appear in all copies.
*
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/
#include <sys/types.h>
#include <sys/time.h>
#include <poll.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <sndio.h>
#include "defs.h"
#include "file.h"
#include "midi.h"
#include "miofile.h"
#include "utils.h"
2012-11-02 09:58:34 -05:00
int port_mio_pollfd(void *, struct pollfd *);
int port_mio_revents(void *, struct pollfd *);
void port_mio_in(void *);
void port_mio_out(void *);
void port_mio_hup(void *);
2012-09-02 17:13:30 -05:00
2012-11-02 09:58:34 -05:00
struct fileops port_mio_ops = {
2012-09-02 17:13:30 -05:00
"mio",
2012-11-02 09:58:34 -05:00
port_mio_pollfd,
port_mio_revents,
port_mio_in,
port_mio_out,
port_mio_hup
2012-09-02 17:13:30 -05:00
};
/*
* open the port using one of the provided paths
*/
static struct mio_hdl *
port_mio_openlist(struct port *c, unsigned int mode)
{
struct mio_hdl *hdl;
struct name *n;
2020-01-23 05:50:20 -06:00
int idx;
2020-01-23 05:50:20 -06:00
idx = 0;
n = c->path_list;
while (1) {
if (n == NULL)
break;
hdl = mio_open(n->str, mode, 1);
if (hdl != NULL) {
if (log_level >= 2) {
port_log(c);
log_puts(": using ");
log_puts(n->str);
log_puts("\n");
}
return hdl;
}
n = n->next;
2020-01-23 05:50:20 -06:00
idx++;
}
return NULL;
}
2012-11-02 09:58:34 -05:00
int
port_mio_open(struct port *p)
2012-09-02 17:13:30 -05:00
{
p->mio.hdl = port_mio_openlist(p, p->midi->mode);
2012-11-02 09:58:34 -05:00
if (p->mio.hdl == NULL)
return 0;
p->mio.file = file_new(&port_mio_ops, p, "port", mio_nfds(p->mio.hdl));
return 1;
}
/*
* Open an alternate port. Upon success, close the old port
* and continue using the new one.
*/
int
port_mio_reopen(struct port *p)
{
struct mio_hdl *hdl;
hdl = port_mio_openlist(p, p->midi->mode);
if (hdl == NULL) {
if (log_level >= 1) {
port_log(p);
log_puts(": couldn't open an alternate port\n");
}
return 0;
}
/* close unused device */
file_del(p->mio.file);
mio_close(p->mio.hdl);
p->mio.hdl = hdl;
p->mio.file = file_new(&port_mio_ops, p, "port", mio_nfds(hdl));
2012-11-02 09:58:34 -05:00
return 1;
2012-09-02 17:13:30 -05:00
}
void
2012-11-02 09:58:34 -05:00
port_mio_close(struct port *p)
2012-09-02 17:13:30 -05:00
{
2012-11-02 09:58:34 -05:00
file_del(p->mio.file);
mio_close(p->mio.hdl);
2012-09-02 17:13:30 -05:00
}
int
2012-11-02 09:58:34 -05:00
port_mio_pollfd(void *addr, struct pollfd *pfd)
2012-09-02 17:13:30 -05:00
{
2012-11-02 09:58:34 -05:00
struct port *p = addr;
struct midi *ep = p->midi;
2012-09-02 17:13:30 -05:00
int events = 0;
2012-11-29 11:56:59 -06:00
if (ep->mode & MODE_MIDIIN)
2012-09-02 17:13:30 -05:00
events |= POLLIN;
if ((ep->mode & MODE_MIDIOUT) && ep->obuf.used > 0)
events |= POLLOUT;
2012-11-02 09:58:34 -05:00
return mio_pollfd(p->mio.hdl, pfd, events);
2012-09-02 17:13:30 -05:00
}
int
2012-11-02 09:58:34 -05:00
port_mio_revents(void *addr, struct pollfd *pfd)
2012-09-02 17:13:30 -05:00
{
2012-11-02 09:58:34 -05:00
struct port *p = addr;
2012-09-02 17:13:30 -05:00
2012-11-02 09:58:34 -05:00
return mio_revents(p->mio.hdl, pfd);
2012-09-02 17:13:30 -05:00
}
void
2012-11-02 09:58:34 -05:00
port_mio_in(void *arg)
2012-09-02 17:13:30 -05:00
{
2012-11-29 11:56:59 -06:00
unsigned char data[MIDI_BUFSZ];
2012-11-02 09:58:34 -05:00
struct port *p = arg;
struct midi *ep = p->midi;
2012-11-29 11:56:59 -06:00
int n;
2012-09-02 17:13:30 -05:00
for (;;) {
2012-11-29 11:56:59 -06:00
n = mio_read(p->mio.hdl, data, MIDI_BUFSZ);
2012-09-02 17:13:30 -05:00
if (n == 0)
break;
2012-11-29 11:56:59 -06:00
midi_in(ep, data, n);
2012-09-02 17:13:30 -05:00
}
}
void
2012-11-02 09:58:34 -05:00
port_mio_out(void *arg)
2012-09-02 17:13:30 -05:00
{
2012-11-02 09:58:34 -05:00
struct port *p = arg;
struct midi *ep = p->midi;
2012-09-02 17:13:30 -05:00
unsigned char *data;
2012-10-24 07:53:37 -05:00
int n, count;
2012-09-02 17:13:30 -05:00
for (;;) {
data = abuf_rgetblk(&ep->obuf, &count);
if (count == 0)
break;
2012-11-02 09:58:34 -05:00
n = mio_write(p->mio.hdl, data, count);
2012-09-02 17:13:30 -05:00
if (n == 0)
break;
abuf_rdiscard(&ep->obuf, n);
if (n < count)
break;
}
if (p->state == PORT_DRAIN && ep->obuf.used == 0)
port_close(p);
2012-11-29 11:56:59 -06:00
midi_fill(ep);
2012-09-02 17:13:30 -05:00
}
void
2012-11-02 09:58:34 -05:00
port_mio_hup(void *arg)
2012-09-02 17:13:30 -05:00
{
2012-11-02 09:58:34 -05:00
struct port *p = arg;
2012-09-02 17:13:30 -05:00
if (!port_reopen(p)) {
midi_abort(p->midi);
if (p->state != PORT_CFG)
port_close(p);
}
2012-09-02 17:13:30 -05:00
}