[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.9

1.9     ! ratchov     1: /*     $OpenBSD: miofile.c,v 1.8 2012/05/23 19:25:11 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"
1.4       ratchov    30: #ifdef DEBUG
                     31: #include "dbg.h"
                     32: #endif
1.1       ratchov    33:
                     34: struct miofile {
                     35:        struct file file;
                     36:        struct mio_hdl *hdl;
                     37: };
                     38:
                     39: void miofile_close(struct file *);
1.7       ratchov    40: unsigned int miofile_read(struct file *, unsigned char *, unsigned int);
                     41: unsigned int miofile_write(struct file *, unsigned char *, unsigned int);
1.1       ratchov    42: void miofile_start(struct file *);
                     43: void miofile_stop(struct file *);
                     44: int miofile_nfds(struct file *);
                     45: int miofile_pollfd(struct file *, struct pollfd *, int);
                     46: int miofile_revents(struct file *, struct pollfd *);
                     47:
                     48: struct fileops miofile_ops = {
                     49:        "mio",
                     50:        sizeof(struct miofile),
                     51:        miofile_close,
                     52:        miofile_read,
                     53:        miofile_write,
                     54:        NULL, /* start */
                     55:        NULL, /* stop */
                     56:        miofile_nfds,
                     57:        miofile_pollfd,
                     58:        miofile_revents
                     59: };
                     60:
                     61: /*
                     62:  * open the device
                     63:  */
                     64: struct miofile *
1.7       ratchov    65: miofile_new(struct fileops *ops, char *path, unsigned int mode)
1.1       ratchov    66: {
                     67:        struct mio_hdl *hdl;
                     68:        struct miofile *f;
                     69:
1.8       ratchov    70:        hdl = mio_open(path, mode, 1);
1.1       ratchov    71:        if (hdl == NULL)
                     72:                return NULL;
1.3       ratchov    73:        f = (struct miofile *)file_new(ops, path, mio_nfds(hdl));
1.1       ratchov    74:        if (f == NULL)
                     75:                goto bad_close;
                     76:        f->hdl = hdl;
                     77:        return f;
                     78:  bad_close:
                     79:        mio_close(hdl);
                     80:        return NULL;
                     81: }
                     82:
1.7       ratchov    83: unsigned int
                     84: miofile_read(struct file *file, unsigned char *data, unsigned int count)
1.1       ratchov    85: {
                     86:        struct miofile *f = (struct miofile *)file;
1.7       ratchov    87:        unsigned int n;
1.1       ratchov    88:
                     89:        n = mio_read(f->hdl, data, count);
                     90:        if (n == 0) {
                     91:                f->file.state &= ~FILE_ROK;
                     92:                if (mio_eof(f->hdl)) {
1.4       ratchov    93: #ifdef DEBUG
                     94:                        dbg_puts(f->file.name);
                     95:                        dbg_puts(": failed to read from device\n");
                     96: #endif
1.1       ratchov    97:                        file_eof(&f->file);
                     98:                } else {
1.4       ratchov    99: #ifdef DEBUG
                    100:                        if (debug_level >= 4) {
                    101:                                file_dbg(&f->file);
                    102:                                dbg_puts(": reading blocked\n");
                    103:                        }
                    104: #endif
1.1       ratchov   105:                }
                    106:                return 0;
                    107:        }
                    108:        return n;
                    109:
                    110: }
                    111:
1.7       ratchov   112: unsigned int
                    113: miofile_write(struct file *file, unsigned char *data, unsigned int count)
1.1       ratchov   114: {
                    115:        struct miofile *f = (struct miofile *)file;
1.7       ratchov   116:        unsigned int n;
1.1       ratchov   117:
                    118:        n = mio_write(f->hdl, data, count);
                    119:        if (n == 0) {
                    120:                f->file.state &= ~FILE_WOK;
                    121:                if (mio_eof(f->hdl)) {
1.4       ratchov   122: #ifdef DEBUG
                    123:                        dbg_puts(f->file.name);
                    124:                        dbg_puts(": failed to write on device\n");
                    125: #endif
1.1       ratchov   126:                        file_hup(&f->file);
                    127:                } else {
1.4       ratchov   128: #ifdef DEBUG
                    129:                        if (debug_level >= 4) {
                    130:                                file_dbg(&f->file);
                    131:                                dbg_puts(": writing blocked\n");
                    132:                        }
                    133: #endif
1.1       ratchov   134:                }
                    135:                return 0;
                    136:        }
                    137:        return n;
                    138: }
                    139:
                    140: int
                    141: miofile_nfds(struct file *file)
                    142: {
                    143:        return mio_nfds(((struct miofile *)file)->hdl);
                    144: }
                    145:
                    146: int
                    147: miofile_pollfd(struct file *file, struct pollfd *pfd, int events)
                    148: {
                    149:        return mio_pollfd(((struct miofile *)file)->hdl, pfd, events);
                    150: }
                    151:
                    152: int
                    153: miofile_revents(struct file *file, struct pollfd *pfd)
                    154: {
                    155:        return mio_revents(((struct miofile *)file)->hdl, pfd);
                    156: }
                    157:
                    158: void
                    159: miofile_close(struct file *file)
                    160: {
1.9     ! ratchov   161:        mio_close(((struct miofile *)file)->hdl);
1.1       ratchov   162: }