Annotation of src/usr.bin/aucat/miofile.c, Revision 1.4
1.4 ! ratchov 1: /* $OpenBSD: miofile.c,v 1.3 2009/11/05 08:36:48 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 *);
40: unsigned miofile_read(struct file *, unsigned char *, unsigned);
41: unsigned miofile_write(struct file *, unsigned char *, unsigned);
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 *
65: miofile_new(struct fileops *ops, char *path, int input, int output)
66: {
67: struct mio_hdl *hdl;
68: struct miofile *f;
69: int mode = 0;
70:
71: if (input)
72: mode |= MIO_IN;
73: if (output)
74: mode |= MIO_OUT;
75: hdl = mio_open(path, mode, 1);
76: if (hdl == NULL)
77: return NULL;
1.3 ratchov 78: if (path == NULL)
79: path = "default";
80: f = (struct miofile *)file_new(ops, path, mio_nfds(hdl));
1.1 ratchov 81: if (f == NULL)
82: goto bad_close;
83: f->hdl = hdl;
84: return f;
85: bad_close:
86: mio_close(hdl);
87: return NULL;
88: }
89:
90: unsigned
91: miofile_read(struct file *file, unsigned char *data, unsigned count)
92: {
93: struct miofile *f = (struct miofile *)file;
94: unsigned n;
95:
96: n = mio_read(f->hdl, data, count);
97: if (n == 0) {
98: f->file.state &= ~FILE_ROK;
99: if (mio_eof(f->hdl)) {
1.4 ! ratchov 100: #ifdef DEBUG
! 101: dbg_puts(f->file.name);
! 102: dbg_puts(": failed to read from device\n");
! 103: #endif
1.1 ratchov 104: file_eof(&f->file);
105: } else {
1.4 ! ratchov 106: #ifdef DEBUG
! 107: if (debug_level >= 4) {
! 108: file_dbg(&f->file);
! 109: dbg_puts(": reading blocked\n");
! 110: }
! 111: #endif
1.1 ratchov 112: }
113: return 0;
114: }
115: return n;
116:
117: }
118:
119: unsigned
120: miofile_write(struct file *file, unsigned char *data, unsigned count)
121: {
122: struct miofile *f = (struct miofile *)file;
123: unsigned n;
124:
125: n = mio_write(f->hdl, data, count);
126: if (n == 0) {
127: f->file.state &= ~FILE_WOK;
128: if (mio_eof(f->hdl)) {
1.4 ! ratchov 129: #ifdef DEBUG
! 130: dbg_puts(f->file.name);
! 131: dbg_puts(": failed to write on device\n");
! 132: #endif
1.1 ratchov 133: file_hup(&f->file);
134: } else {
1.4 ! ratchov 135: #ifdef DEBUG
! 136: if (debug_level >= 4) {
! 137: file_dbg(&f->file);
! 138: dbg_puts(": writing blocked\n");
! 139: }
! 140: #endif
1.1 ratchov 141: }
142: return 0;
143: }
144: return n;
145: }
146:
147: int
148: miofile_nfds(struct file *file)
149: {
150: return mio_nfds(((struct miofile *)file)->hdl);
151: }
152:
153: int
154: miofile_pollfd(struct file *file, struct pollfd *pfd, int events)
155: {
156: return mio_pollfd(((struct miofile *)file)->hdl, pfd, events);
157: }
158:
159: int
160: miofile_revents(struct file *file, struct pollfd *pfd)
161: {
162: return mio_revents(((struct miofile *)file)->hdl, pfd);
163: }
164:
165: void
166: miofile_close(struct file *file)
167: {
168: return mio_close(((struct miofile *)file)->hdl);
169: }