diff options
author | Alexandre Ratchov <ratchov@cvs.openbsd.org> | 2009-10-27 22:41:04 +0000 |
---|---|---|
committer | Alexandre Ratchov <ratchov@cvs.openbsd.org> | 2009-10-27 22:41:04 +0000 |
commit | 9f7e4cf26956529ad343bebf04aa56328b8e6548 (patch) | |
tree | b1fc4ca57aa3f3bcf80c4096fdbacefdb0bd37c6 /usr.bin/aucat | |
parent | 4fa4331d3e7b39ce015d65b4bcc68f7e0fdd6d0a (diff) |
slightly cleanup the socket and control bits:
- reject bogus clients ignoring flow control during the start phase
- don't check if dev_midi is NULL, it can't be NULL anymore
- use ``struct ctl_ops'' instead of a simple call-backs
- don't try to flush play buffer if it's not attached yet
Diffstat (limited to 'usr.bin/aucat')
-rw-r--r-- | usr.bin/aucat/aproc.h | 6 | ||||
-rw-r--r-- | usr.bin/aucat/midi.c | 58 | ||||
-rw-r--r-- | usr.bin/aucat/midi.h | 4 | ||||
-rw-r--r-- | usr.bin/aucat/sock.c | 23 |
4 files changed, 59 insertions, 32 deletions
diff --git a/usr.bin/aucat/aproc.h b/usr.bin/aucat/aproc.h index 299192514af..95bbced80e4 100644 --- a/usr.bin/aucat/aproc.h +++ b/usr.bin/aucat/aproc.h @@ -1,4 +1,4 @@ -/* $OpenBSD: aproc.h,v 1.25 2009/10/10 12:43:09 ratchov Exp $ */ +/* $OpenBSD: aproc.h,v 1.26 2009/10/27 22:41:03 ratchov Exp $ */ /* * Copyright (c) 2008 Alexandre Ratchov <alex@caoua.org> * @@ -174,7 +174,9 @@ struct aproc { #define CTL_NAMEMAX 8 unsigned serial; struct ctl_slot { - void (*cb)(void *, unsigned); + struct ctl_ops { + void (*vol)(void *, unsigned); + } *ops; void *arg; unsigned unit; char name[CTL_NAMEMAX]; diff --git a/usr.bin/aucat/midi.c b/usr.bin/aucat/midi.c index 2a2f9c8c813..aea71324735 100644 --- a/usr.bin/aucat/midi.c +++ b/usr.bin/aucat/midi.c @@ -1,4 +1,4 @@ -/* $OpenBSD: midi.c,v 1.11 2009/10/10 12:43:09 ratchov Exp $ */ +/* $OpenBSD: midi.c,v 1.12 2009/10/27 22:41:03 ratchov Exp $ */ /* * Copyright (c) 2008 Alexandre Ratchov <alex@caoua.org> * @@ -304,6 +304,7 @@ thru_new(char *name) return p; } + /* * broadcast a message to all output buffers on the behalf of ibuf. * ie. don't sent back the message to the sender @@ -338,12 +339,11 @@ ctl_sendmsg(struct aproc *p, struct abuf *ibuf, unsigned char *msg, unsigned len } /* - * allocate a new slot (ie midi channel), register the given call-back - * to be called volume is changed by MIDI. The call-back is invoked at - * initialization to restore the saved volume. + * find the best matching free slot index (ie midi channel). + * return -1, if there are no free slots anymore */ int -ctl_slotnew(struct aproc *p, char *who, void (*cb)(void *, unsigned), void *arg) +ctl_getidx(struct aproc *p, char *who) { char *s; struct ctl_slot *slot; @@ -370,39 +370,36 @@ ctl_slotnew(struct aproc *p, char *who, void (*cb)(void *, unsigned), void *arg) * find the instance number of the control name */ for (i = 0, slot = p->u.ctl.slot; i < CTL_NSLOT; i++, slot++) { - if (slot->cb == NULL) + if (slot->ops == NULL) continue; if (strcmp(slot->name, name) == 0) umap |= (1 << i); } - for (unit = 0; unit < CTL_NSLOT; unit++) { + for (unit = 0; ; unit++) { if (unit == CTL_NSLOT) return -1; - if ((umap & (1 << i)) == 0) + if ((umap & (1 << unit)) == 0) break; } /* * find a free controller slot with the same name/unit */ for (i = 0, slot = p->u.ctl.slot; i < CTL_NSLOT; i++, slot++) { - if (slot->cb == NULL && + if (slot->ops == NULL && strcmp(slot->name, name) == 0 && slot->unit == unit) { - slot->cb = cb; - slot->arg = arg; - slot->cb(slot->arg, slot->vol); - ctl_slotvol(p, i, slot->vol); return i; } } /* * couldn't find a matching slot, pick oldest free slot + * and set its name/unit */ bestser = 0; bestidx = CTL_NSLOT; for (i = 0, slot = p->u.ctl.slot; i < CTL_NSLOT; i++, slot++) { - if (slot->cb != NULL) + if (slot->ops != NULL) continue; ser = p->u.ctl.serial - slot->serial; if (ser > bestser) { @@ -417,20 +414,37 @@ ctl_slotnew(struct aproc *p, char *who, void (*cb)(void *, unsigned), void *arg) slot->serial = p->u.ctl.serial++; slot->unit = unit; slot->vol = MIDI_MAXCTL; - slot->cb = cb; - slot->arg = arg; - slot->cb(slot->arg, slot->vol); - ctl_slotvol(p, bestidx, slot->vol); return bestidx; } /* + * allocate a new slot and register the given call-backs + */ +int +ctl_slotnew(struct aproc *p, char *who, struct ctl_ops *ops, void *arg) +{ + int idx; + struct ctl_slot *s; + + idx = ctl_getidx(p, who); + if (idx < 0) + return -1; + + s = p->u.ctl.slot + idx; + s->ops = ops; + s->arg = arg; + s->ops->vol(s->arg, s->vol); + ctl_slotvol(p, idx, s->vol); + return idx; +} + +/* * release the given slot */ void ctl_slotdel(struct aproc *p, int index) { - p->u.ctl.slot[index].cb = NULL; + p->u.ctl.slot[index].ops = NULL; } /* @@ -464,10 +478,10 @@ ctl_ev(struct aproc *p, struct abuf *ibuf) if (chan >= CTL_NSLOT) return; slot = p->u.ctl.slot + chan; - if (slot->cb == NULL) + if (slot->ops == NULL) return; slot->vol = ibuf->r.midi.msg[2]; - slot->cb(slot->arg, slot->vol); + slot->ops->vol(slot->arg, slot->vol); ctl_sendmsg(p, ibuf, ibuf->r.midi.msg, ibuf->r.midi.len); } } @@ -571,7 +585,7 @@ ctl_new(char *name) p->u.ctl.serial = 0; for (i = 0, s = p->u.ctl.slot; i < CTL_NSLOT; i++, s++) { p->u.ctl.slot[i].unit = i; - p->u.ctl.slot[i].cb = NULL; + p->u.ctl.slot[i].ops = NULL; p->u.ctl.slot[i].vol = MIDI_MAXCTL; p->u.ctl.slot[i].serial = p->u.ctl.serial++; strlcpy(p->u.ctl.slot[i].name, "unknown", CTL_NAMEMAX); diff --git a/usr.bin/aucat/midi.h b/usr.bin/aucat/midi.h index 74ba8389807..c92e284ec93 100644 --- a/usr.bin/aucat/midi.h +++ b/usr.bin/aucat/midi.h @@ -1,4 +1,4 @@ -/* $OpenBSD: midi.h,v 1.3 2009/08/26 06:10:15 ratchov Exp $ */ +/* $OpenBSD: midi.h,v 1.4 2009/10/27 22:41:03 ratchov Exp $ */ /* * Copyright (c) 2008 Alexandre Ratchov <alex@caoua.org> * @@ -20,7 +20,7 @@ struct aproc *thru_new(char *); struct aproc *ctl_new(char *); -int ctl_slotnew(struct aproc *, char *, void (*)(void *, unsigned), void *); +int ctl_slotnew(struct aproc *, char *, struct ctl_ops *, void *); void ctl_slotdel(struct aproc *, int); void ctl_slotvol(struct aproc *, int, unsigned); diff --git a/usr.bin/aucat/sock.c b/usr.bin/aucat/sock.c index 0611755a477..ec5a87cf31e 100644 --- a/usr.bin/aucat/sock.c +++ b/usr.bin/aucat/sock.c @@ -1,4 +1,4 @@ -/* $OpenBSD: sock.c,v 1.33 2009/10/22 21:41:30 ratchov Exp $ */ +/* $OpenBSD: sock.c,v 1.34 2009/10/27 22:41:03 ratchov Exp $ */ /* * Copyright (c) 2008 Alexandre Ratchov <alex@caoua.org> * @@ -53,6 +53,12 @@ struct fileops sock_ops = { }; +void sock_setvol(void *, unsigned); + +struct ctl_ops ctl_sockops = { + sock_setvol, +}; + void rsock_done(struct aproc *p) { @@ -63,7 +69,7 @@ rsock_done(struct aproc *p) sock_reset(f); f->pipe.file.rproc = NULL; if (f->pipe.file.wproc) { - if (dev_midi && f->slot >= 0) + if (f->slot >= 0) ctl_slotdel(dev_midi, f->slot); aproc_del(f->pipe.file.wproc); file_del(&f->pipe.file); @@ -80,7 +86,7 @@ rsock_in(struct aproc *p, struct abuf *ibuf_dummy) if (!sock_read(f)) return 0; obuf = LIST_FIRST(&p->obuflist); - if (obuf) { + if (obuf && f->pstate >= SOCK_RUN) { if (!abuf_flush(obuf)) return 0; } @@ -160,7 +166,7 @@ wsock_done(struct aproc *p) sock_reset(f); f->pipe.file.wproc = NULL; if (f->pipe.file.rproc) { - if (dev_midi && f->slot >= 0) + if (f->slot >= 0) ctl_slotdel(dev_midi, f->slot); aproc_del(f->pipe.file.rproc); file_del(&f->pipe.file); @@ -685,7 +691,7 @@ sock_hello(struct sock *f) f->mode |= AMSG_REC; } if (dev_midi) { - f->slot = ctl_slotnew(dev_midi, p->who, sock_setvol, f); + f->slot = ctl_slotnew(dev_midi, p->who, &ctl_sockops, f); if (f->slot < 0) { return 0; } @@ -713,6 +719,11 @@ sock_execmsg(struct sock *f) aproc_del(f->pipe.file.rproc); return 0; } + if (f->pstate == SOCK_START && + ABUF_FULL(LIST_FIRST(&f->pipe.file.rproc->obuflist))) { + aproc_del(f->pipe.file.rproc); + return 0; + } f->rstate = SOCK_RDATA; f->rtodo = m->u.data.size; if (f->rtodo == 0) { @@ -805,7 +816,7 @@ sock_execmsg(struct sock *f) return 0; } sock_setvol(f, m->u.vol.ctl); - if (dev_midi && f->slot >= 0) + if (f->slot >= 0) ctl_slotvol(dev_midi, f->slot, m->u.vol.ctl); f->rtodo = sizeof(struct amsg); f->rstate = SOCK_RMSG; |