[BACK]Return to midi.c CVS log [TXT][DIR] Up to [local] / src / usr.bin / aucat

Annotation of src/usr.bin/aucat/midi.c, Revision 1.17

1.17    ! ratchov     1: /*     $OpenBSD: midi.c,v 1.16 2010/01/16 23:21:56 ratchov Exp $       */
1.1       ratchov     2: /*
                      3:  * Copyright (c) 2008 Alexandre Ratchov <alex@caoua.org>
                      4:  *
                      5:  * Permission to use, copy, modify, and distribute this software for any
                      6:  * purpose with or without fee is hereby granted, provided that the above
                      7:  * copyright notice and this permission notice appear in all copies.
                      8:  *
                      9:  * THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
                     10:  * WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
                     11:  * MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
                     12:  * ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
                     13:  * WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
                     14:  * ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
                     15:  * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
                     16:  */
                     17: /*
                     18:  * TODO
                     19:  *
1.7       ratchov    20:  * use shadow variables (to save NRPNs, LSB of controller)
                     21:  * in the midi merger
1.1       ratchov    22:  *
                     23:  * make output and input identical when only one
                     24:  * input is used (fix running status)
                     25:  */
                     26: #include <stdio.h>
                     27: #include <stdlib.h>
                     28: #include <string.h>
                     29:
                     30: #include "abuf.h"
                     31: #include "aproc.h"
1.3       ratchov    32: #include "conf.h"
                     33: #include "dev.h"
1.1       ratchov    34: #include "midi.h"
1.14      ratchov    35: #ifdef DEBUG
                     36: #include "dbg.h"
                     37: #endif
1.1       ratchov    38:
                     39: /*
                     40:  * input data rate is XFER / TIMO (in bytes per microsecond),
                     41:  * it must be slightly larger than the MIDI standard 3125 bytes/s
                     42:  */
                     43: #define MIDITHRU_XFER 340
                     44: #define MIDITHRU_TIMO 100000
                     45:
1.3       ratchov    46: /*
                     47:  * masks to extract command and channel of status byte
                     48:  */
                     49: #define MIDI_CMDMASK   0xf0
                     50: #define MIDI_CHANMASK  0x0f
                     51:
                     52: /*
                     53:  * MIDI status bytes of voice messages
                     54:  */
                     55: #define MIDI_NOFF      0x80            /* note off */
                     56: #define MIDI_NON       0x90            /* note on */
                     57: #define MIDI_KAT       0xa0            /* key after touch */
                     58: #define MIDI_CTL       0xb0            /* controller */
                     59: #define MIDI_PC                0xc0            /* program change */
                     60: #define MIDI_CAT       0xd0            /* channel after touch */
                     61: #define MIDI_BEND      0xe0            /* pitch bend */
1.16      ratchov    62: #define MIDI_ACK       0xfe            /* active sensing message */
1.3       ratchov    63:
                     64: /*
                     65:  * MIDI controller numbers
                     66:  */
                     67: #define MIDI_CTLVOL    7               /* volume */
                     68: #define MIDI_CTLPAN    11              /* pan */
                     69:
                     70: /*
                     71:  * length of voice and common messages (status byte included)
                     72:  */
1.1       ratchov    73: unsigned voice_len[] = { 3, 3, 3, 3, 2, 2, 3 };
                     74: unsigned common_len[] = { 0, 2, 3, 2, 0, 0, 1, 1 };
                     75:
1.7       ratchov    76: /*
1.10      ratchov    77:  * send the message stored in of ibuf->r.midi.msg to obuf
1.7       ratchov    78:  */
1.1       ratchov    79: void
                     80: thru_flush(struct aproc *p, struct abuf *ibuf, struct abuf *obuf)
                     81: {
                     82:        unsigned ocount, itodo;
                     83:        unsigned char *odata, *idata;
                     84:
1.10      ratchov    85:        itodo = ibuf->r.midi.used;
                     86:        idata = ibuf->r.midi.msg;
1.14      ratchov    87: #ifdef DEBUG
                     88:        if (debug_level >= 4) {
                     89:                aproc_dbg(p);
                     90:                dbg_puts(": flushing ");
                     91:                dbg_putu(itodo);
                     92:                dbg_puts(" byte message\n");
                     93:        }
                     94: #endif
1.1       ratchov    95:        while (itodo > 0) {
                     96:                if (!ABUF_WOK(obuf)) {
1.14      ratchov    97: #ifdef DEBUG
                     98:                        if (debug_level >= 4) {
                     99:                                aproc_dbg(p);
                    100:                                dbg_puts(": overrun, discarding ");
                    101:                                dbg_putu(obuf->used);
                    102:                                dbg_puts(" bytes\n");
                    103:                        }
                    104: #endif
1.1       ratchov   105:                        abuf_rdiscard(obuf, obuf->used);
                    106:                        if (p->u.thru.owner == ibuf)
                    107:                                p->u.thru.owner = NULL;
                    108:                        return;
                    109:                }
                    110:                odata = abuf_wgetblk(obuf, &ocount, 0);
                    111:                if (ocount > itodo)
                    112:                        ocount = itodo;
                    113:                memcpy(odata, idata, ocount);
                    114:                abuf_wcommit(obuf, ocount);
                    115:                itodo -= ocount;
                    116:                idata += ocount;
                    117:        }
1.10      ratchov   118:        ibuf->r.midi.used = 0;
1.1       ratchov   119:        p->u.thru.owner = ibuf;
                    120: }
                    121:
1.7       ratchov   122: /*
                    123:  * send the real-time message (one byte) to obuf, similar to thrui_flush()
                    124:  */
1.1       ratchov   125: void
                    126: thru_rt(struct aproc *p, struct abuf *ibuf, struct abuf *obuf, unsigned c)
                    127: {
                    128:        unsigned ocount;
                    129:        unsigned char *odata;
                    130:
1.14      ratchov   131: #ifdef DEBUG
                    132:        if (debug_level >= 4) {
                    133:                aproc_dbg(p);
                    134:                dbg_puts(": ");
1.17    ! ratchov   135:                dbg_putx(c);
1.14      ratchov   136:                dbg_puts(": flushing realtime message\n");
                    137:        }
                    138: #endif
1.16      ratchov   139:        if (c == MIDI_ACK)
                    140:                return;
1.1       ratchov   141:        if (!ABUF_WOK(obuf)) {
1.14      ratchov   142: #ifdef DEBUG
                    143:                if (debug_level >= 4) {
                    144:                        aproc_dbg(p);
                    145:                        dbg_puts(": overrun, discarding ");
                    146:                        dbg_putu(obuf->used);
                    147:                        dbg_puts(" bytes\n");
                    148:                }
                    149: #endif
1.1       ratchov   150:                abuf_rdiscard(obuf, obuf->used);
                    151:                if (p->u.thru.owner == ibuf)
                    152:                        p->u.thru.owner = NULL;
                    153:        }
                    154:        odata = abuf_wgetblk(obuf, &ocount, 0);
                    155:        odata[0] = c;
                    156:        abuf_wcommit(obuf, 1);
                    157: }
                    158:
1.7       ratchov   159: /*
                    160:  * parse ibuf contents and store each message into obuf,
                    161:  * use at most ``todo'' bytes (for throttling)
                    162:  */
1.1       ratchov   163: void
                    164: thru_bcopy(struct aproc *p, struct abuf *ibuf, struct abuf *obuf, unsigned todo)
                    165: {
                    166:        unsigned char *idata;
                    167:        unsigned c, icount, ioffs;
                    168:
                    169:        idata = NULL;
                    170:        icount = ioffs = 0;
                    171:        for (;;) {
                    172:                if (icount == 0) {
                    173:                        if (todo == 0)
                    174:                                break;
                    175:                        idata = abuf_rgetblk(ibuf, &icount, ioffs);
                    176:                        if (icount > todo)
                    177:                                icount = todo;
                    178:                        if (icount == 0)
                    179:                                break;
                    180:                        todo -= icount;
                    181:                        ioffs += icount;
                    182:                }
                    183:                c = *idata++;
                    184:                icount--;
                    185:                if (c < 0x80) {
1.10      ratchov   186:                        if (ibuf->r.midi.idx == 0 && ibuf->r.midi.st) {
                    187:                                ibuf->r.midi.msg[ibuf->r.midi.used++] = ibuf->r.midi.st;
                    188:                                ibuf->r.midi.idx++;
1.1       ratchov   189:                        }
1.10      ratchov   190:                        ibuf->r.midi.msg[ibuf->r.midi.used++] = c;
                    191:                        ibuf->r.midi.idx++;
                    192:                        if (ibuf->r.midi.idx == ibuf->r.midi.len) {
1.1       ratchov   193:                                thru_flush(p, ibuf, obuf);
1.10      ratchov   194:                                if (ibuf->r.midi.st >= 0xf0)
                    195:                                        ibuf->r.midi.st = 0;
                    196:                                ibuf->r.midi.idx = 0;
1.1       ratchov   197:                        }
1.10      ratchov   198:                        if (ibuf->r.midi.used == MIDI_MSGMAX) {
                    199:                                if (ibuf->r.midi.used == ibuf->r.midi.idx ||
1.1       ratchov   200:                                    p->u.thru.owner == ibuf)
                    201:                                        thru_flush(p, ibuf, obuf);
                    202:                                else
1.10      ratchov   203:                                        ibuf->r.midi.used = 0;
1.1       ratchov   204:                        }
                    205:                } else if (c < 0xf8) {
1.10      ratchov   206:                        if (ibuf->r.midi.used == ibuf->r.midi.idx ||
1.1       ratchov   207:                            p->u.thru.owner == ibuf) {
                    208:                                thru_flush(p, ibuf, obuf);
                    209:                        } else
1.10      ratchov   210:                                ibuf->r.midi.used = 0;
                    211:                        ibuf->r.midi.msg[0] = c;
                    212:                        ibuf->r.midi.used = 1;
                    213:                        ibuf->r.midi.len = (c >= 0xf0) ?
1.1       ratchov   214:                            common_len[c & 7] :
                    215:                            voice_len[(c >> 4) & 7];
1.10      ratchov   216:                        if (ibuf->r.midi.len == 1) {
1.1       ratchov   217:                                thru_flush(p, ibuf, obuf);
1.10      ratchov   218:                                ibuf->r.midi.idx = 0;
                    219:                                ibuf->r.midi.st = 0;
                    220:                                ibuf->r.midi.len = 0;
1.1       ratchov   221:                        } else {
1.10      ratchov   222:                                ibuf->r.midi.st = c;
                    223:                                ibuf->r.midi.idx = 1;
1.1       ratchov   224:                        }
                    225:                } else {
                    226:                        thru_rt(p, ibuf, obuf, c);
                    227:                }
                    228:        }
                    229: }
                    230:
                    231: int
                    232: thru_in(struct aproc *p, struct abuf *ibuf)
                    233: {
                    234:        struct abuf *i, *inext;
                    235:        unsigned todo;
                    236:
                    237:        if (!ABUF_ROK(ibuf))
                    238:                return 0;
1.10      ratchov   239:        if (ibuf->tickets == 0) {
1.14      ratchov   240: #ifdef DEBUG
                    241:                if (debug_level >= 4) {
                    242:                        abuf_dbg(ibuf);
                    243:                        dbg_puts(": out of tickets, blocking\n");
                    244:                }
                    245: #endif
1.1       ratchov   246:                return 0;
                    247:        }
                    248:        todo = ibuf->used;
1.10      ratchov   249:        if (todo > ibuf->tickets)
                    250:                todo = ibuf->tickets;
                    251:        ibuf->tickets -= todo;
1.1       ratchov   252:        for (i = LIST_FIRST(&p->obuflist); i != NULL; i = inext) {
                    253:                inext = LIST_NEXT(i, oent);
                    254:                if (ibuf->duplex == i)
                    255:                        continue;
                    256:                thru_bcopy(p, ibuf, i, todo);
                    257:                (void)abuf_flush(i);
                    258:        }
                    259:        abuf_rdiscard(ibuf, todo);
                    260:        return 1;
                    261: }
                    262:
                    263: int
                    264: thru_out(struct aproc *p, struct abuf *obuf)
                    265: {
                    266:        return 0;
                    267: }
                    268:
                    269: void
                    270: thru_eof(struct aproc *p, struct abuf *ibuf)
                    271: {
1.13      ratchov   272:        if (!(p->flags & APROC_QUIT))
1.11      ratchov   273:                return;
1.15      ratchov   274:        if (LIST_EMPTY(&p->ibuflist))
1.11      ratchov   275:                aproc_del(p);
1.1       ratchov   276: }
                    277:
                    278: void
                    279: thru_hup(struct aproc *p, struct abuf *obuf)
                    280: {
                    281: }
                    282:
                    283: void
                    284: thru_newin(struct aproc *p, struct abuf *ibuf)
                    285: {
1.10      ratchov   286:        ibuf->r.midi.used = 0;
                    287:        ibuf->r.midi.len = 0;
                    288:        ibuf->r.midi.idx = 0;
                    289:        ibuf->r.midi.st = 0;
                    290:        ibuf->tickets = MIDITHRU_XFER;
1.1       ratchov   291: }
                    292:
                    293: void
                    294: thru_done(struct aproc *p)
                    295: {
                    296:        timo_del(&p->u.thru.timo);
                    297: }
                    298:
                    299: struct aproc_ops thru_ops = {
                    300:        "thru",
                    301:        thru_in,
                    302:        thru_out,
                    303:        thru_eof,
                    304:        thru_hup,
                    305:        thru_newin,
                    306:        NULL, /* newout */
                    307:        NULL, /* ipos */
                    308:        NULL, /* opos */
                    309:        thru_done
                    310: };
                    311:
1.7       ratchov   312: /*
                    313:  * call-back invoked periodically to implement throttling at each invocation
                    314:  * gain more ``tickets'' for processing.  If one of the buffer was blocked by
                    315:  * the throttelling mechanism, then run it
                    316:  */
1.1       ratchov   317: void
                    318: thru_cb(void *addr)
                    319: {
                    320:        struct aproc *p = (struct aproc *)addr;
                    321:        struct abuf *i, *inext;
                    322:        unsigned tickets;
                    323:
                    324:        timo_add(&p->u.thru.timo, MIDITHRU_TIMO);
                    325:
                    326:        for (i = LIST_FIRST(&p->ibuflist); i != NULL; i = inext) {
                    327:                inext = LIST_NEXT(i, ient);
1.10      ratchov   328:                tickets = i->tickets;
                    329:                i->tickets = MIDITHRU_XFER;
1.1       ratchov   330:                if (tickets == 0)
                    331:                        abuf_run(i);
                    332:        }
                    333: }
                    334:
                    335: struct aproc *
                    336: thru_new(char *name)
                    337: {
                    338:        struct aproc *p;
                    339:
                    340:        p = aproc_new(&thru_ops, name);
                    341:        p->u.thru.owner = NULL;
                    342:        timo_set(&p->u.thru.timo, thru_cb, p);
                    343:        timo_add(&p->u.thru.timo, MIDITHRU_TIMO);
1.3       ratchov   344:        return p;
                    345: }
                    346:
1.14      ratchov   347: #ifdef DEBUG
                    348: void
                    349: ctl_slotdbg(struct aproc *p, int slot)
                    350: {
                    351:        struct ctl_slot *s;
                    352:
                    353:        if (slot < 0) {
1.17    ! ratchov   354:                dbg_puts("none");
1.14      ratchov   355:        } else {
                    356:                s = p->u.ctl.slot + slot;
                    357:                dbg_puts(s->name);
                    358:                dbg_putu(s->unit);
1.17    ! ratchov   359:                dbg_puts("(");
1.14      ratchov   360:                dbg_putu(s->vol);
1.17    ! ratchov   361:                dbg_puts(")/");
1.14      ratchov   362:                switch (s->tstate) {
                    363:                case CTL_OFF:
                    364:                        dbg_puts("off");
                    365:                        break;
                    366:                case CTL_RUN:
                    367:                        dbg_puts("run");
                    368:                        break;
                    369:                case CTL_START:
                    370:                        dbg_puts("sta");
                    371:                        break;
                    372:                case CTL_STOP:
                    373:                        dbg_puts("stp");
                    374:                        break;
                    375:                default:
                    376:                        dbg_puts("unk");
                    377:                        break;
                    378:                }
                    379:        }
                    380: }
                    381: #endif
1.12      ratchov   382:
1.7       ratchov   383: /*
                    384:  * broadcast a message to all output buffers on the behalf of ibuf.
                    385:  * ie. don't sent back the message to the sender
                    386:  */
1.3       ratchov   387: void
                    388: ctl_sendmsg(struct aproc *p, struct abuf *ibuf, unsigned char *msg, unsigned len)
                    389: {
                    390:        unsigned ocount, itodo;
                    391:        unsigned char *odata, *idata;
                    392:        struct abuf *i, *inext;
                    393:
                    394:        for (i = LIST_FIRST(&p->obuflist); i != NULL; i = inext) {
                    395:                inext = LIST_NEXT(i, oent);
1.17    ! ratchov   396:                if (i->duplex && i->duplex == ibuf)
1.3       ratchov   397:                        continue;
                    398:                itodo = len;
                    399:                idata = msg;
                    400:                while (itodo > 0) {
                    401:                        if (!ABUF_WOK(i)) {
1.14      ratchov   402: #ifdef DEBUG
                    403:                                if (debug_level >= 4) {
                    404:                                        abuf_dbg(i);
                    405:                                        dbg_puts(": overrun, discarding ");
                    406:                                        dbg_putu(i->used);
                    407:                                        dbg_puts(" bytes\n");
                    408:                                }
                    409: #endif
1.3       ratchov   410:                                abuf_rdiscard(i, i->used);
                    411:                        }
                    412:                        odata = abuf_wgetblk(i, &ocount, 0);
                    413:                        if (ocount > itodo)
                    414:                                ocount = itodo;
1.14      ratchov   415: #ifdef DEBUG
                    416:                        if (debug_level >= 4) {
                    417:                                abuf_dbg(i);
                    418:                                dbg_puts(": stored ");
                    419:                                dbg_putu(ocount);
                    420:                                dbg_puts(" bytes\n");
                    421:                        }
                    422: #endif
1.3       ratchov   423:                        memcpy(odata, idata, ocount);
                    424:                        abuf_wcommit(i, ocount);
                    425:                        itodo -= ocount;
                    426:                        idata += ocount;
                    427:                }
                    428:                (void)abuf_flush(i);
                    429:        }
                    430: }
                    431:
1.7       ratchov   432: /*
1.13      ratchov   433:  * send a quarter frame MTC message
                    434:  */
                    435: void
                    436: ctl_qfr(struct aproc *p)
                    437: {
                    438:        unsigned char buf[2];
                    439:        unsigned data;
                    440:
                    441:        switch (p->u.ctl.qfr) {
                    442:        case 0:
                    443:                data = p->u.ctl.fr & 0xf;
                    444:                break;
                    445:        case 1:
                    446:                data = p->u.ctl.fr >> 4;
                    447:                break;
                    448:        case 2:
                    449:                data = p->u.ctl.sec & 0xf;
                    450:                break;
                    451:        case 3:
                    452:                data = p->u.ctl.sec >> 4;
                    453:                break;
                    454:        case 4:
                    455:                data = p->u.ctl.min & 0xf;
                    456:                break;
                    457:        case 5:
                    458:                data = p->u.ctl.min >> 4;
                    459:                break;
                    460:        case 6:
                    461:                data = p->u.ctl.hr & 0xf;
                    462:                break;
                    463:        case 7:
                    464:                data = (p->u.ctl.hr >> 4) | (p->u.ctl.fps_id << 1);
                    465:                /*
                    466:                 * tick messages are sent 2 frames ahead
                    467:                 */
                    468:                p->u.ctl.fr += 2;
                    469:                if (p->u.ctl.fr < p->u.ctl.fps)
                    470:                        break;
                    471:                p->u.ctl.fr -= p->u.ctl.fps;
                    472:                p->u.ctl.sec++;
                    473:                if (p->u.ctl.sec < 60)
                    474:                        break;;
                    475:                p->u.ctl.sec = 0;
                    476:                p->u.ctl.min++;
                    477:                if (p->u.ctl.min < 60)
                    478:                        break;
                    479:                p->u.ctl.min = 0;
                    480:                p->u.ctl.hr++;
                    481:                if (p->u.ctl.hr < 24)
                    482:                        break;
                    483:                p->u.ctl.hr = 0;
                    484:                break;
                    485:        default:
                    486:                /* NOTREACHED */
                    487:                data = 0;
                    488:        }
                    489:        buf[0] = 0xf1;
                    490:        buf[1] = (p->u.ctl.qfr << 4) | data;
                    491:        p->u.ctl.qfr++;
                    492:        p->u.ctl.qfr &= 7;
                    493:        ctl_sendmsg(p, NULL, buf, 2);
                    494: }
                    495:
                    496: /*
                    497:  * send a full frame MTC message
                    498:  */
                    499: void
                    500: ctl_full(struct aproc *p)
                    501: {
                    502:        unsigned char buf[10];
                    503:        unsigned origin = p->u.ctl.origin;
                    504:        unsigned fps = p->u.ctl.fps;
                    505:
                    506:        p->u.ctl.hr =  (origin / (3600 * MTC_SEC)) % 24;
                    507:        p->u.ctl.min = (origin / (60 * MTC_SEC))   % 60;
                    508:        p->u.ctl.sec = (origin / MTC_SEC)          % 60;
                    509:        p->u.ctl.fr =  (origin / (MTC_SEC / fps))  % fps;
                    510:
                    511:        buf[0] = 0xf0;
                    512:        buf[1] = 0x7f;
                    513:        buf[2] = 0x7f;
                    514:        buf[3] = 0x01;
                    515:        buf[4] = 0x01;
                    516:        buf[5] = p->u.ctl.hr | (p->u.ctl.fps_id << 5);
                    517:        buf[6] = p->u.ctl.min;
                    518:        buf[7] = p->u.ctl.sec;
                    519:        buf[8] = p->u.ctl.fr;
                    520:        buf[9] = 0xf7;
                    521:        p->u.ctl.qfr = 0;
                    522:        ctl_sendmsg(p, NULL, buf, 10);
                    523: }
                    524:
                    525: /*
1.12      ratchov   526:  * find the best matching free slot index (ie midi channel).
                    527:  * return -1, if there are no free slots anymore
1.7       ratchov   528:  */
1.3       ratchov   529: int
1.12      ratchov   530: ctl_getidx(struct aproc *p, char *who)
1.3       ratchov   531: {
                    532:        char *s;
                    533:        struct ctl_slot *slot;
1.4       ratchov   534:        char name[CTL_NAMEMAX];
                    535:        unsigned i, unit, umap = 0;
1.6       ratchov   536:        unsigned ser, bestser, bestidx;
1.3       ratchov   537:
1.4       ratchov   538:        /*
                    539:         * create a ``valid'' control name (lowcase, remove [^a-z], trucate)
                    540:         */
1.5       ratchov   541:        for (i = 0, s = who; ; s++) {
1.4       ratchov   542:                if (i == CTL_NAMEMAX - 1 || *s == '\0') {
                    543:                        name[i] = '\0';
                    544:                        break;
                    545:                } else if (*s >= 'A' && *s <= 'Z') {
                    546:                        name[i++] = *s + 'a' - 'A';
                    547:                } else if (*s >= 'a' && *s <= 'z')
                    548:                        name[i++] = *s;
                    549:        }
                    550:        if (i == 0)
                    551:                strlcpy(name, "noname", CTL_NAMEMAX);
                    552:
                    553:        /*
                    554:         * find the instance number of the control name
                    555:         */
                    556:        for (i = 0, slot = p->u.ctl.slot; i < CTL_NSLOT; i++, slot++) {
1.12      ratchov   557:                if (slot->ops == NULL)
1.4       ratchov   558:                        continue;
                    559:                if (strcmp(slot->name, name) == 0)
                    560:                        umap |= (1 << i);
                    561:        }
1.12      ratchov   562:        for (unit = 0; ; unit++) {
1.4       ratchov   563:                if (unit == CTL_NSLOT)
1.3       ratchov   564:                        return -1;
1.12      ratchov   565:                if ((umap & (1 << unit)) == 0)
1.3       ratchov   566:                        break;
                    567:        }
1.14      ratchov   568: #ifdef DEBUG
                    569:        if (debug_level >= 3) {
                    570:                aproc_dbg(p);
                    571:                dbg_puts(": new control name is ");
                    572:                dbg_puts(name);
                    573:                dbg_putu(unit);
                    574:                dbg_puts("\n");
                    575:        }
                    576: #endif
1.4       ratchov   577:        /*
                    578:         * find a free controller slot with the same name/unit
                    579:         */
                    580:        for (i = 0, slot = p->u.ctl.slot; i < CTL_NSLOT; i++, slot++) {
1.12      ratchov   581:                if (slot->ops == NULL &&
1.4       ratchov   582:                    strcmp(slot->name, name) == 0 &&
                    583:                    slot->unit == unit) {
1.14      ratchov   584: #ifdef DEBUG
                    585:                        if (debug_level >= 3) {
                    586:                                aproc_dbg(p);
                    587:                                dbg_puts(": found slot ");
                    588:                                dbg_putu(i);
                    589:                                dbg_puts("\n");
                    590:                        }
                    591: #endif
1.4       ratchov   592:                        return i;
                    593:                }
                    594:        }
                    595:
                    596:        /*
1.6       ratchov   597:         * couldn't find a matching slot, pick oldest free slot
1.12      ratchov   598:         * and set its name/unit
1.4       ratchov   599:         */
1.6       ratchov   600:        bestser = 0;
                    601:        bestidx = CTL_NSLOT;
                    602:        for (i = 0, slot = p->u.ctl.slot; i < CTL_NSLOT; i++, slot++) {
1.12      ratchov   603:                if (slot->ops != NULL)
1.6       ratchov   604:                        continue;
                    605:                ser = p->u.ctl.serial - slot->serial;
                    606:                if (ser > bestser) {
                    607:                        bestser = ser;
                    608:                        bestidx = i;
                    609:                }
1.3       ratchov   610:        }
1.6       ratchov   611:        if (bestidx == CTL_NSLOT)
                    612:                return -1;
                    613:        slot = p->u.ctl.slot + bestidx;
1.4       ratchov   614:        strlcpy(slot->name, name, CTL_NAMEMAX);
1.6       ratchov   615:        slot->serial = p->u.ctl.serial++;
1.4       ratchov   616:        slot->unit = unit;
1.7       ratchov   617:        slot->vol = MIDI_MAXCTL;
1.14      ratchov   618: #ifdef DEBUG
                    619:        if (debug_level >= 3) {
                    620:                aproc_dbg(p);
                    621:                dbg_puts(": overwritten slot ");
                    622:                dbg_putu(bestidx);
                    623:                dbg_puts("\n");
                    624:        }
                    625: #endif
1.6       ratchov   626:        return bestidx;
1.3       ratchov   627: }
                    628:
1.7       ratchov   629: /*
1.13      ratchov   630:  * check that all clients controlled by MMC are ready to start,
                    631:  * if so, start them all but the caller
                    632:  */
                    633: int
                    634: ctl_trystart(struct aproc *p, int caller)
                    635: {
                    636:        unsigned i;
                    637:        struct ctl_slot *s;
                    638:
                    639:        if (p->u.ctl.tstate != CTL_START) {
1.14      ratchov   640: #ifdef DEBUG
1.17    ! ratchov   641:                if (debug_level >= 3) {
        !           642:                        ctl_slotdbg(p, caller);
        !           643:                        dbg_puts(": server not started, delayd\n");
        !           644:                }
1.14      ratchov   645: #endif
1.13      ratchov   646:                return 0;
                    647:        }
                    648:        for (i = 0, s = p->u.ctl.slot; i < CTL_NSLOT; i++, s++) {
                    649:                if (!s->ops || i == caller)
                    650:                        continue;
                    651:                if (s->tstate != CTL_OFF && s->tstate != CTL_START) {
1.14      ratchov   652: #ifdef DEBUG
1.17    ! ratchov   653:                        if (debug_level >= 3) {
        !           654:                                ctl_slotdbg(p, i);
        !           655:                                dbg_puts(": not ready, server delayed\n");
        !           656:                        }
1.14      ratchov   657: #endif
1.13      ratchov   658:                        return 0;
                    659:                }
                    660:        }
                    661:        for (i = 0, s = p->u.ctl.slot; i < CTL_NSLOT; i++, s++) {
                    662:                if (!s->ops || i == caller)
                    663:                        continue;
                    664:                if (s->tstate == CTL_START) {
1.14      ratchov   665: #ifdef DEBUG
1.17    ! ratchov   666:                        if (debug_level >= 3) {
        !           667:                                ctl_slotdbg(p, i);
        !           668:                                dbg_puts(": started\n");
        !           669:                        }
1.14      ratchov   670: #endif
1.13      ratchov   671:                        s->tstate = CTL_RUN;
                    672:                        s->ops->start(s->arg);
                    673:                }
                    674:        }
                    675:        if (caller >= 0)
                    676:                p->u.ctl.slot[caller].tstate = CTL_RUN;
                    677:        p->u.ctl.tstate = CTL_RUN;
                    678:        p->u.ctl.delta = MTC_SEC * dev_getpos();
1.17    ! ratchov   679:        if (dev_rate % (30 * 4 * dev_round) == 0) {
1.13      ratchov   680:                p->u.ctl.fps_id = MTC_FPS_30;
                    681:                p->u.ctl.fps = 30;
1.17    ! ratchov   682:        } else if (dev_rate % (25 * 4 * dev_round) == 0) {
1.13      ratchov   683:                p->u.ctl.fps_id = MTC_FPS_25;
                    684:                p->u.ctl.fps = 25;
                    685:        } else {
                    686:                p->u.ctl.fps_id = MTC_FPS_24;
                    687:                p->u.ctl.fps = 24;
                    688:        }
1.14      ratchov   689: #ifdef DEBUG
1.17    ! ratchov   690:        if (debug_level >= 3) {
        !           691:                ctl_slotdbg(p, caller);
        !           692:                dbg_puts(": started server at ");
        !           693:                dbg_puti(p->u.ctl.delta);
        !           694:                dbg_puts(", ");
        !           695:                dbg_puti(p->u.ctl.fps);
        !           696:                dbg_puts(" mtc fps\n");
        !           697:        }
1.14      ratchov   698: #endif
1.17    ! ratchov   699:        if (dev_pstate == DEV_INIT)
        !           700:                dev_pstate = DEV_START;
1.13      ratchov   701:        ctl_full(p);
                    702:        return 1;
                    703: }
                    704:
                    705: /*
1.12      ratchov   706:  * allocate a new slot and register the given call-backs
                    707:  */
                    708: int
1.13      ratchov   709: ctl_slotnew(struct aproc *p, char *who, struct ctl_ops *ops, void *arg, int tr)
1.12      ratchov   710: {
                    711:        int idx;
                    712:        struct ctl_slot *s;
                    713:
1.17    ! ratchov   714:        if (!APROC_OK(p)) {
        !           715: #ifdef DEBUG
        !           716:                if (debug_level >= 1) {
        !           717:                        dbg_puts(who);
        !           718:                        dbg_puts(": MIDI control not available\n");
        !           719:                }
        !           720: #endif
1.13      ratchov   721:                return -1;
1.17    ! ratchov   722:        }
1.12      ratchov   723:        idx = ctl_getidx(p, who);
                    724:        if (idx < 0)
                    725:                return -1;
                    726:
                    727:        s = p->u.ctl.slot + idx;
                    728:        s->ops = ops;
                    729:        s->arg = arg;
1.13      ratchov   730:        s->tstate = tr ? CTL_STOP : CTL_OFF;
1.12      ratchov   731:        s->ops->vol(s->arg, s->vol);
                    732:        ctl_slotvol(p, idx, s->vol);
                    733:        return idx;
                    734: }
                    735:
                    736: /*
1.7       ratchov   737:  * release the given slot
                    738:  */
1.3       ratchov   739: void
                    740: ctl_slotdel(struct aproc *p, int index)
                    741: {
1.13      ratchov   742:        unsigned i;
                    743:        struct ctl_slot *s;
                    744:
1.17    ! ratchov   745:        if (!APROC_OK(p))
1.13      ratchov   746:                return;
1.12      ratchov   747:        p->u.ctl.slot[index].ops = NULL;
1.13      ratchov   748:        if (!(p->flags & APROC_QUIT))
                    749:                return;
                    750:        for (i = 0, s = p->u.ctl.slot; i < CTL_NSLOT; i++, s++) {
                    751:                if (s->ops)
                    752:                        return;
                    753:        }
                    754:        if (!LIST_EMPTY(&p->obuflist) || !LIST_EMPTY(&p->ibuflist))
                    755:                aproc_del(p);
                    756: }
                    757:
                    758: /*
                    759:  * called at every clock tick by the mixer, delta is positive, unless
                    760:  * there's an overrun/underrun
                    761:  */
                    762: void
                    763: ctl_ontick(struct aproc *p, int delta)
                    764: {
                    765:        int qfrlen;
                    766:
                    767:        /*
                    768:         * don't send ticks before the start signal
                    769:         */
                    770:        if (p->u.ctl.tstate != CTL_RUN)
                    771:                return;
                    772:
                    773:        p->u.ctl.delta += delta * MTC_SEC;
                    774:
                    775:        /*
                    776:         * don't send ticks during the count-down
                    777:         */
                    778:        if (p->u.ctl.delta < 0)
                    779:                return;
                    780:
                    781:        qfrlen = dev_rate * (MTC_SEC / (4 * p->u.ctl.fps));
                    782:        while (p->u.ctl.delta >= qfrlen) {
                    783:                ctl_qfr(p);
                    784:                p->u.ctl.delta -= qfrlen;
                    785:        }
1.3       ratchov   786: }
                    787:
1.7       ratchov   788: /*
                    789:  * notifty the mixer that volume changed, called by whom allocad the slot using
                    790:  * ctl_slotnew(). Note: it doesn't make sens to call this from within the
                    791:  * call-back.
                    792:  */
1.3       ratchov   793: void
                    794: ctl_slotvol(struct aproc *p, int slot, unsigned vol)
                    795: {
                    796:        unsigned char msg[3];
                    797:
1.17    ! ratchov   798:        if (!APROC_OK(p))
1.13      ratchov   799:                return;
1.14      ratchov   800: #ifdef DEBUG
                    801:        if (debug_level >= 3) {
                    802:                ctl_slotdbg(p, slot);
                    803:                dbg_puts(": changing volume to ");
                    804:                dbg_putu(vol);
                    805:                dbg_puts("\n");
                    806:        }
                    807: #endif
1.7       ratchov   808:        p->u.ctl.slot[slot].vol = vol;
1.3       ratchov   809:        msg[0] = MIDI_CTL | slot;
                    810:        msg[1] = MIDI_CTLVOL;
                    811:        msg[2] = vol;
                    812:        ctl_sendmsg(p, NULL, msg, 3);
                    813: }
                    814:
1.7       ratchov   815: /*
1.13      ratchov   816:  * notify the MMC layer that the stream is attempting
                    817:  * to start. If other streams are not ready, 0 is returned meaning
                    818:  * that the stream should wait. If other streams are ready, they
                    819:  * are started, and the caller should start immediately.
                    820:  */
                    821: int
                    822: ctl_slotstart(struct aproc *p, int slot)
                    823: {
                    824:        struct ctl_slot *s = p->u.ctl.slot + slot;
                    825:
1.17    ! ratchov   826:        if (!APROC_OK(p))
1.13      ratchov   827:                return 1;
                    828:        if (s->tstate == CTL_OFF || p->u.ctl.tstate == CTL_OFF)
                    829:                return 1;
                    830:
                    831:        /*
                    832:         * if the server already started (the client missed the
                    833:         * start rendez-vous) or the server is stopped, then
                    834:         * tag the client as ``wanting to start''
                    835:         */
                    836:        s->tstate = CTL_START;
                    837:        return ctl_trystart(p, slot);
                    838: }
                    839:
                    840: /*
                    841:  * notify the MMC layer that the stream no longer is trying to
                    842:  * start (or that it just stopped), meaning that its ``start'' call-back
                    843:  * shouldn't be called anymore
                    844:  */
                    845: void
                    846: ctl_slotstop(struct aproc *p, int slot)
                    847: {
                    848:        struct ctl_slot *s = p->u.ctl.slot + slot;
                    849:
1.17    ! ratchov   850:        if (!APROC_OK(p))
1.13      ratchov   851:                return;
                    852:        /*
                    853:         * tag the stream as not trying to start,
                    854:         * unless MMC is turned off
                    855:         */
                    856:        if (s->tstate != CTL_OFF)
                    857:                s->tstate = CTL_STOP;
                    858: }
                    859:
                    860: /*
1.17    ! ratchov   861:  * start all slots simultaneously
        !           862:  */
        !           863: void
        !           864: ctl_start(struct aproc *p)
        !           865: {
        !           866:        if (!APROC_OK(p))
        !           867:                return;
        !           868:        if (p->u.ctl.tstate == CTL_STOP) {
        !           869:                p->u.ctl.tstate = CTL_START;
        !           870:                (void)ctl_trystart(p, -1);
        !           871: #ifdef DEBUG
        !           872:        } else {
        !           873:                if (debug_level >= 3) {
        !           874:                        aproc_dbg(p);
        !           875:                        dbg_puts(": ignoring mmc start\n");
        !           876:                }
        !           877: #endif
        !           878:        }
        !           879: }
        !           880:
        !           881: /*
        !           882:  * stop all slots simultaneously
        !           883:  */
        !           884: void
        !           885: ctl_stop(struct aproc *p)
        !           886: {
        !           887:        unsigned i;
        !           888:        struct ctl_slot *s;
        !           889:
        !           890:        if (!APROC_OK(p))
        !           891:                return;
        !           892:        switch (p->u.ctl.tstate) {
        !           893:        case CTL_START:
        !           894:                p->u.ctl.tstate = CTL_STOP;
        !           895:                return;
        !           896:        case CTL_RUN:
        !           897:                p->u.ctl.tstate = CTL_STOP;
        !           898:                break;
        !           899:        default:
        !           900: #ifdef DEBUG
        !           901:                if (debug_level >= 3) {
        !           902:                        aproc_dbg(p);
        !           903:                        dbg_puts(": ignored mmc stop\n");
        !           904:                }
        !           905: #endif
        !           906:                return;
        !           907:        }
        !           908:        for (i = 0, s = p->u.ctl.slot; i < CTL_NSLOT; i++, s++) {
        !           909:                if (!s->ops)
        !           910:                        continue;
        !           911:                if (s->tstate == CTL_RUN) {
        !           912: #ifdef DEBUG
        !           913:                        if (debug_level >= 3) {
        !           914:                                ctl_slotdbg(p, i);
        !           915:                                dbg_puts(": requested to stop\n");
        !           916:                        }
        !           917: #endif
        !           918:                        s->ops->stop(s->arg);
        !           919:                }
        !           920:        }
        !           921: }
        !           922:
        !           923: /*
        !           924:  * relocate all slots simultaneously
        !           925:  */
        !           926: void
        !           927: ctl_loc(struct aproc *p, unsigned origin)
        !           928: {
        !           929:        unsigned i, tstate;
        !           930:        struct ctl_slot *s;
        !           931:
        !           932:        if (!APROC_OK(p))
        !           933:                return;
        !           934: #ifdef DEBUG
        !           935:        if (debug_level >= 2) {
        !           936:                dbg_puts("server relocated to ");
        !           937:                dbg_putu(origin);
        !           938:                dbg_puts("\n");
        !           939:        }
        !           940: #endif
        !           941:        tstate = p->u.ctl.tstate;
        !           942:        if (tstate == CTL_RUN)
        !           943:                ctl_stop(p);
        !           944:        p->u.ctl.origin = origin;
        !           945:        for (i = 0, s = p->u.ctl.slot; i < CTL_NSLOT; i++, s++) {
        !           946:                if (!s->ops)
        !           947:                        continue;
        !           948:                s->ops->loc(s->arg, p->u.ctl.origin);
        !           949:        }
        !           950:        if (tstate == CTL_RUN)
        !           951:                ctl_start(p);
        !           952: }
        !           953:
        !           954: /*
        !           955:  * check if there are controlled streams
        !           956:  */
        !           957: int
        !           958: ctl_idle(struct aproc *p)
        !           959: {
        !           960:        unsigned i;
        !           961:        struct ctl_slot *s;
        !           962:
        !           963:        if (!APROC_OK(p))
        !           964:                return 1;
        !           965:        for (i = 0, s = p->u.ctl.slot; i < CTL_NSLOT; i++, s++) {
        !           966:                if (s->ops)
        !           967:                        return 0;
        !           968:        }
        !           969:        return 1;
        !           970: }
        !           971:
        !           972: /*
1.7       ratchov   973:  * handle a MIDI event received from ibuf
                    974:  */
1.3       ratchov   975: void
                    976: ctl_ev(struct aproc *p, struct abuf *ibuf)
                    977: {
                    978:        unsigned chan;
1.5       ratchov   979:        struct ctl_slot *slot;
1.13      ratchov   980:        unsigned fps;
1.14      ratchov   981: #ifdef DEBUG
                    982:        unsigned i;
                    983:
                    984:        if (debug_level >= 3) {
                    985:                abuf_dbg(ibuf);
                    986:                dbg_puts(": got event:");
                    987:                for (i = 0; i < ibuf->r.midi.idx; i++) {
                    988:                        dbg_puts(" ");
                    989:                        dbg_putx(ibuf->r.midi.msg[i]);
                    990:                }
                    991:                dbg_puts("\n");
                    992:        }
                    993: #endif
1.10      ratchov   994:        if ((ibuf->r.midi.msg[0] & MIDI_CMDMASK) == MIDI_CTL &&
                    995:            ibuf->r.midi.msg[1] == MIDI_CTLVOL) {
                    996:                chan = ibuf->r.midi.msg[0] & MIDI_CHANMASK;
1.3       ratchov   997:                if (chan >= CTL_NSLOT)
                    998:                        return;
1.5       ratchov   999:                slot = p->u.ctl.slot + chan;
1.12      ratchov  1000:                if (slot->ops == NULL)
1.3       ratchov  1001:                        return;
1.10      ratchov  1002:                slot->vol = ibuf->r.midi.msg[2];
1.12      ratchov  1003:                slot->ops->vol(slot->arg, slot->vol);
1.10      ratchov  1004:                ctl_sendmsg(p, ibuf, ibuf->r.midi.msg, ibuf->r.midi.len);
1.3       ratchov  1005:        }
1.13      ratchov  1006:        if (ibuf->r.midi.idx == 6 &&
                   1007:            ibuf->r.midi.msg[0] == 0xf0 &&
                   1008:            ibuf->r.midi.msg[1] == 0x7f &&      /* type is realtime */
                   1009:            ibuf->r.midi.msg[3] == 0x06 &&      /* subtype is mmc */
                   1010:            ibuf->r.midi.msg[5] == 0xf7) {      /* subtype is mmc */
                   1011:                switch (ibuf->r.midi.msg[4]) {
                   1012:                case 0x01:      /* mmc stop */
1.14      ratchov  1013: #ifdef DEBUG
1.17    ! ratchov  1014:                        if (debug_level >= 3) {
1.14      ratchov  1015:                                abuf_dbg(ibuf);
                   1016:                                dbg_puts(": mmc stop\n");
                   1017:                        }
                   1018: #endif
1.17    ! ratchov  1019:                        ctl_stop(p);
1.13      ratchov  1020:                        break;
                   1021:                case 0x02:      /* mmc start */
1.14      ratchov  1022: #ifdef DEBUG
1.17    ! ratchov  1023:                        if (debug_level >= 3) {
1.14      ratchov  1024:                                abuf_dbg(ibuf);
                   1025:                                dbg_puts(": mmc start\n");
                   1026:                        }
                   1027: #endif
1.17    ! ratchov  1028:                        ctl_start(p);
1.13      ratchov  1029:                        break;
                   1030:                }
                   1031:        }
                   1032:        if (ibuf->r.midi.idx == 13 &&
                   1033:            ibuf->r.midi.msg[0] == 0xf0 &&
                   1034:            ibuf->r.midi.msg[1] == 0x7f &&
                   1035:            ibuf->r.midi.msg[3] == 0x06 &&
                   1036:            ibuf->r.midi.msg[4] == 0x44 &&
                   1037:            ibuf->r.midi.msg[5] == 0x06 &&
                   1038:            ibuf->r.midi.msg[6] == 0x01 &&
                   1039:            ibuf->r.midi.msg[12] == 0xf7) {
                   1040:                switch (ibuf->r.midi.msg[7] >> 5) {
                   1041:                case MTC_FPS_24:
                   1042:                        fps = 24;
                   1043:                        break;
                   1044:                case MTC_FPS_25:
                   1045:                        fps = 25;
                   1046:                        break;
                   1047:                case MTC_FPS_30:
                   1048:                        fps = 30;
                   1049:                        break;
                   1050:                default:
                   1051:                        p->u.ctl.origin = 0;
                   1052:                        return;
                   1053:                }
1.17    ! ratchov  1054:                ctl_loc(p,
1.13      ratchov  1055:                    (ibuf->r.midi.msg[7] & 0x1f) * 3600 * MTC_SEC +
                   1056:                    ibuf->r.midi.msg[8] * 60 * MTC_SEC +
                   1057:                    ibuf->r.midi.msg[9] * MTC_SEC +
                   1058:                    ibuf->r.midi.msg[10] * (MTC_SEC / fps) +
1.17    ! ratchov  1059:                    ibuf->r.midi.msg[11] * (MTC_SEC / 100 / fps));
1.13      ratchov  1060:        }
1.3       ratchov  1061: }
                   1062:
                   1063: int
                   1064: ctl_in(struct aproc *p, struct abuf *ibuf)
                   1065: {
                   1066:        unsigned char *idata;
                   1067:        unsigned c, i, icount;
                   1068:
                   1069:        if (!ABUF_ROK(ibuf))
                   1070:                return 0;
                   1071:        idata = abuf_rgetblk(ibuf, &icount, 0);
                   1072:        for (i = 0; i < icount; i++) {
                   1073:                c = *idata++;
1.8       ratchov  1074:                if (c >= 0xf8) {
                   1075:                        /* clock events not used yet */
                   1076:                } else if (c >= 0xf0) {
1.10      ratchov  1077:                        if (ibuf->r.midi.st == 0xf0 && c == 0xf7 &&
                   1078:                            ibuf->r.midi.idx < MIDI_MSGMAX) {
                   1079:                                ibuf->r.midi.msg[ibuf->r.midi.idx++] = c;
1.8       ratchov  1080:                                ctl_ev(p, ibuf);
                   1081:                                continue;
                   1082:                        }
1.10      ratchov  1083:                        ibuf->r.midi.msg[0] = c;
                   1084:                        ibuf->r.midi.len = common_len[c & 7];
                   1085:                        ibuf->r.midi.st = c;
                   1086:                        ibuf->r.midi.idx = 1;
1.3       ratchov  1087:                } else if (c >= 0x80) {
1.10      ratchov  1088:                        ibuf->r.midi.msg[0] = c;
                   1089:                        ibuf->r.midi.len = voice_len[(c >> 4) & 7];
                   1090:                        ibuf->r.midi.st = c;
                   1091:                        ibuf->r.midi.idx = 1;
                   1092:                } else if (ibuf->r.midi.st) {
                   1093:                        if (ibuf->r.midi.idx == MIDI_MSGMAX)
1.8       ratchov  1094:                                continue;
1.10      ratchov  1095:                        if (ibuf->r.midi.idx == 0)
                   1096:                                ibuf->r.midi.msg[ibuf->r.midi.idx++] = ibuf->r.midi.st;
                   1097:                        ibuf->r.midi.msg[ibuf->r.midi.idx++] = c;
                   1098:                        if (ibuf->r.midi.idx == ibuf->r.midi.len) {
1.3       ratchov  1099:                                ctl_ev(p, ibuf);
1.10      ratchov  1100:                                ibuf->r.midi.idx = 0;
1.3       ratchov  1101:                        }
                   1102:                }
                   1103:        }
                   1104:        abuf_rdiscard(ibuf, icount);
                   1105:        return 1;
                   1106: }
                   1107:
                   1108: int
                   1109: ctl_out(struct aproc *p, struct abuf *obuf)
                   1110: {
                   1111:        return 0;
                   1112: }
                   1113:
                   1114: void
                   1115: ctl_eof(struct aproc *p, struct abuf *ibuf)
                   1116: {
1.13      ratchov  1117:        unsigned i;
                   1118:        struct ctl_slot *s;
                   1119:
                   1120:        if (!(p->flags & APROC_QUIT))
                   1121:                return;
                   1122:        for (i = 0, s = p->u.ctl.slot; i < CTL_NSLOT; i++, s++) {
                   1123:                if (s->ops)
                   1124:                        return;
                   1125:        }
                   1126:        if (!LIST_EMPTY(&p->obuflist) || !LIST_EMPTY(&p->ibuflist))
                   1127:                aproc_del(p);
1.3       ratchov  1128: }
                   1129:
                   1130: void
                   1131: ctl_hup(struct aproc *p, struct abuf *obuf)
                   1132: {
1.13      ratchov  1133:        unsigned i;
                   1134:        struct ctl_slot *s;
                   1135:
                   1136:        if (!(p->flags & APROC_QUIT))
                   1137:                return;
                   1138:        for (i = 0, s = p->u.ctl.slot; i < CTL_NSLOT; i++, s++) {
                   1139:                if (s->ops)
                   1140:                        return;
                   1141:        }
                   1142:        if (!LIST_EMPTY(&p->obuflist) || !LIST_EMPTY(&p->ibuflist))
                   1143:                aproc_del(p);
1.3       ratchov  1144: }
                   1145:
                   1146: void
                   1147: ctl_newin(struct aproc *p, struct abuf *ibuf)
                   1148: {
1.10      ratchov  1149:        ibuf->r.midi.used = 0;
                   1150:        ibuf->r.midi.len = 0;
                   1151:        ibuf->r.midi.idx = 0;
                   1152:        ibuf->r.midi.st = 0;
1.3       ratchov  1153: }
                   1154:
                   1155: void
                   1156: ctl_done(struct aproc *p)
                   1157: {
1.14      ratchov  1158: #ifdef DEBUG
                   1159:        unsigned i;
                   1160:        struct ctl_slot *s;
                   1161:
                   1162:        for (i = 0, s = p->u.ctl.slot; i < CTL_NSLOT; i++, s++) {
                   1163:                /*
                   1164:                 * XXX: shouldn't we abord() here ?
                   1165:                 */
                   1166:                if (s->ops != NULL) {
                   1167:                        ctl_slotdbg(p, i);
                   1168:                        dbg_puts(": still in use\n");
                   1169:                }
                   1170:        }
                   1171: #endif
1.3       ratchov  1172: }
                   1173:
                   1174: struct aproc_ops ctl_ops = {
                   1175:        "ctl",
                   1176:        ctl_in,
                   1177:        ctl_out,
                   1178:        ctl_eof,
                   1179:        ctl_hup,
                   1180:        ctl_newin,
                   1181:        NULL, /* newout */
                   1182:        NULL, /* ipos */
                   1183:        NULL, /* opos */
                   1184:        ctl_done
                   1185: };
                   1186:
                   1187: struct aproc *
                   1188: ctl_new(char *name)
                   1189: {
                   1190:        struct aproc *p;
1.5       ratchov  1191:        struct ctl_slot *s;
1.3       ratchov  1192:        unsigned i;
                   1193:
                   1194:        p = aproc_new(&ctl_ops, name);
1.6       ratchov  1195:        p->u.ctl.serial = 0;
1.13      ratchov  1196:        p->u.ctl.tstate = CTL_STOP;
1.5       ratchov  1197:        for (i = 0, s = p->u.ctl.slot; i < CTL_NSLOT; i++, s++) {
1.3       ratchov  1198:                p->u.ctl.slot[i].unit = i;
1.12      ratchov  1199:                p->u.ctl.slot[i].ops = NULL;
1.7       ratchov  1200:                p->u.ctl.slot[i].vol = MIDI_MAXCTL;
1.13      ratchov  1201:                p->u.ctl.slot[i].tstate = CTL_OFF;
1.6       ratchov  1202:                p->u.ctl.slot[i].serial = p->u.ctl.serial++;
1.3       ratchov  1203:                strlcpy(p->u.ctl.slot[i].name, "unknown", CTL_NAMEMAX);
                   1204:        }
1.1       ratchov  1205:        return p;
                   1206: }