diff options
author | Alexandre Ratchov <ratchov@cvs.openbsd.org> | 2021-01-29 10:51:25 +0000 |
---|---|---|
committer | Alexandre Ratchov <ratchov@cvs.openbsd.org> | 2021-01-29 10:51:25 +0000 |
commit | b7efc18a83d715d559d725a45a95538ce2534343 (patch) | |
tree | af786172d8fe5e7ddb600d39703b6953e4f15f33 | |
parent | a39b89c7754cc382b01ec6c563bc1e4810f7d613 (diff) |
Move the audio clients state out of the device structure
No behavior change. Later this will ease moving clients from one
device to another by "just" swapping pointers.
-rw-r--r-- | usr.bin/sndiod/dev.c | 90 | ||||
-rw-r--r-- | usr.bin/sndiod/dev.h | 17 | ||||
-rw-r--r-- | usr.bin/sndiod/sndiod.c | 4 | ||||
-rw-r--r-- | usr.bin/sndiod/sock.c | 4 |
4 files changed, 69 insertions, 46 deletions
diff --git a/usr.bin/sndiod/dev.c b/usr.bin/sndiod/dev.c index 1a98fa39fc7..a221558f512 100644 --- a/usr.bin/sndiod/dev.c +++ b/usr.bin/sndiod/dev.c @@ -1,4 +1,4 @@ -/* $OpenBSD: dev.c,v 1.83 2021/01/28 11:17:58 ratchov Exp $ */ +/* $OpenBSD: dev.c,v 1.84 2021/01/29 10:51:24 ratchov Exp $ */ /* * Copyright (c) 2008-2012 Alexandre Ratchov <alex@caoua.org> * @@ -109,6 +109,24 @@ struct slotops zomb_slotops = { struct dev *dev_list = NULL; unsigned int dev_sndnum = 0; +struct slot slot_array[DEV_NSLOT]; +unsigned int slot_serial; /* for slot allocation */ + +void +slot_array_init(void) +{ + unsigned int i; + + for (i = 0; i < DEV_NSLOT; i++) { + slot_array[i].unit = i; + slot_array[i].ops = NULL; + slot_array[i].vol = MIDI_MAXCTL; + slot_array[i].dev = NULL; + slot_array[i].serial = slot_serial++; + memset(slot_array[i].name, 0, SLOT_NAMEMAX); + } +} + void dev_log(struct dev *d) { @@ -331,7 +349,7 @@ dev_midi_vol(struct dev *d, struct slot *s) { unsigned char msg[3]; - msg[0] = MIDI_CTL | (s - d->slot); + msg[0] = MIDI_CTL | (s - slot_array); msg[1] = MIDI_CTL_VOL; msg[2] = s->vol; midi_send(d->midi, msg, 3); @@ -391,7 +409,7 @@ dev_midi_slotdesc(struct dev *d, struct slot *s) x.id1 = SYSEX_AUCAT_SLOTDESC; if (*s->name != '\0') slot_ctlname(s, (char *)x.u.slotdesc.name, SYSEX_NAMELEN); - x.u.slotdesc.chan = s - d->slot; + x.u.slotdesc.chan = (s - slot_array); x.u.slotdesc.end = SYSEX_END; midi_send(d->midi, (unsigned char *)&x, SYSEX_SIZE(slotdesc)); } @@ -404,7 +422,9 @@ dev_midi_dump(struct dev *d) int i; dev_midi_master(d); - for (i = 0, s = d->slot; i < DEV_NSLOT; i++, s++) { + for (i = 0, s = slot_array; i < DEV_NSLOT; i++, s++) { + if (s->dev != d) + continue; dev_midi_slotdesc(d, s); dev_midi_vol(d, s); } @@ -440,7 +460,9 @@ dev_midi_omsg(void *arg, unsigned char *msg, int len) chan = msg[0] & MIDI_CHANMASK; if (chan >= DEV_NSLOT) return; - slot_setvol(d->slot + chan, msg[2]); + if (slot_array[chan].dev != d) + return; + slot_setvol(slot_array + chan, msg[2]); dev_onval(d, CTLADDR_SLOT_LEVEL(chan), msg[2]); return; } @@ -1044,14 +1066,6 @@ dev_new(char *path, struct aparams *par, d->autovol = autovol; d->refcnt = 0; d->pstate = DEV_CFG; - d->serial = 0; - for (i = 0; i < DEV_NSLOT; i++) { - d->slot[i].unit = i; - d->slot[i].ops = NULL; - d->slot[i].vol = MIDI_MAXCTL; - d->slot[i].serial = d->serial++; - memset(d->slot[i].name, 0, SLOT_NAMEMAX); - } for (i = 0; i < DEV_NCTLSLOT; i++) { d->ctlslot[i].ops = NULL; d->ctlslot[i].dev = d; @@ -1203,6 +1217,7 @@ dev_open(struct dev *d) int i; char name[CTL_NAMEMAX]; struct dev_alt *a; + struct slot *s; d->master_enabled = 0; d->mode = d->reqmode; @@ -1226,14 +1241,14 @@ dev_open(struct dev *d) if (!dev_allocbufs(d)) return 0; - for (i = 0; i < DEV_NSLOT; i++) { - if (d->slot[i].name[0] == 0) + for (i = 0, s = slot_array; i < DEV_NSLOT; i++, s++) { + if (s->dev != d || s->name[0] == 0) continue; - slot_ctlname(&d->slot[i], name, CTL_NAMEMAX); + slot_ctlname(s, name, CTL_NAMEMAX); dev_addctl(d, "app", CTL_NUM, CTLADDR_SLOT_LEVEL(i), name, -1, "level", - NULL, -1, 127, d->slot[i].vol); + NULL, -1, 127, s->vol); } /* if there are multiple alt devs, add server.device knob */ @@ -1261,7 +1276,9 @@ dev_abort(struct dev *d) struct slot *s; struct ctlslot *c; - for (s = d->slot, i = DEV_NSLOT; i > 0; i--, s++) { + for (i = 0, s = slot_array; i < DEV_NSLOT; i++, s++) { + if (s->dev != d) + continue; if (s->ops) s->ops->exit(s->arg); s->ops = NULL; @@ -1562,9 +1579,9 @@ dev_sync_attach(struct dev *d) } return; } - for (i = 0; i < DEV_NSLOT; i++) { - s = d->slot + i; - if (!s->ops || !s->opt->mmc) + + for (i = 0, s = slot_array; i < DEV_NSLOT; i++, s++) { + if (s->dev != d || !s->ops || !s->opt->mmc) continue; if (s->pstate != SLOT_READY) { #ifdef DEBUG @@ -1578,9 +1595,9 @@ dev_sync_attach(struct dev *d) } if (!dev_ref(d)) return; - for (i = 0; i < DEV_NSLOT; i++) { - s = d->slot + i; - if (!s->ops || !s->opt->mmc) + + for (i = 0, s = slot_array; i < DEV_NSLOT; i++, s++) { + if (s->dev != d || !s->ops || !s->opt->mmc) continue; slot_attach(s); s->pstate = SLOT_RUN; @@ -1845,8 +1862,7 @@ slot_new(struct dev *d, struct opt *opt, unsigned int id, char *who, */ for (i = 0; i < DEV_NSLOT; i++) unit[i] = NULL; - for (i = 0; i < DEV_NSLOT; i++) { - s = d->slot + i; + for (i = 0, s = slot_array; i < DEV_NSLOT; i++, s++) { if (strcmp(s->name, name) == 0) unit[s->unit] = s; } @@ -1877,20 +1893,20 @@ slot_new(struct dev *d, struct opt *opt, unsigned int id, char *who, */ bestser = 0; bestidx = DEV_NSLOT; - for (i = 0, s = d->slot; i < DEV_NSLOT; i++, s++) { + for (i = 0, s = slot_array; i < DEV_NSLOT; i++, s++) { if (s->ops != NULL) continue; - ser = d->serial - s->serial; + ser = slot_serial - s->serial; if (ser > bestser) { bestser = ser; bestidx = i; } } if (bestidx != DEV_NSLOT) { - s = d->slot + bestidx; + s = slot_array + bestidx; s->vol = MIDI_MAXCTL; strlcpy(s->name, name, SLOT_NAMEMAX); - s->serial = d->serial++; + s->serial = slot_serial++; for (i = 0; unit[i] != NULL; i++) ; /* nothing */ s->unit = i; @@ -1918,7 +1934,7 @@ found: } if (!dev_ref(d)) return NULL; - dev_label(d, s - d->slot); + dev_label(d, s - slot_array); if ((mode & d->mode) != mode) { if (log_level >= 1) { slot_log(s); @@ -2505,6 +2521,7 @@ int dev_setctl(struct dev *d, int addr, int val) { struct ctl *c; + struct slot *s; int num; c = d->ctl_list; @@ -2559,8 +2576,11 @@ dev_setctl(struct dev *d, int addr, int val) } } else { num = addr - CTLADDR_SLOT_LEVEL(0); - slot_setvol(d->slot + num, val); - dev_midi_vol(d, d->slot + num); + s = slot_array + num; + if (s->dev != d) + return 1; + slot_setvol(s, val); + dev_midi_vol(d, s); } c->val_mask = ~0U; } @@ -2592,7 +2612,7 @@ dev_label(struct dev *d, int i) struct ctl *c; char name[CTL_NAMEMAX]; - slot_ctlname(&d->slot[i], name, CTL_NAMEMAX); + slot_ctlname(&slot_array[i], name, CTL_NAMEMAX); c = d->ctl_list; for (;;) { @@ -2600,7 +2620,7 @@ dev_label(struct dev *d, int i) dev_addctl(d, "app", CTL_NUM, CTLADDR_SLOT_LEVEL(i), name, -1, "level", - NULL, -1, 127, d->slot[i].vol); + NULL, -1, 127, slot_array[i].vol); return; } if (c->addr == CTLADDR_SLOT_LEVEL(i)) diff --git a/usr.bin/sndiod/dev.h b/usr.bin/sndiod/dev.h index 564f8caea17..d06e7f00978 100644 --- a/usr.bin/sndiod/dev.h +++ b/usr.bin/sndiod/dev.h @@ -1,4 +1,4 @@ -/* $OpenBSD: dev.h,v 1.31 2021/01/28 11:15:31 ratchov Exp $ */ +/* $OpenBSD: dev.h,v 1.32 2021/01/29 10:51:24 ratchov Exp $ */ /* * Copyright (c) 2008-2012 Alexandre Ratchov <alex@caoua.org> * @@ -28,6 +28,11 @@ #define CTLADDR_END (CTLADDR_ALT_SEL + DEV_NMAX) /* + * preallocated audio clients + */ +#define DEV_NSLOT 8 + +/* * audio stream state structure */ @@ -182,13 +187,6 @@ struct dev { unsigned char *decbuf; /* buffer for decoding */ /* - * preallocated audio sub-devices - */ -#define DEV_NSLOT 8 - struct slot slot[DEV_NSLOT]; - unsigned int serial; /* for slot allocation */ - - /* * current position, relative to the current cycle */ int delta; @@ -265,6 +263,9 @@ struct dev { }; extern struct dev *dev_list; +extern struct slot slot_array[DEV_NSLOT]; + +void slot_array_init(void); void dev_log(struct dev *); void dev_abort(struct dev *); diff --git a/usr.bin/sndiod/sndiod.c b/usr.bin/sndiod/sndiod.c index 0aa175a2a64..4042594f36a 100644 --- a/usr.bin/sndiod/sndiod.c +++ b/usr.bin/sndiod/sndiod.c @@ -1,4 +1,4 @@ -/* $OpenBSD: sndiod.c,v 1.41 2020/06/18 05:11:13 ratchov Exp $ */ +/* $OpenBSD: sndiod.c,v 1.42 2021/01/29 10:51:24 ratchov Exp $ */ /* * Copyright (c) 2008-2012 Alexandre Ratchov <alex@caoua.org> * @@ -496,6 +496,8 @@ main(int argc, char **argv) tcpaddr_list = NULL; devindex = 0; + slot_array_init(); + while ((c = getopt(argc, argv, "a:b:c:C:de:F:f:j:L:m:Q:q:r:s:t:U:v:w:x:z:")) != -1) { switch (c) { diff --git a/usr.bin/sndiod/sock.c b/usr.bin/sndiod/sock.c index 6f7cea4c926..c7cdc7b9c3b 100644 --- a/usr.bin/sndiod/sock.c +++ b/usr.bin/sndiod/sock.c @@ -1,4 +1,4 @@ -/* $OpenBSD: sock.c,v 1.36 2021/01/28 11:15:31 ratchov Exp $ */ +/* $OpenBSD: sock.c,v 1.37 2021/01/29 10:51:24 ratchov Exp $ */ /* * Copyright (c) 2008-2012 Alexandre Ratchov <alex@caoua.org> * @@ -1238,7 +1238,7 @@ sock_execmsg(struct sock *f) slot_setvol(s, ctl); dev_midi_vol(s->dev, s); dev_onval(s->dev, - CTLADDR_SLOT_LEVEL(f->slot - s->dev->slot), ctl); + CTLADDR_SLOT_LEVEL(f->slot - slot_array), ctl); break; case AMSG_CTLSUB: #ifdef DEBUG |