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

Annotation of src/usr.bin/aucat/miofile.c, Revision 1.3

1.3     ! ratchov     1: /*     $OpenBSD: miofile.c,v 1.2 2009/09/27 11:51:20 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: #include <sys/types.h>
                     19: #include <sys/time.h>
                     20:
                     21: #include <poll.h>
                     22: #include <stdio.h>
                     23: #include <stdlib.h>
                     24: #include <string.h>
                     25: #include <sndio.h>
                     26:
                     27: #include "conf.h"
                     28: #include "file.h"
                     29: #include "miofile.h"
                     30:
                     31: struct miofile {
                     32:        struct file file;
                     33:        struct mio_hdl *hdl;
                     34: };
                     35:
                     36: void miofile_close(struct file *);
                     37: unsigned miofile_read(struct file *, unsigned char *, unsigned);
                     38: unsigned miofile_write(struct file *, unsigned char *, unsigned);
                     39: void miofile_start(struct file *);
                     40: void miofile_stop(struct file *);
                     41: int miofile_nfds(struct file *);
                     42: int miofile_pollfd(struct file *, struct pollfd *, int);
                     43: int miofile_revents(struct file *, struct pollfd *);
                     44:
                     45: struct fileops miofile_ops = {
                     46:        "mio",
                     47:        sizeof(struct miofile),
                     48:        miofile_close,
                     49:        miofile_read,
                     50:        miofile_write,
                     51:        NULL, /* start */
                     52:        NULL, /* stop */
                     53:        miofile_nfds,
                     54:        miofile_pollfd,
                     55:        miofile_revents
                     56: };
                     57:
                     58: /*
                     59:  * open the device
                     60:  */
                     61: struct miofile *
                     62: miofile_new(struct fileops *ops, char *path, int input, int output)
                     63: {
                     64:        struct mio_hdl *hdl;
                     65:        struct miofile *f;
                     66:        int mode = 0;
                     67:
                     68:        if (input)
                     69:                mode |= MIO_IN;
                     70:        if (output)
                     71:                mode |= MIO_OUT;
                     72:        hdl = mio_open(path, mode, 1);
                     73:        if (hdl == NULL)
                     74:                return NULL;
1.3     ! ratchov    75:        if (path == NULL)
        !            76:                path = "default";
        !            77:        f = (struct miofile *)file_new(ops, path, mio_nfds(hdl));
1.1       ratchov    78:        if (f == NULL)
                     79:                goto bad_close;
                     80:        f->hdl = hdl;
                     81:        return f;
                     82:  bad_close:
                     83:        mio_close(hdl);
                     84:        return NULL;
                     85: }
                     86:
                     87: unsigned
                     88: miofile_read(struct file *file, unsigned char *data, unsigned count)
                     89: {
                     90:        struct miofile *f = (struct miofile *)file;
                     91:        unsigned n;
                     92:
                     93:        n = mio_read(f->hdl, data, count);
                     94:        if (n == 0) {
                     95:                f->file.state &= ~FILE_ROK;
                     96:                if (mio_eof(f->hdl)) {
                     97:                        file_eof(&f->file);
                     98:                } else {
                     99:                }
                    100:                return 0;
                    101:        }
                    102:        return n;
                    103:
                    104: }
                    105:
                    106: unsigned
                    107: miofile_write(struct file *file, unsigned char *data, unsigned count)
                    108: {
                    109:        struct miofile *f = (struct miofile *)file;
                    110:        unsigned n;
                    111:
                    112:        n = mio_write(f->hdl, data, count);
                    113:        if (n == 0) {
                    114:                f->file.state &= ~FILE_WOK;
                    115:                if (mio_eof(f->hdl)) {
                    116:                        file_hup(&f->file);
                    117:                } else {
                    118:                }
                    119:                return 0;
                    120:        }
                    121:        return n;
                    122: }
                    123:
                    124: int
                    125: miofile_nfds(struct file *file)
                    126: {
                    127:        return mio_nfds(((struct miofile *)file)->hdl);
                    128: }
                    129:
                    130: int
                    131: miofile_pollfd(struct file *file, struct pollfd *pfd, int events)
                    132: {
                    133:        return mio_pollfd(((struct miofile *)file)->hdl, pfd, events);
                    134: }
                    135:
                    136: int
                    137: miofile_revents(struct file *file, struct pollfd *pfd)
                    138: {
                    139:        return mio_revents(((struct miofile *)file)->hdl, pfd);
                    140: }
                    141:
                    142: void
                    143: miofile_close(struct file *file)
                    144: {
                    145:        return mio_close(((struct miofile *)file)->hdl);
                    146: }